scsiglue.c revision 00fa43ef09c6bb357d58c14a99181cce09c315c6
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Driver for USB Mass Storage compliant devices 21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * SCSI layer glue code 31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Current development and maintenance by: 51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Developed with the assistance of: 81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) 91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov) 101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Initial work by: 121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (c) 1999 Michael Gee (michael@linuxspecific.com) 131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This driver is based on the 'USB Mass Storage Class' document. This 151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * describes in detail the protocol used to communicate with such 161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * devices. Clearly, the designers had SCSI and ATAPI commands in 171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * mind when they created this document. The commands are all very 181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * similar to commands in the SCSI-II and ATAPI specifications. 191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * It is important to note that in a number of cases this class 211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * exhibits class-specific exemptions from the USB specification. 221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notably the usage of NAK, STALL and ACK differs from the norm, in 231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that they are used to communicate wait, failed and OK on commands. 241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Also, for certain devices, the interrupt endpoint is used to convey 261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * status of a command. 271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more 291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * information about this driver. 301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify it 321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * under the terms of the GNU General Public License as published by the 331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Free Software Foundation; either version 2, or (at your option) any 341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * later version. 351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is distributed in the hope that it will be useful, but 371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of 381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * General Public License for more details. 401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * You should have received a copy of the GNU General Public License along 421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc., 431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA. 441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h> 471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h> 484186ecf8ad16dd05759a09594de6a87e48759ba6Arjan van de Ven#include <linux/mutex.h> 491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi.h> 511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_cmnd.h> 521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_devinfo.h> 531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h> 541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_eh.h> 551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "usb.h" 571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsiglue.h" 581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "debug.h" 591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "transport.h" 601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "protocol.h" 611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 62a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern/* Vendor IDs for companies that seem to include the READ CAPACITY bug 63a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * in all their devices 64a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern */ 65a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_NOKIA 0x0421 66a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_NIKON 0x04b0 67506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern#define VENDOR_ID_PENTAX 0x0a17 68a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_MOTOROLA 0x22b8 69a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern 701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*********************************************************************** 711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host functions 721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/ 731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char* host_info(struct Scsi_Host *host) 751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 7600fa43ef09c6bb357d58c14a99181cce09c315c6Matthew Wilcox struct us_data *us = host_to_us(host); 7700fa43ef09c6bb357d58c14a99181cce09c315c6Matthew Wilcox return us->scsi_name; 781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_alloc (struct scsi_device *sdev) 811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 823a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern struct us_data *us = host_to_us(sdev->host); 833a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern 841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Set the INQUIRY transfer length to 36. We don't use any of 861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the extra data and many devices choke if asked for more or 871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * less than 36 bytes. 881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->inquiry_len = 36; 903a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern 91f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern /* USB has unusual DMA-alignment requirements: Although the 92f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * starting address of each scatter-gather element doesn't matter, 93f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * the length of each element except the last must be divisible 94f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * by the Bulk maxpacket value. There's currently no way to 95f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * express this by block-layer constraints, so we'll cop out 96f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * and simply require addresses to be aligned at 512-byte 97f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * boundaries. This is okay since most block I/O involves 98f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * hardware sectors that are multiples of 512 bytes in length, 99f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * and since host controllers up through USB 2.0 have maxpacket 100f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * values no larger than 512. 101f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * 102f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * But it doesn't suffice for Wireless USB, where Bulk maxpacket 103f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * values can be as large as 2048. To make that work properly 104f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern * will require changes to the block layer. 105148d9fe4c91a6356dae1b05b76b8133586c26be4Alan Stern */ 106f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1)); 107465ff3185e0cb76d46137335a4d21d0d9d3ac8a2James Bottomley 1083a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern /* 1093a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * The UFI spec treates the Peripheral Qualifier bits in an 1103a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * INQUIRY result as reserved and requires devices to set them 1113a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * to 0. However the SCSI spec requires these bits to be set 1123a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * to 3 to indicate when a LUN is not present. 1133a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * 1143a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * Let the scanning code know if this target merely sets 1153a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern * Peripheral Device Type to 0x1f to indicate no LUN. 1163a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern */ 1173a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern if (us->subclass == US_SC_UFI) 1183a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern sdev->sdev_target->pdt_1f_for_no_lun = 1; 1193a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern 1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_configure(struct scsi_device *sdev) 1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct us_data *us = host_to_us(sdev->host); 1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 127883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz /* Many devices have trouble transfering more than 32KB at a time, 128883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz * while others have trouble with more than 64K. At this time we 129883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz * are limiting both to 32K (64 sectores). 130883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz */ 1317e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) { 13233abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey unsigned int max_sectors = 64; 13333abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey 1347e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & US_FL_MAX_SECTORS_MIN) 13533abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey max_sectors = PAGE_CACHE_SIZE >> 9; 136ae03bf639a5027d27270123f5f6e3ee6a412781dMartin K. Petersen if (queue_max_sectors(sdev->request_queue) > max_sectors) 13733abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey blk_queue_max_sectors(sdev->request_queue, 13833abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey max_sectors); 1395c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern } else if (sdev->type == TYPE_TAPE) { 1405c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern /* Tapes need much higher max_sector limits, so just 1415c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern * raise it to the maximum possible (4 GB / 512) and 1425c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern * let the queue segment size sort out the real limit. 1435c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern */ 1445c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern blk_queue_max_sectors(sdev->request_queue, 0x7FFFFF); 14533abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey } 1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 14796983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern /* Some USB host controllers can't do DMA; they have to use PIO. 14896983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern * They indicate this by setting their dma_mask to NULL. For 14996983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern * such controllers we need to make sure the block layer sets 15096983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern * up bounce buffers in addressable memory. 15196983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern */ 15296983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern if (!us->pusb_dev->bus->controller->dma_mask) 15396983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH); 15496983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern 1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* We can't put these settings in slave_alloc() because that gets 1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * called before the device type is known. Consequently these 1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * settings can't be overridden via the scsi devinfo mechanism. */ 1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (sdev->type == TYPE_DISK) { 1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 160a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern /* Some vendors seem to put the READ CAPACITY bug into 161a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * all their devices -- primarily makers of cell phones 162a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * and digital cameras. Since these devices always use 163a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * flash media and can be expected to have an even number 164a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * of sectors, we will always enable the CAPACITY_HEURISTICS 165a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * flag unless told otherwise. */ 166a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) { 167a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern case VENDOR_ID_NOKIA: 168a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern case VENDOR_ID_NIKON: 169506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern case VENDOR_ID_PENTAX: 170a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern case VENDOR_ID_MOTOROLA: 171a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern if (!(us->fflags & (US_FL_FIX_CAPACITY | 172a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern US_FL_CAPACITY_OK))) 173a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern us->fflags |= US_FL_CAPACITY_HEURISTICS; 174a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern break; 175a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern } 176a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern 1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Disk-type devices use MODE SENSE(6) if the protocol 1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (SubClass) is Transparent SCSI, otherwise they use 1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MODE SENSE(10). */ 180d277064e7e16d02e0078a6bc1820764ae00dea87Matthieu CASTET if (us->subclass != US_SC_SCSI && us->subclass != US_SC_CYP_ATACB) 1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->use_10_for_ms = 1; 1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Many disks only accept MODE SENSE transfer lengths of 1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 192 bytes (that's what Windows uses). */ 1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->use_192_bytes_for_3f = 1; 1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Some devices don't like MODE SENSE with page=0x3f, 1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * which is the command used for checking if a device 1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is write-protected. Now that we tell the sd driver 1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * to do a 192-byte transfer with this command the 1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * majority of devices work fine, but a few still can't 1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * handle it. The sd driver will simply assume those 1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * devices are write-enabled. */ 1947e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & US_FL_NO_WP_DETECT) 1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->skip_ms_page_3f = 1; 1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* A number of devices have problems with MODE SENSE for 1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * page x08, so we will skip it. */ 1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->skip_ms_page_8 = 1; 2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Some disks return the total number of blocks in response 2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * to READ CAPACITY rather than the highest block number. 2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If this device makes that mistake, tell the sd driver. */ 2047e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & US_FL_FIX_CAPACITY) 2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->fix_capacity = 1; 20686dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm 20761bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum /* A few disks have two indistinguishable version, one of 20861bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum * which reports the correct capacity and the other does not. 20961bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum * The sd driver has to guess which is the case. */ 2107e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & US_FL_CAPACITY_HEURISTICS) 21161bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum sdev->guess_capacity = 1; 21261bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum 2131537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros /* assume SPC3 or latter devices support sense size > 18 */ 2141537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros if (sdev->scsi_level > SCSI_SPC_2) 2151537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros us->fflags |= US_FL_SANE_SENSE; 2161537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros 217a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm /* Some devices report a SCSI revision level above 2 but are 218a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm * unable to handle the REPORT LUNS command (for which 219a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm * support is mandatory at level 3). Since we already have 220a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm * a Get-Max-LUN request, we won't lose much by setting the 221a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm * revision level down to 2. The only devices that would be 222a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm * affected are those with sparse LUNs. */ 223f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern if (sdev->scsi_level > SCSI_2) 224f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern sdev->sdev_target->scsi_level = 225f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern sdev->scsi_level = SCSI_2; 226a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm 22786dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable 22886dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm * Hardware Error) when any low-level error occurs, 22986dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm * recoverable or not. Setting this flag tells the SCSI 23086dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm * midlayer to retry such commands, which frequently will 23186dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm * succeed and fix the error. The worst this can lead to 23286dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm * is an occasional series of retries that will all fail. */ 23386dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm sdev->retry_hwerror = 1; 23486dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm 235f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab /* USB disks should allow restart. Some drives spin down 236f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab * automatically, requiring a START-STOP UNIT command. */ 237f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab sdev->allow_restart = 1; 238f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab 23923c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede /* Some USB cardreaders have trouble reading an sdcard's last 24023c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede * sector in a larger then 1 sector read, since the performance 24123c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede * impact is negible we set this flag for all USB disks */ 24223c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede sdev->last_sector_bug = 1; 24325ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern 24425ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern /* Enable last-sector hacks for single-target devices using 24525ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern * the Bulk-only transport, unless we already know the 24625ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern * capacity will be decremented or is correct. */ 24725ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK | 24825ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern US_FL_SCM_MULT_TARG)) && 24925ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern us->protocol == US_PR_BULK) 25025ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern us->use_last_sector_hacks = 1; 2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } else { 2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Non-disk-type devices don't need to blacklist any pages 2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * or to force 192-byte transfer lengths for MODE SENSE. 2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * But they do need to use MODE SENSE(10). */ 2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->use_10_for_ms = 1; 2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 259f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern /* The CB and CBI transports have no way to pass LUN values 260f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern * other than the bits in the second byte of a CDB. But those 261f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern * bits don't get set to the LUN value if the device reports 262f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern * scsi_level == 0 (UNKNOWN). Hence such devices must necessarily 263f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern * be single-LUN. 264f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern */ 265f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern if ((us->protocol == US_PR_CB || us->protocol == US_PR_CBI) && 266f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern sdev->scsi_level == SCSI_UNKNOWN) 267f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern us->max_lun = 0; 268f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern 2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM 2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * REMOVAL command, so suppress those commands. */ 2717e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & US_FL_NOT_LOCKABLE) 2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->lockable = 0; 2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* this is to satisfy the compiler, tho I don't think the 2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * return code is ever checked anywhere. */ 2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* queue a command */ 2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This is always called with scsi_lock(host) held */ 2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int queuecommand(struct scsi_cmnd *srb, 2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds void (*done)(struct scsi_cmnd *)) 2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct us_data *us = host_to_us(srb->device->host); 2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 286441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison US_DEBUGP("%s called\n", __func__); 2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* check for state-transition errors */ 2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (us->srb != NULL) { 2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n", 291441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison __func__, us->srb); 2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return SCSI_MLQUEUE_HOST_BUSY; 2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* fail the command if we are disconnecting */ 2967e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds US_DEBUGP("Fail command during disconnect\n"); 2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds srb->result = DID_NO_CONNECT << 16; 2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds done(srb); 3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* enqueue the command and wake up the control thread */ 3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds srb->scsi_done = done; 3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds us->srb = srb; 3067119e3c37fbf7c27adb5929f344c826ecb8c7859Alan Stern complete(&us->cmnd_ready); 3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*********************************************************************** 3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Error handling functions 3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/ 3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Command timeout and abort */ 3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int command_abort(struct scsi_cmnd *srb) 3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct us_data *us = host_to_us(srb->device->host); 3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 320441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison US_DEBUGP("%s called\n", __func__); 3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 322226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING 323226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm * bits are protected by the host lock. */ 324226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm scsi_lock(us_to_host(us)); 325226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm 3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Is this command still active? */ 3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (us->srb != srb) { 328226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm scsi_unlock(us_to_host(us)); 3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds US_DEBUGP ("-- nothing to abort\n"); 3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return FAILED; 3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if 3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a device reset isn't already in progress (to avoid interfering 335226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm * with the reset). Note that we must retain the host lock while 336226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm * calling usb_stor_stop_transport(); otherwise it might interfere 337226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm * with an auto-reset that begins as soon as we release the lock. */ 3387e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern set_bit(US_FLIDX_TIMED_OUT, &us->dflags); 3397e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) { 3407e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern set_bit(US_FLIDX_ABORTING, &us->dflags); 3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds usb_stor_stop_transport(us); 3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 343226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm scsi_unlock(us_to_host(us)); 3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Wait for the aborted command to finish */ 3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds wait_for_completion(&us->notify); 3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return SUCCESS; 3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This invokes the transport reset mechanism to reset the state of the 3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device */ 3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int device_reset(struct scsi_cmnd *srb) 3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct us_data *us = host_to_us(srb->device->host); 3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int result; 3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 357441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison US_DEBUGP("%s called\n", __func__); 3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 359d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman /* lock the device pointers and do the reset */ 360d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman mutex_lock(&(us->dev_mutex)); 361d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman result = us->transport_reset(us); 362d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman mutex_unlock(&us->dev_mutex); 3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3644d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm return result < 0 ? FAILED : SUCCESS; 3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3674d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Simulate a SCSI bus reset by resetting the device's USB port. */ 3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int bus_reset(struct scsi_cmnd *srb) 3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct us_data *us = host_to_us(srb->device->host); 3714d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm int result; 3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 373441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison US_DEBUGP("%s called\n", __func__); 3744d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm result = usb_stor_port_reset(us); 3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return result < 0 ? FAILED : SUCCESS; 3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Report a driver-initiated device reset to the SCSI layer. 3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Calling this for a SCSI-initiated reset is unnecessary but harmless. 3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The caller must own the SCSI host lock. */ 3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid usb_stor_report_device_reset(struct us_data *us) 3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int i; 3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct Scsi_Host *host = us_to_host(us); 3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds scsi_report_device_reset(host, 0, 0); 3877e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & US_FL_SCM_MULT_TARG) { 3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds for (i = 1; i < host->max_id; ++i) 3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds scsi_report_device_reset(host, 0, i); 3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3934d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Report a driver-initiated bus reset to the SCSI layer. 3944d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm * Calling this for a SCSI-initiated reset is unnecessary but harmless. 395f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern * The caller must not own the SCSI host lock. */ 3964d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharmvoid usb_stor_report_bus_reset(struct us_data *us) 3974d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm{ 398f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern struct Scsi_Host *host = us_to_host(us); 399f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern 400f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern scsi_lock(host); 401f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern scsi_report_bus_reset(host, 0); 402f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern scsi_unlock(host); 4034d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm} 4044d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm 4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*********************************************************************** 4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * /proc/scsi/ functions 4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/ 4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* we use this macro to help us write into the buffer */ 4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef SPRINTF 4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SPRINTF(args...) \ 4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0) 4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int proc_info (struct Scsi_Host *host, char *buffer, 4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds char **start, off_t offset, int length, int inout) 4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct us_data *us = host_to_us(host); 4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds char *pos = buffer; 4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds const char *string; 4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* if someone is sending us data, just throw it away */ 4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (inout) 4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return length; 4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* print the controller name */ 4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SPRINTF(" Host scsi%d: usb-storage\n", host->host_no); 4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* print product, vendor, and serial number strings */ 4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (us->pusb_dev->manufacturer) 4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = us->pusb_dev->manufacturer; 4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else if (us->unusual_dev->vendorName) 4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = us->unusual_dev->vendorName; 4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else 4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = "Unknown"; 4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SPRINTF(" Vendor: %s\n", string); 4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (us->pusb_dev->product) 4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = us->pusb_dev->product; 4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else if (us->unusual_dev->productName) 4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = us->unusual_dev->productName; 4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else 4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = "Unknown"; 4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SPRINTF(" Product: %s\n", string); 4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (us->pusb_dev->serial) 4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = us->pusb_dev->serial; 4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else 4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds string = "None"; 4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SPRINTF("Serial Number: %s\n", string); 4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* show the protocol and transport */ 4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SPRINTF(" Protocol: %s\n", us->protocol_name); 4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SPRINTF(" Transport: %s\n", us->transport_name); 4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* show the device flags */ 4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (pos < buffer + length) { 4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds pos += sprintf(pos, " Quirks:"); 4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define US_FLAG(name, value) \ 4587e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern if (us->fflags & value) pos += sprintf(pos, " " #name); 4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsUS_DO_ALL_FLAGS 4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef US_FLAG 4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *(pos++) = '\n'; 4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Calculate start of next buffer, and return value. 4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *start = buffer + offset; 4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((pos - buffer) < offset) 4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return (0); 4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else if ((pos - buffer - offset) < length) 4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return (pos - buffer - offset); 4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else 4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return (length); 4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*********************************************************************** 4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs interface 4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/ 4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Output routine for the sysfs max_sectors file */ 483060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf) 4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 487ae03bf639a5027d27270123f5f6e3ee6a412781dMartin K. Petersen return sprintf(buf, "%u\n", queue_max_sectors(sdev->request_queue)); 4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Input routine for the sysfs max_sectors file */ 491060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf, 4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size_t count) 4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned short ms; 4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) { 4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds blk_queue_max_sectors(sdev->request_queue, ms); 4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return strlen(buf); 5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EINVAL; 5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors, 5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds store_max_sectors); 5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct device_attribute *sysfs_device_attr_list[] = { 5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds &dev_attr_max_sectors, 5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds NULL, 5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds }; 5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this defines our host template, with which we'll allocate hosts 5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_host_template usb_stor_host_template = { 5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* basic userland interface stuff */ 5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .name = "usb-storage", 5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .proc_name = "usb-storage", 5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .proc_info = proc_info, 5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .info = host_info, 5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* command interface -- queued only */ 5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .queuecommand = queuecommand, 5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* error and abort handlers */ 5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .eh_abort_handler = command_abort, 5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .eh_device_reset_handler = device_reset, 5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .eh_bus_reset_handler = bus_reset, 5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* queue commands only, only one command per LUN */ 5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .can_queue = 1, 5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .cmd_per_lun = 1, 5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* unknown initiator id */ 5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .this_id = -1, 5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .slave_alloc = slave_alloc, 5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .slave_configure = slave_configure, 5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* lots of sg segments can be handled */ 5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .sg_tablesize = SG_ALL, 5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* limit the total size of a transfer to 120 KB */ 5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .max_sectors = 240, 5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* merge commands... this seems to help performance, but 5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * periodically someone should test to see which setting is more 5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * optimal. 5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .use_clustering = 1, 5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* emulated HBA */ 5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .emulated = 1, 5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* we do our own delay after a device or bus reset */ 5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .skip_settle_delay = 1, 5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* sysfs device attributes */ 5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .sdev_attrs = sysfs_device_attr_list, 5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* module management */ 5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .module = THIS_MODULE 5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* To Report "Illegal Request: Invalid Field in CDB */ 5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned char usb_stor_sense_invalidCDB[18] = { 5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds [0] = 0x70, /* current error */ 5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */ 5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds [7] = 0x0a, /* additional length */ 5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds [12] = 0x24 /* Invalid Field in CDB */ 5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 573e6e244b6cb1f70e7109381626293cd40a8334ed3Alan SternEXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB); 574