1 /* Driver for USB Mass Storage compliant devices
4 * $Id: scsiglue.c,v 1.26 2002/04/22 03:39:43 mdharm Exp $
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
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)
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
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.
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.
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
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
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.
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.
48 #include <linux/slab.h>
49 #include <linux/module.h>
50 #include <linux/mutex.h>
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>
61 #include "transport.h"
64 /***********************************************************************
66 ***********************************************************************/
68 static const char* host_info(struct Scsi_Host *host)
70 return "SCSI emulation for USB Mass Storage devices";
73 static int slave_alloc (struct scsi_device *sdev)
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
80 sdev->inquiry_len = 36;
84 static int slave_configure(struct scsi_device *sdev)
86 struct us_data *us = host_to_us(sdev->host);
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));
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).
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.
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
112 if (sdev->scsi_level < SCSI_2)
113 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
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).
119 if ((us->flags & US_FL_MAX_SECTORS_64) &&
120 sdev->request_queue->max_sectors > 64)
121 blk_queue_max_sectors(sdev->request_queue, 64);
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) {
128 /* Disk-type devices use MODE SENSE(6) if the protocol
129 * (SubClass) is Transparent SCSI, otherwise they use
131 if (us->subclass != US_SC_SCSI)
132 sdev->use_10_for_ms = 1;
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;
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;
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;
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;
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. */
164 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
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;
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;
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)
187 /* this is to satisfy the compiler, tho I don't think the
188 * return code is ever checked anywhere. */
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 *))
197 struct us_data *us = host_to_us(srb->device->host);
199 US_DEBUGP("%s called\n", __FUNCTION__);
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;
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;
216 /* enqueue the command and wake up the control thread */
217 srb->scsi_done = done;
224 /***********************************************************************
225 * Error handling functions
226 ***********************************************************************/
228 /* Command timeout and abort */
229 static int command_abort(struct scsi_cmnd *srb)
231 struct us_data *us = host_to_us(srb->device->host);
233 US_DEBUGP("%s called\n", __FUNCTION__);
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));
239 /* Is this command still active? */
240 if (us->srb != srb) {
241 scsi_unlock(us_to_host(us));
242 US_DEBUGP ("-- nothing to abort\n");
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
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. */
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);
256 scsi_unlock(us_to_host(us));
258 /* Wait for the aborted command to finish */
259 wait_for_completion(&us->notify);
263 /* This invokes the transport reset mechanism to reset the state of the
265 static int device_reset(struct scsi_cmnd *srb)
267 struct us_data *us = host_to_us(srb->device->host);
270 US_DEBUGP("%s called\n", __FUNCTION__);
272 /* lock the device pointers and do the reset */
273 mutex_lock(&(us->dev_mutex));
274 result = us->transport_reset(us);
275 mutex_unlock(&us->dev_mutex);
277 return result < 0 ? FAILED : SUCCESS;
280 /* Simulate a SCSI bus reset by resetting the device's USB port. */
281 static int bus_reset(struct scsi_cmnd *srb)
283 struct us_data *us = host_to_us(srb->device->host);
286 US_DEBUGP("%s called\n", __FUNCTION__);
287 result = usb_stor_port_reset(us);
288 return result < 0 ? FAILED : SUCCESS;
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)
297 struct Scsi_Host *host = us_to_host(us);
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);
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)
311 scsi_report_bus_reset(us_to_host(us), 0);
314 /***********************************************************************
315 * /proc/scsi/ functions
316 ***********************************************************************/
318 /* we use this macro to help us write into the buffer */
320 #define SPRINTF(args...) \
321 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
323 static int proc_info (struct Scsi_Host *host, char *buffer,
324 char **start, off_t offset, int length, int inout)
326 struct us_data *us = host_to_us(host);
330 /* if someone is sending us data, just throw it away */
334 /* print the controller name */
335 SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
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;
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;
351 SPRINTF(" Product: %s\n", string);
352 if (us->pusb_dev->serial)
353 string = us->pusb_dev->serial;
356 SPRINTF("Serial Number: %s\n", string);
358 /* show the protocol and transport */
359 SPRINTF(" Protocol: %s\n", us->protocol_name);
360 SPRINTF(" Transport: %s\n", us->transport_name);
362 /* show the device flags */
363 if (pos < buffer + length) {
364 pos += sprintf(pos, " Quirks:");
366 #define US_FLAG(name, value) \
367 if (us->flags & value) pos += sprintf(pos, " " #name);
375 * Calculate start of next buffer, and return value.
377 *start = buffer + offset;
379 if ((pos - buffer) < offset)
381 else if ((pos - buffer - offset) < length)
382 return (pos - buffer - offset);
387 /***********************************************************************
389 ***********************************************************************/
391 /* Output routine for the sysfs max_sectors file */
392 static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
394 struct scsi_device *sdev = to_scsi_device(dev);
396 return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
399 /* Input routine for the sysfs max_sectors file */
400 static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
403 struct scsi_device *sdev = to_scsi_device(dev);
406 if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
407 blk_queue_max_sectors(sdev->request_queue, ms);
413 static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
416 static struct device_attribute *sysfs_device_attr_list[] = {
417 &dev_attr_max_sectors,
422 * this defines our host template, with which we'll allocate hosts
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,
432 /* command interface -- queued only */
433 .queuecommand = queuecommand,
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,
440 /* queue commands only, only one command per LUN */
444 /* unknown initiator id */
447 .slave_alloc = slave_alloc,
448 .slave_configure = slave_configure,
450 /* lots of sg segments can be handled */
451 .sg_tablesize = SG_ALL,
453 /* limit the total size of a transfer to 120 KB */
456 /* merge commands... this seems to help performance, but
457 * periodically someone should test to see which setting is more
465 /* we do our own delay after a device or bus reset */
466 .skip_settle_delay = 1,
468 /* sysfs device attributes */
469 .sdev_attrs = sysfs_device_attr_list,
471 /* module management */
472 .module = THIS_MODULE
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 */