scsiglue.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. /* Vendor IDs for companies that seem to include the READ CAPACITY bug
  59. * in all their devices
  60. */
  61. #define VENDOR_ID_NOKIA 0x0421
  62. #define VENDOR_ID_NIKON 0x04b0
  63. #define VENDOR_ID_PENTAX 0x0a17
  64. #define VENDOR_ID_MOTOROLA 0x22b8
  65. /***********************************************************************
  66. * Host functions
  67. ***********************************************************************/
  68. static const char* host_info(struct Scsi_Host *host)
  69. {
  70. return "SCSI emulation for USB Mass Storage devices";
  71. }
  72. static int slave_alloc (struct scsi_device *sdev)
  73. {
  74. struct us_data *us = host_to_us(sdev->host);
  75. /*
  76. * Set the INQUIRY transfer length to 36. We don't use any of
  77. * the extra data and many devices choke if asked for more or
  78. * less than 36 bytes.
  79. */
  80. sdev->inquiry_len = 36;
  81. /* USB has unusual DMA-alignment requirements: Although the
  82. * starting address of each scatter-gather element doesn't matter,
  83. * the length of each element except the last must be divisible
  84. * by the Bulk maxpacket value. There's currently no way to
  85. * express this by block-layer constraints, so we'll cop out
  86. * and simply require addresses to be aligned at 512-byte
  87. * boundaries. This is okay since most block I/O involves
  88. * hardware sectors that are multiples of 512 bytes in length,
  89. * and since host controllers up through USB 2.0 have maxpacket
  90. * values no larger than 512.
  91. *
  92. * But it doesn't suffice for Wireless USB, where Bulk maxpacket
  93. * values can be as large as 2048. To make that work properly
  94. * will require changes to the block layer.
  95. */
  96. blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
  97. /*
  98. * The UFI spec treates the Peripheral Qualifier bits in an
  99. * INQUIRY result as reserved and requires devices to set them
  100. * to 0. However the SCSI spec requires these bits to be set
  101. * to 3 to indicate when a LUN is not present.
  102. *
  103. * Let the scanning code know if this target merely sets
  104. * Peripheral Device Type to 0x1f to indicate no LUN.
  105. */
  106. if (us->subclass == US_SC_UFI)
  107. sdev->sdev_target->pdt_1f_for_no_lun = 1;
  108. return 0;
  109. }
  110. static int slave_configure(struct scsi_device *sdev)
  111. {
  112. struct us_data *us = host_to_us(sdev->host);
  113. /* Many devices have trouble transfering more than 32KB at a time,
  114. * while others have trouble with more than 64K. At this time we
  115. * are limiting both to 32K (64 sectores).
  116. */
  117. if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
  118. unsigned int max_sectors = 64;
  119. if (us->fflags & US_FL_MAX_SECTORS_MIN)
  120. max_sectors = PAGE_CACHE_SIZE >> 9;
  121. if (sdev->request_queue->max_sectors > max_sectors)
  122. blk_queue_max_sectors(sdev->request_queue,
  123. max_sectors);
  124. }
  125. /* Some USB host controllers can't do DMA; they have to use PIO.
  126. * They indicate this by setting their dma_mask to NULL. For
  127. * such controllers we need to make sure the block layer sets
  128. * up bounce buffers in addressable memory.
  129. */
  130. if (!us->pusb_dev->bus->controller->dma_mask)
  131. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
  132. /* We can't put these settings in slave_alloc() because that gets
  133. * called before the device type is known. Consequently these
  134. * settings can't be overridden via the scsi devinfo mechanism. */
  135. if (sdev->type == TYPE_DISK) {
  136. /* Some vendors seem to put the READ CAPACITY bug into
  137. * all their devices -- primarily makers of cell phones
  138. * and digital cameras. Since these devices always use
  139. * flash media and can be expected to have an even number
  140. * of sectors, we will always enable the CAPACITY_HEURISTICS
  141. * flag unless told otherwise. */
  142. switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
  143. case VENDOR_ID_NOKIA:
  144. case VENDOR_ID_NIKON:
  145. case VENDOR_ID_PENTAX:
  146. case VENDOR_ID_MOTOROLA:
  147. if (!(us->fflags & (US_FL_FIX_CAPACITY |
  148. US_FL_CAPACITY_OK)))
  149. us->fflags |= US_FL_CAPACITY_HEURISTICS;
  150. break;
  151. }
  152. /* Disk-type devices use MODE SENSE(6) if the protocol
  153. * (SubClass) is Transparent SCSI, otherwise they use
  154. * MODE SENSE(10). */
  155. if (us->subclass != US_SC_SCSI && us->subclass != US_SC_CYP_ATACB)
  156. sdev->use_10_for_ms = 1;
  157. /* Many disks only accept MODE SENSE transfer lengths of
  158. * 192 bytes (that's what Windows uses). */
  159. sdev->use_192_bytes_for_3f = 1;
  160. /* Some devices don't like MODE SENSE with page=0x3f,
  161. * which is the command used for checking if a device
  162. * is write-protected. Now that we tell the sd driver
  163. * to do a 192-byte transfer with this command the
  164. * majority of devices work fine, but a few still can't
  165. * handle it. The sd driver will simply assume those
  166. * devices are write-enabled. */
  167. if (us->fflags & US_FL_NO_WP_DETECT)
  168. sdev->skip_ms_page_3f = 1;
  169. /* A number of devices have problems with MODE SENSE for
  170. * page x08, so we will skip it. */
  171. sdev->skip_ms_page_8 = 1;
  172. /* Some disks return the total number of blocks in response
  173. * to READ CAPACITY rather than the highest block number.
  174. * If this device makes that mistake, tell the sd driver. */
  175. if (us->fflags & US_FL_FIX_CAPACITY)
  176. sdev->fix_capacity = 1;
  177. /* A few disks have two indistinguishable version, one of
  178. * which reports the correct capacity and the other does not.
  179. * The sd driver has to guess which is the case. */
  180. if (us->fflags & US_FL_CAPACITY_HEURISTICS)
  181. sdev->guess_capacity = 1;
  182. /* assume SPC3 or latter devices support sense size > 18 */
  183. if (sdev->scsi_level > SCSI_SPC_2)
  184. us->fflags |= US_FL_SANE_SENSE;
  185. /* Some devices report a SCSI revision level above 2 but are
  186. * unable to handle the REPORT LUNS command (for which
  187. * support is mandatory at level 3). Since we already have
  188. * a Get-Max-LUN request, we won't lose much by setting the
  189. * revision level down to 2. The only devices that would be
  190. * affected are those with sparse LUNs. */
  191. if (sdev->scsi_level > SCSI_2)
  192. sdev->sdev_target->scsi_level =
  193. sdev->scsi_level = SCSI_2;
  194. /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
  195. * Hardware Error) when any low-level error occurs,
  196. * recoverable or not. Setting this flag tells the SCSI
  197. * midlayer to retry such commands, which frequently will
  198. * succeed and fix the error. The worst this can lead to
  199. * is an occasional series of retries that will all fail. */
  200. sdev->retry_hwerror = 1;
  201. /* USB disks should allow restart. Some drives spin down
  202. * automatically, requiring a START-STOP UNIT command. */
  203. sdev->allow_restart = 1;
  204. /* Some USB cardreaders have trouble reading an sdcard's last
  205. * sector in a larger then 1 sector read, since the performance
  206. * impact is negible we set this flag for all USB disks */
  207. sdev->last_sector_bug = 1;
  208. /* Enable last-sector hacks for single-target devices using
  209. * the Bulk-only transport, unless we already know the
  210. * capacity will be decremented or is correct. */
  211. if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
  212. US_FL_SCM_MULT_TARG)) &&
  213. us->protocol == US_PR_BULK)
  214. us->use_last_sector_hacks = 1;
  215. } else {
  216. /* Non-disk-type devices don't need to blacklist any pages
  217. * or to force 192-byte transfer lengths for MODE SENSE.
  218. * But they do need to use MODE SENSE(10). */
  219. sdev->use_10_for_ms = 1;
  220. }
  221. /* The CB and CBI transports have no way to pass LUN values
  222. * other than the bits in the second byte of a CDB. But those
  223. * bits don't get set to the LUN value if the device reports
  224. * scsi_level == 0 (UNKNOWN). Hence such devices must necessarily
  225. * be single-LUN.
  226. */
  227. if ((us->protocol == US_PR_CB || us->protocol == US_PR_CBI) &&
  228. sdev->scsi_level == SCSI_UNKNOWN)
  229. us->max_lun = 0;
  230. /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
  231. * REMOVAL command, so suppress those commands. */
  232. if (us->fflags & US_FL_NOT_LOCKABLE)
  233. sdev->lockable = 0;
  234. /* this is to satisfy the compiler, tho I don't think the
  235. * return code is ever checked anywhere. */
  236. return 0;
  237. }
  238. /* queue a command */
  239. /* This is always called with scsi_lock(host) held */
  240. static int queuecommand(struct scsi_cmnd *srb,
  241. void (*done)(struct scsi_cmnd *))
  242. {
  243. struct us_data *us = host_to_us(srb->device->host);
  244. US_DEBUGP("%s called\n", __func__);
  245. /* check for state-transition errors */
  246. if (us->srb != NULL) {
  247. printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
  248. __func__, us->srb);
  249. return SCSI_MLQUEUE_HOST_BUSY;
  250. }
  251. /* fail the command if we are disconnecting */
  252. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  253. US_DEBUGP("Fail command during disconnect\n");
  254. srb->result = DID_NO_CONNECT << 16;
  255. done(srb);
  256. return 0;
  257. }
  258. /* enqueue the command and wake up the control thread */
  259. srb->scsi_done = done;
  260. us->srb = srb;
  261. complete(&us->cmnd_ready);
  262. return 0;
  263. }
  264. /***********************************************************************
  265. * Error handling functions
  266. ***********************************************************************/
  267. /* Command timeout and abort */
  268. static int command_abort(struct scsi_cmnd *srb)
  269. {
  270. struct us_data *us = host_to_us(srb->device->host);
  271. US_DEBUGP("%s called\n", __func__);
  272. /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
  273. * bits are protected by the host lock. */
  274. scsi_lock(us_to_host(us));
  275. /* Is this command still active? */
  276. if (us->srb != srb) {
  277. scsi_unlock(us_to_host(us));
  278. US_DEBUGP ("-- nothing to abort\n");
  279. return FAILED;
  280. }
  281. /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
  282. * a device reset isn't already in progress (to avoid interfering
  283. * with the reset). Note that we must retain the host lock while
  284. * calling usb_stor_stop_transport(); otherwise it might interfere
  285. * with an auto-reset that begins as soon as we release the lock. */
  286. set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
  287. if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
  288. set_bit(US_FLIDX_ABORTING, &us->dflags);
  289. usb_stor_stop_transport(us);
  290. }
  291. scsi_unlock(us_to_host(us));
  292. /* Wait for the aborted command to finish */
  293. wait_for_completion(&us->notify);
  294. return SUCCESS;
  295. }
  296. /* This invokes the transport reset mechanism to reset the state of the
  297. * device */
  298. static int device_reset(struct scsi_cmnd *srb)
  299. {
  300. struct us_data *us = host_to_us(srb->device->host);
  301. int result;
  302. US_DEBUGP("%s called\n", __func__);
  303. /* lock the device pointers and do the reset */
  304. mutex_lock(&(us->dev_mutex));
  305. result = us->transport_reset(us);
  306. mutex_unlock(&us->dev_mutex);
  307. return result < 0 ? FAILED : SUCCESS;
  308. }
  309. /* Simulate a SCSI bus reset by resetting the device's USB port. */
  310. static int bus_reset(struct scsi_cmnd *srb)
  311. {
  312. struct us_data *us = host_to_us(srb->device->host);
  313. int result;
  314. US_DEBUGP("%s called\n", __func__);
  315. result = usb_stor_port_reset(us);
  316. return result < 0 ? FAILED : SUCCESS;
  317. }
  318. /* Report a driver-initiated device reset to the SCSI layer.
  319. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  320. * The caller must own the SCSI host lock. */
  321. void usb_stor_report_device_reset(struct us_data *us)
  322. {
  323. int i;
  324. struct Scsi_Host *host = us_to_host(us);
  325. scsi_report_device_reset(host, 0, 0);
  326. if (us->fflags & US_FL_SCM_MULT_TARG) {
  327. for (i = 1; i < host->max_id; ++i)
  328. scsi_report_device_reset(host, 0, i);
  329. }
  330. }
  331. /* Report a driver-initiated bus reset to the SCSI layer.
  332. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  333. * The caller must not own the SCSI host lock. */
  334. void usb_stor_report_bus_reset(struct us_data *us)
  335. {
  336. struct Scsi_Host *host = us_to_host(us);
  337. scsi_lock(host);
  338. scsi_report_bus_reset(host, 0);
  339. scsi_unlock(host);
  340. }
  341. /***********************************************************************
  342. * /proc/scsi/ functions
  343. ***********************************************************************/
  344. /* we use this macro to help us write into the buffer */
  345. #undef SPRINTF
  346. #define SPRINTF(args...) \
  347. do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
  348. static int proc_info (struct Scsi_Host *host, char *buffer,
  349. char **start, off_t offset, int length, int inout)
  350. {
  351. struct us_data *us = host_to_us(host);
  352. char *pos = buffer;
  353. const char *string;
  354. /* if someone is sending us data, just throw it away */
  355. if (inout)
  356. return length;
  357. /* print the controller name */
  358. SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
  359. /* print product, vendor, and serial number strings */
  360. if (us->pusb_dev->manufacturer)
  361. string = us->pusb_dev->manufacturer;
  362. else if (us->unusual_dev->vendorName)
  363. string = us->unusual_dev->vendorName;
  364. else
  365. string = "Unknown";
  366. SPRINTF(" Vendor: %s\n", string);
  367. if (us->pusb_dev->product)
  368. string = us->pusb_dev->product;
  369. else if (us->unusual_dev->productName)
  370. string = us->unusual_dev->productName;
  371. else
  372. string = "Unknown";
  373. SPRINTF(" Product: %s\n", string);
  374. if (us->pusb_dev->serial)
  375. string = us->pusb_dev->serial;
  376. else
  377. string = "None";
  378. SPRINTF("Serial Number: %s\n", string);
  379. /* show the protocol and transport */
  380. SPRINTF(" Protocol: %s\n", us->protocol_name);
  381. SPRINTF(" Transport: %s\n", us->transport_name);
  382. /* show the device flags */
  383. if (pos < buffer + length) {
  384. pos += sprintf(pos, " Quirks:");
  385. #define US_FLAG(name, value) \
  386. if (us->fflags & value) pos += sprintf(pos, " " #name);
  387. US_DO_ALL_FLAGS
  388. #undef US_FLAG
  389. *(pos++) = '\n';
  390. }
  391. /*
  392. * Calculate start of next buffer, and return value.
  393. */
  394. *start = buffer + offset;
  395. if ((pos - buffer) < offset)
  396. return (0);
  397. else if ((pos - buffer - offset) < length)
  398. return (pos - buffer - offset);
  399. else
  400. return (length);
  401. }
  402. /***********************************************************************
  403. * Sysfs interface
  404. ***********************************************************************/
  405. /* Output routine for the sysfs max_sectors file */
  406. static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
  407. {
  408. struct scsi_device *sdev = to_scsi_device(dev);
  409. return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
  410. }
  411. /* Input routine for the sysfs max_sectors file */
  412. static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
  413. size_t count)
  414. {
  415. struct scsi_device *sdev = to_scsi_device(dev);
  416. unsigned short ms;
  417. if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
  418. blk_queue_max_sectors(sdev->request_queue, ms);
  419. return strlen(buf);
  420. }
  421. return -EINVAL;
  422. }
  423. static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
  424. store_max_sectors);
  425. static struct device_attribute *sysfs_device_attr_list[] = {
  426. &dev_attr_max_sectors,
  427. NULL,
  428. };
  429. /*
  430. * this defines our host template, with which we'll allocate hosts
  431. */
  432. struct scsi_host_template usb_stor_host_template = {
  433. /* basic userland interface stuff */
  434. .name = "usb-storage",
  435. .proc_name = "usb-storage",
  436. .proc_info = proc_info,
  437. .info = host_info,
  438. /* command interface -- queued only */
  439. .queuecommand = queuecommand,
  440. /* error and abort handlers */
  441. .eh_abort_handler = command_abort,
  442. .eh_device_reset_handler = device_reset,
  443. .eh_bus_reset_handler = bus_reset,
  444. /* queue commands only, only one command per LUN */
  445. .can_queue = 1,
  446. .cmd_per_lun = 1,
  447. /* unknown initiator id */
  448. .this_id = -1,
  449. .slave_alloc = slave_alloc,
  450. .slave_configure = slave_configure,
  451. /* lots of sg segments can be handled */
  452. .sg_tablesize = SG_ALL,
  453. /* limit the total size of a transfer to 120 KB */
  454. .max_sectors = 240,
  455. /* merge commands... this seems to help performance, but
  456. * periodically someone should test to see which setting is more
  457. * optimal.
  458. */
  459. .use_clustering = 1,
  460. /* emulated HBA */
  461. .emulated = 1,
  462. /* we do our own delay after a device or bus reset */
  463. .skip_settle_delay = 1,
  464. /* sysfs device attributes */
  465. .sdev_attrs = sysfs_device_attr_list,
  466. /* module management */
  467. .module = THIS_MODULE
  468. };
  469. /* To Report "Illegal Request: Invalid Field in CDB */
  470. unsigned char usb_stor_sense_invalidCDB[18] = {
  471. [0] = 0x70, /* current error */
  472. [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
  473. [7] = 0x0a, /* additional length */
  474. [12] = 0x24 /* Invalid Field in CDB */
  475. };