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