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