18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for USB Mass Storage compliant devices
48c2ecf20Sopenharmony_ci * SCSI layer glue code
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Current development and maintenance by:
78c2ecf20Sopenharmony_ci *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Developed with the assistance of:
108c2ecf20Sopenharmony_ci *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
118c2ecf20Sopenharmony_ci *   (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Initial work by:
148c2ecf20Sopenharmony_ci *   (c) 1999 Michael Gee (michael@linuxspecific.com)
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * This driver is based on the 'USB Mass Storage Class' document. This
178c2ecf20Sopenharmony_ci * describes in detail the protocol used to communicate with such
188c2ecf20Sopenharmony_ci * devices.  Clearly, the designers had SCSI and ATAPI commands in
198c2ecf20Sopenharmony_ci * mind when they created this document.  The commands are all very
208c2ecf20Sopenharmony_ci * similar to commands in the SCSI-II and ATAPI specifications.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * It is important to note that in a number of cases this class
238c2ecf20Sopenharmony_ci * exhibits class-specific exemptions from the USB specification.
248c2ecf20Sopenharmony_ci * Notably the usage of NAK, STALL and ACK differs from the norm, in
258c2ecf20Sopenharmony_ci * that they are used to communicate wait, failed and OK on commands.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Also, for certain devices, the interrupt endpoint is used to convey
288c2ecf20Sopenharmony_ci * status of a command.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
328c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
338c2ecf20Sopenharmony_ci#include <linux/module.h>
348c2ecf20Sopenharmony_ci#include <linux/mutex.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
378c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
388c2ecf20Sopenharmony_ci#include <scsi/scsi_devinfo.h>
398c2ecf20Sopenharmony_ci#include <scsi/scsi_device.h>
408c2ecf20Sopenharmony_ci#include <scsi/scsi_eh.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#include "usb.h"
438c2ecf20Sopenharmony_ci#include <linux/usb/hcd.h>
448c2ecf20Sopenharmony_ci#include "scsiglue.h"
458c2ecf20Sopenharmony_ci#include "debug.h"
468c2ecf20Sopenharmony_ci#include "transport.h"
478c2ecf20Sopenharmony_ci#include "protocol.h"
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Vendor IDs for companies that seem to include the READ CAPACITY bug
518c2ecf20Sopenharmony_ci * in all their devices
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ci#define VENDOR_ID_NOKIA		0x0421
548c2ecf20Sopenharmony_ci#define VENDOR_ID_NIKON		0x04b0
558c2ecf20Sopenharmony_ci#define VENDOR_ID_PENTAX	0x0a17
568c2ecf20Sopenharmony_ci#define VENDOR_ID_MOTOROLA	0x22b8
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/***********************************************************************
598c2ecf20Sopenharmony_ci * Host functions
608c2ecf20Sopenharmony_ci ***********************************************************************/
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic const char* host_info(struct Scsi_Host *host)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(host);
658c2ecf20Sopenharmony_ci	return us->scsi_name;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int slave_alloc (struct scsi_device *sdev)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(sdev->host);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/*
738c2ecf20Sopenharmony_ci	 * Set the INQUIRY transfer length to 36.  We don't use any of
748c2ecf20Sopenharmony_ci	 * the extra data and many devices choke if asked for more or
758c2ecf20Sopenharmony_ci	 * less than 36 bytes.
768c2ecf20Sopenharmony_ci	 */
778c2ecf20Sopenharmony_ci	sdev->inquiry_len = 36;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/*
808c2ecf20Sopenharmony_ci	 * Some host controllers may have alignment requirements.
818c2ecf20Sopenharmony_ci	 * We'll play it safe by requiring 512-byte alignment always.
828c2ecf20Sopenharmony_ci	 */
838c2ecf20Sopenharmony_ci	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* Tell the SCSI layer if we know there is more than one LUN */
868c2ecf20Sopenharmony_ci	if (us->protocol == USB_PR_BULK && us->max_lun > 0)
878c2ecf20Sopenharmony_ci		sdev->sdev_bflags |= BLIST_FORCELUN;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int slave_configure(struct scsi_device *sdev)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(sdev->host);
958c2ecf20Sopenharmony_ci	struct device *dev = us->pusb_dev->bus->sysdev;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/*
988c2ecf20Sopenharmony_ci	 * Many devices have trouble transferring more than 32KB at a time,
998c2ecf20Sopenharmony_ci	 * while others have trouble with more than 64K. At this time we
1008c2ecf20Sopenharmony_ci	 * are limiting both to 32K (64 sectores).
1018c2ecf20Sopenharmony_ci	 */
1028c2ecf20Sopenharmony_ci	if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
1038c2ecf20Sopenharmony_ci		unsigned int max_sectors = 64;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_MAX_SECTORS_MIN)
1068c2ecf20Sopenharmony_ci			max_sectors = PAGE_SIZE >> 9;
1078c2ecf20Sopenharmony_ci		if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
1088c2ecf20Sopenharmony_ci			blk_queue_max_hw_sectors(sdev->request_queue,
1098c2ecf20Sopenharmony_ci					      max_sectors);
1108c2ecf20Sopenharmony_ci	} else if (sdev->type == TYPE_TAPE) {
1118c2ecf20Sopenharmony_ci		/*
1128c2ecf20Sopenharmony_ci		 * Tapes need much higher max_sector limits, so just
1138c2ecf20Sopenharmony_ci		 * raise it to the maximum possible (4 GB / 512) and
1148c2ecf20Sopenharmony_ci		 * let the queue segment size sort out the real limit.
1158c2ecf20Sopenharmony_ci		 */
1168c2ecf20Sopenharmony_ci		blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
1178c2ecf20Sopenharmony_ci	} else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
1188c2ecf20Sopenharmony_ci		/*
1198c2ecf20Sopenharmony_ci		 * USB3 devices will be limited to 2048 sectors. This gives us
1208c2ecf20Sopenharmony_ci		 * better throughput on most devices.
1218c2ecf20Sopenharmony_ci		 */
1228c2ecf20Sopenharmony_ci		blk_queue_max_hw_sectors(sdev->request_queue, 2048);
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/*
1268c2ecf20Sopenharmony_ci	 * The max_hw_sectors should be up to maximum size of a mapping for
1278c2ecf20Sopenharmony_ci	 * the device. Otherwise, a DMA API might fail on swiotlb environment.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	blk_queue_max_hw_sectors(sdev->request_queue,
1308c2ecf20Sopenharmony_ci		min_t(size_t, queue_max_hw_sectors(sdev->request_queue),
1318c2ecf20Sopenharmony_ci		      dma_max_mapping_size(dev) >> SECTOR_SHIFT));
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/*
1348c2ecf20Sopenharmony_ci	 * Some USB host controllers can't do DMA; they have to use PIO.
1358c2ecf20Sopenharmony_ci	 * For such controllers we need to make sure the block layer sets
1368c2ecf20Sopenharmony_ci	 * up bounce buffers in addressable memory.
1378c2ecf20Sopenharmony_ci	 */
1388c2ecf20Sopenharmony_ci	if (!hcd_uses_dma(bus_to_hcd(us->pusb_dev->bus)) ||
1398c2ecf20Sopenharmony_ci			(bus_to_hcd(us->pusb_dev->bus)->localmem_pool != NULL))
1408c2ecf20Sopenharmony_ci		blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/*
1438c2ecf20Sopenharmony_ci	 * We can't put these settings in slave_alloc() because that gets
1448c2ecf20Sopenharmony_ci	 * called before the device type is known.  Consequently these
1458c2ecf20Sopenharmony_ci	 * settings can't be overridden via the scsi devinfo mechanism.
1468c2ecf20Sopenharmony_ci	 */
1478c2ecf20Sopenharmony_ci	if (sdev->type == TYPE_DISK) {
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		/*
1508c2ecf20Sopenharmony_ci		 * Some vendors seem to put the READ CAPACITY bug into
1518c2ecf20Sopenharmony_ci		 * all their devices -- primarily makers of cell phones
1528c2ecf20Sopenharmony_ci		 * and digital cameras.  Since these devices always use
1538c2ecf20Sopenharmony_ci		 * flash media and can be expected to have an even number
1548c2ecf20Sopenharmony_ci		 * of sectors, we will always enable the CAPACITY_HEURISTICS
1558c2ecf20Sopenharmony_ci		 * flag unless told otherwise.
1568c2ecf20Sopenharmony_ci		 */
1578c2ecf20Sopenharmony_ci		switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
1588c2ecf20Sopenharmony_ci		case VENDOR_ID_NOKIA:
1598c2ecf20Sopenharmony_ci		case VENDOR_ID_NIKON:
1608c2ecf20Sopenharmony_ci		case VENDOR_ID_PENTAX:
1618c2ecf20Sopenharmony_ci		case VENDOR_ID_MOTOROLA:
1628c2ecf20Sopenharmony_ci			if (!(us->fflags & (US_FL_FIX_CAPACITY |
1638c2ecf20Sopenharmony_ci					US_FL_CAPACITY_OK)))
1648c2ecf20Sopenharmony_ci				us->fflags |= US_FL_CAPACITY_HEURISTICS;
1658c2ecf20Sopenharmony_ci			break;
1668c2ecf20Sopenharmony_ci		}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		/*
1698c2ecf20Sopenharmony_ci		 * Disk-type devices use MODE SENSE(6) if the protocol
1708c2ecf20Sopenharmony_ci		 * (SubClass) is Transparent SCSI, otherwise they use
1718c2ecf20Sopenharmony_ci		 * MODE SENSE(10).
1728c2ecf20Sopenharmony_ci		 */
1738c2ecf20Sopenharmony_ci		if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
1748c2ecf20Sopenharmony_ci			sdev->use_10_for_ms = 1;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci		/*
1778c2ecf20Sopenharmony_ci		 *Many disks only accept MODE SENSE transfer lengths of
1788c2ecf20Sopenharmony_ci		 * 192 bytes (that's what Windows uses).
1798c2ecf20Sopenharmony_ci		 */
1808c2ecf20Sopenharmony_ci		sdev->use_192_bytes_for_3f = 1;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci		/*
1838c2ecf20Sopenharmony_ci		 * Some devices don't like MODE SENSE with page=0x3f,
1848c2ecf20Sopenharmony_ci		 * which is the command used for checking if a device
1858c2ecf20Sopenharmony_ci		 * is write-protected.  Now that we tell the sd driver
1868c2ecf20Sopenharmony_ci		 * to do a 192-byte transfer with this command the
1878c2ecf20Sopenharmony_ci		 * majority of devices work fine, but a few still can't
1888c2ecf20Sopenharmony_ci		 * handle it.  The sd driver will simply assume those
1898c2ecf20Sopenharmony_ci		 * devices are write-enabled.
1908c2ecf20Sopenharmony_ci		 */
1918c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_NO_WP_DETECT)
1928c2ecf20Sopenharmony_ci			sdev->skip_ms_page_3f = 1;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		/*
1958c2ecf20Sopenharmony_ci		 * A number of devices have problems with MODE SENSE for
1968c2ecf20Sopenharmony_ci		 * page x08, so we will skip it.
1978c2ecf20Sopenharmony_ci		 */
1988c2ecf20Sopenharmony_ci		sdev->skip_ms_page_8 = 1;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci		/*
2018c2ecf20Sopenharmony_ci		 * Some devices don't handle VPD pages correctly, so skip vpd
2028c2ecf20Sopenharmony_ci		 * pages if not forced by SCSI layer.
2038c2ecf20Sopenharmony_ci		 */
2048c2ecf20Sopenharmony_ci		sdev->skip_vpd_pages = !sdev->try_vpd_pages;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		/* Do not attempt to use REPORT SUPPORTED OPERATION CODES */
2078c2ecf20Sopenharmony_ci		sdev->no_report_opcodes = 1;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		/* Do not attempt to use WRITE SAME */
2108c2ecf20Sopenharmony_ci		sdev->no_write_same = 1;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		/*
2138c2ecf20Sopenharmony_ci		 * Some disks return the total number of blocks in response
2148c2ecf20Sopenharmony_ci		 * to READ CAPACITY rather than the highest block number.
2158c2ecf20Sopenharmony_ci		 * If this device makes that mistake, tell the sd driver.
2168c2ecf20Sopenharmony_ci		 */
2178c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_FIX_CAPACITY)
2188c2ecf20Sopenharmony_ci			sdev->fix_capacity = 1;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci		/*
2218c2ecf20Sopenharmony_ci		 * A few disks have two indistinguishable version, one of
2228c2ecf20Sopenharmony_ci		 * which reports the correct capacity and the other does not.
2238c2ecf20Sopenharmony_ci		 * The sd driver has to guess which is the case.
2248c2ecf20Sopenharmony_ci		 */
2258c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_CAPACITY_HEURISTICS)
2268c2ecf20Sopenharmony_ci			sdev->guess_capacity = 1;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		/* Some devices cannot handle READ_CAPACITY_16 */
2298c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_NO_READ_CAPACITY_16)
2308c2ecf20Sopenharmony_ci			sdev->no_read_capacity_16 = 1;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		/*
2338c2ecf20Sopenharmony_ci		 * Many devices do not respond properly to READ_CAPACITY_16.
2348c2ecf20Sopenharmony_ci		 * Tell the SCSI layer to try READ_CAPACITY_10 first.
2358c2ecf20Sopenharmony_ci		 * However some USB 3.0 drive enclosures return capacity
2368c2ecf20Sopenharmony_ci		 * modulo 2TB. Those must use READ_CAPACITY_16
2378c2ecf20Sopenharmony_ci		 */
2388c2ecf20Sopenharmony_ci		if (!(us->fflags & US_FL_NEEDS_CAP16))
2398c2ecf20Sopenharmony_ci			sdev->try_rc_10_first = 1;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		/*
2428c2ecf20Sopenharmony_ci		 * assume SPC3 or latter devices support sense size > 18
2438c2ecf20Sopenharmony_ci		 * unless US_FL_BAD_SENSE quirk is specified.
2448c2ecf20Sopenharmony_ci		 */
2458c2ecf20Sopenharmony_ci		if (sdev->scsi_level > SCSI_SPC_2 &&
2468c2ecf20Sopenharmony_ci		    !(us->fflags & US_FL_BAD_SENSE))
2478c2ecf20Sopenharmony_ci			us->fflags |= US_FL_SANE_SENSE;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		/*
2508c2ecf20Sopenharmony_ci		 * USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
2518c2ecf20Sopenharmony_ci		 * Hardware Error) when any low-level error occurs,
2528c2ecf20Sopenharmony_ci		 * recoverable or not.  Setting this flag tells the SCSI
2538c2ecf20Sopenharmony_ci		 * midlayer to retry such commands, which frequently will
2548c2ecf20Sopenharmony_ci		 * succeed and fix the error.  The worst this can lead to
2558c2ecf20Sopenharmony_ci		 * is an occasional series of retries that will all fail.
2568c2ecf20Sopenharmony_ci		 */
2578c2ecf20Sopenharmony_ci		sdev->retry_hwerror = 1;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		/*
2608c2ecf20Sopenharmony_ci		 * USB disks should allow restart.  Some drives spin down
2618c2ecf20Sopenharmony_ci		 * automatically, requiring a START-STOP UNIT command.
2628c2ecf20Sopenharmony_ci		 */
2638c2ecf20Sopenharmony_ci		sdev->allow_restart = 1;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci		/*
2668c2ecf20Sopenharmony_ci		 * Some USB cardreaders have trouble reading an sdcard's last
2678c2ecf20Sopenharmony_ci		 * sector in a larger then 1 sector read, since the performance
2688c2ecf20Sopenharmony_ci		 * impact is negligible we set this flag for all USB disks
2698c2ecf20Sopenharmony_ci		 */
2708c2ecf20Sopenharmony_ci		sdev->last_sector_bug = 1;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		/*
2738c2ecf20Sopenharmony_ci		 * Enable last-sector hacks for single-target devices using
2748c2ecf20Sopenharmony_ci		 * the Bulk-only transport, unless we already know the
2758c2ecf20Sopenharmony_ci		 * capacity will be decremented or is correct.
2768c2ecf20Sopenharmony_ci		 */
2778c2ecf20Sopenharmony_ci		if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
2788c2ecf20Sopenharmony_ci					US_FL_SCM_MULT_TARG)) &&
2798c2ecf20Sopenharmony_ci				us->protocol == USB_PR_BULK)
2808c2ecf20Sopenharmony_ci			us->use_last_sector_hacks = 1;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		/* Check if write cache default on flag is set or not */
2838c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_WRITE_CACHE)
2848c2ecf20Sopenharmony_ci			sdev->wce_default_on = 1;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		/* A few buggy USB-ATA bridges don't understand FUA */
2878c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_BROKEN_FUA)
2888c2ecf20Sopenharmony_ci			sdev->broken_fua = 1;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		/* Some even totally fail to indicate a cache */
2918c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_ALWAYS_SYNC) {
2928c2ecf20Sopenharmony_ci			/* don't read caching information */
2938c2ecf20Sopenharmony_ci			sdev->skip_ms_page_8 = 1;
2948c2ecf20Sopenharmony_ci			sdev->skip_ms_page_3f = 1;
2958c2ecf20Sopenharmony_ci			/* assume sync is needed */
2968c2ecf20Sopenharmony_ci			sdev->wce_default_on = 1;
2978c2ecf20Sopenharmony_ci		}
2988c2ecf20Sopenharmony_ci	} else {
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		/*
3018c2ecf20Sopenharmony_ci		 * Non-disk-type devices don't need to ignore any pages
3028c2ecf20Sopenharmony_ci		 * or to force 192-byte transfer lengths for MODE SENSE.
3038c2ecf20Sopenharmony_ci		 * But they do need to use MODE SENSE(10).
3048c2ecf20Sopenharmony_ci		 */
3058c2ecf20Sopenharmony_ci		sdev->use_10_for_ms = 1;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		/* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
3088c2ecf20Sopenharmony_ci		if (us->fflags & US_FL_NO_READ_DISC_INFO)
3098c2ecf20Sopenharmony_ci			sdev->no_read_disc_info = 1;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/*
3138c2ecf20Sopenharmony_ci	 * The CB and CBI transports have no way to pass LUN values
3148c2ecf20Sopenharmony_ci	 * other than the bits in the second byte of a CDB.  But those
3158c2ecf20Sopenharmony_ci	 * bits don't get set to the LUN value if the device reports
3168c2ecf20Sopenharmony_ci	 * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily
3178c2ecf20Sopenharmony_ci	 * be single-LUN.
3188c2ecf20Sopenharmony_ci	 */
3198c2ecf20Sopenharmony_ci	if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
3208c2ecf20Sopenharmony_ci			sdev->scsi_level == SCSI_UNKNOWN)
3218c2ecf20Sopenharmony_ci		us->max_lun = 0;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	/*
3248c2ecf20Sopenharmony_ci	 * Some devices choke when they receive a PREVENT-ALLOW MEDIUM
3258c2ecf20Sopenharmony_ci	 * REMOVAL command, so suppress those commands.
3268c2ecf20Sopenharmony_ci	 */
3278c2ecf20Sopenharmony_ci	if (us->fflags & US_FL_NOT_LOCKABLE)
3288c2ecf20Sopenharmony_ci		sdev->lockable = 0;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/*
3318c2ecf20Sopenharmony_ci	 * this is to satisfy the compiler, tho I don't think the
3328c2ecf20Sopenharmony_ci	 * return code is ever checked anywhere.
3338c2ecf20Sopenharmony_ci	 */
3348c2ecf20Sopenharmony_ci	return 0;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int target_alloc(struct scsi_target *starget)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/*
3428c2ecf20Sopenharmony_ci	 * Some USB drives don't support REPORT LUNS, even though they
3438c2ecf20Sopenharmony_ci	 * report a SCSI revision level above 2.  Tell the SCSI layer
3448c2ecf20Sopenharmony_ci	 * not to issue that command; it will perform a normal sequential
3458c2ecf20Sopenharmony_ci	 * scan instead.
3468c2ecf20Sopenharmony_ci	 */
3478c2ecf20Sopenharmony_ci	starget->no_report_luns = 1;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	/*
3508c2ecf20Sopenharmony_ci	 * The UFI spec treats the Peripheral Qualifier bits in an
3518c2ecf20Sopenharmony_ci	 * INQUIRY result as reserved and requires devices to set them
3528c2ecf20Sopenharmony_ci	 * to 0.  However the SCSI spec requires these bits to be set
3538c2ecf20Sopenharmony_ci	 * to 3 to indicate when a LUN is not present.
3548c2ecf20Sopenharmony_ci	 *
3558c2ecf20Sopenharmony_ci	 * Let the scanning code know if this target merely sets
3568c2ecf20Sopenharmony_ci	 * Peripheral Device Type to 0x1f to indicate no LUN.
3578c2ecf20Sopenharmony_ci	 */
3588c2ecf20Sopenharmony_ci	if (us->subclass == USB_SC_UFI)
3598c2ecf20Sopenharmony_ci		starget->pdt_1f_for_no_lun = 1;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	return 0;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci/* queue a command */
3658c2ecf20Sopenharmony_ci/* This is always called with scsi_lock(host) held */
3668c2ecf20Sopenharmony_cistatic int queuecommand_lck(struct scsi_cmnd *srb,
3678c2ecf20Sopenharmony_ci			void (*done)(struct scsi_cmnd *))
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(srb->device->host);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* check for state-transition errors */
3728c2ecf20Sopenharmony_ci	if (us->srb != NULL) {
3738c2ecf20Sopenharmony_ci		dev_err(&us->pusb_intf->dev,
3748c2ecf20Sopenharmony_ci			"Error in %s: us->srb = %p\n", __func__, us->srb);
3758c2ecf20Sopenharmony_ci		return SCSI_MLQUEUE_HOST_BUSY;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* fail the command if we are disconnecting */
3798c2ecf20Sopenharmony_ci	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
3808c2ecf20Sopenharmony_ci		usb_stor_dbg(us, "Fail command during disconnect\n");
3818c2ecf20Sopenharmony_ci		srb->result = DID_NO_CONNECT << 16;
3828c2ecf20Sopenharmony_ci		done(srb);
3838c2ecf20Sopenharmony_ci		return 0;
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if ((us->fflags & US_FL_NO_ATA_1X) &&
3878c2ecf20Sopenharmony_ci			(srb->cmnd[0] == ATA_12 || srb->cmnd[0] == ATA_16)) {
3888c2ecf20Sopenharmony_ci		memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
3898c2ecf20Sopenharmony_ci		       sizeof(usb_stor_sense_invalidCDB));
3908c2ecf20Sopenharmony_ci		srb->result = SAM_STAT_CHECK_CONDITION;
3918c2ecf20Sopenharmony_ci		done(srb);
3928c2ecf20Sopenharmony_ci		return 0;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	/* enqueue the command and wake up the control thread */
3968c2ecf20Sopenharmony_ci	srb->scsi_done = done;
3978c2ecf20Sopenharmony_ci	us->srb = srb;
3988c2ecf20Sopenharmony_ci	complete(&us->cmnd_ready);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return 0;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic DEF_SCSI_QCMD(queuecommand)
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci/***********************************************************************
4068c2ecf20Sopenharmony_ci * Error handling functions
4078c2ecf20Sopenharmony_ci ***********************************************************************/
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci/* Command timeout and abort */
4108c2ecf20Sopenharmony_cistatic int command_abort_matching(struct us_data *us, struct scsi_cmnd *srb_match)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	/*
4138c2ecf20Sopenharmony_ci	 * us->srb together with the TIMED_OUT, RESETTING, and ABORTING
4148c2ecf20Sopenharmony_ci	 * bits are protected by the host lock.
4158c2ecf20Sopenharmony_ci	 */
4168c2ecf20Sopenharmony_ci	scsi_lock(us_to_host(us));
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	/* is there any active pending command to abort ? */
4198c2ecf20Sopenharmony_ci	if (!us->srb) {
4208c2ecf20Sopenharmony_ci		scsi_unlock(us_to_host(us));
4218c2ecf20Sopenharmony_ci		usb_stor_dbg(us, "-- nothing to abort\n");
4228c2ecf20Sopenharmony_ci		return SUCCESS;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	/* Does the command match the passed srb if any ? */
4268c2ecf20Sopenharmony_ci	if (srb_match && us->srb != srb_match) {
4278c2ecf20Sopenharmony_ci		scsi_unlock(us_to_host(us));
4288c2ecf20Sopenharmony_ci		usb_stor_dbg(us, "-- pending command mismatch\n");
4298c2ecf20Sopenharmony_ci		return FAILED;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/*
4338c2ecf20Sopenharmony_ci	 * Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if
4348c2ecf20Sopenharmony_ci	 * a device reset isn't already in progress (to avoid interfering
4358c2ecf20Sopenharmony_ci	 * with the reset).  Note that we must retain the host lock while
4368c2ecf20Sopenharmony_ci	 * calling usb_stor_stop_transport(); otherwise it might interfere
4378c2ecf20Sopenharmony_ci	 * with an auto-reset that begins as soon as we release the lock.
4388c2ecf20Sopenharmony_ci	 */
4398c2ecf20Sopenharmony_ci	set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
4408c2ecf20Sopenharmony_ci	if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
4418c2ecf20Sopenharmony_ci		set_bit(US_FLIDX_ABORTING, &us->dflags);
4428c2ecf20Sopenharmony_ci		usb_stor_stop_transport(us);
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci	scsi_unlock(us_to_host(us));
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* Wait for the aborted command to finish */
4478c2ecf20Sopenharmony_ci	wait_for_completion(&us->notify);
4488c2ecf20Sopenharmony_ci	return SUCCESS;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic int command_abort(struct scsi_cmnd *srb)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(srb->device->host);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	usb_stor_dbg(us, "%s called\n", __func__);
4568c2ecf20Sopenharmony_ci	return command_abort_matching(us, srb);
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci/*
4608c2ecf20Sopenharmony_ci * This invokes the transport reset mechanism to reset the state of the
4618c2ecf20Sopenharmony_ci * device
4628c2ecf20Sopenharmony_ci */
4638c2ecf20Sopenharmony_cistatic int device_reset(struct scsi_cmnd *srb)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(srb->device->host);
4668c2ecf20Sopenharmony_ci	int result;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	usb_stor_dbg(us, "%s called\n", __func__);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	/* abort any pending command before reset */
4718c2ecf20Sopenharmony_ci	command_abort_matching(us, NULL);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* lock the device pointers and do the reset */
4748c2ecf20Sopenharmony_ci	mutex_lock(&(us->dev_mutex));
4758c2ecf20Sopenharmony_ci	result = us->transport_reset(us);
4768c2ecf20Sopenharmony_ci	mutex_unlock(&us->dev_mutex);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	return result < 0 ? FAILED : SUCCESS;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci/* Simulate a SCSI bus reset by resetting the device's USB port. */
4828c2ecf20Sopenharmony_cistatic int bus_reset(struct scsi_cmnd *srb)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(srb->device->host);
4858c2ecf20Sopenharmony_ci	int result;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	usb_stor_dbg(us, "%s called\n", __func__);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	result = usb_stor_port_reset(us);
4908c2ecf20Sopenharmony_ci	return result < 0 ? FAILED : SUCCESS;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci/*
4948c2ecf20Sopenharmony_ci * Report a driver-initiated device reset to the SCSI layer.
4958c2ecf20Sopenharmony_ci * Calling this for a SCSI-initiated reset is unnecessary but harmless.
4968c2ecf20Sopenharmony_ci * The caller must own the SCSI host lock.
4978c2ecf20Sopenharmony_ci */
4988c2ecf20Sopenharmony_civoid usb_stor_report_device_reset(struct us_data *us)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	int i;
5018c2ecf20Sopenharmony_ci	struct Scsi_Host *host = us_to_host(us);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	scsi_report_device_reset(host, 0, 0);
5048c2ecf20Sopenharmony_ci	if (us->fflags & US_FL_SCM_MULT_TARG) {
5058c2ecf20Sopenharmony_ci		for (i = 1; i < host->max_id; ++i)
5068c2ecf20Sopenharmony_ci			scsi_report_device_reset(host, 0, i);
5078c2ecf20Sopenharmony_ci	}
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci/*
5118c2ecf20Sopenharmony_ci * Report a driver-initiated bus reset to the SCSI layer.
5128c2ecf20Sopenharmony_ci * Calling this for a SCSI-initiated reset is unnecessary but harmless.
5138c2ecf20Sopenharmony_ci * The caller must not own the SCSI host lock.
5148c2ecf20Sopenharmony_ci */
5158c2ecf20Sopenharmony_civoid usb_stor_report_bus_reset(struct us_data *us)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	struct Scsi_Host *host = us_to_host(us);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	scsi_lock(host);
5208c2ecf20Sopenharmony_ci	scsi_report_bus_reset(host, 0);
5218c2ecf20Sopenharmony_ci	scsi_unlock(host);
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/***********************************************************************
5258c2ecf20Sopenharmony_ci * /proc/scsi/ functions
5268c2ecf20Sopenharmony_ci ***********************************************************************/
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic int write_info(struct Scsi_Host *host, char *buffer, int length)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	/* if someone is sending us data, just throw it away */
5318c2ecf20Sopenharmony_ci	return length;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_cistatic int show_info (struct seq_file *m, struct Scsi_Host *host)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	struct us_data *us = host_to_us(host);
5378c2ecf20Sopenharmony_ci	const char *string;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* print the controller name */
5408c2ecf20Sopenharmony_ci	seq_printf(m, "   Host scsi%d: usb-storage\n", host->host_no);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	/* print product, vendor, and serial number strings */
5438c2ecf20Sopenharmony_ci	if (us->pusb_dev->manufacturer)
5448c2ecf20Sopenharmony_ci		string = us->pusb_dev->manufacturer;
5458c2ecf20Sopenharmony_ci	else if (us->unusual_dev->vendorName)
5468c2ecf20Sopenharmony_ci		string = us->unusual_dev->vendorName;
5478c2ecf20Sopenharmony_ci	else
5488c2ecf20Sopenharmony_ci		string = "Unknown";
5498c2ecf20Sopenharmony_ci	seq_printf(m, "       Vendor: %s\n", string);
5508c2ecf20Sopenharmony_ci	if (us->pusb_dev->product)
5518c2ecf20Sopenharmony_ci		string = us->pusb_dev->product;
5528c2ecf20Sopenharmony_ci	else if (us->unusual_dev->productName)
5538c2ecf20Sopenharmony_ci		string = us->unusual_dev->productName;
5548c2ecf20Sopenharmony_ci	else
5558c2ecf20Sopenharmony_ci		string = "Unknown";
5568c2ecf20Sopenharmony_ci	seq_printf(m, "      Product: %s\n", string);
5578c2ecf20Sopenharmony_ci	if (us->pusb_dev->serial)
5588c2ecf20Sopenharmony_ci		string = us->pusb_dev->serial;
5598c2ecf20Sopenharmony_ci	else
5608c2ecf20Sopenharmony_ci		string = "None";
5618c2ecf20Sopenharmony_ci	seq_printf(m, "Serial Number: %s\n", string);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	/* show the protocol and transport */
5648c2ecf20Sopenharmony_ci	seq_printf(m, "     Protocol: %s\n", us->protocol_name);
5658c2ecf20Sopenharmony_ci	seq_printf(m, "    Transport: %s\n", us->transport_name);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/* show the device flags */
5688c2ecf20Sopenharmony_ci	seq_printf(m, "       Quirks:");
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci#define US_FLAG(name, value) \
5718c2ecf20Sopenharmony_ci	if (us->fflags & value) seq_printf(m, " " #name);
5728c2ecf20Sopenharmony_ciUS_DO_ALL_FLAGS
5738c2ecf20Sopenharmony_ci#undef US_FLAG
5748c2ecf20Sopenharmony_ci	seq_putc(m, '\n');
5758c2ecf20Sopenharmony_ci	return 0;
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci/***********************************************************************
5798c2ecf20Sopenharmony_ci * Sysfs interface
5808c2ecf20Sopenharmony_ci ***********************************************************************/
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci/* Output routine for the sysfs max_sectors file */
5838c2ecf20Sopenharmony_cistatic ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	struct scsi_device *sdev = to_scsi_device(dev);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci/* Input routine for the sysfs max_sectors file */
5918c2ecf20Sopenharmony_cistatic ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
5928c2ecf20Sopenharmony_ci		size_t count)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct scsi_device *sdev = to_scsi_device(dev);
5958c2ecf20Sopenharmony_ci	unsigned short ms;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (sscanf(buf, "%hu", &ms) > 0) {
5988c2ecf20Sopenharmony_ci		blk_queue_max_hw_sectors(sdev->request_queue, ms);
5998c2ecf20Sopenharmony_ci		return count;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci	return -EINVAL;
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(max_sectors);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_cistatic struct device_attribute *sysfs_device_attr_list[] = {
6068c2ecf20Sopenharmony_ci	&dev_attr_max_sectors,
6078c2ecf20Sopenharmony_ci	NULL,
6088c2ecf20Sopenharmony_ci};
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci/*
6118c2ecf20Sopenharmony_ci * this defines our host template, with which we'll allocate hosts
6128c2ecf20Sopenharmony_ci */
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic const struct scsi_host_template usb_stor_host_template = {
6158c2ecf20Sopenharmony_ci	/* basic userland interface stuff */
6168c2ecf20Sopenharmony_ci	.name =				"usb-storage",
6178c2ecf20Sopenharmony_ci	.proc_name =			"usb-storage",
6188c2ecf20Sopenharmony_ci	.show_info =			show_info,
6198c2ecf20Sopenharmony_ci	.write_info =			write_info,
6208c2ecf20Sopenharmony_ci	.info =				host_info,
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	/* command interface -- queued only */
6238c2ecf20Sopenharmony_ci	.queuecommand =			queuecommand,
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* error and abort handlers */
6268c2ecf20Sopenharmony_ci	.eh_abort_handler =		command_abort,
6278c2ecf20Sopenharmony_ci	.eh_device_reset_handler =	device_reset,
6288c2ecf20Sopenharmony_ci	.eh_bus_reset_handler =		bus_reset,
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/* queue commands only, only one command per LUN */
6318c2ecf20Sopenharmony_ci	.can_queue =			1,
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	/* unknown initiator id */
6348c2ecf20Sopenharmony_ci	.this_id =			-1,
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	.slave_alloc =			slave_alloc,
6378c2ecf20Sopenharmony_ci	.slave_configure =		slave_configure,
6388c2ecf20Sopenharmony_ci	.target_alloc =			target_alloc,
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* lots of sg segments can be handled */
6418c2ecf20Sopenharmony_ci	.sg_tablesize =			SG_MAX_SEGMENTS,
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	/*
6458c2ecf20Sopenharmony_ci	 * Limit the total size of a transfer to 120 KB.
6468c2ecf20Sopenharmony_ci	 *
6478c2ecf20Sopenharmony_ci	 * Some devices are known to choke with anything larger. It seems like
6488c2ecf20Sopenharmony_ci	 * the problem stems from the fact that original IDE controllers had
6498c2ecf20Sopenharmony_ci	 * only an 8-bit register to hold the number of sectors in one transfer
6508c2ecf20Sopenharmony_ci	 * and even those couldn't handle a full 256 sectors.
6518c2ecf20Sopenharmony_ci	 *
6528c2ecf20Sopenharmony_ci	 * Because we want to make sure we interoperate with as many devices as
6538c2ecf20Sopenharmony_ci	 * possible, we will maintain a 240 sector transfer size limit for USB
6548c2ecf20Sopenharmony_ci	 * Mass Storage devices.
6558c2ecf20Sopenharmony_ci	 *
6568c2ecf20Sopenharmony_ci	 * Tests show that other operating have similar limits with Microsoft
6578c2ecf20Sopenharmony_ci	 * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
6588c2ecf20Sopenharmony_ci	 * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
6598c2ecf20Sopenharmony_ci	 * and 2048 for USB3 devices.
6608c2ecf20Sopenharmony_ci	 */
6618c2ecf20Sopenharmony_ci	.max_sectors =                  240,
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/* emulated HBA */
6648c2ecf20Sopenharmony_ci	.emulated =			1,
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	/* we do our own delay after a device or bus reset */
6678c2ecf20Sopenharmony_ci	.skip_settle_delay =		1,
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/* sysfs device attributes */
6708c2ecf20Sopenharmony_ci	.sdev_attrs =			sysfs_device_attr_list,
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	/* module management */
6738c2ecf20Sopenharmony_ci	.module =			THIS_MODULE
6748c2ecf20Sopenharmony_ci};
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_civoid usb_stor_host_template_init(struct scsi_host_template *sht,
6778c2ecf20Sopenharmony_ci				 const char *name, struct module *owner)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	*sht = usb_stor_host_template;
6808c2ecf20Sopenharmony_ci	sht->name = name;
6818c2ecf20Sopenharmony_ci	sht->proc_name = name;
6828c2ecf20Sopenharmony_ci	sht->module = owner;
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_host_template_init);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci/* To Report "Illegal Request: Invalid Field in CDB */
6878c2ecf20Sopenharmony_ciunsigned char usb_stor_sense_invalidCDB[18] = {
6888c2ecf20Sopenharmony_ci	[0]	= 0x70,			    /* current error */
6898c2ecf20Sopenharmony_ci	[2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */
6908c2ecf20Sopenharmony_ci	[7]	= 0x0a,			    /* additional length */
6918c2ecf20Sopenharmony_ci	[12]	= 0x24			    /* Invalid Field in CDB */
6928c2ecf20Sopenharmony_ci};
6938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);
694