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