Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #include <linux/usb.h>
0003 #include <linux/usb/hcd.h>
0004 #include "usb.h"
0005 
0006 static int uas_is_interface(struct usb_host_interface *intf)
0007 {
0008     return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
0009         intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
0010         intf->desc.bInterfaceProtocol == USB_PR_UAS);
0011 }
0012 
0013 static struct usb_host_interface *uas_find_uas_alt_setting(
0014         struct usb_interface *intf)
0015 {
0016     int i;
0017 
0018     for (i = 0; i < intf->num_altsetting; i++) {
0019         struct usb_host_interface *alt = &intf->altsetting[i];
0020 
0021         if (uas_is_interface(alt))
0022             return alt;
0023     }
0024 
0025     return NULL;
0026 }
0027 
0028 static int uas_find_endpoints(struct usb_host_interface *alt,
0029                   struct usb_host_endpoint *eps[])
0030 {
0031     struct usb_host_endpoint *endpoint = alt->endpoint;
0032     unsigned i, n_endpoints = alt->desc.bNumEndpoints;
0033 
0034     for (i = 0; i < n_endpoints; i++) {
0035         unsigned char *extra = endpoint[i].extra;
0036         int len = endpoint[i].extralen;
0037         while (len >= 3) {
0038             if (extra[1] == USB_DT_PIPE_USAGE) {
0039                 unsigned pipe_id = extra[2];
0040                 if (pipe_id > 0 && pipe_id < 5)
0041                     eps[pipe_id - 1] = &endpoint[i];
0042                 break;
0043             }
0044             len -= extra[0];
0045             extra += extra[0];
0046         }
0047     }
0048 
0049     if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
0050         return -ENODEV;
0051 
0052     return 0;
0053 }
0054 
0055 static int uas_use_uas_driver(struct usb_interface *intf,
0056                   const struct usb_device_id *id,
0057                   unsigned long *flags_ret)
0058 {
0059     struct usb_host_endpoint *eps[4] = { };
0060     struct usb_device *udev = interface_to_usbdev(intf);
0061     struct usb_hcd *hcd = bus_to_hcd(udev->bus);
0062     unsigned long flags = id->driver_info;
0063     struct usb_host_interface *alt;
0064     int r;
0065 
0066     alt = uas_find_uas_alt_setting(intf);
0067     if (!alt)
0068         return 0;
0069 
0070     r = uas_find_endpoints(alt, eps);
0071     if (r < 0)
0072         return 0;
0073 
0074     /*
0075      * ASMedia has a number of usb3 to sata bridge chips, at the time of
0076      * this writing the following versions exist:
0077      * ASM1051 - no uas support version
0078      * ASM1051 - with broken (*) uas support
0079      * ASM1053 - with working uas support, but problems with large xfers
0080      * ASM1153 - with working uas support
0081      *
0082      * Devices with these chips re-use a number of device-ids over the
0083      * entire line, so the device-id is useless to determine if we're
0084      * dealing with an ASM1051 (which we want to avoid).
0085      *
0086      * The ASM1153 can be identified by config.MaxPower == 0,
0087      * where as the ASM105x models have config.MaxPower == 36.
0088      *
0089      * Differentiating between the ASM1053 and ASM1051 is trickier, when
0090      * connected over USB-3 we can look at the number of streams supported,
0091      * ASM1051 supports 32 streams, where as early ASM1053 versions support
0092      * 16 streams, newer ASM1053-s also support 32 streams, but have a
0093      * different prod-id.
0094      *
0095      * (*) ASM1051 chips do work with UAS with some disks (with the
0096      *     US_FL_NO_REPORT_OPCODES quirk), but are broken with other disks
0097      */
0098     if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
0099             (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 ||
0100              le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) {
0101         if (udev->actconfig->desc.bMaxPower == 0) {
0102             /* ASM1153, do nothing */
0103         } else if (udev->speed < USB_SPEED_SUPER) {
0104             /* No streams info, assume ASM1051 */
0105             flags |= US_FL_IGNORE_UAS;
0106         } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
0107             /* Possibly an ASM1051, disable uas */
0108             flags |= US_FL_IGNORE_UAS;
0109         } else {
0110             /* ASM1053, these have issues with large transfers */
0111             flags |= US_FL_MAX_SECTORS_240;
0112         }
0113     }
0114 
0115     /* All Seagate disk enclosures have broken ATA pass-through support */
0116     if (le16_to_cpu(udev->descriptor.idVendor) == 0x0bc2)
0117         flags |= US_FL_NO_ATA_1X;
0118 
0119     usb_stor_adjust_quirks(udev, &flags);
0120 
0121     if (flags & US_FL_IGNORE_UAS) {
0122         dev_warn(&udev->dev,
0123             "UAS is ignored for this device, using usb-storage instead\n");
0124         return 0;
0125     }
0126 
0127     if (udev->bus->sg_tablesize == 0) {
0128         dev_warn(&udev->dev,
0129             "The driver for the USB controller %s does not support scatter-gather which is\n",
0130             hcd->driver->description);
0131         dev_warn(&udev->dev,
0132             "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
0133         return 0;
0134     }
0135 
0136     if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
0137         dev_warn(&udev->dev,
0138             "USB controller %s does not support streams, which are required by the UAS driver.\n",
0139             hcd_to_bus(hcd)->bus_name);
0140         dev_warn(&udev->dev,
0141             "Please try an other USB controller if you wish to use UAS.\n");
0142         return 0;
0143     }
0144 
0145     if (flags_ret)
0146         *flags_ret = flags;
0147 
0148     return 1;
0149 }