scsiglue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for USB Mass Storage compliant devices
  4. * SCSI layer glue code
  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. #include <linux/blkdev.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/module.h>
  33. #include <linux/mutex.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <scsi/scsi_devinfo.h>
  37. #include <scsi/scsi_device.h>
  38. #include <scsi/scsi_eh.h>
  39. #include "usb.h"
  40. #include "scsiglue.h"
  41. #include "debug.h"
  42. #include "transport.h"
  43. #include "protocol.h"
  44. /*
  45. * Vendor IDs for companies that seem to include the READ CAPACITY bug
  46. * in all their devices
  47. */
  48. #define VENDOR_ID_NOKIA 0x0421
  49. #define VENDOR_ID_NIKON 0x04b0
  50. #define VENDOR_ID_PENTAX 0x0a17
  51. #define VENDOR_ID_MOTOROLA 0x22b8
  52. /***********************************************************************
  53. * Host functions
  54. ***********************************************************************/
  55. static const char* host_info(struct Scsi_Host *host)
  56. {
  57. struct us_data *us = host_to_us(host);
  58. return us->scsi_name;
  59. }
  60. static int slave_alloc (struct scsi_device *sdev)
  61. {
  62. struct us_data *us = host_to_us(sdev->host);
  63. /*
  64. * Set the INQUIRY transfer length to 36. We don't use any of
  65. * the extra data and many devices choke if asked for more or
  66. * less than 36 bytes.
  67. */
  68. sdev->inquiry_len = 36;
  69. /* Tell the SCSI layer if we know there is more than one LUN */
  70. if (us->protocol == USB_PR_BULK && us->max_lun > 0)
  71. sdev->sdev_bflags |= BLIST_FORCELUN;
  72. /*
  73. * Some USB storage devices reset if the IO advice hints grouping mode
  74. * page is queried. Hence skip that mode page.
  75. */
  76. sdev->sdev_bflags |= BLIST_SKIP_IO_HINTS;
  77. return 0;
  78. }
  79. static int device_configure(struct scsi_device *sdev, struct queue_limits *lim)
  80. {
  81. struct us_data *us = host_to_us(sdev->host);
  82. struct device *dev = us->pusb_dev->bus->sysdev;
  83. /*
  84. * Many devices have trouble transferring more than 32KB at a time,
  85. * while others have trouble with more than 64K. At this time we
  86. * are limiting both to 32K (64 sectores).
  87. */
  88. if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
  89. unsigned int max_sectors = 64;
  90. if (us->fflags & US_FL_MAX_SECTORS_MIN)
  91. max_sectors = PAGE_SIZE >> 9;
  92. lim->max_hw_sectors = min(lim->max_hw_sectors, max_sectors);
  93. } else if (sdev->type == TYPE_TAPE) {
  94. /*
  95. * Tapes need much higher max_sector limits, so just
  96. * raise it to the maximum possible (4 GB / 512) and
  97. * let the queue segment size sort out the real limit.
  98. */
  99. lim->max_hw_sectors = 0x7FFFFF;
  100. } else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
  101. /*
  102. * USB3 devices will be limited to 2048 sectors. This gives us
  103. * better throughput on most devices.
  104. */
  105. lim->max_hw_sectors = 2048;
  106. }
  107. /*
  108. * The max_hw_sectors should be up to maximum size of a mapping for
  109. * the device. Otherwise, a DMA API might fail on swiotlb environment.
  110. */
  111. lim->max_hw_sectors = min_t(size_t,
  112. lim->max_hw_sectors, dma_max_mapping_size(dev) >> SECTOR_SHIFT);
  113. /*
  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. */
  118. if (sdev->type == TYPE_DISK) {
  119. /*
  120. * Some vendors seem to put the READ CAPACITY bug into
  121. * all their devices -- primarily makers of cell phones
  122. * and digital cameras. Since these devices always use
  123. * flash media and can be expected to have an even number
  124. * of sectors, we will always enable the CAPACITY_HEURISTICS
  125. * flag unless told otherwise.
  126. */
  127. switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
  128. case VENDOR_ID_NOKIA:
  129. case VENDOR_ID_NIKON:
  130. case VENDOR_ID_PENTAX:
  131. case VENDOR_ID_MOTOROLA:
  132. if (!(us->fflags & (US_FL_FIX_CAPACITY |
  133. US_FL_CAPACITY_OK)))
  134. us->fflags |= US_FL_CAPACITY_HEURISTICS;
  135. break;
  136. }
  137. /*
  138. * Disk-type devices use MODE SENSE(6) if the protocol
  139. * (SubClass) is Transparent SCSI, otherwise they use
  140. * MODE SENSE(10).
  141. */
  142. if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
  143. sdev->use_10_for_ms = 1;
  144. /*
  145. *Many disks only accept MODE SENSE transfer lengths of
  146. * 192 bytes (that's what Windows uses).
  147. */
  148. sdev->use_192_bytes_for_3f = 1;
  149. /*
  150. * Some devices report generic values until the media has been
  151. * accessed. Force a READ(10) prior to querying device
  152. * characteristics.
  153. */
  154. sdev->read_before_ms = 1;
  155. /*
  156. * Some devices don't like MODE SENSE with page=0x3f,
  157. * which is the command used for checking if a device
  158. * is write-protected. Now that we tell the sd driver
  159. * to do a 192-byte transfer with this command the
  160. * majority of devices work fine, but a few still can't
  161. * handle it. The sd driver will simply assume those
  162. * devices are write-enabled.
  163. */
  164. if (us->fflags & US_FL_NO_WP_DETECT)
  165. sdev->skip_ms_page_3f = 1;
  166. /*
  167. * A number of devices have problems with MODE SENSE for
  168. * page x08, so we will skip it.
  169. */
  170. sdev->skip_ms_page_8 = 1;
  171. /*
  172. * Some devices don't handle VPD pages correctly, so skip vpd
  173. * pages if not forced by SCSI layer.
  174. */
  175. sdev->skip_vpd_pages = !sdev->try_vpd_pages;
  176. /* Do not attempt to use REPORT SUPPORTED OPERATION CODES */
  177. sdev->no_report_opcodes = 1;
  178. /* Do not attempt to use WRITE SAME */
  179. sdev->no_write_same = 1;
  180. /*
  181. * Some disks return the total number of blocks in response
  182. * to READ CAPACITY rather than the highest block number.
  183. * If this device makes that mistake, tell the sd driver.
  184. */
  185. if (us->fflags & US_FL_FIX_CAPACITY)
  186. sdev->fix_capacity = 1;
  187. /*
  188. * A few disks have two indistinguishable version, one of
  189. * which reports the correct capacity and the other does not.
  190. * The sd driver has to guess which is the case.
  191. */
  192. if (us->fflags & US_FL_CAPACITY_HEURISTICS)
  193. sdev->guess_capacity = 1;
  194. /* Some devices cannot handle READ_CAPACITY_16 */
  195. if (us->fflags & US_FL_NO_READ_CAPACITY_16)
  196. sdev->no_read_capacity_16 = 1;
  197. /*
  198. * Many devices do not respond properly to READ_CAPACITY_16.
  199. * Tell the SCSI layer to try READ_CAPACITY_10 first.
  200. * However some USB 3.0 drive enclosures return capacity
  201. * modulo 2TB. Those must use READ_CAPACITY_16
  202. */
  203. if (!(us->fflags & US_FL_NEEDS_CAP16))
  204. sdev->try_rc_10_first = 1;
  205. /*
  206. * assume SPC3 or latter devices support sense size > 18
  207. * unless US_FL_BAD_SENSE quirk is specified.
  208. */
  209. if (sdev->scsi_level > SCSI_SPC_2 &&
  210. !(us->fflags & US_FL_BAD_SENSE))
  211. us->fflags |= US_FL_SANE_SENSE;
  212. /*
  213. * USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
  214. * Hardware Error) when any low-level error occurs,
  215. * recoverable or not. Setting this flag tells the SCSI
  216. * midlayer to retry such commands, which frequently will
  217. * succeed and fix the error. The worst this can lead to
  218. * is an occasional series of retries that will all fail.
  219. */
  220. sdev->retry_hwerror = 1;
  221. /*
  222. * USB disks should allow restart. Some drives spin down
  223. * automatically, requiring a START-STOP UNIT command.
  224. */
  225. sdev->allow_restart = 1;
  226. /*
  227. * Some USB cardreaders have trouble reading an sdcard's last
  228. * sector in a larger then 1 sector read, since the performance
  229. * impact is negligible we set this flag for all USB disks
  230. */
  231. sdev->last_sector_bug = 1;
  232. /*
  233. * Enable last-sector hacks for single-target devices using
  234. * the Bulk-only transport, unless we already know the
  235. * capacity will be decremented or is correct.
  236. */
  237. if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
  238. US_FL_SCM_MULT_TARG)) &&
  239. us->protocol == USB_PR_BULK)
  240. us->use_last_sector_hacks = 1;
  241. /* Check if write cache default on flag is set or not */
  242. if (us->fflags & US_FL_WRITE_CACHE)
  243. sdev->wce_default_on = 1;
  244. /* A few buggy USB-ATA bridges don't understand FUA */
  245. if (us->fflags & US_FL_BROKEN_FUA)
  246. sdev->broken_fua = 1;
  247. /* Some even totally fail to indicate a cache */
  248. if (us->fflags & US_FL_ALWAYS_SYNC) {
  249. /* don't read caching information */
  250. sdev->skip_ms_page_8 = 1;
  251. sdev->skip_ms_page_3f = 1;
  252. /* assume sync is needed */
  253. sdev->wce_default_on = 1;
  254. }
  255. } else {
  256. /*
  257. * Non-disk-type devices don't need to ignore any pages
  258. * or to force 192-byte transfer lengths for MODE SENSE.
  259. * But they do need to use MODE SENSE(10).
  260. */
  261. sdev->use_10_for_ms = 1;
  262. /* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
  263. if (us->fflags & US_FL_NO_READ_DISC_INFO)
  264. sdev->no_read_disc_info = 1;
  265. }
  266. /*
  267. * The CB and CBI transports have no way to pass LUN values
  268. * other than the bits in the second byte of a CDB. But those
  269. * bits don't get set to the LUN value if the device reports
  270. * scsi_level == 0 (UNKNOWN). Hence such devices must necessarily
  271. * be single-LUN.
  272. */
  273. if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
  274. sdev->scsi_level == SCSI_UNKNOWN)
  275. us->max_lun = 0;
  276. /*
  277. * Some devices choke when they receive a PREVENT-ALLOW MEDIUM
  278. * REMOVAL command, so suppress those commands.
  279. */
  280. if (us->fflags & US_FL_NOT_LOCKABLE)
  281. sdev->lockable = 0;
  282. /*
  283. * this is to satisfy the compiler, tho I don't think the
  284. * return code is ever checked anywhere.
  285. */
  286. return 0;
  287. }
  288. static int target_alloc(struct scsi_target *starget)
  289. {
  290. struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
  291. /*
  292. * Some USB drives don't support REPORT LUNS, even though they
  293. * report a SCSI revision level above 2. Tell the SCSI layer
  294. * not to issue that command; it will perform a normal sequential
  295. * scan instead.
  296. */
  297. starget->no_report_luns = 1;
  298. /*
  299. * The UFI spec treats the Peripheral Qualifier bits in an
  300. * INQUIRY result as reserved and requires devices to set them
  301. * to 0. However the SCSI spec requires these bits to be set
  302. * to 3 to indicate when a LUN is not present.
  303. *
  304. * Let the scanning code know if this target merely sets
  305. * Peripheral Device Type to 0x1f to indicate no LUN.
  306. */
  307. if (us->subclass == USB_SC_UFI)
  308. starget->pdt_1f_for_no_lun = 1;
  309. return 0;
  310. }
  311. /* queue a command */
  312. /* This is always called with scsi_lock(host) held */
  313. static int queuecommand_lck(struct scsi_cmnd *srb)
  314. {
  315. void (*done)(struct scsi_cmnd *) = scsi_done;
  316. struct us_data *us = host_to_us(srb->device->host);
  317. /* check for state-transition errors */
  318. if (us->srb != NULL) {
  319. dev_err(&us->pusb_intf->dev,
  320. "Error in %s: us->srb = %p\n", __func__, us->srb);
  321. return SCSI_MLQUEUE_HOST_BUSY;
  322. }
  323. /* fail the command if we are disconnecting */
  324. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  325. usb_stor_dbg(us, "Fail command during disconnect\n");
  326. srb->result = DID_NO_CONNECT << 16;
  327. done(srb);
  328. return 0;
  329. }
  330. if ((us->fflags & US_FL_NO_ATA_1X) &&
  331. (srb->cmnd[0] == ATA_12 || srb->cmnd[0] == ATA_16)) {
  332. memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
  333. sizeof(usb_stor_sense_invalidCDB));
  334. srb->result = SAM_STAT_CHECK_CONDITION;
  335. done(srb);
  336. return 0;
  337. }
  338. /* enqueue the command and wake up the control thread */
  339. us->srb = srb;
  340. complete(&us->cmnd_ready);
  341. return 0;
  342. }
  343. static DEF_SCSI_QCMD(queuecommand)
  344. /***********************************************************************
  345. * Error handling functions
  346. ***********************************************************************/
  347. /* Command timeout and abort */
  348. static int command_abort_matching(struct us_data *us, struct scsi_cmnd *srb_match)
  349. {
  350. /*
  351. * us->srb together with the TIMED_OUT, RESETTING, and ABORTING
  352. * bits are protected by the host lock.
  353. */
  354. scsi_lock(us_to_host(us));
  355. /* is there any active pending command to abort ? */
  356. if (!us->srb) {
  357. scsi_unlock(us_to_host(us));
  358. usb_stor_dbg(us, "-- nothing to abort\n");
  359. return SUCCESS;
  360. }
  361. /* Does the command match the passed srb if any ? */
  362. if (srb_match && us->srb != srb_match) {
  363. scsi_unlock(us_to_host(us));
  364. usb_stor_dbg(us, "-- pending command mismatch\n");
  365. return FAILED;
  366. }
  367. /*
  368. * Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
  369. * a device reset isn't already in progress (to avoid interfering
  370. * with the reset). Note that we must retain the host lock while
  371. * calling usb_stor_stop_transport(); otherwise it might interfere
  372. * with an auto-reset that begins as soon as we release the lock.
  373. */
  374. set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
  375. if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
  376. set_bit(US_FLIDX_ABORTING, &us->dflags);
  377. usb_stor_stop_transport(us);
  378. }
  379. scsi_unlock(us_to_host(us));
  380. /* Wait for the aborted command to finish */
  381. wait_for_completion(&us->notify);
  382. return SUCCESS;
  383. }
  384. static int command_abort(struct scsi_cmnd *srb)
  385. {
  386. struct us_data *us = host_to_us(srb->device->host);
  387. usb_stor_dbg(us, "%s called\n", __func__);
  388. return command_abort_matching(us, srb);
  389. }
  390. /*
  391. * This invokes the transport reset mechanism to reset the state of the
  392. * device
  393. */
  394. static int device_reset(struct scsi_cmnd *srb)
  395. {
  396. struct us_data *us = host_to_us(srb->device->host);
  397. int result;
  398. usb_stor_dbg(us, "%s called\n", __func__);
  399. /* abort any pending command before reset */
  400. command_abort_matching(us, NULL);
  401. /* lock the device pointers and do the reset */
  402. mutex_lock(&(us->dev_mutex));
  403. result = us->transport_reset(us);
  404. mutex_unlock(&us->dev_mutex);
  405. return result < 0 ? FAILED : SUCCESS;
  406. }
  407. /* Simulate a SCSI bus reset by resetting the device's USB port. */
  408. static int bus_reset(struct scsi_cmnd *srb)
  409. {
  410. struct us_data *us = host_to_us(srb->device->host);
  411. int result;
  412. usb_stor_dbg(us, "%s called\n", __func__);
  413. result = usb_stor_port_reset(us);
  414. return result < 0 ? FAILED : SUCCESS;
  415. }
  416. /*
  417. * Report a driver-initiated device reset to the SCSI layer.
  418. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  419. * The caller must own the SCSI host lock.
  420. */
  421. void usb_stor_report_device_reset(struct us_data *us)
  422. {
  423. int i;
  424. struct Scsi_Host *host = us_to_host(us);
  425. scsi_report_device_reset(host, 0, 0);
  426. if (us->fflags & US_FL_SCM_MULT_TARG) {
  427. for (i = 1; i < host->max_id; ++i)
  428. scsi_report_device_reset(host, 0, i);
  429. }
  430. }
  431. /*
  432. * Report a driver-initiated bus reset to the SCSI layer.
  433. * Calling this for a SCSI-initiated reset is unnecessary but harmless.
  434. * The caller must not own the SCSI host lock.
  435. */
  436. void usb_stor_report_bus_reset(struct us_data *us)
  437. {
  438. struct Scsi_Host *host = us_to_host(us);
  439. scsi_lock(host);
  440. scsi_report_bus_reset(host, 0);
  441. scsi_unlock(host);
  442. }
  443. /***********************************************************************
  444. * /proc/scsi/ functions
  445. ***********************************************************************/
  446. static int write_info(struct Scsi_Host *host, char *buffer, int length)
  447. {
  448. /* if someone is sending us data, just throw it away */
  449. return length;
  450. }
  451. static int show_info (struct seq_file *m, struct Scsi_Host *host)
  452. {
  453. struct us_data *us = host_to_us(host);
  454. const char *string;
  455. /* print the controller name */
  456. seq_printf(m, " Host scsi%d: usb-storage\n", host->host_no);
  457. /* print product, vendor, and serial number strings */
  458. if (us->pusb_dev->manufacturer)
  459. string = us->pusb_dev->manufacturer;
  460. else if (us->unusual_dev->vendorName)
  461. string = us->unusual_dev->vendorName;
  462. else
  463. string = "Unknown";
  464. seq_printf(m, " Vendor: %s\n", string);
  465. if (us->pusb_dev->product)
  466. string = us->pusb_dev->product;
  467. else if (us->unusual_dev->productName)
  468. string = us->unusual_dev->productName;
  469. else
  470. string = "Unknown";
  471. seq_printf(m, " Product: %s\n", string);
  472. if (us->pusb_dev->serial)
  473. string = us->pusb_dev->serial;
  474. else
  475. string = "None";
  476. seq_printf(m, "Serial Number: %s\n", string);
  477. /* show the protocol and transport */
  478. seq_printf(m, " Protocol: %s\n", us->protocol_name);
  479. seq_printf(m, " Transport: %s\n", us->transport_name);
  480. /* show the device flags */
  481. seq_printf(m, " Quirks:");
  482. #define US_FLAG(name, value) \
  483. if (us->fflags & value) seq_printf(m, " " #name);
  484. US_DO_ALL_FLAGS
  485. #undef US_FLAG
  486. seq_putc(m, '\n');
  487. return 0;
  488. }
  489. /***********************************************************************
  490. * Sysfs interface
  491. ***********************************************************************/
  492. /* Output routine for the sysfs max_sectors file */
  493. static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
  494. {
  495. struct scsi_device *sdev = to_scsi_device(dev);
  496. return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
  497. }
  498. /* Input routine for the sysfs max_sectors file */
  499. static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
  500. size_t count)
  501. {
  502. struct scsi_device *sdev = to_scsi_device(dev);
  503. struct queue_limits lim;
  504. unsigned short ms;
  505. int ret;
  506. if (sscanf(buf, "%hu", &ms) <= 0)
  507. return -EINVAL;
  508. blk_mq_freeze_queue(sdev->request_queue);
  509. lim = queue_limits_start_update(sdev->request_queue);
  510. lim.max_hw_sectors = ms;
  511. ret = queue_limits_commit_update(sdev->request_queue, &lim);
  512. blk_mq_unfreeze_queue(sdev->request_queue);
  513. if (ret)
  514. return ret;
  515. return count;
  516. }
  517. static DEVICE_ATTR_RW(max_sectors);
  518. static struct attribute *usb_sdev_attrs[] = {
  519. &dev_attr_max_sectors.attr,
  520. NULL,
  521. };
  522. ATTRIBUTE_GROUPS(usb_sdev);
  523. /*
  524. * this defines our host template, with which we'll allocate hosts
  525. */
  526. static const struct scsi_host_template usb_stor_host_template = {
  527. /* basic userland interface stuff */
  528. .name = "usb-storage",
  529. .proc_name = "usb-storage",
  530. .show_info = show_info,
  531. .write_info = write_info,
  532. .info = host_info,
  533. /* command interface -- queued only */
  534. .queuecommand = queuecommand,
  535. /* error and abort handlers */
  536. .eh_abort_handler = command_abort,
  537. .eh_device_reset_handler = device_reset,
  538. .eh_bus_reset_handler = bus_reset,
  539. /* queue commands only, only one command per LUN */
  540. .can_queue = 1,
  541. /* unknown initiator id */
  542. .this_id = -1,
  543. .slave_alloc = slave_alloc,
  544. .device_configure = device_configure,
  545. .target_alloc = target_alloc,
  546. /* lots of sg segments can be handled */
  547. .sg_tablesize = SG_MAX_SEGMENTS,
  548. /*
  549. * Some host controllers may have alignment requirements.
  550. * We'll play it safe by requiring 512-byte alignment always.
  551. */
  552. .dma_alignment = 511,
  553. /*
  554. * Limit the total size of a transfer to 120 KB.
  555. *
  556. * Some devices are known to choke with anything larger. It seems like
  557. * the problem stems from the fact that original IDE controllers had
  558. * only an 8-bit register to hold the number of sectors in one transfer
  559. * and even those couldn't handle a full 256 sectors.
  560. *
  561. * Because we want to make sure we interoperate with as many devices as
  562. * possible, we will maintain a 240 sector transfer size limit for USB
  563. * Mass Storage devices.
  564. *
  565. * Tests show that other operating have similar limits with Microsoft
  566. * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
  567. * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
  568. * and 2048 for USB3 devices.
  569. */
  570. .max_sectors = 240,
  571. /* emulated HBA */
  572. .emulated = 1,
  573. /* we do our own delay after a device or bus reset */
  574. .skip_settle_delay = 1,
  575. /* sysfs device attributes */
  576. .sdev_groups = usb_sdev_groups,
  577. /* module management */
  578. .module = THIS_MODULE
  579. };
  580. void usb_stor_host_template_init(struct scsi_host_template *sht,
  581. const char *name, struct module *owner)
  582. {
  583. *sht = usb_stor_host_template;
  584. sht->name = name;
  585. sht->proc_name = name;
  586. sht->module = owner;
  587. }
  588. EXPORT_SYMBOL_GPL(usb_stor_host_template_init);
  589. /* To Report "Illegal Request: Invalid Field in CDB */
  590. unsigned char usb_stor_sense_invalidCDB[18] = {
  591. [0] = 0x70, /* current error */
  592. [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
  593. [7] = 0x0a, /* additional length */
  594. [12] = 0x24 /* Invalid Field in CDB */
  595. };
  596. EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);