scsiglue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /* This is always called with scsi_lock(host) held */
  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. /* Is this command still active? */
  206. if (us->srb != srb) {
  207. US_DEBUGP ("-- nothing to abort\n");
  208. return FAILED;
  209. }
  210. /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
  211. * a device reset isn't already in progress (to avoid interfering
  212. * with the reset). To prevent races with auto-reset, we must
  213. * stop any ongoing USB transfers while still holding the host
  214. * lock. */
  215. set_bit(US_FLIDX_TIMED_OUT, &us->flags);
  216. if (!test_bit(US_FLIDX_RESETTING, &us->flags)) {
  217. set_bit(US_FLIDX_ABORTING, &us->flags);
  218. usb_stor_stop_transport(us);
  219. }
  220. /* Wait for the aborted command to finish */
  221. wait_for_completion(&us->notify);
  222. /* Reacquire the lock and allow USB transfers to resume */
  223. clear_bit(US_FLIDX_ABORTING, &us->flags);
  224. clear_bit(US_FLIDX_TIMED_OUT, &us->flags);
  225. return SUCCESS;
  226. }
  227. /* This invokes the transport reset mechanism to reset the state of the
  228. * device */
  229. /* This is always called with scsi_lock(host) held */
  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. /* This is always called with scsi_lock(host) held */
  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. down(&(us->dev_semaphore));
  249. result = usb_stor_port_reset(us);
  250. up(&(us->dev_semaphore));
  251. /* lock the host for the return */
  252. return result < 0 ? FAILED : SUCCESS;
  253. }
  254. /* Report a driver-initiated device reset to the SCSI layer.
  255. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  256. * The caller must own the SCSI host lock. */
  257. void usb_stor_report_device_reset(struct us_data *us)
  258. {
  259. int i;
  260. struct Scsi_Host *host = us_to_host(us);
  261. scsi_report_device_reset(host, 0, 0);
  262. if (us->flags & US_FL_SCM_MULT_TARG) {
  263. for (i = 1; i < host->max_id; ++i)
  264. scsi_report_device_reset(host, 0, i);
  265. }
  266. }
  267. /* Report a driver-initiated bus reset to the SCSI layer.
  268. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  269. * The caller must own the SCSI host lock. */
  270. void usb_stor_report_bus_reset(struct us_data *us)
  271. {
  272. scsi_report_bus_reset(us_to_host(us), 0);
  273. }
  274. /***********************************************************************
  275. * /proc/scsi/ functions
  276. ***********************************************************************/
  277. /* we use this macro to help us write into the buffer */
  278. #undef SPRINTF
  279. #define SPRINTF(args...) \
  280. do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
  281. static int proc_info (struct Scsi_Host *host, char *buffer,
  282. char **start, off_t offset, int length, int inout)
  283. {
  284. struct us_data *us = host_to_us(host);
  285. char *pos = buffer;
  286. const char *string;
  287. /* if someone is sending us data, just throw it away */
  288. if (inout)
  289. return length;
  290. /* print the controller name */
  291. SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
  292. /* print product, vendor, and serial number strings */
  293. if (us->pusb_dev->manufacturer)
  294. string = us->pusb_dev->manufacturer;
  295. else if (us->unusual_dev->vendorName)
  296. string = us->unusual_dev->vendorName;
  297. else
  298. string = "Unknown";
  299. SPRINTF(" Vendor: %s\n", string);
  300. if (us->pusb_dev->product)
  301. string = us->pusb_dev->product;
  302. else if (us->unusual_dev->productName)
  303. string = us->unusual_dev->productName;
  304. else
  305. string = "Unknown";
  306. SPRINTF(" Product: %s\n", string);
  307. if (us->pusb_dev->serial)
  308. string = us->pusb_dev->serial;
  309. else
  310. string = "None";
  311. SPRINTF("Serial Number: %s\n", string);
  312. /* show the protocol and transport */
  313. SPRINTF(" Protocol: %s\n", us->protocol_name);
  314. SPRINTF(" Transport: %s\n", us->transport_name);
  315. /* show the device flags */
  316. if (pos < buffer + length) {
  317. pos += sprintf(pos, " Quirks:");
  318. #define US_FLAG(name, value) \
  319. if (us->flags & value) pos += sprintf(pos, " " #name);
  320. US_DO_ALL_FLAGS
  321. #undef US_FLAG
  322. *(pos++) = '\n';
  323. }
  324. /*
  325. * Calculate start of next buffer, and return value.
  326. */
  327. *start = buffer + offset;
  328. if ((pos - buffer) < offset)
  329. return (0);
  330. else if ((pos - buffer - offset) < length)
  331. return (pos - buffer - offset);
  332. else
  333. return (length);
  334. }
  335. /***********************************************************************
  336. * Sysfs interface
  337. ***********************************************************************/
  338. /* Output routine for the sysfs max_sectors file */
  339. static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
  340. {
  341. struct scsi_device *sdev = to_scsi_device(dev);
  342. return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
  343. }
  344. /* Input routine for the sysfs max_sectors file */
  345. static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
  346. size_t count)
  347. {
  348. struct scsi_device *sdev = to_scsi_device(dev);
  349. unsigned short ms;
  350. if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
  351. blk_queue_max_sectors(sdev->request_queue, ms);
  352. return strlen(buf);
  353. }
  354. return -EINVAL;
  355. }
  356. static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
  357. store_max_sectors);
  358. static struct device_attribute *sysfs_device_attr_list[] = {
  359. &dev_attr_max_sectors,
  360. NULL,
  361. };
  362. /*
  363. * this defines our host template, with which we'll allocate hosts
  364. */
  365. struct scsi_host_template usb_stor_host_template = {
  366. /* basic userland interface stuff */
  367. .name = "usb-storage",
  368. .proc_name = "usb-storage",
  369. .proc_info = proc_info,
  370. .info = host_info,
  371. /* command interface -- queued only */
  372. .queuecommand = queuecommand,
  373. /* error and abort handlers */
  374. .eh_abort_handler = command_abort,
  375. .eh_device_reset_handler = device_reset,
  376. .eh_bus_reset_handler = bus_reset,
  377. /* queue commands only, only one command per LUN */
  378. .can_queue = 1,
  379. .cmd_per_lun = 1,
  380. /* unknown initiator id */
  381. .this_id = -1,
  382. .slave_alloc = slave_alloc,
  383. .slave_configure = slave_configure,
  384. /* lots of sg segments can be handled */
  385. .sg_tablesize = SG_ALL,
  386. /* limit the total size of a transfer to 120 KB */
  387. .max_sectors = 240,
  388. /* merge commands... this seems to help performance, but
  389. * periodically someone should test to see which setting is more
  390. * optimal.
  391. */
  392. .use_clustering = 1,
  393. /* emulated HBA */
  394. .emulated = 1,
  395. /* we do our own delay after a device or bus reset */
  396. .skip_settle_delay = 1,
  397. /* sysfs device attributes */
  398. .sdev_attrs = sysfs_device_attr_list,
  399. /* module management */
  400. .module = THIS_MODULE
  401. };
  402. /* To Report "Illegal Request: Invalid Field in CDB */
  403. unsigned char usb_stor_sense_invalidCDB[18] = {
  404. [0] = 0x70, /* current error */
  405. [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
  406. [7] = 0x0a, /* additional length */
  407. [12] = 0x24 /* Invalid Field in CDB */
  408. };