scsiglue.c 18 KB

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