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> 50 #include <linux/mutex.h> 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 struct us_data *us = host_to_us(sdev->host); 76 77 /* 78 * Set the INQUIRY transfer length to 36. We don't use any of 79 * the extra data and many devices choke if asked for more or 80 * less than 36 bytes. 81 */ 82 sdev->inquiry_len = 36; 83 84 /* 85 * The UFI spec treates the Peripheral Qualifier bits in an 86 * INQUIRY result as reserved and requires devices to set them 87 * to 0. However the SCSI spec requires these bits to be set 88 * to 3 to indicate when a LUN is not present. 89 * 90 * Let the scanning code know if this target merely sets 91 * Peripheral Device Type to 0x1f to indicate no LUN. 92 */ 93 if (us->subclass == US_SC_UFI) 94 sdev->sdev_target->pdt_1f_for_no_lun = 1; 95 96 return 0; 97 } 98 99 static int slave_configure(struct scsi_device *sdev) 100 { 101 struct us_data *us = host_to_us(sdev->host); 102 103 /* Scatter-gather buffers (all but the last) must have a length 104 * divisible by the bulk maxpacket size. Otherwise a data packet 105 * would end up being short, causing a premature end to the data 106 * transfer. Since high-speed bulk pipes have a maxpacket size 107 * of 512, we'll use that as the scsi device queue's DMA alignment 108 * mask. Guaranteeing proper alignment of the first buffer will 109 * have the desired effect because, except at the beginning and 110 * the end, scatter-gather buffers follow page boundaries. */ 111 blk_queue_dma_alignment(sdev->request_queue, (512 - 1)); 112 113 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's 114 * what is originally reported. We need this to avoid confusing 115 * the SCSI layer with devices that report 0 or 1, but need 10-byte 116 * commands (ala ATAPI devices behind certain bridges, or devices 117 * which simply have broken INQUIRY data). 118 * 119 * NOTE: This means /dev/sg programs (ala cdrecord) will get the 120 * actual information. This seems to be the preference for 121 * programs like that. 122 * 123 * NOTE: This also means that /proc/scsi/scsi and sysfs may report 124 * the actual value or the modified one, depending on where the 125 * data comes from. 126 */ 127 if (sdev->scsi_level < SCSI_2) 128 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2; 129 130 /* Many devices have trouble transfering more than 32KB at a time, 131 * while others have trouble with more than 64K. At this time we 132 * are limiting both to 32K (64 sectores). 133 */ 134 if ((us->flags & US_FL_MAX_SECTORS_64) && 135 sdev->request_queue->max_sectors > 64) 136 blk_queue_max_sectors(sdev->request_queue, 64); 137 138 /* We can't put these settings in slave_alloc() because that gets 139 * called before the device type is known. Consequently these 140 * settings can't be overridden via the scsi devinfo mechanism. */ 141 if (sdev->type == TYPE_DISK) { 142 143 /* Disk-type devices use MODE SENSE(6) if the protocol 144 * (SubClass) is Transparent SCSI, otherwise they use 145 * MODE SENSE(10). */ 146 if (us->subclass != US_SC_SCSI) 147 sdev->use_10_for_ms = 1; 148 149 /* Many disks only accept MODE SENSE transfer lengths of 150 * 192 bytes (that's what Windows uses). */ 151 sdev->use_192_bytes_for_3f = 1; 152 153 /* Some devices don't like MODE SENSE with page=0x3f, 154 * which is the command used for checking if a device 155 * is write-protected. Now that we tell the sd driver 156 * to do a 192-byte transfer with this command the 157 * majority of devices work fine, but a few still can't 158 * handle it. The sd driver will simply assume those 159 * devices are write-enabled. */ 160 if (us->flags & US_FL_NO_WP_DETECT) 161 sdev->skip_ms_page_3f = 1; 162 163 /* A number of devices have problems with MODE SENSE for 164 * page x08, so we will skip it. */ 165 sdev->skip_ms_page_8 = 1; 166 167 /* Some disks return the total number of blocks in response 168 * to READ CAPACITY rather than the highest block number. 169 * If this device makes that mistake, tell the sd driver. */ 170 if (us->flags & US_FL_FIX_CAPACITY) 171 sdev->fix_capacity = 1; 172 173 /* Some devices report a SCSI revision level above 2 but are 174 * unable to handle the REPORT LUNS command (for which 175 * support is mandatory at level 3). Since we already have 176 * a Get-Max-LUN request, we won't lose much by setting the 177 * revision level down to 2. The only devices that would be 178 * affected are those with sparse LUNs. */ 179 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2; 180 181 /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable 182 * Hardware Error) when any low-level error occurs, 183 * recoverable or not. Setting this flag tells the SCSI 184 * midlayer to retry such commands, which frequently will 185 * succeed and fix the error. The worst this can lead to 186 * is an occasional series of retries that will all fail. */ 187 sdev->retry_hwerror = 1; 188 189 } else { 190 191 /* Non-disk-type devices don't need to blacklist any pages 192 * or to force 192-byte transfer lengths for MODE SENSE. 193 * But they do need to use MODE SENSE(10). */ 194 sdev->use_10_for_ms = 1; 195 } 196 197 /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM 198 * REMOVAL command, so suppress those commands. */ 199 if (us->flags & US_FL_NOT_LOCKABLE) 200 sdev->lockable = 0; 201 202 /* this is to satisfy the compiler, tho I don't think the 203 * return code is ever checked anywhere. */ 204 return 0; 205 } 206 207 /* queue a command */ 208 /* This is always called with scsi_lock(host) held */ 209 static int queuecommand(struct scsi_cmnd *srb, 210 void (*done)(struct scsi_cmnd *)) 211 { 212 struct us_data *us = host_to_us(srb->device->host); 213 214 US_DEBUGP("%s called\n", __FUNCTION__); 215 216 /* check for state-transition errors */ 217 if (us->srb != NULL) { 218 printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n", 219 __FUNCTION__, us->srb); 220 return SCSI_MLQUEUE_HOST_BUSY; 221 } 222 223 /* fail the command if we are disconnecting */ 224 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 225 US_DEBUGP("Fail command during disconnect\n"); 226 srb->result = DID_NO_CONNECT << 16; 227 done(srb); 228 return 0; 229 } 230 231 /* enqueue the command and wake up the control thread */ 232 srb->scsi_done = done; 233 us->srb = srb; 234 up(&(us->sema)); 235 236 return 0; 237 } 238 239 /*********************************************************************** 240 * Error handling functions 241 ***********************************************************************/ 242 243 /* Command timeout and abort */ 244 static int command_abort(struct scsi_cmnd *srb) 245 { 246 struct us_data *us = host_to_us(srb->device->host); 247 248 US_DEBUGP("%s called\n", __FUNCTION__); 249 250 /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING 251 * bits are protected by the host lock. */ 252 scsi_lock(us_to_host(us)); 253 254 /* Is this command still active? */ 255 if (us->srb != srb) { 256 scsi_unlock(us_to_host(us)); 257 US_DEBUGP ("-- nothing to abort\n"); 258 return FAILED; 259 } 260 261 /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if 262 * a device reset isn't already in progress (to avoid interfering 263 * with the reset). Note that we must retain the host lock while 264 * calling usb_stor_stop_transport(); otherwise it might interfere 265 * with an auto-reset that begins as soon as we release the lock. */ 266 set_bit(US_FLIDX_TIMED_OUT, &us->flags); 267 if (!test_bit(US_FLIDX_RESETTING, &us->flags)) { 268 set_bit(US_FLIDX_ABORTING, &us->flags); 269 usb_stor_stop_transport(us); 270 } 271 scsi_unlock(us_to_host(us)); 272 273 /* Wait for the aborted command to finish */ 274 wait_for_completion(&us->notify); 275 return SUCCESS; 276 } 277 278 /* This invokes the transport reset mechanism to reset the state of the 279 * device */ 280 static int device_reset(struct scsi_cmnd *srb) 281 { 282 struct us_data *us = host_to_us(srb->device->host); 283 int result; 284 285 US_DEBUGP("%s called\n", __FUNCTION__); 286 287 /* lock the device pointers and do the reset */ 288 mutex_lock(&(us->dev_mutex)); 289 result = us->transport_reset(us); 290 mutex_unlock(&us->dev_mutex); 291 292 return result < 0 ? FAILED : SUCCESS; 293 } 294 295 /* Simulate a SCSI bus reset by resetting the device's USB port. */ 296 static int bus_reset(struct scsi_cmnd *srb) 297 { 298 struct us_data *us = host_to_us(srb->device->host); 299 int result; 300 301 US_DEBUGP("%s called\n", __FUNCTION__); 302 result = usb_stor_port_reset(us); 303 return result < 0 ? FAILED : SUCCESS; 304 } 305 306 /* Report a driver-initiated device 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_device_reset(struct us_data *us) 310 { 311 int i; 312 struct Scsi_Host *host = us_to_host(us); 313 314 scsi_report_device_reset(host, 0, 0); 315 if (us->flags & US_FL_SCM_MULT_TARG) { 316 for (i = 1; i < host->max_id; ++i) 317 scsi_report_device_reset(host, 0, i); 318 } 319 } 320 321 /* Report a driver-initiated bus reset to the SCSI layer. 322 * Calling this for a SCSI-initiated reset is unnecessary but harmless. 323 * The caller must own the SCSI host lock. */ 324 void usb_stor_report_bus_reset(struct us_data *us) 325 { 326 scsi_report_bus_reset(us_to_host(us), 0); 327 } 328 329 /*********************************************************************** 330 * /proc/scsi/ functions 331 ***********************************************************************/ 332 333 /* we use this macro to help us write into the buffer */ 334 #undef SPRINTF 335 #define SPRINTF(args...) \ 336 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0) 337 338 static int proc_info (struct Scsi_Host *host, char *buffer, 339 char **start, off_t offset, int length, int inout) 340 { 341 struct us_data *us = host_to_us(host); 342 char *pos = buffer; 343 const char *string; 344 345 /* if someone is sending us data, just throw it away */ 346 if (inout) 347 return length; 348 349 /* print the controller name */ 350 SPRINTF(" Host scsi%d: usb-storage\n", host->host_no); 351 352 /* print product, vendor, and serial number strings */ 353 if (us->pusb_dev->manufacturer) 354 string = us->pusb_dev->manufacturer; 355 else if (us->unusual_dev->vendorName) 356 string = us->unusual_dev->vendorName; 357 else 358 string = "Unknown"; 359 SPRINTF(" Vendor: %s\n", string); 360 if (us->pusb_dev->product) 361 string = us->pusb_dev->product; 362 else if (us->unusual_dev->productName) 363 string = us->unusual_dev->productName; 364 else 365 string = "Unknown"; 366 SPRINTF(" Product: %s\n", string); 367 if (us->pusb_dev->serial) 368 string = us->pusb_dev->serial; 369 else 370 string = "None"; 371 SPRINTF("Serial Number: %s\n", string); 372 373 /* show the protocol and transport */ 374 SPRINTF(" Protocol: %s\n", us->protocol_name); 375 SPRINTF(" Transport: %s\n", us->transport_name); 376 377 /* show the device flags */ 378 if (pos < buffer + length) { 379 pos += sprintf(pos, " Quirks:"); 380 381 #define US_FLAG(name, value) \ 382 if (us->flags & value) pos += sprintf(pos, " " #name); 383 US_DO_ALL_FLAGS 384 #undef US_FLAG 385 386 *(pos++) = '\n'; 387 } 388 389 /* 390 * Calculate start of next buffer, and return value. 391 */ 392 *start = buffer + offset; 393 394 if ((pos - buffer) < offset) 395 return (0); 396 else if ((pos - buffer - offset) < length) 397 return (pos - buffer - offset); 398 else 399 return (length); 400 } 401 402 /*********************************************************************** 403 * Sysfs interface 404 ***********************************************************************/ 405 406 /* Output routine for the sysfs max_sectors file */ 407 static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf) 408 { 409 struct scsi_device *sdev = to_scsi_device(dev); 410 411 return sprintf(buf, "%u\n", sdev->request_queue->max_sectors); 412 } 413 414 /* Input routine for the sysfs max_sectors file */ 415 static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf, 416 size_t count) 417 { 418 struct scsi_device *sdev = to_scsi_device(dev); 419 unsigned short ms; 420 421 if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) { 422 blk_queue_max_sectors(sdev->request_queue, ms); 423 return strlen(buf); 424 } 425 return -EINVAL; 426 } 427 428 static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors, 429 store_max_sectors); 430 431 static struct device_attribute *sysfs_device_attr_list[] = { 432 &dev_attr_max_sectors, 433 NULL, 434 }; 435 436 /* 437 * this defines our host template, with which we'll allocate hosts 438 */ 439 440 struct scsi_host_template usb_stor_host_template = { 441 /* basic userland interface stuff */ 442 .name = "usb-storage", 443 .proc_name = "usb-storage", 444 .proc_info = proc_info, 445 .info = host_info, 446 447 /* command interface -- queued only */ 448 .queuecommand = queuecommand, 449 450 /* error and abort handlers */ 451 .eh_abort_handler = command_abort, 452 .eh_device_reset_handler = device_reset, 453 .eh_bus_reset_handler = bus_reset, 454 455 /* queue commands only, only one command per LUN */ 456 .can_queue = 1, 457 .cmd_per_lun = 1, 458 459 /* unknown initiator id */ 460 .this_id = -1, 461 462 .slave_alloc = slave_alloc, 463 .slave_configure = slave_configure, 464 465 /* lots of sg segments can be handled */ 466 .sg_tablesize = SG_ALL, 467 468 /* limit the total size of a transfer to 120 KB */ 469 .max_sectors = 240, 470 471 /* merge commands... this seems to help performance, but 472 * periodically someone should test to see which setting is more 473 * optimal. 474 */ 475 .use_clustering = 1, 476 477 /* emulated HBA */ 478 .emulated = 1, 479 480 /* we do our own delay after a device or bus reset */ 481 .skip_settle_delay = 1, 482 483 /* sysfs device attributes */ 484 .sdev_attrs = sysfs_device_attr_list, 485 486 /* module management */ 487 .module = THIS_MODULE 488 }; 489 490 /* To Report "Illegal Request: Invalid Field in CDB */ 491 unsigned char usb_stor_sense_invalidCDB[18] = { 492 [0] = 0x70, /* current error */ 493 [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */ 494 [7] = 0x0a, /* additional length */ 495 [12] = 0x24 /* Invalid Field in CDB */ 496 }; 497 498