sky2: dont enable PME legacy mode
[safe/jmp/linux-2.6] / drivers / usb / host / xhci-hcd.c
index ddb1a6a..5e92c72 100644 (file)
@@ -67,22 +67,14 @@ static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
 }
 
 /*
- * Force HC into halt state.
- *
- * Disable any IRQs and clear the run/stop bit.
- * HC will complete any current and actively pipelined transactions, and
- * should halt within 16 microframes of the run/stop bit being cleared.
- * Read HC Halted bit in the status register to see when the HC is finished.
- * XXX: shouldn't we set HC_STATE_HALT here somewhere?
+ * Disable interrupts and begin the xHCI halting process.
  */
-int xhci_halt(struct xhci_hcd *xhci)
+void xhci_quiesce(struct xhci_hcd *xhci)
 {
        u32 halted;
        u32 cmd;
        u32 mask;
 
-       xhci_dbg(xhci, "// Halt the HC\n");
-       /* Disable all interrupts from the host controller */
        mask = ~(XHCI_IRQS);
        halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
        if (!halted)
@@ -91,6 +83,21 @@ int xhci_halt(struct xhci_hcd *xhci)
        cmd = xhci_readl(xhci, &xhci->op_regs->command);
        cmd &= mask;
        xhci_writel(xhci, cmd, &xhci->op_regs->command);
+}
+
+/*
+ * Force HC into halt state.
+ *
+ * Disable any IRQs and clear the run/stop bit.
+ * HC will complete any current and actively pipelined transactions, and
+ * should halt within 16 microframes of the run/stop bit being cleared.
+ * Read HC Halted bit in the status register to see when the HC is finished.
+ * XXX: shouldn't we set HC_STATE_HALT here somewhere?
+ */
+int xhci_halt(struct xhci_hcd *xhci)
+{
+       xhci_dbg(xhci, "// Halt the HC\n");
+       xhci_quiesce(xhci);
 
        return handshake(xhci, &xhci->op_regs->status,
                        STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
@@ -124,28 +131,6 @@ int xhci_reset(struct xhci_hcd *xhci)
        return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
 }
 
-/*
- * Stop the HC from processing the endpoint queues.
- */
-static void xhci_quiesce(struct xhci_hcd *xhci)
-{
-       /*
-        * Queues are per endpoint, so we need to disable an endpoint or slot.
-        *
-        * To disable a slot, we need to insert a disable slot command on the
-        * command ring and ring the doorbell.  This will also free any internal
-        * resources associated with the slot (which might not be what we want).
-        *
-        * A Release Endpoint command sounds better - doesn't free internal HC
-        * memory, but removes the endpoints from the schedule and releases the
-        * bandwidth, disables the doorbells, and clears the endpoint enable
-        * flag.  Usually used prior to a set interface command.
-        *
-        * TODO: Implement after command ring code is done.
-        */
-       BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
-       xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
-}
 
 #if 0
 /* Set up MSI-X table for entry 0 (may claim other entries later) */
@@ -224,7 +209,7 @@ int xhci_init(struct usb_hcd *hcd)
                xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
                xhci->quirks |= XHCI_LINK_TRB_QUIRK;
        } else {
-               xhci_dbg(xhci, "xHCI has no QUIRKS\n");
+               xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
        }
        retval = xhci_mem_init(xhci, GFP_KERNEL);
        xhci_dbg(xhci, "Finished xhci_init\n");
@@ -261,8 +246,14 @@ static void xhci_work(struct xhci_hcd *xhci)
        /* Flush posted writes */
        xhci_readl(xhci, &xhci->ir_set->irq_pending);
 
-       /* FIXME this should be a delayed service routine that clears the EHB */
-       xhci_handle_event(xhci);
+       if (xhci->xhc_state & XHCI_STATE_DYING)
+               xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
+                               "Shouldn't IRQs be disabled?\n");
+       else
+               /* FIXME this should be a delayed service routine
+                * that clears the EHB.
+                */
+               xhci_handle_event(xhci);
 
        /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
        temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
@@ -335,6 +326,12 @@ void xhci_event_ring_work(unsigned long arg)
        spin_lock_irqsave(&xhci->lock, flags);
        temp = xhci_readl(xhci, &xhci->op_regs->status);
        xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
+       if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
+               xhci_dbg(xhci, "HW died, polling stopped.\n");
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               return;
+       }
+
        temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
        xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
        xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
@@ -351,13 +348,14 @@ void xhci_event_ring_work(unsigned long arg)
        xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
        xhci_dbg_cmd_ptrs(xhci);
        for (i = 0; i < MAX_HC_SLOTS; ++i) {
-               if (xhci->devs[i]) {
-                       for (j = 0; j < 31; ++j) {
-                               if (xhci->devs[i]->ep_rings[j]) {
-                                       xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
-                                       xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
-                               }
-                       }
+               if (!xhci->devs[i])
+                       continue;
+               for (j = 0; j < 31; ++j) {
+                       struct xhci_ring *ring = xhci->devs[i]->eps[j].ring;
+                       if (!ring)
+                               continue;
+                       xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
+                       xhci_debug_segment(xhci, ring->deq_seg);
                }
        }
 
@@ -483,8 +481,6 @@ void xhci_stop(struct usb_hcd *hcd)
        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 
        spin_lock_irq(&xhci->lock);
-       if (HC_IS_RUNNING(hcd->state))
-               xhci_quiesce(xhci);
        xhci_halt(xhci);
        xhci_reset(xhci);
        spin_unlock_irq(&xhci->lock);
@@ -567,13 +563,22 @@ unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
        return 1 << (xhci_get_endpoint_index(desc) + 1);
 }
 
+/* Find the flag for this endpoint (for use in the control context).  Use the
+ * endpoint index to create a bitmask.  The slot context is bit 0, endpoint 0 is
+ * bit 1, etc.
+ */
+unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
+{
+       return 1 << (ep_index + 1);
+}
+
 /* Compute the last valid endpoint context index.  Basically, this is the
  * endpoint index plus one.  For slot contexts with more than valid endpoint,
  * we find the most significant bit set in the added contexts flags.
  * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  */
-static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
+unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
 {
        return fls(added_ctxs) - 1;
 }
@@ -601,6 +606,71 @@ int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
        return 1;
 }
 
+static int xhci_configure_endpoint(struct xhci_hcd *xhci,
+               struct usb_device *udev, struct xhci_command *command,
+               bool ctx_change, bool must_succeed);
+
+/*
+ * Full speed devices may have a max packet size greater than 8 bytes, but the
+ * USB core doesn't know that until it reads the first 8 bytes of the
+ * descriptor.  If the usb_device's max packet size changes after that point,
+ * we need to issue an evaluate context command and wait on it.
+ */
+static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
+               unsigned int ep_index, struct urb *urb)
+{
+       struct xhci_container_ctx *in_ctx;
+       struct xhci_container_ctx *out_ctx;
+       struct xhci_input_control_ctx *ctrl_ctx;
+       struct xhci_ep_ctx *ep_ctx;
+       int max_packet_size;
+       int hw_max_packet_size;
+       int ret = 0;
+
+       out_ctx = xhci->devs[slot_id]->out_ctx;
+       ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
+       hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
+       max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
+       if (hw_max_packet_size != max_packet_size) {
+               xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
+               xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
+                               max_packet_size);
+               xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
+                               hw_max_packet_size);
+               xhci_dbg(xhci, "Issuing evaluate context command.\n");
+
+               /* Set up the modified control endpoint 0 */
+               xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
+                               xhci->devs[slot_id]->out_ctx, ep_index);
+               in_ctx = xhci->devs[slot_id]->in_ctx;
+               ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
+               ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
+               ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
+
+               /* Set up the input context flags for the command */
+               /* FIXME: This won't work if a non-default control endpoint
+                * changes max packet sizes.
+                */
+               ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
+               ctrl_ctx->add_flags = EP0_FLAG;
+               ctrl_ctx->drop_flags = 0;
+
+               xhci_dbg(xhci, "Slot %d input context\n", slot_id);
+               xhci_dbg_ctx(xhci, in_ctx, ep_index);
+               xhci_dbg(xhci, "Slot %d output context\n", slot_id);
+               xhci_dbg_ctx(xhci, out_ctx, ep_index);
+
+               ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
+                               true, false);
+
+               /* Clean up the input context for later use by bandwidth
+                * functions.
+                */
+               ctrl_ctx->add_flags = SLOT_FLAG;
+       }
+       return ret;
+}
+
 /*
  * non-error returns are a promise to giveback() the urb later
  * we drop ownership so next owner (or urb unlink) can get it
@@ -612,13 +682,13 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
        int ret = 0;
        unsigned int slot_id, ep_index;
 
+
        if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
                return -EINVAL;
 
        slot_id = urb->dev->slot_id;
        ep_index = xhci_get_endpoint_index(&urb->ep->desc);
 
-       spin_lock_irqsave(&xhci->lock, flags);
        if (!xhci->devs || !xhci->devs[slot_id]) {
                if (!in_interrupt())
                        dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
@@ -631,20 +701,51 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
                ret = -ESHUTDOWN;
                goto exit;
        }
-       if (usb_endpoint_xfer_control(&urb->ep->desc))
+       if (usb_endpoint_xfer_control(&urb->ep->desc)) {
+               /* Check to see if the max packet size for the default control
+                * endpoint changed during FS device enumeration
+                */
+               if (urb->dev->speed == USB_SPEED_FULL) {
+                       ret = xhci_check_maxpacket(xhci, slot_id,
+                                       ep_index, urb);
+                       if (ret < 0)
+                               return ret;
+               }
+
                /* We have a spinlock and interrupts disabled, so we must pass
                 * atomic context to this function, which may allocate memory.
                 */
+               spin_lock_irqsave(&xhci->lock, flags);
+               if (xhci->xhc_state & XHCI_STATE_DYING)
+                       goto dying;
                ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
                                slot_id, ep_index);
-       else if (usb_endpoint_xfer_bulk(&urb->ep->desc))
+               spin_unlock_irqrestore(&xhci->lock, flags);
+       } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
+               spin_lock_irqsave(&xhci->lock, flags);
+               if (xhci->xhc_state & XHCI_STATE_DYING)
+                       goto dying;
                ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
                                slot_id, ep_index);
-       else
+               spin_unlock_irqrestore(&xhci->lock, flags);
+       } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
+               spin_lock_irqsave(&xhci->lock, flags);
+               if (xhci->xhc_state & XHCI_STATE_DYING)
+                       goto dying;
+               ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
+                               slot_id, ep_index);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+       } else {
                ret = -EINVAL;
+       }
 exit:
-       spin_unlock_irqrestore(&xhci->lock, flags);
        return ret;
+dying:
+       xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
+                       "non-responsive xHCI host.\n",
+                       urb->ep->desc.bEndpointAddress, urb);
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       return -ESHUTDOWN;
 }
 
 /*
@@ -682,10 +783,12 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 {
        unsigned long flags;
        int ret;
+       u32 temp;
        struct xhci_hcd *xhci;
        struct xhci_td *td;
        unsigned int ep_index;
        struct xhci_ring *ep_ring;
+       struct xhci_virt_ep *ep;
 
        xhci = hcd_to_xhci(hcd);
        spin_lock_irqsave(&xhci->lock, flags);
@@ -693,22 +796,49 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
        ret = usb_hcd_check_unlink_urb(hcd, urb, status);
        if (ret || !urb->hcpriv)
                goto done;
+       temp = xhci_readl(xhci, &xhci->op_regs->status);
+       if (temp == 0xffffffff) {
+               xhci_dbg(xhci, "HW died, freeing TD.\n");
+               td = (struct xhci_td *) urb->hcpriv;
+
+               usb_hcd_unlink_urb_from_ep(hcd, urb);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
+               kfree(td);
+               return ret;
+       }
+       if (xhci->xhc_state & XHCI_STATE_DYING) {
+               xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
+                               "non-responsive xHCI host.\n",
+                               urb->ep->desc.bEndpointAddress, urb);
+               /* Let the stop endpoint command watchdog timer (which set this
+                * state) finish cleaning up the endpoint TD lists.  We must
+                * have caught it in the middle of dropping a lock and giving
+                * back an URB.
+                */
+               goto done;
+       }
 
        xhci_dbg(xhci, "Cancel URB %p\n", urb);
        xhci_dbg(xhci, "Event ring:\n");
        xhci_debug_ring(xhci, xhci->event_ring);
        ep_index = xhci_get_endpoint_index(&urb->ep->desc);
-       ep_ring = xhci->devs[urb->dev->slot_id]->ep_rings[ep_index];
+       ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
+       ep_ring = ep->ring;
        xhci_dbg(xhci, "Endpoint ring:\n");
        xhci_debug_ring(xhci, ep_ring);
        td = (struct xhci_td *) urb->hcpriv;
 
-       ep_ring->cancels_pending++;
-       list_add_tail(&td->cancelled_td_list, &ep_ring->cancelled_td_list);
+       list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
        /* Queue a stop endpoint command, but only if this is
         * the first cancellation to be handled.
         */
-       if (ep_ring->cancels_pending == 1) {
+       if (!(ep->ep_state & EP_HALT_PENDING)) {
+               ep->ep_state |= EP_HALT_PENDING;
+               ep->stop_cmds_pending++;
+               ep->stop_cmd_timer.expires = jiffies +
+                       XHCI_STOP_EP_CMD_TIMEOUT * HZ;
+               add_timer(&ep->stop_cmd_timer);
                xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
                xhci_ring_cmd_db(xhci);
        }
@@ -781,7 +911,7 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
        ctrl_ctx->drop_flags |= drop_flag;
        new_drop_flags = ctrl_ctx->drop_flags;
 
-       ctrl_ctx->add_flags = ~drop_flag;
+       ctrl_ctx->add_flags &= ~drop_flag;
        new_add_flags = ctrl_ctx->add_flags;
 
        last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
@@ -943,11 +1073,11 @@ static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *vir
 }
 
 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
-               struct usb_device *udev, struct xhci_virt_device *virt_dev)
+               struct usb_device *udev, int *cmd_status)
 {
        int ret;
 
-       switch (virt_dev->cmd_status) {
+       switch (*cmd_status) {
        case COMP_ENOMEM:
                dev_warn(&udev->dev, "Not enough host controller resources "
                                "for new device state.\n");
@@ -973,7 +1103,7 @@ static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
                break;
        default:
                xhci_err(xhci, "ERROR: unexpected command completion "
-                               "code 0x%x.\n", virt_dev->cmd_status);
+                               "code 0x%x.\n", *cmd_status);
                ret = -EINVAL;
                break;
        }
@@ -981,11 +1111,12 @@ static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
 }
 
 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
-               struct usb_device *udev, struct xhci_virt_device *virt_dev)
+               struct usb_device *udev, int *cmd_status)
 {
        int ret;
+       struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
 
-       switch (virt_dev->cmd_status) {
+       switch (*cmd_status) {
        case COMP_EINVAL:
                dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
                                "context command.\n");
@@ -1006,7 +1137,7 @@ static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
                break;
        default:
                xhci_err(xhci, "ERROR: unexpected command completion "
-                               "code 0x%x.\n", virt_dev->cmd_status);
+                               "code 0x%x.\n", *cmd_status);
                ret = -EINVAL;
                break;
        }
@@ -1017,19 +1148,37 @@ static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  * and wait for it to finish.
  */
 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
-               struct usb_device *udev, struct xhci_virt_device *virt_dev,
-               bool ctx_change)
+               struct usb_device *udev,
+               struct xhci_command *command,
+               bool ctx_change, bool must_succeed)
 {
        int ret;
        int timeleft;
        unsigned long flags;
+       struct xhci_container_ctx *in_ctx;
+       struct completion *cmd_completion;
+       int *cmd_status;
+       struct xhci_virt_device *virt_dev;
 
        spin_lock_irqsave(&xhci->lock, flags);
+       virt_dev = xhci->devs[udev->slot_id];
+       if (command) {
+               in_ctx = command->in_ctx;
+               cmd_completion = command->completion;
+               cmd_status = &command->status;
+               command->command_trb = xhci->cmd_ring->enqueue;
+               list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
+       } else {
+               in_ctx = virt_dev->in_ctx;
+               cmd_completion = &virt_dev->cmd_completion;
+               cmd_status = &virt_dev->cmd_status;
+       }
+
        if (!ctx_change)
-               ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx->dma,
-                               udev->slot_id);
+               ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
+                               udev->slot_id, must_succeed);
        else
-               ret = xhci_queue_evaluate_context(xhci, virt_dev->in_ctx->dma,
+               ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
                                udev->slot_id);
        if (ret < 0) {
                spin_unlock_irqrestore(&xhci->lock, flags);
@@ -1041,7 +1190,7 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 
        /* Wait for the configure endpoint command to complete */
        timeleft = wait_for_completion_interruptible_timeout(
-                       &virt_dev->cmd_completion,
+                       cmd_completion,
                        USB_CTRL_SET_TIMEOUT);
        if (timeleft <= 0) {
                xhci_warn(xhci, "%s while waiting for %s command\n",
@@ -1054,8 +1203,8 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
        }
 
        if (!ctx_change)
-               return xhci_configure_endpoint_result(xhci, udev, virt_dev);
-       return xhci_evaluate_context_result(xhci, udev, virt_dev);
+               return xhci_configure_endpoint_result(xhci, udev, cmd_status);
+       return xhci_evaluate_context_result(xhci, udev, cmd_status);
 }
 
 /* Called after one or more calls to xhci_add_endpoint() or
@@ -1101,7 +1250,8 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
        xhci_dbg_ctx(xhci, virt_dev->in_ctx,
                        LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
 
-       ret = xhci_configure_endpoint(xhci, udev, virt_dev, false);
+       ret = xhci_configure_endpoint(xhci, udev, NULL,
+                       false, false);
        if (ret) {
                /* Callee should call reset_bandwidth() */
                return ret;
@@ -1112,13 +1262,35 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
                        LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
 
        xhci_zero_in_ctx(xhci, virt_dev);
-       /* Free any old rings */
+       /* Install new rings and free or cache any old rings */
        for (i = 1; i < 31; ++i) {
-               if (virt_dev->new_ep_rings[i]) {
-                       xhci_ring_free(xhci, virt_dev->ep_rings[i]);
-                       virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
-                       virt_dev->new_ep_rings[i] = NULL;
+               int rings_cached;
+
+               if (!virt_dev->eps[i].new_ring)
+                       continue;
+               /* Only cache or free the old ring if it exists.
+                * It may not if this is the first add of an endpoint.
+                */
+               if (virt_dev->eps[i].ring) {
+                       rings_cached = virt_dev->num_rings_cached;
+                       if (rings_cached < XHCI_MAX_RINGS_CACHED) {
+                               virt_dev->num_rings_cached++;
+                               rings_cached = virt_dev->num_rings_cached;
+                               virt_dev->ring_cache[rings_cached] =
+                                       virt_dev->eps[i].ring;
+                               xhci_dbg(xhci, "Cached old ring, "
+                                               "%d ring%s cached\n",
+                                               rings_cached,
+                                               (rings_cached > 1) ? "s" : "");
+                       } else {
+                               xhci_ring_free(xhci, virt_dev->eps[i].ring);
+                               xhci_dbg(xhci, "Ring cache full (%d rings), "
+                                               "freeing ring\n",
+                                               virt_dev->num_rings_cached);
+                       }
                }
+               virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
+               virt_dev->eps[i].new_ring = NULL;
        }
 
        return ret;
@@ -1144,14 +1316,93 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
        virt_dev = xhci->devs[udev->slot_id];
        /* Free any rings allocated for added endpoints */
        for (i = 0; i < 31; ++i) {
-               if (virt_dev->new_ep_rings[i]) {
-                       xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
-                       virt_dev->new_ep_rings[i] = NULL;
+               if (virt_dev->eps[i].new_ring) {
+                       xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
+                       virt_dev->eps[i].new_ring = NULL;
                }
        }
        xhci_zero_in_ctx(xhci, virt_dev);
 }
 
+static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx,
+               struct xhci_container_ctx *out_ctx,
+               u32 add_flags, u32 drop_flags)
+{
+       struct xhci_input_control_ctx *ctrl_ctx;
+       ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
+       ctrl_ctx->add_flags = add_flags;
+       ctrl_ctx->drop_flags = drop_flags;
+       xhci_slot_copy(xhci, in_ctx, out_ctx);
+       ctrl_ctx->add_flags |= SLOT_FLAG;
+
+       xhci_dbg(xhci, "Input Context:\n");
+       xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
+}
+
+void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
+               unsigned int slot_id, unsigned int ep_index,
+               struct xhci_dequeue_state *deq_state)
+{
+       struct xhci_container_ctx *in_ctx;
+       struct xhci_ep_ctx *ep_ctx;
+       u32 added_ctxs;
+       dma_addr_t addr;
+
+       xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
+                       xhci->devs[slot_id]->out_ctx, ep_index);
+       in_ctx = xhci->devs[slot_id]->in_ctx;
+       ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
+       addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
+                       deq_state->new_deq_ptr);
+       if (addr == 0) {
+               xhci_warn(xhci, "WARN Cannot submit config ep after "
+                               "reset ep command\n");
+               xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
+                               deq_state->new_deq_seg,
+                               deq_state->new_deq_ptr);
+               return;
+       }
+       ep_ctx->deq = addr | deq_state->new_cycle_state;
+
+       added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
+       xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
+                       xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
+}
+
+void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
+               struct usb_device *udev, unsigned int ep_index)
+{
+       struct xhci_dequeue_state deq_state;
+       struct xhci_virt_ep *ep;
+
+       xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
+       ep = &xhci->devs[udev->slot_id]->eps[ep_index];
+       /* We need to move the HW's dequeue pointer past this TD,
+        * or it will attempt to resend it on the next doorbell ring.
+        */
+       xhci_find_new_dequeue_state(xhci, udev->slot_id,
+                       ep_index, ep->stopped_td,
+                       &deq_state);
+
+       /* HW with the reset endpoint quirk will use the saved dequeue state to
+        * issue a configure endpoint command later.
+        */
+       if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
+               xhci_dbg(xhci, "Queueing new dequeue state\n");
+               xhci_queue_new_dequeue_state(xhci, udev->slot_id,
+                               ep_index, &deq_state);
+       } else {
+               /* Better hope no one uses the input context between now and the
+                * reset endpoint completion!
+                */
+               xhci_dbg(xhci, "Setting up input context for "
+                               "configure endpoint command\n");
+               xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
+                               ep_index, &deq_state);
+       }
+}
+
 /* Deal with stalled endpoints.  The core should have sent the control message
  * to clear the halt condition.  However, we need to make the xHCI hardware
  * reset its sequence number, since a device will expect a sequence number of
@@ -1166,8 +1417,7 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
        unsigned int ep_index;
        unsigned long flags;
        int ret;
-       struct xhci_dequeue_state deq_state;
-       struct xhci_ring *ep_ring;
+       struct xhci_virt_ep *virt_ep;
 
        xhci = hcd_to_xhci(hcd);
        udev = (struct usb_device *) ep->hcpriv;
@@ -1177,12 +1427,16 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
        if (!ep->hcpriv)
                return;
        ep_index = xhci_get_endpoint_index(&ep->desc);
-       ep_ring = xhci->devs[udev->slot_id]->ep_rings[ep_index];
-       if (!ep_ring->stopped_td) {
+       virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
+       if (!virt_ep->stopped_td) {
                xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
                                ep->desc.bEndpointAddress);
                return;
        }
+       if (usb_endpoint_xfer_control(&ep->desc)) {
+               xhci_dbg(xhci, "Control endpoint stall already handled.\n");
+               return;
+       }
 
        xhci_dbg(xhci, "Queueing reset endpoint command\n");
        spin_lock_irqsave(&xhci->lock, flags);
@@ -1193,17 +1447,8 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
         * command.  Better hope that last command worked!
         */
        if (!ret) {
-               xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
-               /* We need to move the HW's dequeue pointer past this TD,
-                * or it will attempt to resend it on the next doorbell ring.
-                */
-               xhci_find_new_dequeue_state(xhci, udev->slot_id,
-                               ep_index, ep_ring->stopped_td, &deq_state);
-               xhci_dbg(xhci, "Queueing new dequeue state\n");
-               xhci_queue_new_dequeue_state(xhci, ep_ring,
-                               udev->slot_id,
-                               ep_index, &deq_state);
-               kfree(ep_ring->stopped_td);
+               xhci_cleanup_stalled_ring(xhci, udev, ep_index);
+               kfree(virt_ep->stopped_td);
                xhci_ring_cmd_db(xhci);
        }
        spin_unlock_irqrestore(&xhci->lock, flags);
@@ -1220,12 +1465,32 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
 void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
 {
        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       struct xhci_virt_device *virt_dev;
        unsigned long flags;
+       u32 state;
+       int i;
 
        if (udev->slot_id == 0)
                return;
+       virt_dev = xhci->devs[udev->slot_id];
+       if (!virt_dev)
+               return;
+
+       /* Stop any wayward timer functions (which may grab the lock) */
+       for (i = 0; i < 31; ++i) {
+               virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
+               del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
+       }
 
        spin_lock_irqsave(&xhci->lock, flags);
+       /* Don't disable the slot if the host controller is dead. */
+       state = xhci_readl(xhci, &xhci->op_regs->status);
+       if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
+               xhci_free_virt_device(xhci, udev->slot_id);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               return;
+       }
+
        if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
                spin_unlock_irqrestore(&xhci->lock, flags);
                xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
@@ -1405,6 +1670,88 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
        return 0;
 }
 
+/* Once a hub descriptor is fetched for a device, we need to update the xHC's
+ * internal data structures for the device.
+ */
+int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
+                       struct usb_tt *tt, gfp_t mem_flags)
+{
+       struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       struct xhci_virt_device *vdev;
+       struct xhci_command *config_cmd;
+       struct xhci_input_control_ctx *ctrl_ctx;
+       struct xhci_slot_ctx *slot_ctx;
+       unsigned long flags;
+       unsigned think_time;
+       int ret;
+
+       /* Ignore root hubs */
+       if (!hdev->parent)
+               return 0;
+
+       vdev = xhci->devs[hdev->slot_id];
+       if (!vdev) {
+               xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
+               return -EINVAL;
+       }
+       config_cmd = xhci_alloc_command(xhci, true, mem_flags);
+       if (!config_cmd) {
+               xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
+               return -ENOMEM;
+       }
+
+       spin_lock_irqsave(&xhci->lock, flags);
+       xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
+       ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
+       ctrl_ctx->add_flags |= SLOT_FLAG;
+       slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
+       slot_ctx->dev_info |= DEV_HUB;
+       if (tt->multi)
+               slot_ctx->dev_info |= DEV_MTT;
+       if (xhci->hci_version > 0x95) {
+               xhci_dbg(xhci, "xHCI version %x needs hub "
+                               "TT think time and number of ports\n",
+                               (unsigned int) xhci->hci_version);
+               slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
+               /* Set TT think time - convert from ns to FS bit times.
+                * 0 = 8 FS bit times, 1 = 16 FS bit times,
+                * 2 = 24 FS bit times, 3 = 32 FS bit times.
+                */
+               think_time = tt->think_time;
+               if (think_time != 0)
+                       think_time = (think_time / 666) - 1;
+               slot_ctx->tt_info |= TT_THINK_TIME(think_time);
+       } else {
+               xhci_dbg(xhci, "xHCI version %x doesn't need hub "
+                               "TT think time or number of ports\n",
+                               (unsigned int) xhci->hci_version);
+       }
+       slot_ctx->dev_state = 0;
+       spin_unlock_irqrestore(&xhci->lock, flags);
+
+       xhci_dbg(xhci, "Set up %s for hub device.\n",
+                       (xhci->hci_version > 0x95) ?
+                       "configure endpoint" : "evaluate context");
+       xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
+       xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
+
+       /* Issue and wait for the configure endpoint or
+        * evaluate context command.
+        */
+       if (xhci->hci_version > 0x95)
+               ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
+                               false, false);
+       else
+               ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
+                               true, false);
+
+       xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
+       xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
+
+       xhci_free_command(xhci, config_cmd);
+       return ret;
+}
+
 int xhci_get_frame(struct usb_hcd *hcd)
 {
        struct xhci_hcd *xhci = hcd_to_xhci(hcd);