Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 2 | #include <linux/usb.h> |
| 3 | #include <linux/usb/hcd.h> |
Hans de Goede | 97172a6 | 2013-11-16 12:19:36 +0100 | [diff] [blame] | 4 | #include "usb.h" |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 5 | |
| 6 | static int uas_is_interface(struct usb_host_interface *intf) |
| 7 | { |
| 8 | return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE && |
| 9 | intf->desc.bInterfaceSubClass == USB_SC_SCSI && |
| 10 | intf->desc.bInterfaceProtocol == USB_PR_UAS); |
| 11 | } |
| 12 | |
Alan Stern | 786de92 | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 13 | static struct usb_host_interface *uas_find_uas_alt_setting( |
| 14 | struct usb_interface *intf) |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 15 | { |
| 16 | int i; |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 17 | |
| 18 | for (i = 0; i < intf->num_altsetting; i++) { |
| 19 | struct usb_host_interface *alt = &intf->altsetting[i]; |
| 20 | |
Hans de Goede | cc4deaf | 2014-07-25 22:01:26 +0200 | [diff] [blame] | 21 | if (uas_is_interface(alt)) |
Alan Stern | 786de92 | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 22 | return alt; |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Alan Stern | 786de92 | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 25 | return NULL; |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 26 | } |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 27 | |
Hans de Goede | d77adc02 | 2013-10-29 10:03:34 +0100 | [diff] [blame] | 28 | static int uas_find_endpoints(struct usb_host_interface *alt, |
| 29 | struct usb_host_endpoint *eps[]) |
| 30 | { |
| 31 | struct usb_host_endpoint *endpoint = alt->endpoint; |
| 32 | unsigned i, n_endpoints = alt->desc.bNumEndpoints; |
| 33 | |
| 34 | for (i = 0; i < n_endpoints; i++) { |
| 35 | unsigned char *extra = endpoint[i].extra; |
| 36 | int len = endpoint[i].extralen; |
| 37 | while (len >= 3) { |
| 38 | if (extra[1] == USB_DT_PIPE_USAGE) { |
| 39 | unsigned pipe_id = extra[2]; |
| 40 | if (pipe_id > 0 && pipe_id < 5) |
| 41 | eps[pipe_id - 1] = &endpoint[i]; |
| 42 | break; |
| 43 | } |
| 44 | len -= extra[0]; |
| 45 | extra += extra[0]; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (!eps[0] || !eps[1] || !eps[2] || !eps[3]) |
| 50 | return -ENODEV; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 55 | static int uas_use_uas_driver(struct usb_interface *intf, |
Hans de Goede | a5011d4 | 2015-04-21 11:20:30 +0200 | [diff] [blame] | 56 | const struct usb_device_id *id, |
| 57 | unsigned long *flags_ret) |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 58 | { |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 59 | struct usb_host_endpoint *eps[4] = { }; |
Hans de Goede | 97172a6 | 2013-11-16 12:19:36 +0100 | [diff] [blame] | 60 | struct usb_device *udev = interface_to_usbdev(intf); |
Oliver Neukum | 14aec58 | 2014-02-11 20:36:04 +0100 | [diff] [blame] | 61 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 62 | unsigned long flags = id->driver_info; |
Alan Stern | 786de92 | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 63 | struct usb_host_interface *alt; |
| 64 | int r; |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 65 | |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 66 | alt = uas_find_uas_alt_setting(intf); |
Alan Stern | 786de92 | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 67 | if (!alt) |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 68 | return 0; |
| 69 | |
Alan Stern | 786de92 | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 70 | r = uas_find_endpoints(alt, eps); |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 71 | if (r < 0) |
| 72 | return 0; |
| 73 | |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 74 | /* |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 75 | * ASMedia has a number of usb3 to sata bridge chips, at the time of |
| 76 | * this writing the following versions exist: |
| 77 | * ASM1051 - no uas support version |
| 78 | * ASM1051 - with broken (*) uas support |
Hans de Goede | 8e779c6 | 2015-04-21 11:20:32 +0200 | [diff] [blame] | 79 | * ASM1053 - with working uas support, but problems with large xfers |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 80 | * ASM1153 - with working uas support |
| 81 | * |
| 82 | * Devices with these chips re-use a number of device-ids over the |
| 83 | * entire line, so the device-id is useless to determine if we're |
| 84 | * dealing with an ASM1051 (which we want to avoid). |
| 85 | * |
| 86 | * The ASM1153 can be identified by config.MaxPower == 0, |
| 87 | * where as the ASM105x models have config.MaxPower == 36. |
| 88 | * |
| 89 | * Differentiating between the ASM1053 and ASM1051 is trickier, when |
| 90 | * connected over USB-3 we can look at the number of streams supported, |
| 91 | * ASM1051 supports 32 streams, where as early ASM1053 versions support |
| 92 | * 16 streams, newer ASM1053-s also support 32 streams, but have a |
| 93 | * different prod-id. |
| 94 | * |
| 95 | * (*) ASM1051 chips do work with UAS with some disks (with the |
| 96 | * US_FL_NO_REPORT_OPCODES quirk), but are broken with other disks |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 97 | */ |
Hans de Goede | a79e5bc | 2014-09-11 11:06:12 +0200 | [diff] [blame] | 98 | if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c && |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 99 | (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 || |
| 100 | le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) { |
| 101 | if (udev->actconfig->desc.bMaxPower == 0) { |
| 102 | /* ASM1153, do nothing */ |
| 103 | } else if (udev->speed < USB_SPEED_SUPER) { |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 104 | /* No streams info, assume ASM1051 */ |
| 105 | flags |= US_FL_IGNORE_UAS; |
| 106 | } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) { |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 107 | /* Possibly an ASM1051, disable uas */ |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 108 | flags |= US_FL_IGNORE_UAS; |
Hans de Goede | 8e779c6 | 2015-04-21 11:20:32 +0200 | [diff] [blame] | 109 | } else { |
| 110 | /* ASM1053, these have issues with large transfers */ |
| 111 | flags |= US_FL_MAX_SECTORS_240; |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Hans de Goede | 7fee72d | 2017-11-14 19:27:22 +0100 | [diff] [blame] | 115 | /* All Seagate disk enclosures have broken ATA pass-through support */ |
| 116 | if (le16_to_cpu(udev->descriptor.idVendor) == 0x0bc2) |
| 117 | flags |= US_FL_NO_ATA_1X; |
| 118 | |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 119 | usb_stor_adjust_quirks(udev, &flags); |
| 120 | |
| 121 | if (flags & US_FL_IGNORE_UAS) { |
| 122 | dev_warn(&udev->dev, |
| 123 | "UAS is blacklisted for this device, using usb-storage instead\n"); |
| 124 | return 0; |
| 125 | } |
| 126 | |
Hans de Goede | cc4deaf | 2014-07-25 22:01:26 +0200 | [diff] [blame] | 127 | if (udev->bus->sg_tablesize == 0) { |
| 128 | dev_warn(&udev->dev, |
| 129 | "The driver for the USB controller %s does not support scatter-gather which is\n", |
| 130 | hcd->driver->description); |
| 131 | dev_warn(&udev->dev, |
| 132 | "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n"); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Hans de Goede | 43508be | 2014-07-25 22:01:27 +0200 | [diff] [blame] | 136 | if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) { |
| 137 | dev_warn(&udev->dev, |
| 138 | "USB controller %s does not support streams, which are required by the UAS driver.\n", |
| 139 | hcd_to_bus(hcd)->bus_name); |
| 140 | dev_warn(&udev->dev, |
| 141 | "Please try an other USB controller if you wish to use UAS.\n"); |
| 142 | return 0; |
| 143 | } |
| 144 | |
Hans de Goede | a5011d4 | 2015-04-21 11:20:30 +0200 | [diff] [blame] | 145 | if (flags_ret) |
| 146 | *flags_ret = flags; |
| 147 | |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 148 | return 1; |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 149 | } |