scsiglue.c revision ae38c78a03e1b77ad45248fcf097e4568e740209
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{
813a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	struct us_data *us = host_to_us(sdev->host);
823a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set the INQUIRY transfer length to 36.  We don't use any of
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the extra data and many devices choke if asked for more or
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * less than 36 bytes.
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sdev->inquiry_len = 36;
893a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
90f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	/* USB has unusual DMA-alignment requirements: Although the
91f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * starting address of each scatter-gather element doesn't matter,
92f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * the length of each element except the last must be divisible
93f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * by the Bulk maxpacket value.  There's currently no way to
94f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * express this by block-layer constraints, so we'll cop out
95f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and simply require addresses to be aligned at 512-byte
96f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * boundaries.  This is okay since most block I/O involves
97f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * hardware sectors that are multiples of 512 bytes in length,
98f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and since host controllers up through USB 2.0 have maxpacket
99f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values no larger than 512.
100f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 *
101f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
102f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values can be as large as 2048.  To make that work properly
103f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * will require changes to the block layer.
104148d9fe4c91a6356dae1b05b76b8133586c26be4Alan Stern	 */
105f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
106465ff3185e0cb76d46137335a4d21d0d9d3ac8a2James Bottomley
1073a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	/*
1083a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * The UFI spec treates the Peripheral Qualifier bits in an
1093a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * INQUIRY result as reserved and requires devices to set them
1103a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * to 0.  However the SCSI spec requires these bits to be set
1113a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * to 3 to indicate when a LUN is not present.
1123a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 *
1133a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * Let the scanning code know if this target merely sets
1143a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * Peripheral Device Type to 0x1f to indicate no LUN.
1153a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 */
1168fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz	if (us->subclass == USB_SC_UFI)
1173a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern		sdev->sdev_target->pdt_1f_for_no_lun = 1;
1183a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_configure(struct scsi_device *sdev)
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(sdev->host);
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
126883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	/* Many devices have trouble transfering more than 32KB at a time,
127883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * while others have trouble with more than 64K. At this time we
128883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * are limiting both to 32K (64 sectores).
129883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 */
1307e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
13133abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey		unsigned int max_sectors = 64;
13233abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey
1337e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_MAX_SECTORS_MIN)
13433abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey			max_sectors = PAGE_CACHE_SIZE >> 9;
13549d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern		if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
136086fa5ff0854c676ec333760f4c0154b3b242616Martin K. Petersen			blk_queue_max_hw_sectors(sdev->request_queue,
13733abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey					      max_sectors);
1385c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern	} else if (sdev->type == TYPE_TAPE) {
1395c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		/* Tapes need much higher max_sector limits, so just
1405c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		 * raise it to the maximum possible (4 GB / 512) and
1415c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		 * let the queue segment size sort out the real limit.
1425c16034d73da2c1b663aa25dedadbc533b3d811cAlan Stern		 */
143086fa5ff0854c676ec333760f4c0154b3b242616Martin K. Petersen		blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
14433abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey	}
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14696983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	/* Some USB host controllers can't do DMA; they have to use PIO.
14796983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * They indicate this by setting their dma_mask to NULL.  For
14896983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * such controllers we need to make sure the block layer sets
14996983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * up bounce buffers in addressable memory.
15096983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 */
15196983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	if (!us->pusb_dev->bus->controller->dma_mask)
15296983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern		blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
15396983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* We can't put these settings in slave_alloc() because that gets
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * called before the device type is known.  Consequently these
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * settings can't be overridden via the scsi devinfo mechanism. */
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sdev->type == TYPE_DISK) {
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
159a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		/* Some vendors seem to put the READ CAPACITY bug into
160a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * all their devices -- primarily makers of cell phones
161a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * and digital cameras.  Since these devices always use
162a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * flash media and can be expected to have an even number
163a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * of sectors, we will always enable the CAPACITY_HEURISTICS
164a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * flag unless told otherwise. */
165a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
166a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_NOKIA:
167a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_NIKON:
168506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern		case VENDOR_ID_PENTAX:
169a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_MOTOROLA:
170a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern			if (!(us->fflags & (US_FL_FIX_CAPACITY |
171a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern					US_FL_CAPACITY_OK)))
172a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern				us->fflags |= US_FL_CAPACITY_HEURISTICS;
173a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern			break;
174a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		}
175a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Disk-type devices use MODE SENSE(6) if the protocol
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * (SubClass) is Transparent SCSI, otherwise they use
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * MODE SENSE(10). */
1798fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz		if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->use_10_for_ms = 1;
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Many disks only accept MODE SENSE transfer lengths of
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * 192 bytes (that's what Windows uses). */
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_192_bytes_for_3f = 1;
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some devices don't like MODE SENSE with page=0x3f,
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * which is the command used for checking if a device
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * is write-protected.  Now that we tell the sd driver
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to do a 192-byte transfer with this command the
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * majority of devices work fine, but a few still can't
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * handle it.  The sd driver will simply assume those
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * devices are write-enabled. */
1937e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_NO_WP_DETECT)
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->skip_ms_page_3f = 1;
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* A number of devices have problems with MODE SENSE for
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * page x08, so we will skip it. */
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->skip_ms_page_8 = 1;
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some disks return the total number of blocks in response
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to READ CAPACITY rather than the highest block number.
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If this device makes that mistake, tell the sd driver. */
2037e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_FIX_CAPACITY)
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->fix_capacity = 1;
20586dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
20661bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		/* A few disks have two indistinguishable version, one of
20761bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * which reports the correct capacity and the other does not.
20861bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * The sd driver has to guess which is the case. */
2097e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_CAPACITY_HEURISTICS)
21061bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum			sdev->guess_capacity = 1;
21161bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum
2121537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		/* assume SPC3 or latter devices support sense size > 18 */
2131537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		if (sdev->scsi_level > SCSI_SPC_2)
2141537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros			us->fflags |= US_FL_SANE_SENSE;
2151537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros
216a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		/* Some devices report a SCSI revision level above 2 but are
217a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * unable to handle the REPORT LUNS command (for which
218a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * support is mandatory at level 3).  Since we already have
219a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * a Get-Max-LUN request, we won't lose much by setting the
220a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * revision level down to 2.  The only devices that would be
221a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * affected are those with sparse LUNs. */
222f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		if (sdev->scsi_level > SCSI_2)
223f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->sdev_target->scsi_level =
224f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern					sdev->scsi_level = SCSI_2;
225a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm
22686dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		/* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
22786dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * Hardware Error) when any low-level error occurs,
22886dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * recoverable or not.  Setting this flag tells the SCSI
22986dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * midlayer to retry such commands, which frequently will
23086dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * succeed and fix the error.  The worst this can lead to
23186dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * is an occasional series of retries that will all fail. */
23286dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		sdev->retry_hwerror = 1;
23386dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
234f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		/* USB disks should allow restart.  Some drives spin down
235f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		 * automatically, requiring a START-STOP UNIT command. */
236f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		sdev->allow_restart = 1;
237f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab
23823c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		/* Some USB cardreaders have trouble reading an sdcard's last
23923c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * sector in a larger then 1 sector read, since the performance
24023c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * impact is negible we set this flag for all USB disks */
24123c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		sdev->last_sector_bug = 1;
24225ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern
24325ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		/* Enable last-sector hacks for single-target devices using
24425ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		 * the Bulk-only transport, unless we already know the
24525ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		 * capacity will be decremented or is correct. */
24625ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
24725ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern					US_FL_SCM_MULT_TARG)) &&
2488fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz				us->protocol == USB_PR_BULK)
24925ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern			us->use_last_sector_hacks = 1;
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Non-disk-type devices don't need to blacklist any pages
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * or to force 192-byte transfer lengths for MODE SENSE.
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * But they do need to use MODE SENSE(10). */
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_10_for_ms = 1;
256ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede
257ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede		/* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
258ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede		if (us->fflags & US_FL_NO_READ_DISC_INFO)
259ae38c78a03e1b77ad45248fcf097e4568e740209Hans de Goede			sdev->no_read_disc_info = 1;
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
262f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	/* The CB and CBI transports have no way to pass LUN values
263f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * other than the bits in the second byte of a CDB.  But those
264f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * bits don't get set to the LUN value if the device reports
265f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily
266f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * be single-LUN.
267f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 */
2688fa7fd74ef398370383df276ca41082ba35aafd8Michal Nazarewicz	if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
269f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->scsi_level == SCSI_UNKNOWN)
270f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		us->max_lun = 0;
271f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * REMOVAL command, so suppress those commands. */
2747e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_NOT_LOCKABLE)
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->lockable = 0;
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* this is to satisfy the compiler, tho I don't think the
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * return code is ever checked anywhere. */
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* queue a command */
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This is always called with scsi_lock(host) held */
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int queuecommand(struct scsi_cmnd *srb,
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			void (*done)(struct scsi_cmnd *))
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
289441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check for state-transition errors */
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != NULL) {
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
294441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, us->srb);
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return SCSI_MLQUEUE_HOST_BUSY;
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* fail the command if we are disconnecting */
2997e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP("Fail command during disconnect\n");
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		srb->result = DID_NO_CONNECT << 16;
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		done(srb);
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* enqueue the command and wake up the control thread */
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	srb->scsi_done = done;
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	us->srb = srb;
3097119e3c37fbf7c27adb5929f344c826ecb8c7859Alan Stern	complete(&us->cmnd_ready);
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Error handling functions
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Command timeout and abort */
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int command_abort(struct scsi_cmnd *srb)
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
323441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
325226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	/* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
326226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * bits are protected by the host lock. */
327226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_lock(us_to_host(us));
328226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Is this command still active? */
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != srb) {
331226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm		scsi_unlock(us_to_host(us));
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP ("-- nothing to abort\n");
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return FAILED;
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * a device reset isn't already in progress (to avoid interfering
338226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with the reset).  Note that we must retain the host lock while
339226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * calling usb_stor_stop_transport(); otherwise it might interfere
340226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with an auto-reset that begins as soon as we release the lock. */
3417e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
3427e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
3437e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		set_bit(US_FLIDX_ABORTING, &us->dflags);
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_stor_stop_transport(us);
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
346226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_unlock(us_to_host(us));
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Wait for the aborted command to finish */
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_for_completion(&us->notify);
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return SUCCESS;
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This invokes the transport reset mechanism to reset the state of the
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device */
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int device_reset(struct scsi_cmnd *srb)
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
360441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
362d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	/* lock the device pointers and do the reset */
363d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_lock(&(us->dev_mutex));
364d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	result = us->transport_reset(us);
365d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_unlock(&us->dev_mutex);
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3674d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	return result < 0 ? FAILED : SUCCESS;
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3704d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Simulate a SCSI bus reset by resetting the device's USB port. */
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int bus_reset(struct scsi_cmnd *srb)
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3744d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	int result;
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
376441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3774d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	result = usb_stor_port_reset(us);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result < 0 ? FAILED : SUCCESS;
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Report a driver-initiated device reset to the SCSI layer.
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Calling this for a SCSI-initiated reset is unnecessary but harmless.
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The caller must own the SCSI host lock. */
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid usb_stor_report_device_reset(struct us_data *us)
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *host = us_to_host(us);
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	scsi_report_device_reset(host, 0, 0);
3907e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_SCM_MULT_TARG) {
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i = 1; i < host->max_id; ++i)
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			scsi_report_device_reset(host, 0, i);
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3964d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Report a driver-initiated bus reset to the SCSI layer.
3974d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm * Calling this for a SCSI-initiated reset is unnecessary but harmless.
398f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern * The caller must not own the SCSI host lock. */
3994d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharmvoid usb_stor_report_bus_reset(struct us_data *us)
4004d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm{
401f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	struct Scsi_Host *host = us_to_host(us);
402f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern
403f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_lock(host);
404f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_report_bus_reset(host, 0);
405f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_unlock(host);
4064d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm}
4074d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * /proc/scsi/ functions
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* we use this macro to help us write into the buffer */
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef SPRINTF
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SPRINTF(args...) \
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int proc_info (struct Scsi_Host *host, char *buffer,
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		char **start, off_t offset, int length, int inout)
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(host);
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *pos = buffer;
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *string;
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if someone is sending us data, just throw it away */
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (inout)
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return length;
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print the controller name */
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("   Host scsi%d: usb-storage\n", host->host_no);
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print product, vendor, and serial number strings */
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->manufacturer)
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->manufacturer;
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->vendorName)
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->vendorName;
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("       Vendor: %s\n", string);
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->product)
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->product;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->productName)
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->productName;
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("      Product: %s\n", string);
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->serial)
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->serial;
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "None";
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("Serial Number: %s\n", string);
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the protocol and transport */
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("     Protocol: %s\n", us->protocol_name);
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("    Transport: %s\n", us->transport_name);
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the device flags */
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (pos < buffer + length) {
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		pos += sprintf(pos, "       Quirks:");
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define US_FLAG(name, value) \
4617e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & value) pos += sprintf(pos, " " #name);
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsUS_DO_ALL_FLAGS
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef US_FLAG
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*(pos++) = '\n';
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Calculate start of next buffer, and return value.
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*start = buffer + offset;
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((pos - buffer) < offset)
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (0);
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if ((pos - buffer - offset) < length)
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (pos - buffer - offset);
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (length);
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs interface
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Output routine for the sysfs max_sectors file */
486060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
49049d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern	return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Input routine for the sysfs max_sectors file */
494060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		size_t count)
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned short ms;
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
50049d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern	if (sscanf(buf, "%hu", &ms) > 0) {
501086fa5ff0854c676ec333760f4c0154b3b242616Martin K. Petersen		blk_queue_max_hw_sectors(sdev->request_queue, ms);
50249d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern		return count;
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EINVAL;
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		store_max_sectors);
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct device_attribute *sysfs_device_attr_list[] = {
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		&dev_attr_max_sectors,
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		NULL,
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		};
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this defines our host template, with which we'll allocate hosts
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_host_template usb_stor_host_template = {
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* basic userland interface stuff */
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name =				"usb-storage",
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_name =			"usb-storage",
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_info =			proc_info,
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.info =				host_info,
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* command interface -- queued only */
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.queuecommand =			queuecommand,
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* error and abort handlers */
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_abort_handler =		command_abort,
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_device_reset_handler =	device_reset,
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_bus_reset_handler =		bus_reset,
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* queue commands only, only one command per LUN */
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.can_queue =			1,
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.cmd_per_lun =			1,
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* unknown initiator id */
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.this_id =			-1,
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_alloc =			slave_alloc,
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_configure =		slave_configure,
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* lots of sg segments can be handled */
54549d6271b85a3e18062eaf4d6f8d899abe00a7725Alan Stern	.sg_tablesize =			SCSI_MAX_SG_CHAIN_SEGMENTS,
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* limit the total size of a transfer to 120 KB */
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.max_sectors =                  240,
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* merge commands... this seems to help performance, but
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * periodically someone should test to see which setting is more
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * optimal.
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.use_clustering =		1,
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* emulated HBA */
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.emulated =			1,
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* we do our own delay after a device or bus reset */
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.skip_settle_delay =		1,
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* sysfs device attributes */
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sdev_attrs =			sysfs_device_attr_list,
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* module management */
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.module =			THIS_MODULE
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* To Report "Illegal Request: Invalid Field in CDB */
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned char usb_stor_sense_invalidCDB[18] = {
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[0]	= 0x70,			    /* current error */
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[7]	= 0x0a,			    /* additional length */
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[12]	= 0x24			    /* Invalid Field in CDB */
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
576e6e244b6cb1f70e7109381626293cd40a8334ed3Alan SternEXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);
577