11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Driver for USB Mass Storage compliant devices
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * SCSI layer glue code
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Current development and maintenance by:
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Developed with the assistance of:
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Initial work by:
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 1999 Michael Gee (michael@linuxspecific.com)
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This driver is based on the 'USB Mass Storage Class' document. This
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * describes in detail the protocol used to communicate with such
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * devices.  Clearly, the designers had SCSI and ATAPI commands in
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * mind when they created this document.  The commands are all very
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * similar to commands in the SCSI-II and ATAPI specifications.
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * It is important to note that in a number of cases this class
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * exhibits class-specific exemptions from the USB specification.
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notably the usage of NAK, STALL and ACK differs from the norm, in
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that they are used to communicate wait, failed and OK on commands.
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Also, for certain devices, the interrupt endpoint is used to convey
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * status of a command.
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * information about this driver.
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify it
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * under the terms of the GNU General Public License as published by the
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Free Software Foundation; either version 2, or (at your option) any
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * later version.
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is distributed in the hope that it will be useful, but
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * General Public License for more details.
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * You should have received a copy of the GNU General Public License along
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc.,
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA.
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
474186ecf8ad16dd05759a09594de6a87e48759ba6Arjan van de Ven#include <linux/mutex.h>
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi.h>
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_cmnd.h>
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_devinfo.h>
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_eh.h>
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "usb.h"
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsiglue.h"
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "debug.h"
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "transport.h"
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "protocol.h"
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
61a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern/* Vendor IDs for companies that seem to include the READ CAPACITY bug
62a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * in all their devices
63a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern */
64a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_NOKIA		0x0421
65a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_NIKON		0x04b0
66506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern#define VENDOR_ID_PENTAX	0x0a17
67a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_MOTOROLA	0x22b8
68a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host functions
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char* host_info(struct Scsi_Host *host)
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7500fa43ef09c6bb357d58c14a99181cce09c315c6Matthew Wilcox	struct us_data *us = host_to_us(host);
7600fa43ef09c6bb357d58c14a99181cce09c315c6Matthew Wilcox	return us->scsi_name;
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_alloc (struct scsi_device *sdev)
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set the INQUIRY transfer length to 36.  We don't use any of
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the extra data and many devices choke if asked for more or
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * less than 36 bytes.
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sdev->inquiry_len = 36;
873a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
88f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	/* USB has unusual DMA-alignment requirements: Although the
89f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * starting address of each scatter-gather element doesn't matter,
90f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * the length of each element except the last must be divisible
91f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * by the Bulk maxpacket value.  There's currently no way to
92f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * express this by block-layer constraints, so we'll cop out
93f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and simply require addresses to be aligned at 512-byte
94f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * boundaries.  This is okay since most block I/O involves
95f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * hardware sectors that are multiples of 512 bytes in length,
96f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and since host controllers up through USB 2.0 have maxpacket
97f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values no larger than 512.
98f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 *
99f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
100f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values can be as large as 2048.  To make that work properly
101f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * will require changes to the block layer.
102148d9fe4c91a6356dae1b05b76b8133586c26be4Alan Stern	 */
103f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
104465ff3185e0cb76d46137335a4d21d0d9d3ac8a2James Bottomley
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_configure(struct scsi_device *sdev)
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(sdev->host);
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11225985edcedea6396277003854657b5f3cb31a628Lucas De Marchi	/* Many devices have trouble transferring more than 32KB at a time,
113883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * while others have trouble with more than 64K. At this time we
114883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * are limiting both to 32K (64 sectores).
115883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 */
1167e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
11733abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey		unsigned int max_sectors = 64;
11833abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey
1197e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_MAX_SECTORS_MIN)
12033abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey			max_sectors = PAGE_CACHE_SIZE >> 9;
12149d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern		if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
122086fa5ff0854c676ec333760f4c0154b3b242616Martin K. Petersen			blk_queue_max_hw_sectors(sdev->request_queue,
12333abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey					      max_sectors);
1245c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern	} else if (sdev->type == TYPE_TAPE) {
1255c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		/* Tapes need much higher max_sector limits, so just
1265c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		 * raise it to the maximum possible (4 GB / 512) and
1275c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		 * let the queue segment size sort out the real limit.
1285c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		 */
129086fa5ff0854c676ec333760f4c0154b3b242616Martin K. Petersen		blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
13033abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey	}
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13296983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	/* Some USB host controllers can't do DMA; they have to use PIO.
13396983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * They indicate this by setting their dma_mask to NULL.  For
13496983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * such controllers we need to make sure the block layer sets
13596983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * up bounce buffers in addressable memory.
13696983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 */
13796983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	if (!us->pusb_dev->bus->controller->dma_mask)
13896983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern		blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
13996983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* We can't put these settings in slave_alloc() because that gets
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * called before the device type is known.  Consequently these
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * settings can't be overridden via the scsi devinfo mechanism. */
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sdev->type == TYPE_DISK) {
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
145a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		/* Some vendors seem to put the READ CAPACITY bug into
146a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * all their devices -- primarily makers of cell phones
147a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * and digital cameras.  Since these devices always use
148a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * flash media and can be expected to have an even number
149a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * of sectors, we will always enable the CAPACITY_HEURISTICS
150a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * flag unless told otherwise. */
151a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
152a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_NOKIA:
153a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_NIKON:
154506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern		case VENDOR_ID_PENTAX:
155a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_MOTOROLA:
156a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern			if (!(us->fflags & (US_FL_FIX_CAPACITY |
157a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern					US_FL_CAPACITY_OK)))
158a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern				us->fflags |= US_FL_CAPACITY_HEURISTICS;
159a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern			break;
160a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		}
161a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Disk-type devices use MODE SENSE(6) if the protocol
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * (SubClass) is Transparent SCSI, otherwise they use
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * MODE SENSE(10). */
1658fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz		if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->use_10_for_ms = 1;
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Many disks only accept MODE SENSE transfer lengths of
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * 192 bytes (that's what Windows uses). */
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_192_bytes_for_3f = 1;
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some devices don't like MODE SENSE with page=0x3f,
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * which is the command used for checking if a device
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * is write-protected.  Now that we tell the sd driver
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to do a 192-byte transfer with this command the
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * majority of devices work fine, but a few still can't
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * handle it.  The sd driver will simply assume those
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * devices are write-enabled. */
1797e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_NO_WP_DETECT)
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->skip_ms_page_3f = 1;
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* A number of devices have problems with MODE SENSE for
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * page x08, so we will skip it. */
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->skip_ms_page_8 = 1;
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18609b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern		/* Some devices don't handle VPD pages correctly */
18709b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern		sdev->skip_vpd_pages = 1;
18809b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some disks return the total number of blocks in response
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to READ CAPACITY rather than the highest block number.
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If this device makes that mistake, tell the sd driver. */
1927e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_FIX_CAPACITY)
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->fix_capacity = 1;
19486dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
19561bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		/* A few disks have two indistinguishable version, one of
19661bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * which reports the correct capacity and the other does not.
19761bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * The sd driver has to guess which is the case. */
1987e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_CAPACITY_HEURISTICS)
19961bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum			sdev->guess_capacity = 1;
20061bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum
20100914025cc4e783d4703b4db1d47b41f389e50c8Hans de Goede		/* Some devices cannot handle READ_CAPACITY_16 */
20200914025cc4e783d4703b4db1d47b41f389e50c8Hans de Goede		if (us->fflags & US_FL_NO_READ_CAPACITY_16)
20300914025cc4e783d4703b4db1d47b41f389e50c8Hans de Goede			sdev->no_read_capacity_16 = 1;
20400914025cc4e783d4703b4db1d47b41f389e50c8Hans de Goede
205b18504e4a631e31edd939fe569342274927d4b41Alan Stern		/*
206b18504e4a631e31edd939fe569342274927d4b41Alan Stern		 * Many devices do not respond properly to READ_CAPACITY_16.
207b18504e4a631e31edd939fe569342274927d4b41Alan Stern		 * Tell the SCSI layer to try READ_CAPACITY_10 first.
208b18504e4a631e31edd939fe569342274927d4b41Alan Stern		 */
209b18504e4a631e31edd939fe569342274927d4b41Alan Stern		sdev->try_rc_10_first = 1;
210b18504e4a631e31edd939fe569342274927d4b41Alan Stern
2111537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		/* assume SPC3 or latter devices support sense size > 18 */
2121537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		if (sdev->scsi_level > SCSI_SPC_2)
2131537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros			us->fflags |= US_FL_SANE_SENSE;
2141537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros
21586dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		/* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
21686dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * Hardware Error) when any low-level error occurs,
21786dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * recoverable or not.  Setting this flag tells the SCSI
21886dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * midlayer to retry such commands, which frequently will
21986dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * succeed and fix the error.  The worst this can lead to
22086dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * is an occasional series of retries that will all fail. */
22186dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		sdev->retry_hwerror = 1;
22286dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
223f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		/* USB disks should allow restart.  Some drives spin down
224f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		 * automatically, requiring a START-STOP UNIT command. */
225f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		sdev->allow_restart = 1;
226f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab
22723c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		/* Some USB cardreaders have trouble reading an sdcard's last
22823c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * sector in a larger then 1 sector read, since the performance
22923c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * impact is negible we set this flag for all USB disks */
23023c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		sdev->last_sector_bug = 1;
23125ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern
23225ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		/* Enable last-sector hacks for single-target devices using
23325ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		 * the Bulk-only transport, unless we already know the
23425ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		 * capacity will be decremented or is correct. */
23525ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
23625ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern					US_FL_SCM_MULT_TARG)) &&
2378fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz				us->protocol == USB_PR_BULK)
23825ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern			us->use_last_sector_hacks = 1;
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Non-disk-type devices don't need to blacklist any pages
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * or to force 192-byte transfer lengths for MODE SENSE.
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * But they do need to use MODE SENSE(10). */
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_10_for_ms = 1;
245ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede
246ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede		/* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
247ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede		if (us->fflags & US_FL_NO_READ_DISC_INFO)
248ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede			sdev->no_read_disc_info = 1;
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
251f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	/* The CB and CBI transports have no way to pass LUN values
252f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * other than the bits in the second byte of a CDB.  But those
253f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * bits don't get set to the LUN value if the device reports
254f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily
255f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * be single-LUN.
256f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 */
2578fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz	if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
258f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->scsi_level == SCSI_UNKNOWN)
259f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		us->max_lun = 0;
260f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * REMOVAL command, so suppress those commands. */
2637e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_NOT_LOCKABLE)
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->lockable = 0;
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* this is to satisfy the compiler, tho I don't think the
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * return code is ever checked anywhere. */
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27109b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Sternstatic int target_alloc(struct scsi_target *starget)
27209b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern{
273af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
274af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern
27509b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	/*
27609b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	 * Some USB drives don't support REPORT LUNS, even though they
27709b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	 * report a SCSI revision level above 2.  Tell the SCSI layer
27809b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	 * not to issue that command; it will perform a normal sequential
27909b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	 * scan instead.
28009b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	 */
28109b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	starget->no_report_luns = 1;
282af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern
283af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	/*
284af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 * The UFI spec treats the Peripheral Qualifier bits in an
285af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 * INQUIRY result as reserved and requires devices to set them
286af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 * to 0.  However the SCSI spec requires these bits to be set
287af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 * to 3 to indicate when a LUN is not present.
288af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 *
289af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 * Let the scanning code know if this target merely sets
290af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 * Peripheral Device Type to 0x1f to indicate no LUN.
291af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	 */
292af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern	if (us->subclass == USB_SC_UFI)
293af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern		starget->pdt_1f_for_no_lun = 1;
294af74d2dae8f85a0e90a30594beb507f5d954fa3fAlan Stern
29509b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	return 0;
29609b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern}
29709b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* queue a command */
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This is always called with scsi_lock(host) held */
300f281233d3eba15fb225d21ae2e228fd4553d824aJeff Garzikstatic int queuecommand_lck(struct scsi_cmnd *srb,
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			void (*done)(struct scsi_cmnd *))
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
305441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check for state-transition errors */
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != NULL) {
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
310441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, us->srb);
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return SCSI_MLQUEUE_HOST_BUSY;
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* fail the command if we are disconnecting */
3157e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP("Fail command during disconnect\n");
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		srb->result = DID_NO_CONNECT << 16;
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		done(srb);
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* enqueue the command and wake up the control thread */
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	srb->scsi_done = done;
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	us->srb = srb;
3257119e3c37fbf7c27adb5929f344c826ecb8c7859Alan Stern	complete(&us->cmnd_ready);
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
330f281233d3eba15fb225d21ae2e228fd4553d824aJeff Garzikstatic DEF_SCSI_QCMD(queuecommand)
331f281233d3eba15fb225d21ae2e228fd4553d824aJeff Garzik
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Error handling functions
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Command timeout and abort */
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int command_abort(struct scsi_cmnd *srb)
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
341441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
343226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	/* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
344226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * bits are protected by the host lock. */
345226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_lock(us_to_host(us));
346226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Is this command still active? */
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != srb) {
349226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm		scsi_unlock(us_to_host(us));
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP ("-- nothing to abort\n");
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return FAILED;
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * a device reset isn't already in progress (to avoid interfering
356226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with the reset).  Note that we must retain the host lock while
357226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * calling usb_stor_stop_transport(); otherwise it might interfere
358226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with an auto-reset that begins as soon as we release the lock. */
3597e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
3607e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
3617e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		set_bit(US_FLIDX_ABORTING, &us->dflags);
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_stor_stop_transport(us);
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
364226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_unlock(us_to_host(us));
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Wait for the aborted command to finish */
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_for_completion(&us->notify);
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return SUCCESS;
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This invokes the transport reset mechanism to reset the state of the
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device */
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int device_reset(struct scsi_cmnd *srb)
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
378441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
380d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	/* lock the device pointers and do the reset */
381d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_lock(&(us->dev_mutex));
382d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	result = us->transport_reset(us);
383d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_unlock(&us->dev_mutex);
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3854d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	return result < 0 ? FAILED : SUCCESS;
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3884d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Simulate a SCSI bus reset by resetting the device's USB port. */
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int bus_reset(struct scsi_cmnd *srb)
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3924d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	int result;
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
394441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3954d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	result = usb_stor_port_reset(us);
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result < 0 ? FAILED : SUCCESS;
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Report a driver-initiated device reset to the SCSI layer.
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Calling this for a SCSI-initiated reset is unnecessary but harmless.
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The caller must own the SCSI host lock. */
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid usb_stor_report_device_reset(struct us_data *us)
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *host = us_to_host(us);
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	scsi_report_device_reset(host, 0, 0);
4087e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_SCM_MULT_TARG) {
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i = 1; i < host->max_id; ++i)
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			scsi_report_device_reset(host, 0, i);
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4144d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Report a driver-initiated bus reset to the SCSI layer.
4154d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm * Calling this for a SCSI-initiated reset is unnecessary but harmless.
416f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern * The caller must not own the SCSI host lock. */
4174d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharmvoid usb_stor_report_bus_reset(struct us_data *us)
4184d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm{
419f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	struct Scsi_Host *host = us_to_host(us);
420f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern
421f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_lock(host);
422f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_report_bus_reset(host, 0);
423f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_unlock(host);
4244d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm}
4254d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * /proc/scsi/ functions
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* we use this macro to help us write into the buffer */
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef SPRINTF
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SPRINTF(args...) \
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int proc_info (struct Scsi_Host *host, char *buffer,
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		char **start, off_t offset, int length, int inout)
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(host);
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *pos = buffer;
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *string;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if someone is sending us data, just throw it away */
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (inout)
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return length;
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print the controller name */
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("   Host scsi%d: usb-storage\n", host->host_no);
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print product, vendor, and serial number strings */
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->manufacturer)
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->manufacturer;
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->vendorName)
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->vendorName;
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("       Vendor: %s\n", string);
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->product)
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->product;
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->productName)
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->productName;
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("      Product: %s\n", string);
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->serial)
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->serial;
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "None";
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("Serial Number: %s\n", string);
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the protocol and transport */
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("     Protocol: %s\n", us->protocol_name);
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("    Transport: %s\n", us->transport_name);
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the device flags */
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (pos < buffer + length) {
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		pos += sprintf(pos, "       Quirks:");
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define US_FLAG(name, value) \
4797e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & value) pos += sprintf(pos, " " #name);
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsUS_DO_ALL_FLAGS
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef US_FLAG
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*(pos++) = '\n';
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Calculate start of next buffer, and return value.
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*start = buffer + offset;
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((pos - buffer) < offset)
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (0);
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if ((pos - buffer - offset) < length)
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (pos - buffer - offset);
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (length);
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs interface
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Output routine for the sysfs max_sectors file */
504060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
50849d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern	return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Input routine for the sysfs max_sectors file */
512060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		size_t count)
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned short ms;
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
51849d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern	if (sscanf(buf, "%hu", &ms) > 0) {
519086fa5ff0854c676ec333760f4c0154b3b242616Martin K. Petersen		blk_queue_max_hw_sectors(sdev->request_queue, ms);
52049d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern		return count;
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EINVAL;
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		store_max_sectors);
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct device_attribute *sysfs_device_attr_list[] = {
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		&dev_attr_max_sectors,
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		NULL,
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		};
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this defines our host template, with which we'll allocate hosts
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_host_template usb_stor_host_template = {
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* basic userland interface stuff */
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name =				"usb-storage",
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_name =			"usb-storage",
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_info =			proc_info,
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.info =				host_info,
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* command interface -- queued only */
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.queuecommand =			queuecommand,
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* error and abort handlers */
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_abort_handler =		command_abort,
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_device_reset_handler =	device_reset,
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_bus_reset_handler =		bus_reset,
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* queue commands only, only one command per LUN */
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.can_queue =			1,
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.cmd_per_lun =			1,
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* unknown initiator id */
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.this_id =			-1,
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_alloc =			slave_alloc,
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_configure =		slave_configure,
56109b6b51b0b6c1b9bb61815baf205e4d74c89ff04Alan Stern	.target_alloc =			target_alloc,
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* lots of sg segments can be handled */
56449d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern	.sg_tablesize =			SCSI_MAX_SG_CHAIN_SEGMENTS,
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* limit the total size of a transfer to 120 KB */
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.max_sectors =                  240,
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* merge commands... this seems to help performance, but
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * periodically someone should test to see which setting is more
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * optimal.
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.use_clustering =		1,
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* emulated HBA */
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.emulated =			1,
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* we do our own delay after a device or bus reset */
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.skip_settle_delay =		1,
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* sysfs device attributes */
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sdev_attrs =			sysfs_device_attr_list,
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* module management */
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.module =			THIS_MODULE
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* To Report "Illegal Request: Invalid Field in CDB */
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned char usb_stor_sense_invalidCDB[18] = {
5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[0]	= 0x70,			    /* current error */
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[7]	= 0x0a,			    /* additional length */
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[12]	= 0x24			    /* Invalid Field in CDB */
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
595e6e244b6cb1f70e7109381626293cd40a8334ed3Alan SternEXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);
596