scsiglue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_devinfo.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_eh.h>
  54. #include "usb.h"
  55. #include "scsiglue.h"
  56. #include "debug.h"
  57. #include "transport.h"
  58. #include "protocol.h"
  59. /***********************************************************************
  60. * Host functions
  61. ***********************************************************************/
  62. static const char* host_info(struct Scsi_Host *host)
  63. {
  64. return "SCSI emulation for USB Mass Storage devices";
  65. }
  66. static int slave_alloc (struct scsi_device *sdev)
  67. {
  68. /*
  69. * Set the INQUIRY transfer length to 36. We don't use any of
  70. * the extra data and many devices choke if asked for more or
  71. * less than 36 bytes.
  72. */
  73. sdev->inquiry_len = 36;
  74. return 0;
  75. }
  76. static int slave_configure(struct scsi_device *sdev)
  77. {
  78. struct us_data *us = host_to_us(sdev->host);
  79. /* Scatter-gather buffers (all but the last) must have a length
  80. * divisible by the bulk maxpacket size. Otherwise a data packet
  81. * would end up being short, causing a premature end to the data
  82. * transfer. Since high-speed bulk pipes have a maxpacket size
  83. * of 512, we'll use that as the scsi device queue's DMA alignment
  84. * mask. Guaranteeing proper alignment of the first buffer will
  85. * have the desired effect because, except at the beginning and
  86. * the end, scatter-gather buffers follow page boundaries. */
  87. blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
  88. /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
  89. * what is originally reported. We need this to avoid confusing
  90. * the SCSI layer with devices that report 0 or 1, but need 10-byte
  91. * commands (ala ATAPI devices behind certain bridges, or devices
  92. * which simply have broken INQUIRY data).
  93. *
  94. * NOTE: This means /dev/sg programs (ala cdrecord) will get the
  95. * actual information. This seems to be the preference for
  96. * programs like that.
  97. *
  98. * NOTE: This also means that /proc/scsi/scsi and sysfs may report
  99. * the actual value or the modified one, depending on where the
  100. * data comes from.
  101. */
  102. if (sdev->scsi_level < SCSI_2)
  103. sdev->scsi_level = SCSI_2;
  104. /* According to the technical support people at Genesys Logic,
  105. * devices using their chips have problems transferring more than
  106. * 32 KB at a time. In practice people have found that 64 KB
  107. * works okay and that's what Windows does. But we'll be
  108. * conservative; people can always use the sysfs interface to
  109. * increase max_sectors. */
  110. if (le16_to_cpu(us->pusb_dev->descriptor.idVendor) == USB_VENDOR_ID_GENESYS &&
  111. sdev->request_queue->max_sectors > 64)
  112. blk_queue_max_sectors(sdev->request_queue, 64);
  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. /* Some devices report a SCSI revision level above 2 but are
  143. * unable to handle the REPORT LUNS command (for which
  144. * support is mandatory at level 3). Since we already have
  145. * a Get-Max-LUN request, we won't lose much by setting the
  146. * revision level down to 2. The only devices that would be
  147. * affected are those with sparse LUNs. */
  148. sdev->scsi_level = SCSI_2;
  149. /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
  150. * Hardware Error) when any low-level error occurs,
  151. * recoverable or not. Setting this flag tells the SCSI
  152. * midlayer to retry such commands, which frequently will
  153. * succeed and fix the error. The worst this can lead to
  154. * is an occasional series of retries that will all fail. */
  155. sdev->retry_hwerror = 1;
  156. } else {
  157. /* Non-disk-type devices don't need to blacklist any pages
  158. * or to force 192-byte transfer lengths for MODE SENSE.
  159. * But they do need to use MODE SENSE(10). */
  160. sdev->use_10_for_ms = 1;
  161. }
  162. /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
  163. * REMOVAL command, so suppress those commands. */
  164. if (us->flags & US_FL_NOT_LOCKABLE)
  165. sdev->lockable = 0;
  166. /* this is to satisfy the compiler, tho I don't think the
  167. * return code is ever checked anywhere. */
  168. return 0;
  169. }
  170. /* queue a command */
  171. /* This is always called with scsi_lock(host) held */
  172. static int queuecommand(struct scsi_cmnd *srb,
  173. void (*done)(struct scsi_cmnd *))
  174. {
  175. struct us_data *us = host_to_us(srb->device->host);
  176. US_DEBUGP("%s called\n", __FUNCTION__);
  177. /* check for state-transition errors */
  178. if (us->srb != NULL) {
  179. printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
  180. __FUNCTION__, us->srb);
  181. return SCSI_MLQUEUE_HOST_BUSY;
  182. }
  183. /* fail the command if we are disconnecting */
  184. if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
  185. US_DEBUGP("Fail command during disconnect\n");
  186. srb->result = DID_NO_CONNECT << 16;
  187. done(srb);
  188. return 0;
  189. }
  190. /* enqueue the command and wake up the control thread */
  191. srb->scsi_done = done;
  192. us->srb = srb;
  193. up(&(us->sema));
  194. return 0;
  195. }
  196. /***********************************************************************
  197. * Error handling functions
  198. ***********************************************************************/
  199. /* Command timeout and abort */
  200. static int command_abort(struct scsi_cmnd *srb)
  201. {
  202. struct us_data *us = host_to_us(srb->device->host);
  203. US_DEBUGP("%s called\n", __FUNCTION__);
  204. /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
  205. * bits are protected by the host lock. */
  206. scsi_lock(us_to_host(us));
  207. /* Is this command still active? */
  208. if (us->srb != srb) {
  209. scsi_unlock(us_to_host(us));
  210. US_DEBUGP ("-- nothing to abort\n");
  211. return FAILED;
  212. }
  213. /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
  214. * a device reset isn't already in progress (to avoid interfering
  215. * with the reset). Note that we must retain the host lock while
  216. * calling usb_stor_stop_transport(); otherwise it might interfere
  217. * with an auto-reset that begins as soon as we release the lock. */
  218. set_bit(US_FLIDX_TIMED_OUT, &us->flags);
  219. if (!test_bit(US_FLIDX_RESETTING, &us->flags)) {
  220. set_bit(US_FLIDX_ABORTING, &us->flags);
  221. usb_stor_stop_transport(us);
  222. }
  223. scsi_unlock(us_to_host(us));
  224. /* Wait for the aborted command to finish */
  225. wait_for_completion(&us->notify);
  226. return SUCCESS;
  227. }
  228. /* This invokes the transport reset mechanism to reset the state of the
  229. * device */
  230. static int device_reset(struct scsi_cmnd *srb)
  231. {
  232. struct us_data *us = host_to_us(srb->device->host);
  233. int result;
  234. US_DEBUGP("%s called\n", __FUNCTION__);
  235. /* lock the device pointers and do the reset */
  236. down(&(us->dev_semaphore));
  237. result = us->transport_reset(us);
  238. up(&(us->dev_semaphore));
  239. return result < 0 ? FAILED : SUCCESS;
  240. }
  241. /* Simulate a SCSI bus reset by resetting the device's USB port. */
  242. static int bus_reset(struct scsi_cmnd *srb)
  243. {
  244. struct us_data *us = host_to_us(srb->device->host);
  245. int result;
  246. US_DEBUGP("%s called\n", __FUNCTION__);
  247. down(&(us->dev_semaphore));
  248. result = usb_stor_port_reset(us);
  249. up(&(us->dev_semaphore));
  250. return result < 0 ? FAILED : SUCCESS;
  251. }
  252. /* Report a driver-initiated device reset to the SCSI layer.
  253. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  254. * The caller must own the SCSI host lock. */
  255. void usb_stor_report_device_reset(struct us_data *us)
  256. {
  257. int i;
  258. struct Scsi_Host *host = us_to_host(us);
  259. scsi_report_device_reset(host, 0, 0);
  260. if (us->flags & US_FL_SCM_MULT_TARG) {
  261. for (i = 1; i < host->max_id; ++i)
  262. scsi_report_device_reset(host, 0, i);
  263. }
  264. }
  265. /* Report a driver-initiated bus reset to the SCSI layer.
  266. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  267. * The caller must own the SCSI host lock. */
  268. void usb_stor_report_bus_reset(struct us_data *us)
  269. {
  270. scsi_report_bus_reset(us_to_host(us), 0);
  271. }
  272. /***********************************************************************
  273. * /proc/scsi/ functions
  274. ***********************************************************************/
  275. /* we use this macro to help us write into the buffer */
  276. #undef SPRINTF
  277. #define SPRINTF(args...) \
  278. do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
  279. static int proc_info (struct Scsi_Host *host, char *buffer,
  280. char **start, off_t offset, int length, int inout)
  281. {
  282. struct us_data *us = host_to_us(host);
  283. char *pos = buffer;
  284. const char *string;
  285. /* if someone is sending us data, just throw it away */
  286. if (inout)
  287. return length;
  288. /* print the controller name */
  289. SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
  290. /* print product, vendor, and serial number strings */
  291. if (us->pusb_dev->manufacturer)
  292. string = us->pusb_dev->manufacturer;
  293. else if (us->unusual_dev->vendorName)
  294. string = us->unusual_dev->vendorName;
  295. else
  296. string = "Unknown";
  297. SPRINTF(" Vendor: %s\n", string);
  298. if (us->pusb_dev->product)
  299. string = us->pusb_dev->product;
  300. else if (us->unusual_dev->productName)
  301. string = us->unusual_dev->productName;
  302. else
  303. string = "Unknown";
  304. SPRINTF(" Product: %s\n", string);
  305. if (us->pusb_dev->serial)
  306. string = us->pusb_dev->serial;
  307. else
  308. string = "None";
  309. SPRINTF("Serial Number: %s\n", string);
  310. /* show the protocol and transport */
  311. SPRINTF(" Protocol: %s\n", us->protocol_name);
  312. SPRINTF(" Transport: %s\n", us->transport_name);
  313. /* show the device flags */
  314. if (pos < buffer + length) {
  315. pos += sprintf(pos, " Quirks:");
  316. #define US_FLAG(name, value) \
  317. if (us->flags & value) pos += sprintf(pos, " " #name);
  318. US_DO_ALL_FLAGS
  319. #undef US_FLAG
  320. *(pos++) = '\n';
  321. }
  322. /*
  323. * Calculate start of next buffer, and return value.
  324. */
  325. *start = buffer + offset;
  326. if ((pos - buffer) < offset)
  327. return (0);
  328. else if ((pos - buffer - offset) < length)
  329. return (pos - buffer - offset);
  330. else
  331. return (length);
  332. }
  333. /***********************************************************************
  334. * Sysfs interface
  335. ***********************************************************************/
  336. /* Output routine for the sysfs max_sectors file */
  337. static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
  338. {
  339. struct scsi_device *sdev = to_scsi_device(dev);
  340. return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
  341. }
  342. /* Input routine for the sysfs max_sectors file */
  343. static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
  344. size_t count)
  345. {
  346. struct scsi_device *sdev = to_scsi_device(dev);
  347. unsigned short ms;
  348. if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
  349. blk_queue_max_sectors(sdev->request_queue, ms);
  350. return strlen(buf);
  351. }
  352. return -EINVAL;
  353. }
  354. static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
  355. store_max_sectors);
  356. static struct device_attribute *sysfs_device_attr_list[] = {
  357. &dev_attr_max_sectors,
  358. NULL,
  359. };
  360. /*
  361. * this defines our host template, with which we'll allocate hosts
  362. */
  363. struct scsi_host_template usb_stor_host_template = {
  364. /* basic userland interface stuff */
  365. .name = "usb-storage",
  366. .proc_name = "usb-storage",
  367. .proc_info = proc_info,
  368. .info = host_info,
  369. /* command interface -- queued only */
  370. .queuecommand = queuecommand,
  371. /* error and abort handlers */
  372. .eh_abort_handler = command_abort,
  373. .eh_device_reset_handler = device_reset,
  374. .eh_bus_reset_handler = bus_reset,
  375. /* queue commands only, only one command per LUN */
  376. .can_queue = 1,
  377. .cmd_per_lun = 1,
  378. /* unknown initiator id */
  379. .this_id = -1,
  380. .slave_alloc = slave_alloc,
  381. .slave_configure = slave_configure,
  382. /* lots of sg segments can be handled */
  383. .sg_tablesize = SG_ALL,
  384. /* limit the total size of a transfer to 120 KB */
  385. .max_sectors = 240,
  386. /* merge commands... this seems to help performance, but
  387. * periodically someone should test to see which setting is more
  388. * optimal.
  389. */
  390. .use_clustering = 1,
  391. /* emulated HBA */
  392. .emulated = 1,
  393. /* we do our own delay after a device or bus reset */
  394. .skip_settle_delay = 1,
  395. /* sysfs device attributes */
  396. .sdev_attrs = sysfs_device_attr_list,
  397. /* module management */
  398. .module = THIS_MODULE
  399. };
  400. /* To Report "Illegal Request: Invalid Field in CDB */
  401. unsigned char usb_stor_sense_invalidCDB[18] = {
  402. [0] = 0x70, /* current error */
  403. [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
  404. [7] = 0x0a, /* additional length */
  405. [12] = 0x24 /* Invalid Field in CDB */
  406. };