Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb-storage: redo incorrect reads

Some USB mass-storage devices have bugs that cause them not to handle
the first READ(10) command they receive correctly. The Corsair
Padlock v2 returns completely bogus data for its first read (possibly
it returns the data in encrypted form even though the device is
supposed to be unlocked). The Feiya SD/SDHC card reader fails to
complete the first READ(10) command after it is plugged in or after a
new card is inserted, returning a status code that indicates it thinks
the command was invalid, which prevents the kernel from retrying the
read.

Since the first read of a new device or a new medium is for the
partition sector, the kernel is unable to retrieve the device's
partition table. Users have to manually issue an "hdparm -z" or
"blockdev --rereadpt" command before they can access the device.

This patch (as1470) works around the problem. It adds a new quirk
flag, US_FL_INVALID_READ10, indicating that the first READ(10) should
always be retried immediately, as should any failing READ(10) commands
(provided the preceding READ(10) command succeeded, to avoid getting
stuck in a loop). The patch also adds appropriate unusual_devs
entries containing the new flag.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Tested-by: Sven Geggus <sven-usbst@geggus.net>
Tested-by: Paul Hartman <paul.hartman+linux@gmail.com>
CC: Matthew Dharm <mdharm-usb@one-eyed-alien.net>
CC: <stable@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Alan Stern and committed by
Greg Kroah-Hartman
21c13a4f 3af51ac9

+67 -2
+2
Documentation/kernel-parameters.txt
··· 2598 2598 unlock ejectable media); 2599 2599 m = MAX_SECTORS_64 (don't transfer more 2600 2600 than 64 sectors = 32 KB at a time); 2601 + n = INITIAL_READ10 (force a retry of the 2602 + initial READ(10) command); 2601 2603 o = CAPACITY_OK (accept the capacity 2602 2604 reported by the device); 2603 2605 r = IGNORE_RESIDUE (the device reports
+29
drivers/usb/storage/transport.c
··· 819 819 } 820 820 } 821 821 822 + /* 823 + * Some devices don't work or return incorrect data the first 824 + * time they get a READ(10) command, or for the first READ(10) 825 + * after a media change. If the INITIAL_READ10 flag is set, 826 + * keep track of whether READ(10) commands succeed. If the 827 + * previous one succeeded and this one failed, set the REDO_READ10 828 + * flag to force a retry. 829 + */ 830 + if (unlikely((us->fflags & US_FL_INITIAL_READ10) && 831 + srb->cmnd[0] == READ_10)) { 832 + if (srb->result == SAM_STAT_GOOD) { 833 + set_bit(US_FLIDX_READ10_WORKED, &us->dflags); 834 + } else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) { 835 + clear_bit(US_FLIDX_READ10_WORKED, &us->dflags); 836 + set_bit(US_FLIDX_REDO_READ10, &us->dflags); 837 + } 838 + 839 + /* 840 + * Next, if the REDO_READ10 flag is set, return a result 841 + * code that will cause the SCSI core to retry the READ(10) 842 + * command immediately. 843 + */ 844 + if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) { 845 + clear_bit(US_FLIDX_REDO_READ10, &us->dflags); 846 + srb->result = DID_IMM_RETRY << 16; 847 + srb->sense_buffer[0] = 0; 848 + } 849 + } 850 + 822 851 /* Did we transfer less than the minimum amount required? */ 823 852 if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) && 824 853 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
+19
drivers/usb/storage/unusual_devs.h
··· 1114 1114 USB_SC_DEVICE, USB_PR_DEVICE, NULL, 1115 1115 US_FL_FIX_CAPACITY ), 1116 1116 1117 + /* Reported by Paul Hartman <paul.hartman+linux@gmail.com> 1118 + * This card reader returns "Illegal Request, Logical Block Address 1119 + * Out of Range" for the first READ(10) after a new card is inserted. 1120 + */ 1121 + UNUSUAL_DEV( 0x090c, 0x6000, 0x0100, 0x0100, 1122 + "Feiya", 1123 + "SD/SDHC Card Reader", 1124 + USB_SC_DEVICE, USB_PR_DEVICE, NULL, 1125 + US_FL_INITIAL_READ10 ), 1126 + 1117 1127 /* This Pentax still camera is not conformant 1118 1128 * to the USB storage specification: - 1119 1129 * - It does not like the INQUIRY command. So we must handle this command ··· 1897 1887 "Photo Frame", 1898 1888 USB_SC_DEVICE, USB_PR_DEVICE, NULL, 1899 1889 US_FL_NO_READ_DISC_INFO ), 1890 + 1891 + /* Reported by Sven Geggus <sven-usbst@geggus.net> 1892 + * This encrypted pen drive returns bogus data for the initial READ(10). 1893 + */ 1894 + UNUSUAL_DEV( 0x1b1c, 0x1ab5, 0x0200, 0x0200, 1895 + "Corsair", 1896 + "Padlock v2", 1897 + USB_SC_DEVICE, USB_PR_DEVICE, NULL, 1898 + US_FL_INITIAL_READ10 ), 1900 1899 1901 1900 /* Patch by Richard Sch�tz <r.schtz@t-online.de> 1902 1901 * This external hard drive enclosure uses a JMicron chip which
+12 -1
drivers/usb/storage/usb.c
··· 440 440 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 | 441 441 US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE | 442 442 US_FL_SINGLE_LUN | US_FL_NO_WP_DETECT | 443 - US_FL_NO_READ_DISC_INFO | US_FL_NO_READ_CAPACITY_16); 443 + US_FL_NO_READ_DISC_INFO | US_FL_NO_READ_CAPACITY_16 | 444 + US_FL_INITIAL_READ10); 444 445 445 446 p = quirks; 446 447 while (*p) { ··· 490 489 break; 491 490 case 'm': 492 491 f |= US_FL_MAX_SECTORS_64; 492 + break; 493 + case 'n': 494 + f |= US_FL_INITIAL_READ10; 493 495 break; 494 496 case 'o': 495 497 f |= US_FL_CAPACITY_OK; ··· 956 952 result = get_pipes(us); 957 953 if (result) 958 954 goto BadDevice; 955 + 956 + /* 957 + * If the device returns invalid data for the first READ(10) 958 + * command, indicate the command should be retried. 959 + */ 960 + if (us->fflags & US_FL_INITIAL_READ10) 961 + set_bit(US_FLIDX_REDO_READ10, &us->dflags); 959 962 960 963 /* Acquire all the other resources and add the host */ 961 964 result = usb_stor_acquire_resources(us);
+2
drivers/usb/storage/usb.h
··· 73 73 #define US_FLIDX_RESETTING 4 /* device reset in progress */ 74 74 #define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */ 75 75 #define US_FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */ 76 + #define US_FLIDX_REDO_READ10 7 /* redo READ(10) command */ 77 + #define US_FLIDX_READ10_WORKED 8 /* previous READ(10) succeeded */ 76 78 77 79 #define USB_STOR_STRING_LEN 32 78 80
+3 -1
include/linux/usb_usual.h
··· 62 62 US_FLAG(NO_READ_DISC_INFO, 0x00040000) \ 63 63 /* cannot handle READ_DISC_INFO */ \ 64 64 US_FLAG(NO_READ_CAPACITY_16, 0x00080000) \ 65 - /* cannot handle READ_CAPACITY_16 */ 65 + /* cannot handle READ_CAPACITY_16 */ \ 66 + US_FLAG(INITIAL_READ10, 0x00100000) \ 67 + /* Initial READ(10) (and others) must be retried */ 66 68 67 69 #define US_FLAG(name, value) US_FL_##name = value , 68 70 enum { US_DO_ALL_FLAGS };