scsiglue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. /*
  77. * The UFI spec treates the Peripheral Qualifier bits in an
  78. * INQUIRY result as reserved and requires devices to set them
  79. * to 0. However the SCSI spec requires these bits to be set
  80. * to 3 to indicate when a LUN is not present.
  81. *
  82. * Let the scanning code know if this target merely sets
  83. * Peripheral Device Type to 0x1f to indicate no LUN.
  84. */
  85. if (us->subclass == US_SC_UFI)
  86. sdev->sdev_target->pdt_1f_for_no_lun = 1;
  87. return 0;
  88. }
  89. static int slave_configure(struct scsi_device *sdev)
  90. {
  91. struct us_data *us = host_to_us(sdev->host);
  92. /* Scatter-gather buffers (all but the last) must have a length
  93. * divisible by the bulk maxpacket size. Otherwise a data packet
  94. * would end up being short, causing a premature end to the data
  95. * transfer. Since high-speed bulk pipes have a maxpacket size
  96. * of 512, we'll use that as the scsi device queue's DMA alignment
  97. * mask. Guaranteeing proper alignment of the first buffer will
  98. * have the desired effect because, except at the beginning and
  99. * the end, scatter-gather buffers follow page boundaries. */
  100. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  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) &&
  106. sdev->request_queue->max_sectors > 64)
  107. blk_queue_max_sectors(sdev->request_queue, 64);
  108. /* We can't put these settings in slave_alloc() because that gets
  109. * called before the device type is known. Consequently these
  110. * settings can't be overridden via the scsi devinfo mechanism. */
  111. if (sdev->type == TYPE_DISK) {
  112. /* Disk-type devices use MODE SENSE(6) if the protocol
  113. * (SubClass) is Transparent SCSI, otherwise they use
  114. * MODE SENSE(10). */
  115. if (us->subclass != US_SC_SCSI)
  116. sdev->use_10_for_ms = 1;
  117. /* Many disks only accept MODE SENSE transfer lengths of
  118. * 192 bytes (that's what Windows uses). */
  119. sdev->use_192_bytes_for_3f = 1;
  120. /* Some devices don't like MODE SENSE with page=0x3f,
  121. * which is the command used for checking if a device
  122. * is write-protected. Now that we tell the sd driver
  123. * to do a 192-byte transfer with this command the
  124. * majority of devices work fine, but a few still can't
  125. * handle it. The sd driver will simply assume those
  126. * devices are write-enabled. */
  127. if (us->flags & US_FL_NO_WP_DETECT)
  128. sdev->skip_ms_page_3f = 1;
  129. /* A number of devices have problems with MODE SENSE for
  130. * page x08, so we will skip it. */
  131. sdev->skip_ms_page_8 = 1;
  132. /* Some disks return the total number of blocks in response
  133. * to READ CAPACITY rather than the highest block number.
  134. * If this device makes that mistake, tell the sd driver. */
  135. if (us->flags & US_FL_FIX_CAPACITY)
  136. sdev->fix_capacity = 1;
  137. /* Some devices report a SCSI revision level above 2 but are
  138. * unable to handle the REPORT LUNS command (for which
  139. * support is mandatory at level 3). Since we already have
  140. * a Get-Max-LUN request, we won't lose much by setting the
  141. * revision level down to 2. The only devices that would be
  142. * affected are those with sparse LUNs. */
  143. if (sdev->scsi_level > SCSI_2)
  144. sdev->sdev_target->scsi_level =
  145. sdev->scsi_level = SCSI_2;
  146. /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
  147. * Hardware Error) when any low-level error occurs,
  148. * recoverable or not. Setting this flag tells the SCSI
  149. * midlayer to retry such commands, which frequently will
  150. * succeed and fix the error. The worst this can lead to
  151. * is an occasional series of retries that will all fail. */
  152. sdev->retry_hwerror = 1;
  153. } else {
  154. /* Non-disk-type devices don't need to blacklist any pages
  155. * or to force 192-byte transfer lengths for MODE SENSE.
  156. * But they do need to use MODE SENSE(10). */
  157. sdev->use_10_for_ms = 1;
  158. }
  159. /* The CB and CBI transports have no way to pass LUN values
  160. * other than the bits in the second byte of a CDB. But those
  161. * bits don't get set to the LUN value if the device reports
  162. * scsi_level == 0 (UNKNOWN). Hence such devices must necessarily
  163. * be single-LUN.
  164. */
  165. if ((us->protocol == US_PR_CB || us->protocol == US_PR_CBI) &&
  166. sdev->scsi_level == SCSI_UNKNOWN)
  167. us->max_lun = 0;
  168. /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
  169. * REMOVAL command, so suppress those commands. */
  170. if (us->flags & US_FL_NOT_LOCKABLE)
  171. sdev->lockable = 0;
  172. /* this is to satisfy the compiler, tho I don't think the
  173. * return code is ever checked anywhere. */
  174. return 0;
  175. }
  176. /* queue a command */
  177. /* This is always called with scsi_lock(host) held */
  178. static int queuecommand(struct scsi_cmnd *srb,
  179. void (*done)(struct scsi_cmnd *))
  180. {
  181. struct us_data *us = host_to_us(srb->device->host);
  182. US_DEBUGP("%s called\n", __FUNCTION__);
  183. /* check for state-transition errors */
  184. if (us->srb != NULL) {
  185. printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
  186. __FUNCTION__, us->srb);
  187. return SCSI_MLQUEUE_HOST_BUSY;
  188. }
  189. /* fail the command if we are disconnecting */
  190. if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
  191. US_DEBUGP("Fail command during disconnect\n");
  192. srb->result = DID_NO_CONNECT << 16;
  193. done(srb);
  194. return 0;
  195. }
  196. /* enqueue the command and wake up the control thread */
  197. srb->scsi_done = done;
  198. us->srb = srb;
  199. up(&(us->sema));
  200. return 0;
  201. }
  202. /***********************************************************************
  203. * Error handling functions
  204. ***********************************************************************/
  205. /* Command timeout and abort */
  206. static int command_abort(struct scsi_cmnd *srb)
  207. {
  208. struct us_data *us = host_to_us(srb->device->host);
  209. US_DEBUGP("%s called\n", __FUNCTION__);
  210. /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
  211. * bits are protected by the host lock. */
  212. scsi_lock(us_to_host(us));
  213. /* Is this command still active? */
  214. if (us->srb != srb) {
  215. scsi_unlock(us_to_host(us));
  216. US_DEBUGP ("-- nothing to abort\n");
  217. return FAILED;
  218. }
  219. /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
  220. * a device reset isn't already in progress (to avoid interfering
  221. * with the reset). Note that we must retain the host lock while
  222. * calling usb_stor_stop_transport(); otherwise it might interfere
  223. * with an auto-reset that begins as soon as we release the lock. */
  224. set_bit(US_FLIDX_TIMED_OUT, &us->flags);
  225. if (!test_bit(US_FLIDX_RESETTING, &us->flags)) {
  226. set_bit(US_FLIDX_ABORTING, &us->flags);
  227. usb_stor_stop_transport(us);
  228. }
  229. scsi_unlock(us_to_host(us));
  230. /* Wait for the aborted command to finish */
  231. wait_for_completion(&us->notify);
  232. return SUCCESS;
  233. }
  234. /* This invokes the transport reset mechanism to reset the state of the
  235. * device */
  236. static int device_reset(struct scsi_cmnd *srb)
  237. {
  238. struct us_data *us = host_to_us(srb->device->host);
  239. int result;
  240. US_DEBUGP("%s called\n", __FUNCTION__);
  241. /* lock the device pointers and do the reset */
  242. mutex_lock(&(us->dev_mutex));
  243. result = us->transport_reset(us);
  244. mutex_unlock(&us->dev_mutex);
  245. return result < 0 ? FAILED : SUCCESS;
  246. }
  247. /* Simulate a SCSI bus reset by resetting the device's USB port. */
  248. static int bus_reset(struct scsi_cmnd *srb)
  249. {
  250. struct us_data *us = host_to_us(srb->device->host);
  251. int result;
  252. US_DEBUGP("%s called\n", __FUNCTION__);
  253. result = usb_stor_port_reset(us);
  254. return result < 0 ? FAILED : SUCCESS;
  255. }
  256. /* Report a driver-initiated device reset to the SCSI layer.
  257. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  258. * The caller must own the SCSI host lock. */
  259. void usb_stor_report_device_reset(struct us_data *us)
  260. {
  261. int i;
  262. struct Scsi_Host *host = us_to_host(us);
  263. scsi_report_device_reset(host, 0, 0);
  264. if (us->flags & US_FL_SCM_MULT_TARG) {
  265. for (i = 1; i < host->max_id; ++i)
  266. scsi_report_device_reset(host, 0, i);
  267. }
  268. }
  269. /* Report a driver-initiated bus reset to the SCSI layer.
  270. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  271. * The caller must own the SCSI host lock. */
  272. void usb_stor_report_bus_reset(struct us_data *us)
  273. {
  274. scsi_report_bus_reset(us_to_host(us), 0);
  275. }
  276. /***********************************************************************
  277. * /proc/scsi/ functions
  278. ***********************************************************************/
  279. /* we use this macro to help us write into the buffer */
  280. #undef SPRINTF
  281. #define SPRINTF(args...) \
  282. do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
  283. static int proc_info (struct Scsi_Host *host, char *buffer,
  284. char **start, off_t offset, int length, int inout)
  285. {
  286. struct us_data *us = host_to_us(host);
  287. char *pos = buffer;
  288. const char *string;
  289. /* if someone is sending us data, just throw it away */
  290. if (inout)
  291. return length;
  292. /* print the controller name */
  293. SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
  294. /* print product, vendor, and serial number strings */
  295. if (us->pusb_dev->manufacturer)
  296. string = us->pusb_dev->manufacturer;
  297. else if (us->unusual_dev->vendorName)
  298. string = us->unusual_dev->vendorName;
  299. else
  300. string = "Unknown";
  301. SPRINTF(" Vendor: %s\n", string);
  302. if (us->pusb_dev->product)
  303. string = us->pusb_dev->product;
  304. else if (us->unusual_dev->productName)
  305. string = us->unusual_dev->productName;
  306. else
  307. string = "Unknown";
  308. SPRINTF(" Product: %s\n", string);
  309. if (us->pusb_dev->serial)
  310. string = us->pusb_dev->serial;
  311. else
  312. string = "None";
  313. SPRINTF("Serial Number: %s\n", string);
  314. /* show the protocol and transport */
  315. SPRINTF(" Protocol: %s\n", us->protocol_name);
  316. SPRINTF(" Transport: %s\n", us->transport_name);
  317. /* show the device flags */
  318. if (pos < buffer + length) {
  319. pos += sprintf(pos, " Quirks:");
  320. #define US_FLAG(name, value) \
  321. if (us->flags & value) pos += sprintf(pos, " " #name);
  322. US_DO_ALL_FLAGS
  323. #undef US_FLAG
  324. *(pos++) = '\n';
  325. }
  326. /*
  327. * Calculate start of next buffer, and return value.
  328. */
  329. *start = buffer + offset;
  330. if ((pos - buffer) < offset)
  331. return (0);
  332. else if ((pos - buffer - offset) < length)
  333. return (pos - buffer - offset);
  334. else
  335. return (length);
  336. }
  337. /***********************************************************************
  338. * Sysfs interface
  339. ***********************************************************************/
  340. /* Output routine for the sysfs max_sectors file */
  341. static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
  342. {
  343. struct scsi_device *sdev = to_scsi_device(dev);
  344. return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
  345. }
  346. /* Input routine for the sysfs max_sectors file */
  347. static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
  348. size_t count)
  349. {
  350. struct scsi_device *sdev = to_scsi_device(dev);
  351. unsigned short ms;
  352. if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
  353. blk_queue_max_sectors(sdev->request_queue, ms);
  354. return strlen(buf);
  355. }
  356. return -EINVAL;
  357. }
  358. static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
  359. store_max_sectors);
  360. static struct device_attribute *sysfs_device_attr_list[] = {
  361. &dev_attr_max_sectors,
  362. NULL,
  363. };
  364. /*
  365. * this defines our host template, with which we'll allocate hosts
  366. */
  367. struct scsi_host_template usb_stor_host_template = {
  368. /* basic userland interface stuff */
  369. .name = "usb-storage",
  370. .proc_name = "usb-storage",
  371. .proc_info = proc_info,
  372. .info = host_info,
  373. /* command interface -- queued only */
  374. .queuecommand = queuecommand,
  375. /* error and abort handlers */
  376. .eh_abort_handler = command_abort,
  377. .eh_device_reset_handler = device_reset,
  378. .eh_bus_reset_handler = bus_reset,
  379. /* queue commands only, only one command per LUN */
  380. .can_queue = 1,
  381. .cmd_per_lun = 1,
  382. /* unknown initiator id */
  383. .this_id = -1,
  384. .slave_alloc = slave_alloc,
  385. .slave_configure = slave_configure,
  386. /* lots of sg segments can be handled */
  387. .sg_tablesize = SG_ALL,
  388. /* limit the total size of a transfer to 120 KB */
  389. .max_sectors = 240,
  390. /* merge commands... this seems to help performance, but
  391. * periodically someone should test to see which setting is more
  392. * optimal.
  393. */
  394. .use_clustering = 1,
  395. /* emulated HBA */
  396. .emulated = 1,
  397. /* we do our own delay after a device or bus reset */
  398. .skip_settle_delay = 1,
  399. /* sysfs device attributes */
  400. .sdev_attrs = sysfs_device_attr_list,
  401. /* module management */
  402. .module = THIS_MODULE
  403. };
  404. /* To Report "Illegal Request: Invalid Field in CDB */
  405. unsigned char usb_stor_sense_invalidCDB[18] = {
  406. [0] = 0x70, /* current error */
  407. [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
  408. [7] = 0x0a, /* additional length */
  409. [12] = 0x24 /* Invalid Field in CDB */
  410. };