scsiglue.c 15 KB

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