uas-detect.h 4.3 KB

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