aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-03-07 15:53:41 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-03-07 15:53:41 -0500
commitc9050b64940a1b47dfa623630431fec400edbf33 (patch)
tree1b0c061e2b0753066fe982c52e130767bb181551
parent86e2864d7e9d429b94624a28ba3f05fc2db89051 (diff)
parent7969943789df1196faa9ba67518d83fd93e4f9f6 (diff)
Merge tag 'for-usb-next-2014-03-06' of git://git.kernel.org/pub/scm/linux/kernel/git/sarah/xhci into usb-next
Sarah writes: xhci: Streams and UAS cleanups, misc cleanups for 3.15 Hi Greg, Here's 76 patches to queue to usb-next for 3.15. The bulk of this rather large pull request is the UAS driver cleanup, the xHCI streams fixes, and the new userspace API for usbfs to be able to use and alloc/free bulk streams. I've hammered on these changes, and the UAS driver seems solid. The performance numbers are pretty spiffy too: root@xanatos:~# echo 3 > /proc/sys/vm/drop_caches; dd if=/dev/sdb of=/dev/null bs=4k count=1000M iflag=count_bytes 256000+0 records in 256000+0 records out 1048576000 bytes (1.0 GB) copied, 3.28557 s, 319 MB/s That's about 100 MB/s faster than my fastest Bulk-only-Transport mass storage drive. There's a couple of miscellaneous cleanup patches and non-urgent bug fixes in here as well: 7969943789df xhci: add the meaningful IRQ description if it is empty bcffae7708eb xhci: Prevent runtime pm from autosuspending during initialization e587b8b270d3 xhci: make warnings greppable 25cd2882e2fc usb/xhci: Change how we indicate a host supports Link PM. Sarah Sharp
-rw-r--r--MAINTAINERS3
-rw-r--r--drivers/usb/core/config.c1
-rw-r--r--drivers/usb/core/devio.c158
-rw-r--r--drivers/usb/core/driver.c23
-rw-r--r--drivers/usb/core/hcd.c37
-rw-r--r--drivers/usb/core/hub.c51
-rw-r--r--drivers/usb/core/message.c7
-rw-r--r--drivers/usb/host/xhci-hub.c8
-rw-r--r--drivers/usb/host/xhci-mem.c212
-rw-r--r--drivers/usb/host/xhci-pci.c18
-rw-r--r--drivers/usb/host/xhci-plat.c3
-rw-r--r--drivers/usb/host/xhci-ring.c146
-rw-r--r--drivers/usb/host/xhci.c33
-rw-r--r--drivers/usb/host/xhci.h5
-rw-r--r--drivers/usb/storage/Kconfig2
-rw-r--r--drivers/usb/storage/uas-detect.h96
-rw-r--r--drivers/usb/storage/uas.c687
-rw-r--r--drivers/usb/storage/unusual_devs.h5
-rw-r--r--drivers/usb/storage/unusual_uas.h52
-rw-r--r--drivers/usb/storage/usb.c26
-rw-r--r--drivers/usb/storage/usb.h3
-rw-r--r--include/linux/usb.h4
-rw-r--r--include/linux/usb/hcd.h1
-rw-r--r--include/linux/usb/uas.h14
-rw-r--r--include/linux/usb_usual.h6
-rw-r--r--include/uapi/linux/usbdevice_fs.h12
26 files changed, 1182 insertions, 431 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index c6d0e93eff62..fc536bc74300 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9063,8 +9063,7 @@ S: Maintained
9063F: drivers/net/wireless/ath/ar5523/ 9063F: drivers/net/wireless/ath/ar5523/
9064 9064
9065USB ATTACHED SCSI 9065USB ATTACHED SCSI
9066M: Matthew Wilcox <willy@linux.intel.com> 9066M: Hans de Goede <hdegoede@redhat.com>
9067M: Sarah Sharp <sarah.a.sharp@linux.intel.com>
9068M: Gerd Hoffmann <kraxel@redhat.com> 9067M: Gerd Hoffmann <kraxel@redhat.com>
9069L: linux-usb@vger.kernel.org 9068L: linux-usb@vger.kernel.org
9070L: linux-scsi@vger.kernel.org 9069L: linux-scsi@vger.kernel.org
diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index 8d72f0c65937..14ba398d6def 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -10,7 +10,6 @@
10 10
11 11
12#define USB_MAXALTSETTING 128 /* Hard limit */ 12#define USB_MAXALTSETTING 128 /* Hard limit */
13#define USB_MAXENDPOINTS 30 /* Hard limit */
14 13
15#define USB_MAXCONFIG 8 /* Arbitrary limit */ 14#define USB_MAXCONFIG 8 /* Arbitrary limit */
16 15
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index f3ba2e076ee3..12401ee4ba0e 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -769,6 +769,88 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
769 return ret; 769 return ret;
770} 770}
771 771
772static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
773 unsigned char ep)
774{
775 if (ep & USB_ENDPOINT_DIR_MASK)
776 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
777 else
778 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
779}
780
781static int parse_usbdevfs_streams(struct dev_state *ps,
782 struct usbdevfs_streams __user *streams,
783 unsigned int *num_streams_ret,
784 unsigned int *num_eps_ret,
785 struct usb_host_endpoint ***eps_ret,
786 struct usb_interface **intf_ret)
787{
788 unsigned int i, num_streams, num_eps;
789 struct usb_host_endpoint **eps;
790 struct usb_interface *intf = NULL;
791 unsigned char ep;
792 int ifnum, ret;
793
794 if (get_user(num_streams, &streams->num_streams) ||
795 get_user(num_eps, &streams->num_eps))
796 return -EFAULT;
797
798 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
799 return -EINVAL;
800
801 /* The XHCI controller allows max 2 ^ 16 streams */
802 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
803 return -EINVAL;
804
805 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
806 if (!eps)
807 return -ENOMEM;
808
809 for (i = 0; i < num_eps; i++) {
810 if (get_user(ep, &streams->eps[i])) {
811 ret = -EFAULT;
812 goto error;
813 }
814 eps[i] = ep_to_host_endpoint(ps->dev, ep);
815 if (!eps[i]) {
816 ret = -EINVAL;
817 goto error;
818 }
819
820 /* usb_alloc/free_streams operate on an usb_interface */
821 ifnum = findintfep(ps->dev, ep);
822 if (ifnum < 0) {
823 ret = ifnum;
824 goto error;
825 }
826
827 if (i == 0) {
828 ret = checkintf(ps, ifnum);
829 if (ret < 0)
830 goto error;
831 intf = usb_ifnum_to_if(ps->dev, ifnum);
832 } else {
833 /* Verify all eps belong to the same interface */
834 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
835 ret = -EINVAL;
836 goto error;
837 }
838 }
839 }
840
841 if (num_streams_ret)
842 *num_streams_ret = num_streams;
843 *num_eps_ret = num_eps;
844 *eps_ret = eps;
845 *intf_ret = intf;
846
847 return 0;
848
849error:
850 kfree(eps);
851 return ret;
852}
853
772static int match_devt(struct device *dev, void *data) 854static int match_devt(struct device *dev, void *data)
773{ 855{
774 return dev->devt == (dev_t) (unsigned long) data; 856 return dev->devt == (dev_t) (unsigned long) data;
@@ -1143,6 +1225,9 @@ static int proc_setintf(struct dev_state *ps, void __user *arg)
1143 return -EFAULT; 1225 return -EFAULT;
1144 if ((ret = checkintf(ps, setintf.interface))) 1226 if ((ret = checkintf(ps, setintf.interface)))
1145 return ret; 1227 return ret;
1228
1229 destroy_async_on_interface(ps, setintf.interface);
1230
1146 return usb_set_interface(ps->dev, setintf.interface, 1231 return usb_set_interface(ps->dev, setintf.interface,
1147 setintf.altsetting); 1232 setintf.altsetting);
1148} 1233}
@@ -1205,6 +1290,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1205 struct usb_ctrlrequest *dr = NULL; 1290 struct usb_ctrlrequest *dr = NULL;
1206 unsigned int u, totlen, isofrmlen; 1291 unsigned int u, totlen, isofrmlen;
1207 int i, ret, is_in, num_sgs = 0, ifnum = -1; 1292 int i, ret, is_in, num_sgs = 0, ifnum = -1;
1293 int number_of_packets = 0;
1294 unsigned int stream_id = 0;
1208 void *buf; 1295 void *buf;
1209 1296
1210 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP | 1297 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
@@ -1225,15 +1312,10 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1225 if (ret) 1312 if (ret)
1226 return ret; 1313 return ret;
1227 } 1314 }
1228 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) { 1315 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1229 is_in = 1;
1230 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1231 } else {
1232 is_in = 0;
1233 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1234 }
1235 if (!ep) 1316 if (!ep)
1236 return -ENOENT; 1317 return -ENOENT;
1318 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1237 1319
1238 u = 0; 1320 u = 0;
1239 switch(uurb->type) { 1321 switch(uurb->type) {
@@ -1258,7 +1340,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1258 le16_to_cpup(&dr->wIndex)); 1340 le16_to_cpup(&dr->wIndex));
1259 if (ret) 1341 if (ret)
1260 goto error; 1342 goto error;
1261 uurb->number_of_packets = 0;
1262 uurb->buffer_length = le16_to_cpup(&dr->wLength); 1343 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1263 uurb->buffer += 8; 1344 uurb->buffer += 8;
1264 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { 1345 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
@@ -1288,17 +1369,17 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1288 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT; 1369 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1289 goto interrupt_urb; 1370 goto interrupt_urb;
1290 } 1371 }
1291 uurb->number_of_packets = 0;
1292 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); 1372 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1293 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize) 1373 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1294 num_sgs = 0; 1374 num_sgs = 0;
1375 if (ep->streams)
1376 stream_id = uurb->stream_id;
1295 break; 1377 break;
1296 1378
1297 case USBDEVFS_URB_TYPE_INTERRUPT: 1379 case USBDEVFS_URB_TYPE_INTERRUPT:
1298 if (!usb_endpoint_xfer_int(&ep->desc)) 1380 if (!usb_endpoint_xfer_int(&ep->desc))
1299 return -EINVAL; 1381 return -EINVAL;
1300 interrupt_urb: 1382 interrupt_urb:
1301 uurb->number_of_packets = 0;
1302 break; 1383 break;
1303 1384
1304 case USBDEVFS_URB_TYPE_ISO: 1385 case USBDEVFS_URB_TYPE_ISO:
@@ -1308,15 +1389,16 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1308 return -EINVAL; 1389 return -EINVAL;
1309 if (!usb_endpoint_xfer_isoc(&ep->desc)) 1390 if (!usb_endpoint_xfer_isoc(&ep->desc))
1310 return -EINVAL; 1391 return -EINVAL;
1392 number_of_packets = uurb->number_of_packets;
1311 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * 1393 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1312 uurb->number_of_packets; 1394 number_of_packets;
1313 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL))) 1395 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1314 return -ENOMEM; 1396 return -ENOMEM;
1315 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) { 1397 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1316 ret = -EFAULT; 1398 ret = -EFAULT;
1317 goto error; 1399 goto error;
1318 } 1400 }
1319 for (totlen = u = 0; u < uurb->number_of_packets; u++) { 1401 for (totlen = u = 0; u < number_of_packets; u++) {
1320 /* 1402 /*
1321 * arbitrary limit need for USB 3.0 1403 * arbitrary limit need for USB 3.0
1322 * bMaxBurst (0~15 allowed, 1~16 packets) 1404 * bMaxBurst (0~15 allowed, 1~16 packets)
@@ -1347,7 +1429,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1347 ret = -EFAULT; 1429 ret = -EFAULT;
1348 goto error; 1430 goto error;
1349 } 1431 }
1350 as = alloc_async(uurb->number_of_packets); 1432 as = alloc_async(number_of_packets);
1351 if (!as) { 1433 if (!as) {
1352 ret = -ENOMEM; 1434 ret = -ENOMEM;
1353 goto error; 1435 goto error;
@@ -1441,7 +1523,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1441 as->urb->setup_packet = (unsigned char *)dr; 1523 as->urb->setup_packet = (unsigned char *)dr;
1442 dr = NULL; 1524 dr = NULL;
1443 as->urb->start_frame = uurb->start_frame; 1525 as->urb->start_frame = uurb->start_frame;
1444 as->urb->number_of_packets = uurb->number_of_packets; 1526 as->urb->number_of_packets = number_of_packets;
1527 as->urb->stream_id = stream_id;
1445 if (uurb->type == USBDEVFS_URB_TYPE_ISO || 1528 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1446 ps->dev->speed == USB_SPEED_HIGH) 1529 ps->dev->speed == USB_SPEED_HIGH)
1447 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1); 1530 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
@@ -1449,7 +1532,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1449 as->urb->interval = ep->desc.bInterval; 1532 as->urb->interval = ep->desc.bInterval;
1450 as->urb->context = as; 1533 as->urb->context = as;
1451 as->urb->complete = async_completed; 1534 as->urb->complete = async_completed;
1452 for (totlen = u = 0; u < uurb->number_of_packets; u++) { 1535 for (totlen = u = 0; u < number_of_packets; u++) {
1453 as->urb->iso_frame_desc[u].offset = totlen; 1536 as->urb->iso_frame_desc[u].offset = totlen;
1454 as->urb->iso_frame_desc[u].length = isopkt[u].length; 1537 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1455 totlen += isopkt[u].length; 1538 totlen += isopkt[u].length;
@@ -1999,6 +2082,45 @@ static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
1999 return claimintf(ps, dc.interface); 2082 return claimintf(ps, dc.interface);
2000} 2083}
2001 2084
2085static int proc_alloc_streams(struct dev_state *ps, void __user *arg)
2086{
2087 unsigned num_streams, num_eps;
2088 struct usb_host_endpoint **eps;
2089 struct usb_interface *intf;
2090 int r;
2091
2092 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2093 &eps, &intf);
2094 if (r)
2095 return r;
2096
2097 destroy_async_on_interface(ps,
2098 intf->altsetting[0].desc.bInterfaceNumber);
2099
2100 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2101 kfree(eps);
2102 return r;
2103}
2104
2105static int proc_free_streams(struct dev_state *ps, void __user *arg)
2106{
2107 unsigned num_eps;
2108 struct usb_host_endpoint **eps;
2109 struct usb_interface *intf;
2110 int r;
2111
2112 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2113 if (r)
2114 return r;
2115
2116 destroy_async_on_interface(ps,
2117 intf->altsetting[0].desc.bInterfaceNumber);
2118
2119 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2120 kfree(eps);
2121 return r;
2122}
2123
2002/* 2124/*
2003 * NOTE: All requests here that have interface numbers as parameters 2125 * NOTE: All requests here that have interface numbers as parameters
2004 * are assuming that somehow the configuration has been prevented from 2126 * are assuming that somehow the configuration has been prevented from
@@ -2175,6 +2297,12 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2175 case USBDEVFS_DISCONNECT_CLAIM: 2297 case USBDEVFS_DISCONNECT_CLAIM:
2176 ret = proc_disconnect_claim(ps, p); 2298 ret = proc_disconnect_claim(ps, p);
2177 break; 2299 break;
2300 case USBDEVFS_ALLOC_STREAMS:
2301 ret = proc_alloc_streams(ps, p);
2302 break;
2303 case USBDEVFS_FREE_STREAMS:
2304 ret = proc_free_streams(ps, p);
2305 break;
2178 } 2306 }
2179 usb_unlock_device(dev); 2307 usb_unlock_device(dev);
2180 if (ret >= 0) 2308 if (ret >= 0)
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 85e0450a2bc7..08283d40616c 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -400,8 +400,9 @@ static int usb_unbind_interface(struct device *dev)
400{ 400{
401 struct usb_driver *driver = to_usb_driver(dev->driver); 401 struct usb_driver *driver = to_usb_driver(dev->driver);
402 struct usb_interface *intf = to_usb_interface(dev); 402 struct usb_interface *intf = to_usb_interface(dev);
403 struct usb_host_endpoint *ep, **eps = NULL;
403 struct usb_device *udev; 404 struct usb_device *udev;
404 int error, r, lpm_disable_error; 405 int i, j, error, r, lpm_disable_error;
405 406
406 intf->condition = USB_INTERFACE_UNBINDING; 407 intf->condition = USB_INTERFACE_UNBINDING;
407 408
@@ -425,6 +426,26 @@ static int usb_unbind_interface(struct device *dev)
425 driver->disconnect(intf); 426 driver->disconnect(intf);
426 usb_cancel_queued_reset(intf); 427 usb_cancel_queued_reset(intf);
427 428
429 /* Free streams */
430 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
431 ep = &intf->cur_altsetting->endpoint[i];
432 if (ep->streams == 0)
433 continue;
434 if (j == 0) {
435 eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
436 GFP_KERNEL);
437 if (!eps) {
438 dev_warn(dev, "oom, leaking streams\n");
439 break;
440 }
441 }
442 eps[j++] = ep;
443 }
444 if (j) {
445 usb_free_streams(intf, eps, j, GFP_KERNEL);
446 kfree(eps);
447 }
448
428 /* Reset other interface state. 449 /* Reset other interface state.
429 * We cannot do a Set-Interface if the device is suspended or 450 * We cannot do a Set-Interface if the device is suspended or
430 * if it is prepared for a system sleep (since installing a new 451 * if it is prepared for a system sleep (since installing a new
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 2518c3250750..9c4e2922b04d 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2049,7 +2049,7 @@ int usb_alloc_streams(struct usb_interface *interface,
2049{ 2049{
2050 struct usb_hcd *hcd; 2050 struct usb_hcd *hcd;
2051 struct usb_device *dev; 2051 struct usb_device *dev;
2052 int i; 2052 int i, ret;
2053 2053
2054 dev = interface_to_usbdev(interface); 2054 dev = interface_to_usbdev(interface);
2055 hcd = bus_to_hcd(dev->bus); 2055 hcd = bus_to_hcd(dev->bus);
@@ -2058,13 +2058,24 @@ int usb_alloc_streams(struct usb_interface *interface,
2058 if (dev->speed != USB_SPEED_SUPER) 2058 if (dev->speed != USB_SPEED_SUPER)
2059 return -EINVAL; 2059 return -EINVAL;
2060 2060
2061 /* Streams only apply to bulk endpoints. */ 2061 for (i = 0; i < num_eps; i++) {
2062 for (i = 0; i < num_eps; i++) 2062 /* Streams only apply to bulk endpoints. */
2063 if (!usb_endpoint_xfer_bulk(&eps[i]->desc)) 2063 if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
2064 return -EINVAL; 2064 return -EINVAL;
2065 /* Re-alloc is not allowed */
2066 if (eps[i]->streams)
2067 return -EINVAL;
2068 }
2065 2069
2066 return hcd->driver->alloc_streams(hcd, dev, eps, num_eps, 2070 ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
2067 num_streams, mem_flags); 2071 num_streams, mem_flags);
2072 if (ret < 0)
2073 return ret;
2074
2075 for (i = 0; i < num_eps; i++)
2076 eps[i]->streams = ret;
2077
2078 return ret;
2068} 2079}
2069EXPORT_SYMBOL_GPL(usb_alloc_streams); 2080EXPORT_SYMBOL_GPL(usb_alloc_streams);
2070 2081
@@ -2078,8 +2089,7 @@ EXPORT_SYMBOL_GPL(usb_alloc_streams);
2078 * Reverts a group of bulk endpoints back to not using stream IDs. 2089 * Reverts a group of bulk endpoints back to not using stream IDs.
2079 * Can fail if we are given bad arguments, or HCD is broken. 2090 * Can fail if we are given bad arguments, or HCD is broken.
2080 * 2091 *
2081 * Return: On success, the number of allocated streams. On failure, a negative 2092 * Return: 0 on success. On failure, a negative error code.
2082 * error code.
2083 */ 2093 */
2084int usb_free_streams(struct usb_interface *interface, 2094int usb_free_streams(struct usb_interface *interface,
2085 struct usb_host_endpoint **eps, unsigned int num_eps, 2095 struct usb_host_endpoint **eps, unsigned int num_eps,
@@ -2087,19 +2097,26 @@ int usb_free_streams(struct usb_interface *interface,
2087{ 2097{
2088 struct usb_hcd *hcd; 2098 struct usb_hcd *hcd;
2089 struct usb_device *dev; 2099 struct usb_device *dev;
2090 int i; 2100 int i, ret;
2091 2101
2092 dev = interface_to_usbdev(interface); 2102 dev = interface_to_usbdev(interface);
2093 hcd = bus_to_hcd(dev->bus); 2103 hcd = bus_to_hcd(dev->bus);
2094 if (dev->speed != USB_SPEED_SUPER) 2104 if (dev->speed != USB_SPEED_SUPER)
2095 return -EINVAL; 2105 return -EINVAL;
2096 2106
2097 /* Streams only apply to bulk endpoints. */ 2107 /* Double-free is not allowed */
2098 for (i = 0; i < num_eps; i++) 2108 for (i = 0; i < num_eps; i++)
2099 if (!eps[i] || !usb_endpoint_xfer_bulk(&eps[i]->desc)) 2109 if (!eps[i] || !eps[i]->streams)
2100 return -EINVAL; 2110 return -EINVAL;
2101 2111
2102 return hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags); 2112 ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
2113 if (ret < 0)
2114 return ret;
2115
2116 for (i = 0; i < num_eps; i++)
2117 eps[i]->streams = 0;
2118
2119 return ret;
2103} 2120}
2104EXPORT_SYMBOL_GPL(usb_free_streams); 2121EXPORT_SYMBOL_GPL(usb_free_streams);
2105 2122
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index f6c6b6f7cc9c..5da5394127e9 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -141,19 +141,27 @@ static int usb_device_supports_lpm(struct usb_device *udev)
141 return 0; 141 return 0;
142 } 142 }
143 143
144 /* All USB 3.0 must support LPM, but we need their max exit latency 144 /*
145 * information from the SuperSpeed Extended Capabilities BOS descriptor. 145 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
146 * However, there are some that don't, and they set the U1/U2 exit
147 * latencies to zero.
146 */ 148 */
147 if (!udev->bos->ss_cap) { 149 if (!udev->bos->ss_cap) {
148 dev_warn(&udev->dev, "No LPM exit latency info found. " 150 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
149 "Power management will be impacted.\n"); 151 return 0;
152 }
153
154 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
155 udev->bos->ss_cap->bU2DevExitLat == 0) {
156 if (udev->parent)
157 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
158 else
159 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
150 return 0; 160 return 0;
151 } 161 }
152 if (udev->parent->lpm_capable)
153 return 1;
154 162
155 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. " 163 if (!udev->parent || udev->parent->lpm_capable)
156 "Power management will be impacted.\n"); 164 return 1;
157 return 0; 165 return 0;
158} 166}
159 167
@@ -4750,6 +4758,8 @@ static void hub_events(void)
4750 4758
4751 /* deal with port status changes */ 4759 /* deal with port status changes */
4752 for (i = 1; i <= hdev->maxchild; i++) { 4760 for (i = 1; i <= hdev->maxchild; i++) {
4761 struct usb_device *udev = hub->ports[i - 1]->child;
4762
4753 if (test_bit(i, hub->busy_bits)) 4763 if (test_bit(i, hub->busy_bits))
4754 continue; 4764 continue;
4755 connect_change = test_bit(i, hub->change_bits); 4765 connect_change = test_bit(i, hub->change_bits);
@@ -4848,8 +4858,6 @@ static void hub_events(void)
4848 */ 4858 */
4849 if (hub_port_warm_reset_required(hub, portstatus)) { 4859 if (hub_port_warm_reset_required(hub, portstatus)) {
4850 int status; 4860 int status;
4851 struct usb_device *udev =
4852 hub->ports[i - 1]->child;
4853 4861
4854 dev_dbg(hub_dev, "warm reset port %d\n", i); 4862 dev_dbg(hub_dev, "warm reset port %d\n", i);
4855 if (!udev || 4863 if (!udev ||
@@ -4866,6 +4874,24 @@ static void hub_events(void)
4866 usb_unlock_device(udev); 4874 usb_unlock_device(udev);
4867 connect_change = 0; 4875 connect_change = 0;
4868 } 4876 }
4877 /*
4878 * On disconnect USB3 protocol ports transit from U0 to
4879 * SS.Inactive to Rx.Detect. If this happens a warm-
4880 * reset is not needed, but a (re)connect may happen
4881 * before khubd runs and sees the disconnect, and the
4882 * device may be an unknown state.
4883 *
4884 * If the port went through SS.Inactive without khubd
4885 * seeing it the C_LINK_STATE change flag will be set,
4886 * and we reset the dev to put it in a known state.
4887 */
4888 } else if (udev && hub_is_superspeed(hub->hdev) &&
4889 (portchange & USB_PORT_STAT_C_LINK_STATE) &&
4890 (portstatus & USB_PORT_STAT_CONNECTION)) {
4891 usb_lock_device(udev);
4892 usb_reset_device(udev);
4893 usb_unlock_device(udev);
4894 connect_change = 0;
4869 } 4895 }
4870 4896
4871 if (connect_change) 4897 if (connect_change)
@@ -5123,7 +5149,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5123 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 5149 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
5124 struct usb_device_descriptor descriptor = udev->descriptor; 5150 struct usb_device_descriptor descriptor = udev->descriptor;
5125 struct usb_host_bos *bos; 5151 struct usb_host_bos *bos;
5126 int i, ret = 0; 5152 int i, j, ret = 0;
5127 int port1 = udev->portnum; 5153 int port1 = udev->portnum;
5128 5154
5129 if (udev->state == USB_STATE_NOTATTACHED || 5155 if (udev->state == USB_STATE_NOTATTACHED ||
@@ -5249,6 +5275,9 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
5249 ret); 5275 ret);
5250 goto re_enumerate; 5276 goto re_enumerate;
5251 } 5277 }
5278 /* Resetting also frees any allocated streams */
5279 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5280 intf->cur_altsetting->endpoint[j].streams = 0;
5252 } 5281 }
5253 5282
5254done: 5283done:
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index f829a1aad1c3..964695741031 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1293,8 +1293,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1293 struct usb_interface *iface; 1293 struct usb_interface *iface;
1294 struct usb_host_interface *alt; 1294 struct usb_host_interface *alt;
1295 struct usb_hcd *hcd = bus_to_hcd(dev->bus); 1295 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1296 int ret; 1296 int i, ret, manual = 0;
1297 int manual = 0;
1298 unsigned int epaddr; 1297 unsigned int epaddr;
1299 unsigned int pipe; 1298 unsigned int pipe;
1300 1299
@@ -1329,6 +1328,10 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1329 mutex_unlock(hcd->bandwidth_mutex); 1328 mutex_unlock(hcd->bandwidth_mutex);
1330 return -ENOMEM; 1329 return -ENOMEM;
1331 } 1330 }
1331 /* Changing alt-setting also frees any allocated streams */
1332 for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
1333 iface->cur_altsetting->endpoint[i].streams = 0;
1334
1332 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt); 1335 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1333 if (ret < 0) { 1336 if (ret < 0) {
1334 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n", 1337 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
index 9992fbfec85f..1ad6bc1951c7 100644
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -732,9 +732,11 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
732 /* Set the U1 and U2 exit latencies. */ 732 /* Set the U1 and U2 exit latencies. */
733 memcpy(buf, &usb_bos_descriptor, 733 memcpy(buf, &usb_bos_descriptor,
734 USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE); 734 USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
735 temp = readl(&xhci->cap_regs->hcs_params3); 735 if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
736 buf[12] = HCS_U1_LATENCY(temp); 736 temp = readl(&xhci->cap_regs->hcs_params3);
737 put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]); 737 buf[12] = HCS_U1_LATENCY(temp);
738 put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
739 }
738 740
739 /* Indicate whether the host has LTM support. */ 741 /* Indicate whether the host has LTM support. */
740 temp = readl(&xhci->cap_regs->hcc_params); 742 temp = readl(&xhci->cap_regs->hcc_params);
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index bce4391a0e7d..c089668308ad 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -149,14 +149,140 @@ static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring,
149 } 149 }
150} 150}
151 151
152/*
153 * We need a radix tree for mapping physical addresses of TRBs to which stream
154 * ID they belong to. We need to do this because the host controller won't tell
155 * us which stream ring the TRB came from. We could store the stream ID in an
156 * event data TRB, but that doesn't help us for the cancellation case, since the
157 * endpoint may stop before it reaches that event data TRB.
158 *
159 * The radix tree maps the upper portion of the TRB DMA address to a ring
160 * segment that has the same upper portion of DMA addresses. For example, say I
161 * have segments of size 1KB, that are always 1KB aligned. A segment may
162 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
163 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
164 * pass the radix tree a key to get the right stream ID:
165 *
166 * 0x10c90fff >> 10 = 0x43243
167 * 0x10c912c0 >> 10 = 0x43244
168 * 0x10c91400 >> 10 = 0x43245
169 *
170 * Obviously, only those TRBs with DMA addresses that are within the segment
171 * will make the radix tree return the stream ID for that ring.
172 *
173 * Caveats for the radix tree:
174 *
175 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
176 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
177 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
178 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
179 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
180 * extended systems (where the DMA address can be bigger than 32-bits),
181 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
182 */
183static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map,
184 struct xhci_ring *ring,
185 struct xhci_segment *seg,
186 gfp_t mem_flags)
187{
188 unsigned long key;
189 int ret;
190
191 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
192 /* Skip any segments that were already added. */
193 if (radix_tree_lookup(trb_address_map, key))
194 return 0;
195
196 ret = radix_tree_maybe_preload(mem_flags);
197 if (ret)
198 return ret;
199 ret = radix_tree_insert(trb_address_map,
200 key, ring);
201 radix_tree_preload_end();
202 return ret;
203}
204
205static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map,
206 struct xhci_segment *seg)
207{
208 unsigned long key;
209
210 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
211 if (radix_tree_lookup(trb_address_map, key))
212 radix_tree_delete(trb_address_map, key);
213}
214
215static int xhci_update_stream_segment_mapping(
216 struct radix_tree_root *trb_address_map,
217 struct xhci_ring *ring,
218 struct xhci_segment *first_seg,
219 struct xhci_segment *last_seg,
220 gfp_t mem_flags)
221{
222 struct xhci_segment *seg;
223 struct xhci_segment *failed_seg;
224 int ret;
225
226 if (WARN_ON_ONCE(trb_address_map == NULL))
227 return 0;
228
229 seg = first_seg;
230 do {
231 ret = xhci_insert_segment_mapping(trb_address_map,
232 ring, seg, mem_flags);
233 if (ret)
234 goto remove_streams;
235 if (seg == last_seg)
236 return 0;
237 seg = seg->next;
238 } while (seg != first_seg);
239
240 return 0;
241
242remove_streams:
243 failed_seg = seg;
244 seg = first_seg;
245 do {
246 xhci_remove_segment_mapping(trb_address_map, seg);
247 if (seg == failed_seg)
248 return ret;
249 seg = seg->next;
250 } while (seg != first_seg);
251
252 return ret;
253}
254
255static void xhci_remove_stream_mapping(struct xhci_ring *ring)
256{
257 struct xhci_segment *seg;
258
259 if (WARN_ON_ONCE(ring->trb_address_map == NULL))
260 return;
261
262 seg = ring->first_seg;
263 do {
264 xhci_remove_segment_mapping(ring->trb_address_map, seg);
265 seg = seg->next;
266 } while (seg != ring->first_seg);
267}
268
269static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
270{
271 return xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
272 ring->first_seg, ring->last_seg, mem_flags);
273}
274
152/* XXX: Do we need the hcd structure in all these functions? */ 275/* XXX: Do we need the hcd structure in all these functions? */
153void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring) 276void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
154{ 277{
155 if (!ring) 278 if (!ring)
156 return; 279 return;
157 280
158 if (ring->first_seg) 281 if (ring->first_seg) {
282 if (ring->type == TYPE_STREAM)
283 xhci_remove_stream_mapping(ring);
159 xhci_free_segments_for_ring(xhci, ring->first_seg); 284 xhci_free_segments_for_ring(xhci, ring->first_seg);
285 }
160 286
161 kfree(ring); 287 kfree(ring);
162} 288}
@@ -349,6 +475,21 @@ int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
349 if (ret) 475 if (ret)
350 return -ENOMEM; 476 return -ENOMEM;
351 477
478 if (ring->type == TYPE_STREAM)
479 ret = xhci_update_stream_segment_mapping(ring->trb_address_map,
480 ring, first, last, flags);
481 if (ret) {
482 struct xhci_segment *next;
483 do {
484 next = first->next;
485 xhci_segment_free(xhci, first);
486 if (first == last)
487 break;
488 first = next;
489 } while (true);
490 return ret;
491 }
492
352 xhci_link_rings(xhci, ring, first, last, num_segs); 493 xhci_link_rings(xhci, ring, first, last, num_segs);
353 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, 494 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
354 "ring expansion succeed, now has %d segments", 495 "ring expansion succeed, now has %d segments",
@@ -434,12 +575,12 @@ static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
434 struct xhci_stream_ctx *stream_ctx, dma_addr_t dma) 575 struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
435{ 576{
436 struct device *dev = xhci_to_hcd(xhci)->self.controller; 577 struct device *dev = xhci_to_hcd(xhci)->self.controller;
578 size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
437 579
438 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE) 580 if (size > MEDIUM_STREAM_ARRAY_SIZE)
439 dma_free_coherent(dev, 581 dma_free_coherent(dev, size,
440 sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
441 stream_ctx, dma); 582 stream_ctx, dma);
442 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE) 583 else if (size <= SMALL_STREAM_ARRAY_SIZE)
443 return dma_pool_free(xhci->small_streams_pool, 584 return dma_pool_free(xhci->small_streams_pool,
444 stream_ctx, dma); 585 stream_ctx, dma);
445 else 586 else
@@ -462,12 +603,12 @@ static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
462 gfp_t mem_flags) 603 gfp_t mem_flags)
463{ 604{
464 struct device *dev = xhci_to_hcd(xhci)->self.controller; 605 struct device *dev = xhci_to_hcd(xhci)->self.controller;
606 size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
465 607
466 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE) 608 if (size > MEDIUM_STREAM_ARRAY_SIZE)
467 return dma_alloc_coherent(dev, 609 return dma_alloc_coherent(dev, size,
468 sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
469 dma, mem_flags); 610 dma, mem_flags);
470 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE) 611 else if (size <= SMALL_STREAM_ARRAY_SIZE)
471 return dma_pool_alloc(xhci->small_streams_pool, 612 return dma_pool_alloc(xhci->small_streams_pool,
472 mem_flags, dma); 613 mem_flags, dma);
473 else 614 else
@@ -510,36 +651,6 @@ struct xhci_ring *xhci_stream_id_to_ring(
510 * The number of stream contexts in the stream context array may be bigger than 651 * The number of stream contexts in the stream context array may be bigger than
511 * the number of streams the driver wants to use. This is because the number of 652 * the number of streams the driver wants to use. This is because the number of
512 * stream context array entries must be a power of two. 653 * stream context array entries must be a power of two.
513 *
514 * We need a radix tree for mapping physical addresses of TRBs to which stream
515 * ID they belong to. We need to do this because the host controller won't tell
516 * us which stream ring the TRB came from. We could store the stream ID in an
517 * event data TRB, but that doesn't help us for the cancellation case, since the
518 * endpoint may stop before it reaches that event data TRB.
519 *
520 * The radix tree maps the upper portion of the TRB DMA address to a ring
521 * segment that has the same upper portion of DMA addresses. For example, say I
522 * have segments of size 1KB, that are always 64-byte aligned. A segment may
523 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
524 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
525 * pass the radix tree a key to get the right stream ID:
526 *
527 * 0x10c90fff >> 10 = 0x43243
528 * 0x10c912c0 >> 10 = 0x43244
529 * 0x10c91400 >> 10 = 0x43245
530 *
531 * Obviously, only those TRBs with DMA addresses that are within the segment
532 * will make the radix tree return the stream ID for that ring.
533 *
534 * Caveats for the radix tree:
535 *
536 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
537 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
538 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
539 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
540 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
541 * extended systems (where the DMA address can be bigger than 32-bits),
542 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
543 */ 654 */
544struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci, 655struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
545 unsigned int num_stream_ctxs, 656 unsigned int num_stream_ctxs,
@@ -548,7 +659,6 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
548 struct xhci_stream_info *stream_info; 659 struct xhci_stream_info *stream_info;
549 u32 cur_stream; 660 u32 cur_stream;
550 struct xhci_ring *cur_ring; 661 struct xhci_ring *cur_ring;
551 unsigned long key;
552 u64 addr; 662 u64 addr;
553 int ret; 663 int ret;
554 664
@@ -603,6 +713,7 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
603 if (!cur_ring) 713 if (!cur_ring)
604 goto cleanup_rings; 714 goto cleanup_rings;
605 cur_ring->stream_id = cur_stream; 715 cur_ring->stream_id = cur_stream;
716 cur_ring->trb_address_map = &stream_info->trb_address_map;
606 /* Set deq ptr, cycle bit, and stream context type */ 717 /* Set deq ptr, cycle bit, and stream context type */
607 addr = cur_ring->first_seg->dma | 718 addr = cur_ring->first_seg->dma |
608 SCT_FOR_CTX(SCT_PRI_TR) | 719 SCT_FOR_CTX(SCT_PRI_TR) |
@@ -612,10 +723,7 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
612 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n", 723 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
613 cur_stream, (unsigned long long) addr); 724 cur_stream, (unsigned long long) addr);
614 725
615 key = (unsigned long) 726 ret = xhci_update_stream_mapping(cur_ring, mem_flags);
616 (cur_ring->first_seg->dma >> TRB_SEGMENT_SHIFT);
617 ret = radix_tree_insert(&stream_info->trb_address_map,
618 key, cur_ring);
619 if (ret) { 727 if (ret) {
620 xhci_ring_free(xhci, cur_ring); 728 xhci_ring_free(xhci, cur_ring);
621 stream_info->stream_rings[cur_stream] = NULL; 729 stream_info->stream_rings[cur_stream] = NULL;
@@ -635,9 +743,6 @@ cleanup_rings:
635 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) { 743 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
636 cur_ring = stream_info->stream_rings[cur_stream]; 744 cur_ring = stream_info->stream_rings[cur_stream];
637 if (cur_ring) { 745 if (cur_ring) {
638 addr = cur_ring->first_seg->dma;
639 radix_tree_delete(&stream_info->trb_address_map,
640 addr >> TRB_SEGMENT_SHIFT);
641 xhci_ring_free(xhci, cur_ring); 746 xhci_ring_free(xhci, cur_ring);
642 stream_info->stream_rings[cur_stream] = NULL; 747 stream_info->stream_rings[cur_stream] = NULL;
643 } 748 }
@@ -698,7 +803,6 @@ void xhci_free_stream_info(struct xhci_hcd *xhci,
698{ 803{
699 int cur_stream; 804 int cur_stream;
700 struct xhci_ring *cur_ring; 805 struct xhci_ring *cur_ring;
701 dma_addr_t addr;
702 806
703 if (!stream_info) 807 if (!stream_info)
704 return; 808 return;
@@ -707,9 +811,6 @@ void xhci_free_stream_info(struct xhci_hcd *xhci,
707 cur_stream++) { 811 cur_stream++) {
708 cur_ring = stream_info->stream_rings[cur_stream]; 812 cur_ring = stream_info->stream_rings[cur_stream];
709 if (cur_ring) { 813 if (cur_ring) {
710 addr = cur_ring->first_seg->dma;
711 radix_tree_delete(&stream_info->trb_address_map,
712 addr >> TRB_SEGMENT_SHIFT);
713 xhci_ring_free(xhci, cur_ring); 814 xhci_ring_free(xhci, cur_ring);
714 stream_info->stream_rings[cur_stream] = NULL; 815 stream_info->stream_rings[cur_stream] = NULL;
715 } 816 }
@@ -1711,7 +1812,6 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
1711 1812
1712 if (xhci->lpm_command) 1813 if (xhci->lpm_command)
1713 xhci_free_command(xhci, xhci->lpm_command); 1814 xhci_free_command(xhci, xhci->lpm_command);
1714 xhci->cmd_ring_reserved_trbs = 0;
1715 if (xhci->cmd_ring) 1815 if (xhci->cmd_ring)
1716 xhci_ring_free(xhci, xhci->cmd_ring); 1816 xhci_ring_free(xhci, xhci->cmd_ring);
1717 xhci->cmd_ring = NULL; 1817 xhci->cmd_ring = NULL;
@@ -1776,6 +1876,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
1776 } 1876 }
1777 1877
1778no_bw: 1878no_bw:
1879 xhci->cmd_ring_reserved_trbs = 0;
1779 xhci->num_usb2_ports = 0; 1880 xhci->num_usb2_ports = 0;
1780 xhci->num_usb3_ports = 0; 1881 xhci->num_usb3_ports = 0;
1781 xhci->num_active_eps = 0; 1882 xhci->num_active_eps = 0;
@@ -2274,11 +2375,12 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
2274 /* 2375 /*
2275 * Initialize the ring segment pool. The ring must be a contiguous 2376 * Initialize the ring segment pool. The ring must be a contiguous
2276 * structure comprised of TRBs. The TRBs must be 16 byte aligned, 2377 * structure comprised of TRBs. The TRBs must be 16 byte aligned,
2277 * however, the command ring segment needs 64-byte aligned segments, 2378 * however, the command ring segment needs 64-byte aligned segments
2278 * so we pick the greater alignment need. 2379 * and our use of dma addresses in the trb_address_map radix tree needs
2380 * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need.
2279 */ 2381 */
2280 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev, 2382 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
2281 TRB_SEGMENT_SIZE, 64, xhci->page_size); 2383 TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size);
2282 2384
2283 /* See Table 46 and Note on Figure 55 */ 2385 /* See Table 46 and Note on Figure 55 */
2284 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev, 2386 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index 04f986d9234f..47390e369cd4 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -190,6 +190,10 @@ static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
190 struct usb_hcd *hcd; 190 struct usb_hcd *hcd;
191 191
192 driver = (struct hc_driver *)id->driver_data; 192 driver = (struct hc_driver *)id->driver_data;
193
194 /* Prevent runtime suspending between USB-2 and USB-3 initialization */
195 pm_runtime_get_noresume(&dev->dev);
196
193 /* Register the USB 2.0 roothub. 197 /* Register the USB 2.0 roothub.
194 * FIXME: USB core must know to register the USB 2.0 roothub first. 198 * FIXME: USB core must know to register the USB 2.0 roothub first.
195 * This is sort of silly, because we could just set the HCD driver flags 199 * This is sort of silly, because we could just set the HCD driver flags
@@ -199,7 +203,7 @@ static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
199 retval = usb_hcd_pci_probe(dev, id); 203 retval = usb_hcd_pci_probe(dev, id);
200 204
201 if (retval) 205 if (retval)
202 return retval; 206 goto put_runtime_pm;
203 207
204 /* USB 2.0 roothub is stored in the PCI device now. */ 208 /* USB 2.0 roothub is stored in the PCI device now. */
205 hcd = dev_get_drvdata(&dev->dev); 209 hcd = dev_get_drvdata(&dev->dev);
@@ -222,11 +226,11 @@ static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
222 goto put_usb3_hcd; 226 goto put_usb3_hcd;
223 /* Roothub already marked as USB 3.0 speed */ 227 /* Roothub already marked as USB 3.0 speed */
224 228
225 /* We know the LPM timeout algorithms for this host, let the USB core 229 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
226 * enable and disable LPM for devices under the USB 3.0 roothub. 230 xhci->shared_hcd->can_do_streams = 1;
227 */ 231
228 if (xhci->quirks & XHCI_LPM_SUPPORT) 232 /* USB-2 and USB-3 roothubs initialized, allow runtime pm suspend */
229 hcd_to_bus(xhci->shared_hcd)->root_hub->lpm_capable = 1; 233 pm_runtime_put_noidle(&dev->dev);
230 234
231 return 0; 235 return 0;
232 236
@@ -234,6 +238,8 @@ put_usb3_hcd:
234 usb_put_hcd(xhci->shared_hcd); 238 usb_put_hcd(xhci->shared_hcd);
235dealloc_usb2_hcd: 239dealloc_usb2_hcd:
236 usb_hcd_pci_remove(dev); 240 usb_hcd_pci_remove(dev);
241put_runtime_pm:
242 pm_runtime_put_noidle(&dev->dev);
237 return retval; 243 return retval;
238} 244}
239 245
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 8affef910782..151901ce1ba9 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -158,6 +158,9 @@ static int xhci_plat_probe(struct platform_device *pdev)
158 */ 158 */
159 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci; 159 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
160 160
161 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
162 xhci->shared_hcd->can_do_streams = 1;
163
161 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); 164 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
162 if (ret) 165 if (ret)
163 goto put_usb3_hcd; 166 goto put_usb3_hcd;
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 0ed64eb68e48..5f926bea5ab1 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -546,9 +546,9 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
546 struct xhci_dequeue_state *state) 546 struct xhci_dequeue_state *state)
547{ 547{
548 struct xhci_virt_device *dev = xhci->devs[slot_id]; 548 struct xhci_virt_device *dev = xhci->devs[slot_id];
549 struct xhci_virt_ep *ep = &dev->eps[ep_index];
549 struct xhci_ring *ep_ring; 550 struct xhci_ring *ep_ring;
550 struct xhci_generic_trb *trb; 551 struct xhci_generic_trb *trb;
551 struct xhci_ep_ctx *ep_ctx;
552 dma_addr_t addr; 552 dma_addr_t addr;
553 553
554 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id, 554 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
@@ -573,8 +573,16 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
573 /* Dig out the cycle state saved by the xHC during the stop ep cmd */ 573 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
574 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 574 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
575 "Finding endpoint context"); 575 "Finding endpoint context");
576 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index); 576 /* 4.6.9 the css flag is written to the stream context for streams */
577 state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq); 577 if (ep->ep_state & EP_HAS_STREAMS) {
578 struct xhci_stream_ctx *ctx =
579 &ep->stream_info->stream_ctx_array[stream_id];
580 state->new_cycle_state = 0x1 & le64_to_cpu(ctx->stream_ring);
581 } else {
582 struct xhci_ep_ctx *ep_ctx
583 = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
584 state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
585 }
578 586
579 state->new_deq_ptr = cur_td->last_trb; 587 state->new_deq_ptr = cur_td->last_trb;
580 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 588 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
@@ -892,6 +900,57 @@ remove_finished_td:
892 /* Return to the event handler with xhci->lock re-acquired */ 900 /* Return to the event handler with xhci->lock re-acquired */
893} 901}
894 902
903static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring)
904{
905 struct xhci_td *cur_td;
906
907 while (!list_empty(&ring->td_list)) {
908 cur_td = list_first_entry(&ring->td_list,
909 struct xhci_td, td_list);
910 list_del_init(&cur_td->td_list);
911 if (!list_empty(&cur_td->cancelled_td_list))
912 list_del_init(&cur_td->cancelled_td_list);
913 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
914 }
915}
916
917static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci,
918 int slot_id, int ep_index)
919{
920 struct xhci_td *cur_td;
921 struct xhci_virt_ep *ep;
922 struct xhci_ring *ring;
923
924 ep = &xhci->devs[slot_id]->eps[ep_index];
925 if ((ep->ep_state & EP_HAS_STREAMS) ||
926 (ep->ep_state & EP_GETTING_NO_STREAMS)) {
927 int stream_id;
928
929 for (stream_id = 0; stream_id < ep->stream_info->num_streams;
930 stream_id++) {
931 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
932 "Killing URBs for slot ID %u, ep index %u, stream %u",
933 slot_id, ep_index, stream_id + 1);
934 xhci_kill_ring_urbs(xhci,
935 ep->stream_info->stream_rings[stream_id]);
936 }
937 } else {
938 ring = ep->ring;
939 if (!ring)
940 return;
941 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
942 "Killing URBs for slot ID %u, ep index %u",
943 slot_id, ep_index);
944 xhci_kill_ring_urbs(xhci, ring);
945 }
946 while (!list_empty(&ep->cancelled_td_list)) {
947 cur_td = list_first_entry(&ep->cancelled_td_list,
948 struct xhci_td, cancelled_td_list);
949 list_del_init(&cur_td->cancelled_td_list);
950 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
951 }
952}
953
895/* Watchdog timer function for when a stop endpoint command fails to complete. 954/* Watchdog timer function for when a stop endpoint command fails to complete.
896 * In this case, we assume the host controller is broken or dying or dead. The 955 * In this case, we assume the host controller is broken or dying or dead. The
897 * host may still be completing some other events, so we have to be careful to 956 * host may still be completing some other events, so we have to be careful to
@@ -915,9 +974,6 @@ void xhci_stop_endpoint_command_watchdog(unsigned long arg)
915{ 974{
916 struct xhci_hcd *xhci; 975 struct xhci_hcd *xhci;
917 struct xhci_virt_ep *ep; 976 struct xhci_virt_ep *ep;
918 struct xhci_virt_ep *temp_ep;
919 struct xhci_ring *ring;
920 struct xhci_td *cur_td;
921 int ret, i, j; 977 int ret, i, j;
922 unsigned long flags; 978 unsigned long flags;
923 979
@@ -974,34 +1030,8 @@ void xhci_stop_endpoint_command_watchdog(unsigned long arg)
974 for (i = 0; i < MAX_HC_SLOTS; i++) { 1030 for (i = 0; i < MAX_HC_SLOTS; i++) {
975 if (!xhci->devs[i]) 1031 if (!xhci->devs[i])
976 continue; 1032 continue;
977 for (j = 0; j < 31; j++) { 1033 for (j = 0; j < 31; j++)
978 temp_ep = &xhci->devs[i]->eps[j]; 1034 xhci_kill_endpoint_urbs(xhci, i, j);
979 ring = temp_ep->ring;
980 if (!ring)
981 continue;
982 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
983 "Killing URBs for slot ID %u, "
984 "ep index %u", i, j);
985 while (!list_empty(&ring->td_list)) {
986 cur_td = list_first_entry(&ring->td_list,
987 struct xhci_td,
988 td_list);
989 list_del_init(&cur_td->td_list);
990 if (!list_empty(&cur_td->cancelled_td_list))
991 list_del_init(&cur_td->cancelled_td_list);
992 xhci_giveback_urb_in_irq(xhci, cur_td,
993 -ESHUTDOWN);
994 }
995 while (!list_empty(&temp_ep->cancelled_td_list)) {
996 cur_td = list_first_entry(
997 &temp_ep->cancelled_td_list,
998 struct xhci_td,
999 cancelled_td_list);
1000 list_del_init(&cur_td->cancelled_td_list);
1001 xhci_giveback_urb_in_irq(xhci, cur_td,
1002 -ESHUTDOWN);
1003 }
1004 }
1005 } 1035 }
1006 spin_unlock_irqrestore(&xhci->lock, flags); 1036 spin_unlock_irqrestore(&xhci->lock, flags);
1007 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1037 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
@@ -1073,17 +1103,18 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
1073 unsigned int stream_id; 1103 unsigned int stream_id;
1074 struct xhci_ring *ep_ring; 1104 struct xhci_ring *ep_ring;
1075 struct xhci_virt_device *dev; 1105 struct xhci_virt_device *dev;
1106 struct xhci_virt_ep *ep;
1076 struct xhci_ep_ctx *ep_ctx; 1107 struct xhci_ep_ctx *ep_ctx;
1077 struct xhci_slot_ctx *slot_ctx; 1108 struct xhci_slot_ctx *slot_ctx;
1078 1109
1079 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 1110 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
1080 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2])); 1111 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
1081 dev = xhci->devs[slot_id]; 1112 dev = xhci->devs[slot_id];
1113 ep = &dev->eps[ep_index];
1082 1114
1083 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id); 1115 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
1084 if (!ep_ring) { 1116 if (!ep_ring) {
1085 xhci_warn(xhci, "WARN Set TR deq ptr command for " 1117 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n",
1086 "freed stream ID %u\n",
1087 stream_id); 1118 stream_id);
1088 /* XXX: Harmless??? */ 1119 /* XXX: Harmless??? */
1089 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING; 1120 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
@@ -1099,12 +1130,10 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
1099 1130
1100 switch (cmd_comp_code) { 1131 switch (cmd_comp_code) {
1101 case COMP_TRB_ERR: 1132 case COMP_TRB_ERR:
1102 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because " 1133 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n");
1103 "of stream ID configuration\n");
1104 break; 1134 break;
1105 case COMP_CTX_STATE: 1135 case COMP_CTX_STATE:
1106 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due " 1136 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n");
1107 "to incorrect slot or ep state.\n");
1108 ep_state = le32_to_cpu(ep_ctx->ep_info); 1137 ep_state = le32_to_cpu(ep_ctx->ep_info);
1109 ep_state &= EP_STATE_MASK; 1138 ep_state &= EP_STATE_MASK;
1110 slot_state = le32_to_cpu(slot_ctx->dev_state); 1139 slot_state = le32_to_cpu(slot_ctx->dev_state);
@@ -1114,13 +1143,12 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
1114 slot_state, ep_state); 1143 slot_state, ep_state);
1115 break; 1144 break;
1116 case COMP_EBADSLT: 1145 case COMP_EBADSLT:
1117 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because " 1146 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n",
1118 "slot %u was not enabled.\n", slot_id); 1147 slot_id);
1119 break; 1148 break;
1120 default: 1149 default:
1121 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown " 1150 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n",
1122 "completion code of %u.\n", 1151 cmd_comp_code);
1123 cmd_comp_code);
1124 break; 1152 break;
1125 } 1153 }
1126 /* OK what do we do now? The endpoint state is hosed, and we 1154 /* OK what do we do now? The endpoint state is hosed, and we
@@ -1130,23 +1158,28 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
1130 * cancelling URBs, which might not be an error... 1158 * cancelling URBs, which might not be an error...
1131 */ 1159 */
1132 } else { 1160 } else {
1161 u64 deq;
1162 /* 4.6.10 deq ptr is written to the stream ctx for streams */
1163 if (ep->ep_state & EP_HAS_STREAMS) {
1164 struct xhci_stream_ctx *ctx =
1165 &ep->stream_info->stream_ctx_array[stream_id];
1166 deq = le64_to_cpu(ctx->stream_ring) & SCTX_DEQ_MASK;
1167 } else {
1168 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK;
1169 }
1133 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1170 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1134 "Successful Set TR Deq Ptr cmd, deq = @%08llx", 1171 "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
1135 le64_to_cpu(ep_ctx->deq)); 1172 if (xhci_trb_virt_to_dma(ep->queued_deq_seg,
1136 if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg, 1173 ep->queued_deq_ptr) == deq) {
1137 dev->eps[ep_index].queued_deq_ptr) ==
1138 (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) {
1139 /* Update the ring's dequeue segment and dequeue pointer 1174 /* Update the ring's dequeue segment and dequeue pointer
1140 * to reflect the new position. 1175 * to reflect the new position.
1141 */ 1176 */
1142 update_ring_for_set_deq_completion(xhci, dev, 1177 update_ring_for_set_deq_completion(xhci, dev,
1143 ep_ring, ep_index); 1178 ep_ring, ep_index);
1144 } else { 1179 } else {
1145 xhci_warn(xhci, "Mismatch between completed Set TR Deq " 1180 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n");
1146 "Ptr command & xHCI internal state.\n");
1147 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", 1181 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
1148 dev->eps[ep_index].queued_deq_seg, 1182 ep->queued_deq_seg, ep->queued_deq_ptr);
1149 dev->eps[ep_index].queued_deq_ptr);
1150 } 1183 }
1151 } 1184 }
1152 1185
@@ -4070,6 +4103,7 @@ static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
4070 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 4103 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4071 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); 4104 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
4072 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id); 4105 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
4106 u32 trb_sct = 0;
4073 u32 type = TRB_TYPE(TRB_SET_DEQ); 4107 u32 type = TRB_TYPE(TRB_SET_DEQ);
4074 struct xhci_virt_ep *ep; 4108 struct xhci_virt_ep *ep;
4075 4109
@@ -4088,7 +4122,9 @@ static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
4088 } 4122 }
4089 ep->queued_deq_seg = deq_seg; 4123 ep->queued_deq_seg = deq_seg;
4090 ep->queued_deq_ptr = deq_ptr; 4124 ep->queued_deq_ptr = deq_ptr;
4091 return queue_command(xhci, lower_32_bits(addr) | cycle_state, 4125 if (stream_id)
4126 trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
4127 return queue_command(xhci, lower_32_bits(addr) | trb_sct | cycle_state,
4092 upper_32_bits(addr), trb_stream_id, 4128 upper_32_bits(addr), trb_stream_id,
4093 trb_slot_id | trb_ep_index | type, false); 4129 trb_slot_id | trb_ep_index | type, false);
4094} 4130}
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 6fe577d46fa2..652be2138b4b 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -390,6 +390,10 @@ static int xhci_try_enable_msi(struct usb_hcd *hcd)
390 } 390 }
391 391
392 legacy_irq: 392 legacy_irq:
393 if (!strlen(hcd->irq_descr))
394 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
395 hcd->driver->description, hcd->self.busnum);
396
393 /* fall back to legacy interrupt*/ 397 /* fall back to legacy interrupt*/
394 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED, 398 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
395 hcd->irq_descr, hcd); 399 hcd->irq_descr, hcd);
@@ -2678,6 +2682,20 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
2678 return ret; 2682 return ret;
2679} 2683}
2680 2684
2685static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2686 struct xhci_virt_device *vdev, int i)
2687{
2688 struct xhci_virt_ep *ep = &vdev->eps[i];
2689
2690 if (ep->ep_state & EP_HAS_STREAMS) {
2691 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2692 xhci_get_endpoint_address(i));
2693 xhci_free_stream_info(xhci, ep->stream_info);
2694 ep->stream_info = NULL;
2695 ep->ep_state &= ~EP_HAS_STREAMS;
2696 }
2697}
2698
2681/* Called after one or more calls to xhci_add_endpoint() or 2699/* Called after one or more calls to xhci_add_endpoint() or
2682 * xhci_drop_endpoint(). If this call fails, the USB core is expected 2700 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2683 * to call xhci_reset_bandwidth(). 2701 * to call xhci_reset_bandwidth().
@@ -2742,8 +2760,10 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2742 /* Free any rings that were dropped, but not changed. */ 2760 /* Free any rings that were dropped, but not changed. */
2743 for (i = 1; i < 31; ++i) { 2761 for (i = 1; i < 31; ++i) {
2744 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) && 2762 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2745 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) 2763 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
2746 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); 2764 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2765 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2766 }
2747 } 2767 }
2748 xhci_zero_in_ctx(xhci, virt_dev); 2768 xhci_zero_in_ctx(xhci, virt_dev);
2749 /* 2769 /*
@@ -2759,6 +2779,7 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2759 if (virt_dev->eps[i].ring) { 2779 if (virt_dev->eps[i].ring) {
2760 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); 2780 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2761 } 2781 }
2782 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2762 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; 2783 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2763 virt_dev->eps[i].new_ring = NULL; 2784 virt_dev->eps[i].new_ring = NULL;
2764 } 2785 }
@@ -2954,7 +2975,7 @@ static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2954 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__); 2975 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
2955 if (ret <= 0) 2976 if (ret <= 0)
2956 return -EINVAL; 2977 return -EINVAL;
2957 if (ep->ss_ep_comp.bmAttributes == 0) { 2978 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
2958 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" 2979 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2959 " descriptor for ep 0x%x does not support streams\n", 2980 " descriptor for ep 0x%x does not support streams\n",
2960 ep->desc.bEndpointAddress); 2981 ep->desc.bEndpointAddress);
@@ -3121,6 +3142,12 @@ int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3121 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", 3142 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3122 num_streams); 3143 num_streams);
3123 3144
3145 /* MaxPSASize value 0 (2 streams) means streams are not supported */
3146 if (HCC_MAX_PSA(xhci->hcc_params) < 4) {
3147 xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3148 return -ENOSYS;
3149 }
3150
3124 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags); 3151 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3125 if (!config_cmd) { 3152 if (!config_cmd) {
3126 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n"); 3153 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
@@ -3519,6 +3546,8 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
3519 struct xhci_virt_ep *ep = &virt_dev->eps[i]; 3546 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3520 3547
3521 if (ep->ep_state & EP_HAS_STREAMS) { 3548 if (ep->ep_state & EP_HAS_STREAMS) {
3549 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3550 xhci_get_endpoint_address(i));
3522 xhci_free_stream_info(xhci, ep->stream_info); 3551 xhci_free_stream_info(xhci, ep->stream_info);
3523 ep->stream_info = NULL; 3552 ep->stream_info = NULL;
3524 ep->ep_state &= ~EP_HAS_STREAMS; 3553 ep->ep_state &= ~EP_HAS_STREAMS;
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 58ed9d088e63..d280e9213d08 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -703,6 +703,7 @@ struct xhci_ep_ctx {
703 703
704/* deq bitmasks */ 704/* deq bitmasks */
705#define EP_CTX_CYCLE_MASK (1 << 0) 705#define EP_CTX_CYCLE_MASK (1 << 0)
706#define SCTX_DEQ_MASK (~0xfL)
706 707
707 708
708/** 709/**
@@ -1118,9 +1119,10 @@ enum xhci_setup_dev {
1118#define TRB_TO_SUSPEND_PORT(p) (((p) & (1 << 23)) >> 23) 1119#define TRB_TO_SUSPEND_PORT(p) (((p) & (1 << 23)) >> 23)
1119#define LAST_EP_INDEX 30 1120#define LAST_EP_INDEX 30
1120 1121
1121/* Set TR Dequeue Pointer command TRB fields */ 1122/* Set TR Dequeue Pointer command TRB fields, 6.4.3.9 */
1122#define TRB_TO_STREAM_ID(p) ((((p) & (0xffff << 16)) >> 16)) 1123#define TRB_TO_STREAM_ID(p) ((((p) & (0xffff << 16)) >> 16))
1123#define STREAM_ID_FOR_TRB(p) ((((p)) & 0xffff) << 16) 1124#define STREAM_ID_FOR_TRB(p) ((((p)) & 0xffff) << 16)
1125#define SCT_FOR_TRB(p) (((p) << 1) & 0x7)
1124 1126
1125 1127
1126/* Port Status Change Event TRB fields */ 1128/* Port Status Change Event TRB fields */
@@ -1341,6 +1343,7 @@ struct xhci_ring {
1341 unsigned int num_trbs_free_temp; 1343 unsigned int num_trbs_free_temp;
1342 enum xhci_ring_type type; 1344 enum xhci_ring_type type;
1343 bool last_td_was_short; 1345 bool last_td_was_short;
1346 struct radix_tree_root *trb_address_map;
1344}; 1347};
1345 1348
1346struct xhci_erst_entry { 1349struct xhci_erst_entry {
diff --git a/drivers/usb/storage/Kconfig b/drivers/usb/storage/Kconfig
index 1dd0604d1911..13b5bfbaf951 100644
--- a/drivers/usb/storage/Kconfig
+++ b/drivers/usb/storage/Kconfig
@@ -204,7 +204,7 @@ config USB_STORAGE_ENE_UB6250
204 204
205config USB_UAS 205config USB_UAS
206 tristate "USB Attached SCSI" 206 tristate "USB Attached SCSI"
207 depends on SCSI && BROKEN 207 depends on SCSI && USB_STORAGE
208 help 208 help
209 The USB Attached SCSI protocol is supported by some USB 209 The USB Attached SCSI protocol is supported by some USB
210 storage devices. It permits higher performance by supporting 210 storage devices. It permits higher performance by supporting
diff --git a/drivers/usb/storage/uas-detect.h b/drivers/usb/storage/uas-detect.h
new file mode 100644
index 000000000000..bb05b984d5f6
--- /dev/null
+++ b/drivers/usb/storage/uas-detect.h
@@ -0,0 +1,96 @@
1#include <linux/usb.h>
2#include <linux/usb/hcd.h>
3#include "usb.h"
4
5static 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
12static int uas_isnt_supported(struct usb_device *udev)
13{
14 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
15
16 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
17 "support scatter-gather which is\n",
18 hcd->driver->description);
19 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
20 "alternative USB controller if you wish to use UAS.\n");
21 return -ENODEV;
22}
23
24static int uas_find_uas_alt_setting(struct usb_interface *intf)
25{
26 int i;
27 struct usb_device *udev = interface_to_usbdev(intf);
28 int sg_supported = udev->bus->sg_tablesize != 0;
29
30 for (i = 0; i < intf->num_altsetting; i++) {
31 struct usb_host_interface *alt = &intf->altsetting[i];
32
33 if (uas_is_interface(alt)) {
34 if (!sg_supported)
35 return uas_isnt_supported(udev);
36 return alt->desc.bAlternateSetting;
37 }
38 }
39
40 return -ENODEV;
41}
42
43static int uas_find_endpoints(struct usb_host_interface *alt,
44 struct usb_host_endpoint *eps[])
45{
46 struct usb_host_endpoint *endpoint = alt->endpoint;
47 unsigned i, n_endpoints = alt->desc.bNumEndpoints;
48
49 for (i = 0; i < n_endpoints; i++) {
50 unsigned char *extra = endpoint[i].extra;
51 int len = endpoint[i].extralen;
52 while (len >= 3) {
53 if (extra[1] == USB_DT_PIPE_USAGE) {
54 unsigned pipe_id = extra[2];
55 if (pipe_id > 0 && pipe_id < 5)
56 eps[pipe_id - 1] = &endpoint[i];
57 break;
58 }
59 len -= extra[0];
60 extra += extra[0];
61 }
62 }
63
64 if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
65 return -ENODEV;
66
67 return 0;
68}
69
70static int uas_use_uas_driver(struct usb_interface *intf,
71 const struct usb_device_id *id)
72{
73 struct usb_host_endpoint *eps[4] = { };
74 struct usb_device *udev = interface_to_usbdev(intf);
75 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
76 unsigned long flags = id->driver_info;
77 int r, alt;
78
79 usb_stor_adjust_quirks(udev, &flags);
80
81 if (flags & US_FL_IGNORE_UAS)
82 return 0;
83
84 if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams)
85 return 0;
86
87 alt = uas_find_uas_alt_setting(intf);
88 if (alt < 0)
89 return 0;
90
91 r = uas_find_endpoints(&intf->altsetting[alt], eps);
92 if (r < 0)
93 return 0;
94
95 return 1;
96}
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index d966b59f7d7b..a7ac97cc5949 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -2,6 +2,7 @@
2 * USB Attached SCSI 2 * USB Attached SCSI
3 * Note that this is not the same as the USB Mass Storage driver 3 * Note that this is not the same as the USB Mass Storage driver
4 * 4 *
5 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013
5 * Copyright Matthew Wilcox for Intel Corp, 2010 6 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010 7 * Copyright Sarah Sharp for Intel Corp, 2010
7 * 8 *
@@ -13,17 +14,21 @@
13#include <linux/types.h> 14#include <linux/types.h>
14#include <linux/module.h> 15#include <linux/module.h>
15#include <linux/usb.h> 16#include <linux/usb.h>
17#include <linux/usb_usual.h>
16#include <linux/usb/hcd.h> 18#include <linux/usb/hcd.h>
17#include <linux/usb/storage.h> 19#include <linux/usb/storage.h>
18#include <linux/usb/uas.h> 20#include <linux/usb/uas.h>
19 21
20#include <scsi/scsi.h> 22#include <scsi/scsi.h>
23#include <scsi/scsi_eh.h>
21#include <scsi/scsi_dbg.h> 24#include <scsi/scsi_dbg.h>
22#include <scsi/scsi_cmnd.h> 25#include <scsi/scsi_cmnd.h>
23#include <scsi/scsi_device.h> 26#include <scsi/scsi_device.h>
24#include <scsi/scsi_host.h> 27#include <scsi/scsi_host.h>
25#include <scsi/scsi_tcq.h> 28#include <scsi/scsi_tcq.h>
26 29
30#include "uas-detect.h"
31
27/* 32/*
28 * The r00-r01c specs define this version of the SENSE IU data structure. 33 * The r00-r01c specs define this version of the SENSE IU data structure.
29 * It's still in use by several different firmware releases. 34 * It's still in use by several different firmware releases.
@@ -45,12 +50,17 @@ struct uas_dev_info {
45 struct usb_anchor sense_urbs; 50 struct usb_anchor sense_urbs;
46 struct usb_anchor data_urbs; 51 struct usb_anchor data_urbs;
47 int qdepth, resetting; 52 int qdepth, resetting;
48 struct response_ui response; 53 struct response_iu response;
49 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; 54 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
50 unsigned use_streams:1; 55 unsigned use_streams:1;
51 unsigned uas_sense_old:1; 56 unsigned uas_sense_old:1;
57 unsigned running_task:1;
58 unsigned shutdown:1;
52 struct scsi_cmnd *cmnd; 59 struct scsi_cmnd *cmnd;
53 spinlock_t lock; 60 spinlock_t lock;
61 struct work_struct work;
62 struct list_head inflight_list;
63 struct list_head dead_list;
54}; 64};
55 65
56enum { 66enum {
@@ -85,103 +95,117 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
85 struct uas_dev_info *devinfo, gfp_t gfp); 95 struct uas_dev_info *devinfo, gfp_t gfp);
86static void uas_do_work(struct work_struct *work); 96static void uas_do_work(struct work_struct *work);
87static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller); 97static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
98static void uas_free_streams(struct uas_dev_info *devinfo);
99static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
88 100
89static DECLARE_WORK(uas_work, uas_do_work); 101/* Must be called with devinfo->lock held, will temporary unlock the lock */
90static DEFINE_SPINLOCK(uas_work_lock);
91static LIST_HEAD(uas_work_list);
92
93static void uas_unlink_data_urbs(struct uas_dev_info *devinfo, 102static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
94 struct uas_cmd_info *cmdinfo) 103 struct uas_cmd_info *cmdinfo,
104 unsigned long *lock_flags)
95{ 105{
96 unsigned long flags;
97
98 /* 106 /*
99 * The UNLINK_DATA_URBS flag makes sure uas_try_complete 107 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
100 * (called by urb completion) doesn't release cmdinfo 108 * (called by urb completion) doesn't release cmdinfo
101 * underneath us. 109 * underneath us.
102 */ 110 */
103 spin_lock_irqsave(&devinfo->lock, flags);
104 cmdinfo->state |= UNLINK_DATA_URBS; 111 cmdinfo->state |= UNLINK_DATA_URBS;
105 spin_unlock_irqrestore(&devinfo->lock, flags); 112 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
106 113
107 if (cmdinfo->data_in_urb) 114 if (cmdinfo->data_in_urb)
108 usb_unlink_urb(cmdinfo->data_in_urb); 115 usb_unlink_urb(cmdinfo->data_in_urb);
109 if (cmdinfo->data_out_urb) 116 if (cmdinfo->data_out_urb)
110 usb_unlink_urb(cmdinfo->data_out_urb); 117 usb_unlink_urb(cmdinfo->data_out_urb);
111 118
112 spin_lock_irqsave(&devinfo->lock, flags); 119 spin_lock_irqsave(&devinfo->lock, *lock_flags);
113 cmdinfo->state &= ~UNLINK_DATA_URBS; 120 cmdinfo->state &= ~UNLINK_DATA_URBS;
114 spin_unlock_irqrestore(&devinfo->lock, flags);
115} 121}
116 122
117static void uas_do_work(struct work_struct *work) 123static void uas_do_work(struct work_struct *work)
118{ 124{
125 struct uas_dev_info *devinfo =
126 container_of(work, struct uas_dev_info, work);
119 struct uas_cmd_info *cmdinfo; 127 struct uas_cmd_info *cmdinfo;
120 struct uas_cmd_info *temp;
121 struct list_head list;
122 unsigned long flags; 128 unsigned long flags;
123 int err; 129 int err;
124 130
125 spin_lock_irq(&uas_work_lock); 131 spin_lock_irqsave(&devinfo->lock, flags);
126 list_replace_init(&uas_work_list, &list); 132 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
127 spin_unlock_irq(&uas_work_lock);
128
129 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
130 struct scsi_pointer *scp = (void *)cmdinfo; 133 struct scsi_pointer *scp = (void *)cmdinfo;
131 struct scsi_cmnd *cmnd = container_of(scp, 134 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
132 struct scsi_cmnd, SCp); 135 SCp);
133 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 136
134 spin_lock_irqsave(&devinfo->lock, flags); 137 if (!(cmdinfo->state & IS_IN_WORK_LIST))
135 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 138 continue;
139
140 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
136 if (!err) 141 if (!err)
137 cmdinfo->state &= ~IS_IN_WORK_LIST; 142 cmdinfo->state &= ~IS_IN_WORK_LIST;
138 spin_unlock_irqrestore(&devinfo->lock, flags); 143 else
139 if (err) { 144 schedule_work(&devinfo->work);
140 list_del(&cmdinfo->list);
141 spin_lock_irq(&uas_work_lock);
142 list_add_tail(&cmdinfo->list, &uas_work_list);
143 spin_unlock_irq(&uas_work_lock);
144 schedule_work(&uas_work);
145 }
146 } 145 }
146 spin_unlock_irqrestore(&devinfo->lock, flags);
147} 147}
148 148
149static void uas_abort_work(struct uas_dev_info *devinfo) 149static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
150 struct uas_cmd_info *cmdinfo,
151 int result, const char *caller)
152{
153 struct scsi_pointer *scp = (void *)cmdinfo;
154 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
155
156 uas_log_cmd_state(cmnd, caller);
157 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
158 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
159 cmdinfo->state |= COMMAND_ABORTED;
160 cmdinfo->state &= ~IS_IN_WORK_LIST;
161 cmnd->result = result << 16;
162 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
163}
164
165static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
166 const char *caller)
150{ 167{
151 struct uas_cmd_info *cmdinfo; 168 struct uas_cmd_info *cmdinfo;
152 struct uas_cmd_info *temp; 169 struct uas_cmd_info *temp;
153 struct list_head list;
154 unsigned long flags; 170 unsigned long flags;
155 171
156 spin_lock_irq(&uas_work_lock); 172 spin_lock_irqsave(&devinfo->lock, flags);
157 list_replace_init(&uas_work_list, &list); 173 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
158 spin_unlock_irq(&uas_work_lock); 174 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
175 spin_unlock_irqrestore(&devinfo->lock, flags);
176}
177
178static void uas_add_work(struct uas_cmd_info *cmdinfo)
179{
180 struct scsi_pointer *scp = (void *)cmdinfo;
181 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
182 struct uas_dev_info *devinfo = cmnd->device->hostdata;
183
184 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
185 cmdinfo->state |= IS_IN_WORK_LIST;
186 schedule_work(&devinfo->work);
187}
188
189static void uas_zap_dead(struct uas_dev_info *devinfo)
190{
191 struct uas_cmd_info *cmdinfo;
192 struct uas_cmd_info *temp;
193 unsigned long flags;
159 194
160 spin_lock_irqsave(&devinfo->lock, flags); 195 spin_lock_irqsave(&devinfo->lock, flags);
161 list_for_each_entry_safe(cmdinfo, temp, &list, list) { 196 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
162 struct scsi_pointer *scp = (void *)cmdinfo; 197 struct scsi_pointer *scp = (void *)cmdinfo;
163 struct scsi_cmnd *cmnd = container_of(scp, 198 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
164 struct scsi_cmnd, SCp); 199 SCp);
165 struct uas_dev_info *di = (void *)cmnd->device->hostdata; 200 uas_log_cmd_state(cmnd, __func__);
166 201 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
167 if (di == devinfo) { 202 /* all urbs are killed, clear inflight bits */
168 cmdinfo->state |= COMMAND_ABORTED; 203 cmdinfo->state &= ~(COMMAND_INFLIGHT |
169 cmdinfo->state &= ~IS_IN_WORK_LIST; 204 DATA_IN_URB_INFLIGHT |
170 if (devinfo->resetting) { 205 DATA_OUT_URB_INFLIGHT);
171 /* uas_stat_cmplt() will not do that 206 uas_try_complete(cmnd, __func__);
172 * when a device reset is in
173 * progress */
174 cmdinfo->state &= ~COMMAND_INFLIGHT;
175 }
176 uas_try_complete(cmnd, __func__);
177 } else {
178 /* not our uas device, relink into list */
179 list_del(&cmdinfo->list);
180 spin_lock_irq(&uas_work_lock);
181 list_add_tail(&cmdinfo->list, &uas_work_list);
182 spin_unlock_irq(&uas_work_lock);
183 }
184 } 207 }
208 devinfo->running_task = 0;
185 spin_unlock_irqrestore(&devinfo->lock, flags); 209 spin_unlock_irqrestore(&devinfo->lock, flags);
186} 210}
187 211
@@ -259,20 +283,19 @@ static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
259 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 283 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
260 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 284 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
261 285
262 WARN_ON(!spin_is_locked(&devinfo->lock)); 286 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
263 if (cmdinfo->state & (COMMAND_INFLIGHT | 287 if (cmdinfo->state & (COMMAND_INFLIGHT |
264 DATA_IN_URB_INFLIGHT | 288 DATA_IN_URB_INFLIGHT |
265 DATA_OUT_URB_INFLIGHT | 289 DATA_OUT_URB_INFLIGHT |
266 UNLINK_DATA_URBS)) 290 UNLINK_DATA_URBS))
267 return -EBUSY; 291 return -EBUSY;
268 BUG_ON(cmdinfo->state & COMMAND_COMPLETED); 292 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
269 cmdinfo->state |= COMMAND_COMPLETED; 293 cmdinfo->state |= COMMAND_COMPLETED;
270 usb_free_urb(cmdinfo->data_in_urb); 294 usb_free_urb(cmdinfo->data_in_urb);
271 usb_free_urb(cmdinfo->data_out_urb); 295 usb_free_urb(cmdinfo->data_out_urb);
272 if (cmdinfo->state & COMMAND_ABORTED) { 296 if (cmdinfo->state & COMMAND_ABORTED)
273 scmd_printk(KERN_INFO, cmnd, "abort completed\n"); 297 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
274 cmnd->result = DID_ABORT << 16; 298 list_del(&cmdinfo->list);
275 }
276 cmnd->scsi_done(cmnd); 299 cmnd->scsi_done(cmnd);
277 return 0; 300 return 0;
278} 301}
@@ -286,11 +309,7 @@ static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
286 cmdinfo->state |= direction | SUBMIT_STATUS_URB; 309 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
287 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 310 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
288 if (err) { 311 if (err) {
289 spin_lock(&uas_work_lock); 312 uas_add_work(cmdinfo);
290 list_add_tail(&cmdinfo->list, &uas_work_list);
291 cmdinfo->state |= IS_IN_WORK_LIST;
292 spin_unlock(&uas_work_lock);
293 schedule_work(&uas_work);
294 } 313 }
295} 314}
296 315
@@ -298,14 +317,20 @@ static void uas_stat_cmplt(struct urb *urb)
298{ 317{
299 struct iu *iu = urb->transfer_buffer; 318 struct iu *iu = urb->transfer_buffer;
300 struct Scsi_Host *shost = urb->context; 319 struct Scsi_Host *shost = urb->context;
301 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 320 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
302 struct scsi_cmnd *cmnd; 321 struct scsi_cmnd *cmnd;
303 struct uas_cmd_info *cmdinfo; 322 struct uas_cmd_info *cmdinfo;
304 unsigned long flags; 323 unsigned long flags;
305 u16 tag; 324 u16 tag;
306 325
307 if (urb->status) { 326 if (urb->status) {
308 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status); 327 if (urb->status == -ENOENT) {
328 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
329 urb->stream_id);
330 } else {
331 dev_err(&urb->dev->dev, "stat urb: status %d\n",
332 urb->status);
333 }
309 usb_free_urb(urb); 334 usb_free_urb(urb);
310 return; 335 return;
311 } 336 }
@@ -324,6 +349,9 @@ static void uas_stat_cmplt(struct urb *urb)
324 349
325 if (!cmnd) { 350 if (!cmnd) {
326 if (iu->iu_id == IU_ID_RESPONSE) { 351 if (iu->iu_id == IU_ID_RESPONSE) {
352 if (!devinfo->running_task)
353 dev_warn(&urb->dev->dev,
354 "stat urb: recv unexpected response iu\n");
327 /* store results for uas_eh_task_mgmt() */ 355 /* store results for uas_eh_task_mgmt() */
328 memcpy(&devinfo->response, iu, sizeof(devinfo->response)); 356 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
329 } 357 }
@@ -346,17 +374,25 @@ static void uas_stat_cmplt(struct urb *urb)
346 uas_sense(urb, cmnd); 374 uas_sense(urb, cmnd);
347 if (cmnd->result != 0) { 375 if (cmnd->result != 0) {
348 /* cancel data transfers on error */ 376 /* cancel data transfers on error */
349 spin_unlock_irqrestore(&devinfo->lock, flags); 377 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
350 uas_unlink_data_urbs(devinfo, cmdinfo);
351 spin_lock_irqsave(&devinfo->lock, flags);
352 } 378 }
353 cmdinfo->state &= ~COMMAND_INFLIGHT; 379 cmdinfo->state &= ~COMMAND_INFLIGHT;
354 uas_try_complete(cmnd, __func__); 380 uas_try_complete(cmnd, __func__);
355 break; 381 break;
356 case IU_ID_READ_READY: 382 case IU_ID_READ_READY:
383 if (!cmdinfo->data_in_urb ||
384 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
385 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
386 break;
387 }
357 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); 388 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
358 break; 389 break;
359 case IU_ID_WRITE_READY: 390 case IU_ID_WRITE_READY:
391 if (!cmdinfo->data_out_urb ||
392 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
393 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
394 break;
395 }
360 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); 396 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
361 break; 397 break;
362 default: 398 default:
@@ -383,8 +419,15 @@ static void uas_data_cmplt(struct urb *urb)
383 sdb = scsi_out(cmnd); 419 sdb = scsi_out(cmnd);
384 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT; 420 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
385 } 421 }
386 BUG_ON(sdb == NULL); 422 if (sdb == NULL) {
387 if (urb->status) { 423 WARN_ON_ONCE(1);
424 } else if (urb->status) {
425 if (urb->status != -ECONNRESET) {
426 uas_log_cmd_state(cmnd, __func__);
427 scmd_printk(KERN_ERR, cmnd,
428 "data cmplt err %d stream %d\n",
429 urb->status, urb->stream_id);
430 }
388 /* error: no data transfered */ 431 /* error: no data transfered */
389 sdb->resid = sdb->length; 432 sdb->resid = sdb->length;
390 } else { 433 } else {
@@ -394,6 +437,17 @@ static void uas_data_cmplt(struct urb *urb)
394 spin_unlock_irqrestore(&devinfo->lock, flags); 437 spin_unlock_irqrestore(&devinfo->lock, flags);
395} 438}
396 439
440static void uas_cmd_cmplt(struct urb *urb)
441{
442 struct scsi_cmnd *cmnd = urb->context;
443
444 if (urb->status) {
445 uas_log_cmd_state(cmnd, __func__);
446 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
447 }
448 usb_free_urb(urb);
449}
450
397static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, 451static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
398 unsigned int pipe, u16 stream_id, 452 unsigned int pipe, u16 stream_id,
399 struct scsi_cmnd *cmnd, 453 struct scsi_cmnd *cmnd,
@@ -408,8 +462,7 @@ static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
408 goto out; 462 goto out;
409 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, 463 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
410 uas_data_cmplt, cmnd); 464 uas_data_cmplt, cmnd);
411 if (devinfo->use_streams) 465 urb->stream_id = stream_id;
412 urb->stream_id = stream_id;
413 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; 466 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
414 urb->sg = sdb->table.sgl; 467 urb->sg = sdb->table.sgl;
415 out: 468 out:
@@ -442,7 +495,7 @@ static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
442} 495}
443 496
444static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, 497static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
445 struct scsi_cmnd *cmnd, u16 stream_id) 498 struct scsi_cmnd *cmnd)
446{ 499{
447 struct usb_device *udev = devinfo->udev; 500 struct usb_device *udev = devinfo->udev;
448 struct scsi_device *sdev = cmnd->device; 501 struct scsi_device *sdev = cmnd->device;
@@ -472,7 +525,7 @@ static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
472 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); 525 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
473 526
474 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, 527 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
475 usb_free_urb, NULL); 528 uas_cmd_cmplt, cmnd);
476 urb->transfer_flags |= URB_FREE_BUFFER; 529 urb->transfer_flags |= URB_FREE_BUFFER;
477 out: 530 out:
478 return urb; 531 return urb;
@@ -512,13 +565,17 @@ static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
512 } 565 }
513 566
514 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu), 567 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
515 usb_free_urb, NULL); 568 uas_cmd_cmplt, cmnd);
516 urb->transfer_flags |= URB_FREE_BUFFER; 569 urb->transfer_flags |= URB_FREE_BUFFER;
517 570
571 usb_anchor_urb(urb, &devinfo->cmd_urbs);
518 err = usb_submit_urb(urb, gfp); 572 err = usb_submit_urb(urb, gfp);
519 if (err) 573 if (err) {
574 usb_unanchor_urb(urb);
575 uas_log_cmd_state(cmnd, __func__);
576 scmd_printk(KERN_ERR, cmnd, "task submission err %d\n", err);
520 goto err; 577 goto err;
521 usb_anchor_urb(urb, &devinfo->cmd_urbs); 578 }
522 579
523 return 0; 580 return 0;
524 581
@@ -533,38 +590,43 @@ err:
533 * daft to me. 590 * daft to me.
534 */ 591 */
535 592
536static int uas_submit_sense_urb(struct Scsi_Host *shost, 593static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
537 gfp_t gfp, unsigned int stream) 594 gfp_t gfp, unsigned int stream)
538{ 595{
539 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 596 struct Scsi_Host *shost = cmnd->device->host;
597 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
540 struct urb *urb; 598 struct urb *urb;
599 int err;
541 600
542 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream); 601 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
543 if (!urb) 602 if (!urb)
544 return SCSI_MLQUEUE_DEVICE_BUSY; 603 return NULL;
545 if (usb_submit_urb(urb, gfp)) { 604 usb_anchor_urb(urb, &devinfo->sense_urbs);
605 err = usb_submit_urb(urb, gfp);
606 if (err) {
607 usb_unanchor_urb(urb);
608 uas_log_cmd_state(cmnd, __func__);
546 shost_printk(KERN_INFO, shost, 609 shost_printk(KERN_INFO, shost,
547 "sense urb submission failure\n"); 610 "sense urb submission error %d stream %d\n",
611 err, stream);
548 usb_free_urb(urb); 612 usb_free_urb(urb);
549 return SCSI_MLQUEUE_DEVICE_BUSY; 613 return NULL;
550 } 614 }
551 usb_anchor_urb(urb, &devinfo->sense_urbs); 615 return urb;
552 return 0;
553} 616}
554 617
555static int uas_submit_urbs(struct scsi_cmnd *cmnd, 618static int uas_submit_urbs(struct scsi_cmnd *cmnd,
556 struct uas_dev_info *devinfo, gfp_t gfp) 619 struct uas_dev_info *devinfo, gfp_t gfp)
557{ 620{
558 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 621 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
622 struct urb *urb;
559 int err; 623 int err;
560 624
561 WARN_ON(!spin_is_locked(&devinfo->lock)); 625 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
562 if (cmdinfo->state & SUBMIT_STATUS_URB) { 626 if (cmdinfo->state & SUBMIT_STATUS_URB) {
563 err = uas_submit_sense_urb(cmnd->device->host, gfp, 627 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
564 cmdinfo->stream); 628 if (!urb)
565 if (err) { 629 return SCSI_MLQUEUE_DEVICE_BUSY;
566 return err;
567 }
568 cmdinfo->state &= ~SUBMIT_STATUS_URB; 630 cmdinfo->state &= ~SUBMIT_STATUS_URB;
569 } 631 }
570 632
@@ -578,14 +640,18 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
578 } 640 }
579 641
580 if (cmdinfo->state & SUBMIT_DATA_IN_URB) { 642 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
581 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) { 643 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
644 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
645 if (err) {
646 usb_unanchor_urb(cmdinfo->data_in_urb);
647 uas_log_cmd_state(cmnd, __func__);
582 scmd_printk(KERN_INFO, cmnd, 648 scmd_printk(KERN_INFO, cmnd,
583 "data in urb submission failure\n"); 649 "data in urb submission error %d stream %d\n",
650 err, cmdinfo->data_in_urb->stream_id);
584 return SCSI_MLQUEUE_DEVICE_BUSY; 651 return SCSI_MLQUEUE_DEVICE_BUSY;
585 } 652 }
586 cmdinfo->state &= ~SUBMIT_DATA_IN_URB; 653 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
587 cmdinfo->state |= DATA_IN_URB_INFLIGHT; 654 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
588 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
589 } 655 }
590 656
591 if (cmdinfo->state & ALLOC_DATA_OUT_URB) { 657 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
@@ -598,33 +664,37 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
598 } 664 }
599 665
600 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { 666 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
601 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) { 667 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
668 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
669 if (err) {
670 usb_unanchor_urb(cmdinfo->data_out_urb);
671 uas_log_cmd_state(cmnd, __func__);
602 scmd_printk(KERN_INFO, cmnd, 672 scmd_printk(KERN_INFO, cmnd,
603 "data out urb submission failure\n"); 673 "data out urb submission error %d stream %d\n",
674 err, cmdinfo->data_out_urb->stream_id);
604 return SCSI_MLQUEUE_DEVICE_BUSY; 675 return SCSI_MLQUEUE_DEVICE_BUSY;
605 } 676 }
606 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; 677 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
607 cmdinfo->state |= DATA_OUT_URB_INFLIGHT; 678 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
608 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
609 } 679 }
610 680
611 if (cmdinfo->state & ALLOC_CMD_URB) { 681 if (cmdinfo->state & ALLOC_CMD_URB) {
612 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd, 682 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
613 cmdinfo->stream);
614 if (!cmdinfo->cmd_urb) 683 if (!cmdinfo->cmd_urb)
615 return SCSI_MLQUEUE_DEVICE_BUSY; 684 return SCSI_MLQUEUE_DEVICE_BUSY;
616 cmdinfo->state &= ~ALLOC_CMD_URB; 685 cmdinfo->state &= ~ALLOC_CMD_URB;
617 } 686 }
618 687
619 if (cmdinfo->state & SUBMIT_CMD_URB) { 688 if (cmdinfo->state & SUBMIT_CMD_URB) {
620 usb_get_urb(cmdinfo->cmd_urb); 689 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
621 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) { 690 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
691 if (err) {
692 usb_unanchor_urb(cmdinfo->cmd_urb);
693 uas_log_cmd_state(cmnd, __func__);
622 scmd_printk(KERN_INFO, cmnd, 694 scmd_printk(KERN_INFO, cmnd,
623 "cmd urb submission failure\n"); 695 "cmd urb submission error %d\n", err);
624 return SCSI_MLQUEUE_DEVICE_BUSY; 696 return SCSI_MLQUEUE_DEVICE_BUSY;
625 } 697 }
626 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
627 usb_put_urb(cmdinfo->cmd_urb);
628 cmdinfo->cmd_urb = NULL; 698 cmdinfo->cmd_urb = NULL;
629 cmdinfo->state &= ~SUBMIT_CMD_URB; 699 cmdinfo->state &= ~SUBMIT_CMD_URB;
630 cmdinfo->state |= COMMAND_INFLIGHT; 700 cmdinfo->state |= COMMAND_INFLIGHT;
@@ -644,18 +714,22 @@ static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
644 714
645 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); 715 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
646 716
717 spin_lock_irqsave(&devinfo->lock, flags);
718
647 if (devinfo->resetting) { 719 if (devinfo->resetting) {
648 cmnd->result = DID_ERROR << 16; 720 cmnd->result = DID_ERROR << 16;
649 cmnd->scsi_done(cmnd); 721 cmnd->scsi_done(cmnd);
722 spin_unlock_irqrestore(&devinfo->lock, flags);
650 return 0; 723 return 0;
651 } 724 }
652 725
653 spin_lock_irqsave(&devinfo->lock, flags);
654 if (devinfo->cmnd) { 726 if (devinfo->cmnd) {
655 spin_unlock_irqrestore(&devinfo->lock, flags); 727 spin_unlock_irqrestore(&devinfo->lock, flags);
656 return SCSI_MLQUEUE_DEVICE_BUSY; 728 return SCSI_MLQUEUE_DEVICE_BUSY;
657 } 729 }
658 730
731 memset(cmdinfo, 0, sizeof(*cmdinfo));
732
659 if (blk_rq_tagged(cmnd->request)) { 733 if (blk_rq_tagged(cmnd->request)) {
660 cmdinfo->stream = cmnd->request->tag + 2; 734 cmdinfo->stream = cmnd->request->tag + 2;
661 } else { 735 } else {
@@ -692,13 +766,10 @@ static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
692 spin_unlock_irqrestore(&devinfo->lock, flags); 766 spin_unlock_irqrestore(&devinfo->lock, flags);
693 return SCSI_MLQUEUE_DEVICE_BUSY; 767 return SCSI_MLQUEUE_DEVICE_BUSY;
694 } 768 }
695 spin_lock(&uas_work_lock); 769 uas_add_work(cmdinfo);
696 list_add_tail(&cmdinfo->list, &uas_work_list);
697 cmdinfo->state |= IS_IN_WORK_LIST;
698 spin_unlock(&uas_work_lock);
699 schedule_work(&uas_work);
700 } 770 }
701 771
772 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
702 spin_unlock_irqrestore(&devinfo->lock, flags); 773 spin_unlock_irqrestore(&devinfo->lock, flags);
703 return 0; 774 return 0;
704} 775}
@@ -709,46 +780,78 @@ static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
709 const char *fname, u8 function) 780 const char *fname, u8 function)
710{ 781{
711 struct Scsi_Host *shost = cmnd->device->host; 782 struct Scsi_Host *shost = cmnd->device->host;
712 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 783 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
713 u16 tag = devinfo->qdepth - 1; 784 u16 tag = devinfo->qdepth;
714 unsigned long flags; 785 unsigned long flags;
786 struct urb *sense_urb;
787 int result = SUCCESS;
715 788
716 spin_lock_irqsave(&devinfo->lock, flags); 789 spin_lock_irqsave(&devinfo->lock, flags);
790
791 if (devinfo->resetting) {
792 spin_unlock_irqrestore(&devinfo->lock, flags);
793 return FAILED;
794 }
795
796 if (devinfo->running_task) {
797 shost_printk(KERN_INFO, shost,
798 "%s: %s: error already running a task\n",
799 __func__, fname);
800 spin_unlock_irqrestore(&devinfo->lock, flags);
801 return FAILED;
802 }
803
804 devinfo->running_task = 1;
717 memset(&devinfo->response, 0, sizeof(devinfo->response)); 805 memset(&devinfo->response, 0, sizeof(devinfo->response));
718 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) { 806 sense_urb = uas_submit_sense_urb(cmnd, GFP_NOIO,
807 devinfo->use_streams ? tag : 0);
808 if (!sense_urb) {
719 shost_printk(KERN_INFO, shost, 809 shost_printk(KERN_INFO, shost,
720 "%s: %s: submit sense urb failed\n", 810 "%s: %s: submit sense urb failed\n",
721 __func__, fname); 811 __func__, fname);
812 devinfo->running_task = 0;
722 spin_unlock_irqrestore(&devinfo->lock, flags); 813 spin_unlock_irqrestore(&devinfo->lock, flags);
723 return FAILED; 814 return FAILED;
724 } 815 }
725 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) { 816 if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
726 shost_printk(KERN_INFO, shost, 817 shost_printk(KERN_INFO, shost,
727 "%s: %s: submit task mgmt urb failed\n", 818 "%s: %s: submit task mgmt urb failed\n",
728 __func__, fname); 819 __func__, fname);
820 devinfo->running_task = 0;
729 spin_unlock_irqrestore(&devinfo->lock, flags); 821 spin_unlock_irqrestore(&devinfo->lock, flags);
822 usb_kill_urb(sense_urb);
730 return FAILED; 823 return FAILED;
731 } 824 }
732 spin_unlock_irqrestore(&devinfo->lock, flags); 825 spin_unlock_irqrestore(&devinfo->lock, flags);
733 826
734 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) { 827 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
828 /*
829 * Note we deliberately do not clear running_task here. If we
830 * allow new tasks to be submitted, there is no way to figure
831 * out if a received response_iu is for the failed task or for
832 * the new one. A bus-reset will eventually clear running_task.
833 */
735 shost_printk(KERN_INFO, shost, 834 shost_printk(KERN_INFO, shost,
736 "%s: %s timed out\n", __func__, fname); 835 "%s: %s timed out\n", __func__, fname);
737 return FAILED; 836 return FAILED;
738 } 837 }
838
839 spin_lock_irqsave(&devinfo->lock, flags);
840 devinfo->running_task = 0;
739 if (be16_to_cpu(devinfo->response.tag) != tag) { 841 if (be16_to_cpu(devinfo->response.tag) != tag) {
740 shost_printk(KERN_INFO, shost, 842 shost_printk(KERN_INFO, shost,
741 "%s: %s failed (wrong tag %d/%d)\n", __func__, 843 "%s: %s failed (wrong tag %d/%d)\n", __func__,
742 fname, be16_to_cpu(devinfo->response.tag), tag); 844 fname, be16_to_cpu(devinfo->response.tag), tag);
743 return FAILED; 845 result = FAILED;
744 } 846 } else if (devinfo->response.response_code != RC_TMF_COMPLETE) {
745 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
746 shost_printk(KERN_INFO, shost, 847 shost_printk(KERN_INFO, shost,
747 "%s: %s failed (rc 0x%x)\n", __func__, 848 "%s: %s failed (rc 0x%x)\n", __func__,
748 fname, devinfo->response.response_code); 849 fname, devinfo->response.response_code);
749 return FAILED; 850 result = FAILED;
750 } 851 }
751 return SUCCESS; 852 spin_unlock_irqrestore(&devinfo->lock, flags);
853
854 return result;
752} 855}
753 856
754static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) 857static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
@@ -758,22 +861,19 @@ static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
758 unsigned long flags; 861 unsigned long flags;
759 int ret; 862 int ret;
760 863
761 uas_log_cmd_state(cmnd, __func__);
762 spin_lock_irqsave(&devinfo->lock, flags); 864 spin_lock_irqsave(&devinfo->lock, flags);
763 cmdinfo->state |= COMMAND_ABORTED; 865
764 if (cmdinfo->state & IS_IN_WORK_LIST) { 866 if (devinfo->resetting) {
765 spin_lock(&uas_work_lock); 867 spin_unlock_irqrestore(&devinfo->lock, flags);
766 list_del(&cmdinfo->list); 868 return FAILED;
767 cmdinfo->state &= ~IS_IN_WORK_LIST;
768 spin_unlock(&uas_work_lock);
769 } 869 }
870
871 uas_mark_cmd_dead(devinfo, cmdinfo, DID_ABORT, __func__);
770 if (cmdinfo->state & COMMAND_INFLIGHT) { 872 if (cmdinfo->state & COMMAND_INFLIGHT) {
771 spin_unlock_irqrestore(&devinfo->lock, flags); 873 spin_unlock_irqrestore(&devinfo->lock, flags);
772 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK); 874 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
773 } else { 875 } else {
774 spin_unlock_irqrestore(&devinfo->lock, flags); 876 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
775 uas_unlink_data_urbs(devinfo, cmdinfo);
776 spin_lock_irqsave(&devinfo->lock, flags);
777 uas_try_complete(cmnd, __func__); 877 uas_try_complete(cmnd, __func__);
778 spin_unlock_irqrestore(&devinfo->lock, flags); 878 spin_unlock_irqrestore(&devinfo->lock, flags);
779 ret = SUCCESS; 879 ret = SUCCESS;
@@ -795,14 +895,25 @@ static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
795 struct usb_device *udev = devinfo->udev; 895 struct usb_device *udev = devinfo->udev;
796 int err; 896 int err;
797 897
898 err = usb_lock_device_for_reset(udev, devinfo->intf);
899 if (err) {
900 shost_printk(KERN_ERR, sdev->host,
901 "%s FAILED to get lock err %d\n", __func__, err);
902 return FAILED;
903 }
904
905 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
798 devinfo->resetting = 1; 906 devinfo->resetting = 1;
799 uas_abort_work(devinfo); 907 uas_abort_inflight(devinfo, DID_RESET, __func__);
800 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 908 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
801 usb_kill_anchored_urbs(&devinfo->sense_urbs); 909 usb_kill_anchored_urbs(&devinfo->sense_urbs);
802 usb_kill_anchored_urbs(&devinfo->data_urbs); 910 usb_kill_anchored_urbs(&devinfo->data_urbs);
911 uas_zap_dead(devinfo);
803 err = usb_reset_device(udev); 912 err = usb_reset_device(udev);
804 devinfo->resetting = 0; 913 devinfo->resetting = 0;
805 914
915 usb_unlock_device(udev);
916
806 if (err) { 917 if (err) {
807 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__); 918 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
808 return FAILED; 919 return FAILED;
@@ -814,7 +925,25 @@ static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
814 925
815static int uas_slave_alloc(struct scsi_device *sdev) 926static int uas_slave_alloc(struct scsi_device *sdev)
816{ 927{
817 sdev->hostdata = (void *)sdev->host->hostdata[0]; 928 sdev->hostdata = (void *)sdev->host->hostdata;
929
930 /* USB has unusual DMA-alignment requirements: Although the
931 * starting address of each scatter-gather element doesn't matter,
932 * the length of each element except the last must be divisible
933 * by the Bulk maxpacket value. There's currently no way to
934 * express this by block-layer constraints, so we'll cop out
935 * and simply require addresses to be aligned at 512-byte
936 * boundaries. This is okay since most block I/O involves
937 * hardware sectors that are multiples of 512 bytes in length,
938 * and since host controllers up through USB 2.0 have maxpacket
939 * values no larger than 512.
940 *
941 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
942 * values can be as large as 2048. To make that work properly
943 * will require changes to the block layer.
944 */
945 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
946
818 return 0; 947 return 0;
819} 948}
820 949
@@ -822,7 +951,7 @@ static int uas_slave_configure(struct scsi_device *sdev)
822{ 951{
823 struct uas_dev_info *devinfo = sdev->hostdata; 952 struct uas_dev_info *devinfo = sdev->hostdata;
824 scsi_set_tag_type(sdev, MSG_ORDERED_TAG); 953 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
825 scsi_activate_tcq(sdev, devinfo->qdepth - 3); 954 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
826 return 0; 955 return 0;
827} 956}
828 957
@@ -843,7 +972,14 @@ static struct scsi_host_template uas_host_template = {
843 .ordered_tag = 1, 972 .ordered_tag = 1,
844}; 973};
845 974
975#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
976 vendorName, productName, useProtocol, useTransport, \
977 initFunction, flags) \
978{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
979 .driver_info = (flags) }
980
846static struct usb_device_id uas_usb_ids[] = { 981static struct usb_device_id uas_usb_ids[] = {
982# include "unusual_uas.h"
847 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, 983 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
848 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, 984 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
849 /* 0xaa is a prototype device I happen to have access to */ 985 /* 0xaa is a prototype device I happen to have access to */
@@ -852,105 +988,55 @@ static struct usb_device_id uas_usb_ids[] = {
852}; 988};
853MODULE_DEVICE_TABLE(usb, uas_usb_ids); 989MODULE_DEVICE_TABLE(usb, uas_usb_ids);
854 990
855static int uas_is_interface(struct usb_host_interface *intf) 991#undef UNUSUAL_DEV
856{
857 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
858 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
859 intf->desc.bInterfaceProtocol == USB_PR_UAS);
860}
861
862static int uas_isnt_supported(struct usb_device *udev)
863{
864 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
865
866 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
867 "support scatter-gather which is\n",
868 hcd->driver->description);
869 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
870 "alternative USB controller if you wish to use UAS.\n");
871 return -ENODEV;
872}
873 992
874static int uas_switch_interface(struct usb_device *udev, 993static int uas_switch_interface(struct usb_device *udev,
875 struct usb_interface *intf) 994 struct usb_interface *intf)
876{ 995{
877 int i; 996 int alt;
878 int sg_supported = udev->bus->sg_tablesize != 0; 997
879 998 alt = uas_find_uas_alt_setting(intf);
880 for (i = 0; i < intf->num_altsetting; i++) { 999 if (alt < 0)
881 struct usb_host_interface *alt = &intf->altsetting[i]; 1000 return alt;
882
883 if (uas_is_interface(alt)) {
884 if (!sg_supported)
885 return uas_isnt_supported(udev);
886 return usb_set_interface(udev,
887 alt->desc.bInterfaceNumber,
888 alt->desc.bAlternateSetting);
889 }
890 }
891 1001
892 return -ENODEV; 1002 return usb_set_interface(udev,
1003 intf->altsetting[0].desc.bInterfaceNumber, alt);
893} 1004}
894 1005
895static void uas_configure_endpoints(struct uas_dev_info *devinfo) 1006static int uas_configure_endpoints(struct uas_dev_info *devinfo)
896{ 1007{
897 struct usb_host_endpoint *eps[4] = { }; 1008 struct usb_host_endpoint *eps[4] = { };
898 struct usb_interface *intf = devinfo->intf;
899 struct usb_device *udev = devinfo->udev; 1009 struct usb_device *udev = devinfo->udev;
900 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint; 1010 int r;
901 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
902 1011
903 devinfo->uas_sense_old = 0; 1012 devinfo->uas_sense_old = 0;
904 devinfo->cmnd = NULL; 1013 devinfo->cmnd = NULL;
905 1014
906 for (i = 0; i < n_endpoints; i++) { 1015 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
907 unsigned char *extra = endpoint[i].extra; 1016 if (r)
908 int len = endpoint[i].extralen; 1017 return r;
909 while (len > 1) {
910 if (extra[1] == USB_DT_PIPE_USAGE) {
911 unsigned pipe_id = extra[2];
912 if (pipe_id > 0 && pipe_id < 5)
913 eps[pipe_id - 1] = &endpoint[i];
914 break;
915 }
916 len -= extra[0];
917 extra += extra[0];
918 }
919 }
920 1018
921 /* 1019 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
922 * Assume that if we didn't find a control pipe descriptor, we're 1020 usb_endpoint_num(&eps[0]->desc));
923 * using a device with old firmware that happens to be set up like 1021 devinfo->status_pipe = usb_rcvbulkpipe(udev,
924 * this. 1022 usb_endpoint_num(&eps[1]->desc));
925 */ 1023 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
926 if (!eps[0]) { 1024 usb_endpoint_num(&eps[2]->desc));
927 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1); 1025 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
928 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1); 1026 usb_endpoint_num(&eps[3]->desc));
929 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
930 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
931
932 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
933 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
934 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
935 } else {
936 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
937 eps[0]->desc.bEndpointAddress);
938 devinfo->status_pipe = usb_rcvbulkpipe(udev,
939 eps[1]->desc.bEndpointAddress);
940 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
941 eps[2]->desc.bEndpointAddress);
942 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
943 eps[3]->desc.bEndpointAddress);
944 }
945 1027
946 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256, 1028 if (udev->speed != USB_SPEED_SUPER) {
947 GFP_KERNEL);
948 if (devinfo->qdepth < 0) {
949 devinfo->qdepth = 256; 1029 devinfo->qdepth = 256;
950 devinfo->use_streams = 0; 1030 devinfo->use_streams = 0;
951 } else { 1031 } else {
1032 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
1033 3, 256, GFP_KERNEL);
1034 if (devinfo->qdepth < 0)
1035 return devinfo->qdepth;
952 devinfo->use_streams = 1; 1036 devinfo->use_streams = 1;
953 } 1037 }
1038
1039 return 0;
954} 1040}
955 1041
956static void uas_free_streams(struct uas_dev_info *devinfo) 1042static void uas_free_streams(struct uas_dev_info *devinfo)
@@ -964,30 +1050,23 @@ static void uas_free_streams(struct uas_dev_info *devinfo)
964 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL); 1050 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
965} 1051}
966 1052
967/*
968 * XXX: What I'd like to do here is register a SCSI host for each USB host in
969 * the system. Follow usb-storage's design of registering a SCSI host for
970 * each USB device for the moment. Can implement this by walking up the
971 * USB hierarchy until we find a USB host.
972 */
973static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) 1053static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
974{ 1054{
975 int result; 1055 int result = -ENOMEM;
976 struct Scsi_Host *shost; 1056 struct Scsi_Host *shost = NULL;
977 struct uas_dev_info *devinfo; 1057 struct uas_dev_info *devinfo;
978 struct usb_device *udev = interface_to_usbdev(intf); 1058 struct usb_device *udev = interface_to_usbdev(intf);
979 1059
980 if (uas_switch_interface(udev, intf)) 1060 if (!uas_use_uas_driver(intf, id))
981 return -ENODEV; 1061 return -ENODEV;
982 1062
983 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL); 1063 if (uas_switch_interface(udev, intf))
984 if (!devinfo) 1064 return -ENODEV;
985 return -ENOMEM;
986 1065
987 result = -ENOMEM; 1066 shost = scsi_host_alloc(&uas_host_template,
988 shost = scsi_host_alloc(&uas_host_template, sizeof(void *)); 1067 sizeof(struct uas_dev_info));
989 if (!shost) 1068 if (!shost)
990 goto free; 1069 goto set_alt0;
991 1070
992 shost->max_cmd_len = 16 + 252; 1071 shost->max_cmd_len = 16 + 252;
993 shost->max_id = 1; 1072 shost->max_id = 1;
@@ -995,33 +1074,40 @@ static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
995 shost->max_channel = 0; 1074 shost->max_channel = 0;
996 shost->sg_tablesize = udev->bus->sg_tablesize; 1075 shost->sg_tablesize = udev->bus->sg_tablesize;
997 1076
1077 devinfo = (struct uas_dev_info *)shost->hostdata;
998 devinfo->intf = intf; 1078 devinfo->intf = intf;
999 devinfo->udev = udev; 1079 devinfo->udev = udev;
1000 devinfo->resetting = 0; 1080 devinfo->resetting = 0;
1081 devinfo->running_task = 0;
1082 devinfo->shutdown = 0;
1001 init_usb_anchor(&devinfo->cmd_urbs); 1083 init_usb_anchor(&devinfo->cmd_urbs);
1002 init_usb_anchor(&devinfo->sense_urbs); 1084 init_usb_anchor(&devinfo->sense_urbs);
1003 init_usb_anchor(&devinfo->data_urbs); 1085 init_usb_anchor(&devinfo->data_urbs);
1004 spin_lock_init(&devinfo->lock); 1086 spin_lock_init(&devinfo->lock);
1005 uas_configure_endpoints(devinfo); 1087 INIT_WORK(&devinfo->work, uas_do_work);
1088 INIT_LIST_HEAD(&devinfo->inflight_list);
1089 INIT_LIST_HEAD(&devinfo->dead_list);
1006 1090
1007 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3); 1091 result = uas_configure_endpoints(devinfo);
1008 if (result) 1092 if (result)
1009 goto free; 1093 goto set_alt0;
1010 1094
1011 result = scsi_add_host(shost, &intf->dev); 1095 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
1012 if (result) 1096 if (result)
1013 goto deconfig_eps; 1097 goto free_streams;
1014 1098
1015 shost->hostdata[0] = (unsigned long)devinfo; 1099 result = scsi_add_host(shost, &intf->dev);
1100 if (result)
1101 goto free_streams;
1016 1102
1017 scsi_scan_host(shost); 1103 scsi_scan_host(shost);
1018 usb_set_intfdata(intf, shost); 1104 usb_set_intfdata(intf, shost);
1019 return result; 1105 return result;
1020 1106
1021deconfig_eps: 1107free_streams:
1022 uas_free_streams(devinfo); 1108 uas_free_streams(devinfo);
1023 free: 1109set_alt0:
1024 kfree(devinfo); 1110 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1025 if (shost) 1111 if (shost)
1026 scsi_host_put(shost); 1112 scsi_host_put(shost);
1027 return result; 1113 return result;
@@ -1029,45 +1115,146 @@ deconfig_eps:
1029 1115
1030static int uas_pre_reset(struct usb_interface *intf) 1116static int uas_pre_reset(struct usb_interface *intf)
1031{ 1117{
1032/* XXX: Need to return 1 if it's not our device in error handling */ 1118 struct Scsi_Host *shost = usb_get_intfdata(intf);
1119 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1120 unsigned long flags;
1121
1122 if (devinfo->shutdown)
1123 return 0;
1124
1125 /* Block new requests */
1126 spin_lock_irqsave(shost->host_lock, flags);
1127 scsi_block_requests(shost);
1128 spin_unlock_irqrestore(shost->host_lock, flags);
1129
1130 /* Wait for any pending requests to complete */
1131 flush_work(&devinfo->work);
1132 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1133 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1134 return 1;
1135 }
1136
1137 uas_free_streams(devinfo);
1138
1033 return 0; 1139 return 0;
1034} 1140}
1035 1141
1036static int uas_post_reset(struct usb_interface *intf) 1142static int uas_post_reset(struct usb_interface *intf)
1037{ 1143{
1038/* XXX: Need to return 1 if it's not our device in error handling */ 1144 struct Scsi_Host *shost = usb_get_intfdata(intf);
1145 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1146 unsigned long flags;
1147
1148 if (devinfo->shutdown)
1149 return 0;
1150
1151 if (uas_configure_endpoints(devinfo) != 0) {
1152 shost_printk(KERN_ERR, shost,
1153 "%s: alloc streams error after reset", __func__);
1154 return 1;
1155 }
1156
1157 spin_lock_irqsave(shost->host_lock, flags);
1158 scsi_report_bus_reset(shost, 0);
1159 spin_unlock_irqrestore(shost->host_lock, flags);
1160
1161 scsi_unblock_requests(shost);
1162
1163 return 0;
1164}
1165
1166static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1167{
1168 struct Scsi_Host *shost = usb_get_intfdata(intf);
1169 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1170
1171 /* Wait for any pending requests to complete */
1172 flush_work(&devinfo->work);
1173 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1174 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1175 return -ETIME;
1176 }
1177
1178 return 0;
1179}
1180
1181static int uas_resume(struct usb_interface *intf)
1182{
1183 return 0;
1184}
1185
1186static int uas_reset_resume(struct usb_interface *intf)
1187{
1188 struct Scsi_Host *shost = usb_get_intfdata(intf);
1189 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1190 unsigned long flags;
1191
1192 if (uas_configure_endpoints(devinfo) != 0) {
1193 shost_printk(KERN_ERR, shost,
1194 "%s: alloc streams error after reset", __func__);
1195 return -EIO;
1196 }
1197
1198 spin_lock_irqsave(shost->host_lock, flags);
1199 scsi_report_bus_reset(shost, 0);
1200 spin_unlock_irqrestore(shost->host_lock, flags);
1201
1039 return 0; 1202 return 0;
1040} 1203}
1041 1204
1042static void uas_disconnect(struct usb_interface *intf) 1205static void uas_disconnect(struct usb_interface *intf)
1043{ 1206{
1044 struct Scsi_Host *shost = usb_get_intfdata(intf); 1207 struct Scsi_Host *shost = usb_get_intfdata(intf);
1045 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 1208 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1046 1209
1047 devinfo->resetting = 1; 1210 devinfo->resetting = 1;
1048 uas_abort_work(devinfo); 1211 cancel_work_sync(&devinfo->work);
1212 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
1049 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 1213 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1050 usb_kill_anchored_urbs(&devinfo->sense_urbs); 1214 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1051 usb_kill_anchored_urbs(&devinfo->data_urbs); 1215 usb_kill_anchored_urbs(&devinfo->data_urbs);
1216 uas_zap_dead(devinfo);
1052 scsi_remove_host(shost); 1217 scsi_remove_host(shost);
1053 uas_free_streams(devinfo); 1218 uas_free_streams(devinfo);
1054 kfree(devinfo); 1219 scsi_host_put(shost);
1055} 1220}
1056 1221
1057/* 1222/*
1058 * XXX: Should this plug into libusual so we can auto-upgrade devices from 1223 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1059 * Bulk-Only to UAS? 1224 * hang on reboot when the device is still in uas mode. Note the reset is
1225 * necessary as some devices won't revert to usb-storage mode without it.
1060 */ 1226 */
1227static void uas_shutdown(struct device *dev)
1228{
1229 struct usb_interface *intf = to_usb_interface(dev);
1230 struct usb_device *udev = interface_to_usbdev(intf);
1231 struct Scsi_Host *shost = usb_get_intfdata(intf);
1232 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1233
1234 if (system_state != SYSTEM_RESTART)
1235 return;
1236
1237 devinfo->shutdown = 1;
1238 uas_free_streams(devinfo);
1239 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1240 usb_reset_device(udev);
1241}
1242
1061static struct usb_driver uas_driver = { 1243static struct usb_driver uas_driver = {
1062 .name = "uas", 1244 .name = "uas",
1063 .probe = uas_probe, 1245 .probe = uas_probe,
1064 .disconnect = uas_disconnect, 1246 .disconnect = uas_disconnect,
1065 .pre_reset = uas_pre_reset, 1247 .pre_reset = uas_pre_reset,
1066 .post_reset = uas_post_reset, 1248 .post_reset = uas_post_reset,
1249 .suspend = uas_suspend,
1250 .resume = uas_resume,
1251 .reset_resume = uas_reset_resume,
1252 .drvwrap.driver.shutdown = uas_shutdown,
1067 .id_table = uas_usb_ids, 1253 .id_table = uas_usb_ids,
1068}; 1254};
1069 1255
1070module_usb_driver(uas_driver); 1256module_usb_driver(uas_driver);
1071 1257
1072MODULE_LICENSE("GPL"); 1258MODULE_LICENSE("GPL");
1073MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp"); 1259MODULE_AUTHOR(
1260 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index adbeb255616a..f4a82291894a 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -2086,6 +2086,11 @@ UNUSUAL_DEV( 0xed10, 0x7636, 0x0001, 0x0001,
2086 "Digital MP3 Audio Player", 2086 "Digital MP3 Audio Player",
2087 USB_SC_DEVICE, USB_PR_DEVICE, NULL, US_FL_NOT_LOCKABLE ), 2087 USB_SC_DEVICE, USB_PR_DEVICE, NULL, US_FL_NOT_LOCKABLE ),
2088 2088
2089/* Unusual uas devices */
2090#if IS_ENABLED(CONFIG_USB_UAS)
2091#include "unusual_uas.h"
2092#endif
2093
2089/* Control/Bulk transport for all SubClass values */ 2094/* Control/Bulk transport for all SubClass values */
2090USUAL_DEV(USB_SC_RBC, USB_PR_CB), 2095USUAL_DEV(USB_SC_RBC, USB_PR_CB),
2091USUAL_DEV(USB_SC_8020, USB_PR_CB), 2096USUAL_DEV(USB_SC_8020, USB_PR_CB),
diff --git a/drivers/usb/storage/unusual_uas.h b/drivers/usb/storage/unusual_uas.h
new file mode 100644
index 000000000000..7244444df8ee
--- /dev/null
+++ b/drivers/usb/storage/unusual_uas.h
@@ -0,0 +1,52 @@
1/* Driver for USB Attached SCSI devices - Unusual Devices File
2 *
3 * (c) 2013 Hans de Goede <hdegoede@redhat.com>
4 *
5 * Based on the same file for the usb-storage driver, which is:
6 * (c) 2000-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 * (c) 2000 Adam J. Richter (adam@yggdrasil.com), Yggdrasil Computing, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24/*
25 * IMPORTANT NOTE: This file must be included in another file which defines
26 * a UNUSUAL_DEV macro before this file is included.
27 */
28
29/*
30 * If you edit this file, please try to keep it sorted first by VendorID,
31 * then by ProductID.
32 *
33 * If you want to add an entry for this file, be sure to include the
34 * following information:
35 * - a patch that adds the entry for your device, including your
36 * email address right above the entry (plus maybe a brief
37 * explanation of the reason for the entry),
38 * - lsusb -v output for the device
39 * Send your submission to Hans de Goede <hdegoede@redhat.com>
40 * and don't forget to CC: the USB development list <linux-usb@vger.kernel.org>
41 */
42
43/*
44 * This is an example entry for the US_FL_IGNORE_UAS flag. Once we have an
45 * actual entry using US_FL_IGNORE_UAS this entry should be removed.
46 *
47 * UNUSUAL_DEV( 0xabcd, 0x1234, 0x0100, 0x0100,
48 * "Example",
49 * "Storage with broken UAS",
50 * USB_SC_DEVICE, USB_PR_DEVICE, NULL,
51 * US_FL_IGNORE_UAS),
52 */
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index 1c0b89f2a138..f1c96261a501 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -72,6 +72,10 @@
72#include "sierra_ms.h" 72#include "sierra_ms.h"
73#include "option_ms.h" 73#include "option_ms.h"
74 74
75#if IS_ENABLED(CONFIG_USB_UAS)
76#include "uas-detect.h"
77#endif
78
75/* Some informational data */ 79/* Some informational data */
76MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>"); 80MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
77MODULE_DESCRIPTION("USB Mass Storage driver for Linux"); 81MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
@@ -459,14 +463,14 @@ static int associate_dev(struct us_data *us, struct usb_interface *intf)
459#define TOLOWER(x) ((x) | 0x20) 463#define TOLOWER(x) ((x) | 0x20)
460 464
461/* Adjust device flags based on the "quirks=" module parameter */ 465/* Adjust device flags based on the "quirks=" module parameter */
462static void adjust_quirks(struct us_data *us) 466void usb_stor_adjust_quirks(struct usb_device *udev, unsigned long *fflags)
463{ 467{
464 char *p; 468 char *p;
465 u16 vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor); 469 u16 vid = le16_to_cpu(udev->descriptor.idVendor);
466 u16 pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct); 470 u16 pid = le16_to_cpu(udev->descriptor.idProduct);
467 unsigned f = 0; 471 unsigned f = 0;
468 unsigned int mask = (US_FL_SANE_SENSE | US_FL_BAD_SENSE | 472 unsigned int mask = (US_FL_SANE_SENSE | US_FL_BAD_SENSE |
469 US_FL_FIX_CAPACITY | 473 US_FL_FIX_CAPACITY | US_FL_IGNORE_UAS |
470 US_FL_CAPACITY_HEURISTICS | US_FL_IGNORE_DEVICE | 474 US_FL_CAPACITY_HEURISTICS | US_FL_IGNORE_DEVICE |
471 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 | 475 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
472 US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE | 476 US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE |
@@ -537,14 +541,18 @@ static void adjust_quirks(struct us_data *us)
537 case 's': 541 case 's':
538 f |= US_FL_SINGLE_LUN; 542 f |= US_FL_SINGLE_LUN;
539 break; 543 break;
544 case 'u':
545 f |= US_FL_IGNORE_UAS;
546 break;
540 case 'w': 547 case 'w':
541 f |= US_FL_NO_WP_DETECT; 548 f |= US_FL_NO_WP_DETECT;
542 break; 549 break;
543 /* Ignore unrecognized flag characters */ 550 /* Ignore unrecognized flag characters */
544 } 551 }
545 } 552 }
546 us->fflags = (us->fflags & ~mask) | f; 553 *fflags = (*fflags & ~mask) | f;
547} 554}
555EXPORT_SYMBOL_GPL(usb_stor_adjust_quirks);
548 556
549/* Get the unusual_devs entries and the string descriptors */ 557/* Get the unusual_devs entries and the string descriptors */
550static int get_device_info(struct us_data *us, const struct usb_device_id *id, 558static int get_device_info(struct us_data *us, const struct usb_device_id *id,
@@ -564,7 +572,7 @@ static int get_device_info(struct us_data *us, const struct usb_device_id *id,
564 idesc->bInterfaceProtocol : 572 idesc->bInterfaceProtocol :
565 unusual_dev->useTransport; 573 unusual_dev->useTransport;
566 us->fflags = id->driver_info; 574 us->fflags = id->driver_info;
567 adjust_quirks(us); 575 usb_stor_adjust_quirks(us->pusb_dev, &us->fflags);
568 576
569 if (us->fflags & US_FL_IGNORE_DEVICE) { 577 if (us->fflags & US_FL_IGNORE_DEVICE) {
570 dev_info(pdev, "device ignored\n"); 578 dev_info(pdev, "device ignored\n");
@@ -1035,6 +1043,12 @@ static int storage_probe(struct usb_interface *intf,
1035 int result; 1043 int result;
1036 int size; 1044 int size;
1037 1045
1046 /* If uas is enabled and this device can do uas then ignore it. */
1047#if IS_ENABLED(CONFIG_USB_UAS)
1048 if (uas_use_uas_driver(intf, id))
1049 return -ENXIO;
1050#endif
1051
1038 /* 1052 /*
1039 * If the device isn't standard (is handled by a subdriver 1053 * If the device isn't standard (is handled by a subdriver
1040 * module) then don't accept it. 1054 * module) then don't accept it.
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
index 75f70f04f37b..307e339a9478 100644
--- a/drivers/usb/storage/usb.h
+++ b/drivers/usb/storage/usb.h
@@ -201,4 +201,7 @@ extern int usb_stor_probe1(struct us_data **pus,
201extern int usb_stor_probe2(struct us_data *us); 201extern int usb_stor_probe2(struct us_data *us);
202extern void usb_stor_disconnect(struct usb_interface *intf); 202extern void usb_stor_disconnect(struct usb_interface *intf);
203 203
204extern void usb_stor_adjust_quirks(struct usb_device *dev,
205 unsigned long *fflags);
206
204#endif 207#endif
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 7f6eb859873e..f1015cee5944 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -57,6 +57,7 @@ struct ep_device;
57 * @extra: descriptors following this endpoint in the configuration 57 * @extra: descriptors following this endpoint in the configuration
58 * @extralen: how many bytes of "extra" are valid 58 * @extralen: how many bytes of "extra" are valid
59 * @enabled: URBs may be submitted to this endpoint 59 * @enabled: URBs may be submitted to this endpoint
60 * @streams: number of USB-3 streams allocated on the endpoint
60 * 61 *
61 * USB requests are always queued to a given endpoint, identified by a 62 * USB requests are always queued to a given endpoint, identified by a
62 * descriptor within an active interface in a given USB configuration. 63 * descriptor within an active interface in a given USB configuration.
@@ -71,6 +72,7 @@ struct usb_host_endpoint {
71 unsigned char *extra; /* Extra descriptors */ 72 unsigned char *extra; /* Extra descriptors */
72 int extralen; 73 int extralen;
73 int enabled; 74 int enabled;
75 int streams;
74}; 76};
75 77
76/* host-side wrapper for one interface setting's parsed descriptors */ 78/* host-side wrapper for one interface setting's parsed descriptors */
@@ -202,6 +204,8 @@ static inline void usb_set_intfdata(struct usb_interface *intf, void *data)
202struct usb_interface *usb_get_intf(struct usb_interface *intf); 204struct usb_interface *usb_get_intf(struct usb_interface *intf);
203void usb_put_intf(struct usb_interface *intf); 205void usb_put_intf(struct usb_interface *intf);
204 206
207/* Hard limit */
208#define USB_MAXENDPOINTS 30
205/* this maximum is arbitrary */ 209/* this maximum is arbitrary */
206#define USB_MAXINTERFACES 32 210#define USB_MAXINTERFACES 32
207#define USB_MAXIADS (USB_MAXINTERFACES/2) 211#define USB_MAXIADS (USB_MAXINTERFACES/2)
diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h
index efe8d8a7c7ad..485cd5e2100c 100644
--- a/include/linux/usb/hcd.h
+++ b/include/linux/usb/hcd.h
@@ -143,6 +143,7 @@ struct usb_hcd {
143 unsigned authorized_default:1; 143 unsigned authorized_default:1;
144 unsigned has_tt:1; /* Integrated TT in root hub */ 144 unsigned has_tt:1; /* Integrated TT in root hub */
145 unsigned amd_resume_bug:1; /* AMD remote wakeup quirk */ 145 unsigned amd_resume_bug:1; /* AMD remote wakeup quirk */
146 unsigned can_do_streams:1; /* HC supports streams */
146 147
147 unsigned int irq; /* irq allocated */ 148 unsigned int irq; /* irq allocated */
148 void __iomem *regs; /* device memory/io */ 149 void __iomem *regs; /* device memory/io */
diff --git a/include/linux/usb/uas.h b/include/linux/usb/uas.h
index 5499ab5c94bd..3fc8e8b9f043 100644
--- a/include/linux/usb/uas.h
+++ b/include/linux/usb/uas.h
@@ -9,7 +9,7 @@ struct iu {
9 __u8 iu_id; 9 __u8 iu_id;
10 __u8 rsvd1; 10 __u8 rsvd1;
11 __be16 tag; 11 __be16 tag;
12}; 12} __attribute__((__packed__));
13 13
14enum { 14enum {
15 IU_ID_COMMAND = 0x01, 15 IU_ID_COMMAND = 0x01,
@@ -52,7 +52,7 @@ struct command_iu {
52 __u8 rsvd7; 52 __u8 rsvd7;
53 struct scsi_lun lun; 53 struct scsi_lun lun;
54 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */ 54 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */
55}; 55} __attribute__((__packed__));
56 56
57struct task_mgmt_iu { 57struct task_mgmt_iu {
58 __u8 iu_id; 58 __u8 iu_id;
@@ -62,7 +62,7 @@ struct task_mgmt_iu {
62 __u8 rsvd2; 62 __u8 rsvd2;
63 __be16 task_tag; 63 __be16 task_tag;
64 struct scsi_lun lun; 64 struct scsi_lun lun;
65}; 65} __attribute__((__packed__));
66 66
67/* 67/*
68 * Also used for the Read Ready and Write Ready IUs since they have the 68 * Also used for the Read Ready and Write Ready IUs since they have the
@@ -77,15 +77,15 @@ struct sense_iu {
77 __u8 rsvd7[7]; 77 __u8 rsvd7[7];
78 __be16 len; 78 __be16 len;
79 __u8 sense[SCSI_SENSE_BUFFERSIZE]; 79 __u8 sense[SCSI_SENSE_BUFFERSIZE];
80}; 80} __attribute__((__packed__));
81 81
82struct response_ui { 82struct response_iu {
83 __u8 iu_id; 83 __u8 iu_id;
84 __u8 rsvd1; 84 __u8 rsvd1;
85 __be16 tag; 85 __be16 tag;
86 __be16 add_response_info; 86 __u8 add_response_info[3];
87 __u8 response_code; 87 __u8 response_code;
88}; 88} __attribute__((__packed__));
89 89
90struct usb_pipe_usage_descriptor { 90struct usb_pipe_usage_descriptor {
91 __u8 bLength; 91 __u8 bLength;
diff --git a/include/linux/usb_usual.h b/include/linux/usb_usual.h
index 630356866030..1a64b26046ed 100644
--- a/include/linux/usb_usual.h
+++ b/include/linux/usb_usual.h
@@ -67,8 +67,10 @@
67 /* Initial READ(10) (and others) must be retried */ \ 67 /* Initial READ(10) (and others) must be retried */ \
68 US_FLAG(WRITE_CACHE, 0x00200000) \ 68 US_FLAG(WRITE_CACHE, 0x00200000) \
69 /* Write Cache status is not available */ \ 69 /* Write Cache status is not available */ \
70 US_FLAG(NEEDS_CAP16, 0x00400000) 70 US_FLAG(NEEDS_CAP16, 0x00400000) \
71 /* cannot handle READ_CAPACITY_10 */ 71 /* cannot handle READ_CAPACITY_10 */ \
72 US_FLAG(IGNORE_UAS, 0x00800000) \
73 /* Device advertises UAS but it is broken */
72 74
73#define US_FLAG(name, value) US_FL_##name = value , 75#define US_FLAG(name, value) US_FL_##name = value ,
74enum { US_DO_ALL_FLAGS }; 76enum { US_DO_ALL_FLAGS };
diff --git a/include/uapi/linux/usbdevice_fs.h b/include/uapi/linux/usbdevice_fs.h
index 0c65e4b12617..abe5f4bd4d82 100644
--- a/include/uapi/linux/usbdevice_fs.h
+++ b/include/uapi/linux/usbdevice_fs.h
@@ -102,7 +102,10 @@ struct usbdevfs_urb {
102 int buffer_length; 102 int buffer_length;
103 int actual_length; 103 int actual_length;
104 int start_frame; 104 int start_frame;
105 int number_of_packets; 105 union {
106 int number_of_packets; /* Only used for isoc urbs */
107 unsigned int stream_id; /* Only used with bulk streams */
108 };
106 int error_count; 109 int error_count;
107 unsigned int signr; /* signal to be sent on completion, 110 unsigned int signr; /* signal to be sent on completion,
108 or 0 if none should be sent. */ 111 or 0 if none should be sent. */
@@ -144,6 +147,11 @@ struct usbdevfs_disconnect_claim {
144 char driver[USBDEVFS_MAXDRIVERNAME + 1]; 147 char driver[USBDEVFS_MAXDRIVERNAME + 1];
145}; 148};
146 149
150struct usbdevfs_streams {
151 unsigned int num_streams; /* Not used by USBDEVFS_FREE_STREAMS */
152 unsigned int num_eps;
153 unsigned char eps[0];
154};
147 155
148#define USBDEVFS_CONTROL _IOWR('U', 0, struct usbdevfs_ctrltransfer) 156#define USBDEVFS_CONTROL _IOWR('U', 0, struct usbdevfs_ctrltransfer)
149#define USBDEVFS_CONTROL32 _IOWR('U', 0, struct usbdevfs_ctrltransfer32) 157#define USBDEVFS_CONTROL32 _IOWR('U', 0, struct usbdevfs_ctrltransfer32)
@@ -176,5 +184,7 @@ struct usbdevfs_disconnect_claim {
176#define USBDEVFS_RELEASE_PORT _IOR('U', 25, unsigned int) 184#define USBDEVFS_RELEASE_PORT _IOR('U', 25, unsigned int)
177#define USBDEVFS_GET_CAPABILITIES _IOR('U', 26, __u32) 185#define USBDEVFS_GET_CAPABILITIES _IOR('U', 26, __u32)
178#define USBDEVFS_DISCONNECT_CLAIM _IOR('U', 27, struct usbdevfs_disconnect_claim) 186#define USBDEVFS_DISCONNECT_CLAIM _IOR('U', 27, struct usbdevfs_disconnect_claim)
187#define USBDEVFS_ALLOC_STREAMS _IOR('U', 28, struct usbdevfs_streams)
188#define USBDEVFS_FREE_STREAMS _IOR('U', 29, struct usbdevfs_streams)
179 189
180#endif /* _UAPI_LINUX_USBDEVICE_FS_H */ 190#endif /* _UAPI_LINUX_USBDEVICE_FS_H */