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