Merge tag 'xtensa-next-20130225' of git://github.com/czankel/xtensa-linux
[deliverable/linux.git] / drivers / staging / keucr / scsiglue.c
CommitLineData
beddfe84
CYC
1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
126bb03b
AC
3#include <linux/slab.h>
4#include <linux/module.h>
5#include <linux/mutex.h>
6
7#include <scsi/scsi.h>
8#include <scsi/scsi_cmnd.h>
9#include <scsi/scsi_devinfo.h>
10#include <scsi/scsi_device.h>
11#include <scsi/scsi_eh.h>
12
13#include "usb.h"
14#include "scsiglue.h"
15#include "transport.h"
16
17/* Host functions */
8a25a2cf
CYC
18/*
19 * host_info()
20 */
21static const char *host_info(struct Scsi_Host *host)
126bb03b 22{
beddfe84 23 /* pr_info("scsiglue --- host_info\n"); */
126bb03b
AC
24 return "SCSI emulation for USB Mass Storage devices";
25}
26
8a25a2cf
CYC
27/*
28 * slave_alloc()
29 */
126bb03b
AC
30static int slave_alloc(struct scsi_device *sdev)
31{
32 struct us_data *us = host_to_us(sdev->host);
33
beddfe84 34 /* pr_info("scsiglue --- slave_alloc\n"); */
126bb03b
AC
35 sdev->inquiry_len = 36;
36
37 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
38
bceadddd 39 if (us->subclass == USB_SC_UFI)
126bb03b
AC
40 sdev->sdev_target->pdt_1f_for_no_lun = 1;
41
42 return 0;
43}
44
8a25a2cf
CYC
45/*
46 * slave_configure()
47 */
126bb03b
AC
48static int slave_configure(struct scsi_device *sdev)
49{
50 struct us_data *us = host_to_us(sdev->host);
51
beddfe84 52 /* pr_info("scsiglue --- slave_configure\n"); */
8a25a2cf 53 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
126bb03b
AC
54 unsigned int max_sectors = 64;
55
56 if (us->fflags & US_FL_MAX_SECTORS_MIN)
57 max_sectors = PAGE_CACHE_SIZE >> 9;
58 if (queue_max_sectors(sdev->request_queue) > max_sectors)
59 blk_queue_max_hw_sectors(sdev->request_queue,
60 max_sectors);
61 }
62
8a25a2cf
CYC
63 if (sdev->type == TYPE_DISK) {
64 if (us->subclass != USB_SC_SCSI &&
65 us->subclass != USB_SC_CYP_ATACB)
126bb03b
AC
66 sdev->use_10_for_ms = 1;
67 sdev->use_192_bytes_for_3f = 1;
68 if (us->fflags & US_FL_NO_WP_DETECT)
69 sdev->skip_ms_page_3f = 1;
70 sdev->skip_ms_page_8 = 1;
71 if (us->fflags & US_FL_FIX_CAPACITY)
72 sdev->fix_capacity = 1;
73 if (us->fflags & US_FL_CAPACITY_HEURISTICS)
74 sdev->guess_capacity = 1;
75 if (sdev->scsi_level > SCSI_2)
76 sdev->sdev_target->scsi_level = sdev->scsi_level = SCSI_2;
77 sdev->retry_hwerror = 1;
78 sdev->allow_restart = 1;
79 sdev->last_sector_bug = 1;
8a25a2cf 80 } else {
126bb03b
AC
81 sdev->use_10_for_ms = 1;
82 }
83
8a25a2cf
CYC
84 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
85 sdev->scsi_level == SCSI_UNKNOWN)
126bb03b
AC
86 us->max_lun = 0;
87
88 if (us->fflags & US_FL_NOT_LOCKABLE)
89 sdev->lockable = 0;
90
91 return 0;
92}
93
94/* This is always called with scsi_lock(host) held */
8a25a2cf
CYC
95/*
96 * queuecommand()
97 */
98static int queuecommand_lck(struct scsi_cmnd *srb,
99 void (*done)(struct scsi_cmnd *))
126bb03b
AC
100{
101 struct us_data *us = host_to_us(srb->device->host);
102
beddfe84 103 /* pr_info("scsiglue --- queuecommand\n"); */
126bb03b
AC
104
105 /* check for state-transition errors */
8a25a2cf 106 if (us->srb != NULL) {
beddfe84
CYC
107 /* pr_info("Error in %s: us->srb = %p\n"
108 __FUNCTION__, us->srb); */
126bb03b
AC
109 return SCSI_MLQUEUE_HOST_BUSY;
110 }
111
112 /* fail the command if we are disconnecting */
8a25a2cf 113 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
beddfe84 114 pr_info("Fail command during disconnect\n");
126bb03b
AC
115 srb->result = DID_NO_CONNECT << 16;
116 done(srb);
117 return 0;
118 }
119
120 /* enqueue the command and wake up the control thread */
121 srb->scsi_done = done;
122 us->srb = srb;
123 complete(&us->cmnd_ready);
124
125 return 0;
126}
127
f281233d
JG
128static DEF_SCSI_QCMD(queuecommand)
129
126bb03b
AC
130/***********************************************************************
131 * Error handling functions
132 ***********************************************************************/
133
134/* Command timeout and abort */
8a25a2cf
CYC
135/*
136 * command_abort()
137 */
126bb03b
AC
138static int command_abort(struct scsi_cmnd *srb)
139{
140 struct us_data *us = host_to_us(srb->device->host);
141
beddfe84 142 /* pr_info("scsiglue --- command_abort\n"); */
126bb03b
AC
143
144 scsi_lock(us_to_host(us));
8a25a2cf 145 if (us->srb != srb) {
126bb03b 146 scsi_unlock(us_to_host(us));
581cd7ee 147 printk("-- nothing to abort\n");
126bb03b
AC
148 return FAILED;
149 }
150
151 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
8a25a2cf 152 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
126bb03b
AC
153 set_bit(US_FLIDX_ABORTING, &us->dflags);
154 usb_stor_stop_transport(us);
155 }
156 scsi_unlock(us_to_host(us));
157
158 /* Wait for the aborted command to finish */
159 wait_for_completion(&us->notify);
160 return SUCCESS;
161}
162
8a25a2cf
CYC
163/* This invokes the transport reset mechanism to reset the state of the
164 * device.
165 */
166/*
167 * device_reset()
168 */
126bb03b
AC
169static int device_reset(struct scsi_cmnd *srb)
170{
171 struct us_data *us = host_to_us(srb->device->host);
172 int result;
173
beddfe84 174 /* pr_info("scsiglue --- device_reset\n"); */
126bb03b
AC
175
176 /* lock the device pointers and do the reset */
177 mutex_lock(&(us->dev_mutex));
178 result = us->transport_reset(us);
179 mutex_unlock(&us->dev_mutex);
180
181 return result < 0 ? FAILED : SUCCESS;
182}
183
8a25a2cf
CYC
184/*
185 * bus_reset()
186 */
126bb03b
AC
187static int bus_reset(struct scsi_cmnd *srb)
188{
189 struct us_data *us = host_to_us(srb->device->host);
190 int result;
191
beddfe84 192 /* pr_info("scsiglue --- bus_reset\n"); */
126bb03b
AC
193 result = usb_stor_port_reset(us);
194 return result < 0 ? FAILED : SUCCESS;
195}
196
8a25a2cf
CYC
197/*
198 * usb_stor_report_device_reset()
199 */
126bb03b
AC
200void usb_stor_report_device_reset(struct us_data *us)
201{
202 int i;
203 struct Scsi_Host *host = us_to_host(us);
204
beddfe84 205 /* pr_info("scsiglue --- usb_stor_report_device_reset\n"); */
126bb03b 206 scsi_report_device_reset(host, 0, 0);
8a25a2cf 207 if (us->fflags & US_FL_SCM_MULT_TARG) {
126bb03b
AC
208 for (i = 1; i < host->max_id; ++i)
209 scsi_report_device_reset(host, 0, i);
210 }
211}
212
8a25a2cf
CYC
213/*
214 * usb_stor_report_bus_reset()
215 */
126bb03b
AC
216void usb_stor_report_bus_reset(struct us_data *us)
217{
218 struct Scsi_Host *host = us_to_host(us);
219
beddfe84 220 /* pr_info("scsiglue --- usb_stor_report_bus_reset\n"); */
126bb03b
AC
221 scsi_lock(host);
222 scsi_report_bus_reset(host, 0);
223 scsi_unlock(host);
224}
225
226/***********************************************************************
227 * /proc/scsi/ functions
228 ***********************************************************************/
229
230/* we use this macro to help us write into the buffer */
231#undef SPRINTF
232#define SPRINTF(args...) \
a2c49a9a
WB
233 do { \
234 if (pos < buffer+length) \
235 pos += sprintf(pos, ## args); \
236 } while (0)
126bb03b 237
8a25a2cf
CYC
238/*
239 * proc_info()
240 */
241static int proc_info(struct Scsi_Host *host, char *buffer, char **start,
242 off_t offset, int length, int inout)
126bb03b
AC
243{
244 struct us_data *us = host_to_us(host);
245 char *pos = buffer;
246 const char *string;
247
beddfe84 248 /* pr_info("scsiglue --- proc_info\n"); */
126bb03b
AC
249 if (inout)
250 return length;
251
252 /* print the controller name */
253 SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
254
255 /* print product, vendor, and serial number strings */
256 if (us->pusb_dev->manufacturer)
257 string = us->pusb_dev->manufacturer;
258 else if (us->unusual_dev->vendorName)
259 string = us->unusual_dev->vendorName;
260 else
261 string = "Unknown";
262 SPRINTF(" Vendor: %s\n", string);
263 if (us->pusb_dev->product)
264 string = us->pusb_dev->product;
265 else if (us->unusual_dev->productName)
266 string = us->unusual_dev->productName;
267 else
268 string = "Unknown";
269 SPRINTF(" Product: %s\n", string);
270 if (us->pusb_dev->serial)
271 string = us->pusb_dev->serial;
272 else
273 string = "None";
274 SPRINTF("Serial Number: %s\n", string);
275
276 /* show the protocol and transport */
277 SPRINTF(" Protocol: %s\n", us->protocol_name);
278 SPRINTF(" Transport: %s\n", us->transport_name);
279
280 /* show the device flags */
8a25a2cf 281 if (pos < buffer + length) {
126bb03b
AC
282 pos += sprintf(pos, " Quirks:");
283
284#define US_FLAG(name, value) \
a2c49a9a
WB
285 do { \
286 if (us->fflags & value) \
287 pos += sprintf(pos, " " #name); \
288 } while (0);
126bb03b
AC
289US_DO_ALL_FLAGS
290#undef US_FLAG
291
292 *(pos++) = '\n';
293 }
294
295 /* Calculate start of next buffer, and return value. */
296 *start = buffer + offset;
297
298 if ((pos - buffer) < offset)
8a25a2cf 299 return 0;
126bb03b 300 else if ((pos - buffer - offset) < length)
8a25a2cf 301 return pos - buffer - offset;
126bb03b 302 else
8a25a2cf 303 return length;
126bb03b
AC
304}
305
306/***********************************************************************
307 * Sysfs interface
308 ***********************************************************************/
309
310/* Output routine for the sysfs max_sectors file */
8a25a2cf
CYC
311/*
312 * show_max_sectors()
313 */
314static ssize_t show_max_sectors(struct device *dev,
315 struct device_attribute *attr, char *buf)
126bb03b
AC
316{
317 struct scsi_device *sdev = to_scsi_device(dev);
318
beddfe84 319 /* pr_info("scsiglue --- ssize_t show_max_sectors\n"); */
126bb03b
AC
320 return sprintf(buf, "%u\n", queue_max_sectors(sdev->request_queue));
321}
322
323/* Input routine for the sysfs max_sectors file */
8a25a2cf
CYC
324/*
325 * store_max_sectors()
326 */
327static ssize_t store_max_sectors(struct device *dev,
328 struct device_attribute *attr,
329 const char *buf, size_t count)
126bb03b
AC
330{
331 struct scsi_device *sdev = to_scsi_device(dev);
332 unsigned short ms;
333
beddfe84 334 /* pr_info("scsiglue --- ssize_t store_max_sectors\n"); */
8a25a2cf 335 if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
126bb03b
AC
336 blk_queue_max_hw_sectors(sdev->request_queue, ms);
337 return strlen(buf);
338 }
8a25a2cf 339 return -EINVAL;
126bb03b
AC
340}
341
342static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors, store_max_sectors);
343static struct device_attribute *sysfs_device_attr_list[] = {&dev_attr_max_sectors, NULL, };
344
345/* this defines our host template, with which we'll allocate hosts */
346
8a25a2cf
CYC
347/*
348 * usb_stor_host_template()
349 */
126bb03b
AC
350struct scsi_host_template usb_stor_host_template = {
351 /* basic userland interface stuff */
352 .name = "eucr-storage",
353 .proc_name = "eucr-storage",
354 .proc_info = proc_info,
355 .info = host_info,
356
357 /* command interface -- queued only */
358 .queuecommand = queuecommand,
359
360 /* error and abort handlers */
361 .eh_abort_handler = command_abort,
362 .eh_device_reset_handler = device_reset,
363 .eh_bus_reset_handler = bus_reset,
364
365 /* queue commands only, only one command per LUN */
366 .can_queue = 1,
367 .cmd_per_lun = 1,
368
369 /* unknown initiator id */
370 .this_id = -1,
371
372 .slave_alloc = slave_alloc,
373 .slave_configure = slave_configure,
374
375 /* lots of sg segments can be handled */
376 .sg_tablesize = SG_ALL,
377
378 /* limit the total size of a transfer to 120 KB */
379 .max_sectors = 240,
380
381 /* merge commands... this seems to help performance, but
382 * periodically someone should test to see which setting is more
383 * optimal.
384 */
385 .use_clustering = 1,
386
387 /* emulated HBA */
388 .emulated = 1,
389
390 /* we do our own delay after a device or bus reset */
391 .skip_settle_delay = 1,
392
393 /* sysfs device attributes */
394 .sdev_attrs = sysfs_device_attr_list,
395
396 /* module management */
397 .module = THIS_MODULE
398};
399
400/* To Report "Illegal Request: Invalid Field in CDB */
401unsigned char usb_stor_sense_invalidCDB[18] = {
402 [0] = 0x70, /* current error */
403 [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
404 [7] = 0x0a, /* additional length */
405 [12] = 0x24 /* Invalid Field in CDB */
406};
407
408/***********************************************************************
409 * Scatter-gather transfer buffer access routines
410 ***********************************************************************/
411
8a25a2cf
CYC
412/*
413 * usb_stor_access_xfer_buf()
414 */
126bb03b
AC
415unsigned int usb_stor_access_xfer_buf(struct us_data *us, unsigned char *buffer,
416 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
417 unsigned int *offset, enum xfer_buf_dir dir)
418{
419 unsigned int cnt;
420
beddfe84 421 /* pr_info("transport --- usb_stor_access_xfer_buf\n"); */
126bb03b
AC
422 struct scatterlist *sg = *sgptr;
423
424 if (!sg)
425 sg = scsi_sglist(srb);
426
427 cnt = 0;
8a25a2cf
CYC
428 while (cnt < buflen && sg) {
429 struct page *page = sg_page(sg) +
430 ((sg->offset + *offset) >> PAGE_SHIFT);
126bb03b
AC
431 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
432 unsigned int sglen = sg->length - *offset;
433
8a25a2cf 434 if (sglen > buflen - cnt) {
126bb03b
AC
435 /* Transfer ends within this s-g entry */
436 sglen = buflen - cnt;
437 *offset += sglen;
8a25a2cf 438 } else {
126bb03b
AC
439 /* Transfer continues to next s-g entry */
440 *offset = 0;
441 sg = sg_next(sg);
442 }
443
8a25a2cf
CYC
444 while (sglen > 0) {
445 unsigned int plen = min(sglen,
446 (unsigned int)PAGE_SIZE - poff);
126bb03b
AC
447 unsigned char *ptr = kmap(page);
448
449 if (dir == TO_XFER_BUF)
450 memcpy(ptr + poff, buffer + cnt, plen);
451 else
452 memcpy(buffer + cnt, ptr + poff, plen);
453 kunmap(page);
454
455 /* Start at the beginning of the next page */
456 poff = 0;
457 ++page;
458 cnt += plen;
459 sglen -= plen;
460 }
461 }
462 *sgptr = sg;
463
464 /* Return the amount actually transferred */
465 return cnt;
466}
467
8a25a2cf
CYC
468/*
469 * Store the contents of buffer into srb's transfer
470 * buffer and set the SCSI residue.
471 */
472/*
473 * usb_stor_set_xfer_buf()
474 */
475void usb_stor_set_xfer_buf(struct us_data *us, unsigned char *buffer,
476 unsigned int buflen, struct scsi_cmnd *srb, unsigned int dir)
126bb03b
AC
477{
478 unsigned int offset = 0;
479 struct scatterlist *sg = NULL;
480
beddfe84 481 /* pr_info("transport --- usb_stor_set_xfer_buf\n"); */
8a25a2cf 482 /* TO_XFER_BUF = 0, FROM_XFER_BUF = 1 */
126bb03b 483 buflen = min(buflen, scsi_bufflen(srb));
8a25a2cf
CYC
484 buflen = usb_stor_access_xfer_buf(us, buffer, buflen, srb,
485 &sg, &offset, dir);
126bb03b
AC
486 if (buflen < scsi_bufflen(srb))
487 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
488}
This page took 0.374068 seconds and 5 git commands to generate.