Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Driver for USB Mass Storage compliant devices |
2 | * SCSI layer glue code | |
3 | * | |
4 | * $Id: scsiglue.c,v 1.26 2002/04/22 03:39:43 mdharm Exp $ | |
5 | * | |
6 | * Current development and maintenance by: | |
7 | * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) | |
8 | * | |
9 | * Developed with the assistance of: | |
10 | * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) | |
11 | * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov) | |
12 | * | |
13 | * Initial work by: | |
14 | * (c) 1999 Michael Gee (michael@linuxspecific.com) | |
15 | * | |
16 | * This driver is based on the 'USB Mass Storage Class' document. This | |
17 | * describes in detail the protocol used to communicate with such | |
18 | * devices. Clearly, the designers had SCSI and ATAPI commands in | |
19 | * mind when they created this document. The commands are all very | |
20 | * similar to commands in the SCSI-II and ATAPI specifications. | |
21 | * | |
22 | * It is important to note that in a number of cases this class | |
23 | * exhibits class-specific exemptions from the USB specification. | |
24 | * Notably the usage of NAK, STALL and ACK differs from the norm, in | |
25 | * that they are used to communicate wait, failed and OK on commands. | |
26 | * | |
27 | * Also, for certain devices, the interrupt endpoint is used to convey | |
28 | * status of a command. | |
29 | * | |
30 | * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more | |
31 | * information about this driver. | |
32 | * | |
33 | * This program is free software; you can redistribute it and/or modify it | |
34 | * under the terms of the GNU General Public License as published by the | |
35 | * Free Software Foundation; either version 2, or (at your option) any | |
36 | * later version. | |
37 | * | |
38 | * This program is distributed in the hope that it will be useful, but | |
39 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41 | * General Public License for more details. | |
42 | * | |
43 | * You should have received a copy of the GNU General Public License along | |
44 | * with this program; if not, write to the Free Software Foundation, Inc., | |
45 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
46 | */ | |
47 | ||
48 | #include <linux/slab.h> | |
49 | #include <linux/module.h> | |
4186ecf8 | 50 | #include <linux/mutex.h> |
1da177e4 LT |
51 | |
52 | #include <scsi/scsi.h> | |
53 | #include <scsi/scsi_cmnd.h> | |
54 | #include <scsi/scsi_devinfo.h> | |
55 | #include <scsi/scsi_device.h> | |
56 | #include <scsi/scsi_eh.h> | |
57 | ||
58 | #include "usb.h" | |
59 | #include "scsiglue.h" | |
60 | #include "debug.h" | |
61 | #include "transport.h" | |
62 | #include "protocol.h" | |
63 | ||
64 | /*********************************************************************** | |
65 | * Host functions | |
66 | ***********************************************************************/ | |
67 | ||
68 | static const char* host_info(struct Scsi_Host *host) | |
69 | { | |
70 | return "SCSI emulation for USB Mass Storage devices"; | |
71 | } | |
72 | ||
73 | static int slave_alloc (struct scsi_device *sdev) | |
74 | { | |
75 | /* | |
76 | * Set the INQUIRY transfer length to 36. We don't use any of | |
77 | * the extra data and many devices choke if asked for more or | |
78 | * less than 36 bytes. | |
79 | */ | |
80 | sdev->inquiry_len = 36; | |
81 | return 0; | |
82 | } | |
83 | ||
84 | static int slave_configure(struct scsi_device *sdev) | |
85 | { | |
86 | struct us_data *us = host_to_us(sdev->host); | |
87 | ||
88 | /* Scatter-gather buffers (all but the last) must have a length | |
89 | * divisible by the bulk maxpacket size. Otherwise a data packet | |
90 | * would end up being short, causing a premature end to the data | |
91 | * transfer. Since high-speed bulk pipes have a maxpacket size | |
92 | * of 512, we'll use that as the scsi device queue's DMA alignment | |
93 | * mask. Guaranteeing proper alignment of the first buffer will | |
94 | * have the desired effect because, except at the beginning and | |
95 | * the end, scatter-gather buffers follow page boundaries. */ | |
96 | blk_queue_dma_alignment(sdev->request_queue, (512 - 1)); | |
97 | ||
98 | /* Set the SCSI level to at least 2. We'll leave it at 3 if that's | |
99 | * what is originally reported. We need this to avoid confusing | |
100 | * the SCSI layer with devices that report 0 or 1, but need 10-byte | |
101 | * commands (ala ATAPI devices behind certain bridges, or devices | |
102 | * which simply have broken INQUIRY data). | |
103 | * | |
104 | * NOTE: This means /dev/sg programs (ala cdrecord) will get the | |
105 | * actual information. This seems to be the preference for | |
106 | * programs like that. | |
107 | * | |
108 | * NOTE: This also means that /proc/scsi/scsi and sysfs may report | |
109 | * the actual value or the modified one, depending on where the | |
110 | * data comes from. | |
111 | */ | |
112 | if (sdev->scsi_level < SCSI_2) | |
28120be5 | 113 | sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2; |
1da177e4 | 114 | |
883d989a PD |
115 | /* Many devices have trouble transfering more than 32KB at a time, |
116 | * while others have trouble with more than 64K. At this time we | |
117 | * are limiting both to 32K (64 sectores). | |
118 | */ | |
119 | if ((us->flags & US_FL_MAX_SECTORS_64) && | |
1da177e4 LT |
120 | sdev->request_queue->max_sectors > 64) |
121 | blk_queue_max_sectors(sdev->request_queue, 64); | |
122 | ||
123 | /* We can't put these settings in slave_alloc() because that gets | |
124 | * called before the device type is known. Consequently these | |
125 | * settings can't be overridden via the scsi devinfo mechanism. */ | |
126 | if (sdev->type == TYPE_DISK) { | |
127 | ||
128 | /* Disk-type devices use MODE SENSE(6) if the protocol | |
129 | * (SubClass) is Transparent SCSI, otherwise they use | |
130 | * MODE SENSE(10). */ | |
131 | if (us->subclass != US_SC_SCSI) | |
132 | sdev->use_10_for_ms = 1; | |
133 | ||
134 | /* Many disks only accept MODE SENSE transfer lengths of | |
135 | * 192 bytes (that's what Windows uses). */ | |
136 | sdev->use_192_bytes_for_3f = 1; | |
137 | ||
138 | /* Some devices don't like MODE SENSE with page=0x3f, | |
139 | * which is the command used for checking if a device | |
140 | * is write-protected. Now that we tell the sd driver | |
141 | * to do a 192-byte transfer with this command the | |
142 | * majority of devices work fine, but a few still can't | |
143 | * handle it. The sd driver will simply assume those | |
144 | * devices are write-enabled. */ | |
145 | if (us->flags & US_FL_NO_WP_DETECT) | |
146 | sdev->skip_ms_page_3f = 1; | |
147 | ||
148 | /* A number of devices have problems with MODE SENSE for | |
149 | * page x08, so we will skip it. */ | |
150 | sdev->skip_ms_page_8 = 1; | |
151 | ||
152 | /* Some disks return the total number of blocks in response | |
153 | * to READ CAPACITY rather than the highest block number. | |
154 | * If this device makes that mistake, tell the sd driver. */ | |
155 | if (us->flags & US_FL_FIX_CAPACITY) | |
156 | sdev->fix_capacity = 1; | |
86dbde9c | 157 | |
a4e62832 MD |
158 | /* Some devices report a SCSI revision level above 2 but are |
159 | * unable to handle the REPORT LUNS command (for which | |
160 | * support is mandatory at level 3). Since we already have | |
161 | * a Get-Max-LUN request, we won't lose much by setting the | |
162 | * revision level down to 2. The only devices that would be | |
163 | * affected are those with sparse LUNs. */ | |
28120be5 | 164 | sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2; |
a4e62832 | 165 | |
86dbde9c MD |
166 | /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable |
167 | * Hardware Error) when any low-level error occurs, | |
168 | * recoverable or not. Setting this flag tells the SCSI | |
169 | * midlayer to retry such commands, which frequently will | |
170 | * succeed and fix the error. The worst this can lead to | |
171 | * is an occasional series of retries that will all fail. */ | |
172 | sdev->retry_hwerror = 1; | |
173 | ||
1da177e4 LT |
174 | } else { |
175 | ||
176 | /* Non-disk-type devices don't need to blacklist any pages | |
177 | * or to force 192-byte transfer lengths for MODE SENSE. | |
178 | * But they do need to use MODE SENSE(10). */ | |
179 | sdev->use_10_for_ms = 1; | |
180 | } | |
181 | ||
182 | /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM | |
183 | * REMOVAL command, so suppress those commands. */ | |
184 | if (us->flags & US_FL_NOT_LOCKABLE) | |
185 | sdev->lockable = 0; | |
186 | ||
187 | /* this is to satisfy the compiler, tho I don't think the | |
188 | * return code is ever checked anywhere. */ | |
189 | return 0; | |
190 | } | |
191 | ||
192 | /* queue a command */ | |
193 | /* This is always called with scsi_lock(host) held */ | |
194 | static int queuecommand(struct scsi_cmnd *srb, | |
195 | void (*done)(struct scsi_cmnd *)) | |
196 | { | |
197 | struct us_data *us = host_to_us(srb->device->host); | |
198 | ||
199 | US_DEBUGP("%s called\n", __FUNCTION__); | |
200 | ||
201 | /* check for state-transition errors */ | |
202 | if (us->srb != NULL) { | |
203 | printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n", | |
204 | __FUNCTION__, us->srb); | |
205 | return SCSI_MLQUEUE_HOST_BUSY; | |
206 | } | |
207 | ||
208 | /* fail the command if we are disconnecting */ | |
209 | if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { | |
210 | US_DEBUGP("Fail command during disconnect\n"); | |
211 | srb->result = DID_NO_CONNECT << 16; | |
212 | done(srb); | |
213 | return 0; | |
214 | } | |
215 | ||
216 | /* enqueue the command and wake up the control thread */ | |
217 | srb->scsi_done = done; | |
218 | us->srb = srb; | |
219 | up(&(us->sema)); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | /*********************************************************************** | |
225 | * Error handling functions | |
226 | ***********************************************************************/ | |
227 | ||
228 | /* Command timeout and abort */ | |
1da177e4 LT |
229 | static int command_abort(struct scsi_cmnd *srb) |
230 | { | |
231 | struct us_data *us = host_to_us(srb->device->host); | |
232 | ||
233 | US_DEBUGP("%s called\n", __FUNCTION__); | |
234 | ||
226173ed MD |
235 | /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING |
236 | * bits are protected by the host lock. */ | |
237 | scsi_lock(us_to_host(us)); | |
238 | ||
1da177e4 LT |
239 | /* Is this command still active? */ |
240 | if (us->srb != srb) { | |
226173ed | 241 | scsi_unlock(us_to_host(us)); |
1da177e4 LT |
242 | US_DEBUGP ("-- nothing to abort\n"); |
243 | return FAILED; | |
244 | } | |
245 | ||
246 | /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if | |
247 | * a device reset isn't already in progress (to avoid interfering | |
226173ed MD |
248 | * with the reset). Note that we must retain the host lock while |
249 | * calling usb_stor_stop_transport(); otherwise it might interfere | |
250 | * with an auto-reset that begins as soon as we release the lock. */ | |
1da177e4 LT |
251 | set_bit(US_FLIDX_TIMED_OUT, &us->flags); |
252 | if (!test_bit(US_FLIDX_RESETTING, &us->flags)) { | |
253 | set_bit(US_FLIDX_ABORTING, &us->flags); | |
254 | usb_stor_stop_transport(us); | |
255 | } | |
226173ed | 256 | scsi_unlock(us_to_host(us)); |
1da177e4 LT |
257 | |
258 | /* Wait for the aborted command to finish */ | |
259 | wait_for_completion(&us->notify); | |
1da177e4 LT |
260 | return SUCCESS; |
261 | } | |
262 | ||
263 | /* This invokes the transport reset mechanism to reset the state of the | |
264 | * device */ | |
1da177e4 LT |
265 | static int device_reset(struct scsi_cmnd *srb) |
266 | { | |
267 | struct us_data *us = host_to_us(srb->device->host); | |
268 | int result; | |
269 | ||
270 | US_DEBUGP("%s called\n", __FUNCTION__); | |
271 | ||
1da177e4 | 272 | /* lock the device pointers and do the reset */ |
4186ecf8 | 273 | mutex_lock(&(us->dev_mutex)); |
4d07ef76 | 274 | result = us->transport_reset(us); |
4186ecf8 | 275 | mutex_unlock(&us->dev_mutex); |
1da177e4 | 276 | |
4d07ef76 | 277 | return result < 0 ? FAILED : SUCCESS; |
1da177e4 LT |
278 | } |
279 | ||
4d07ef76 | 280 | /* Simulate a SCSI bus reset by resetting the device's USB port. */ |
1da177e4 LT |
281 | static int bus_reset(struct scsi_cmnd *srb) |
282 | { | |
283 | struct us_data *us = host_to_us(srb->device->host); | |
4d07ef76 | 284 | int result; |
1da177e4 LT |
285 | |
286 | US_DEBUGP("%s called\n", __FUNCTION__); | |
4d07ef76 | 287 | result = usb_stor_port_reset(us); |
1da177e4 LT |
288 | return result < 0 ? FAILED : SUCCESS; |
289 | } | |
290 | ||
291 | /* Report a driver-initiated device reset to the SCSI layer. | |
292 | * Calling this for a SCSI-initiated reset is unnecessary but harmless. | |
293 | * The caller must own the SCSI host lock. */ | |
294 | void usb_stor_report_device_reset(struct us_data *us) | |
295 | { | |
296 | int i; | |
297 | struct Scsi_Host *host = us_to_host(us); | |
298 | ||
299 | scsi_report_device_reset(host, 0, 0); | |
300 | if (us->flags & US_FL_SCM_MULT_TARG) { | |
301 | for (i = 1; i < host->max_id; ++i) | |
302 | scsi_report_device_reset(host, 0, i); | |
303 | } | |
304 | } | |
305 | ||
4d07ef76 MD |
306 | /* Report a driver-initiated bus reset to the SCSI layer. |
307 | * Calling this for a SCSI-initiated reset is unnecessary but harmless. | |
308 | * The caller must own the SCSI host lock. */ | |
309 | void usb_stor_report_bus_reset(struct us_data *us) | |
310 | { | |
311 | scsi_report_bus_reset(us_to_host(us), 0); | |
312 | } | |
313 | ||
1da177e4 LT |
314 | /*********************************************************************** |
315 | * /proc/scsi/ functions | |
316 | ***********************************************************************/ | |
317 | ||
318 | /* we use this macro to help us write into the buffer */ | |
319 | #undef SPRINTF | |
320 | #define SPRINTF(args...) \ | |
321 | do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0) | |
322 | ||
323 | static int proc_info (struct Scsi_Host *host, char *buffer, | |
324 | char **start, off_t offset, int length, int inout) | |
325 | { | |
326 | struct us_data *us = host_to_us(host); | |
327 | char *pos = buffer; | |
328 | const char *string; | |
329 | ||
330 | /* if someone is sending us data, just throw it away */ | |
331 | if (inout) | |
332 | return length; | |
333 | ||
334 | /* print the controller name */ | |
335 | SPRINTF(" Host scsi%d: usb-storage\n", host->host_no); | |
336 | ||
337 | /* print product, vendor, and serial number strings */ | |
338 | if (us->pusb_dev->manufacturer) | |
339 | string = us->pusb_dev->manufacturer; | |
340 | else if (us->unusual_dev->vendorName) | |
341 | string = us->unusual_dev->vendorName; | |
342 | else | |
343 | string = "Unknown"; | |
344 | SPRINTF(" Vendor: %s\n", string); | |
345 | if (us->pusb_dev->product) | |
346 | string = us->pusb_dev->product; | |
347 | else if (us->unusual_dev->productName) | |
348 | string = us->unusual_dev->productName; | |
349 | else | |
350 | string = "Unknown"; | |
351 | SPRINTF(" Product: %s\n", string); | |
352 | if (us->pusb_dev->serial) | |
353 | string = us->pusb_dev->serial; | |
354 | else | |
355 | string = "None"; | |
356 | SPRINTF("Serial Number: %s\n", string); | |
357 | ||
358 | /* show the protocol and transport */ | |
359 | SPRINTF(" Protocol: %s\n", us->protocol_name); | |
360 | SPRINTF(" Transport: %s\n", us->transport_name); | |
361 | ||
362 | /* show the device flags */ | |
363 | if (pos < buffer + length) { | |
364 | pos += sprintf(pos, " Quirks:"); | |
365 | ||
366 | #define US_FLAG(name, value) \ | |
367 | if (us->flags & value) pos += sprintf(pos, " " #name); | |
368 | US_DO_ALL_FLAGS | |
369 | #undef US_FLAG | |
370 | ||
371 | *(pos++) = '\n'; | |
372 | } | |
373 | ||
374 | /* | |
375 | * Calculate start of next buffer, and return value. | |
376 | */ | |
377 | *start = buffer + offset; | |
378 | ||
379 | if ((pos - buffer) < offset) | |
380 | return (0); | |
381 | else if ((pos - buffer - offset) < length) | |
382 | return (pos - buffer - offset); | |
383 | else | |
384 | return (length); | |
385 | } | |
386 | ||
387 | /*********************************************************************** | |
388 | * Sysfs interface | |
389 | ***********************************************************************/ | |
390 | ||
391 | /* Output routine for the sysfs max_sectors file */ | |
060b8845 | 392 | static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
393 | { |
394 | struct scsi_device *sdev = to_scsi_device(dev); | |
395 | ||
396 | return sprintf(buf, "%u\n", sdev->request_queue->max_sectors); | |
397 | } | |
398 | ||
399 | /* Input routine for the sysfs max_sectors file */ | |
060b8845 | 400 | static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf, |
1da177e4 LT |
401 | size_t count) |
402 | { | |
403 | struct scsi_device *sdev = to_scsi_device(dev); | |
404 | unsigned short ms; | |
405 | ||
406 | if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) { | |
407 | blk_queue_max_sectors(sdev->request_queue, ms); | |
408 | return strlen(buf); | |
409 | } | |
410 | return -EINVAL; | |
411 | } | |
412 | ||
413 | static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors, | |
414 | store_max_sectors); | |
415 | ||
416 | static struct device_attribute *sysfs_device_attr_list[] = { | |
417 | &dev_attr_max_sectors, | |
418 | NULL, | |
419 | }; | |
420 | ||
421 | /* | |
422 | * this defines our host template, with which we'll allocate hosts | |
423 | */ | |
424 | ||
425 | struct scsi_host_template usb_stor_host_template = { | |
426 | /* basic userland interface stuff */ | |
427 | .name = "usb-storage", | |
428 | .proc_name = "usb-storage", | |
429 | .proc_info = proc_info, | |
430 | .info = host_info, | |
431 | ||
432 | /* command interface -- queued only */ | |
433 | .queuecommand = queuecommand, | |
434 | ||
435 | /* error and abort handlers */ | |
436 | .eh_abort_handler = command_abort, | |
437 | .eh_device_reset_handler = device_reset, | |
438 | .eh_bus_reset_handler = bus_reset, | |
439 | ||
440 | /* queue commands only, only one command per LUN */ | |
441 | .can_queue = 1, | |
442 | .cmd_per_lun = 1, | |
443 | ||
444 | /* unknown initiator id */ | |
445 | .this_id = -1, | |
446 | ||
447 | .slave_alloc = slave_alloc, | |
448 | .slave_configure = slave_configure, | |
449 | ||
450 | /* lots of sg segments can be handled */ | |
451 | .sg_tablesize = SG_ALL, | |
452 | ||
453 | /* limit the total size of a transfer to 120 KB */ | |
454 | .max_sectors = 240, | |
455 | ||
456 | /* merge commands... this seems to help performance, but | |
457 | * periodically someone should test to see which setting is more | |
458 | * optimal. | |
459 | */ | |
460 | .use_clustering = 1, | |
461 | ||
462 | /* emulated HBA */ | |
463 | .emulated = 1, | |
464 | ||
465 | /* we do our own delay after a device or bus reset */ | |
466 | .skip_settle_delay = 1, | |
467 | ||
468 | /* sysfs device attributes */ | |
469 | .sdev_attrs = sysfs_device_attr_list, | |
470 | ||
471 | /* module management */ | |
472 | .module = THIS_MODULE | |
473 | }; | |
474 | ||
475 | /* To Report "Illegal Request: Invalid Field in CDB */ | |
476 | unsigned char usb_stor_sense_invalidCDB[18] = { | |
477 | [0] = 0x70, /* current error */ | |
478 | [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */ | |
479 | [7] = 0x0a, /* additional length */ | |
480 | [12] = 0x24 /* Invalid Field in CDB */ | |
481 | }; | |
482 |