scsiglue.c 18 KB

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