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