Author: hselasky
Date: Mon Jan 13 15:21:11 2014
New Revision: 260589
URL: http://svnweb.freebsd.org/changeset/base/260589

Log:
  Implement better error recovery for Transaction Translators, TTs,
  found in High Speed USB HUBs which translate from High Speed USB into
  FULL or LOW speed USB. In some rare cases SPLIT transactions might get
  lost, which might leave the TT in an unknown state. Whenever we detect
  such an error try to issue either a clear TT buffer request, or if
  that is not possible reset the whole TT.
  
  MFC after:    1 week

Modified:
  head/sys/dev/usb/usb_device.c
  head/sys/dev/usb/usb_device.h
  head/sys/dev/usb/usb_hub.c
  head/sys/dev/usb/usb_hub.h
  head/sys/dev/usb/usb_request.c
  head/sys/dev/usb/usb_transfer.c

Modified: head/sys/dev/usb/usb_device.c
==============================================================================
--- head/sys/dev/usb/usb_device.c       Mon Jan 13 15:06:03 2014        
(r260588)
+++ head/sys/dev/usb/usb_device.c       Mon Jan 13 15:21:11 2014        
(r260589)
@@ -98,7 +98,7 @@ static void   usb_init_attach_arg(struct u
                    struct usb_attach_arg *);
 static void    usb_suspend_resume_sub(struct usb_device *, device_t,
                    uint8_t);
-static void    usbd_clear_stall_proc(struct usb_proc_msg *_pm);
+static usb_proc_callback_t usbd_clear_stall_proc;
 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
 static void    usbd_set_device_strings(struct usb_device *);
 #if USB_HAVE_DEVCTL
@@ -1474,7 +1474,7 @@ usb_suspend_resume(struct usb_device *ud
 static void
 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
 {
-       struct usb_clear_stall_msg *pm = (void *)_pm;
+       struct usb_udev_msg *pm = (void *)_pm;
        struct usb_device *udev = pm->udev;
 
        /* Change lock */

Modified: head/sys/dev/usb/usb_device.h
==============================================================================
--- head/sys/dev/usb/usb_device.h       Mon Jan 13 15:06:03 2014        
(r260588)
+++ head/sys/dev/usb/usb_device.h       Mon Jan 13 15:21:11 2014        
(r260589)
@@ -53,7 +53,7 @@ struct usb_symlink;           /* UGEN */
 #define        USB_UNCFG_FLAG_NONE 0x00
 #define        USB_UNCFG_FLAG_FREE_EP0 0x02            /* endpoint zero is 
freed */
 
-struct usb_clear_stall_msg {
+struct usb_udev_msg {
        struct usb_proc_msg hdr;
        struct usb_device *udev;
 };
@@ -179,8 +179,8 @@ union usb_device_scratch {
  * these structures for every USB device.
  */
 struct usb_device {
-       struct usb_clear_stall_msg cs_msg[2];   /* generic clear stall
-                                                * messages */
+       /* generic clear stall message */
+       struct usb_udev_msg cs_msg[2];
        struct sx enum_sx;
        struct sx sr_sx;
        struct mtx device_mtx;
@@ -316,4 +316,10 @@ void       usbd_sr_lock(struct usb_device *);
 void   usbd_sr_unlock(struct usb_device *);
 uint8_t usbd_enum_is_locked(struct usb_device *);
 
+#if USB_HAVE_TT_SUPPORT
+void   uhub_tt_buffer_reset_async_locked(struct usb_device *, struct 
usb_endpoint *);
+#endif
+
+uint8_t uhub_count_active_host_ports(struct usb_device *, enum usb_dev_speed);
+
 #endif                                 /* _USB_DEVICE_H_ */

Modified: head/sys/dev/usb/usb_hub.c
==============================================================================
--- head/sys/dev/usb/usb_hub.c  Mon Jan 13 15:06:03 2014        (r260588)
+++ head/sys/dev/usb/usb_hub.c  Mon Jan 13 15:21:11 2014        (r260589)
@@ -74,7 +74,13 @@
 #endif                 /* USB_GLOBAL_INCLUDE_FILE */
 
 #define        UHUB_INTR_INTERVAL 250          /* ms */
-#define        UHUB_N_TRANSFER 1
+enum {
+       UHUB_INTR_TRANSFER,
+#if USB_HAVE_TT_SUPPORT
+       UHUB_RESET_TT_TRANSFER,
+#endif
+       UHUB_N_TRANSFER,
+};
 
 #ifdef USB_DEBUG
 static int uhub_debug = 0;
@@ -129,6 +135,9 @@ static bus_child_location_str_t uhub_chi
 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
 
 static usb_callback_t uhub_intr_callback;
+#if USB_HAVE_TT_SUPPORT
+static usb_callback_t uhub_reset_tt_callback;
+#endif
 
 static void usb_dev_resume_peer(struct usb_device *udev);
 static void usb_dev_suspend_peer(struct usb_device *udev);
@@ -136,7 +145,7 @@ static uint8_t usb_peer_should_wakeup(st
 
 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
 
-       [0] = {
+       [UHUB_INTR_TRANSFER] = {
                .type = UE_INTERRUPT,
                .endpoint = UE_ADDR_ANY,
                .direction = UE_DIR_ANY,
@@ -146,6 +155,17 @@ static const struct usb_config uhub_conf
                .callback = &uhub_intr_callback,
                .interval = UHUB_INTR_INTERVAL,
        },
+#if USB_HAVE_TT_SUPPORT
+       [UHUB_RESET_TT_TRANSFER] = {
+               .type = UE_CONTROL,
+               .endpoint = 0x00,       /* Control pipe */
+               .direction = UE_DIR_ANY,
+               .bufsize = sizeof(struct usb_device_request),
+               .callback = &uhub_reset_tt_callback,
+               .timeout = 1000,        /* 1 second */
+               .usb_mode = USB_MODE_HOST,
+       },
+#endif
 };
 
 /*
@@ -215,6 +235,199 @@ uhub_intr_callback(struct usb_xfer *xfer
 }
 
 /*------------------------------------------------------------------------*
+ *      uhub_reset_tt_proc
+ *
+ * This function starts the TT reset USB request
+ *------------------------------------------------------------------------*/
+#if USB_HAVE_TT_SUPPORT
+static void
+uhub_reset_tt_proc(struct usb_proc_msg *_pm)
+{
+       struct usb_udev_msg *pm = (void *)_pm;
+       struct usb_device *udev = pm->udev;
+       struct usb_hub *hub;
+       struct uhub_softc *sc;
+
+       hub = udev->hub;
+       if (hub == NULL)
+               return;
+       sc = hub->hubsoftc;
+       if (sc == NULL)
+               return;
+
+       /* Change lock */
+       USB_BUS_UNLOCK(udev->bus);
+       mtx_lock(&sc->sc_mtx);
+       /* Start transfer */
+       usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
+       /* Change lock */
+       mtx_unlock(&sc->sc_mtx);
+       USB_BUS_LOCK(udev->bus);
+}
+#endif
+
+/*------------------------------------------------------------------------*
+ *      uhub_tt_buffer_reset_async_locked
+ *
+ * This function queues a TT reset for the given USB device and endpoint.
+ *------------------------------------------------------------------------*/
+#if USB_HAVE_TT_SUPPORT
+void
+uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct 
usb_endpoint *ep)
+{
+       struct usb_device_request req;
+       struct usb_device *udev;
+       struct usb_hub *hub;
+       struct usb_port *up;
+       uint16_t wValue;
+       uint8_t port;
+
+       if (child == NULL || ep == NULL)
+               return;
+
+       udev = child->parent_hs_hub;
+       port = child->hs_port_no;
+
+       if (udev == NULL)
+               return;
+
+       hub = udev->hub;
+       if ((hub == NULL) ||
+           (udev->speed != USB_SPEED_HIGH) ||
+           (child->speed != USB_SPEED_LOW &&
+            child->speed != USB_SPEED_FULL) ||
+           (child->flags.usb_mode != USB_MODE_HOST) ||
+           (port == 0) || (ep->edesc == NULL)) {
+               /* not applicable */
+               return;
+       }
+
+       USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
+
+       up = hub->ports + port - 1;
+
+       if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
+           udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
+               port = 1;
+
+       /* if we already received a clear buffer request, reset the whole TT */
+       if (up->req_reset_tt.bRequest != 0) {
+               req.bmRequestType = UT_WRITE_CLASS_OTHER;
+               req.bRequest = UR_RESET_TT;
+               USETW(req.wValue, 0);
+               req.wIndex[0] = port;
+               req.wIndex[1] = 0;
+               USETW(req.wLength, 0);
+       } else {
+               wValue = (ep->edesc->bEndpointAddress & 0xF) |
+                     ((child->address & 0x7F) << 4) |
+                     ((ep->edesc->bEndpointAddress & 0x80) << 8) |
+                     ((ep->edesc->bmAttributes & 3) << 12);
+
+               req.bmRequestType = UT_WRITE_CLASS_OTHER;
+               req.bRequest = UR_CLEAR_TT_BUFFER;
+               USETW(req.wValue, wValue);
+               req.wIndex[0] = port;
+               req.wIndex[1] = 0;
+               USETW(req.wLength, 0);
+       }
+       up->req_reset_tt = req;
+       /* get reset transfer started */
+       usb_proc_msignal(USB_BUS_NON_GIANT_PROC(udev->bus),
+           &hub->tt_msg[0], &hub->tt_msg[1]);
+}
+#endif
+
+#if USB_HAVE_TT_SUPPORT
+static void
+uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
+{
+       struct uhub_softc *sc;
+       struct usb_device *udev;
+       struct usb_port *up;
+       uint8_t x;
+
+       DPRINTF("TT buffer reset\n");
+
+       sc = usbd_xfer_softc(xfer);
+       udev = sc->sc_udev;
+
+       switch (USB_GET_STATE(xfer)) {
+       case USB_ST_TRANSFERRED:
+       case USB_ST_SETUP:
+tr_setup:
+               USB_BUS_LOCK(udev->bus);
+               /* find first port which needs a TT reset */
+               for (x = 0; x != udev->hub->nports; x++) {
+                       up = udev->hub->ports + x;
+
+                       if (up->req_reset_tt.bRequest == 0)
+                               continue;
+
+                       /* copy in the transfer */
+                       usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
+                           sizeof(up->req_reset_tt));
+                       /* reset buffer */
+                       memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
+
+                       /* set length */
+                       usbd_xfer_set_frame_len(xfer, 0, 
sizeof(up->req_reset_tt));
+                       xfer->nframes = 1;
+                       USB_BUS_UNLOCK(udev->bus);
+
+                       usbd_transfer_submit(xfer);
+                       return;
+               }
+               USB_BUS_UNLOCK(udev->bus);
+               break;
+
+       default:
+               if (error == USB_ERR_CANCELLED)
+                       break;
+
+               DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
+               goto tr_setup;
+       }
+}
+#endif
+
+/*------------------------------------------------------------------------*
+ *      uhub_count_active_host_ports
+ *
+ * This function counts the number of active ports at the given speed.
+ *------------------------------------------------------------------------*/
+uint8_t
+uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
+{
+       struct uhub_softc *sc;
+       struct usb_device *child;
+       struct usb_hub *hub;
+       struct usb_port *up;
+       uint8_t retval = 0;
+       uint8_t x;
+
+       if (udev == NULL)
+               goto done;
+       hub = udev->hub;
+       if (hub == NULL)
+               goto done;
+       sc = hub->hubsoftc;
+       if (sc == NULL)
+               goto done;
+
+       for (x = 0; x != hub->nports; x++) {
+               up = hub->ports + x;
+               child = usb_bus_port_get_device(udev->bus, up);
+               if (child != NULL &&
+                   child->flags.usb_mode == USB_MODE_HOST &&
+                   child->speed == speed)
+                       retval++;
+       }
+done:
+       return (retval);
+}
+
+/*------------------------------------------------------------------------*
  *     uhub_explore_sub - subroutine
  *
  * Return values:
@@ -1114,7 +1327,12 @@ uhub_attach(device_t dev)
        hub->explore = &uhub_explore;
        hub->nports = nports;
        hub->hubudev = udev;
-
+#if USB_HAVE_TT_SUPPORT
+       hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
+       hub->tt_msg[0].udev = udev;
+       hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
+       hub->tt_msg[1].udev = udev;
+#endif
        /* if self powered hub, give ports maximum current */
        if (udev->flags.self_powered) {
                hub->portpower = USB_MAX_POWER;
@@ -1216,11 +1434,9 @@ uhub_attach(device_t dev)
 
        /* Start the interrupt endpoint, if any */
 
-       if (sc->sc_xfer[0] != NULL) {
-               mtx_lock(&sc->sc_mtx);
-               usbd_transfer_start(sc->sc_xfer[0]);
-               mtx_unlock(&sc->sc_mtx);
-       }
+       mtx_lock(&sc->sc_mtx);
+       usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
+       mtx_unlock(&sc->sc_mtx);
 
        /* Enable automatic power save on all USB HUBs */
 
@@ -1250,6 +1466,7 @@ uhub_detach(device_t dev)
 {
        struct uhub_softc *sc = device_get_softc(dev);
        struct usb_hub *hub = sc->sc_udev->hub;
+       struct usb_bus *bus = sc->sc_udev->bus;
        struct usb_device *child;
        uint8_t x;
 
@@ -1262,7 +1479,7 @@ uhub_detach(device_t dev)
        /* Detach all ports */
        for (x = 0; x != hub->nports; x++) {
 
-               child = usb_bus_port_get_device(sc->sc_udev->bus, hub->ports + 
x);
+               child = usb_bus_port_get_device(bus, hub->ports + x);
 
                if (child == NULL) {
                        continue;
@@ -1274,6 +1491,14 @@ uhub_detach(device_t dev)
                usb_free_device(child, 0);
        }
 
+#if USB_HAVE_TT_SUPPORT
+       /* Make sure our TT messages are not queued anywhere */
+       USB_BUS_LOCK(bus);
+       usb_proc_mwait(USB_BUS_NON_GIANT_PROC(bus),
+           &hub->tt_msg[0], &hub->tt_msg[1]);
+       USB_BUS_UNLOCK(bus);
+#endif
+
 #if (USB_HAVE_FIXED_PORT == 0)
        free(hub, M_USBDEV);
 #endif

Modified: head/sys/dev/usb/usb_hub.h
==============================================================================
--- head/sys/dev/usb/usb_hub.h  Mon Jan 13 15:06:03 2014        (r260588)
+++ head/sys/dev/usb/usb_hub.h  Mon Jan 13 15:21:11 2014        (r260589)
@@ -35,6 +35,9 @@ struct usb_port {
 #define        USB_RESTART_MAX 5
        uint8_t device_index;           /* zero means not valid */
        enum usb_hc_mode usb_mode;      /* host or device mode */
+#if USB_HAVE_TT_SUPPORT
+       struct usb_device_request req_reset_tt __aligned(4);
+#endif
 };
 
 /*
@@ -44,6 +47,9 @@ struct usb_hub {
        struct usb_device *hubudev;     /* the HUB device */
        usb_error_t (*explore) (struct usb_device *hub);
        void   *hubsoftc;
+#if USB_HAVE_TT_SUPPORT
+       struct usb_udev_msg tt_msg[2];
+#endif
        usb_size_t uframe_usage[USB_HS_MICRO_FRAMES_MAX];
        uint16_t portpower;             /* mA per USB port */
        uint8_t isoc_last_time;

Modified: head/sys/dev/usb/usb_request.c
==============================================================================
--- head/sys/dev/usb/usb_request.c      Mon Jan 13 15:06:03 2014        
(r260588)
+++ head/sys/dev/usb/usb_request.c      Mon Jan 13 15:21:11 2014        
(r260589)
@@ -721,6 +721,17 @@ done:
        if ((mtx != NULL) && (mtx != &Giant))
                mtx_lock(mtx);
 
+       switch (err) {
+       case USB_ERR_NORMAL_COMPLETION:
+       case USB_ERR_SHORT_XFER:
+       case USB_ERR_STALLED:
+       case USB_ERR_CANCELLED:
+               break;
+       default:
+               DPRINTF("I/O error - waiting a bit for TT cleanup\n");
+               usb_pause_mtx(mtx, hz / 16);
+               break;
+       }
        return ((usb_error_t)err);
 }
 
@@ -2010,6 +2021,7 @@ usbd_req_re_enumerate(struct usb_device 
                return (USB_ERR_INVAL);
        }
 retry:
+#if USB_HAVE_TT_SUPPORT
        /*
         * Try to reset the High Speed parent HUB of a LOW- or FULL-
         * speed device, if any.
@@ -2017,15 +2029,24 @@ retry:
        if (udev->parent_hs_hub != NULL &&
            udev->speed != USB_SPEED_HIGH) {
                DPRINTF("Trying to reset parent High Speed TT.\n");
-               err = usbd_req_reset_tt(udev->parent_hs_hub, NULL,
-                   udev->hs_port_no);
+               if (udev->parent_hs_hub == parent_hub &&
+                   (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
+                    uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) 
== 1) {
+                       /* we can reset the whole TT */
+                       err = usbd_req_reset_tt(parent_hub, NULL,
+                           udev->hs_port_no);
+               } else {
+                       /* only reset a particular device and endpoint */
+                       err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, 
NULL,
+                           udev->hs_port_no, old_addr, UE_CONTROL, 0);
+               }
                if (err) {
                        DPRINTF("Resetting parent High "
                            "Speed TT failed (%s).\n",
                            usbd_errstr(err));
                }
        }
-
+#endif
        /* Try to warm reset first */
        if (parent_hub->speed == USB_SPEED_SUPER)
                usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);

Modified: head/sys/dev/usb/usb_transfer.c
==============================================================================
--- head/sys/dev/usb/usb_transfer.c     Mon Jan 13 15:06:03 2014        
(r260588)
+++ head/sys/dev/usb/usb_transfer.c     Mon Jan 13 15:21:11 2014        
(r260589)
@@ -2432,7 +2432,9 @@ usbd_transfer_enqueue(struct usb_xfer_qu
 void
 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
 {
-       USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
+       struct usb_xfer_root *info = xfer->xroot;
+
+       USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
 
        DPRINTF("err=%s\n", usbd_errstr(error));
 
@@ -2446,10 +2448,10 @@ usbd_transfer_done(struct usb_xfer *xfer
                xfer->flags_int.control_act = 0;
                return;
        }
-       /* only set transfer error if not already set */
-       if (!xfer->error) {
+       /* only set transfer error, if not already set */
+       if (xfer->error == USB_ERR_NORMAL_COMPLETION)
                xfer->error = error;
-       }
+
        /* stop any callouts */
        usb_callout_stop(&xfer->timeout_handle);
 
@@ -2461,14 +2463,14 @@ usbd_transfer_done(struct usb_xfer *xfer
        usbd_transfer_dequeue(xfer);
 
 #if USB_HAVE_BUSDMA
-       if (mtx_owned(xfer->xroot->xfer_mtx)) {
+       if (mtx_owned(info->xfer_mtx)) {
                struct usb_xfer_queue *pq;
 
                /*
                 * If the private USB lock is not locked, then we assume
                 * that the BUS-DMA load stage has been passed:
                 */
-               pq = &xfer->xroot->dma_q;
+               pq = &info->dma_q;
 
                if (pq->curr == xfer) {
                        /* start the next BUS-DMA load, if any */
@@ -2478,10 +2480,10 @@ usbd_transfer_done(struct usb_xfer *xfer
 #endif
        /* keep some statistics */
        if (xfer->error) {
-               xfer->xroot->bus->stats_err.uds_requests
+               info->bus->stats_err.uds_requests
                    [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
        } else {
-               xfer->xroot->bus->stats_ok.uds_requests
+               info->bus->stats_ok.uds_requests
                    [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
        }
 
@@ -2847,6 +2849,22 @@ usbd_callback_wrapper_sub(struct usb_xfe
                /* end of control transfer, if any */
                xfer->flags_int.control_act = 0;
 
+#if USB_HAVE_TT_SUPPORT
+               switch (xfer->error) {
+               case USB_ERR_NORMAL_COMPLETION:
+               case USB_ERR_SHORT_XFER:
+               case USB_ERR_STALLED:
+               case USB_ERR_CANCELLED:
+                       /* nothing to do */
+                       break;
+               default:
+                       /* try to reset the TT, if any */
+                       USB_BUS_LOCK(bus);
+                       uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, 
xfer->endpoint);
+                       USB_BUS_UNLOCK(bus);
+                       break;
+               }
+#endif
                /* check if we should block the execution queue */
                if ((xfer->error != USB_ERR_CANCELLED) &&
                    (xfer->flags.pipe_bof)) {
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to