1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/sched.h>
35#include <linux/gfp.h>
36#include <linux/errno.h>
37#include <linux/export.h>
38
39#include <linux/usb/quirks.h>
40
41#include <scsi/scsi.h>
42#include <scsi/scsi_eh.h>
43#include <scsi/scsi_device.h>
44
45#include "usb.h"
46#include "transport.h"
47#include "protocol.h"
48#include "scsiglue.h"
49#include "debug.h"
50
51#include <linux/blkdev.h>
52#include "../../scsi/sd.h"
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104static void usb_stor_blocking_completion(struct urb *urb)
105{
106 struct completion *urb_done_ptr = urb->context;
107
108 complete(urb_done_ptr);
109}
110
111
112
113
114
115
116
117
118static int usb_stor_msg_common(struct us_data *us, int timeout)
119{
120 struct completion urb_done;
121 long timeleft;
122 int status;
123
124
125 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
126 return -EIO;
127
128
129 init_completion(&urb_done);
130
131
132 us->current_urb->context = &urb_done;
133 us->current_urb->transfer_flags = 0;
134
135
136
137
138
139
140
141 if (us->current_urb->transfer_buffer == us->iobuf)
142 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
143 us->current_urb->transfer_dma = us->iobuf_dma;
144
145
146 status = usb_submit_urb(us->current_urb, GFP_NOIO);
147 if (status) {
148
149 return status;
150 }
151
152
153
154
155
156 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
157
158
159 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
160
161
162 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
163 usb_stor_dbg(us, "-- cancelling URB\n");
164 usb_unlink_urb(us->current_urb);
165 }
166 }
167
168
169 timeleft = wait_for_completion_interruptible_timeout(
170 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
171
172 clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
173
174 if (timeleft <= 0) {
175 usb_stor_dbg(us, "%s -- cancelling URB\n",
176 timeleft == 0 ? "Timeout" : "Signal");
177 usb_kill_urb(us->current_urb);
178 }
179
180
181 return us->current_urb->status;
182}
183
184
185
186
187
188int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
189 u8 request, u8 requesttype, u16 value, u16 index,
190 void *data, u16 size, int timeout)
191{
192 int status;
193
194 usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
195 request, requesttype, value, index, size);
196
197
198 us->cr->bRequestType = requesttype;
199 us->cr->bRequest = request;
200 us->cr->wValue = cpu_to_le16(value);
201 us->cr->wIndex = cpu_to_le16(index);
202 us->cr->wLength = cpu_to_le16(size);
203
204
205 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
206 (unsigned char*) us->cr, data, size,
207 usb_stor_blocking_completion, NULL);
208 status = usb_stor_msg_common(us, timeout);
209
210
211 if (status == 0)
212 status = us->current_urb->actual_length;
213 return status;
214}
215EXPORT_SYMBOL_GPL(usb_stor_control_msg);
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
231{
232 int result;
233 int endp = usb_pipeendpoint(pipe);
234
235 if (usb_pipein (pipe))
236 endp |= USB_DIR_IN;
237
238 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
239 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
240 USB_ENDPOINT_HALT, endp,
241 NULL, 0, 3*HZ);
242
243 if (result >= 0)
244 usb_reset_endpoint(us->pusb_dev, endp);
245
246 usb_stor_dbg(us, "result = %d\n", result);
247 return result;
248}
249EXPORT_SYMBOL_GPL(usb_stor_clear_halt);
250
251
252
253
254
255
256
257
258
259static int interpret_urb_result(struct us_data *us, unsigned int pipe,
260 unsigned int length, int result, unsigned int partial)
261{
262 usb_stor_dbg(us, "Status code %d; transferred %u/%u\n",
263 result, partial, length);
264 switch (result) {
265
266
267 case 0:
268 if (partial != length) {
269 usb_stor_dbg(us, "-- short transfer\n");
270 return USB_STOR_XFER_SHORT;
271 }
272
273 usb_stor_dbg(us, "-- transfer complete\n");
274 return USB_STOR_XFER_GOOD;
275
276
277 case -EPIPE:
278
279
280
281
282 if (usb_pipecontrol(pipe)) {
283 usb_stor_dbg(us, "-- stall on control pipe\n");
284 return USB_STOR_XFER_STALLED;
285 }
286
287
288 usb_stor_dbg(us, "clearing endpoint halt for pipe 0x%x\n",
289 pipe);
290 if (usb_stor_clear_halt(us, pipe) < 0)
291 return USB_STOR_XFER_ERROR;
292 return USB_STOR_XFER_STALLED;
293
294
295 case -EOVERFLOW:
296 usb_stor_dbg(us, "-- babble\n");
297 return USB_STOR_XFER_LONG;
298
299
300 case -ECONNRESET:
301 usb_stor_dbg(us, "-- transfer cancelled\n");
302 return USB_STOR_XFER_ERROR;
303
304
305 case -EREMOTEIO:
306 usb_stor_dbg(us, "-- short read transfer\n");
307 return USB_STOR_XFER_SHORT;
308
309
310 case -EIO:
311 usb_stor_dbg(us, "-- abort or disconnect in progress\n");
312 return USB_STOR_XFER_ERROR;
313
314
315 default:
316 usb_stor_dbg(us, "-- unknown error\n");
317 return USB_STOR_XFER_ERROR;
318 }
319}
320
321
322
323
324
325int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
326 u8 request, u8 requesttype, u16 value, u16 index,
327 void *data, u16 size)
328{
329 int result;
330
331 usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
332 request, requesttype, value, index, size);
333
334
335 us->cr->bRequestType = requesttype;
336 us->cr->bRequest = request;
337 us->cr->wValue = cpu_to_le16(value);
338 us->cr->wIndex = cpu_to_le16(index);
339 us->cr->wLength = cpu_to_le16(size);
340
341
342 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
343 (unsigned char*) us->cr, data, size,
344 usb_stor_blocking_completion, NULL);
345 result = usb_stor_msg_common(us, 0);
346
347 return interpret_urb_result(us, pipe, size, result,
348 us->current_urb->actual_length);
349}
350EXPORT_SYMBOL_GPL(usb_stor_ctrl_transfer);
351
352
353
354
355
356
357
358
359static int usb_stor_intr_transfer(struct us_data *us, void *buf,
360 unsigned int length)
361{
362 int result;
363 unsigned int pipe = us->recv_intr_pipe;
364 unsigned int maxp;
365
366 usb_stor_dbg(us, "xfer %u bytes\n", length);
367
368
369 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
370 if (maxp > length)
371 maxp = length;
372
373
374 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
375 maxp, usb_stor_blocking_completion, NULL,
376 us->ep_bInterval);
377 result = usb_stor_msg_common(us, 0);
378
379 return interpret_urb_result(us, pipe, length, result,
380 us->current_urb->actual_length);
381}
382
383
384
385
386
387
388int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
389 void *buf, unsigned int length, unsigned int *act_len)
390{
391 int result;
392
393 usb_stor_dbg(us, "xfer %u bytes\n", length);
394
395
396 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
397 usb_stor_blocking_completion, NULL);
398 result = usb_stor_msg_common(us, 0);
399
400
401 if (act_len)
402 *act_len = us->current_urb->actual_length;
403 return interpret_urb_result(us, pipe, length, result,
404 us->current_urb->actual_length);
405}
406EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_buf);
407
408
409
410
411
412
413
414static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
415 struct scatterlist *sg, int num_sg, unsigned int length,
416 unsigned int *act_len)
417{
418 int result;
419
420
421 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
422 return USB_STOR_XFER_ERROR;
423
424
425 usb_stor_dbg(us, "xfer %u bytes, %d entries\n", length, num_sg);
426 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
427 sg, num_sg, length, GFP_NOIO);
428 if (result) {
429 usb_stor_dbg(us, "usb_sg_init returned %d\n", result);
430 return USB_STOR_XFER_ERROR;
431 }
432
433
434
435
436
437 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
438
439
440 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
441
442
443 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
444 usb_stor_dbg(us, "-- cancelling sg request\n");
445 usb_sg_cancel(&us->current_sg);
446 }
447 }
448
449
450 usb_sg_wait(&us->current_sg);
451 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
452
453 result = us->current_sg.status;
454 if (act_len)
455 *act_len = us->current_sg.bytes;
456 return interpret_urb_result(us, pipe, length, result,
457 us->current_sg.bytes);
458}
459
460
461
462
463
464int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
465 struct scsi_cmnd* srb)
466{
467 unsigned int partial;
468 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
469 scsi_sg_count(srb), scsi_bufflen(srb),
470 &partial);
471
472 scsi_set_resid(srb, scsi_bufflen(srb) - partial);
473 return result;
474}
475EXPORT_SYMBOL_GPL(usb_stor_bulk_srb);
476
477
478
479
480
481
482
483
484
485
486int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
487 void *buf, unsigned int length_left, int use_sg, int *residual)
488{
489 int result;
490 unsigned int partial;
491
492
493 if (use_sg) {
494
495 result = usb_stor_bulk_transfer_sglist(us, pipe,
496 (struct scatterlist *) buf, use_sg,
497 length_left, &partial);
498 length_left -= partial;
499 } else {
500
501 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
502 length_left, &partial);
503 length_left -= partial;
504 }
505
506
507 if (residual)
508 *residual = length_left;
509 return result;
510}
511EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_sg);
512
513
514
515
516
517
518
519
520
521
522static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)
523{
524 struct gendisk *disk;
525 struct scsi_disk *sdkp;
526 u32 sector;
527
528
529 static unsigned char record_not_found[18] = {
530 [0] = 0x70,
531 [2] = MEDIUM_ERROR,
532 [7] = 0x0a,
533 [12] = 0x14
534 };
535
536
537
538
539
540
541
542 if (!us->use_last_sector_hacks)
543 return;
544
545
546 if (srb->cmnd[0] != READ_10 && srb->cmnd[0] != WRITE_10)
547 goto done;
548
549
550 sector = (srb->cmnd[2] << 24) | (srb->cmnd[3] << 16) |
551 (srb->cmnd[4] << 8) | (srb->cmnd[5]);
552 disk = srb->request->rq_disk;
553 if (!disk)
554 goto done;
555 sdkp = scsi_disk(disk);
556 if (!sdkp)
557 goto done;
558 if (sector + 1 != sdkp->capacity)
559 goto done;
560
561 if (srb->result == SAM_STAT_GOOD && scsi_get_resid(srb) == 0) {
562
563
564
565
566
567 us->use_last_sector_hacks = 0;
568
569 } else {
570
571
572
573
574
575
576
577
578
579 if (++us->last_sector_retries < 3)
580 return;
581 srb->result = SAM_STAT_CHECK_CONDITION;
582 memcpy(srb->sense_buffer, record_not_found,
583 sizeof(record_not_found));
584 }
585
586 done:
587
588
589
590
591
592 if (srb->cmnd[0] != TEST_UNIT_READY)
593 us->last_sector_retries = 0;
594}
595
596
597
598
599
600
601
602void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
603{
604 int need_auto_sense;
605 int result;
606
607
608 scsi_set_resid(srb, 0);
609 result = us->transport(srb, us);
610
611
612
613
614
615 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
616 usb_stor_dbg(us, "-- command was aborted\n");
617 srb->result = DID_ABORT << 16;
618 goto Handle_Errors;
619 }
620
621
622 if (result == USB_STOR_TRANSPORT_ERROR) {
623 usb_stor_dbg(us, "-- transport indicates error, resetting\n");
624 srb->result = DID_ERROR << 16;
625 goto Handle_Errors;
626 }
627
628
629 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
630 srb->result = SAM_STAT_CHECK_CONDITION;
631 last_sector_hacks(us, srb);
632 return;
633 }
634
635 srb->result = SAM_STAT_GOOD;
636
637
638
639
640
641
642
643 need_auto_sense = 0;
644
645
646
647
648
649
650
651 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
652 srb->sc_data_direction != DMA_FROM_DEVICE) {
653 usb_stor_dbg(us, "-- CB transport device requiring auto-sense\n");
654 need_auto_sense = 1;
655 }
656
657
658
659
660
661
662 if (result == USB_STOR_TRANSPORT_FAILED) {
663 usb_stor_dbg(us, "-- transport indicates command failure\n");
664 need_auto_sense = 1;
665 }
666
667
668
669
670
671
672
673 if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
674 result == USB_STOR_TRANSPORT_GOOD &&
675 !(us->fflags & US_FL_SANE_SENSE) &&
676 !(us->fflags & US_FL_BAD_SENSE) &&
677 !(srb->cmnd[2] & 0x20))) {
678 usb_stor_dbg(us, "-- SAT supported, increasing auto-sense\n");
679 us->fflags |= US_FL_SANE_SENSE;
680 }
681
682
683
684
685
686 if ((scsi_get_resid(srb) > 0) &&
687 !((srb->cmnd[0] == REQUEST_SENSE) ||
688 (srb->cmnd[0] == INQUIRY) ||
689 (srb->cmnd[0] == MODE_SENSE) ||
690 (srb->cmnd[0] == LOG_SENSE) ||
691 (srb->cmnd[0] == MODE_SENSE_10))) {
692 usb_stor_dbg(us, "-- unexpectedly short transfer\n");
693 }
694
695
696 if (need_auto_sense) {
697 int temp_result;
698 struct scsi_eh_save ses;
699 int sense_size = US_SENSE_SIZE;
700 struct scsi_sense_hdr sshdr;
701 const u8 *scdd;
702 u8 fm_ili;
703
704
705 if (us->fflags & US_FL_SANE_SENSE)
706 sense_size = ~0;
707Retry_Sense:
708 usb_stor_dbg(us, "Issuing auto-REQUEST_SENSE\n");
709
710 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
711
712
713 if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
714 us->subclass == USB_SC_CYP_ATACB)
715 srb->cmd_len = 6;
716 else
717 srb->cmd_len = 12;
718
719
720 scsi_set_resid(srb, 0);
721 temp_result = us->transport(us->srb, us);
722
723
724 scsi_eh_restore_cmnd(srb, &ses);
725
726 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
727 usb_stor_dbg(us, "-- auto-sense aborted\n");
728 srb->result = DID_ABORT << 16;
729
730
731 if (sense_size != US_SENSE_SIZE) {
732 us->fflags &= ~US_FL_SANE_SENSE;
733 us->fflags |= US_FL_BAD_SENSE;
734 }
735 goto Handle_Errors;
736 }
737
738
739
740
741
742
743
744 if (temp_result == USB_STOR_TRANSPORT_FAILED &&
745 sense_size != US_SENSE_SIZE) {
746 usb_stor_dbg(us, "-- auto-sense failure, retry small sense\n");
747 sense_size = US_SENSE_SIZE;
748 us->fflags &= ~US_FL_SANE_SENSE;
749 us->fflags |= US_FL_BAD_SENSE;
750 goto Retry_Sense;
751 }
752
753
754 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
755 usb_stor_dbg(us, "-- auto-sense failure\n");
756
757
758
759
760
761
762 srb->result = DID_ERROR << 16;
763 if (!(us->fflags & US_FL_SCM_MULT_TARG))
764 goto Handle_Errors;
765 return;
766 }
767
768
769
770
771
772
773 if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
774 !(us->fflags & US_FL_SANE_SENSE) &&
775 !(us->fflags & US_FL_BAD_SENSE) &&
776 (srb->sense_buffer[0] & 0x7C) == 0x70) {
777 usb_stor_dbg(us, "-- SANE_SENSE support enabled\n");
778 us->fflags |= US_FL_SANE_SENSE;
779
780
781
782
783
784 usb_stor_dbg(us, "-- Sense data truncated to %i from %i\n",
785 US_SENSE_SIZE,
786 srb->sense_buffer[7] + 8);
787 srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
788 }
789
790 scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
791 &sshdr);
792
793 usb_stor_dbg(us, "-- Result from auto-sense is %d\n",
794 temp_result);
795 usb_stor_dbg(us, "-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
796 sshdr.response_code, sshdr.sense_key,
797 sshdr.asc, sshdr.ascq);
798#ifdef CONFIG_USB_STORAGE_DEBUG
799 usb_stor_show_sense(us, sshdr.sense_key, sshdr.asc, sshdr.ascq);
800#endif
801
802
803 srb->result = SAM_STAT_CHECK_CONDITION;
804
805 scdd = scsi_sense_desc_find(srb->sense_buffer,
806 SCSI_SENSE_BUFFERSIZE, 4);
807 fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
808
809
810
811
812
813
814 if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
815 fm_ili == 0) {
816
817
818
819
820
821 if (result == USB_STOR_TRANSPORT_GOOD) {
822 srb->result = SAM_STAT_GOOD;
823 srb->sense_buffer[0] = 0x0;
824 }
825
826
827
828
829
830
831
832 else if (srb->cmnd[0] == ATA_16 ||
833 srb->cmnd[0] == ATA_12) {
834
835 }
836
837
838
839
840
841
842 else {
843 srb->result = DID_ERROR << 16;
844 if ((sshdr.response_code & 0x72) == 0x72)
845 srb->sense_buffer[1] = HARDWARE_ERROR;
846 else
847 srb->sense_buffer[2] = HARDWARE_ERROR;
848 }
849 }
850 }
851
852
853
854
855
856
857
858
859
860 if (unlikely((us->fflags & US_FL_INITIAL_READ10) &&
861 srb->cmnd[0] == READ_10)) {
862 if (srb->result == SAM_STAT_GOOD) {
863 set_bit(US_FLIDX_READ10_WORKED, &us->dflags);
864 } else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) {
865 clear_bit(US_FLIDX_READ10_WORKED, &us->dflags);
866 set_bit(US_FLIDX_REDO_READ10, &us->dflags);
867 }
868
869
870
871
872
873
874 if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) {
875 clear_bit(US_FLIDX_REDO_READ10, &us->dflags);
876 srb->result = DID_IMM_RETRY << 16;
877 srb->sense_buffer[0] = 0;
878 }
879 }
880
881
882 if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
883 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
884 srb->result = DID_ERROR << 16;
885
886 last_sector_hacks(us, srb);
887 return;
888
889
890
891
892
893
894 Handle_Errors:
895
896
897
898
899
900 scsi_lock(us_to_host(us));
901 set_bit(US_FLIDX_RESETTING, &us->dflags);
902 clear_bit(US_FLIDX_ABORTING, &us->dflags);
903 scsi_unlock(us_to_host(us));
904
905
906
907
908
909 mutex_unlock(&us->dev_mutex);
910 result = usb_stor_port_reset(us);
911 mutex_lock(&us->dev_mutex);
912
913 if (result < 0) {
914 scsi_lock(us_to_host(us));
915 usb_stor_report_device_reset(us);
916 scsi_unlock(us_to_host(us));
917 us->transport_reset(us);
918 }
919 clear_bit(US_FLIDX_RESETTING, &us->dflags);
920 last_sector_hacks(us, srb);
921}
922
923
924void usb_stor_stop_transport(struct us_data *us)
925{
926
927
928
929
930
931
932 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
933 usb_stor_dbg(us, "-- cancelling URB\n");
934 usb_unlink_urb(us->current_urb);
935 }
936
937
938 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
939 usb_stor_dbg(us, "-- cancelling sg request\n");
940 usb_sg_cancel(&us->current_sg);
941 }
942}
943
944
945
946
947
948int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
949{
950 unsigned int transfer_length = scsi_bufflen(srb);
951 unsigned int pipe = 0;
952 int result;
953
954
955
956
957
958
959
960 memcpy(us->iobuf, srb->cmnd, srb->cmd_len);
961 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
962 US_CBI_ADSC,
963 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
964 us->ifnum, us->iobuf, srb->cmd_len);
965
966
967 usb_stor_dbg(us, "Call to usb_stor_ctrl_transfer() returned %d\n",
968 result);
969
970
971 if (result == USB_STOR_XFER_STALLED) {
972 return USB_STOR_TRANSPORT_FAILED;
973 }
974
975
976 if (result != USB_STOR_XFER_GOOD) {
977 return USB_STOR_TRANSPORT_ERROR;
978 }
979
980
981
982 if (transfer_length) {
983 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
984 us->recv_bulk_pipe : us->send_bulk_pipe;
985 result = usb_stor_bulk_srb(us, pipe, srb);
986 usb_stor_dbg(us, "CBI data stage result is 0x%x\n", result);
987
988
989 if (result == USB_STOR_XFER_STALLED)
990 return USB_STOR_TRANSPORT_FAILED;
991 if (result > USB_STOR_XFER_STALLED)
992 return USB_STOR_TRANSPORT_ERROR;
993 }
994
995
996
997
998
999
1000
1001 if (us->protocol != USB_PR_CBI)
1002 return USB_STOR_TRANSPORT_GOOD;
1003
1004 result = usb_stor_intr_transfer(us, us->iobuf, 2);
1005 usb_stor_dbg(us, "Got interrupt data (0x%x, 0x%x)\n",
1006 us->iobuf[0], us->iobuf[1]);
1007 if (result != USB_STOR_XFER_GOOD)
1008 return USB_STOR_TRANSPORT_ERROR;
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 if (us->subclass == USB_SC_UFI) {
1019 if (srb->cmnd[0] == REQUEST_SENSE ||
1020 srb->cmnd[0] == INQUIRY)
1021 return USB_STOR_TRANSPORT_GOOD;
1022 if (us->iobuf[0])
1023 goto Failed;
1024 return USB_STOR_TRANSPORT_GOOD;
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034 if (us->iobuf[0]) {
1035 usb_stor_dbg(us, "CBI IRQ data showed reserved bType 0x%x\n",
1036 us->iobuf[0]);
1037 goto Failed;
1038
1039 }
1040
1041
1042 switch (us->iobuf[1] & 0x0F) {
1043 case 0x00:
1044 return USB_STOR_TRANSPORT_GOOD;
1045 case 0x01:
1046 goto Failed;
1047 }
1048 return USB_STOR_TRANSPORT_ERROR;
1049
1050
1051
1052
1053
1054 Failed:
1055 if (pipe)
1056 usb_stor_clear_halt(us, pipe);
1057 return USB_STOR_TRANSPORT_FAILED;
1058}
1059EXPORT_SYMBOL_GPL(usb_stor_CB_transport);
1060
1061
1062
1063
1064
1065
1066int usb_stor_Bulk_max_lun(struct us_data *us)
1067{
1068 int result;
1069
1070
1071 us->iobuf[0] = 0;
1072 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
1073 US_BULK_GET_MAX_LUN,
1074 USB_DIR_IN | USB_TYPE_CLASS |
1075 USB_RECIP_INTERFACE,
1076 0, us->ifnum, us->iobuf, 1, 10*HZ);
1077
1078 usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
1079 result, us->iobuf[0]);
1080
1081
1082
1083
1084
1085
1086 if (result > 0) {
1087 if (us->iobuf[0] < 16) {
1088 return us->iobuf[0];
1089 } else {
1090 dev_info(&us->pusb_intf->dev,
1091 "Max LUN %d is not valid, using 0 instead",
1092 us->iobuf[0]);
1093 }
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103 return 0;
1104}
1105
1106int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
1107{
1108 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
1109 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
1110 unsigned int transfer_length = scsi_bufflen(srb);
1111 unsigned int residue;
1112 int result;
1113 int fake_sense = 0;
1114 unsigned int cswlen;
1115 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
1116
1117
1118 if (unlikely(us->fflags & US_FL_BULK32)) {
1119 cbwlen = 32;
1120 us->iobuf[31] = 0;
1121 }
1122
1123
1124 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1125 bcb->DataTransferLength = cpu_to_le32(transfer_length);
1126 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ?
1127 US_BULK_FLAG_IN : 0;
1128 bcb->Tag = ++us->tag;
1129 bcb->Lun = srb->device->lun;
1130 if (us->fflags & US_FL_SCM_MULT_TARG)
1131 bcb->Lun |= srb->device->id << 4;
1132 bcb->Length = srb->cmd_len;
1133
1134
1135 memset(bcb->CDB, 0, sizeof(bcb->CDB));
1136 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
1137
1138
1139 usb_stor_dbg(us, "Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
1140 le32_to_cpu(bcb->Signature), bcb->Tag,
1141 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
1142 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
1143 bcb->Length);
1144 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
1145 bcb, cbwlen, NULL);
1146 usb_stor_dbg(us, "Bulk command transfer result=%d\n", result);
1147 if (result != USB_STOR_XFER_GOOD)
1148 return USB_STOR_TRANSPORT_ERROR;
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 if (unlikely(us->fflags & US_FL_GO_SLOW))
1159 usleep_range(125, 150);
1160
1161 if (transfer_length) {
1162 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
1163 us->recv_bulk_pipe : us->send_bulk_pipe;
1164 result = usb_stor_bulk_srb(us, pipe, srb);
1165 usb_stor_dbg(us, "Bulk data transfer result 0x%x\n", result);
1166 if (result == USB_STOR_XFER_ERROR)
1167 return USB_STOR_TRANSPORT_ERROR;
1168
1169
1170
1171
1172
1173
1174
1175
1176 if (result == USB_STOR_XFER_LONG)
1177 fake_sense = 1;
1178
1179
1180
1181
1182
1183
1184
1185 if (result == USB_STOR_XFER_SHORT &&
1186 srb->sc_data_direction == DMA_FROM_DEVICE &&
1187 transfer_length - scsi_get_resid(srb) ==
1188 US_BULK_CS_WRAP_LEN) {
1189 struct scatterlist *sg = NULL;
1190 unsigned int offset = 0;
1191
1192 if (usb_stor_access_xfer_buf((unsigned char *) bcs,
1193 US_BULK_CS_WRAP_LEN, srb, &sg,
1194 &offset, FROM_XFER_BUF) ==
1195 US_BULK_CS_WRAP_LEN &&
1196 bcs->Signature ==
1197 cpu_to_le32(US_BULK_CS_SIGN)) {
1198 usb_stor_dbg(us, "Device skipped data phase\n");
1199 scsi_set_resid(srb, transfer_length);
1200 goto skipped_data_phase;
1201 }
1202 }
1203 }
1204
1205
1206
1207
1208
1209
1210
1211 usb_stor_dbg(us, "Attempting to get CSW...\n");
1212 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1213 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1214
1215
1216
1217
1218
1219
1220 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
1221 usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
1222 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1223 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1224 }
1225
1226
1227 if (result == USB_STOR_XFER_STALLED) {
1228
1229
1230 usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
1231 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1232 bcs, US_BULK_CS_WRAP_LEN, NULL);
1233 }
1234
1235
1236 usb_stor_dbg(us, "Bulk status result = %d\n", result);
1237 if (result != USB_STOR_XFER_GOOD)
1238 return USB_STOR_TRANSPORT_ERROR;
1239
1240 skipped_data_phase:
1241
1242 residue = le32_to_cpu(bcs->Residue);
1243 usb_stor_dbg(us, "Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1244 le32_to_cpu(bcs->Signature), bcs->Tag,
1245 residue, bcs->Status);
1246 if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
1247 bcs->Status > US_BULK_STAT_PHASE) {
1248 usb_stor_dbg(us, "Bulk logical error\n");
1249 return USB_STOR_TRANSPORT_ERROR;
1250 }
1251
1252
1253
1254
1255
1256
1257 if (!us->bcs_signature) {
1258 us->bcs_signature = bcs->Signature;
1259 if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
1260 usb_stor_dbg(us, "Learnt BCS signature 0x%08X\n",
1261 le32_to_cpu(us->bcs_signature));
1262 } else if (bcs->Signature != us->bcs_signature) {
1263 usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
1264 le32_to_cpu(bcs->Signature),
1265 le32_to_cpu(us->bcs_signature));
1266 return USB_STOR_TRANSPORT_ERROR;
1267 }
1268
1269
1270
1271
1272
1273 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
1274
1275
1276
1277
1278
1279
1280 if (bcs->Status == US_BULK_STAT_OK &&
1281 scsi_get_resid(srb) == 0 &&
1282 ((srb->cmnd[0] == INQUIRY &&
1283 transfer_length == 36) ||
1284 (srb->cmnd[0] == READ_CAPACITY &&
1285 transfer_length == 8))) {
1286 us->fflags |= US_FL_IGNORE_RESIDUE;
1287
1288 } else {
1289 residue = min(residue, transfer_length);
1290 scsi_set_resid(srb, max(scsi_get_resid(srb),
1291 (int) residue));
1292 }
1293 }
1294
1295
1296 switch (bcs->Status) {
1297 case US_BULK_STAT_OK:
1298
1299 if (fake_sense) {
1300 memcpy(srb->sense_buffer,
1301 usb_stor_sense_invalidCDB,
1302 sizeof(usb_stor_sense_invalidCDB));
1303 return USB_STOR_TRANSPORT_NO_SENSE;
1304 }
1305
1306
1307 return USB_STOR_TRANSPORT_GOOD;
1308
1309 case US_BULK_STAT_FAIL:
1310
1311 return USB_STOR_TRANSPORT_FAILED;
1312
1313 case US_BULK_STAT_PHASE:
1314
1315
1316
1317
1318 return USB_STOR_TRANSPORT_ERROR;
1319 }
1320
1321
1322 return USB_STOR_TRANSPORT_ERROR;
1323}
1324EXPORT_SYMBOL_GPL(usb_stor_Bulk_transport);
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339static int usb_stor_reset_common(struct us_data *us,
1340 u8 request, u8 requesttype,
1341 u16 value, u16 index, void *data, u16 size)
1342{
1343 int result;
1344 int result2;
1345
1346 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1347 usb_stor_dbg(us, "No reset during disconnect\n");
1348 return -EIO;
1349 }
1350
1351 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1352 request, requesttype, value, index, data, size,
1353 5*HZ);
1354 if (result < 0) {
1355 usb_stor_dbg(us, "Soft reset failed: %d\n", result);
1356 return result;
1357 }
1358
1359
1360
1361
1362
1363 wait_event_interruptible_timeout(us->delay_wait,
1364 test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
1365 HZ*6);
1366 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1367 usb_stor_dbg(us, "Reset interrupted by disconnect\n");
1368 return -EIO;
1369 }
1370
1371 usb_stor_dbg(us, "Soft reset: clearing bulk-in endpoint halt\n");
1372 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1373
1374 usb_stor_dbg(us, "Soft reset: clearing bulk-out endpoint halt\n");
1375 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1376
1377
1378 if (result >= 0)
1379 result = result2;
1380 if (result < 0)
1381 usb_stor_dbg(us, "Soft reset failed\n");
1382 else
1383 usb_stor_dbg(us, "Soft reset done\n");
1384 return result;
1385}
1386
1387
1388#define CB_RESET_CMD_SIZE 12
1389
1390int usb_stor_CB_reset(struct us_data *us)
1391{
1392 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1393 us->iobuf[0] = SEND_DIAGNOSTIC;
1394 us->iobuf[1] = 4;
1395 return usb_stor_reset_common(us, US_CBI_ADSC,
1396 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1397 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1398}
1399EXPORT_SYMBOL_GPL(usb_stor_CB_reset);
1400
1401
1402
1403
1404
1405int usb_stor_Bulk_reset(struct us_data *us)
1406{
1407 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1408 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1409 0, us->ifnum, NULL, 0);
1410}
1411EXPORT_SYMBOL_GPL(usb_stor_Bulk_reset);
1412
1413
1414
1415
1416
1417int usb_stor_port_reset(struct us_data *us)
1418{
1419 int result;
1420
1421
1422 if (us->pusb_dev->quirks & USB_QUIRK_RESET)
1423 return -EPERM;
1424
1425 result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
1426 if (result < 0)
1427 usb_stor_dbg(us, "unable to lock device for reset: %d\n",
1428 result);
1429 else {
1430
1431 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
1432 result = -EIO;
1433 usb_stor_dbg(us, "No reset during disconnect\n");
1434 } else {
1435 result = usb_reset_device(us->pusb_dev);
1436 usb_stor_dbg(us, "usb_reset_device returns %d\n",
1437 result);
1438 }
1439 usb_unlock_device(us->pusb_dev);
1440 }
1441 return result;
1442}
1443