]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/storage/transport.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / storage / transport.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
f0183a33
FB
2/*
3 * Driver for USB Mass Storage compliant devices
1da177e4
LT
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
11 * (c) 2002 Alan Stern <stern@rowland.org>
12 *
13 * Initial work by:
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 *
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
21 *
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
26 *
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
29 *
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
32 *
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
36 * later version.
37 *
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
1da177e4 48#include <linux/sched.h>
5a0e3ad6 49#include <linux/gfp.h>
1da177e4 50#include <linux/errno.h>
f940fcd8 51#include <linux/export.h>
1da177e4 52
5d398779
ON
53#include <linux/usb/quirks.h>
54
1da177e4 55#include <scsi/scsi.h>
dff6de73 56#include <scsi/scsi_eh.h>
1da177e4
LT
57#include <scsi/scsi_device.h>
58
59#include "usb.h"
60#include "transport.h"
61#include "protocol.h"
62#include "scsiglue.h"
63#include "debug.h"
64
25ff1c31
AS
65#include <linux/blkdev.h>
66#include "../../scsi/sd.h"
67
1da177e4
LT
68
69/***********************************************************************
70 * Data transfer routines
71 ***********************************************************************/
72
73/*
74 * This is subtle, so pay attention:
75 * ---------------------------------
76 * We're very concerned about races with a command abort. Hanging this code
77 * is a sure fire way to hang the kernel. (Note that this discussion applies
78 * only to transactions resulting from a scsi queued-command, since only
79 * these transactions are subject to a scsi abort. Other transactions, such
80 * as those occurring during device-specific initialization, must be handled
81 * by a separate code path.)
82 *
83 * The abort function (usb_storage_command_abort() in scsiglue.c) first
7e4d6c38 84 * sets the machine state and the ABORTING bit in us->dflags to prevent
1da177e4 85 * new URBs from being submitted. It then calls usb_stor_stop_transport()
7e4d6c38 86 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags
1da177e4
LT
87 * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE
88 * bit is tested to see if the current_sg scatter-gather request needs to be
89 * stopped. The timeout callback routine does much the same thing.
90 *
7e4d6c38 91 * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to
1da177e4
LT
92 * prevent new URBs from being submitted, and usb_stor_stop_transport() is
93 * called to stop any ongoing requests.
94 *
95 * The submit function first verifies that the submitting is allowed
96 * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
97 * completes without errors, and only then sets the URB_ACTIVE bit. This
98 * prevents the stop_transport() function from trying to cancel the URB
99 * while the submit call is underway. Next, the submit function must test
100 * the flags to see if an abort or disconnect occurred during the submission
101 * or before the URB_ACTIVE bit was set. If so, it's essential to cancel
102 * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
103 * is still set). Either way, the function must then wait for the URB to
b375a049
AS
104 * finish. Note that the URB can still be in progress even after a call to
105 * usb_unlink_urb() returns.
1da177e4
LT
106 *
107 * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
108 * either the stop_transport() function or the submitting function
109 * is guaranteed to call usb_unlink_urb() for an active URB,
110 * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
111 * called more than once or from being called during usb_submit_urb().
112 */
113
f0183a33
FB
114/*
115 * This is the completion handler which will wake us up when an URB
1da177e4
LT
116 * completes.
117 */
7d12e780 118static void usb_stor_blocking_completion(struct urb *urb)
1da177e4 119{
cdc97792 120 struct completion *urb_done_ptr = urb->context;
1da177e4
LT
121
122 complete(urb_done_ptr);
123}
1da177e4 124
f0183a33
FB
125/*
126 * This is the common part of the URB message submission code
1da177e4
LT
127 *
128 * All URBs from the usb-storage driver involved in handling a queued scsi
129 * command _must_ pass through this function (or something like it) for the
130 * abort mechanisms to work properly.
131 */
132static int usb_stor_msg_common(struct us_data *us, int timeout)
133{
134 struct completion urb_done;
3428cc43 135 long timeleft;
1da177e4
LT
136 int status;
137
543f7810
AS
138 /* don't submit URBs during abort processing */
139 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
1da177e4
LT
140 return -EIO;
141
142 /* set up data structures for the wakeup system */
143 init_completion(&urb_done);
144
145 /* fill the common fields in the URB */
146 us->current_urb->context = &urb_done;
c222fb2e 147 us->current_urb->transfer_flags = 0;
1da177e4 148
f0183a33
FB
149 /*
150 * we assume that if transfer_buffer isn't us->iobuf then it
1da177e4
LT
151 * hasn't been mapped for DMA. Yes, this is clunky, but it's
152 * easier than always having the caller tell us whether the
f0183a33
FB
153 * transfer buffer has already been mapped.
154 */
1da177e4
LT
155 if (us->current_urb->transfer_buffer == us->iobuf)
156 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
157 us->current_urb->transfer_dma = us->iobuf_dma;
1da177e4
LT
158
159 /* submit the URB */
160 status = usb_submit_urb(us->current_urb, GFP_NOIO);
161 if (status) {
162 /* something went wrong */
163 return status;
164 }
165
f0183a33
FB
166 /*
167 * since the URB has been submitted successfully, it's now okay
168 * to cancel it
169 */
7e4d6c38 170 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
1da177e4 171
543f7810
AS
172 /* did an abort occur during the submission? */
173 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
1da177e4
LT
174
175 /* cancel the URB, if it hasn't been cancelled already */
7e4d6c38 176 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
191648d0 177 usb_stor_dbg(us, "-- cancelling URB\n");
1da177e4
LT
178 usb_unlink_urb(us->current_urb);
179 }
180 }
181
1da177e4 182 /* wait for the completion of the URB */
3428cc43
FBH
183 timeleft = wait_for_completion_interruptible_timeout(
184 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
1da177e4 185
7e4d6c38 186 clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
3428cc43
FBH
187
188 if (timeleft <= 0) {
191648d0
JP
189 usb_stor_dbg(us, "%s -- cancelling URB\n",
190 timeleft == 0 ? "Timeout" : "Signal");
d6b7d3b6 191 usb_kill_urb(us->current_urb);
3428cc43 192 }
1da177e4
LT
193
194 /* return the URB status */
195 return us->current_urb->status;
196}
197
198/*
199 * Transfer one control message, with timeouts, and allowing early
200 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
201 */
202int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
203 u8 request, u8 requesttype, u16 value, u16 index,
204 void *data, u16 size, int timeout)
205{
206 int status;
207
191648d0
JP
208 usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
209 request, requesttype, value, index, size);
1da177e4
LT
210
211 /* fill in the devrequest structure */
212 us->cr->bRequestType = requesttype;
213 us->cr->bRequest = request;
214 us->cr->wValue = cpu_to_le16(value);
215 us->cr->wIndex = cpu_to_le16(index);
216 us->cr->wLength = cpu_to_le16(size);
217
218 /* fill and submit the URB */
219 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
220 (unsigned char*) us->cr, data, size,
221 usb_stor_blocking_completion, NULL);
222 status = usb_stor_msg_common(us, timeout);
223
224 /* return the actual length of the data transferred if no error */
225 if (status == 0)
226 status = us->current_urb->actual_length;
227 return status;
228}
e6e244b6 229EXPORT_SYMBOL_GPL(usb_stor_control_msg);
1da177e4 230
f0183a33
FB
231/*
232 * This is a version of usb_clear_halt() that allows early termination and
1da177e4
LT
233 * doesn't read the status from the device -- this is because some devices
234 * crash their internal firmware when the status is requested after a halt.
235 *
236 * A definitive list of these 'bad' devices is too difficult to maintain or
237 * make complete enough to be useful. This problem was first observed on the
238 * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
239 * MacOS nor Windows checks the status after clearing a halt.
240 *
241 * Since many vendors in this space limit their testing to interoperability
242 * with these two OSes, specification violations like this one are common.
243 */
244int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
245{
246 int result;
247 int endp = usb_pipeendpoint(pipe);
248
249 if (usb_pipein (pipe))
250 endp |= USB_DIR_IN;
251
252 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
253 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
254 USB_ENDPOINT_HALT, endp,
255 NULL, 0, 3*HZ);
256
5203ad44 257 if (result >= 0)
3444b26a 258 usb_reset_endpoint(us->pusb_dev, endp);
1da177e4 259
191648d0 260 usb_stor_dbg(us, "result = %d\n", result);
1da177e4
LT
261 return result;
262}
e6e244b6 263EXPORT_SYMBOL_GPL(usb_stor_clear_halt);
1da177e4
LT
264
265
266/*
267 * Interpret the results of a URB transfer
268 *
269 * This function prints appropriate debugging messages, clears halts on
270 * non-control endpoints, and translates the status to the corresponding
271 * USB_STOR_XFER_xxx return code.
272 */
273static int interpret_urb_result(struct us_data *us, unsigned int pipe,
274 unsigned int length, int result, unsigned int partial)
275{
191648d0
JP
276 usb_stor_dbg(us, "Status code %d; transferred %u/%u\n",
277 result, partial, length);
1da177e4
LT
278 switch (result) {
279
280 /* no error code; did we send all the data? */
281 case 0:
282 if (partial != length) {
191648d0 283 usb_stor_dbg(us, "-- short transfer\n");
1da177e4
LT
284 return USB_STOR_XFER_SHORT;
285 }
286
191648d0 287 usb_stor_dbg(us, "-- transfer complete\n");
1da177e4
LT
288 return USB_STOR_XFER_GOOD;
289
290 /* stalled */
291 case -EPIPE:
f0183a33
FB
292 /*
293 * for control endpoints, (used by CB[I]) a stall indicates
294 * a failed command
295 */
1da177e4 296 if (usb_pipecontrol(pipe)) {
191648d0 297 usb_stor_dbg(us, "-- stall on control pipe\n");
1da177e4
LT
298 return USB_STOR_XFER_STALLED;
299 }
300
301 /* for other sorts of endpoint, clear the stall */
191648d0
JP
302 usb_stor_dbg(us, "clearing endpoint halt for pipe 0x%x\n",
303 pipe);
1da177e4
LT
304 if (usb_stor_clear_halt(us, pipe) < 0)
305 return USB_STOR_XFER_ERROR;
306 return USB_STOR_XFER_STALLED;
307
1da177e4
LT
308 /* babble - the device tried to send more than we wanted to read */
309 case -EOVERFLOW:
191648d0 310 usb_stor_dbg(us, "-- babble\n");
1da177e4
LT
311 return USB_STOR_XFER_LONG;
312
313 /* the transfer was cancelled by abort, disconnect, or timeout */
314 case -ECONNRESET:
191648d0 315 usb_stor_dbg(us, "-- transfer cancelled\n");
1da177e4
LT
316 return USB_STOR_XFER_ERROR;
317
318 /* short scatter-gather read transfer */
319 case -EREMOTEIO:
191648d0 320 usb_stor_dbg(us, "-- short read transfer\n");
1da177e4
LT
321 return USB_STOR_XFER_SHORT;
322
323 /* abort or disconnect in progress */
324 case -EIO:
191648d0 325 usb_stor_dbg(us, "-- abort or disconnect in progress\n");
1da177e4
LT
326 return USB_STOR_XFER_ERROR;
327
328 /* the catch-all error case */
329 default:
191648d0 330 usb_stor_dbg(us, "-- unknown error\n");
1da177e4
LT
331 return USB_STOR_XFER_ERROR;
332 }
333}
334
335/*
336 * Transfer one control message, without timeouts, but allowing early
337 * termination. Return codes are USB_STOR_XFER_xxx.
338 */
339int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
340 u8 request, u8 requesttype, u16 value, u16 index,
341 void *data, u16 size)
342{
343 int result;
344
191648d0
JP
345 usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
346 request, requesttype, value, index, size);
1da177e4
LT
347
348 /* fill in the devrequest structure */
349 us->cr->bRequestType = requesttype;
350 us->cr->bRequest = request;
351 us->cr->wValue = cpu_to_le16(value);
352 us->cr->wIndex = cpu_to_le16(index);
353 us->cr->wLength = cpu_to_le16(size);
354
355 /* fill and submit the URB */
356 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
357 (unsigned char*) us->cr, data, size,
358 usb_stor_blocking_completion, NULL);
359 result = usb_stor_msg_common(us, 0);
360
361 return interpret_urb_result(us, pipe, size, result,
362 us->current_urb->actual_length);
363}
e6e244b6 364EXPORT_SYMBOL_GPL(usb_stor_ctrl_transfer);
1da177e4
LT
365
366/*
367 * Receive one interrupt buffer, without timeouts, but allowing early
368 * termination. Return codes are USB_STOR_XFER_xxx.
369 *
370 * This routine always uses us->recv_intr_pipe as the pipe and
371 * us->ep_bInterval as the interrupt interval.
372 */
373static int usb_stor_intr_transfer(struct us_data *us, void *buf,
374 unsigned int length)
375{
376 int result;
377 unsigned int pipe = us->recv_intr_pipe;
378 unsigned int maxp;
379
191648d0 380 usb_stor_dbg(us, "xfer %u bytes\n", length);
1da177e4
LT
381
382 /* calculate the max packet size */
383 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
384 if (maxp > length)
385 maxp = length;
386
387 /* fill and submit the URB */
388 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
389 maxp, usb_stor_blocking_completion, NULL,
390 us->ep_bInterval);
391 result = usb_stor_msg_common(us, 0);
392
393 return interpret_urb_result(us, pipe, length, result,
394 us->current_urb->actual_length);
395}
396
397/*
398 * Transfer one buffer via bulk pipe, without timeouts, but allowing early
399 * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
400 * stalls during the transfer, the halt is automatically cleared.
401 */
402int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
403 void *buf, unsigned int length, unsigned int *act_len)
404{
405 int result;
406
191648d0 407 usb_stor_dbg(us, "xfer %u bytes\n", length);
1da177e4
LT
408
409 /* fill and submit the URB */
410 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
411 usb_stor_blocking_completion, NULL);
412 result = usb_stor_msg_common(us, 0);
413
414 /* store the actual length of the data transferred */
415 if (act_len)
416 *act_len = us->current_urb->actual_length;
417 return interpret_urb_result(us, pipe, length, result,
418 us->current_urb->actual_length);
419}
e6e244b6 420EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_buf);
1da177e4
LT
421
422/*
423 * Transfer a scatter-gather list via bulk transfer
424 *
425 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
426 * above, but it uses the usbcore scatter-gather library.
427 */
428static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
429 struct scatterlist *sg, int num_sg, unsigned int length,
430 unsigned int *act_len)
431{
432 int result;
433
543f7810
AS
434 /* don't submit s-g requests during abort processing */
435 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
1da177e4
LT
436 return USB_STOR_XFER_ERROR;
437
438 /* initialize the scatter-gather request block */
191648d0 439 usb_stor_dbg(us, "xfer %u bytes, %d entries\n", length, num_sg);
1da177e4 440 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
55acbda0 441 sg, num_sg, length, GFP_NOIO);
1da177e4 442 if (result) {
191648d0 443 usb_stor_dbg(us, "usb_sg_init returned %d\n", result);
1da177e4
LT
444 return USB_STOR_XFER_ERROR;
445 }
446
f0183a33
FB
447 /*
448 * since the block has been initialized successfully, it's now
449 * okay to cancel it
450 */
7e4d6c38 451 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
1da177e4 452
543f7810
AS
453 /* did an abort occur during the submission? */
454 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
1da177e4
LT
455
456 /* cancel the request, if it hasn't been cancelled already */
7e4d6c38 457 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
191648d0 458 usb_stor_dbg(us, "-- cancelling sg request\n");
1da177e4
LT
459 usb_sg_cancel(&us->current_sg);
460 }
461 }
462
463 /* wait for the completion of the transfer */
464 usb_sg_wait(&us->current_sg);
7e4d6c38 465 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
1da177e4
LT
466
467 result = us->current_sg.status;
468 if (act_len)
469 *act_len = us->current_sg.bytes;
470 return interpret_urb_result(us, pipe, length, result,
471 us->current_sg.bytes);
472}
473
6d416e61
BH
474/*
475 * Common used function. Transfer a complete command
476 * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
477 */
478int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
479 struct scsi_cmnd* srb)
480{
481 unsigned int partial;
482 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
483 scsi_sg_count(srb), scsi_bufflen(srb),
484 &partial);
485
486 scsi_set_resid(srb, scsi_bufflen(srb) - partial);
487 return result;
488}
e6e244b6 489EXPORT_SYMBOL_GPL(usb_stor_bulk_srb);
6d416e61 490
1da177e4
LT
491/*
492 * Transfer an entire SCSI command's worth of data payload over the bulk
493 * pipe.
494 *
495 * Note that this uses usb_stor_bulk_transfer_buf() and
496 * usb_stor_bulk_transfer_sglist() to achieve its goals --
497 * this function simply determines whether we're going to use
498 * scatter-gather or not, and acts appropriately.
499 */
500int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
501 void *buf, unsigned int length_left, int use_sg, int *residual)
502{
503 int result;
504 unsigned int partial;
505
506 /* are we scatter-gathering? */
507 if (use_sg) {
508 /* use the usb core scatter-gather primitives */
509 result = usb_stor_bulk_transfer_sglist(us, pipe,
510 (struct scatterlist *) buf, use_sg,
511 length_left, &partial);
512 length_left -= partial;
513 } else {
514 /* no scatter-gather, just make the request */
515 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
516 length_left, &partial);
517 length_left -= partial;
518 }
519
520 /* store the residual and return the error code */
521 if (residual)
522 *residual = length_left;
523 return result;
524}
e6e244b6 525EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_sg);
1da177e4
LT
526
527/***********************************************************************
528 * Transport routines
529 ***********************************************************************/
530
f0183a33
FB
531/*
532 * There are so many devices that report the capacity incorrectly,
25ff1c31
AS
533 * this routine was written to counteract some of the resulting
534 * problems.
535 */
536static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)
537{
538 struct gendisk *disk;
539 struct scsi_disk *sdkp;
540 u32 sector;
541
542 /* To Report "Medium Error: Record Not Found */
543 static unsigned char record_not_found[18] = {
544 [0] = 0x70, /* current error */
545 [2] = MEDIUM_ERROR, /* = 0x03 */
546 [7] = 0x0a, /* additional length */
547 [12] = 0x14 /* Record Not Found */
548 };
549
f0183a33
FB
550 /*
551 * If last-sector problems can't occur, whether because the
25ff1c31
AS
552 * capacity was already decremented or because the device is
553 * known to report the correct capacity, then we don't need
554 * to do anything.
555 */
556 if (!us->use_last_sector_hacks)
557 return;
558
559 /* Was this command a READ(10) or a WRITE(10)? */
560 if (srb->cmnd[0] != READ_10 && srb->cmnd[0] != WRITE_10)
561 goto done;
562
563 /* Did this command access the last sector? */
564 sector = (srb->cmnd[2] << 24) | (srb->cmnd[3] << 16) |
565 (srb->cmnd[4] << 8) | (srb->cmnd[5]);
566 disk = srb->request->rq_disk;
567 if (!disk)
568 goto done;
569 sdkp = scsi_disk(disk);
570 if (!sdkp)
571 goto done;
572 if (sector + 1 != sdkp->capacity)
573 goto done;
574
575 if (srb->result == SAM_STAT_GOOD && scsi_get_resid(srb) == 0) {
576
f0183a33
FB
577 /*
578 * The command succeeded. We know this device doesn't
0d020aae 579 * have the last-sector bug, so stop checking it.
25ff1c31 580 */
0d020aae 581 us->use_last_sector_hacks = 0;
25ff1c31
AS
582
583 } else {
f0183a33
FB
584 /*
585 * The command failed. Allow up to 3 retries in case this
25ff1c31
AS
586 * is some normal sort of failure. After that, assume the
587 * capacity is wrong and we're trying to access the sector
588 * beyond the end. Replace the result code and sense data
589 * with values that will cause the SCSI core to fail the
590 * command immediately, instead of going into an infinite
591 * (or even just a very long) retry loop.
592 */
593 if (++us->last_sector_retries < 3)
594 return;
595 srb->result = SAM_STAT_CHECK_CONDITION;
596 memcpy(srb->sense_buffer, record_not_found,
597 sizeof(record_not_found));
25ff1c31
AS
598 }
599
600 done:
f0183a33
FB
601 /*
602 * Don't reset the retry counter for TEST UNIT READY commands,
25ff1c31
AS
603 * because they get issued after device resets which might be
604 * caused by a failed last-sector access.
605 */
606 if (srb->cmnd[0] != TEST_UNIT_READY)
607 us->last_sector_retries = 0;
608}
609
f0183a33
FB
610/*
611 * Invoke the transport and basic error-handling/recovery methods
1da177e4
LT
612 *
613 * This is used by the protocol layers to actually send the message to
614 * the device and receive the response.
615 */
616void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
617{
618 int need_auto_sense;
619 int result;
620
621 /* send the command to the transport layer */
6d416e61 622 scsi_set_resid(srb, 0);
1da177e4
LT
623 result = us->transport(srb, us);
624
f0183a33
FB
625 /*
626 * if the command gets aborted by the higher layers, we need to
1da177e4
LT
627 * short-circuit all other processing
628 */
7e4d6c38 629 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
191648d0 630 usb_stor_dbg(us, "-- command was aborted\n");
4d07ef76
MD
631 srb->result = DID_ABORT << 16;
632 goto Handle_Errors;
1da177e4
LT
633 }
634
635 /* if there is a transport error, reset and don't auto-sense */
636 if (result == USB_STOR_TRANSPORT_ERROR) {
191648d0 637 usb_stor_dbg(us, "-- transport indicates error, resetting\n");
1da177e4 638 srb->result = DID_ERROR << 16;
4d07ef76 639 goto Handle_Errors;
1da177e4
LT
640 }
641
642 /* if the transport provided its own sense data, don't auto-sense */
643 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
644 srb->result = SAM_STAT_CHECK_CONDITION;
25ff1c31 645 last_sector_hacks(us, srb);
1da177e4
LT
646 return;
647 }
648
649 srb->result = SAM_STAT_GOOD;
650
f0183a33
FB
651 /*
652 * Determine if we need to auto-sense
1da177e4
LT
653 *
654 * I normally don't use a flag like this, but it's almost impossible
655 * to understand what's going on here if I don't.
656 */
657 need_auto_sense = 0;
658
659 /*
660 * If we're running the CB transport, which is incapable
661 * of determining status on its own, we will auto-sense
662 * unless the operation involved a data-in transfer. Devices
663 * can signal most data-in errors by stalling the bulk-in pipe.
664 */
8fa7fd74 665 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
1da177e4 666 srb->sc_data_direction != DMA_FROM_DEVICE) {
191648d0 667 usb_stor_dbg(us, "-- CB transport device requiring auto-sense\n");
1da177e4
LT
668 need_auto_sense = 1;
669 }
670
671 /*
672 * If we have a failure, we're going to do a REQUEST_SENSE
673 * automatically. Note that we differentiate between a command
674 * "failure" and an "error" in the transport mechanism.
675 */
676 if (result == USB_STOR_TRANSPORT_FAILED) {
191648d0 677 usb_stor_dbg(us, "-- transport indicates command failure\n");
1da177e4
LT
678 need_auto_sense = 1;
679 }
680
1537e0ad
BE
681 /*
682 * Determine if this device is SAT by seeing if the
683 * command executed successfully. Otherwise we'll have
684 * to wait for at least one CHECK_CONDITION to determine
685 * SANE_SENSE support
686 */
a0bb1081 687 if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
1537e0ad
BE
688 result == USB_STOR_TRANSPORT_GOOD &&
689 !(us->fflags & US_FL_SANE_SENSE) &&
a0bb1081
AS
690 !(us->fflags & US_FL_BAD_SENSE) &&
691 !(srb->cmnd[2] & 0x20))) {
191648d0 692 usb_stor_dbg(us, "-- SAT supported, increasing auto-sense\n");
1537e0ad
BE
693 us->fflags |= US_FL_SANE_SENSE;
694 }
695
1da177e4
LT
696 /*
697 * A short transfer on a command where we don't expect it
698 * is unusual, but it doesn't mean we need to auto-sense.
699 */
6d416e61 700 if ((scsi_get_resid(srb) > 0) &&
1da177e4
LT
701 !((srb->cmnd[0] == REQUEST_SENSE) ||
702 (srb->cmnd[0] == INQUIRY) ||
703 (srb->cmnd[0] == MODE_SENSE) ||
704 (srb->cmnd[0] == LOG_SENSE) ||
705 (srb->cmnd[0] == MODE_SENSE_10))) {
191648d0 706 usb_stor_dbg(us, "-- unexpectedly short transfer\n");
1da177e4
LT
707 }
708
709 /* Now, if we need to do the auto-sense, let's do it */
710 if (need_auto_sense) {
711 int temp_result;
dff6de73 712 struct scsi_eh_save ses;
1537e0ad 713 int sense_size = US_SENSE_SIZE;
e16da02f
LT
714 struct scsi_sense_hdr sshdr;
715 const u8 *scdd;
716 u8 fm_ili;
1537e0ad
BE
717
718 /* device supports and needs bigger sense buffer */
719 if (us->fflags & US_FL_SANE_SENSE)
720 sense_size = ~0;
b8430e1b 721Retry_Sense:
191648d0 722 usb_stor_dbg(us, "Issuing auto-REQUEST_SENSE\n");
1da177e4 723
1537e0ad 724 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
1da177e4
LT
725
726 /* FIXME: we must do the protocol translation here */
8fa7fd74
MN
727 if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
728 us->subclass == USB_SC_CYP_ATACB)
1da177e4
LT
729 srb->cmd_len = 6;
730 else
731 srb->cmd_len = 12;
732
1da177e4 733 /* issue the auto-sense command */
6d416e61 734 scsi_set_resid(srb, 0);
1da177e4
LT
735 temp_result = us->transport(us->srb, us);
736
737 /* let's clean up right away */
dff6de73 738 scsi_eh_restore_cmnd(srb, &ses);
1da177e4 739
7e4d6c38 740 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
191648d0 741 usb_stor_dbg(us, "-- auto-sense aborted\n");
4d07ef76 742 srb->result = DID_ABORT << 16;
a0bb1081
AS
743
744 /* If SANE_SENSE caused this problem, disable it */
745 if (sense_size != US_SENSE_SIZE) {
746 us->fflags &= ~US_FL_SANE_SENSE;
747 us->fflags |= US_FL_BAD_SENSE;
748 }
4d07ef76 749 goto Handle_Errors;
1da177e4 750 }
b8430e1b 751
f0183a33
FB
752 /*
753 * Some devices claim to support larger sense but fail when
b8430e1b
BH
754 * trying to request it. When a transport failure happens
755 * using US_FS_SANE_SENSE, we always retry with a standard
756 * (small) sense request. This fixes some USB GSM modems
757 */
758 if (temp_result == USB_STOR_TRANSPORT_FAILED &&
a0bb1081 759 sense_size != US_SENSE_SIZE) {
191648d0 760 usb_stor_dbg(us, "-- auto-sense failure, retry small sense\n");
b8430e1b 761 sense_size = US_SENSE_SIZE;
a0bb1081
AS
762 us->fflags &= ~US_FL_SANE_SENSE;
763 us->fflags |= US_FL_BAD_SENSE;
b8430e1b
BH
764 goto Retry_Sense;
765 }
766
767 /* Other failures */
1da177e4 768 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
191648d0 769 usb_stor_dbg(us, "-- auto-sense failure\n");
1da177e4 770
f0183a33
FB
771 /*
772 * we skip the reset if this happens to be a
1da177e4
LT
773 * multi-target device, since failure of an
774 * auto-sense is perfectly valid
775 */
1da177e4 776 srb->result = DID_ERROR << 16;
7e4d6c38 777 if (!(us->fflags & US_FL_SCM_MULT_TARG))
4d07ef76 778 goto Handle_Errors;
1da177e4
LT
779 return;
780 }
781
f0183a33
FB
782 /*
783 * If the sense data returned is larger than 18-bytes then we
1537e0ad
BE
784 * assume this device supports requesting more in the future.
785 * The response code must be 70h through 73h inclusive.
786 */
787 if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
788 !(us->fflags & US_FL_SANE_SENSE) &&
a0bb1081 789 !(us->fflags & US_FL_BAD_SENSE) &&
1537e0ad 790 (srb->sense_buffer[0] & 0x7C) == 0x70) {
191648d0 791 usb_stor_dbg(us, "-- SANE_SENSE support enabled\n");
1537e0ad
BE
792 us->fflags |= US_FL_SANE_SENSE;
793
f0183a33
FB
794 /*
795 * Indicate to the user that we truncated their sense
1537e0ad
BE
796 * because we didn't know it supported larger sense.
797 */
191648d0
JP
798 usb_stor_dbg(us, "-- Sense data truncated to %i from %i\n",
799 US_SENSE_SIZE,
800 srb->sense_buffer[7] + 8);
1537e0ad
BE
801 srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
802 }
803
e16da02f
LT
804 scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
805 &sshdr);
806
191648d0
JP
807 usb_stor_dbg(us, "-- Result from auto-sense is %d\n",
808 temp_result);
809 usb_stor_dbg(us, "-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
810 sshdr.response_code, sshdr.sense_key,
811 sshdr.asc, sshdr.ascq);
1da177e4 812#ifdef CONFIG_USB_STORAGE_DEBUG
191648d0 813 usb_stor_show_sense(us, sshdr.sense_key, sshdr.asc, sshdr.ascq);
1da177e4
LT
814#endif
815
816 /* set the result so the higher layers expect this data */
817 srb->result = SAM_STAT_CHECK_CONDITION;
818
e16da02f
LT
819 scdd = scsi_sense_desc_find(srb->sense_buffer,
820 SCSI_SENSE_BUFFERSIZE, 4);
821 fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
822
f0183a33
FB
823 /*
824 * We often get empty sense data. This could indicate that
f1a0743b
AS
825 * everything worked or that there was an unspecified
826 * problem. We have to decide which.
827 */
e16da02f
LT
828 if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
829 fm_ili == 0) {
f0183a33
FB
830 /*
831 * If things are really okay, then let's show that.
f1a0743b
AS
832 * Zero out the sense buffer so the higher layers
833 * won't realize we did an unsolicited auto-sense.
834 */
835 if (result == USB_STOR_TRANSPORT_GOOD) {
836 srb->result = SAM_STAT_GOOD;
837 srb->sense_buffer[0] = 0x0;
a4fd4a72
AS
838 }
839
840 /*
841 * ATA-passthru commands use sense data to report
842 * the command completion status, and often devices
843 * return Check Condition status when nothing is
844 * wrong.
845 */
846 else if (srb->cmnd[0] == ATA_16 ||
847 srb->cmnd[0] == ATA_12) {
848 /* leave the data alone */
849 }
f1a0743b 850
f0183a33
FB
851 /*
852 * If there was a problem, report an unspecified
f1a0743b
AS
853 * hardware error to prevent the higher layers from
854 * entering an infinite retry loop.
855 */
a4fd4a72 856 else {
f1a0743b 857 srb->result = DID_ERROR << 16;
e16da02f
LT
858 if ((sshdr.response_code & 0x72) == 0x72)
859 srb->sense_buffer[1] = HARDWARE_ERROR;
860 else
861 srb->sense_buffer[2] = HARDWARE_ERROR;
f1a0743b 862 }
1da177e4
LT
863 }
864 }
865
21c13a4f
AS
866 /*
867 * Some devices don't work or return incorrect data the first
868 * time they get a READ(10) command, or for the first READ(10)
869 * after a media change. If the INITIAL_READ10 flag is set,
870 * keep track of whether READ(10) commands succeed. If the
871 * previous one succeeded and this one failed, set the REDO_READ10
872 * flag to force a retry.
873 */
874 if (unlikely((us->fflags & US_FL_INITIAL_READ10) &&
875 srb->cmnd[0] == READ_10)) {
876 if (srb->result == SAM_STAT_GOOD) {
877 set_bit(US_FLIDX_READ10_WORKED, &us->dflags);
878 } else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) {
879 clear_bit(US_FLIDX_READ10_WORKED, &us->dflags);
880 set_bit(US_FLIDX_REDO_READ10, &us->dflags);
881 }
882
883 /*
884 * Next, if the REDO_READ10 flag is set, return a result
885 * code that will cause the SCSI core to retry the READ(10)
886 * command immediately.
887 */
888 if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) {
889 clear_bit(US_FLIDX_REDO_READ10, &us->dflags);
890 srb->result = DID_IMM_RETRY << 16;
891 srb->sense_buffer[0] = 0;
892 }
893 }
894
1da177e4 895 /* Did we transfer less than the minimum amount required? */
8bfa2472 896 if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
6d416e61 897 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
1c9fbafc 898 srb->result = DID_ERROR << 16;
1da177e4 899
25ff1c31 900 last_sector_hacks(us, srb);
1da177e4
LT
901 return;
902
f0183a33
FB
903 /*
904 * Error and abort processing: try to resynchronize with the device
4d07ef76 905 * by issuing a port reset. If that fails, try a class-specific
f0183a33
FB
906 * device reset.
907 */
4d07ef76
MD
908 Handle_Errors:
909
f0183a33
FB
910 /*
911 * Set the RESETTING bit, and clear the ABORTING bit so that
912 * the reset may proceed.
913 */
4d07ef76 914 scsi_lock(us_to_host(us));
7e4d6c38
AS
915 set_bit(US_FLIDX_RESETTING, &us->dflags);
916 clear_bit(US_FLIDX_ABORTING, &us->dflags);
4d07ef76
MD
917 scsi_unlock(us_to_host(us));
918
f0183a33
FB
919 /*
920 * We must release the device lock because the pre_reset routine
921 * will want to acquire it.
922 */
47104b0d 923 mutex_unlock(&us->dev_mutex);
4d07ef76 924 result = usb_stor_port_reset(us);
47104b0d
AS
925 mutex_lock(&us->dev_mutex);
926
4d07ef76
MD
927 if (result < 0) {
928 scsi_lock(us_to_host(us));
929 usb_stor_report_device_reset(us);
930 scsi_unlock(us_to_host(us));
1da177e4 931 us->transport_reset(us);
4d07ef76 932 }
7e4d6c38 933 clear_bit(US_FLIDX_RESETTING, &us->dflags);
25ff1c31 934 last_sector_hacks(us, srb);
1da177e4
LT
935}
936
937/* Stop the current URB transfer */
938void usb_stor_stop_transport(struct us_data *us)
939{
f0183a33
FB
940 /*
941 * If the state machine is blocked waiting for an URB,
1da177e4
LT
942 * let's wake it up. The test_and_clear_bit() call
943 * guarantees that if a URB has just been submitted,
f0183a33
FB
944 * it won't be cancelled more than once.
945 */
7e4d6c38 946 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
191648d0 947 usb_stor_dbg(us, "-- cancelling URB\n");
1da177e4
LT
948 usb_unlink_urb(us->current_urb);
949 }
950
951 /* If we are waiting for a scatter-gather operation, cancel it. */
7e4d6c38 952 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
191648d0 953 usb_stor_dbg(us, "-- cancelling sg request\n");
1da177e4
LT
954 usb_sg_cancel(&us->current_sg);
955 }
956}
957
958/*
64648a9d 959 * Control/Bulk and Control/Bulk/Interrupt transport
1da177e4
LT
960 */
961
64648a9d 962int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
1da177e4 963{
6d416e61 964 unsigned int transfer_length = scsi_bufflen(srb);
1da177e4
LT
965 unsigned int pipe = 0;
966 int result;
967
968 /* COMMAND STAGE */
969 /* let's send the command via the control pipe */
2ce9d227
PV
970 /*
971 * Command is sometime (f.e. after scsi_eh_prep_cmnd) on the stack.
972 * Stack may be vmallocated. So no DMA for us. Make a copy.
973 */
974 memcpy(us->iobuf, srb->cmnd, srb->cmd_len);
1da177e4
LT
975 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
976 US_CBI_ADSC,
977 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
2ce9d227 978 us->ifnum, us->iobuf, srb->cmd_len);
1da177e4
LT
979
980 /* check the return code for the command */
191648d0
JP
981 usb_stor_dbg(us, "Call to usb_stor_ctrl_transfer() returned %d\n",
982 result);
1da177e4
LT
983
984 /* if we stalled the command, it means command failed */
985 if (result == USB_STOR_XFER_STALLED) {
986 return USB_STOR_TRANSPORT_FAILED;
987 }
988
989 /* Uh oh... serious problem here */
990 if (result != USB_STOR_XFER_GOOD) {
991 return USB_STOR_TRANSPORT_ERROR;
992 }
993
994 /* DATA STAGE */
995 /* transfer the data payload for this command, if one exists*/
996 if (transfer_length) {
997 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
998 us->recv_bulk_pipe : us->send_bulk_pipe;
6d416e61 999 result = usb_stor_bulk_srb(us, pipe, srb);
191648d0 1000 usb_stor_dbg(us, "CBI data stage result is 0x%x\n", result);
1da177e4
LT
1001
1002 /* if we stalled the data transfer it means command failed */
1003 if (result == USB_STOR_XFER_STALLED)
1004 return USB_STOR_TRANSPORT_FAILED;
1005 if (result > USB_STOR_XFER_STALLED)
1006 return USB_STOR_TRANSPORT_ERROR;
1007 }
1008
1009 /* STATUS STAGE */
64648a9d 1010
f0183a33
FB
1011 /*
1012 * NOTE: CB does not have a status stage. Silly, I know. So
64648a9d
AS
1013 * we have to catch this at a higher level.
1014 */
8fa7fd74 1015 if (us->protocol != USB_PR_CBI)
64648a9d
AS
1016 return USB_STOR_TRANSPORT_GOOD;
1017
1da177e4 1018 result = usb_stor_intr_transfer(us, us->iobuf, 2);
191648d0
JP
1019 usb_stor_dbg(us, "Got interrupt data (0x%x, 0x%x)\n",
1020 us->iobuf[0], us->iobuf[1]);
1da177e4
LT
1021 if (result != USB_STOR_XFER_GOOD)
1022 return USB_STOR_TRANSPORT_ERROR;
1023
f0183a33
FB
1024 /*
1025 * UFI gives us ASC and ASCQ, like a request sense
1da177e4
LT
1026 *
1027 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
1028 * devices, so we ignore the information for those commands. Note
1029 * that this means we could be ignoring a real error on these
1030 * commands, but that can't be helped.
1031 */
8fa7fd74 1032 if (us->subclass == USB_SC_UFI) {
1da177e4
LT
1033 if (srb->cmnd[0] == REQUEST_SENSE ||
1034 srb->cmnd[0] == INQUIRY)
1035 return USB_STOR_TRANSPORT_GOOD;
1036 if (us->iobuf[0])
1037 goto Failed;
1038 return USB_STOR_TRANSPORT_GOOD;
1039 }
1040
f0183a33
FB
1041 /*
1042 * If not UFI, we interpret the data as a result code
1da177e4
LT
1043 * The first byte should always be a 0x0.
1044 *
1045 * Some bogus devices don't follow that rule. They stuff the ASC
1046 * into the first byte -- so if it's non-zero, call it a failure.
1047 */
1048 if (us->iobuf[0]) {
191648d0
JP
1049 usb_stor_dbg(us, "CBI IRQ data showed reserved bType 0x%x\n",
1050 us->iobuf[0]);
1da177e4
LT
1051 goto Failed;
1052
1053 }
1054
1055 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
1056 switch (us->iobuf[1] & 0x0F) {
1057 case 0x00:
1058 return USB_STOR_TRANSPORT_GOOD;
1059 case 0x01:
1060 goto Failed;
1061 }
1062 return USB_STOR_TRANSPORT_ERROR;
1063
f0183a33
FB
1064 /*
1065 * the CBI spec requires that the bulk pipe must be cleared
1da177e4
LT
1066 * following any data-in/out command failure (section 2.4.3.1.3)
1067 */
1068 Failed:
1069 if (pipe)
1070 usb_stor_clear_halt(us, pipe);
1071 return USB_STOR_TRANSPORT_FAILED;
1072}
e6e244b6 1073EXPORT_SYMBOL_GPL(usb_stor_CB_transport);
1da177e4 1074
1da177e4
LT
1075/*
1076 * Bulk only transport
1077 */
1078
1079/* Determine what the maximum LUN supported is */
1080int usb_stor_Bulk_max_lun(struct us_data *us)
1081{
1082 int result;
1083
1084 /* issue the command */
b876aef7 1085 us->iobuf[0] = 0;
1da177e4
LT
1086 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
1087 US_BULK_GET_MAX_LUN,
1088 USB_DIR_IN | USB_TYPE_CLASS |
1089 USB_RECIP_INTERFACE,
7a777919 1090 0, us->ifnum, us->iobuf, 1, 10*HZ);
1da177e4 1091
191648d0
JP
1092 usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
1093 result, us->iobuf[0]);
1da177e4 1094
55dc68c0
MK
1095 /*
1096 * If we have a successful request, return the result if valid. The
1097 * CBW LUN field is 4 bits wide, so the value reported by the device
1098 * should fit into that.
1099 */
1100 if (result > 0) {
1101 if (us->iobuf[0] < 16) {
1102 return us->iobuf[0];
1103 } else {
1104 dev_info(&us->pusb_intf->dev,
1105 "Max LUN %d is not valid, using 0 instead",
1106 us->iobuf[0]);
1107 }
1108 }
1da177e4 1109
1da177e4
LT
1110 /*
1111 * Some devices don't like GetMaxLUN. They may STALL the control
1112 * pipe, they may return a zero-length result, they may do nothing at
1113 * all and timeout, or they may fail in even more bizarrely creative
1114 * ways. In these cases the best approach is to use the default
1115 * value: only one LUN.
1116 */
1117 return 0;
1118}
1119
1120int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
1121{
1122 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1123 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
6d416e61 1124 unsigned int transfer_length = scsi_bufflen(srb);
1da177e4
LT
1125 unsigned int residue;
1126 int result;
1127 int fake_sense = 0;
1128 unsigned int cswlen;
1129 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
1130
1131 /* Take care of BULK32 devices; set extra byte to 0 */
7e4d6c38 1132 if (unlikely(us->fflags & US_FL_BULK32)) {
1da177e4
LT
1133 cbwlen = 32;
1134 us->iobuf[31] = 0;
1135 }
1136
1137 /* set up the command wrapper */
1138 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1139 bcb->DataTransferLength = cpu_to_le32(transfer_length);
b8db6d64
SAS
1140 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ?
1141 US_BULK_FLAG_IN : 0;
0f64e078 1142 bcb->Tag = ++us->tag;
1da177e4 1143 bcb->Lun = srb->device->lun;
7e4d6c38 1144 if (us->fflags & US_FL_SCM_MULT_TARG)
1da177e4
LT
1145 bcb->Lun |= srb->device->id << 4;
1146 bcb->Length = srb->cmd_len;
1147
1148 /* copy the command payload */
1149 memset(bcb->CDB, 0, sizeof(bcb->CDB));
1150 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
1151
1152 /* send it to out endpoint */
191648d0
JP
1153 usb_stor_dbg(us, "Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
1154 le32_to_cpu(bcb->Signature), bcb->Tag,
1155 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
1156 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
1157 bcb->Length);
1da177e4
LT
1158 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
1159 bcb, cbwlen, NULL);
191648d0 1160 usb_stor_dbg(us, "Bulk command transfer result=%d\n", result);
1da177e4
LT
1161 if (result != USB_STOR_XFER_GOOD)
1162 return USB_STOR_TRANSPORT_ERROR;
1163
1164 /* DATA STAGE */
1165 /* send/receive data payload, if there is any */
1166
f0183a33
FB
1167 /*
1168 * Some USB-IDE converter chips need a 100us delay between the
1da177e4 1169 * command phase and the data phase. Some devices need a little
f0183a33
FB
1170 * more than that, probably because of clock rate inaccuracies.
1171 */
7e4d6c38 1172 if (unlikely(us->fflags & US_FL_GO_SLOW))
e616b39a 1173 usleep_range(125, 150);
1da177e4
LT
1174
1175 if (transfer_length) {
1176 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
1177 us->recv_bulk_pipe : us->send_bulk_pipe;
6d416e61 1178 result = usb_stor_bulk_srb(us, pipe, srb);
191648d0 1179 usb_stor_dbg(us, "Bulk data transfer result 0x%x\n", result);
1da177e4
LT
1180 if (result == USB_STOR_XFER_ERROR)
1181 return USB_STOR_TRANSPORT_ERROR;
1182
f0183a33
FB
1183 /*
1184 * If the device tried to send back more data than the
1da177e4
LT
1185 * amount requested, the spec requires us to transfer
1186 * the CSW anyway. Since there's no point retrying the
1187 * the command, we'll return fake sense data indicating
1188 * Illegal Request, Invalid Field in CDB.
1189 */
1190 if (result == USB_STOR_XFER_LONG)
1191 fake_sense = 1;
93c9bf4d
AS
1192
1193 /*
1194 * Sometimes a device will mistakenly skip the data phase
1195 * and go directly to the status phase without sending a
1196 * zero-length packet. If we get a 13-byte response here,
1197 * check whether it really is a CSW.
1198 */
1199 if (result == USB_STOR_XFER_SHORT &&
1200 srb->sc_data_direction == DMA_FROM_DEVICE &&
1201 transfer_length - scsi_get_resid(srb) ==
1202 US_BULK_CS_WRAP_LEN) {
1203 struct scatterlist *sg = NULL;
1204 unsigned int offset = 0;
1205
1206 if (usb_stor_access_xfer_buf((unsigned char *) bcs,
1207 US_BULK_CS_WRAP_LEN, srb, &sg,
1208 &offset, FROM_XFER_BUF) ==
1209 US_BULK_CS_WRAP_LEN &&
1210 bcs->Signature ==
1211 cpu_to_le32(US_BULK_CS_SIGN)) {
1212 usb_stor_dbg(us, "Device skipped data phase\n");
1213 scsi_set_resid(srb, transfer_length);
1214 goto skipped_data_phase;
1215 }
1216 }
1da177e4
LT
1217 }
1218
f0183a33
FB
1219 /*
1220 * See flow chart on pg 15 of the Bulk Only Transport spec for
1da177e4
LT
1221 * an explanation of how this code works.
1222 */
1223
1224 /* get CSW for device status */
191648d0 1225 usb_stor_dbg(us, "Attempting to get CSW...\n");
1da177e4
LT
1226 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1227 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1228
f0183a33
FB
1229 /*
1230 * Some broken devices add unnecessary zero-length packets to the
1da177e4
LT
1231 * end of their data transfers. Such packets show up as 0-length
1232 * CSWs. If we encounter such a thing, try to read the CSW again.
1233 */
1234 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
191648d0 1235 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
1da177e4
LT
1236 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1237 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1238 }
1239
1240 /* did the attempt to read the CSW fail? */
1241 if (result == USB_STOR_XFER_STALLED) {
1242
1243 /* get the status again */
191648d0 1244 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
1da177e4
LT
1245 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1246 bcs, US_BULK_CS_WRAP_LEN, NULL);
1247 }
1248
1249 /* if we still have a failure at this point, we're in trouble */
191648d0 1250 usb_stor_dbg(us, "Bulk status result = %d\n", result);
1da177e4
LT
1251 if (result != USB_STOR_XFER_GOOD)
1252 return USB_STOR_TRANSPORT_ERROR;
1253
93c9bf4d 1254 skipped_data_phase:
1da177e4
LT
1255 /* check bulk status */
1256 residue = le32_to_cpu(bcs->Residue);
191648d0
JP
1257 usb_stor_dbg(us, "Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1258 le32_to_cpu(bcs->Signature), bcs->Tag,
1259 residue, bcs->Status);
7e4d6c38 1260 if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
cc36bdd4 1261 bcs->Status > US_BULK_STAT_PHASE) {
191648d0 1262 usb_stor_dbg(us, "Bulk logical error\n");
1da177e4
LT
1263 return USB_STOR_TRANSPORT_ERROR;
1264 }
1265
f0183a33
FB
1266 /*
1267 * Some broken devices report odd signatures, so we do not check them
1da177e4
LT
1268 * for validity against the spec. We store the first one we see,
1269 * and check subsequent transfers for validity against this signature.
1270 */
1271 if (!us->bcs_signature) {
1272 us->bcs_signature = bcs->Signature;
1273 if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
191648d0
JP
1274 usb_stor_dbg(us, "Learnt BCS signature 0x%08X\n",
1275 le32_to_cpu(us->bcs_signature));
1da177e4 1276 } else if (bcs->Signature != us->bcs_signature) {
191648d0
JP
1277 usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
1278 le32_to_cpu(bcs->Signature),
1279 le32_to_cpu(us->bcs_signature));
1da177e4
LT
1280 return USB_STOR_TRANSPORT_ERROR;
1281 }
1282
f0183a33
FB
1283 /*
1284 * try to compute the actual residue, based on how much data
1285 * was really transferred and what the device tells us
1286 */
59f4ff2e
AS
1287 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
1288
f0183a33
FB
1289 /*
1290 * Heuristically detect devices that generate bogus residues
59f4ff2e
AS
1291 * by seeing what happens with INQUIRY and READ CAPACITY
1292 * commands.
1293 */
1294 if (bcs->Status == US_BULK_STAT_OK &&
1295 scsi_get_resid(srb) == 0 &&
1296 ((srb->cmnd[0] == INQUIRY &&
1297 transfer_length == 36) ||
1298 (srb->cmnd[0] == READ_CAPACITY &&
1299 transfer_length == 8))) {
1300 us->fflags |= US_FL_IGNORE_RESIDUE;
1301
1302 } else {
1da177e4 1303 residue = min(residue, transfer_length);
6d416e61
BH
1304 scsi_set_resid(srb, max(scsi_get_resid(srb),
1305 (int) residue));
1da177e4
LT
1306 }
1307 }
1308
1309 /* based on the status code, we report good or bad */
1310 switch (bcs->Status) {
1311 case US_BULK_STAT_OK:
1312 /* device babbled -- return fake sense data */
1313 if (fake_sense) {
1314 memcpy(srb->sense_buffer,
1315 usb_stor_sense_invalidCDB,
1316 sizeof(usb_stor_sense_invalidCDB));
1317 return USB_STOR_TRANSPORT_NO_SENSE;
1318 }
1319
1320 /* command good -- note that data could be short */
1321 return USB_STOR_TRANSPORT_GOOD;
1322
1323 case US_BULK_STAT_FAIL:
1324 /* command failed */
1325 return USB_STOR_TRANSPORT_FAILED;
1326
1327 case US_BULK_STAT_PHASE:
f0183a33
FB
1328 /*
1329 * phase error -- note that a transport reset will be
1da177e4
LT
1330 * invoked by the invoke_transport() function
1331 */
1332 return USB_STOR_TRANSPORT_ERROR;
1333 }
1334
1335 /* we should never get here, but if we do, we're in trouble */
1336 return USB_STOR_TRANSPORT_ERROR;
1337}
e6e244b6 1338EXPORT_SYMBOL_GPL(usb_stor_Bulk_transport);
1da177e4
LT
1339
1340/***********************************************************************
1341 * Reset routines
1342 ***********************************************************************/
1343
f0183a33
FB
1344/*
1345 * This is the common part of the device reset code.
1da177e4
LT
1346 *
1347 * It's handy that every transport mechanism uses the control endpoint for
1348 * resets.
1349 *
5203ad44 1350 * Basically, we send a reset with a 5-second timeout, so we don't get
1da177e4
LT
1351 * jammed attempting to do the reset.
1352 */
1353static int usb_stor_reset_common(struct us_data *us,
1354 u8 request, u8 requesttype,
1355 u16 value, u16 index, void *data, u16 size)
1356{
1357 int result;
1358 int result2;
1da177e4 1359
7e4d6c38 1360 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
191648d0 1361 usb_stor_dbg(us, "No reset during disconnect\n");
4d07ef76
MD
1362 return -EIO;
1363 }
1da177e4 1364
1da177e4
LT
1365 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1366 request, requesttype, value, index, data, size,
5203ad44 1367 5*HZ);
1da177e4 1368 if (result < 0) {
191648d0 1369 usb_stor_dbg(us, "Soft reset failed: %d\n", result);
4d07ef76 1370 return result;
1da177e4
LT
1371 }
1372
f0183a33
FB
1373 /*
1374 * Give the device some time to recover from the reset,
1375 * but don't delay disconnect processing.
1376 */
7e4d6c38
AS
1377 wait_event_interruptible_timeout(us->delay_wait,
1378 test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
1379 HZ*6);
1380 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
191648d0 1381 usb_stor_dbg(us, "Reset interrupted by disconnect\n");
4d07ef76 1382 return -EIO;
1da177e4
LT
1383 }
1384
191648d0 1385 usb_stor_dbg(us, "Soft reset: clearing bulk-in endpoint halt\n");
1da177e4
LT
1386 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1387
191648d0 1388 usb_stor_dbg(us, "Soft reset: clearing bulk-out endpoint halt\n");
1da177e4
LT
1389 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1390
5203ad44
MD
1391 /* return a result code based on the result of the clear-halts */
1392 if (result >= 0)
1393 result = result2;
4d07ef76 1394 if (result < 0)
191648d0 1395 usb_stor_dbg(us, "Soft reset failed\n");
4d07ef76 1396 else
191648d0 1397 usb_stor_dbg(us, "Soft reset done\n");
4d07ef76 1398 return result;
1da177e4
LT
1399}
1400
f0183a33 1401/* This issues a CB[I] Reset to the device in question */
1da177e4
LT
1402#define CB_RESET_CMD_SIZE 12
1403
1404int usb_stor_CB_reset(struct us_data *us)
1405{
1da177e4
LT
1406 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1407 us->iobuf[0] = SEND_DIAGNOSTIC;
1408 us->iobuf[1] = 4;
1409 return usb_stor_reset_common(us, US_CBI_ADSC,
1410 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1411 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1412}
e6e244b6 1413EXPORT_SYMBOL_GPL(usb_stor_CB_reset);
1da177e4 1414
f0183a33
FB
1415/*
1416 * This issues a Bulk-only Reset to the device in question, including
1da177e4
LT
1417 * clearing the subsequent endpoint halts that may occur.
1418 */
1419int usb_stor_Bulk_reset(struct us_data *us)
1420{
1da177e4
LT
1421 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1422 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1423 0, us->ifnum, NULL, 0);
1424}
e6e244b6 1425EXPORT_SYMBOL_GPL(usb_stor_Bulk_reset);
4d07ef76 1426
f0183a33
FB
1427/*
1428 * Issue a USB port reset to the device. The caller must not hold
47104b0d
AS
1429 * us->dev_mutex.
1430 */
4d07ef76
MD
1431int usb_stor_port_reset(struct us_data *us)
1432{
011b15df 1433 int result;
4d07ef76 1434
5d398779 1435 /*for these devices we must use the class specific method */
7fda953f 1436 if (us->pusb_dev->quirks & USB_QUIRK_RESET)
5d398779
ON
1437 return -EPERM;
1438
011b15df 1439 result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
47104b0d 1440 if (result < 0)
191648d0
JP
1441 usb_stor_dbg(us, "unable to lock device for reset: %d\n",
1442 result);
47104b0d
AS
1443 else {
1444 /* Were we disconnected while waiting for the lock? */
7e4d6c38 1445 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
47104b0d 1446 result = -EIO;
191648d0 1447 usb_stor_dbg(us, "No reset during disconnect\n");
4d07ef76 1448 } else {
742120c6 1449 result = usb_reset_device(us->pusb_dev);
191648d0
JP
1450 usb_stor_dbg(us, "usb_reset_device returns %d\n",
1451 result);
4d07ef76 1452 }
011b15df 1453 usb_unlock_device(us->pusb_dev);
4d07ef76
MD
1454 }
1455 return result;
1456}