xref: /openbmc/linux/drivers/usb/storage/transport.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2f0183a33SFelipe Balbi /*
3f0183a33SFelipe Balbi  * Driver for USB Mass Storage compliant devices
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Current development and maintenance by:
61da177e4SLinus Torvalds  *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Developed with the assistance of:
91da177e4SLinus Torvalds  *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
101da177e4SLinus Torvalds  *   (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
111da177e4SLinus Torvalds  *   (c) 2002 Alan Stern <stern@rowland.org>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Initial work by:
141da177e4SLinus Torvalds  *   (c) 1999 Michael Gee (michael@linuxspecific.com)
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * This driver is based on the 'USB Mass Storage Class' document. This
171da177e4SLinus Torvalds  * describes in detail the protocol used to communicate with such
181da177e4SLinus Torvalds  * devices.  Clearly, the designers had SCSI and ATAPI commands in
191da177e4SLinus Torvalds  * mind when they created this document.  The commands are all very
201da177e4SLinus Torvalds  * similar to commands in the SCSI-II and ATAPI specifications.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * It is important to note that in a number of cases this class
231da177e4SLinus Torvalds  * exhibits class-specific exemptions from the USB specification.
241da177e4SLinus Torvalds  * Notably the usage of NAK, STALL and ACK differs from the norm, in
251da177e4SLinus Torvalds  * that they are used to communicate wait, failed and OK on commands.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * Also, for certain devices, the interrupt endpoint is used to convey
281da177e4SLinus Torvalds  * status of a command.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/sched.h>
325a0e3ad6STejun Heo #include <linux/gfp.h>
331da177e4SLinus Torvalds #include <linux/errno.h>
34f940fcd8SPaul Gortmaker #include <linux/export.h>
351da177e4SLinus Torvalds 
365d398779SOliver Neukum #include <linux/usb/quirks.h>
375d398779SOliver Neukum 
381da177e4SLinus Torvalds #include <scsi/scsi.h>
39dff6de73SBoaz Harrosh #include <scsi/scsi_eh.h>
401da177e4SLinus Torvalds #include <scsi/scsi_device.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "usb.h"
431da177e4SLinus Torvalds #include "transport.h"
441da177e4SLinus Torvalds #include "protocol.h"
451da177e4SLinus Torvalds #include "scsiglue.h"
461da177e4SLinus Torvalds #include "debug.h"
471da177e4SLinus Torvalds 
4825ff1c31SAlan Stern #include <linux/blkdev.h>
4925ff1c31SAlan Stern #include "../../scsi/sd.h"
5025ff1c31SAlan Stern 
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /***********************************************************************
531da177e4SLinus Torvalds  * Data transfer routines
541da177e4SLinus Torvalds  ***********************************************************************/
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * This is subtle, so pay attention:
581da177e4SLinus Torvalds  * ---------------------------------
591da177e4SLinus Torvalds  * We're very concerned about races with a command abort.  Hanging this code
601da177e4SLinus Torvalds  * is a sure fire way to hang the kernel.  (Note that this discussion applies
611da177e4SLinus Torvalds  * only to transactions resulting from a scsi queued-command, since only
621da177e4SLinus Torvalds  * these transactions are subject to a scsi abort.  Other transactions, such
631da177e4SLinus Torvalds  * as those occurring during device-specific initialization, must be handled
641da177e4SLinus Torvalds  * by a separate code path.)
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  * The abort function (usb_storage_command_abort() in scsiglue.c) first
677e4d6c38SAlan Stern  * sets the machine state and the ABORTING bit in us->dflags to prevent
681da177e4SLinus Torvalds  * new URBs from being submitted.  It then calls usb_stor_stop_transport()
697e4d6c38SAlan Stern  * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags
701da177e4SLinus Torvalds  * to see if the current_urb needs to be stopped.  Likewise, the SG_ACTIVE
711da177e4SLinus Torvalds  * bit is tested to see if the current_sg scatter-gather request needs to be
721da177e4SLinus Torvalds  * stopped.  The timeout callback routine does much the same thing.
731da177e4SLinus Torvalds  *
747e4d6c38SAlan Stern  * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to
751da177e4SLinus Torvalds  * prevent new URBs from being submitted, and usb_stor_stop_transport() is
761da177e4SLinus Torvalds  * called to stop any ongoing requests.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * The submit function first verifies that the submitting is allowed
791da177e4SLinus Torvalds  * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
801da177e4SLinus Torvalds  * completes without errors, and only then sets the URB_ACTIVE bit.  This
811da177e4SLinus Torvalds  * prevents the stop_transport() function from trying to cancel the URB
821da177e4SLinus Torvalds  * while the submit call is underway.  Next, the submit function must test
831da177e4SLinus Torvalds  * the flags to see if an abort or disconnect occurred during the submission
841da177e4SLinus Torvalds  * or before the URB_ACTIVE bit was set.  If so, it's essential to cancel
851da177e4SLinus Torvalds  * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
861da177e4SLinus Torvalds  * is still set).  Either way, the function must then wait for the URB to
87b375a049SAlan Stern  * finish.  Note that the URB can still be in progress even after a call to
88b375a049SAlan Stern  * usb_unlink_urb() returns.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
911da177e4SLinus Torvalds  * either the stop_transport() function or the submitting function
921da177e4SLinus Torvalds  * is guaranteed to call usb_unlink_urb() for an active URB,
931da177e4SLinus Torvalds  * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
941da177e4SLinus Torvalds  * called more than once or from being called during usb_submit_urb().
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds 
97f0183a33SFelipe Balbi /*
98f0183a33SFelipe Balbi  * This is the completion handler which will wake us up when an URB
991da177e4SLinus Torvalds  * completes.
1001da177e4SLinus Torvalds  */
usb_stor_blocking_completion(struct urb * urb)1017d12e780SDavid Howells static void usb_stor_blocking_completion(struct urb *urb)
1021da177e4SLinus Torvalds {
103cdc97792SMing Lei 	struct completion *urb_done_ptr = urb->context;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	complete(urb_done_ptr);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108f0183a33SFelipe Balbi /*
109f0183a33SFelipe Balbi  * This is the common part of the URB message submission code
1101da177e4SLinus Torvalds  *
1111da177e4SLinus Torvalds  * All URBs from the usb-storage driver involved in handling a queued scsi
1121da177e4SLinus Torvalds  * command _must_ pass through this function (or something like it) for the
1131da177e4SLinus Torvalds  * abort mechanisms to work properly.
1141da177e4SLinus Torvalds  */
usb_stor_msg_common(struct us_data * us,int timeout)1151da177e4SLinus Torvalds static int usb_stor_msg_common(struct us_data *us, int timeout)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct completion urb_done;
1183428cc43SFranck Bui-Huu 	long timeleft;
1191da177e4SLinus Torvalds 	int status;
1201da177e4SLinus Torvalds 
121543f7810SAlan Stern 	/* don't submit URBs during abort processing */
122543f7810SAlan Stern 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
1231da177e4SLinus Torvalds 		return -EIO;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* set up data structures for the wakeup system */
1261da177e4SLinus Torvalds 	init_completion(&urb_done);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	/* fill the common fields in the URB */
1291da177e4SLinus Torvalds 	us->current_urb->context = &urb_done;
130c222fb2eSBob Copeland 	us->current_urb->transfer_flags = 0;
1311da177e4SLinus Torvalds 
132f0183a33SFelipe Balbi 	/*
133f0183a33SFelipe Balbi 	 * we assume that if transfer_buffer isn't us->iobuf then it
1341da177e4SLinus Torvalds 	 * hasn't been mapped for DMA.  Yes, this is clunky, but it's
1351da177e4SLinus Torvalds 	 * easier than always having the caller tell us whether the
136f0183a33SFelipe Balbi 	 * transfer buffer has already been mapped.
137f0183a33SFelipe Balbi 	 */
1381da177e4SLinus Torvalds 	if (us->current_urb->transfer_buffer == us->iobuf)
1391da177e4SLinus Torvalds 		us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1401da177e4SLinus Torvalds 	us->current_urb->transfer_dma = us->iobuf_dma;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	/* submit the URB */
1431da177e4SLinus Torvalds 	status = usb_submit_urb(us->current_urb, GFP_NOIO);
1441da177e4SLinus Torvalds 	if (status) {
1451da177e4SLinus Torvalds 		/* something went wrong */
1461da177e4SLinus Torvalds 		return status;
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds 
149f0183a33SFelipe Balbi 	/*
150f0183a33SFelipe Balbi 	 * since the URB has been submitted successfully, it's now okay
151f0183a33SFelipe Balbi 	 * to cancel it
152f0183a33SFelipe Balbi 	 */
1537e4d6c38SAlan Stern 	set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
1541da177e4SLinus Torvalds 
155543f7810SAlan Stern 	/* did an abort occur during the submission? */
156543f7810SAlan Stern 	if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 		/* cancel the URB, if it hasn't been cancelled already */
1597e4d6c38SAlan Stern 		if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
160191648d0SJoe Perches 			usb_stor_dbg(us, "-- cancelling URB\n");
1611da177e4SLinus Torvalds 			usb_unlink_urb(us->current_urb);
1621da177e4SLinus Torvalds 		}
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	/* wait for the completion of the URB */
1663428cc43SFranck Bui-Huu 	timeleft = wait_for_completion_interruptible_timeout(
1673428cc43SFranck Bui-Huu 			&urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
1683428cc43SFranck Bui-Huu 
1697e4d6c38SAlan Stern 	clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
1701da177e4SLinus Torvalds 
1713428cc43SFranck Bui-Huu 	if (timeleft <= 0) {
172191648d0SJoe Perches 		usb_stor_dbg(us, "%s -- cancelling URB\n",
1733428cc43SFranck Bui-Huu 			     timeleft == 0 ? "Timeout" : "Signal");
174d6b7d3b6SAlan Stern 		usb_kill_urb(us->current_urb);
1753428cc43SFranck Bui-Huu 	}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* return the URB status */
1781da177e4SLinus Torvalds 	return us->current_urb->status;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds  * Transfer one control message, with timeouts, and allowing early
1831da177e4SLinus Torvalds  * termination.  Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
1841da177e4SLinus Torvalds  */
usb_stor_control_msg(struct us_data * us,unsigned int pipe,u8 request,u8 requesttype,u16 value,u16 index,void * data,u16 size,int timeout)1851da177e4SLinus Torvalds int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
1861da177e4SLinus Torvalds 		 u8 request, u8 requesttype, u16 value, u16 index,
1871da177e4SLinus Torvalds 		 void *data, u16 size, int timeout)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	int status;
1901da177e4SLinus Torvalds 
191191648d0SJoe Perches 	usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
192191648d0SJoe Perches 		     request, requesttype, value, index, size);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* fill in the devrequest structure */
1951da177e4SLinus Torvalds 	us->cr->bRequestType = requesttype;
1961da177e4SLinus Torvalds 	us->cr->bRequest = request;
1971da177e4SLinus Torvalds 	us->cr->wValue = cpu_to_le16(value);
1981da177e4SLinus Torvalds 	us->cr->wIndex = cpu_to_le16(index);
1991da177e4SLinus Torvalds 	us->cr->wLength = cpu_to_le16(size);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* fill and submit the URB */
2021da177e4SLinus Torvalds 	usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
2031da177e4SLinus Torvalds 			 (unsigned char*) us->cr, data, size,
2041da177e4SLinus Torvalds 			 usb_stor_blocking_completion, NULL);
2051da177e4SLinus Torvalds 	status = usb_stor_msg_common(us, timeout);
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* return the actual length of the data transferred if no error */
2081da177e4SLinus Torvalds 	if (status == 0)
2091da177e4SLinus Torvalds 		status = us->current_urb->actual_length;
2101da177e4SLinus Torvalds 	return status;
2111da177e4SLinus Torvalds }
212e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_control_msg);
2131da177e4SLinus Torvalds 
214f0183a33SFelipe Balbi /*
215f0183a33SFelipe Balbi  * This is a version of usb_clear_halt() that allows early termination and
2161da177e4SLinus Torvalds  * doesn't read the status from the device -- this is because some devices
2171da177e4SLinus Torvalds  * crash their internal firmware when the status is requested after a halt.
2181da177e4SLinus Torvalds  *
2191da177e4SLinus Torvalds  * A definitive list of these 'bad' devices is too difficult to maintain or
2201da177e4SLinus Torvalds  * make complete enough to be useful.  This problem was first observed on the
2211da177e4SLinus Torvalds  * Hagiwara FlashGate DUAL unit.  However, bus traces reveal that neither
2221da177e4SLinus Torvalds  * MacOS nor Windows checks the status after clearing a halt.
2231da177e4SLinus Torvalds  *
2241da177e4SLinus Torvalds  * Since many vendors in this space limit their testing to interoperability
2251da177e4SLinus Torvalds  * with these two OSes, specification violations like this one are common.
2261da177e4SLinus Torvalds  */
usb_stor_clear_halt(struct us_data * us,unsigned int pipe)2271da177e4SLinus Torvalds int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	int result;
2301da177e4SLinus Torvalds 	int endp = usb_pipeendpoint(pipe);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	if (usb_pipein (pipe))
2331da177e4SLinus Torvalds 		endp |= USB_DIR_IN;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	result = usb_stor_control_msg(us, us->send_ctrl_pipe,
2361da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
2371da177e4SLinus Torvalds 		USB_ENDPOINT_HALT, endp,
2381da177e4SLinus Torvalds 		NULL, 0, 3*HZ);
2391da177e4SLinus Torvalds 
2405203ad44SMatthew Dharm 	if (result >= 0)
2413444b26aSDavid Vrabel 		usb_reset_endpoint(us->pusb_dev, endp);
2421da177e4SLinus Torvalds 
243191648d0SJoe Perches 	usb_stor_dbg(us, "result = %d\n", result);
2441da177e4SLinus Torvalds 	return result;
2451da177e4SLinus Torvalds }
246e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_clear_halt);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /*
2501da177e4SLinus Torvalds  * Interpret the results of a URB transfer
2511da177e4SLinus Torvalds  *
2521da177e4SLinus Torvalds  * This function prints appropriate debugging messages, clears halts on
2531da177e4SLinus Torvalds  * non-control endpoints, and translates the status to the corresponding
2541da177e4SLinus Torvalds  * USB_STOR_XFER_xxx return code.
2551da177e4SLinus Torvalds  */
interpret_urb_result(struct us_data * us,unsigned int pipe,unsigned int length,int result,unsigned int partial)2561da177e4SLinus Torvalds static int interpret_urb_result(struct us_data *us, unsigned int pipe,
2571da177e4SLinus Torvalds 		unsigned int length, int result, unsigned int partial)
2581da177e4SLinus Torvalds {
259191648d0SJoe Perches 	usb_stor_dbg(us, "Status code %d; transferred %u/%u\n",
2601da177e4SLinus Torvalds 		     result, partial, length);
2611da177e4SLinus Torvalds 	switch (result) {
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* no error code; did we send all the data? */
2641da177e4SLinus Torvalds 	case 0:
2651da177e4SLinus Torvalds 		if (partial != length) {
266191648d0SJoe Perches 			usb_stor_dbg(us, "-- short transfer\n");
2671da177e4SLinus Torvalds 			return USB_STOR_XFER_SHORT;
2681da177e4SLinus Torvalds 		}
2691da177e4SLinus Torvalds 
270191648d0SJoe Perches 		usb_stor_dbg(us, "-- transfer complete\n");
2711da177e4SLinus Torvalds 		return USB_STOR_XFER_GOOD;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* stalled */
2741da177e4SLinus Torvalds 	case -EPIPE:
275f0183a33SFelipe Balbi 		/*
276f0183a33SFelipe Balbi 		 * for control endpoints, (used by CB[I]) a stall indicates
277f0183a33SFelipe Balbi 		 * a failed command
278f0183a33SFelipe Balbi 		 */
2791da177e4SLinus Torvalds 		if (usb_pipecontrol(pipe)) {
280191648d0SJoe Perches 			usb_stor_dbg(us, "-- stall on control pipe\n");
2811da177e4SLinus Torvalds 			return USB_STOR_XFER_STALLED;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		/* for other sorts of endpoint, clear the stall */
285191648d0SJoe Perches 		usb_stor_dbg(us, "clearing endpoint halt for pipe 0x%x\n",
286191648d0SJoe Perches 			     pipe);
2871da177e4SLinus Torvalds 		if (usb_stor_clear_halt(us, pipe) < 0)
2881da177e4SLinus Torvalds 			return USB_STOR_XFER_ERROR;
2891da177e4SLinus Torvalds 		return USB_STOR_XFER_STALLED;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* babble - the device tried to send more than we wanted to read */
2921da177e4SLinus Torvalds 	case -EOVERFLOW:
293191648d0SJoe Perches 		usb_stor_dbg(us, "-- babble\n");
2941da177e4SLinus Torvalds 		return USB_STOR_XFER_LONG;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* the transfer was cancelled by abort, disconnect, or timeout */
2971da177e4SLinus Torvalds 	case -ECONNRESET:
298191648d0SJoe Perches 		usb_stor_dbg(us, "-- transfer cancelled\n");
2991da177e4SLinus Torvalds 		return USB_STOR_XFER_ERROR;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/* short scatter-gather read transfer */
3021da177e4SLinus Torvalds 	case -EREMOTEIO:
303191648d0SJoe Perches 		usb_stor_dbg(us, "-- short read transfer\n");
3041da177e4SLinus Torvalds 		return USB_STOR_XFER_SHORT;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* abort or disconnect in progress */
3071da177e4SLinus Torvalds 	case -EIO:
308191648d0SJoe Perches 		usb_stor_dbg(us, "-- abort or disconnect in progress\n");
3091da177e4SLinus Torvalds 		return USB_STOR_XFER_ERROR;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* the catch-all error case */
3121da177e4SLinus Torvalds 	default:
313191648d0SJoe Perches 		usb_stor_dbg(us, "-- unknown error\n");
3141da177e4SLinus Torvalds 		return USB_STOR_XFER_ERROR;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  * Transfer one control message, without timeouts, but allowing early
3201da177e4SLinus Torvalds  * termination.  Return codes are USB_STOR_XFER_xxx.
3211da177e4SLinus Torvalds  */
usb_stor_ctrl_transfer(struct us_data * us,unsigned int pipe,u8 request,u8 requesttype,u16 value,u16 index,void * data,u16 size)3221da177e4SLinus Torvalds int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
3231da177e4SLinus Torvalds 		u8 request, u8 requesttype, u16 value, u16 index,
3241da177e4SLinus Torvalds 		void *data, u16 size)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	int result;
3271da177e4SLinus Torvalds 
328191648d0SJoe Perches 	usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
329191648d0SJoe Perches 		     request, requesttype, value, index, size);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* fill in the devrequest structure */
3321da177e4SLinus Torvalds 	us->cr->bRequestType = requesttype;
3331da177e4SLinus Torvalds 	us->cr->bRequest = request;
3341da177e4SLinus Torvalds 	us->cr->wValue = cpu_to_le16(value);
3351da177e4SLinus Torvalds 	us->cr->wIndex = cpu_to_le16(index);
3361da177e4SLinus Torvalds 	us->cr->wLength = cpu_to_le16(size);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	/* fill and submit the URB */
3391da177e4SLinus Torvalds 	usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
3401da177e4SLinus Torvalds 			 (unsigned char*) us->cr, data, size,
3411da177e4SLinus Torvalds 			 usb_stor_blocking_completion, NULL);
3421da177e4SLinus Torvalds 	result = usb_stor_msg_common(us, 0);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	return interpret_urb_result(us, pipe, size, result,
3451da177e4SLinus Torvalds 			us->current_urb->actual_length);
3461da177e4SLinus Torvalds }
347e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_ctrl_transfer);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /*
3501da177e4SLinus Torvalds  * Receive one interrupt buffer, without timeouts, but allowing early
3511da177e4SLinus Torvalds  * termination.  Return codes are USB_STOR_XFER_xxx.
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  * This routine always uses us->recv_intr_pipe as the pipe and
3541da177e4SLinus Torvalds  * us->ep_bInterval as the interrupt interval.
3551da177e4SLinus Torvalds  */
usb_stor_intr_transfer(struct us_data * us,void * buf,unsigned int length)3561da177e4SLinus Torvalds static int usb_stor_intr_transfer(struct us_data *us, void *buf,
3571da177e4SLinus Torvalds 				  unsigned int length)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	int result;
3601da177e4SLinus Torvalds 	unsigned int pipe = us->recv_intr_pipe;
3611da177e4SLinus Torvalds 	unsigned int maxp;
3621da177e4SLinus Torvalds 
363191648d0SJoe Perches 	usb_stor_dbg(us, "xfer %u bytes\n", length);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* calculate the max packet size */
366dcd2e49bSVincent Mailhol 	maxp = usb_maxpacket(us->pusb_dev, pipe);
3671da177e4SLinus Torvalds 	if (maxp > length)
3681da177e4SLinus Torvalds 		maxp = length;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* fill and submit the URB */
3711da177e4SLinus Torvalds 	usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
3721da177e4SLinus Torvalds 			maxp, usb_stor_blocking_completion, NULL,
3731da177e4SLinus Torvalds 			us->ep_bInterval);
3741da177e4SLinus Torvalds 	result = usb_stor_msg_common(us, 0);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	return interpret_urb_result(us, pipe, length, result,
3771da177e4SLinus Torvalds 			us->current_urb->actual_length);
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /*
3811da177e4SLinus Torvalds  * Transfer one buffer via bulk pipe, without timeouts, but allowing early
3821da177e4SLinus Torvalds  * termination.  Return codes are USB_STOR_XFER_xxx.  If the bulk pipe
3831da177e4SLinus Torvalds  * stalls during the transfer, the halt is automatically cleared.
3841da177e4SLinus Torvalds  */
usb_stor_bulk_transfer_buf(struct us_data * us,unsigned int pipe,void * buf,unsigned int length,unsigned int * act_len)3851da177e4SLinus Torvalds int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
3861da177e4SLinus Torvalds 	void *buf, unsigned int length, unsigned int *act_len)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	int result;
3891da177e4SLinus Torvalds 
390191648d0SJoe Perches 	usb_stor_dbg(us, "xfer %u bytes\n", length);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* fill and submit the URB */
3931da177e4SLinus Torvalds 	usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
3941da177e4SLinus Torvalds 		      usb_stor_blocking_completion, NULL);
3951da177e4SLinus Torvalds 	result = usb_stor_msg_common(us, 0);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/* store the actual length of the data transferred */
3981da177e4SLinus Torvalds 	if (act_len)
3991da177e4SLinus Torvalds 		*act_len = us->current_urb->actual_length;
4001da177e4SLinus Torvalds 	return interpret_urb_result(us, pipe, length, result,
4011da177e4SLinus Torvalds 			us->current_urb->actual_length);
4021da177e4SLinus Torvalds }
403e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_buf);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*
4061da177e4SLinus Torvalds  * Transfer a scatter-gather list via bulk transfer
4071da177e4SLinus Torvalds  *
4081da177e4SLinus Torvalds  * This function does basically the same thing as usb_stor_bulk_transfer_buf()
4091da177e4SLinus Torvalds  * above, but it uses the usbcore scatter-gather library.
4101da177e4SLinus Torvalds  */
usb_stor_bulk_transfer_sglist(struct us_data * us,unsigned int pipe,struct scatterlist * sg,int num_sg,unsigned int length,unsigned int * act_len)4111da177e4SLinus Torvalds static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
4121da177e4SLinus Torvalds 		struct scatterlist *sg, int num_sg, unsigned int length,
4131da177e4SLinus Torvalds 		unsigned int *act_len)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	int result;
4161da177e4SLinus Torvalds 
417543f7810SAlan Stern 	/* don't submit s-g requests during abort processing */
418543f7810SAlan Stern 	if (test_bit(US_FLIDX_ABORTING, &us->dflags))
4196a6516c0SLukas Bulwahn 		goto usb_stor_xfer_error;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	/* initialize the scatter-gather request block */
422191648d0SJoe Perches 	usb_stor_dbg(us, "xfer %u bytes, %d entries\n", length, num_sg);
4231da177e4SLinus Torvalds 	result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
42455acbda0SChristoph Lameter 			sg, num_sg, length, GFP_NOIO);
4251da177e4SLinus Torvalds 	if (result) {
426191648d0SJoe Perches 		usb_stor_dbg(us, "usb_sg_init returned %d\n", result);
4276a6516c0SLukas Bulwahn 		goto usb_stor_xfer_error;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
430f0183a33SFelipe Balbi 	/*
431f0183a33SFelipe Balbi 	 * since the block has been initialized successfully, it's now
432f0183a33SFelipe Balbi 	 * okay to cancel it
433f0183a33SFelipe Balbi 	 */
4347e4d6c38SAlan Stern 	set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
4351da177e4SLinus Torvalds 
436543f7810SAlan Stern 	/* did an abort occur during the submission? */
437543f7810SAlan Stern 	if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 		/* cancel the request, if it hasn't been cancelled already */
4407e4d6c38SAlan Stern 		if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
441191648d0SJoe Perches 			usb_stor_dbg(us, "-- cancelling sg request\n");
4421da177e4SLinus Torvalds 			usb_sg_cancel(&us->current_sg);
4431da177e4SLinus Torvalds 		}
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/* wait for the completion of the transfer */
4471da177e4SLinus Torvalds 	usb_sg_wait(&us->current_sg);
4487e4d6c38SAlan Stern 	clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	result = us->current_sg.status;
4511da177e4SLinus Torvalds 	if (act_len)
4521da177e4SLinus Torvalds 		*act_len = us->current_sg.bytes;
4531da177e4SLinus Torvalds 	return interpret_urb_result(us, pipe, length, result,
4541da177e4SLinus Torvalds 			us->current_sg.bytes);
4556a6516c0SLukas Bulwahn 
4566a6516c0SLukas Bulwahn usb_stor_xfer_error:
4576a6516c0SLukas Bulwahn 	if (act_len)
4586a6516c0SLukas Bulwahn 		*act_len = 0;
4596a6516c0SLukas Bulwahn 	return USB_STOR_XFER_ERROR;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /*
4636d416e61SBoaz Harrosh  * Common used function. Transfer a complete command
4646d416e61SBoaz Harrosh  * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
4656d416e61SBoaz Harrosh  */
usb_stor_bulk_srb(struct us_data * us,unsigned int pipe,struct scsi_cmnd * srb)4666d416e61SBoaz Harrosh int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
4676d416e61SBoaz Harrosh 		      struct scsi_cmnd* srb)
4686d416e61SBoaz Harrosh {
4696d416e61SBoaz Harrosh 	unsigned int partial;
4706d416e61SBoaz Harrosh 	int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
4716d416e61SBoaz Harrosh 				      scsi_sg_count(srb), scsi_bufflen(srb),
4726d416e61SBoaz Harrosh 				      &partial);
4736d416e61SBoaz Harrosh 
4746d416e61SBoaz Harrosh 	scsi_set_resid(srb, scsi_bufflen(srb) - partial);
4756d416e61SBoaz Harrosh 	return result;
4766d416e61SBoaz Harrosh }
477e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_bulk_srb);
4786d416e61SBoaz Harrosh 
4796d416e61SBoaz Harrosh /*
4801da177e4SLinus Torvalds  * Transfer an entire SCSI command's worth of data payload over the bulk
4811da177e4SLinus Torvalds  * pipe.
4821da177e4SLinus Torvalds  *
4831da177e4SLinus Torvalds  * Note that this uses usb_stor_bulk_transfer_buf() and
4841da177e4SLinus Torvalds  * usb_stor_bulk_transfer_sglist() to achieve its goals --
4851da177e4SLinus Torvalds  * this function simply determines whether we're going to use
4861da177e4SLinus Torvalds  * scatter-gather or not, and acts appropriately.
4871da177e4SLinus Torvalds  */
usb_stor_bulk_transfer_sg(struct us_data * us,unsigned int pipe,void * buf,unsigned int length_left,int use_sg,int * residual)4881da177e4SLinus Torvalds int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
4891da177e4SLinus Torvalds 		void *buf, unsigned int length_left, int use_sg, int *residual)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	int result;
4921da177e4SLinus Torvalds 	unsigned int partial;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	/* are we scatter-gathering? */
4951da177e4SLinus Torvalds 	if (use_sg) {
4961da177e4SLinus Torvalds 		/* use the usb core scatter-gather primitives */
4971da177e4SLinus Torvalds 		result = usb_stor_bulk_transfer_sglist(us, pipe,
4981da177e4SLinus Torvalds 				(struct scatterlist *) buf, use_sg,
4991da177e4SLinus Torvalds 				length_left, &partial);
5001da177e4SLinus Torvalds 		length_left -= partial;
5011da177e4SLinus Torvalds 	} else {
5021da177e4SLinus Torvalds 		/* no scatter-gather, just make the request */
5031da177e4SLinus Torvalds 		result = usb_stor_bulk_transfer_buf(us, pipe, buf,
5041da177e4SLinus Torvalds 				length_left, &partial);
5051da177e4SLinus Torvalds 		length_left -= partial;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	/* store the residual and return the error code */
5091da177e4SLinus Torvalds 	if (residual)
5101da177e4SLinus Torvalds 		*residual = length_left;
5111da177e4SLinus Torvalds 	return result;
5121da177e4SLinus Torvalds }
513e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_sg);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /***********************************************************************
5161da177e4SLinus Torvalds  * Transport routines
5171da177e4SLinus Torvalds  ***********************************************************************/
5181da177e4SLinus Torvalds 
519f0183a33SFelipe Balbi /*
520f0183a33SFelipe Balbi  * There are so many devices that report the capacity incorrectly,
52125ff1c31SAlan Stern  * this routine was written to counteract some of the resulting
52225ff1c31SAlan Stern  * problems.
52325ff1c31SAlan Stern  */
last_sector_hacks(struct us_data * us,struct scsi_cmnd * srb)52425ff1c31SAlan Stern static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)
52525ff1c31SAlan Stern {
52625ff1c31SAlan Stern 	struct gendisk *disk;
52725ff1c31SAlan Stern 	struct scsi_disk *sdkp;
52825ff1c31SAlan Stern 	u32 sector;
52925ff1c31SAlan Stern 
53025ff1c31SAlan Stern 	/* To Report "Medium Error: Record Not Found */
53125ff1c31SAlan Stern 	static unsigned char record_not_found[18] = {
53225ff1c31SAlan Stern 		[0]	= 0x70,			/* current error */
53325ff1c31SAlan Stern 		[2]	= MEDIUM_ERROR,		/* = 0x03 */
53425ff1c31SAlan Stern 		[7]	= 0x0a,			/* additional length */
53525ff1c31SAlan Stern 		[12]	= 0x14			/* Record Not Found */
53625ff1c31SAlan Stern 	};
53725ff1c31SAlan Stern 
538f0183a33SFelipe Balbi 	/*
539f0183a33SFelipe Balbi 	 * If last-sector problems can't occur, whether because the
54025ff1c31SAlan Stern 	 * capacity was already decremented or because the device is
54125ff1c31SAlan Stern 	 * known to report the correct capacity, then we don't need
54225ff1c31SAlan Stern 	 * to do anything.
54325ff1c31SAlan Stern 	 */
54425ff1c31SAlan Stern 	if (!us->use_last_sector_hacks)
54525ff1c31SAlan Stern 		return;
54625ff1c31SAlan Stern 
54725ff1c31SAlan Stern 	/* Was this command a READ(10) or a WRITE(10)? */
54825ff1c31SAlan Stern 	if (srb->cmnd[0] != READ_10 && srb->cmnd[0] != WRITE_10)
54925ff1c31SAlan Stern 		goto done;
55025ff1c31SAlan Stern 
55125ff1c31SAlan Stern 	/* Did this command access the last sector? */
55225ff1c31SAlan Stern 	sector = (srb->cmnd[2] << 24) | (srb->cmnd[3] << 16) |
55325ff1c31SAlan Stern 			(srb->cmnd[4] << 8) | (srb->cmnd[5]);
554f3fa33acSChristoph Hellwig 	disk = scsi_cmd_to_rq(srb)->q->disk;
55525ff1c31SAlan Stern 	if (!disk)
55625ff1c31SAlan Stern 		goto done;
55725ff1c31SAlan Stern 	sdkp = scsi_disk(disk);
55825ff1c31SAlan Stern 	if (!sdkp)
55925ff1c31SAlan Stern 		goto done;
56025ff1c31SAlan Stern 	if (sector + 1 != sdkp->capacity)
56125ff1c31SAlan Stern 		goto done;
56225ff1c31SAlan Stern 
56325ff1c31SAlan Stern 	if (srb->result == SAM_STAT_GOOD && scsi_get_resid(srb) == 0) {
56425ff1c31SAlan Stern 
565f0183a33SFelipe Balbi 		/*
566f0183a33SFelipe Balbi 		 * The command succeeded.  We know this device doesn't
5670d020aaeSAlan Stern 		 * have the last-sector bug, so stop checking it.
56825ff1c31SAlan Stern 		 */
5690d020aaeSAlan Stern 		us->use_last_sector_hacks = 0;
57025ff1c31SAlan Stern 
57125ff1c31SAlan Stern 	} else {
572f0183a33SFelipe Balbi 		/*
573f0183a33SFelipe Balbi 		 * The command failed.  Allow up to 3 retries in case this
57425ff1c31SAlan Stern 		 * is some normal sort of failure.  After that, assume the
57525ff1c31SAlan Stern 		 * capacity is wrong and we're trying to access the sector
57625ff1c31SAlan Stern 		 * beyond the end.  Replace the result code and sense data
57725ff1c31SAlan Stern 		 * with values that will cause the SCSI core to fail the
57825ff1c31SAlan Stern 		 * command immediately, instead of going into an infinite
57925ff1c31SAlan Stern 		 * (or even just a very long) retry loop.
58025ff1c31SAlan Stern 		 */
58125ff1c31SAlan Stern 		if (++us->last_sector_retries < 3)
58225ff1c31SAlan Stern 			return;
58325ff1c31SAlan Stern 		srb->result = SAM_STAT_CHECK_CONDITION;
58425ff1c31SAlan Stern 		memcpy(srb->sense_buffer, record_not_found,
58525ff1c31SAlan Stern 				sizeof(record_not_found));
58625ff1c31SAlan Stern 	}
58725ff1c31SAlan Stern 
58825ff1c31SAlan Stern  done:
589f0183a33SFelipe Balbi 	/*
590f0183a33SFelipe Balbi 	 * Don't reset the retry counter for TEST UNIT READY commands,
59125ff1c31SAlan Stern 	 * because they get issued after device resets which might be
59225ff1c31SAlan Stern 	 * caused by a failed last-sector access.
59325ff1c31SAlan Stern 	 */
59425ff1c31SAlan Stern 	if (srb->cmnd[0] != TEST_UNIT_READY)
59525ff1c31SAlan Stern 		us->last_sector_retries = 0;
59625ff1c31SAlan Stern }
59725ff1c31SAlan Stern 
598f0183a33SFelipe Balbi /*
599f0183a33SFelipe Balbi  * Invoke the transport and basic error-handling/recovery methods
6001da177e4SLinus Torvalds  *
6011da177e4SLinus Torvalds  * This is used by the protocol layers to actually send the message to
6021da177e4SLinus Torvalds  * the device and receive the response.
6031da177e4SLinus Torvalds  */
usb_stor_invoke_transport(struct scsi_cmnd * srb,struct us_data * us)6041da177e4SLinus Torvalds void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds 	int need_auto_sense;
6071da177e4SLinus Torvalds 	int result;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	/* send the command to the transport layer */
6106d416e61SBoaz Harrosh 	scsi_set_resid(srb, 0);
6111da177e4SLinus Torvalds 	result = us->transport(srb, us);
6121da177e4SLinus Torvalds 
613f0183a33SFelipe Balbi 	/*
614f0183a33SFelipe Balbi 	 * if the command gets aborted by the higher layers, we need to
6151da177e4SLinus Torvalds 	 * short-circuit all other processing
6161da177e4SLinus Torvalds 	 */
6177e4d6c38SAlan Stern 	if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
618191648d0SJoe Perches 		usb_stor_dbg(us, "-- command was aborted\n");
6194d07ef76SMatthew Dharm 		srb->result = DID_ABORT << 16;
6204d07ef76SMatthew Dharm 		goto Handle_Errors;
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/* if there is a transport error, reset and don't auto-sense */
6241da177e4SLinus Torvalds 	if (result == USB_STOR_TRANSPORT_ERROR) {
625191648d0SJoe Perches 		usb_stor_dbg(us, "-- transport indicates error, resetting\n");
6261da177e4SLinus Torvalds 		srb->result = DID_ERROR << 16;
6274d07ef76SMatthew Dharm 		goto Handle_Errors;
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	/* if the transport provided its own sense data, don't auto-sense */
6311da177e4SLinus Torvalds 	if (result == USB_STOR_TRANSPORT_NO_SENSE) {
6321da177e4SLinus Torvalds 		srb->result = SAM_STAT_CHECK_CONDITION;
63325ff1c31SAlan Stern 		last_sector_hacks(us, srb);
6341da177e4SLinus Torvalds 		return;
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	srb->result = SAM_STAT_GOOD;
6381da177e4SLinus Torvalds 
639f0183a33SFelipe Balbi 	/*
640f0183a33SFelipe Balbi 	 * Determine if we need to auto-sense
6411da177e4SLinus Torvalds 	 *
6421da177e4SLinus Torvalds 	 * I normally don't use a flag like this, but it's almost impossible
6431da177e4SLinus Torvalds 	 * to understand what's going on here if I don't.
6441da177e4SLinus Torvalds 	 */
6451da177e4SLinus Torvalds 	need_auto_sense = 0;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	/*
6481da177e4SLinus Torvalds 	 * If we're running the CB transport, which is incapable
6491da177e4SLinus Torvalds 	 * of determining status on its own, we will auto-sense
6501da177e4SLinus Torvalds 	 * unless the operation involved a data-in transfer.  Devices
6511da177e4SLinus Torvalds 	 * can signal most data-in errors by stalling the bulk-in pipe.
6521da177e4SLinus Torvalds 	 */
6538fa7fd74SMichal Nazarewicz 	if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
6541da177e4SLinus Torvalds 			srb->sc_data_direction != DMA_FROM_DEVICE) {
655191648d0SJoe Perches 		usb_stor_dbg(us, "-- CB transport device requiring auto-sense\n");
6561da177e4SLinus Torvalds 		need_auto_sense = 1;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
659546aa0e4SAlan Stern 	/* Some devices (Kindle) require another command after SYNC CACHE */
660546aa0e4SAlan Stern 	if ((us->fflags & US_FL_SENSE_AFTER_SYNC) &&
661546aa0e4SAlan Stern 			srb->cmnd[0] == SYNCHRONIZE_CACHE) {
662546aa0e4SAlan Stern 		usb_stor_dbg(us, "-- sense after SYNC CACHE\n");
663546aa0e4SAlan Stern 		need_auto_sense = 1;
664546aa0e4SAlan Stern 	}
665546aa0e4SAlan Stern 
6661da177e4SLinus Torvalds 	/*
6671da177e4SLinus Torvalds 	 * If we have a failure, we're going to do a REQUEST_SENSE
6681da177e4SLinus Torvalds 	 * automatically.  Note that we differentiate between a command
6691da177e4SLinus Torvalds 	 * "failure" and an "error" in the transport mechanism.
6701da177e4SLinus Torvalds 	 */
6711da177e4SLinus Torvalds 	if (result == USB_STOR_TRANSPORT_FAILED) {
672191648d0SJoe Perches 		usb_stor_dbg(us, "-- transport indicates command failure\n");
6731da177e4SLinus Torvalds 		need_auto_sense = 1;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	/*
6771537e0adSBen Efros 	 * Determine if this device is SAT by seeing if the
6781537e0adSBen Efros 	 * command executed successfully.  Otherwise we'll have
6791537e0adSBen Efros 	 * to wait for at least one CHECK_CONDITION to determine
6801537e0adSBen Efros 	 * SANE_SENSE support
6811537e0adSBen Efros 	 */
682a0bb1081SAlan Stern 	if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
6831537e0adSBen Efros 	    result == USB_STOR_TRANSPORT_GOOD &&
6841537e0adSBen Efros 	    !(us->fflags & US_FL_SANE_SENSE) &&
685a0bb1081SAlan Stern 	    !(us->fflags & US_FL_BAD_SENSE) &&
686a0bb1081SAlan Stern 	    !(srb->cmnd[2] & 0x20))) {
687191648d0SJoe Perches 		usb_stor_dbg(us, "-- SAT supported, increasing auto-sense\n");
6881537e0adSBen Efros 		us->fflags |= US_FL_SANE_SENSE;
6891537e0adSBen Efros 	}
6901537e0adSBen Efros 
6911537e0adSBen Efros 	/*
6921da177e4SLinus Torvalds 	 * A short transfer on a command where we don't expect it
6931da177e4SLinus Torvalds 	 * is unusual, but it doesn't mean we need to auto-sense.
6941da177e4SLinus Torvalds 	 */
6956d416e61SBoaz Harrosh 	if ((scsi_get_resid(srb) > 0) &&
6961da177e4SLinus Torvalds 	    !((srb->cmnd[0] == REQUEST_SENSE) ||
6971da177e4SLinus Torvalds 	      (srb->cmnd[0] == INQUIRY) ||
6981da177e4SLinus Torvalds 	      (srb->cmnd[0] == MODE_SENSE) ||
6991da177e4SLinus Torvalds 	      (srb->cmnd[0] == LOG_SENSE) ||
7001da177e4SLinus Torvalds 	      (srb->cmnd[0] == MODE_SENSE_10))) {
701191648d0SJoe Perches 		usb_stor_dbg(us, "-- unexpectedly short transfer\n");
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/* Now, if we need to do the auto-sense, let's do it */
7051da177e4SLinus Torvalds 	if (need_auto_sense) {
7061da177e4SLinus Torvalds 		int temp_result;
707dff6de73SBoaz Harrosh 		struct scsi_eh_save ses;
7081537e0adSBen Efros 		int sense_size = US_SENSE_SIZE;
709e16da02fSLuben Tuikov 		struct scsi_sense_hdr sshdr;
710e16da02fSLuben Tuikov 		const u8 *scdd;
711e16da02fSLuben Tuikov 		u8 fm_ili;
7121537e0adSBen Efros 
7131537e0adSBen Efros 		/* device supports and needs bigger sense buffer */
7141537e0adSBen Efros 		if (us->fflags & US_FL_SANE_SENSE)
7151537e0adSBen Efros 			sense_size = ~0;
716b8430e1bSBenjamin Herrenschmidt Retry_Sense:
717191648d0SJoe Perches 		usb_stor_dbg(us, "Issuing auto-REQUEST_SENSE\n");
7181da177e4SLinus Torvalds 
7191537e0adSBen Efros 		scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		/* FIXME: we must do the protocol translation here */
7228fa7fd74SMichal Nazarewicz 		if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
7238fa7fd74SMichal Nazarewicz 				us->subclass == USB_SC_CYP_ATACB)
7241da177e4SLinus Torvalds 			srb->cmd_len = 6;
7251da177e4SLinus Torvalds 		else
7261da177e4SLinus Torvalds 			srb->cmd_len = 12;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 		/* issue the auto-sense command */
7296d416e61SBoaz Harrosh 		scsi_set_resid(srb, 0);
7301da177e4SLinus Torvalds 		temp_result = us->transport(us->srb, us);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 		/* let's clean up right away */
733dff6de73SBoaz Harrosh 		scsi_eh_restore_cmnd(srb, &ses);
7341da177e4SLinus Torvalds 
7357e4d6c38SAlan Stern 		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
736191648d0SJoe Perches 			usb_stor_dbg(us, "-- auto-sense aborted\n");
7374d07ef76SMatthew Dharm 			srb->result = DID_ABORT << 16;
738a0bb1081SAlan Stern 
739a0bb1081SAlan Stern 			/* If SANE_SENSE caused this problem, disable it */
740a0bb1081SAlan Stern 			if (sense_size != US_SENSE_SIZE) {
741a0bb1081SAlan Stern 				us->fflags &= ~US_FL_SANE_SENSE;
742a0bb1081SAlan Stern 				us->fflags |= US_FL_BAD_SENSE;
743a0bb1081SAlan Stern 			}
7444d07ef76SMatthew Dharm 			goto Handle_Errors;
7451da177e4SLinus Torvalds 		}
746b8430e1bSBenjamin Herrenschmidt 
747f0183a33SFelipe Balbi 		/*
748f0183a33SFelipe Balbi 		 * Some devices claim to support larger sense but fail when
749b8430e1bSBenjamin Herrenschmidt 		 * trying to request it. When a transport failure happens
750b8430e1bSBenjamin Herrenschmidt 		 * using US_FS_SANE_SENSE, we always retry with a standard
751b8430e1bSBenjamin Herrenschmidt 		 * (small) sense request. This fixes some USB GSM modems
752b8430e1bSBenjamin Herrenschmidt 		 */
753b8430e1bSBenjamin Herrenschmidt 		if (temp_result == USB_STOR_TRANSPORT_FAILED &&
754b8430e1bSBenjamin Herrenschmidt 				sense_size != US_SENSE_SIZE) {
755191648d0SJoe Perches 			usb_stor_dbg(us, "-- auto-sense failure, retry small sense\n");
756b8430e1bSBenjamin Herrenschmidt 			sense_size = US_SENSE_SIZE;
757a0bb1081SAlan Stern 			us->fflags &= ~US_FL_SANE_SENSE;
758a0bb1081SAlan Stern 			us->fflags |= US_FL_BAD_SENSE;
759b8430e1bSBenjamin Herrenschmidt 			goto Retry_Sense;
760b8430e1bSBenjamin Herrenschmidt 		}
761b8430e1bSBenjamin Herrenschmidt 
762b8430e1bSBenjamin Herrenschmidt 		/* Other failures */
7631da177e4SLinus Torvalds 		if (temp_result != USB_STOR_TRANSPORT_GOOD) {
764191648d0SJoe Perches 			usb_stor_dbg(us, "-- auto-sense failure\n");
7651da177e4SLinus Torvalds 
766f0183a33SFelipe Balbi 			/*
767f0183a33SFelipe Balbi 			 * we skip the reset if this happens to be a
7681da177e4SLinus Torvalds 			 * multi-target device, since failure of an
7691da177e4SLinus Torvalds 			 * auto-sense is perfectly valid
7701da177e4SLinus Torvalds 			 */
7711da177e4SLinus Torvalds 			srb->result = DID_ERROR << 16;
7727e4d6c38SAlan Stern 			if (!(us->fflags & US_FL_SCM_MULT_TARG))
7734d07ef76SMatthew Dharm 				goto Handle_Errors;
7741da177e4SLinus Torvalds 			return;
7751da177e4SLinus Torvalds 		}
7761da177e4SLinus Torvalds 
777f0183a33SFelipe Balbi 		/*
778f0183a33SFelipe Balbi 		 * If the sense data returned is larger than 18-bytes then we
7791537e0adSBen Efros 		 * assume this device supports requesting more in the future.
7801537e0adSBen Efros 		 * The response code must be 70h through 73h inclusive.
7811537e0adSBen Efros 		 */
7821537e0adSBen Efros 		if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
7831537e0adSBen Efros 		    !(us->fflags & US_FL_SANE_SENSE) &&
784a0bb1081SAlan Stern 		    !(us->fflags & US_FL_BAD_SENSE) &&
7851537e0adSBen Efros 		    (srb->sense_buffer[0] & 0x7C) == 0x70) {
786191648d0SJoe Perches 			usb_stor_dbg(us, "-- SANE_SENSE support enabled\n");
7871537e0adSBen Efros 			us->fflags |= US_FL_SANE_SENSE;
7881537e0adSBen Efros 
789f0183a33SFelipe Balbi 			/*
790f0183a33SFelipe Balbi 			 * Indicate to the user that we truncated their sense
7911537e0adSBen Efros 			 * because we didn't know it supported larger sense.
7921537e0adSBen Efros 			 */
793191648d0SJoe Perches 			usb_stor_dbg(us, "-- Sense data truncated to %i from %i\n",
7941537e0adSBen Efros 				     US_SENSE_SIZE,
7951537e0adSBen Efros 				     srb->sense_buffer[7] + 8);
7961537e0adSBen Efros 			srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
7971537e0adSBen Efros 		}
7981537e0adSBen Efros 
799e16da02fSLuben Tuikov 		scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
800e16da02fSLuben Tuikov 				     &sshdr);
801e16da02fSLuben Tuikov 
802191648d0SJoe Perches 		usb_stor_dbg(us, "-- Result from auto-sense is %d\n",
803191648d0SJoe Perches 			     temp_result);
804191648d0SJoe Perches 		usb_stor_dbg(us, "-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
805e16da02fSLuben Tuikov 			     sshdr.response_code, sshdr.sense_key,
806e16da02fSLuben Tuikov 			     sshdr.asc, sshdr.ascq);
8071da177e4SLinus Torvalds #ifdef CONFIG_USB_STORAGE_DEBUG
808191648d0SJoe Perches 		usb_stor_show_sense(us, sshdr.sense_key, sshdr.asc, sshdr.ascq);
8091da177e4SLinus Torvalds #endif
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 		/* set the result so the higher layers expect this data */
8121da177e4SLinus Torvalds 		srb->result = SAM_STAT_CHECK_CONDITION;
8131da177e4SLinus Torvalds 
814e16da02fSLuben Tuikov 		scdd = scsi_sense_desc_find(srb->sense_buffer,
815e16da02fSLuben Tuikov 					    SCSI_SENSE_BUFFERSIZE, 4);
816e16da02fSLuben Tuikov 		fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
817e16da02fSLuben Tuikov 
818f0183a33SFelipe Balbi 		/*
819f0183a33SFelipe Balbi 		 * We often get empty sense data.  This could indicate that
820f1a0743bSAlan Stern 		 * everything worked or that there was an unspecified
821f1a0743bSAlan Stern 		 * problem.  We have to decide which.
822f1a0743bSAlan Stern 		 */
823e16da02fSLuben Tuikov 		if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
824e16da02fSLuben Tuikov 		    fm_ili == 0) {
825f0183a33SFelipe Balbi 			/*
826f0183a33SFelipe Balbi 			 * If things are really okay, then let's show that.
827f1a0743bSAlan Stern 			 * Zero out the sense buffer so the higher layers
828f1a0743bSAlan Stern 			 * won't realize we did an unsolicited auto-sense.
829f1a0743bSAlan Stern 			 */
830f1a0743bSAlan Stern 			if (result == USB_STOR_TRANSPORT_GOOD) {
8311da177e4SLinus Torvalds 				srb->result = SAM_STAT_GOOD;
8321da177e4SLinus Torvalds 				srb->sense_buffer[0] = 0x0;
833a4fd4a72SAlan Stern 			}
834a4fd4a72SAlan Stern 
835a4fd4a72SAlan Stern 			/*
836a4fd4a72SAlan Stern 			 * ATA-passthru commands use sense data to report
837a4fd4a72SAlan Stern 			 * the command completion status, and often devices
838a4fd4a72SAlan Stern 			 * return Check Condition status when nothing is
839a4fd4a72SAlan Stern 			 * wrong.
840a4fd4a72SAlan Stern 			 */
841a4fd4a72SAlan Stern 			else if (srb->cmnd[0] == ATA_16 ||
842a4fd4a72SAlan Stern 					srb->cmnd[0] == ATA_12) {
843a4fd4a72SAlan Stern 				/* leave the data alone */
844a4fd4a72SAlan Stern 			}
845f1a0743bSAlan Stern 
846f0183a33SFelipe Balbi 			/*
847f0183a33SFelipe Balbi 			 * If there was a problem, report an unspecified
848f1a0743bSAlan Stern 			 * hardware error to prevent the higher layers from
849f1a0743bSAlan Stern 			 * entering an infinite retry loop.
850f1a0743bSAlan Stern 			 */
851a4fd4a72SAlan Stern 			else {
852f1a0743bSAlan Stern 				srb->result = DID_ERROR << 16;
853e16da02fSLuben Tuikov 				if ((sshdr.response_code & 0x72) == 0x72)
854e16da02fSLuben Tuikov 					srb->sense_buffer[1] = HARDWARE_ERROR;
855e16da02fSLuben Tuikov 				else
856f1a0743bSAlan Stern 					srb->sense_buffer[2] = HARDWARE_ERROR;
857f1a0743bSAlan Stern 			}
8581da177e4SLinus Torvalds 		}
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 
86121c13a4fSAlan Stern 	/*
86221c13a4fSAlan Stern 	 * Some devices don't work or return incorrect data the first
86321c13a4fSAlan Stern 	 * time they get a READ(10) command, or for the first READ(10)
86421c13a4fSAlan Stern 	 * after a media change.  If the INITIAL_READ10 flag is set,
86521c13a4fSAlan Stern 	 * keep track of whether READ(10) commands succeed.  If the
86621c13a4fSAlan Stern 	 * previous one succeeded and this one failed, set the REDO_READ10
86721c13a4fSAlan Stern 	 * flag to force a retry.
86821c13a4fSAlan Stern 	 */
86921c13a4fSAlan Stern 	if (unlikely((us->fflags & US_FL_INITIAL_READ10) &&
87021c13a4fSAlan Stern 			srb->cmnd[0] == READ_10)) {
87121c13a4fSAlan Stern 		if (srb->result == SAM_STAT_GOOD) {
87221c13a4fSAlan Stern 			set_bit(US_FLIDX_READ10_WORKED, &us->dflags);
87321c13a4fSAlan Stern 		} else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) {
87421c13a4fSAlan Stern 			clear_bit(US_FLIDX_READ10_WORKED, &us->dflags);
87521c13a4fSAlan Stern 			set_bit(US_FLIDX_REDO_READ10, &us->dflags);
87621c13a4fSAlan Stern 		}
87721c13a4fSAlan Stern 
87821c13a4fSAlan Stern 		/*
87921c13a4fSAlan Stern 		 * Next, if the REDO_READ10 flag is set, return a result
88021c13a4fSAlan Stern 		 * code that will cause the SCSI core to retry the READ(10)
88121c13a4fSAlan Stern 		 * command immediately.
88221c13a4fSAlan Stern 		 */
88321c13a4fSAlan Stern 		if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) {
88421c13a4fSAlan Stern 			clear_bit(US_FLIDX_REDO_READ10, &us->dflags);
88521c13a4fSAlan Stern 			srb->result = DID_IMM_RETRY << 16;
88621c13a4fSAlan Stern 			srb->sense_buffer[0] = 0;
88721c13a4fSAlan Stern 		}
88821c13a4fSAlan Stern 	}
88921c13a4fSAlan Stern 
8901da177e4SLinus Torvalds 	/* Did we transfer less than the minimum amount required? */
8918bfa2472SAlan Stern 	if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
8926d416e61SBoaz Harrosh 			scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
8931c9fbafcSMartin K. Petersen 		srb->result = DID_ERROR << 16;
8941da177e4SLinus Torvalds 
89525ff1c31SAlan Stern 	last_sector_hacks(us, srb);
8961da177e4SLinus Torvalds 	return;
8971da177e4SLinus Torvalds 
898f0183a33SFelipe Balbi 	/*
899f0183a33SFelipe Balbi 	 * Error and abort processing: try to resynchronize with the device
9004d07ef76SMatthew Dharm 	 * by issuing a port reset.  If that fails, try a class-specific
901f0183a33SFelipe Balbi 	 * device reset.
902f0183a33SFelipe Balbi 	 */
9034d07ef76SMatthew Dharm   Handle_Errors:
9044d07ef76SMatthew Dharm 
905f0183a33SFelipe Balbi 	/*
906f0183a33SFelipe Balbi 	 * Set the RESETTING bit, and clear the ABORTING bit so that
907f0183a33SFelipe Balbi 	 * the reset may proceed.
908f0183a33SFelipe Balbi 	 */
9094d07ef76SMatthew Dharm 	scsi_lock(us_to_host(us));
9107e4d6c38SAlan Stern 	set_bit(US_FLIDX_RESETTING, &us->dflags);
9117e4d6c38SAlan Stern 	clear_bit(US_FLIDX_ABORTING, &us->dflags);
9124d07ef76SMatthew Dharm 	scsi_unlock(us_to_host(us));
9134d07ef76SMatthew Dharm 
914f0183a33SFelipe Balbi 	/*
915f0183a33SFelipe Balbi 	 * We must release the device lock because the pre_reset routine
916f0183a33SFelipe Balbi 	 * will want to acquire it.
917f0183a33SFelipe Balbi 	 */
91847104b0dSAlan Stern 	mutex_unlock(&us->dev_mutex);
9194d07ef76SMatthew Dharm 	result = usb_stor_port_reset(us);
92047104b0dSAlan Stern 	mutex_lock(&us->dev_mutex);
92147104b0dSAlan Stern 
9224d07ef76SMatthew Dharm 	if (result < 0) {
9234d07ef76SMatthew Dharm 		scsi_lock(us_to_host(us));
9244d07ef76SMatthew Dharm 		usb_stor_report_device_reset(us);
9254d07ef76SMatthew Dharm 		scsi_unlock(us_to_host(us));
9261da177e4SLinus Torvalds 		us->transport_reset(us);
9271da177e4SLinus Torvalds 	}
9287e4d6c38SAlan Stern 	clear_bit(US_FLIDX_RESETTING, &us->dflags);
92925ff1c31SAlan Stern 	last_sector_hacks(us, srb);
9304d07ef76SMatthew Dharm }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds /* Stop the current URB transfer */
usb_stor_stop_transport(struct us_data * us)9331da177e4SLinus Torvalds void usb_stor_stop_transport(struct us_data *us)
9341da177e4SLinus Torvalds {
935f0183a33SFelipe Balbi 	/*
936f0183a33SFelipe Balbi 	 * If the state machine is blocked waiting for an URB,
9371da177e4SLinus Torvalds 	 * let's wake it up.  The test_and_clear_bit() call
9381da177e4SLinus Torvalds 	 * guarantees that if a URB has just been submitted,
939f0183a33SFelipe Balbi 	 * it won't be cancelled more than once.
940f0183a33SFelipe Balbi 	 */
9417e4d6c38SAlan Stern 	if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
942191648d0SJoe Perches 		usb_stor_dbg(us, "-- cancelling URB\n");
9431da177e4SLinus Torvalds 		usb_unlink_urb(us->current_urb);
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	/* If we are waiting for a scatter-gather operation, cancel it. */
9477e4d6c38SAlan Stern 	if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
948191648d0SJoe Perches 		usb_stor_dbg(us, "-- cancelling sg request\n");
9491da177e4SLinus Torvalds 		usb_sg_cancel(&us->current_sg);
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds /*
95464648a9dSAlan Stern  * Control/Bulk and Control/Bulk/Interrupt transport
9551da177e4SLinus Torvalds  */
9561da177e4SLinus Torvalds 
usb_stor_CB_transport(struct scsi_cmnd * srb,struct us_data * us)95764648a9dSAlan Stern int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
9581da177e4SLinus Torvalds {
9596d416e61SBoaz Harrosh 	unsigned int transfer_length = scsi_bufflen(srb);
9601da177e4SLinus Torvalds 	unsigned int pipe = 0;
9611da177e4SLinus Torvalds 	int result;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	/* COMMAND STAGE */
9641da177e4SLinus Torvalds 	/* let's send the command via the control pipe */
9652ce9d227SPetr Vandrovec 	/*
9662ce9d227SPetr Vandrovec 	 * Command is sometime (f.e. after scsi_eh_prep_cmnd) on the stack.
9672ce9d227SPetr Vandrovec 	 * Stack may be vmallocated.  So no DMA for us.  Make a copy.
9682ce9d227SPetr Vandrovec 	 */
9692ce9d227SPetr Vandrovec 	memcpy(us->iobuf, srb->cmnd, srb->cmd_len);
9701da177e4SLinus Torvalds 	result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
9711da177e4SLinus Torvalds 				      US_CBI_ADSC,
9721da177e4SLinus Torvalds 				      USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
9732ce9d227SPetr Vandrovec 				      us->ifnum, us->iobuf, srb->cmd_len);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	/* check the return code for the command */
976191648d0SJoe Perches 	usb_stor_dbg(us, "Call to usb_stor_ctrl_transfer() returned %d\n",
977191648d0SJoe Perches 		     result);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	/* if we stalled the command, it means command failed */
9801da177e4SLinus Torvalds 	if (result == USB_STOR_XFER_STALLED) {
9811da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_FAILED;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* Uh oh... serious problem here */
9851da177e4SLinus Torvalds 	if (result != USB_STOR_XFER_GOOD) {
9861da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_ERROR;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	/* DATA STAGE */
9901da177e4SLinus Torvalds 	/* transfer the data payload for this command, if one exists*/
9911da177e4SLinus Torvalds 	if (transfer_length) {
9921da177e4SLinus Torvalds 		pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
9931da177e4SLinus Torvalds 				us->recv_bulk_pipe : us->send_bulk_pipe;
9946d416e61SBoaz Harrosh 		result = usb_stor_bulk_srb(us, pipe, srb);
995191648d0SJoe Perches 		usb_stor_dbg(us, "CBI data stage result is 0x%x\n", result);
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 		/* if we stalled the data transfer it means command failed */
9981da177e4SLinus Torvalds 		if (result == USB_STOR_XFER_STALLED)
9991da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_FAILED;
10001da177e4SLinus Torvalds 		if (result > USB_STOR_XFER_STALLED)
10011da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_ERROR;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* STATUS STAGE */
100564648a9dSAlan Stern 
1006f0183a33SFelipe Balbi 	/*
1007f0183a33SFelipe Balbi 	 * NOTE: CB does not have a status stage.  Silly, I know.  So
100864648a9dSAlan Stern 	 * we have to catch this at a higher level.
100964648a9dSAlan Stern 	 */
10108fa7fd74SMichal Nazarewicz 	if (us->protocol != USB_PR_CBI)
101164648a9dSAlan Stern 		return USB_STOR_TRANSPORT_GOOD;
101264648a9dSAlan Stern 
10131da177e4SLinus Torvalds 	result = usb_stor_intr_transfer(us, us->iobuf, 2);
1014191648d0SJoe Perches 	usb_stor_dbg(us, "Got interrupt data (0x%x, 0x%x)\n",
10151da177e4SLinus Torvalds 		     us->iobuf[0], us->iobuf[1]);
10161da177e4SLinus Torvalds 	if (result != USB_STOR_XFER_GOOD)
10171da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_ERROR;
10181da177e4SLinus Torvalds 
1019f0183a33SFelipe Balbi 	/*
1020f0183a33SFelipe Balbi 	 * UFI gives us ASC and ASCQ, like a request sense
10211da177e4SLinus Torvalds 	 *
10221da177e4SLinus Torvalds 	 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
10231da177e4SLinus Torvalds 	 * devices, so we ignore the information for those commands.  Note
10241da177e4SLinus Torvalds 	 * that this means we could be ignoring a real error on these
10251da177e4SLinus Torvalds 	 * commands, but that can't be helped.
10261da177e4SLinus Torvalds 	 */
10278fa7fd74SMichal Nazarewicz 	if (us->subclass == USB_SC_UFI) {
10281da177e4SLinus Torvalds 		if (srb->cmnd[0] == REQUEST_SENSE ||
10291da177e4SLinus Torvalds 		    srb->cmnd[0] == INQUIRY)
10301da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_GOOD;
10311da177e4SLinus Torvalds 		if (us->iobuf[0])
10321da177e4SLinus Torvalds 			goto Failed;
10331da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_GOOD;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 
1036f0183a33SFelipe Balbi 	/*
1037f0183a33SFelipe Balbi 	 * If not UFI, we interpret the data as a result code
10381da177e4SLinus Torvalds 	 * The first byte should always be a 0x0.
10391da177e4SLinus Torvalds 	 *
10401da177e4SLinus Torvalds 	 * Some bogus devices don't follow that rule.  They stuff the ASC
10411da177e4SLinus Torvalds 	 * into the first byte -- so if it's non-zero, call it a failure.
10421da177e4SLinus Torvalds 	 */
10431da177e4SLinus Torvalds 	if (us->iobuf[0]) {
1044191648d0SJoe Perches 		usb_stor_dbg(us, "CBI IRQ data showed reserved bType 0x%x\n",
10451da177e4SLinus Torvalds 			     us->iobuf[0]);
10461da177e4SLinus Torvalds 		goto Failed;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	}
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	/* The second byte & 0x0F should be 0x0 for good, otherwise error */
10511da177e4SLinus Torvalds 	switch (us->iobuf[1] & 0x0F) {
10521da177e4SLinus Torvalds 		case 0x00:
10531da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_GOOD;
10541da177e4SLinus Torvalds 		case 0x01:
10551da177e4SLinus Torvalds 			goto Failed;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 	return USB_STOR_TRANSPORT_ERROR;
10581da177e4SLinus Torvalds 
1059f0183a33SFelipe Balbi 	/*
1060f0183a33SFelipe Balbi 	 * the CBI spec requires that the bulk pipe must be cleared
10611da177e4SLinus Torvalds 	 * following any data-in/out command failure (section 2.4.3.1.3)
10621da177e4SLinus Torvalds 	 */
10631da177e4SLinus Torvalds   Failed:
10641da177e4SLinus Torvalds 	if (pipe)
10651da177e4SLinus Torvalds 		usb_stor_clear_halt(us, pipe);
10661da177e4SLinus Torvalds 	return USB_STOR_TRANSPORT_FAILED;
10671da177e4SLinus Torvalds }
1068e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_CB_transport);
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds /*
10711da177e4SLinus Torvalds  * Bulk only transport
10721da177e4SLinus Torvalds  */
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds /* Determine what the maximum LUN supported is */
usb_stor_Bulk_max_lun(struct us_data * us)10751da177e4SLinus Torvalds int usb_stor_Bulk_max_lun(struct us_data *us)
10761da177e4SLinus Torvalds {
10771da177e4SLinus Torvalds 	int result;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	/* issue the command */
1080b876aef7SAlan Stern 	us->iobuf[0] = 0;
10811da177e4SLinus Torvalds 	result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
10821da177e4SLinus Torvalds 				 US_BULK_GET_MAX_LUN,
10831da177e4SLinus Torvalds 				 USB_DIR_IN | USB_TYPE_CLASS |
10841da177e4SLinus Torvalds 				 USB_RECIP_INTERFACE,
10857a777919SGiacomo Lozito 				 0, us->ifnum, us->iobuf, 1, 10*HZ);
10861da177e4SLinus Torvalds 
1087191648d0SJoe Perches 	usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
10881da177e4SLinus Torvalds 		     result, us->iobuf[0]);
10891da177e4SLinus Torvalds 
109055dc68c0SMark Knibbs 	/*
109155dc68c0SMark Knibbs 	 * If we have a successful request, return the result if valid. The
109255dc68c0SMark Knibbs 	 * CBW LUN field is 4 bits wide, so the value reported by the device
109355dc68c0SMark Knibbs 	 * should fit into that.
109455dc68c0SMark Knibbs 	 */
109555dc68c0SMark Knibbs 	if (result > 0) {
109655dc68c0SMark Knibbs 		if (us->iobuf[0] < 16) {
10971da177e4SLinus Torvalds 			return us->iobuf[0];
109855dc68c0SMark Knibbs 		} else {
109955dc68c0SMark Knibbs 			dev_info(&us->pusb_intf->dev,
110055dc68c0SMark Knibbs 				 "Max LUN %d is not valid, using 0 instead",
110155dc68c0SMark Knibbs 				 us->iobuf[0]);
110255dc68c0SMark Knibbs 		}
110355dc68c0SMark Knibbs 	}
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	/*
11061da177e4SLinus Torvalds 	 * Some devices don't like GetMaxLUN.  They may STALL the control
11071da177e4SLinus Torvalds 	 * pipe, they may return a zero-length result, they may do nothing at
11081da177e4SLinus Torvalds 	 * all and timeout, or they may fail in even more bizarrely creative
11091da177e4SLinus Torvalds 	 * ways.  In these cases the best approach is to use the default
11101da177e4SLinus Torvalds 	 * value: only one LUN.
11111da177e4SLinus Torvalds 	 */
11121da177e4SLinus Torvalds 	return 0;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
usb_stor_Bulk_transport(struct scsi_cmnd * srb,struct us_data * us)11151da177e4SLinus Torvalds int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
11181da177e4SLinus Torvalds 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
11196d416e61SBoaz Harrosh 	unsigned int transfer_length = scsi_bufflen(srb);
11201da177e4SLinus Torvalds 	unsigned int residue;
11211da177e4SLinus Torvalds 	int result;
11221da177e4SLinus Torvalds 	int fake_sense = 0;
11231da177e4SLinus Torvalds 	unsigned int cswlen;
11241da177e4SLinus Torvalds 	unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	/* Take care of BULK32 devices; set extra byte to 0 */
11277e4d6c38SAlan Stern 	if (unlikely(us->fflags & US_FL_BULK32)) {
11281da177e4SLinus Torvalds 		cbwlen = 32;
11291da177e4SLinus Torvalds 		us->iobuf[31] = 0;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	/* set up the command wrapper */
11331da177e4SLinus Torvalds 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
11341da177e4SLinus Torvalds 	bcb->DataTransferLength = cpu_to_le32(transfer_length);
1135b8db6d64SSebastian Andrzej Siewior 	bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ?
1136b8db6d64SSebastian Andrzej Siewior 		US_BULK_FLAG_IN : 0;
11370f64e078SMatthew Dharm 	bcb->Tag = ++us->tag;
11381da177e4SLinus Torvalds 	bcb->Lun = srb->device->lun;
11397e4d6c38SAlan Stern 	if (us->fflags & US_FL_SCM_MULT_TARG)
11401da177e4SLinus Torvalds 		bcb->Lun |= srb->device->id << 4;
11411da177e4SLinus Torvalds 	bcb->Length = srb->cmd_len;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	/* copy the command payload */
11441da177e4SLinus Torvalds 	memset(bcb->CDB, 0, sizeof(bcb->CDB));
11451da177e4SLinus Torvalds 	memcpy(bcb->CDB, srb->cmnd, bcb->Length);
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	/* send it to out endpoint */
1148191648d0SJoe Perches 	usb_stor_dbg(us, "Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
11491da177e4SLinus Torvalds 		     le32_to_cpu(bcb->Signature), bcb->Tag,
11501da177e4SLinus Torvalds 		     le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
11511da177e4SLinus Torvalds 		     (bcb->Lun >> 4), (bcb->Lun & 0x0F),
11521da177e4SLinus Torvalds 		     bcb->Length);
11531da177e4SLinus Torvalds 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
11541da177e4SLinus Torvalds 				bcb, cbwlen, NULL);
1155191648d0SJoe Perches 	usb_stor_dbg(us, "Bulk command transfer result=%d\n", result);
11561da177e4SLinus Torvalds 	if (result != USB_STOR_XFER_GOOD)
11571da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_ERROR;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	/* DATA STAGE */
11601da177e4SLinus Torvalds 	/* send/receive data payload, if there is any */
11611da177e4SLinus Torvalds 
1162f0183a33SFelipe Balbi 	/*
1163f0183a33SFelipe Balbi 	 * Some USB-IDE converter chips need a 100us delay between the
11641da177e4SLinus Torvalds 	 * command phase and the data phase.  Some devices need a little
1165f0183a33SFelipe Balbi 	 * more than that, probably because of clock rate inaccuracies.
1166f0183a33SFelipe Balbi 	 */
11677e4d6c38SAlan Stern 	if (unlikely(us->fflags & US_FL_GO_SLOW))
1168e616b39aSSunny Kumar 		usleep_range(125, 150);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	if (transfer_length) {
11711da177e4SLinus Torvalds 		unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
11721da177e4SLinus Torvalds 				us->recv_bulk_pipe : us->send_bulk_pipe;
11736d416e61SBoaz Harrosh 		result = usb_stor_bulk_srb(us, pipe, srb);
1174191648d0SJoe Perches 		usb_stor_dbg(us, "Bulk data transfer result 0x%x\n", result);
11751da177e4SLinus Torvalds 		if (result == USB_STOR_XFER_ERROR)
11761da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_ERROR;
11771da177e4SLinus Torvalds 
1178f0183a33SFelipe Balbi 		/*
1179f0183a33SFelipe Balbi 		 * If the device tried to send back more data than the
11801da177e4SLinus Torvalds 		 * amount requested, the spec requires us to transfer
1181*274a12eaSXiang wangx 		 * the CSW anyway.  Since there's no point retrying
11821da177e4SLinus Torvalds 		 * the command, we'll return fake sense data indicating
11831da177e4SLinus Torvalds 		 * Illegal Request, Invalid Field in CDB.
11841da177e4SLinus Torvalds 		 */
11851da177e4SLinus Torvalds 		if (result == USB_STOR_XFER_LONG)
11861da177e4SLinus Torvalds 			fake_sense = 1;
118793c9bf4dSAlan Stern 
118893c9bf4dSAlan Stern 		/*
118993c9bf4dSAlan Stern 		 * Sometimes a device will mistakenly skip the data phase
119093c9bf4dSAlan Stern 		 * and go directly to the status phase without sending a
119193c9bf4dSAlan Stern 		 * zero-length packet.  If we get a 13-byte response here,
119293c9bf4dSAlan Stern 		 * check whether it really is a CSW.
119393c9bf4dSAlan Stern 		 */
119493c9bf4dSAlan Stern 		if (result == USB_STOR_XFER_SHORT &&
119593c9bf4dSAlan Stern 				srb->sc_data_direction == DMA_FROM_DEVICE &&
119693c9bf4dSAlan Stern 				transfer_length - scsi_get_resid(srb) ==
119793c9bf4dSAlan Stern 					US_BULK_CS_WRAP_LEN) {
119893c9bf4dSAlan Stern 			struct scatterlist *sg = NULL;
119993c9bf4dSAlan Stern 			unsigned int offset = 0;
120093c9bf4dSAlan Stern 
120193c9bf4dSAlan Stern 			if (usb_stor_access_xfer_buf((unsigned char *) bcs,
120293c9bf4dSAlan Stern 					US_BULK_CS_WRAP_LEN, srb, &sg,
120393c9bf4dSAlan Stern 					&offset, FROM_XFER_BUF) ==
120493c9bf4dSAlan Stern 						US_BULK_CS_WRAP_LEN &&
120593c9bf4dSAlan Stern 					bcs->Signature ==
120693c9bf4dSAlan Stern 						cpu_to_le32(US_BULK_CS_SIGN)) {
120793c9bf4dSAlan Stern 				usb_stor_dbg(us, "Device skipped data phase\n");
120893c9bf4dSAlan Stern 				scsi_set_resid(srb, transfer_length);
120993c9bf4dSAlan Stern 				goto skipped_data_phase;
121093c9bf4dSAlan Stern 			}
121193c9bf4dSAlan Stern 		}
12121da177e4SLinus Torvalds 	}
12131da177e4SLinus Torvalds 
1214f0183a33SFelipe Balbi 	/*
1215f0183a33SFelipe Balbi 	 * See flow chart on pg 15 of the Bulk Only Transport spec for
12161da177e4SLinus Torvalds 	 * an explanation of how this code works.
12171da177e4SLinus Torvalds 	 */
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	/* get CSW for device status */
1220191648d0SJoe Perches 	usb_stor_dbg(us, "Attempting to get CSW...\n");
12211da177e4SLinus Torvalds 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
12221da177e4SLinus Torvalds 				bcs, US_BULK_CS_WRAP_LEN, &cswlen);
12231da177e4SLinus Torvalds 
1224f0183a33SFelipe Balbi 	/*
1225f0183a33SFelipe Balbi 	 * Some broken devices add unnecessary zero-length packets to the
12261da177e4SLinus Torvalds 	 * end of their data transfers.  Such packets show up as 0-length
12271da177e4SLinus Torvalds 	 * CSWs.  If we encounter such a thing, try to read the CSW again.
12281da177e4SLinus Torvalds 	 */
12291da177e4SLinus Torvalds 	if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
1230191648d0SJoe Perches 		usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
12311da177e4SLinus Torvalds 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
12321da177e4SLinus Torvalds 				bcs, US_BULK_CS_WRAP_LEN, &cswlen);
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	/* did the attempt to read the CSW fail? */
12361da177e4SLinus Torvalds 	if (result == USB_STOR_XFER_STALLED) {
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		/* get the status again */
1239191648d0SJoe Perches 		usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
12401da177e4SLinus Torvalds 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
12411da177e4SLinus Torvalds 				bcs, US_BULK_CS_WRAP_LEN, NULL);
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	/* if we still have a failure at this point, we're in trouble */
1245191648d0SJoe Perches 	usb_stor_dbg(us, "Bulk status result = %d\n", result);
12461da177e4SLinus Torvalds 	if (result != USB_STOR_XFER_GOOD)
12471da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_ERROR;
12481da177e4SLinus Torvalds 
124993c9bf4dSAlan Stern  skipped_data_phase:
12501da177e4SLinus Torvalds 	/* check bulk status */
12511da177e4SLinus Torvalds 	residue = le32_to_cpu(bcs->Residue);
1252191648d0SJoe Perches 	usb_stor_dbg(us, "Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
12531da177e4SLinus Torvalds 		     le32_to_cpu(bcs->Signature), bcs->Tag,
12541da177e4SLinus Torvalds 		     residue, bcs->Status);
12557e4d6c38SAlan Stern 	if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
1256cc36bdd4SConstantin Baranov 		bcs->Status > US_BULK_STAT_PHASE) {
1257191648d0SJoe Perches 		usb_stor_dbg(us, "Bulk logical error\n");
12581da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_ERROR;
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds 
1261f0183a33SFelipe Balbi 	/*
1262f0183a33SFelipe Balbi 	 * Some broken devices report odd signatures, so we do not check them
12631da177e4SLinus Torvalds 	 * for validity against the spec. We store the first one we see,
12641da177e4SLinus Torvalds 	 * and check subsequent transfers for validity against this signature.
12651da177e4SLinus Torvalds 	 */
12661da177e4SLinus Torvalds 	if (!us->bcs_signature) {
12671da177e4SLinus Torvalds 		us->bcs_signature = bcs->Signature;
12681da177e4SLinus Torvalds 		if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
1269191648d0SJoe Perches 			usb_stor_dbg(us, "Learnt BCS signature 0x%08X\n",
12701da177e4SLinus Torvalds 				     le32_to_cpu(us->bcs_signature));
12711da177e4SLinus Torvalds 	} else if (bcs->Signature != us->bcs_signature) {
1272191648d0SJoe Perches 		usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
12731da177e4SLinus Torvalds 			     le32_to_cpu(bcs->Signature),
12741da177e4SLinus Torvalds 			     le32_to_cpu(us->bcs_signature));
12751da177e4SLinus Torvalds 		return USB_STOR_TRANSPORT_ERROR;
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 
1278f0183a33SFelipe Balbi 	/*
1279f0183a33SFelipe Balbi 	 * try to compute the actual residue, based on how much data
1280f0183a33SFelipe Balbi 	 * was really transferred and what the device tells us
1281f0183a33SFelipe Balbi 	 */
128259f4ff2eSAlan Stern 	if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
128359f4ff2eSAlan Stern 
1284f0183a33SFelipe Balbi 		/*
1285f0183a33SFelipe Balbi 		 * Heuristically detect devices that generate bogus residues
128659f4ff2eSAlan Stern 		 * by seeing what happens with INQUIRY and READ CAPACITY
128759f4ff2eSAlan Stern 		 * commands.
128859f4ff2eSAlan Stern 		 */
128959f4ff2eSAlan Stern 		if (bcs->Status == US_BULK_STAT_OK &&
129059f4ff2eSAlan Stern 				scsi_get_resid(srb) == 0 &&
129159f4ff2eSAlan Stern 					((srb->cmnd[0] == INQUIRY &&
129259f4ff2eSAlan Stern 						transfer_length == 36) ||
129359f4ff2eSAlan Stern 					(srb->cmnd[0] == READ_CAPACITY &&
129459f4ff2eSAlan Stern 						transfer_length == 8))) {
129559f4ff2eSAlan Stern 			us->fflags |= US_FL_IGNORE_RESIDUE;
129659f4ff2eSAlan Stern 
129759f4ff2eSAlan Stern 		} else {
12981da177e4SLinus Torvalds 			residue = min(residue, transfer_length);
12999237f04eSDamien Le Moal 			scsi_set_resid(srb, max(scsi_get_resid(srb), residue));
13001da177e4SLinus Torvalds 		}
13011da177e4SLinus Torvalds 	}
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	/* based on the status code, we report good or bad */
13041da177e4SLinus Torvalds 	switch (bcs->Status) {
13051da177e4SLinus Torvalds 		case US_BULK_STAT_OK:
13061da177e4SLinus Torvalds 			/* device babbled -- return fake sense data */
13071da177e4SLinus Torvalds 			if (fake_sense) {
13081da177e4SLinus Torvalds 				memcpy(srb->sense_buffer,
13091da177e4SLinus Torvalds 				       usb_stor_sense_invalidCDB,
13101da177e4SLinus Torvalds 				       sizeof(usb_stor_sense_invalidCDB));
13111da177e4SLinus Torvalds 				return USB_STOR_TRANSPORT_NO_SENSE;
13121da177e4SLinus Torvalds 			}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 			/* command good -- note that data could be short */
13151da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_GOOD;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 		case US_BULK_STAT_FAIL:
13181da177e4SLinus Torvalds 			/* command failed */
13191da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_FAILED;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 		case US_BULK_STAT_PHASE:
1322f0183a33SFelipe Balbi 			/*
1323f0183a33SFelipe Balbi 			 * phase error -- note that a transport reset will be
13241da177e4SLinus Torvalds 			 * invoked by the invoke_transport() function
13251da177e4SLinus Torvalds 			 */
13261da177e4SLinus Torvalds 			return USB_STOR_TRANSPORT_ERROR;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	/* we should never get here, but if we do, we're in trouble */
13301da177e4SLinus Torvalds 	return USB_STOR_TRANSPORT_ERROR;
13311da177e4SLinus Torvalds }
1332e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_Bulk_transport);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds /***********************************************************************
13351da177e4SLinus Torvalds  * Reset routines
13361da177e4SLinus Torvalds  ***********************************************************************/
13371da177e4SLinus Torvalds 
1338f0183a33SFelipe Balbi /*
1339f0183a33SFelipe Balbi  * This is the common part of the device reset code.
13401da177e4SLinus Torvalds  *
13411da177e4SLinus Torvalds  * It's handy that every transport mechanism uses the control endpoint for
13421da177e4SLinus Torvalds  * resets.
13431da177e4SLinus Torvalds  *
13445203ad44SMatthew Dharm  * Basically, we send a reset with a 5-second timeout, so we don't get
13451da177e4SLinus Torvalds  * jammed attempting to do the reset.
13461da177e4SLinus Torvalds  */
usb_stor_reset_common(struct us_data * us,u8 request,u8 requesttype,u16 value,u16 index,void * data,u16 size)13471da177e4SLinus Torvalds static int usb_stor_reset_common(struct us_data *us,
13481da177e4SLinus Torvalds 		u8 request, u8 requesttype,
13491da177e4SLinus Torvalds 		u16 value, u16 index, void *data, u16 size)
13501da177e4SLinus Torvalds {
13511da177e4SLinus Torvalds 	int result;
13521da177e4SLinus Torvalds 	int result2;
13531da177e4SLinus Torvalds 
13547e4d6c38SAlan Stern 	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1355191648d0SJoe Perches 		usb_stor_dbg(us, "No reset during disconnect\n");
13564d07ef76SMatthew Dharm 		return -EIO;
13574d07ef76SMatthew Dharm 	}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	result = usb_stor_control_msg(us, us->send_ctrl_pipe,
13601da177e4SLinus Torvalds 			request, requesttype, value, index, data, size,
13615203ad44SMatthew Dharm 			5*HZ);
13621da177e4SLinus Torvalds 	if (result < 0) {
1363191648d0SJoe Perches 		usb_stor_dbg(us, "Soft reset failed: %d\n", result);
13644d07ef76SMatthew Dharm 		return result;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
1367f0183a33SFelipe Balbi 	/*
1368f0183a33SFelipe Balbi 	 * Give the device some time to recover from the reset,
1369f0183a33SFelipe Balbi 	 * but don't delay disconnect processing.
1370f0183a33SFelipe Balbi 	 */
13711da177e4SLinus Torvalds 	wait_event_interruptible_timeout(us->delay_wait,
13727e4d6c38SAlan Stern 			test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
13731da177e4SLinus Torvalds 			HZ*6);
13747e4d6c38SAlan Stern 	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1375191648d0SJoe Perches 		usb_stor_dbg(us, "Reset interrupted by disconnect\n");
13764d07ef76SMatthew Dharm 		return -EIO;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
1379191648d0SJoe Perches 	usb_stor_dbg(us, "Soft reset: clearing bulk-in endpoint halt\n");
13801da177e4SLinus Torvalds 	result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
13811da177e4SLinus Torvalds 
1382191648d0SJoe Perches 	usb_stor_dbg(us, "Soft reset: clearing bulk-out endpoint halt\n");
13831da177e4SLinus Torvalds 	result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
13841da177e4SLinus Torvalds 
13855203ad44SMatthew Dharm 	/* return a result code based on the result of the clear-halts */
13865203ad44SMatthew Dharm 	if (result >= 0)
13875203ad44SMatthew Dharm 		result = result2;
13884d07ef76SMatthew Dharm 	if (result < 0)
1389191648d0SJoe Perches 		usb_stor_dbg(us, "Soft reset failed\n");
13904d07ef76SMatthew Dharm 	else
1391191648d0SJoe Perches 		usb_stor_dbg(us, "Soft reset done\n");
13924d07ef76SMatthew Dharm 	return result;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
1395f0183a33SFelipe Balbi /* This issues a CB[I] Reset to the device in question */
13961da177e4SLinus Torvalds #define CB_RESET_CMD_SIZE	12
13971da177e4SLinus Torvalds 
usb_stor_CB_reset(struct us_data * us)13981da177e4SLinus Torvalds int usb_stor_CB_reset(struct us_data *us)
13991da177e4SLinus Torvalds {
14001da177e4SLinus Torvalds 	memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
14011da177e4SLinus Torvalds 	us->iobuf[0] = SEND_DIAGNOSTIC;
14021da177e4SLinus Torvalds 	us->iobuf[1] = 4;
14031da177e4SLinus Torvalds 	return usb_stor_reset_common(us, US_CBI_ADSC,
14041da177e4SLinus Torvalds 				 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
14051da177e4SLinus Torvalds 				 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
14061da177e4SLinus Torvalds }
1407e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_CB_reset);
14081da177e4SLinus Torvalds 
1409f0183a33SFelipe Balbi /*
1410f0183a33SFelipe Balbi  * This issues a Bulk-only Reset to the device in question, including
14111da177e4SLinus Torvalds  * clearing the subsequent endpoint halts that may occur.
14121da177e4SLinus Torvalds  */
usb_stor_Bulk_reset(struct us_data * us)14131da177e4SLinus Torvalds int usb_stor_Bulk_reset(struct us_data *us)
14141da177e4SLinus Torvalds {
14151da177e4SLinus Torvalds 	return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
14161da177e4SLinus Torvalds 				 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
14171da177e4SLinus Torvalds 				 0, us->ifnum, NULL, 0);
14181da177e4SLinus Torvalds }
1419e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_Bulk_reset);
14204d07ef76SMatthew Dharm 
1421f0183a33SFelipe Balbi /*
1422f0183a33SFelipe Balbi  * Issue a USB port reset to the device.  The caller must not hold
142347104b0dSAlan Stern  * us->dev_mutex.
142447104b0dSAlan Stern  */
usb_stor_port_reset(struct us_data * us)14254d07ef76SMatthew Dharm int usb_stor_port_reset(struct us_data *us)
14264d07ef76SMatthew Dharm {
1427011b15dfSAlan Stern 	int result;
14284d07ef76SMatthew Dharm 
14295d398779SOliver Neukum 	/*for these devices we must use the class specific method */
14307fda953fSLan Tianyu 	if (us->pusb_dev->quirks & USB_QUIRK_RESET)
14315d398779SOliver Neukum 		return -EPERM;
14325d398779SOliver Neukum 
1433011b15dfSAlan Stern 	result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
143447104b0dSAlan Stern 	if (result < 0)
1435191648d0SJoe Perches 		usb_stor_dbg(us, "unable to lock device for reset: %d\n",
1436191648d0SJoe Perches 			     result);
143747104b0dSAlan Stern 	else {
143847104b0dSAlan Stern 		/* Were we disconnected while waiting for the lock? */
14397e4d6c38SAlan Stern 		if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
14404d07ef76SMatthew Dharm 			result = -EIO;
1441191648d0SJoe Perches 			usb_stor_dbg(us, "No reset during disconnect\n");
14424d07ef76SMatthew Dharm 		} else {
1443742120c6SMing Lei 			result = usb_reset_device(us->pusb_dev);
1444191648d0SJoe Perches 			usb_stor_dbg(us, "usb_reset_device returns %d\n",
14454d07ef76SMatthew Dharm 				     result);
14464d07ef76SMatthew Dharm 		}
144747104b0dSAlan Stern 		usb_unlock_device(us->pusb_dev);
14484d07ef76SMatthew Dharm 	}
14494d07ef76SMatthew Dharm 	return result;
14504d07ef76SMatthew Dharm }
1451