1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
282aa0387SHans de Goede #include <linux/usb.h>
382aa0387SHans de Goede #include <linux/usb/hcd.h>
497172a66SHans de Goede #include "usb.h"
582aa0387SHans de Goede
uas_is_interface(struct usb_host_interface * intf)682aa0387SHans de Goede static int uas_is_interface(struct usb_host_interface *intf)
782aa0387SHans de Goede {
882aa0387SHans de Goede return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
982aa0387SHans de Goede intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
1082aa0387SHans de Goede intf->desc.bInterfaceProtocol == USB_PR_UAS);
1182aa0387SHans de Goede }
1282aa0387SHans de Goede
uas_find_uas_alt_setting(struct usb_interface * intf)13786de92bSAlan Stern static struct usb_host_interface *uas_find_uas_alt_setting(
14786de92bSAlan Stern struct usb_interface *intf)
1582aa0387SHans de Goede {
1682aa0387SHans de Goede int i;
1782aa0387SHans de Goede
1882aa0387SHans de Goede for (i = 0; i < intf->num_altsetting; i++) {
1982aa0387SHans de Goede struct usb_host_interface *alt = &intf->altsetting[i];
2082aa0387SHans de Goede
21cc4deafcSHans de Goede if (uas_is_interface(alt))
22786de92bSAlan Stern return alt;
2382aa0387SHans de Goede }
2482aa0387SHans de Goede
25786de92bSAlan Stern return NULL;
2682aa0387SHans de Goede }
2779b4c061SHans de Goede
uas_find_endpoints(struct usb_host_interface * alt,struct usb_host_endpoint * eps[])28d77adc02SHans de Goede static int uas_find_endpoints(struct usb_host_interface *alt,
29d77adc02SHans de Goede struct usb_host_endpoint *eps[])
30d77adc02SHans de Goede {
31d77adc02SHans de Goede struct usb_host_endpoint *endpoint = alt->endpoint;
32d77adc02SHans de Goede unsigned i, n_endpoints = alt->desc.bNumEndpoints;
33d77adc02SHans de Goede
34d77adc02SHans de Goede for (i = 0; i < n_endpoints; i++) {
35d77adc02SHans de Goede unsigned char *extra = endpoint[i].extra;
36d77adc02SHans de Goede int len = endpoint[i].extralen;
37d77adc02SHans de Goede while (len >= 3) {
38d77adc02SHans de Goede if (extra[1] == USB_DT_PIPE_USAGE) {
39d77adc02SHans de Goede unsigned pipe_id = extra[2];
40d77adc02SHans de Goede if (pipe_id > 0 && pipe_id < 5)
41d77adc02SHans de Goede eps[pipe_id - 1] = &endpoint[i];
42d77adc02SHans de Goede break;
43d77adc02SHans de Goede }
44d77adc02SHans de Goede len -= extra[0];
45d77adc02SHans de Goede extra += extra[0];
46d77adc02SHans de Goede }
47d77adc02SHans de Goede }
48d77adc02SHans de Goede
49d77adc02SHans de Goede if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
50d77adc02SHans de Goede return -ENODEV;
51d77adc02SHans de Goede
52d77adc02SHans de Goede return 0;
53d77adc02SHans de Goede }
54d77adc02SHans de Goede
uas_use_uas_driver(struct usb_interface * intf,const struct usb_device_id * id,u64 * flags_ret)5579b4c061SHans de Goede static int uas_use_uas_driver(struct usb_interface *intf,
56a5011d44SHans de Goede const struct usb_device_id *id,
57*d181b34bSMilan Broz u64 *flags_ret)
5879b4c061SHans de Goede {
596134041bSHans de Goede struct usb_host_endpoint *eps[4] = { };
6097172a66SHans de Goede struct usb_device *udev = interface_to_usbdev(intf);
6114aec589SOliver Neukum struct usb_hcd *hcd = bus_to_hcd(udev->bus);
62*d181b34bSMilan Broz u64 flags = id->driver_info;
63786de92bSAlan Stern struct usb_host_interface *alt;
64786de92bSAlan Stern int r;
6579b4c061SHans de Goede
666134041bSHans de Goede alt = uas_find_uas_alt_setting(intf);
67786de92bSAlan Stern if (!alt)
686134041bSHans de Goede return 0;
696134041bSHans de Goede
70786de92bSAlan Stern r = uas_find_endpoints(alt, eps);
716134041bSHans de Goede if (r < 0)
726134041bSHans de Goede return 0;
736134041bSHans de Goede
74a9c54caaSHans de Goede /*
75078fd7d6SHans de Goede * ASMedia has a number of usb3 to sata bridge chips, at the time of
76078fd7d6SHans de Goede * this writing the following versions exist:
77078fd7d6SHans de Goede * ASM1051 - no uas support version
78078fd7d6SHans de Goede * ASM1051 - with broken (*) uas support
798e779c6cSHans de Goede * ASM1053 - with working uas support, but problems with large xfers
80078fd7d6SHans de Goede * ASM1153 - with working uas support
81078fd7d6SHans de Goede *
82078fd7d6SHans de Goede * Devices with these chips re-use a number of device-ids over the
83078fd7d6SHans de Goede * entire line, so the device-id is useless to determine if we're
84078fd7d6SHans de Goede * dealing with an ASM1051 (which we want to avoid).
85078fd7d6SHans de Goede *
86078fd7d6SHans de Goede * The ASM1153 can be identified by config.MaxPower == 0,
87078fd7d6SHans de Goede * where as the ASM105x models have config.MaxPower == 36.
88078fd7d6SHans de Goede *
89078fd7d6SHans de Goede * Differentiating between the ASM1053 and ASM1051 is trickier, when
90078fd7d6SHans de Goede * connected over USB-3 we can look at the number of streams supported,
91078fd7d6SHans de Goede * ASM1051 supports 32 streams, where as early ASM1053 versions support
92078fd7d6SHans de Goede * 16 streams, newer ASM1053-s also support 32 streams, but have a
93078fd7d6SHans de Goede * different prod-id.
94078fd7d6SHans de Goede *
95078fd7d6SHans de Goede * (*) ASM1051 chips do work with UAS with some disks (with the
96078fd7d6SHans de Goede * US_FL_NO_REPORT_OPCODES quirk), but are broken with other disks
97a9c54caaSHans de Goede */
98a79e5bc5SHans de Goede if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
99078fd7d6SHans de Goede (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 ||
100078fd7d6SHans de Goede le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) {
101078fd7d6SHans de Goede if (udev->actconfig->desc.bMaxPower == 0) {
102078fd7d6SHans de Goede /* ASM1153, do nothing */
103078fd7d6SHans de Goede } else if (udev->speed < USB_SPEED_SUPER) {
104a9c54caaSHans de Goede /* No streams info, assume ASM1051 */
105a9c54caaSHans de Goede flags |= US_FL_IGNORE_UAS;
106a9c54caaSHans de Goede } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
107078fd7d6SHans de Goede /* Possibly an ASM1051, disable uas */
108a9c54caaSHans de Goede flags |= US_FL_IGNORE_UAS;
1098e779c6cSHans de Goede } else {
1108e779c6cSHans de Goede /* ASM1053, these have issues with large transfers */
1118e779c6cSHans de Goede flags |= US_FL_MAX_SECTORS_240;
112a9c54caaSHans de Goede }
113a9c54caaSHans de Goede }
114a9c54caaSHans de Goede
1157fee72d5SHans de Goede /* All Seagate disk enclosures have broken ATA pass-through support */
1167fee72d5SHans de Goede if (le16_to_cpu(udev->descriptor.idVendor) == 0x0bc2)
1177fee72d5SHans de Goede flags |= US_FL_NO_ATA_1X;
1187fee72d5SHans de Goede
119dbd24ec1SJuhyung Park /*
120dbd24ec1SJuhyung Park * RTL9210-based enclosure from HIKSEMI, MD202 reportedly have issues
121dbd24ec1SJuhyung Park * with UAS. This isn't distinguishable with just idVendor and
122dbd24ec1SJuhyung Park * idProduct, use manufacturer and product too.
123dbd24ec1SJuhyung Park *
124dbd24ec1SJuhyung Park * Reported-by: Hongling Zeng <zenghongling@kylinos.cn>
125dbd24ec1SJuhyung Park */
126dbd24ec1SJuhyung Park if (le16_to_cpu(udev->descriptor.idVendor) == 0x0bda &&
127dbd24ec1SJuhyung Park le16_to_cpu(udev->descriptor.idProduct) == 0x9210 &&
128dbd24ec1SJuhyung Park (udev->manufacturer && !strcmp(udev->manufacturer, "HIKSEMI")) &&
129dbd24ec1SJuhyung Park (udev->product && !strcmp(udev->product, "MD202")))
130dbd24ec1SJuhyung Park flags |= US_FL_IGNORE_UAS;
131dbd24ec1SJuhyung Park
132a9c54caaSHans de Goede usb_stor_adjust_quirks(udev, &flags);
133a9c54caaSHans de Goede
134a9c54caaSHans de Goede if (flags & US_FL_IGNORE_UAS) {
135a9c54caaSHans de Goede dev_warn(&udev->dev,
136c83a74edSGreg Kroah-Hartman "UAS is ignored for this device, using usb-storage instead\n");
137a9c54caaSHans de Goede return 0;
138a9c54caaSHans de Goede }
139a9c54caaSHans de Goede
140cc4deafcSHans de Goede if (udev->bus->sg_tablesize == 0) {
141cc4deafcSHans de Goede dev_warn(&udev->dev,
142cc4deafcSHans de Goede "The driver for the USB controller %s does not support scatter-gather which is\n",
143cc4deafcSHans de Goede hcd->driver->description);
144cc4deafcSHans de Goede dev_warn(&udev->dev,
145cc4deafcSHans de Goede "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
146cc4deafcSHans de Goede return 0;
147cc4deafcSHans de Goede }
148cc4deafcSHans de Goede
14943508be5SHans de Goede if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
15043508be5SHans de Goede dev_warn(&udev->dev,
15143508be5SHans de Goede "USB controller %s does not support streams, which are required by the UAS driver.\n",
15243508be5SHans de Goede hcd_to_bus(hcd)->bus_name);
15343508be5SHans de Goede dev_warn(&udev->dev,
15443508be5SHans de Goede "Please try an other USB controller if you wish to use UAS.\n");
15543508be5SHans de Goede return 0;
15643508be5SHans de Goede }
15743508be5SHans de Goede
158a5011d44SHans de Goede if (flags_ret)
159a5011d44SHans de Goede *flags_ret = flags;
160a5011d44SHans de Goede
1616134041bSHans de Goede return 1;
16279b4c061SHans de Goede }
163