uas-detect.h 4.3 KB

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