15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2f0183a33SFelipe Balbi /*
3f0183a33SFelipe Balbi * Driver for USB Mass Storage compliant devices
41da177e4SLinus Torvalds * SCSI layer glue code
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Current development and maintenance by:
71da177e4SLinus Torvalds * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Developed with the assistance of:
101da177e4SLinus Torvalds * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
111da177e4SLinus Torvalds * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
121da177e4SLinus Torvalds *
131da177e4SLinus Torvalds * Initial work by:
141da177e4SLinus Torvalds * (c) 1999 Michael Gee (michael@linuxspecific.com)
151da177e4SLinus Torvalds *
161da177e4SLinus Torvalds * This driver is based on the 'USB Mass Storage Class' document. This
171da177e4SLinus Torvalds * describes in detail the protocol used to communicate with such
181da177e4SLinus Torvalds * devices. Clearly, the designers had SCSI and ATAPI commands in
191da177e4SLinus Torvalds * mind when they created this document. The commands are all very
201da177e4SLinus Torvalds * similar to commands in the SCSI-II and ATAPI specifications.
211da177e4SLinus Torvalds *
221da177e4SLinus Torvalds * It is important to note that in a number of cases this class
231da177e4SLinus Torvalds * exhibits class-specific exemptions from the USB specification.
241da177e4SLinus Torvalds * Notably the usage of NAK, STALL and ACK differs from the norm, in
251da177e4SLinus Torvalds * that they are used to communicate wait, failed and OK on commands.
261da177e4SLinus Torvalds *
271da177e4SLinus Torvalds * Also, for certain devices, the interrupt endpoint is used to convey
281da177e4SLinus Torvalds * status of a command.
291da177e4SLinus Torvalds */
301da177e4SLinus Torvalds
31d74ffae8SYoshihiro Shimoda #include <linux/blkdev.h>
32d74ffae8SYoshihiro Shimoda #include <linux/dma-mapping.h>
331da177e4SLinus Torvalds #include <linux/module.h>
344186ecf8SArjan van de Ven #include <linux/mutex.h>
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds #include <scsi/scsi.h>
371da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
381da177e4SLinus Torvalds #include <scsi/scsi_devinfo.h>
391da177e4SLinus Torvalds #include <scsi/scsi_device.h>
401da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds #include "usb.h"
43bf89015aSChristoph Hellwig #include <linux/usb/hcd.h>
441da177e4SLinus Torvalds #include "scsiglue.h"
451da177e4SLinus Torvalds #include "debug.h"
461da177e4SLinus Torvalds #include "transport.h"
471da177e4SLinus Torvalds #include "protocol.h"
481da177e4SLinus Torvalds
49f0183a33SFelipe Balbi /*
50f0183a33SFelipe Balbi * Vendor IDs for companies that seem to include the READ CAPACITY bug
51a81a81a2SAlan Stern * in all their devices
52a81a81a2SAlan Stern */
53a81a81a2SAlan Stern #define VENDOR_ID_NOKIA 0x0421
54a81a81a2SAlan Stern #define VENDOR_ID_NIKON 0x04b0
55506e9469SAlan Stern #define VENDOR_ID_PENTAX 0x0a17
56a81a81a2SAlan Stern #define VENDOR_ID_MOTOROLA 0x22b8
57a81a81a2SAlan Stern
581da177e4SLinus Torvalds /***********************************************************************
591da177e4SLinus Torvalds * Host functions
601da177e4SLinus Torvalds ***********************************************************************/
611da177e4SLinus Torvalds
host_info(struct Scsi_Host * host)621da177e4SLinus Torvalds static const char* host_info(struct Scsi_Host *host)
631da177e4SLinus Torvalds {
6400fa43efSMatthew Wilcox struct us_data *us = host_to_us(host);
6500fa43efSMatthew Wilcox return us->scsi_name;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds
slave_alloc(struct scsi_device * sdev)681da177e4SLinus Torvalds static int slave_alloc (struct scsi_device *sdev)
691da177e4SLinus Torvalds {
70823d12c9SAlan Stern struct us_data *us = host_to_us(sdev->host);
71823d12c9SAlan Stern
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds * Set the INQUIRY transfer length to 36. We don't use any of
741da177e4SLinus Torvalds * the extra data and many devices choke if asked for more or
751da177e4SLinus Torvalds * less than 36 bytes.
761da177e4SLinus Torvalds */
771da177e4SLinus Torvalds sdev->inquiry_len = 36;
783a3416b1SAlan Stern
79f0183a33SFelipe Balbi /*
80747668dbSAlan Stern * Some host controllers may have alignment requirements.
81747668dbSAlan Stern * We'll play it safe by requiring 512-byte alignment always.
82148d9fe4SAlan Stern */
83f756cbd4SAlan Stern blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
84465ff318SJames Bottomley
85823d12c9SAlan Stern /* Tell the SCSI layer if we know there is more than one LUN */
86823d12c9SAlan Stern if (us->protocol == USB_PR_BULK && us->max_lun > 0)
87823d12c9SAlan Stern sdev->sdev_bflags |= BLIST_FORCELUN;
88823d12c9SAlan Stern
891da177e4SLinus Torvalds return 0;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds
slave_configure(struct scsi_device * sdev)921da177e4SLinus Torvalds static int slave_configure(struct scsi_device *sdev)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds struct us_data *us = host_to_us(sdev->host);
9597ad4a77SGreg Kroah-Hartman struct device *dev = us->pusb_dev->bus->sysdev;
961da177e4SLinus Torvalds
97f0183a33SFelipe Balbi /*
98f0183a33SFelipe Balbi * Many devices have trouble transferring more than 32KB at a time,
99883d989aSPhil Dibowitz * while others have trouble with more than 64K. At this time we
100883d989aSPhil Dibowitz * are limiting both to 32K (64 sectores).
101883d989aSPhil Dibowitz */
1027e4d6c38SAlan Stern if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
10333abc04fSDoug Maxey unsigned int max_sectors = 64;
10433abc04fSDoug Maxey
1057e4d6c38SAlan Stern if (us->fflags & US_FL_MAX_SECTORS_MIN)
10609cbfeafSKirill A. Shutemov max_sectors = PAGE_SIZE >> 9;
10749d6271bSAlan Stern if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
108086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(sdev->request_queue,
10933abc04fSDoug Maxey max_sectors);
1105c16034dSAlan Stern } else if (sdev->type == TYPE_TAPE) {
111f0183a33SFelipe Balbi /*
112f0183a33SFelipe Balbi * Tapes need much higher max_sector limits, so just
1135c16034dSAlan Stern * raise it to the maximum possible (4 GB / 512) and
1145c16034dSAlan Stern * let the queue segment size sort out the real limit.
1155c16034dSAlan Stern */
116086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
1175b91dfe1SFelipe Balbi } else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
118f0183a33SFelipe Balbi /*
119f0183a33SFelipe Balbi * USB3 devices will be limited to 2048 sectors. This gives us
1205b91dfe1SFelipe Balbi * better throughput on most devices.
1215b91dfe1SFelipe Balbi */
1225b91dfe1SFelipe Balbi blk_queue_max_hw_sectors(sdev->request_queue, 2048);
12333abc04fSDoug Maxey }
1241da177e4SLinus Torvalds
125f0183a33SFelipe Balbi /*
126d74ffae8SYoshihiro Shimoda * The max_hw_sectors should be up to maximum size of a mapping for
127d74ffae8SYoshihiro Shimoda * the device. Otherwise, a DMA API might fail on swiotlb environment.
128d74ffae8SYoshihiro Shimoda */
129d74ffae8SYoshihiro Shimoda blk_queue_max_hw_sectors(sdev->request_queue,
130d74ffae8SYoshihiro Shimoda min_t(size_t, queue_max_hw_sectors(sdev->request_queue),
131d74ffae8SYoshihiro Shimoda dma_max_mapping_size(dev) >> SECTOR_SHIFT));
132d74ffae8SYoshihiro Shimoda
133d74ffae8SYoshihiro Shimoda /*
134f0183a33SFelipe Balbi * Some USB host controllers can't do DMA; they have to use PIO.
135bf89015aSChristoph Hellwig * For such controllers we need to make sure the block layer sets
13696983d2dSAlan Stern * up bounce buffers in addressable memory.
13796983d2dSAlan Stern */
138f8c63edfSFredrik Noring if (!hcd_uses_dma(bus_to_hcd(us->pusb_dev->bus)) ||
139f8c63edfSFredrik Noring (bus_to_hcd(us->pusb_dev->bus)->localmem_pool != NULL))
14096983d2dSAlan Stern blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
14196983d2dSAlan Stern
142f0183a33SFelipe Balbi /*
143f0183a33SFelipe Balbi * We can't put these settings in slave_alloc() because that gets
1441da177e4SLinus Torvalds * called before the device type is known. Consequently these
145f0183a33SFelipe Balbi * settings can't be overridden via the scsi devinfo mechanism.
146f0183a33SFelipe Balbi */
1471da177e4SLinus Torvalds if (sdev->type == TYPE_DISK) {
1481da177e4SLinus Torvalds
149f0183a33SFelipe Balbi /*
150f0183a33SFelipe Balbi * Some vendors seem to put the READ CAPACITY bug into
151a81a81a2SAlan Stern * all their devices -- primarily makers of cell phones
152a81a81a2SAlan Stern * and digital cameras. Since these devices always use
153a81a81a2SAlan Stern * flash media and can be expected to have an even number
154a81a81a2SAlan Stern * of sectors, we will always enable the CAPACITY_HEURISTICS
155f0183a33SFelipe Balbi * flag unless told otherwise.
156f0183a33SFelipe Balbi */
157a81a81a2SAlan Stern switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
158a81a81a2SAlan Stern case VENDOR_ID_NOKIA:
159a81a81a2SAlan Stern case VENDOR_ID_NIKON:
160506e9469SAlan Stern case VENDOR_ID_PENTAX:
161a81a81a2SAlan Stern case VENDOR_ID_MOTOROLA:
162a81a81a2SAlan Stern if (!(us->fflags & (US_FL_FIX_CAPACITY |
163a81a81a2SAlan Stern US_FL_CAPACITY_OK)))
164a81a81a2SAlan Stern us->fflags |= US_FL_CAPACITY_HEURISTICS;
165a81a81a2SAlan Stern break;
166a81a81a2SAlan Stern }
167a81a81a2SAlan Stern
168f0183a33SFelipe Balbi /*
169f0183a33SFelipe Balbi * Disk-type devices use MODE SENSE(6) if the protocol
1701da177e4SLinus Torvalds * (SubClass) is Transparent SCSI, otherwise they use
171f0183a33SFelipe Balbi * MODE SENSE(10).
172f0183a33SFelipe Balbi */
1738fa7fd74SMichal Nazarewicz if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
1741da177e4SLinus Torvalds sdev->use_10_for_ms = 1;
1751da177e4SLinus Torvalds
176f0183a33SFelipe Balbi /*
177f0183a33SFelipe Balbi *Many disks only accept MODE SENSE transfer lengths of
178f0183a33SFelipe Balbi * 192 bytes (that's what Windows uses).
179f0183a33SFelipe Balbi */
1801da177e4SLinus Torvalds sdev->use_192_bytes_for_3f = 1;
1811da177e4SLinus Torvalds
182f0183a33SFelipe Balbi /*
183*9eb04addSMartin K. Petersen * Some devices report generic values until the media has been
184*9eb04addSMartin K. Petersen * accessed. Force a READ(10) prior to querying device
185*9eb04addSMartin K. Petersen * characteristics.
186*9eb04addSMartin K. Petersen */
187*9eb04addSMartin K. Petersen sdev->read_before_ms = 1;
188*9eb04addSMartin K. Petersen
189*9eb04addSMartin K. Petersen /*
190f0183a33SFelipe Balbi * Some devices don't like MODE SENSE with page=0x3f,
1911da177e4SLinus Torvalds * which is the command used for checking if a device
1921da177e4SLinus Torvalds * is write-protected. Now that we tell the sd driver
1931da177e4SLinus Torvalds * to do a 192-byte transfer with this command the
1941da177e4SLinus Torvalds * majority of devices work fine, but a few still can't
1951da177e4SLinus Torvalds * handle it. The sd driver will simply assume those
196f0183a33SFelipe Balbi * devices are write-enabled.
197f0183a33SFelipe Balbi */
1987e4d6c38SAlan Stern if (us->fflags & US_FL_NO_WP_DETECT)
1991da177e4SLinus Torvalds sdev->skip_ms_page_3f = 1;
2001da177e4SLinus Torvalds
201f0183a33SFelipe Balbi /*
202f0183a33SFelipe Balbi * A number of devices have problems with MODE SENSE for
203f0183a33SFelipe Balbi * page x08, so we will skip it.
204f0183a33SFelipe Balbi */
2051da177e4SLinus Torvalds sdev->skip_ms_page_8 = 1;
2061da177e4SLinus Torvalds
20734914878SMarcos Paulo de Souza /*
20834914878SMarcos Paulo de Souza * Some devices don't handle VPD pages correctly, so skip vpd
20934914878SMarcos Paulo de Souza * pages if not forced by SCSI layer.
21034914878SMarcos Paulo de Souza */
21134914878SMarcos Paulo de Souza sdev->skip_vpd_pages = !sdev->try_vpd_pages;
21209b6b51bSAlan Stern
2133c6bdaeaSMartin K. Petersen /* Do not attempt to use REPORT SUPPORTED OPERATION CODES */
2143c6bdaeaSMartin K. Petersen sdev->no_report_opcodes = 1;
2153c6bdaeaSMartin K. Petersen
2165db44863SMartin K. Petersen /* Do not attempt to use WRITE SAME */
2175db44863SMartin K. Petersen sdev->no_write_same = 1;
2185db44863SMartin K. Petersen
219f0183a33SFelipe Balbi /*
220f0183a33SFelipe Balbi * Some disks return the total number of blocks in response
2211da177e4SLinus Torvalds * to READ CAPACITY rather than the highest block number.
222f0183a33SFelipe Balbi * If this device makes that mistake, tell the sd driver.
223f0183a33SFelipe Balbi */
2247e4d6c38SAlan Stern if (us->fflags & US_FL_FIX_CAPACITY)
2251da177e4SLinus Torvalds sdev->fix_capacity = 1;
22686dbde9cSMatthew Dharm
227f0183a33SFelipe Balbi /*
228f0183a33SFelipe Balbi * A few disks have two indistinguishable version, one of
22961bf54b7SOliver Neukum * which reports the correct capacity and the other does not.
230f0183a33SFelipe Balbi * The sd driver has to guess which is the case.
231f0183a33SFelipe Balbi */
2327e4d6c38SAlan Stern if (us->fflags & US_FL_CAPACITY_HEURISTICS)
23361bf54b7SOliver Neukum sdev->guess_capacity = 1;
23461bf54b7SOliver Neukum
23500914025SHans de Goede /* Some devices cannot handle READ_CAPACITY_16 */
23600914025SHans de Goede if (us->fflags & US_FL_NO_READ_CAPACITY_16)
23700914025SHans de Goede sdev->no_read_capacity_16 = 1;
23800914025SHans de Goede
2396a0bdffaSAlan Stern /*
2406a0bdffaSAlan Stern * Many devices do not respond properly to READ_CAPACITY_16.
2416a0bdffaSAlan Stern * Tell the SCSI layer to try READ_CAPACITY_10 first.
24232c37fc3SOliver Neukum * However some USB 3.0 drive enclosures return capacity
24332c37fc3SOliver Neukum * modulo 2TB. Those must use READ_CAPACITY_16
2446a0bdffaSAlan Stern */
24532c37fc3SOliver Neukum if (!(us->fflags & US_FL_NEEDS_CAP16))
2466a0bdffaSAlan Stern sdev->try_rc_10_first = 1;
2476a0bdffaSAlan Stern
248c5603d2fSIcenowy Zheng /*
249c5603d2fSIcenowy Zheng * assume SPC3 or latter devices support sense size > 18
250c5603d2fSIcenowy Zheng * unless US_FL_BAD_SENSE quirk is specified.
251c5603d2fSIcenowy Zheng */
252c5603d2fSIcenowy Zheng if (sdev->scsi_level > SCSI_SPC_2 &&
253c5603d2fSIcenowy Zheng !(us->fflags & US_FL_BAD_SENSE))
2541537e0adSBen Efros us->fflags |= US_FL_SANE_SENSE;
2551537e0adSBen Efros
256f0183a33SFelipe Balbi /*
257f0183a33SFelipe Balbi * USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
25886dbde9cSMatthew Dharm * Hardware Error) when any low-level error occurs,
25986dbde9cSMatthew Dharm * recoverable or not. Setting this flag tells the SCSI
26086dbde9cSMatthew Dharm * midlayer to retry such commands, which frequently will
26186dbde9cSMatthew Dharm * succeed and fix the error. The worst this can lead to
262f0183a33SFelipe Balbi * is an occasional series of retries that will all fail.
263f0183a33SFelipe Balbi */
26486dbde9cSMatthew Dharm sdev->retry_hwerror = 1;
26586dbde9cSMatthew Dharm
266f0183a33SFelipe Balbi /*
267f0183a33SFelipe Balbi * USB disks should allow restart. Some drives spin down
268f0183a33SFelipe Balbi * automatically, requiring a START-STOP UNIT command.
269f0183a33SFelipe Balbi */
270f09e495dSMauro Carvalho Chehab sdev->allow_restart = 1;
271f09e495dSMauro Carvalho Chehab
272f0183a33SFelipe Balbi /*
273f0183a33SFelipe Balbi * Some USB cardreaders have trouble reading an sdcard's last
27423c3e290SHans de Goede * sector in a larger then 1 sector read, since the performance
275f0183a33SFelipe Balbi * impact is negligible we set this flag for all USB disks
276f0183a33SFelipe Balbi */
27723c3e290SHans de Goede sdev->last_sector_bug = 1;
27825ff1c31SAlan Stern
279f0183a33SFelipe Balbi /*
280f0183a33SFelipe Balbi * Enable last-sector hacks for single-target devices using
28125ff1c31SAlan Stern * the Bulk-only transport, unless we already know the
282f0183a33SFelipe Balbi * capacity will be decremented or is correct.
283f0183a33SFelipe Balbi */
28425ff1c31SAlan Stern if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
28525ff1c31SAlan Stern US_FL_SCM_MULT_TARG)) &&
2868fa7fd74SMichal Nazarewicz us->protocol == USB_PR_BULK)
28725ff1c31SAlan Stern us->use_last_sector_hacks = 1;
288eaa05dfcSNamjae Jeon
289eaa05dfcSNamjae Jeon /* Check if write cache default on flag is set or not */
290eaa05dfcSNamjae Jeon if (us->fflags & US_FL_WRITE_CACHE)
291eaa05dfcSNamjae Jeon sdev->wce_default_on = 1;
292eaa05dfcSNamjae Jeon
293b14bf2d0SAlan Stern /* A few buggy USB-ATA bridges don't understand FUA */
294b14bf2d0SAlan Stern if (us->fflags & US_FL_BROKEN_FUA)
295b14bf2d0SAlan Stern sdev->broken_fua = 1;
296b14bf2d0SAlan Stern
297050bc4e8SOliver Neukum /* Some even totally fail to indicate a cache */
298050bc4e8SOliver Neukum if (us->fflags & US_FL_ALWAYS_SYNC) {
299050bc4e8SOliver Neukum /* don't read caching information */
300050bc4e8SOliver Neukum sdev->skip_ms_page_8 = 1;
301050bc4e8SOliver Neukum sdev->skip_ms_page_3f = 1;
302050bc4e8SOliver Neukum /* assume sync is needed */
303050bc4e8SOliver Neukum sdev->wce_default_on = 1;
304050bc4e8SOliver Neukum }
3051da177e4SLinus Torvalds } else {
3061da177e4SLinus Torvalds
307f0183a33SFelipe Balbi /*
30819246d27SGreg Kroah-Hartman * Non-disk-type devices don't need to ignore any pages
3091da177e4SLinus Torvalds * or to force 192-byte transfer lengths for MODE SENSE.
310f0183a33SFelipe Balbi * But they do need to use MODE SENSE(10).
311f0183a33SFelipe Balbi */
3121da177e4SLinus Torvalds sdev->use_10_for_ms = 1;
313ae38c78aSHans de Goede
314ae38c78aSHans de Goede /* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
315ae38c78aSHans de Goede if (us->fflags & US_FL_NO_READ_DISC_INFO)
316ae38c78aSHans de Goede sdev->no_read_disc_info = 1;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds
319f0183a33SFelipe Balbi /*
320f0183a33SFelipe Balbi * The CB and CBI transports have no way to pass LUN values
321f3f49065SAlan Stern * other than the bits in the second byte of a CDB. But those
322f3f49065SAlan Stern * bits don't get set to the LUN value if the device reports
323f3f49065SAlan Stern * scsi_level == 0 (UNKNOWN). Hence such devices must necessarily
324f3f49065SAlan Stern * be single-LUN.
325f3f49065SAlan Stern */
3268fa7fd74SMichal Nazarewicz if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
327f3f49065SAlan Stern sdev->scsi_level == SCSI_UNKNOWN)
328f3f49065SAlan Stern us->max_lun = 0;
329f3f49065SAlan Stern
330f0183a33SFelipe Balbi /*
331f0183a33SFelipe Balbi * Some devices choke when they receive a PREVENT-ALLOW MEDIUM
332f0183a33SFelipe Balbi * REMOVAL command, so suppress those commands.
333f0183a33SFelipe Balbi */
3347e4d6c38SAlan Stern if (us->fflags & US_FL_NOT_LOCKABLE)
3351da177e4SLinus Torvalds sdev->lockable = 0;
3361da177e4SLinus Torvalds
337f0183a33SFelipe Balbi /*
338f0183a33SFelipe Balbi * this is to satisfy the compiler, tho I don't think the
339f0183a33SFelipe Balbi * return code is ever checked anywhere.
340f0183a33SFelipe Balbi */
3411da177e4SLinus Torvalds return 0;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds
target_alloc(struct scsi_target * starget)34409b6b51bSAlan Stern static int target_alloc(struct scsi_target *starget)
34509b6b51bSAlan Stern {
346af74d2daSAlan Stern struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
347af74d2daSAlan Stern
34809b6b51bSAlan Stern /*
34909b6b51bSAlan Stern * Some USB drives don't support REPORT LUNS, even though they
35009b6b51bSAlan Stern * report a SCSI revision level above 2. Tell the SCSI layer
35109b6b51bSAlan Stern * not to issue that command; it will perform a normal sequential
35209b6b51bSAlan Stern * scan instead.
35309b6b51bSAlan Stern */
35409b6b51bSAlan Stern starget->no_report_luns = 1;
355af74d2daSAlan Stern
356af74d2daSAlan Stern /*
357af74d2daSAlan Stern * The UFI spec treats the Peripheral Qualifier bits in an
358af74d2daSAlan Stern * INQUIRY result as reserved and requires devices to set them
359af74d2daSAlan Stern * to 0. However the SCSI spec requires these bits to be set
360af74d2daSAlan Stern * to 3 to indicate when a LUN is not present.
361af74d2daSAlan Stern *
362af74d2daSAlan Stern * Let the scanning code know if this target merely sets
363af74d2daSAlan Stern * Peripheral Device Type to 0x1f to indicate no LUN.
364af74d2daSAlan Stern */
365af74d2daSAlan Stern if (us->subclass == USB_SC_UFI)
366af74d2daSAlan Stern starget->pdt_1f_for_no_lun = 1;
367af74d2daSAlan Stern
36809b6b51bSAlan Stern return 0;
36909b6b51bSAlan Stern }
37009b6b51bSAlan Stern
3711da177e4SLinus Torvalds /* queue a command */
3721da177e4SLinus Torvalds /* This is always called with scsi_lock(host) held */
queuecommand_lck(struct scsi_cmnd * srb)373af049dfdSBart Van Assche static int queuecommand_lck(struct scsi_cmnd *srb)
3741da177e4SLinus Torvalds {
375af049dfdSBart Van Assche void (*done)(struct scsi_cmnd *) = scsi_done;
3761da177e4SLinus Torvalds struct us_data *us = host_to_us(srb->device->host);
3771da177e4SLinus Torvalds
3781da177e4SLinus Torvalds /* check for state-transition errors */
3791da177e4SLinus Torvalds if (us->srb != NULL) {
3803055c92cSMatthias Maennich dev_err(&us->pusb_intf->dev,
3813055c92cSMatthias Maennich "Error in %s: us->srb = %p\n", __func__, us->srb);
3821da177e4SLinus Torvalds return SCSI_MLQUEUE_HOST_BUSY;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds
3851da177e4SLinus Torvalds /* fail the command if we are disconnecting */
3867e4d6c38SAlan Stern if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
387191648d0SJoe Perches usb_stor_dbg(us, "Fail command during disconnect\n");
3881da177e4SLinus Torvalds srb->result = DID_NO_CONNECT << 16;
3891da177e4SLinus Torvalds done(srb);
3901da177e4SLinus Torvalds return 0;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds
393f45681f9STim Anderson if ((us->fflags & US_FL_NO_ATA_1X) &&
394f45681f9STim Anderson (srb->cmnd[0] == ATA_12 || srb->cmnd[0] == ATA_16)) {
395f45681f9STim Anderson memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
396f45681f9STim Anderson sizeof(usb_stor_sense_invalidCDB));
397f45681f9STim Anderson srb->result = SAM_STAT_CHECK_CONDITION;
398f45681f9STim Anderson done(srb);
399f45681f9STim Anderson return 0;
400f45681f9STim Anderson }
401f45681f9STim Anderson
4021da177e4SLinus Torvalds /* enqueue the command and wake up the control thread */
4031da177e4SLinus Torvalds us->srb = srb;
4047119e3c3SAlan Stern complete(&us->cmnd_ready);
4051da177e4SLinus Torvalds
4061da177e4SLinus Torvalds return 0;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds
DEF_SCSI_QCMD(queuecommand)409f281233dSJeff Garzik static DEF_SCSI_QCMD(queuecommand)
410f281233dSJeff Garzik
4111da177e4SLinus Torvalds /***********************************************************************
4121da177e4SLinus Torvalds * Error handling functions
4131da177e4SLinus Torvalds ***********************************************************************/
4141da177e4SLinus Torvalds
4151da177e4SLinus Torvalds /* Command timeout and abort */
416a398d5eaSMaxime Bizon static int command_abort_matching(struct us_data *us, struct scsi_cmnd *srb_match)
4171da177e4SLinus Torvalds {
418f0183a33SFelipe Balbi /*
419f0183a33SFelipe Balbi * us->srb together with the TIMED_OUT, RESETTING, and ABORTING
420f0183a33SFelipe Balbi * bits are protected by the host lock.
421f0183a33SFelipe Balbi */
422226173edSMatthew Dharm scsi_lock(us_to_host(us));
423226173edSMatthew Dharm
424a398d5eaSMaxime Bizon /* is there any active pending command to abort ? */
425a398d5eaSMaxime Bizon if (!us->srb) {
426226173edSMatthew Dharm scsi_unlock(us_to_host(us));
427191648d0SJoe Perches usb_stor_dbg(us, "-- nothing to abort\n");
428a398d5eaSMaxime Bizon return SUCCESS;
429a398d5eaSMaxime Bizon }
430a398d5eaSMaxime Bizon
431a398d5eaSMaxime Bizon /* Does the command match the passed srb if any ? */
432a398d5eaSMaxime Bizon if (srb_match && us->srb != srb_match) {
433a398d5eaSMaxime Bizon scsi_unlock(us_to_host(us));
434a398d5eaSMaxime Bizon usb_stor_dbg(us, "-- pending command mismatch\n");
4351da177e4SLinus Torvalds return FAILED;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds
438f0183a33SFelipe Balbi /*
439f0183a33SFelipe Balbi * Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
4401da177e4SLinus Torvalds * a device reset isn't already in progress (to avoid interfering
441226173edSMatthew Dharm * with the reset). Note that we must retain the host lock while
442226173edSMatthew Dharm * calling usb_stor_stop_transport(); otherwise it might interfere
443f0183a33SFelipe Balbi * with an auto-reset that begins as soon as we release the lock.
444f0183a33SFelipe Balbi */
4457e4d6c38SAlan Stern set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
4467e4d6c38SAlan Stern if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
4477e4d6c38SAlan Stern set_bit(US_FLIDX_ABORTING, &us->dflags);
4481da177e4SLinus Torvalds usb_stor_stop_transport(us);
4491da177e4SLinus Torvalds }
450226173edSMatthew Dharm scsi_unlock(us_to_host(us));
4511da177e4SLinus Torvalds
4521da177e4SLinus Torvalds /* Wait for the aborted command to finish */
4531da177e4SLinus Torvalds wait_for_completion(&us->notify);
4541da177e4SLinus Torvalds return SUCCESS;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds
command_abort(struct scsi_cmnd * srb)457a398d5eaSMaxime Bizon static int command_abort(struct scsi_cmnd *srb)
458a398d5eaSMaxime Bizon {
459a398d5eaSMaxime Bizon struct us_data *us = host_to_us(srb->device->host);
460a398d5eaSMaxime Bizon
461a398d5eaSMaxime Bizon usb_stor_dbg(us, "%s called\n", __func__);
462a398d5eaSMaxime Bizon return command_abort_matching(us, srb);
463a398d5eaSMaxime Bizon }
464a398d5eaSMaxime Bizon
465f0183a33SFelipe Balbi /*
466f0183a33SFelipe Balbi * This invokes the transport reset mechanism to reset the state of the
467f0183a33SFelipe Balbi * device
468f0183a33SFelipe Balbi */
device_reset(struct scsi_cmnd * srb)4691da177e4SLinus Torvalds static int device_reset(struct scsi_cmnd *srb)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds struct us_data *us = host_to_us(srb->device->host);
4721da177e4SLinus Torvalds int result;
4731da177e4SLinus Torvalds
474191648d0SJoe Perches usb_stor_dbg(us, "%s called\n", __func__);
4751da177e4SLinus Torvalds
476a398d5eaSMaxime Bizon /* abort any pending command before reset */
477a398d5eaSMaxime Bizon command_abort_matching(us, NULL);
478a398d5eaSMaxime Bizon
4791da177e4SLinus Torvalds /* lock the device pointers and do the reset */
4804186ecf8SArjan van de Ven mutex_lock(&(us->dev_mutex));
4811da177e4SLinus Torvalds result = us->transport_reset(us);
4824186ecf8SArjan van de Ven mutex_unlock(&us->dev_mutex);
4831da177e4SLinus Torvalds
4844d07ef76SMatthew Dharm return result < 0 ? FAILED : SUCCESS;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds
4874d07ef76SMatthew Dharm /* Simulate a SCSI bus reset by resetting the device's USB port. */
bus_reset(struct scsi_cmnd * srb)4881da177e4SLinus Torvalds static int bus_reset(struct scsi_cmnd *srb)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds struct us_data *us = host_to_us(srb->device->host);
4914d07ef76SMatthew Dharm int result;
4921da177e4SLinus Torvalds
493191648d0SJoe Perches usb_stor_dbg(us, "%s called\n", __func__);
494191648d0SJoe Perches
4954d07ef76SMatthew Dharm result = usb_stor_port_reset(us);
4961da177e4SLinus Torvalds return result < 0 ? FAILED : SUCCESS;
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds
499f0183a33SFelipe Balbi /*
500f0183a33SFelipe Balbi * Report a driver-initiated device reset to the SCSI layer.
5011da177e4SLinus Torvalds * Calling this for a SCSI-initiated reset is unnecessary but harmless.
502f0183a33SFelipe Balbi * The caller must own the SCSI host lock.
503f0183a33SFelipe Balbi */
usb_stor_report_device_reset(struct us_data * us)5041da177e4SLinus Torvalds void usb_stor_report_device_reset(struct us_data *us)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds int i;
5071da177e4SLinus Torvalds struct Scsi_Host *host = us_to_host(us);
5081da177e4SLinus Torvalds
5091da177e4SLinus Torvalds scsi_report_device_reset(host, 0, 0);
5107e4d6c38SAlan Stern if (us->fflags & US_FL_SCM_MULT_TARG) {
5111da177e4SLinus Torvalds for (i = 1; i < host->max_id; ++i)
5121da177e4SLinus Torvalds scsi_report_device_reset(host, 0, i);
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds
516f0183a33SFelipe Balbi /*
517f0183a33SFelipe Balbi * Report a driver-initiated bus reset to the SCSI layer.
5184d07ef76SMatthew Dharm * Calling this for a SCSI-initiated reset is unnecessary but harmless.
519f0183a33SFelipe Balbi * The caller must not own the SCSI host lock.
520f0183a33SFelipe Balbi */
usb_stor_report_bus_reset(struct us_data * us)5214d07ef76SMatthew Dharm void usb_stor_report_bus_reset(struct us_data *us)
5224d07ef76SMatthew Dharm {
523f07600cfSAlan Stern struct Scsi_Host *host = us_to_host(us);
524f07600cfSAlan Stern
525f07600cfSAlan Stern scsi_lock(host);
526f07600cfSAlan Stern scsi_report_bus_reset(host, 0);
527f07600cfSAlan Stern scsi_unlock(host);
5284d07ef76SMatthew Dharm }
5294d07ef76SMatthew Dharm
5301da177e4SLinus Torvalds /***********************************************************************
5311da177e4SLinus Torvalds * /proc/scsi/ functions
5321da177e4SLinus Torvalds ***********************************************************************/
5331da177e4SLinus Torvalds
write_info(struct Scsi_Host * host,char * buffer,int length)53409dae7fcSAl Viro static int write_info(struct Scsi_Host *host, char *buffer, int length)
53509dae7fcSAl Viro {
53609dae7fcSAl Viro /* if someone is sending us data, just throw it away */
53709dae7fcSAl Viro return length;
53809dae7fcSAl Viro }
53909dae7fcSAl Viro
show_info(struct seq_file * m,struct Scsi_Host * host)54009dae7fcSAl Viro static int show_info (struct seq_file *m, struct Scsi_Host *host)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds struct us_data *us = host_to_us(host);
5431da177e4SLinus Torvalds const char *string;
5441da177e4SLinus Torvalds
5451da177e4SLinus Torvalds /* print the controller name */
5467d203a9eSJoe Perches seq_printf(m, " Host scsi%d: usb-storage\n", host->host_no);
5471da177e4SLinus Torvalds
5481da177e4SLinus Torvalds /* print product, vendor, and serial number strings */
5491da177e4SLinus Torvalds if (us->pusb_dev->manufacturer)
5501da177e4SLinus Torvalds string = us->pusb_dev->manufacturer;
5511da177e4SLinus Torvalds else if (us->unusual_dev->vendorName)
5521da177e4SLinus Torvalds string = us->unusual_dev->vendorName;
5531da177e4SLinus Torvalds else
5541da177e4SLinus Torvalds string = "Unknown";
5557d203a9eSJoe Perches seq_printf(m, " Vendor: %s\n", string);
5561da177e4SLinus Torvalds if (us->pusb_dev->product)
5571da177e4SLinus Torvalds string = us->pusb_dev->product;
5581da177e4SLinus Torvalds else if (us->unusual_dev->productName)
5591da177e4SLinus Torvalds string = us->unusual_dev->productName;
5601da177e4SLinus Torvalds else
5611da177e4SLinus Torvalds string = "Unknown";
5627d203a9eSJoe Perches seq_printf(m, " Product: %s\n", string);
5631da177e4SLinus Torvalds if (us->pusb_dev->serial)
5641da177e4SLinus Torvalds string = us->pusb_dev->serial;
5651da177e4SLinus Torvalds else
5661da177e4SLinus Torvalds string = "None";
5677d203a9eSJoe Perches seq_printf(m, "Serial Number: %s\n", string);
5681da177e4SLinus Torvalds
5691da177e4SLinus Torvalds /* show the protocol and transport */
5707d203a9eSJoe Perches seq_printf(m, " Protocol: %s\n", us->protocol_name);
5717d203a9eSJoe Perches seq_printf(m, " Transport: %s\n", us->transport_name);
5721da177e4SLinus Torvalds
5731da177e4SLinus Torvalds /* show the device flags */
5747d203a9eSJoe Perches seq_printf(m, " Quirks:");
5751da177e4SLinus Torvalds
5761da177e4SLinus Torvalds #define US_FLAG(name, value) \
57709dae7fcSAl Viro if (us->fflags & value) seq_printf(m, " " #name);
5781da177e4SLinus Torvalds US_DO_ALL_FLAGS
5791da177e4SLinus Torvalds #undef US_FLAG
58009dae7fcSAl Viro seq_putc(m, '\n');
58109dae7fcSAl Viro return 0;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds
5841da177e4SLinus Torvalds /***********************************************************************
5851da177e4SLinus Torvalds * Sysfs interface
5861da177e4SLinus Torvalds ***********************************************************************/
5871da177e4SLinus Torvalds
5881da177e4SLinus Torvalds /* Output routine for the sysfs max_sectors file */
max_sectors_show(struct device * dev,struct device_attribute * attr,char * buf)589f2e0ae93SGreg Kroah-Hartman static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev);
5921da177e4SLinus Torvalds
59349d6271bSAlan Stern return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds
5961da177e4SLinus Torvalds /* Input routine for the sysfs max_sectors file */
max_sectors_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)597f2e0ae93SGreg Kroah-Hartman static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
5981da177e4SLinus Torvalds size_t count)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev);
6011da177e4SLinus Torvalds unsigned short ms;
6021da177e4SLinus Torvalds
60349d6271bSAlan Stern if (sscanf(buf, "%hu", &ms) > 0) {
604086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(sdev->request_queue, ms);
60549d6271bSAlan Stern return count;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds return -EINVAL;
6081da177e4SLinus Torvalds }
609f2e0ae93SGreg Kroah-Hartman static DEVICE_ATTR_RW(max_sectors);
6101da177e4SLinus Torvalds
61101e570feSBart Van Assche static struct attribute *usb_sdev_attrs[] = {
61201e570feSBart Van Assche &dev_attr_max_sectors.attr,
6131da177e4SLinus Torvalds NULL,
6141da177e4SLinus Torvalds };
6151da177e4SLinus Torvalds
61601e570feSBart Van Assche ATTRIBUTE_GROUPS(usb_sdev);
61701e570feSBart Van Assche
6181da177e4SLinus Torvalds /*
6191da177e4SLinus Torvalds * this defines our host template, with which we'll allocate hosts
6201da177e4SLinus Torvalds */
6211da177e4SLinus Torvalds
622aa519be3SAkinobu Mita static const struct scsi_host_template usb_stor_host_template = {
6231da177e4SLinus Torvalds /* basic userland interface stuff */
6241da177e4SLinus Torvalds .name = "usb-storage",
6251da177e4SLinus Torvalds .proc_name = "usb-storage",
62609dae7fcSAl Viro .show_info = show_info,
62709dae7fcSAl Viro .write_info = write_info,
6281da177e4SLinus Torvalds .info = host_info,
6291da177e4SLinus Torvalds
6301da177e4SLinus Torvalds /* command interface -- queued only */
6311da177e4SLinus Torvalds .queuecommand = queuecommand,
6321da177e4SLinus Torvalds
6331da177e4SLinus Torvalds /* error and abort handlers */
6341da177e4SLinus Torvalds .eh_abort_handler = command_abort,
6351da177e4SLinus Torvalds .eh_device_reset_handler = device_reset,
6361da177e4SLinus Torvalds .eh_bus_reset_handler = bus_reset,
6371da177e4SLinus Torvalds
6381da177e4SLinus Torvalds /* queue commands only, only one command per LUN */
6391da177e4SLinus Torvalds .can_queue = 1,
6401da177e4SLinus Torvalds
6411da177e4SLinus Torvalds /* unknown initiator id */
6421da177e4SLinus Torvalds .this_id = -1,
6431da177e4SLinus Torvalds
6441da177e4SLinus Torvalds .slave_alloc = slave_alloc,
6451da177e4SLinus Torvalds .slave_configure = slave_configure,
64609b6b51bSAlan Stern .target_alloc = target_alloc,
6471da177e4SLinus Torvalds
6481da177e4SLinus Torvalds /* lots of sg segments can be handled */
64965e8617fSMing Lin .sg_tablesize = SG_MAX_SEGMENTS,
6501da177e4SLinus Torvalds
651779b457fSFelipe Balbi
652779b457fSFelipe Balbi /*
653779b457fSFelipe Balbi * Limit the total size of a transfer to 120 KB.
654779b457fSFelipe Balbi *
655779b457fSFelipe Balbi * Some devices are known to choke with anything larger. It seems like
656779b457fSFelipe Balbi * the problem stems from the fact that original IDE controllers had
657779b457fSFelipe Balbi * only an 8-bit register to hold the number of sectors in one transfer
658779b457fSFelipe Balbi * and even those couldn't handle a full 256 sectors.
659779b457fSFelipe Balbi *
660779b457fSFelipe Balbi * Because we want to make sure we interoperate with as many devices as
661779b457fSFelipe Balbi * possible, we will maintain a 240 sector transfer size limit for USB
662779b457fSFelipe Balbi * Mass Storage devices.
663779b457fSFelipe Balbi *
664779b457fSFelipe Balbi * Tests show that other operating have similar limits with Microsoft
665779b457fSFelipe Balbi * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
666779b457fSFelipe Balbi * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
667779b457fSFelipe Balbi * and 2048 for USB3 devices.
668779b457fSFelipe Balbi */
6691da177e4SLinus Torvalds .max_sectors = 240,
6701da177e4SLinus Torvalds
6711da177e4SLinus Torvalds /* emulated HBA */
6721da177e4SLinus Torvalds .emulated = 1,
6731da177e4SLinus Torvalds
6741da177e4SLinus Torvalds /* we do our own delay after a device or bus reset */
6751da177e4SLinus Torvalds .skip_settle_delay = 1,
6761da177e4SLinus Torvalds
6771da177e4SLinus Torvalds /* sysfs device attributes */
67801e570feSBart Van Assche .sdev_groups = usb_sdev_groups,
6791da177e4SLinus Torvalds
6801da177e4SLinus Torvalds /* module management */
6811da177e4SLinus Torvalds .module = THIS_MODULE
6821da177e4SLinus Torvalds };
6831da177e4SLinus Torvalds
usb_stor_host_template_init(struct scsi_host_template * sht,const char * name,struct module * owner)684aa519be3SAkinobu Mita void usb_stor_host_template_init(struct scsi_host_template *sht,
685aa519be3SAkinobu Mita const char *name, struct module *owner)
686aa519be3SAkinobu Mita {
687aa519be3SAkinobu Mita *sht = usb_stor_host_template;
688aa519be3SAkinobu Mita sht->name = name;
689aa519be3SAkinobu Mita sht->proc_name = name;
690aa519be3SAkinobu Mita sht->module = owner;
691aa519be3SAkinobu Mita }
692aa519be3SAkinobu Mita EXPORT_SYMBOL_GPL(usb_stor_host_template_init);
693aa519be3SAkinobu Mita
6941da177e4SLinus Torvalds /* To Report "Illegal Request: Invalid Field in CDB */
6951da177e4SLinus Torvalds unsigned char usb_stor_sense_invalidCDB[18] = {
6961da177e4SLinus Torvalds [0] = 0x70, /* current error */
6971da177e4SLinus Torvalds [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
6981da177e4SLinus Torvalds [7] = 0x0a, /* additional length */
6991da177e4SLinus Torvalds [12] = 0x24 /* Invalid Field in CDB */
7001da177e4SLinus Torvalds };
701e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);
702