scsiglue.c 20 KB

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