scsiglue.c 16 KB

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