scsiglue.c 16 KB

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