xhci: Restore event ring dequeue pointer on resume.
[linux-flexiantxendom0.git] / drivers / usb / host / xhci.c
index 238ebe1..0f92504 100644 (file)
@@ -98,17 +98,21 @@ void xhci_quiesce(struct xhci_hcd *xhci)
  */
 int xhci_halt(struct xhci_hcd *xhci)
 {
+       int ret;
        xhci_dbg(xhci, "// Halt the HC\n");
        xhci_quiesce(xhci);
 
-       return handshake(xhci, &xhci->op_regs->status,
+       ret = handshake(xhci, &xhci->op_regs->status,
                        STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
+       if (!ret)
+               xhci->xhc_state |= XHCI_STATE_HALTED;
+       return ret;
 }
 
 /*
  * Set the run bit and wait for the host to be running.
  */
-int xhci_start(struct xhci_hcd *xhci)
+static int xhci_start(struct xhci_hcd *xhci)
 {
        u32 temp;
        int ret;
@@ -129,6 +133,8 @@ int xhci_start(struct xhci_hcd *xhci)
                xhci_err(xhci, "Host took too long to start, "
                                "waited %u microseconds.\n",
                                XHCI_MAX_HALT_USEC);
+       if (!ret)
+               xhci->xhc_state &= ~XHCI_STATE_HALTED;
        return ret;
 }
 
@@ -169,28 +175,19 @@ int xhci_reset(struct xhci_hcd *xhci)
        return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
 }
 
-/*
- * Free IRQs
- * free all IRQs request
- */
-static void xhci_free_irq(struct xhci_hcd *xhci)
+#ifdef CONFIG_PCI
+static int xhci_free_msi(struct xhci_hcd *xhci)
 {
        int i;
-       struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
-
-       /* return if using legacy interrupt */
-       if (xhci_to_hcd(xhci)->irq >= 0)
-               return;
 
-       if (xhci->msix_entries) {
-               for (i = 0; i < xhci->msix_count; i++)
-                       if (xhci->msix_entries[i].vector)
-                               free_irq(xhci->msix_entries[i].vector,
-                                               xhci_to_hcd(xhci));
-       } else if (pdev->irq >= 0)
-               free_irq(pdev->irq, xhci_to_hcd(xhci));
+       if (!xhci->msix_entries)
+               return -EINVAL;
 
-       return;
+       for (i = 0; i < xhci->msix_count; i++)
+               if (xhci->msix_entries[i].vector)
+                       free_irq(xhci->msix_entries[i].vector,
+                                       xhci_to_hcd(xhci));
+       return 0;
 }
 
 /*
@@ -203,14 +200,14 @@ static int xhci_setup_msi(struct xhci_hcd *xhci)
 
        ret = pci_enable_msi(pdev);
        if (ret) {
-               xhci_err(xhci, "failed to allocate MSI entry\n");
+               xhci_dbg(xhci, "failed to allocate MSI entry\n");
                return ret;
        }
 
        ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
                                0, "xhci_hcd", xhci_to_hcd(xhci));
        if (ret) {
-               xhci_err(xhci, "disable MSI interrupt\n");
+               xhci_dbg(xhci, "disable MSI interrupt\n");
                pci_disable_msi(pdev);
        }
 
@@ -218,6 +215,28 @@ static int xhci_setup_msi(struct xhci_hcd *xhci)
 }
 
 /*
+ * Free IRQs
+ * free all IRQs request
+ */
+static void xhci_free_irq(struct xhci_hcd *xhci)
+{
+       struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
+       int ret;
+
+       /* return if using legacy interrupt */
+       if (xhci_to_hcd(xhci)->irq >= 0)
+               return;
+
+       ret = xhci_free_msi(xhci);
+       if (!ret)
+               return;
+       if (pdev->irq >= 0)
+               free_irq(pdev->irq, xhci_to_hcd(xhci));
+
+       return;
+}
+
+/*
  * Set up MSI-X
  */
 static int xhci_setup_msix(struct xhci_hcd *xhci)
@@ -251,7 +270,7 @@ static int xhci_setup_msix(struct xhci_hcd *xhci)
 
        ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
        if (ret) {
-               xhci_err(xhci, "Failed to enable MSI-X\n");
+               xhci_dbg(xhci, "Failed to enable MSI-X\n");
                goto free_entries;
        }
 
@@ -267,7 +286,7 @@ static int xhci_setup_msix(struct xhci_hcd *xhci)
        return ret;
 
 disable_msix:
-       xhci_err(xhci, "disable MSI-X interrupt\n");
+       xhci_dbg(xhci, "disable MSI-X interrupt\n");
        xhci_free_irq(xhci);
        pci_disable_msix(pdev);
 free_entries:
@@ -296,6 +315,77 @@ static void xhci_cleanup_msix(struct xhci_hcd *xhci)
        return;
 }
 
+static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
+{
+       int i;
+
+       if (xhci->msix_entries) {
+               for (i = 0; i < xhci->msix_count; i++)
+                       synchronize_irq(xhci->msix_entries[i].vector);
+       }
+}
+
+static int xhci_try_enable_msi(struct usb_hcd *hcd)
+{
+       struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       struct pci_dev  *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
+       int ret;
+
+       /*
+        * Some Fresco Logic host controllers advertise MSI, but fail to
+        * generate interrupts.  Don't even try to enable MSI.
+        */
+       if (xhci->quirks & XHCI_BROKEN_MSI)
+               return 0;
+
+       /* unregister the legacy interrupt */
+       if (hcd->irq)
+               free_irq(hcd->irq, hcd);
+       hcd->irq = -1;
+
+       ret = xhci_setup_msix(xhci);
+       if (ret)
+               /* fall back to msi*/
+               ret = xhci_setup_msi(xhci);
+
+       if (!ret)
+               /* hcd->irq is -1, we have MSI */
+               return 0;
+
+       if (!pdev->irq) {
+               xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
+               return -EINVAL;
+       }
+
+       /* fall back to legacy interrupt*/
+       ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
+                       hcd->irq_descr, hcd);
+       if (ret) {
+               xhci_err(xhci, "request interrupt %d failed\n",
+                               pdev->irq);
+               return ret;
+       }
+       hcd->irq = pdev->irq;
+       return 0;
+}
+
+#else
+
+static int xhci_try_enable_msi(struct usb_hcd *hcd)
+{
+       return 0;
+}
+
+static void xhci_cleanup_msix(struct xhci_hcd *xhci)
+{
+}
+
+static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
+{
+}
+
+#endif
+
 /*
  * Initialize memory for HCD and xHC (one-time init).
  *
@@ -310,7 +400,7 @@ int xhci_init(struct usb_hcd *hcd)
 
        xhci_dbg(xhci, "xhci_init\n");
        spin_lock_init(&xhci->lock);
-       if (link_quirk) {
+       if (xhci->hci_version == 0x95 && link_quirk) {
                xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
                xhci->quirks |= XHCI_LINK_TRB_QUIRK;
        } else {
@@ -326,7 +416,7 @@ int xhci_init(struct usb_hcd *hcd)
 
 
 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
-void xhci_event_ring_work(unsigned long arg)
+static void xhci_event_ring_work(unsigned long arg)
 {
        unsigned long flags;
        int temp;
@@ -339,7 +429,8 @@ 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)) {
+       if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
+                       (xhci->xhc_state & XHCI_STATE_HALTED)) {
                xhci_dbg(xhci, "HW died, polling stopped.\n");
                spin_unlock_irqrestore(&xhci->lock, flags);
                return;
@@ -406,9 +497,8 @@ int xhci_run(struct usb_hcd *hcd)
 {
        u32 temp;
        u64 temp_64;
-       u32 ret;
+       int ret;
        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
-       struct pci_dev  *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
 
        /* Start the xHCI host controller running only after the USB 2.0 roothub
         * is setup.
@@ -419,27 +509,10 @@ int xhci_run(struct usb_hcd *hcd)
                return xhci_run_finished(xhci);
 
        xhci_dbg(xhci, "xhci_run\n");
-       /* unregister the legacy interrupt */
-       if (hcd->irq)
-               free_irq(hcd->irq, hcd);
-       hcd->irq = -1;
 
-       ret = xhci_setup_msix(xhci);
+       ret = xhci_try_enable_msi(hcd);
        if (ret)
-               /* fall back to msi*/
-               ret = xhci_setup_msi(xhci);
-
-       if (ret) {
-               /* fall back to legacy interrupt*/
-               ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
-                                       hcd->irq_descr, hcd);
-               if (ret) {
-                       xhci_err(xhci, "request interrupt %d failed\n",
-                                       pdev->irq);
-                       return ret;
-               }
-               hcd->irq = pdev->irq;
-       }
+               return ret;
 
 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
        init_timer(&xhci->event_ring_timer);
@@ -484,7 +557,7 @@ int xhci_run(struct usb_hcd *hcd)
                        xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
        xhci_writel(xhci, ER_IRQ_ENABLE(temp),
                        &xhci->ir_set->irq_pending);
-       xhci_print_ir_set(xhci, xhci->ir_set, 0);
+       xhci_print_ir_set(xhci, 0);
 
        if (xhci->quirks & XHCI_NEC_HOST)
                xhci_queue_vendor_command(xhci, 0, 0, 0,
@@ -544,13 +617,16 @@ void xhci_stop(struct usb_hcd *hcd)
        del_timer_sync(&xhci->event_ring_timer);
 #endif
 
+       if (xhci->quirks & XHCI_AMD_PLL_FIX)
+               usb_amd_dev_put();
+
        xhci_dbg(xhci, "// Disabling event ring interrupts\n");
        temp = xhci_readl(xhci, &xhci->op_regs->status);
        xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
        temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
        xhci_writel(xhci, ER_IRQ_DISABLE(temp),
                        &xhci->ir_set->irq_pending);
-       xhci_print_ir_set(xhci, xhci->ir_set, 0);
+       xhci_print_ir_set(xhci, 0);
 
        xhci_dbg(xhci, "cleaning up memory\n");
        xhci_mem_cleanup(xhci);
@@ -605,6 +681,7 @@ static void xhci_restore_registers(struct xhci_hcd *xhci)
        xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
        xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
        xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
+       xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
 }
 
 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
@@ -640,7 +717,10 @@ static void xhci_clear_command_ring(struct xhci_hcd *xhci)
        ring = xhci->cmd_ring;
        seg = ring->deq_seg;
        do {
-               memset(seg->trbs, 0, SEGMENT_SIZE);
+               memset(seg->trbs, 0,
+                       sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
+               seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
+                       cpu_to_le32(~TRB_CYCLE);
                seg = seg->next;
        } while (seg != ring->deq_seg);
 
@@ -677,7 +757,6 @@ int xhci_suspend(struct xhci_hcd *xhci)
        int                     rc = 0;
        struct usb_hcd          *hcd = xhci_to_hcd(xhci);
        u32                     command;
-       int                     i;
 
        spin_lock_irq(&xhci->lock);
        clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
@@ -713,10 +792,7 @@ int xhci_suspend(struct xhci_hcd *xhci)
 
        /* step 5: remove core well power */
        /* synchronize irq when using MSI-X */
-       if (xhci->msix_entries) {
-               for (i = 0; i < xhci->msix_count; i++)
-                       synchronize_irq(xhci->msix_entries[i].vector);
-       }
+       xhci_msix_sync_irqs(xhci);
 
        return rc;
 }
@@ -732,17 +808,22 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
        u32                     command, temp = 0;
        struct usb_hcd          *hcd = xhci_to_hcd(xhci);
        struct usb_hcd          *secondary_hcd;
-       int                     retval;
+       int                     retval = 0;
 
        /* Wait a bit if either of the roothubs need to settle from the
-        * transistion into bus suspend.
+        * transition into bus suspend.
         */
        if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
                        time_before(jiffies,
                                xhci->bus_state[1].next_statechange))
                msleep(100);
 
+       set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+       set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
+
        spin_lock_irq(&xhci->lock);
+       if (xhci->quirks & XHCI_RESET_ON_RESUME)
+               hibernated = true;
 
        if (!hibernated) {
                /* step 1: restore register */
@@ -765,7 +846,9 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
 
        /* If restore operation fails, re-initialize the HC during resume */
        if ((temp & STS_SRE) || hibernated) {
-               usb_root_hub_lost_power(hcd->self.root_hub);
+               /* Let the USB core know _both_ roothubs lost power. */
+               usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
+               usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
 
                xhci_dbg(xhci, "Stop HCD\n");
                xhci_halt(xhci);
@@ -785,7 +868,7 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
                temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
                xhci_writel(xhci, ER_IRQ_DISABLE(temp),
                                &xhci->ir_set->irq_pending);
-               xhci_print_ir_set(xhci, xhci->ir_set, 0);
+               xhci_print_ir_set(xhci, 0);
 
                xhci_dbg(xhci, "cleaning up memory\n");
                xhci_mem_cleanup(xhci);
@@ -807,20 +890,13 @@ int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
                        return retval;
                xhci_dbg(xhci, "Start the primary HCD\n");
                retval = xhci_run(hcd->primary_hcd);
-               if (retval)
-                       goto failed_restart;
-
-               xhci_dbg(xhci, "Start the secondary HCD\n");
-               retval = xhci_run(secondary_hcd);
                if (!retval) {
-                       set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
-                       set_bit(HCD_FLAG_HW_ACCESSIBLE,
-                                       &xhci->shared_hcd->flags);
+                       xhci_dbg(xhci, "Start the secondary HCD\n");
+                       retval = xhci_run(secondary_hcd);
                }
-failed_restart:
                hcd->state = HC_STATE_SUSPENDED;
                xhci->shared_hcd->state = HC_STATE_SUSPENDED;
-               return retval;
+               goto done;
        }
 
        /* step 4: set Run/Stop bit */
@@ -839,11 +915,14 @@ failed_restart:
         * Running endpoints by ringing their doorbells
         */
 
-       set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
-       set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
-
        spin_unlock_irq(&xhci->lock);
-       return 0;
+
+ done:
+       if (retval == 0) {
+               usb_hcd_resume_root_hub(hcd);
+               usb_hcd_resume_root_hub(xhci->shared_hcd);
+       }
+       return retval;
 }
 #endif /* CONFIG_PM */
 
@@ -902,7 +981,7 @@ unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
 /* Returns 1 if the arguments are OK;
  * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  */
-int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
+static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
                struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
                const char *func) {
        struct xhci_hcd *xhci;
@@ -919,10 +998,12 @@ int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
                return 0;
        }
 
+       xhci = hcd_to_xhci(hcd);
+       if (xhci->xhc_state & XHCI_STATE_HALTED)
+               return -ENODEV;
+
        if (check_virt_dev) {
-               xhci = hcd_to_xhci(hcd);
-               if (!udev->slot_id || !xhci->devs
-                       || !xhci->devs[udev->slot_id]) {
+               if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
                        printk(KERN_DEBUG "xHCI %s called with unaddressed "
                                                "device\n", func);
                        return -EINVAL;
@@ -962,8 +1043,8 @@ static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
 
        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;
+       hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
+       max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
        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",
@@ -977,15 +1058,15 @@ static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
                                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);
+               ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
+               ep_ctx->ep_info2 |= cpu_to_le32(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->add_flags = cpu_to_le32(EP0_FLAG);
                ctrl_ctx->drop_flags = 0;
 
                xhci_dbg(xhci, "Slot %d input context\n", slot_id);
@@ -999,7 +1080,7 @@ static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
                /* Clean up the input context for later use by bandwidth
                 * functions.
                 */
-               ctrl_ctx->add_flags = SLOT_FLAG;
+               ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
        }
        return ret;
 }
@@ -1011,6 +1092,7 @@ static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
 int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
 {
        struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       struct xhci_td *buffer;
        unsigned long flags;
        int ret = 0;
        unsigned int slot_id, ep_index;
@@ -1041,13 +1123,15 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
        if (!urb_priv)
                return -ENOMEM;
 
+       buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
+       if (!buffer) {
+               kfree(urb_priv);
+               return -ENOMEM;
+       }
+
        for (i = 0; i < size; i++) {
-               urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
-               if (!urb_priv->td[i]) {
-                       urb_priv->length = i;
-                       xhci_urb_free_priv(xhci, urb_priv);
-                       return -ENOMEM;
-               }
+               urb_priv->td[i] = buffer;
+               buffer++;
        }
 
        urb_priv->length = size;
@@ -1061,8 +1145,11 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
                if (urb->dev->speed == USB_SPEED_FULL) {
                        ret = xhci_check_maxpacket(xhci, slot_id,
                                        ep_index, urb);
-                       if (ret < 0)
+                       if (ret < 0) {
+                               xhci_urb_free_priv(xhci, urb_priv);
+                               urb->hcpriv = NULL;
                                return ret;
+                       }
                }
 
                /* We have a spinlock and interrupts disabled, so we must pass
@@ -1073,6 +1160,8 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
                        goto dying;
                ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
                                slot_id, ep_index);
+               if (ret)
+                       goto free_priv;
                spin_unlock_irqrestore(&xhci->lock, flags);
        } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
                spin_lock_irqsave(&xhci->lock, flags);
@@ -1093,6 +1182,8 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
                        ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
                                        slot_id, ep_index);
                }
+               if (ret)
+                       goto free_priv;
                spin_unlock_irqrestore(&xhci->lock, flags);
        } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
                spin_lock_irqsave(&xhci->lock, flags);
@@ -1100,6 +1191,8 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
                        goto dying;
                ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
                                slot_id, ep_index);
+               if (ret)
+                       goto free_priv;
                spin_unlock_irqrestore(&xhci->lock, flags);
        } else {
                spin_lock_irqsave(&xhci->lock, flags);
@@ -1107,18 +1200,22 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
                        goto dying;
                ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
                                slot_id, ep_index);
+               if (ret)
+                       goto free_priv;
                spin_unlock_irqrestore(&xhci->lock, flags);
        }
 exit:
        return ret;
 dying:
-       xhci_urb_free_priv(xhci, urb_priv);
-       urb->hcpriv = NULL;
        xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
                        "non-responsive xHCI host.\n",
                        urb->ep->desc.bEndpointAddress, urb);
+       ret = -ESHUTDOWN;
+free_priv:
+       xhci_urb_free_priv(xhci, urb_priv);
+       urb->hcpriv = NULL;
        spin_unlock_irqrestore(&xhci->lock, flags);
-       return -ESHUTDOWN;
+       return ret;
 }
 
 /* Get the right ring for the given URB.
@@ -1212,9 +1309,16 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
        if (ret || !urb->hcpriv)
                goto done;
        temp = xhci_readl(xhci, &xhci->op_regs->status);
-       if (temp == 0xffffffff) {
+       if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
                xhci_dbg(xhci, "HW died, freeing TD.\n");
                urb_priv = urb->hcpriv;
+               for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
+                       td = urb_priv->td[i];
+                       if (!list_empty(&td->td_list))
+                               list_del_init(&td->td_list);
+                       if (!list_empty(&td->cancelled_td_list))
+                               list_del_init(&td->cancelled_td_list);
+               }
 
                usb_hcd_unlink_urb_from_ep(hcd, urb);
                spin_unlock_irqrestore(&xhci->lock, flags);
@@ -1222,7 +1326,8 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
                xhci_urb_free_priv(xhci, urb_priv);
                return ret;
        }
-       if (xhci->xhc_state & XHCI_STATE_DYING) {
+       if ((xhci->xhc_state & XHCI_STATE_DYING) ||
+                       (xhci->xhc_state & XHCI_STATE_HALTED)) {
                xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
                                "non-responsive xHCI host.\n",
                                urb->ep->desc.bEndpointAddress, urb);
@@ -1303,8 +1408,10 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
        if (ret <= 0)
                return ret;
        xhci = hcd_to_xhci(hcd);
-       xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
+       if (xhci->xhc_state & XHCI_STATE_DYING)
+               return -ENODEV;
 
+       xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
        drop_flag = xhci_get_endpoint_flag(&ep->desc);
        if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
                xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
@@ -1320,27 +1427,30 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
        /* If the HC already knows the endpoint is disabled,
         * or the HCD has noted it is disabled, ignore this request
         */
-       if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
-                       ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
+       if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
+            cpu_to_le32(EP_STATE_DISABLED)) ||
+           le32_to_cpu(ctrl_ctx->drop_flags) &
+           xhci_get_endpoint_flag(&ep->desc)) {
                xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
                                __func__, ep);
                return 0;
        }
 
-       ctrl_ctx->drop_flags |= drop_flag;
-       new_drop_flags = ctrl_ctx->drop_flags;
+       ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
+       new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
 
-       ctrl_ctx->add_flags &= ~drop_flag;
-       new_add_flags = ctrl_ctx->add_flags;
+       ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
+       new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
 
-       last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
+       last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
        slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
        /* Update the last valid endpoint context, if we deleted the last one */
-       if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
-               slot_ctx->dev_info &= ~LAST_CTX_MASK;
-               slot_ctx->dev_info |= LAST_CTX(last_ctx);
+       if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
+           LAST_CTX(last_ctx)) {
+               slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
+               slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
        }
-       new_slot_info = slot_ctx->dev_info;
+       new_slot_info = le32_to_cpu(slot_ctx->dev_info);
 
        xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
 
@@ -1378,6 +1488,7 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
        u32 added_ctxs;
        unsigned int last_ctx;
        u32 new_add_flags, new_drop_flags, new_slot_info;
+       struct xhci_virt_device *virt_dev;
        int ret = 0;
 
        ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
@@ -1387,6 +1498,8 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
                return ret;
        }
        xhci = hcd_to_xhci(hcd);
+       if (xhci->xhc_state & XHCI_STATE_DYING)
+               return -ENODEV;
 
        added_ctxs = xhci_get_endpoint_flag(&ep->desc);
        last_ctx = xhci_last_valid_endpoint(added_ctxs);
@@ -1400,15 +1513,30 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
                return 0;
        }
 
-       in_ctx = xhci->devs[udev->slot_id]->in_ctx;
-       out_ctx = xhci->devs[udev->slot_id]->out_ctx;
+       virt_dev = xhci->devs[udev->slot_id];
+       in_ctx = virt_dev->in_ctx;
+       out_ctx = virt_dev->out_ctx;
        ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
        ep_index = xhci_get_endpoint_index(&ep->desc);
        ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
+
+       /* If this endpoint is already in use, and the upper layers are trying
+        * to add it again without dropping it, reject the addition.
+        */
+       if (virt_dev->eps[ep_index].ring &&
+                       !(le32_to_cpu(ctrl_ctx->drop_flags) &
+                               xhci_get_endpoint_flag(&ep->desc))) {
+               xhci_warn(xhci, "Trying to add endpoint 0x%x "
+                               "without dropping it.\n",
+                               (unsigned int) ep->desc.bEndpointAddress);
+               return -EINVAL;
+       }
+
        /* If the HCD has already noted the endpoint is enabled,
         * ignore this request.
         */
-       if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
+       if (le32_to_cpu(ctrl_ctx->add_flags) &
+           xhci_get_endpoint_flag(&ep->desc)) {
                xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
                                __func__, ep);
                return 0;
@@ -1419,15 +1547,14 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
         * process context, not interrupt context (or so documenation
         * for usb_set_interface() and usb_set_configuration() claim).
         */
-       if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
-                               udev, ep, GFP_NOIO) < 0) {
+       if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
                dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
                                __func__, ep->desc.bEndpointAddress);
                return -ENOMEM;
        }
 
-       ctrl_ctx->add_flags |= added_ctxs;
-       new_add_flags = ctrl_ctx->add_flags;
+       ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
+       new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
 
        /* If xhci_endpoint_disable() was called for this endpoint, but the
         * xHC hasn't been notified yet through the check_bandwidth() call,
@@ -1435,15 +1562,16 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
         * descriptors.  We must drop and re-add this endpoint, so we leave the
         * drop flags alone.
         */
-       new_drop_flags = ctrl_ctx->drop_flags;
+       new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
 
        slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
        /* Update the last valid endpoint context, if we just added one past */
-       if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
-               slot_ctx->dev_info &= ~LAST_CTX_MASK;
-               slot_ctx->dev_info |= LAST_CTX(last_ctx);
+       if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
+           LAST_CTX(last_ctx)) {
+               slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
+               slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
        }
-       new_slot_info = slot_ctx->dev_info;
+       new_slot_info = le32_to_cpu(slot_ctx->dev_info);
 
        /* Store the usb_device pointer for later use */
        ep->hcpriv = udev;
@@ -1473,9 +1601,9 @@ static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *vir
        ctrl_ctx->drop_flags = 0;
        ctrl_ctx->add_flags = 0;
        slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
-       slot_ctx->dev_info &= ~LAST_CTX_MASK;
+       slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
        /* Endpoint 0 is always valid */
-       slot_ctx->dev_info |= LAST_CTX(1);
+       slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
        for (i = 1; i < 31; ++i) {
                ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
                ep_ctx->ep_info = 0;
@@ -1486,7 +1614,7 @@ 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, int *cmd_status)
+               struct usb_device *udev, u32 *cmd_status)
 {
        int ret;
 
@@ -1498,6 +1626,7 @@ static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
                /* FIXME: can we allocate more resources for the HC? */
                break;
        case COMP_BW_ERR:
+       case COMP_2ND_BW_ERR:
                dev_warn(&udev->dev, "Not enough bandwidth "
                                "for new device state.\n");
                ret = -ENOSPC;
@@ -1510,6 +1639,11 @@ static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
                                "and endpoint is not disabled.\n");
                ret = -EINVAL;
                break;
+       case COMP_DEV_ERR:
+               dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
+                               "configure command.\n");
+               ret = -ENODEV;
+               break;
        case COMP_SUCCESS:
                dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
                ret = 0;
@@ -1524,7 +1658,7 @@ static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
 }
 
 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
-               struct usb_device *udev, int *cmd_status)
+               struct usb_device *udev, u32 *cmd_status)
 {
        int ret;
        struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
@@ -1544,6 +1678,16 @@ static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
                xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
                ret = -EINVAL;
                break;
+       case COMP_DEV_ERR:
+               dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
+                               "context command.\n");
+               ret = -ENODEV;
+               break;
+       case COMP_MEL_ERR:
+               /* Max Exit Latency too large error */
+               dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
+               ret = -EINVAL;
+               break;
        case COMP_SUCCESS:
                dev_dbg(&udev->dev, "Successful evaluate context command\n");
                ret = 0;
@@ -1557,92 +1701,793 @@ static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
        return ret;
 }
 
-/* Issue a configure endpoint command or evaluate context command
- * and wait for it to finish.
- */
-static int xhci_configure_endpoint(struct xhci_hcd *xhci,
-               struct usb_device *udev,
-               struct xhci_command *command,
-               bool ctx_change, bool must_succeed)
+static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx)
 {
-       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;
+       struct xhci_input_control_ctx *ctrl_ctx;
+       u32 valid_add_flags;
+       u32 valid_drop_flags;
 
-       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;
+       ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
+       /* Ignore the slot flag (bit 0), and the default control endpoint flag
+        * (bit 1).  The default control endpoint is added during the Address
+        * Device command and is never removed until the slot is disabled.
+        */
+       valid_add_flags = ctrl_ctx->add_flags >> 2;
+       valid_drop_flags = ctrl_ctx->drop_flags >> 2;
 
-               /* Enqueue pointer can be left pointing to the link TRB,
-                * we must handle that
-                */
-               if ((command->command_trb->link.control & TRB_TYPE_BITMASK)
-                               == TRB_TYPE(TRB_LINK))
-                       command->command_trb =
-                               xhci->cmd_ring->enq_seg->next->trbs;
+       /* Use hweight32 to count the number of ones in the add flags, or
+        * number of endpoints added.  Don't count endpoints that are changed
+        * (both added and dropped).
+        */
+       return hweight32(valid_add_flags) -
+               hweight32(valid_add_flags & valid_drop_flags);
+}
 
-               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;
-       }
-       init_completion(cmd_completion);
+static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx)
+{
+       struct xhci_input_control_ctx *ctrl_ctx;
+       u32 valid_add_flags;
+       u32 valid_drop_flags;
 
-       if (!ctx_change)
-               ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
-                               udev->slot_id, must_succeed);
-       else
-               ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
-                               udev->slot_id);
-       if (ret < 0) {
-               if (command)
-                       list_del(&command->cmd_list);
-               spin_unlock_irqrestore(&xhci->lock, flags);
-               xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
+       ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
+       valid_add_flags = ctrl_ctx->add_flags >> 2;
+       valid_drop_flags = ctrl_ctx->drop_flags >> 2;
+
+       return hweight32(valid_drop_flags) -
+               hweight32(valid_add_flags & valid_drop_flags);
+}
+
+/*
+ * We need to reserve the new number of endpoints before the configure endpoint
+ * command completes.  We can't subtract the dropped endpoints from the number
+ * of active endpoints until the command completes because we can oversubscribe
+ * the host in this case:
+ *
+ *  - the first configure endpoint command drops more endpoints than it adds
+ *  - a second configure endpoint command that adds more endpoints is queued
+ *  - the first configure endpoint command fails, so the config is unchanged
+ *  - the second command may succeed, even though there isn't enough resources
+ *
+ * Must be called with xhci->lock held.
+ */
+static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx)
+{
+       u32 added_eps;
+
+       added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
+       if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
+               xhci_dbg(xhci, "Not enough ep ctxs: "
+                               "%u active, need to add %u, limit is %u.\n",
+                               xhci->num_active_eps, added_eps,
+                               xhci->limit_active_eps);
                return -ENOMEM;
        }
-       xhci_ring_cmd_db(xhci);
-       spin_unlock_irqrestore(&xhci->lock, flags);
+       xhci->num_active_eps += added_eps;
+       xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
+                       xhci->num_active_eps);
+       return 0;
+}
 
-       /* Wait for the configure endpoint command to complete */
-       timeleft = wait_for_completion_interruptible_timeout(
-                       cmd_completion,
-                       USB_CTRL_SET_TIMEOUT);
-       if (timeleft <= 0) {
-               xhci_warn(xhci, "%s while waiting for %s command\n",
-                               timeleft == 0 ? "Timeout" : "Signal",
-                               ctx_change == 0 ?
-                                       "configure endpoint" :
-                                       "evaluate context");
-               /* FIXME cancel the configure endpoint command */
-               return -ETIME;
-       }
+/*
+ * The configure endpoint was failed by the xHC for some other reason, so we
+ * need to revert the resources that failed configuration would have used.
+ *
+ * Must be called with xhci->lock held.
+ */
+static void xhci_free_host_resources(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx)
+{
+       u32 num_failed_eps;
 
-       if (!ctx_change)
-               return xhci_configure_endpoint_result(xhci, udev, cmd_status);
-       return xhci_evaluate_context_result(xhci, udev, cmd_status);
+       num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
+       xhci->num_active_eps -= num_failed_eps;
+       xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
+                       num_failed_eps,
+                       xhci->num_active_eps);
 }
 
-/* Called after one or more calls to xhci_add_endpoint() or
- * xhci_drop_endpoint().  If this call fails, the USB core is expected
- * to call xhci_reset_bandwidth().
+/*
+ * Now that the command has completed, clean up the active endpoint count by
+ * subtracting out the endpoints that were dropped (but not changed).
  *
- * Since we are in the middle of changing either configuration or
- * installing a new alt setting, the USB core won't allow URBs to be
- * enqueued for any endpoint on the old config or interface.  Nothing
- * else should be touching the xhci->devs[slot_id] structure, so we
- * don't need to take the xhci->lock for manipulating that.
+ * Must be called with xhci->lock held.
  */
-int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
+static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx)
+{
+       u32 num_dropped_eps;
+
+       num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
+       xhci->num_active_eps -= num_dropped_eps;
+       if (num_dropped_eps)
+               xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
+                               num_dropped_eps,
+                               xhci->num_active_eps);
+}
+
+unsigned int xhci_get_block_size(struct usb_device *udev)
+{
+       switch (udev->speed) {
+       case USB_SPEED_LOW:
+       case USB_SPEED_FULL:
+               return FS_BLOCK;
+       case USB_SPEED_HIGH:
+               return HS_BLOCK;
+       case USB_SPEED_SUPER:
+               return SS_BLOCK;
+       case USB_SPEED_UNKNOWN:
+       case USB_SPEED_WIRELESS:
+       default:
+               /* Should never happen */
+               return 1;
+       }
+}
+
+unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
+{
+       if (interval_bw->overhead[LS_OVERHEAD_TYPE])
+               return LS_OVERHEAD;
+       if (interval_bw->overhead[FS_OVERHEAD_TYPE])
+               return FS_OVERHEAD;
+       return HS_OVERHEAD;
+}
+
+/* If we are changing a LS/FS device under a HS hub,
+ * make sure (if we are activating a new TT) that the HS bus has enough
+ * bandwidth for this new TT.
+ */
+static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev,
+               int old_active_eps)
+{
+       struct xhci_interval_bw_table *bw_table;
+       struct xhci_tt_bw_info *tt_info;
+
+       /* Find the bandwidth table for the root port this TT is attached to. */
+       bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
+       tt_info = virt_dev->tt_info;
+       /* If this TT already had active endpoints, the bandwidth for this TT
+        * has already been added.  Removing all periodic endpoints (and thus
+        * making the TT enactive) will only decrease the bandwidth used.
+        */
+       if (old_active_eps)
+               return 0;
+       if (old_active_eps == 0 && tt_info->active_eps != 0) {
+               if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
+                       return -ENOMEM;
+               return 0;
+       }
+       /* Not sure why we would have no new active endpoints...
+        *
+        * Maybe because of an Evaluate Context change for a hub update or a
+        * control endpoint 0 max packet size change?
+        * FIXME: skip the bandwidth calculation in that case.
+        */
+       return 0;
+}
+
+static int xhci_check_ss_bw(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev)
+{
+       unsigned int bw_reserved;
+
+       bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
+       if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
+               return -ENOMEM;
+
+       bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
+       if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
+               return -ENOMEM;
+
+       return 0;
+}
+
+/*
+ * This algorithm is a very conservative estimate of the worst-case scheduling
+ * scenario for any one interval.  The hardware dynamically schedules the
+ * packets, so we can't tell which microframe could be the limiting factor in
+ * the bandwidth scheduling.  This only takes into account periodic endpoints.
+ *
+ * Obviously, we can't solve an NP complete problem to find the minimum worst
+ * case scenario.  Instead, we come up with an estimate that is no less than
+ * the worst case bandwidth used for any one microframe, but may be an
+ * over-estimate.
+ *
+ * We walk the requirements for each endpoint by interval, starting with the
+ * smallest interval, and place packets in the schedule where there is only one
+ * possible way to schedule packets for that interval.  In order to simplify
+ * this algorithm, we record the largest max packet size for each interval, and
+ * assume all packets will be that size.
+ *
+ * For interval 0, we obviously must schedule all packets for each interval.
+ * The bandwidth for interval 0 is just the amount of data to be transmitted
+ * (the sum of all max ESIT payload sizes, plus any overhead per packet times
+ * the number of packets).
+ *
+ * For interval 1, we have two possible microframes to schedule those packets
+ * in.  For this algorithm, if we can schedule the same number of packets for
+ * each possible scheduling opportunity (each microframe), we will do so.  The
+ * remaining number of packets will be saved to be transmitted in the gaps in
+ * the next interval's scheduling sequence.
+ *
+ * As we move those remaining packets to be scheduled with interval 2 packets,
+ * we have to double the number of remaining packets to transmit.  This is
+ * because the intervals are actually powers of 2, and we would be transmitting
+ * the previous interval's packets twice in this interval.  We also have to be
+ * sure that when we look at the largest max packet size for this interval, we
+ * also look at the largest max packet size for the remaining packets and take
+ * the greater of the two.
+ *
+ * The algorithm continues to evenly distribute packets in each scheduling
+ * opportunity, and push the remaining packets out, until we get to the last
+ * interval.  Then those packets and their associated overhead are just added
+ * to the bandwidth used.
+ */
+static int xhci_check_bw_table(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev,
+               int old_active_eps)
+{
+       unsigned int bw_reserved;
+       unsigned int max_bandwidth;
+       unsigned int bw_used;
+       unsigned int block_size;
+       struct xhci_interval_bw_table *bw_table;
+       unsigned int packet_size = 0;
+       unsigned int overhead = 0;
+       unsigned int packets_transmitted = 0;
+       unsigned int packets_remaining = 0;
+       unsigned int i;
+
+       if (virt_dev->udev->speed == USB_SPEED_SUPER)
+               return xhci_check_ss_bw(xhci, virt_dev);
+
+       if (virt_dev->udev->speed == USB_SPEED_HIGH) {
+               max_bandwidth = HS_BW_LIMIT;
+               /* Convert percent of bus BW reserved to blocks reserved */
+               bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
+       } else {
+               max_bandwidth = FS_BW_LIMIT;
+               bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
+       }
+
+       bw_table = virt_dev->bw_table;
+       /* We need to translate the max packet size and max ESIT payloads into
+        * the units the hardware uses.
+        */
+       block_size = xhci_get_block_size(virt_dev->udev);
+
+       /* If we are manipulating a LS/FS device under a HS hub, double check
+        * that the HS bus has enough bandwidth if we are activing a new TT.
+        */
+       if (virt_dev->tt_info) {
+               xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
+                               virt_dev->real_port);
+               if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
+                       xhci_warn(xhci, "Not enough bandwidth on HS bus for "
+                                       "newly activated TT.\n");
+                       return -ENOMEM;
+               }
+               xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
+                               virt_dev->tt_info->slot_id,
+                               virt_dev->tt_info->ttport);
+       } else {
+               xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
+                               virt_dev->real_port);
+       }
+
+       /* Add in how much bandwidth will be used for interval zero, or the
+        * rounded max ESIT payload + number of packets * largest overhead.
+        */
+       bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
+               bw_table->interval_bw[0].num_packets *
+               xhci_get_largest_overhead(&bw_table->interval_bw[0]);
+
+       for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
+               unsigned int bw_added;
+               unsigned int largest_mps;
+               unsigned int interval_overhead;
+
+               /*
+                * How many packets could we transmit in this interval?
+                * If packets didn't fit in the previous interval, we will need
+                * to transmit that many packets twice within this interval.
+                */
+               packets_remaining = 2 * packets_remaining +
+                       bw_table->interval_bw[i].num_packets;
+
+               /* Find the largest max packet size of this or the previous
+                * interval.
+                */
+               if (list_empty(&bw_table->interval_bw[i].endpoints))
+                       largest_mps = 0;
+               else {
+                       struct xhci_virt_ep *virt_ep;
+                       struct list_head *ep_entry;
+
+                       ep_entry = bw_table->interval_bw[i].endpoints.next;
+                       virt_ep = list_entry(ep_entry,
+                                       struct xhci_virt_ep, bw_endpoint_list);
+                       /* Convert to blocks, rounding up */
+                       largest_mps = DIV_ROUND_UP(
+                                       virt_ep->bw_info.max_packet_size,
+                                       block_size);
+               }
+               if (largest_mps > packet_size)
+                       packet_size = largest_mps;
+
+               /* Use the larger overhead of this or the previous interval. */
+               interval_overhead = xhci_get_largest_overhead(
+                               &bw_table->interval_bw[i]);
+               if (interval_overhead > overhead)
+                       overhead = interval_overhead;
+
+               /* How many packets can we evenly distribute across
+                * (1 << (i + 1)) possible scheduling opportunities?
+                */
+               packets_transmitted = packets_remaining >> (i + 1);
+
+               /* Add in the bandwidth used for those scheduled packets */
+               bw_added = packets_transmitted * (overhead + packet_size);
+
+               /* How many packets do we have remaining to transmit? */
+               packets_remaining = packets_remaining % (1 << (i + 1));
+
+               /* What largest max packet size should those packets have? */
+               /* If we've transmitted all packets, don't carry over the
+                * largest packet size.
+                */
+               if (packets_remaining == 0) {
+                       packet_size = 0;
+                       overhead = 0;
+               } else if (packets_transmitted > 0) {
+                       /* Otherwise if we do have remaining packets, and we've
+                        * scheduled some packets in this interval, take the
+                        * largest max packet size from endpoints with this
+                        * interval.
+                        */
+                       packet_size = largest_mps;
+                       overhead = interval_overhead;
+               }
+               /* Otherwise carry over packet_size and overhead from the last
+                * time we had a remainder.
+                */
+               bw_used += bw_added;
+               if (bw_used > max_bandwidth) {
+                       xhci_warn(xhci, "Not enough bandwidth. "
+                                       "Proposed: %u, Max: %u\n",
+                               bw_used, max_bandwidth);
+                       return -ENOMEM;
+               }
+       }
+       /*
+        * Ok, we know we have some packets left over after even-handedly
+        * scheduling interval 15.  We don't know which microframes they will
+        * fit into, so we over-schedule and say they will be scheduled every
+        * microframe.
+        */
+       if (packets_remaining > 0)
+               bw_used += overhead + packet_size;
+
+       if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
+               unsigned int port_index = virt_dev->real_port - 1;
+
+               /* OK, we're manipulating a HS device attached to a
+                * root port bandwidth domain.  Include the number of active TTs
+                * in the bandwidth used.
+                */
+               bw_used += TT_HS_OVERHEAD *
+                       xhci->rh_bw[port_index].num_active_tts;
+       }
+
+       xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
+               "Available: %u " "percent\n",
+               bw_used, max_bandwidth, bw_reserved,
+               (max_bandwidth - bw_used - bw_reserved) * 100 /
+               max_bandwidth);
+
+       bw_used += bw_reserved;
+       if (bw_used > max_bandwidth) {
+               xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
+                               bw_used, max_bandwidth);
+               return -ENOMEM;
+       }
+
+       bw_table->bw_used = bw_used;
+       return 0;
+}
+
+static bool xhci_is_async_ep(unsigned int ep_type)
+{
+       return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
+                                       ep_type != ISOC_IN_EP &&
+                                       ep_type != INT_IN_EP);
+}
+
+static bool xhci_is_sync_in_ep(unsigned int ep_type)
+{
+       return (ep_type == ISOC_IN_EP || ep_type != INT_IN_EP);
+}
+
+static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
+{
+       unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
+
+       if (ep_bw->ep_interval == 0)
+               return SS_OVERHEAD_BURST +
+                       (ep_bw->mult * ep_bw->num_packets *
+                                       (SS_OVERHEAD + mps));
+       return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
+                               (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
+                               1 << ep_bw->ep_interval);
+
+}
+
+void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
+               struct xhci_bw_info *ep_bw,
+               struct xhci_interval_bw_table *bw_table,
+               struct usb_device *udev,
+               struct xhci_virt_ep *virt_ep,
+               struct xhci_tt_bw_info *tt_info)
+{
+       struct xhci_interval_bw *interval_bw;
+       int normalized_interval;
+
+       if (xhci_is_async_ep(ep_bw->type))
+               return;
+
+       if (udev->speed == USB_SPEED_SUPER) {
+               if (xhci_is_sync_in_ep(ep_bw->type))
+                       xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
+                               xhci_get_ss_bw_consumed(ep_bw);
+               else
+                       xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
+                               xhci_get_ss_bw_consumed(ep_bw);
+               return;
+       }
+
+       /* SuperSpeed endpoints never get added to intervals in the table, so
+        * this check is only valid for HS/FS/LS devices.
+        */
+       if (list_empty(&virt_ep->bw_endpoint_list))
+               return;
+       /* For LS/FS devices, we need to translate the interval expressed in
+        * microframes to frames.
+        */
+       if (udev->speed == USB_SPEED_HIGH)
+               normalized_interval = ep_bw->ep_interval;
+       else
+               normalized_interval = ep_bw->ep_interval - 3;
+
+       if (normalized_interval == 0)
+               bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
+       interval_bw = &bw_table->interval_bw[normalized_interval];
+       interval_bw->num_packets -= ep_bw->num_packets;
+       switch (udev->speed) {
+       case USB_SPEED_LOW:
+               interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
+               break;
+       case USB_SPEED_FULL:
+               interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
+               break;
+       case USB_SPEED_HIGH:
+               interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
+               break;
+       case USB_SPEED_SUPER:
+       case USB_SPEED_UNKNOWN:
+       case USB_SPEED_WIRELESS:
+               /* Should never happen because only LS/FS/HS endpoints will get
+                * added to the endpoint list.
+                */
+               return;
+       }
+       if (tt_info)
+               tt_info->active_eps -= 1;
+       list_del_init(&virt_ep->bw_endpoint_list);
+}
+
+static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
+               struct xhci_bw_info *ep_bw,
+               struct xhci_interval_bw_table *bw_table,
+               struct usb_device *udev,
+               struct xhci_virt_ep *virt_ep,
+               struct xhci_tt_bw_info *tt_info)
+{
+       struct xhci_interval_bw *interval_bw;
+       struct xhci_virt_ep *smaller_ep;
+       int normalized_interval;
+
+       if (xhci_is_async_ep(ep_bw->type))
+               return;
+
+       if (udev->speed == USB_SPEED_SUPER) {
+               if (xhci_is_sync_in_ep(ep_bw->type))
+                       xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
+                               xhci_get_ss_bw_consumed(ep_bw);
+               else
+                       xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
+                               xhci_get_ss_bw_consumed(ep_bw);
+               return;
+       }
+
+       /* For LS/FS devices, we need to translate the interval expressed in
+        * microframes to frames.
+        */
+       if (udev->speed == USB_SPEED_HIGH)
+               normalized_interval = ep_bw->ep_interval;
+       else
+               normalized_interval = ep_bw->ep_interval - 3;
+
+       if (normalized_interval == 0)
+               bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
+       interval_bw = &bw_table->interval_bw[normalized_interval];
+       interval_bw->num_packets += ep_bw->num_packets;
+       switch (udev->speed) {
+       case USB_SPEED_LOW:
+               interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
+               break;
+       case USB_SPEED_FULL:
+               interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
+               break;
+       case USB_SPEED_HIGH:
+               interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
+               break;
+       case USB_SPEED_SUPER:
+       case USB_SPEED_UNKNOWN:
+       case USB_SPEED_WIRELESS:
+               /* Should never happen because only LS/FS/HS endpoints will get
+                * added to the endpoint list.
+                */
+               return;
+       }
+
+       if (tt_info)
+               tt_info->active_eps += 1;
+       /* Insert the endpoint into the list, largest max packet size first. */
+       list_for_each_entry(smaller_ep, &interval_bw->endpoints,
+                       bw_endpoint_list) {
+               if (ep_bw->max_packet_size >=
+                               smaller_ep->bw_info.max_packet_size) {
+                       /* Add the new ep before the smaller endpoint */
+                       list_add_tail(&virt_ep->bw_endpoint_list,
+                                       &smaller_ep->bw_endpoint_list);
+                       return;
+               }
+       }
+       /* Add the new endpoint at the end of the list. */
+       list_add_tail(&virt_ep->bw_endpoint_list,
+                       &interval_bw->endpoints);
+}
+
+void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev,
+               int old_active_eps)
+{
+       struct xhci_root_port_bw_info *rh_bw_info;
+       if (!virt_dev->tt_info)
+               return;
+
+       rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
+       if (old_active_eps == 0 &&
+                               virt_dev->tt_info->active_eps != 0) {
+               rh_bw_info->num_active_tts += 1;
+               rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
+       } else if (old_active_eps != 0 &&
+                               virt_dev->tt_info->active_eps == 0) {
+               rh_bw_info->num_active_tts -= 1;
+               rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
+       }
+}
+
+static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev,
+               struct xhci_container_ctx *in_ctx)
+{
+       struct xhci_bw_info ep_bw_info[31];
+       int i;
+       struct xhci_input_control_ctx *ctrl_ctx;
+       int old_active_eps = 0;
+
+       if (virt_dev->tt_info)
+               old_active_eps = virt_dev->tt_info->active_eps;
+
+       ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
+
+       for (i = 0; i < 31; i++) {
+               if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
+                       continue;
+
+               /* Make a copy of the BW info in case we need to revert this */
+               memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
+                               sizeof(ep_bw_info[i]));
+               /* Drop the endpoint from the interval table if the endpoint is
+                * being dropped or changed.
+                */
+               if (EP_IS_DROPPED(ctrl_ctx, i))
+                       xhci_drop_ep_from_interval_table(xhci,
+                                       &virt_dev->eps[i].bw_info,
+                                       virt_dev->bw_table,
+                                       virt_dev->udev,
+                                       &virt_dev->eps[i],
+                                       virt_dev->tt_info);
+       }
+       /* Overwrite the information stored in the endpoints' bw_info */
+       xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
+       for (i = 0; i < 31; i++) {
+               /* Add any changed or added endpoints to the interval table */
+               if (EP_IS_ADDED(ctrl_ctx, i))
+                       xhci_add_ep_to_interval_table(xhci,
+                                       &virt_dev->eps[i].bw_info,
+                                       virt_dev->bw_table,
+                                       virt_dev->udev,
+                                       &virt_dev->eps[i],
+                                       virt_dev->tt_info);
+       }
+
+       if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
+               /* Ok, this fits in the bandwidth we have.
+                * Update the number of active TTs.
+                */
+               xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
+               return 0;
+       }
+
+       /* We don't have enough bandwidth for this, revert the stored info. */
+       for (i = 0; i < 31; i++) {
+               if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
+                       continue;
+
+               /* Drop the new copies of any added or changed endpoints from
+                * the interval table.
+                */
+               if (EP_IS_ADDED(ctrl_ctx, i)) {
+                       xhci_drop_ep_from_interval_table(xhci,
+                                       &virt_dev->eps[i].bw_info,
+                                       virt_dev->bw_table,
+                                       virt_dev->udev,
+                                       &virt_dev->eps[i],
+                                       virt_dev->tt_info);
+               }
+               /* Revert the endpoint back to its old information */
+               memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
+                               sizeof(ep_bw_info[i]));
+               /* Add any changed or dropped endpoints back into the table */
+               if (EP_IS_DROPPED(ctrl_ctx, i))
+                       xhci_add_ep_to_interval_table(xhci,
+                                       &virt_dev->eps[i].bw_info,
+                                       virt_dev->bw_table,
+                                       virt_dev->udev,
+                                       &virt_dev->eps[i],
+                                       virt_dev->tt_info);
+       }
+       return -ENOMEM;
+}
+
+
+/* Issue a configure endpoint command or evaluate context command
+ * and wait for it to finish.
+ */
+static int xhci_configure_endpoint(struct xhci_hcd *xhci,
+               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;
+       u32 *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;
+       else
+               in_ctx = virt_dev->in_ctx;
+
+       if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
+                       xhci_reserve_host_resources(xhci, in_ctx)) {
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               xhci_warn(xhci, "Not enough host resources, "
+                               "active endpoint contexts = %u\n",
+                               xhci->num_active_eps);
+               return -ENOMEM;
+       }
+       if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
+                       xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
+               if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
+                       xhci_free_host_resources(xhci, in_ctx);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               xhci_warn(xhci, "Not enough bandwidth\n");
+               return -ENOMEM;
+       }
+
+       if (command) {
+               cmd_completion = command->completion;
+               cmd_status = &command->status;
+               command->command_trb = xhci->cmd_ring->enqueue;
+
+               /* Enqueue pointer can be left pointing to the link TRB,
+                * we must handle that
+                */
+               if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
+                       command->command_trb =
+                               xhci->cmd_ring->enq_seg->next->trbs;
+
+               list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
+       } else {
+               cmd_completion = &virt_dev->cmd_completion;
+               cmd_status = &virt_dev->cmd_status;
+       }
+       init_completion(cmd_completion);
+
+       if (!ctx_change)
+               ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
+                               udev->slot_id, must_succeed);
+       else
+               ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
+                               udev->slot_id);
+       if (ret < 0) {
+               if (command)
+                       list_del(&command->cmd_list);
+               if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
+                       xhci_free_host_resources(xhci, in_ctx);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
+               return -ENOMEM;
+       }
+       xhci_ring_cmd_db(xhci);
+       spin_unlock_irqrestore(&xhci->lock, flags);
+
+       /* Wait for the configure endpoint command to complete */
+       timeleft = wait_for_completion_interruptible_timeout(
+                       cmd_completion,
+                       USB_CTRL_SET_TIMEOUT);
+       if (timeleft <= 0) {
+               xhci_warn(xhci, "%s while waiting for %s command\n",
+                               timeleft == 0 ? "Timeout" : "Signal",
+                               ctx_change == 0 ?
+                                       "configure endpoint" :
+                                       "evaluate context");
+               /* FIXME cancel the configure endpoint command */
+               return -ETIME;
+       }
+
+       if (!ctx_change)
+               ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
+       else
+               ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
+
+       if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
+               spin_lock_irqsave(&xhci->lock, flags);
+               /* If the command failed, remove the reserved resources.
+                * Otherwise, clean up the estimate to include dropped eps.
+                */
+               if (ret)
+                       xhci_free_host_resources(xhci, in_ctx);
+               else
+                       xhci_finish_resource_reservation(xhci, in_ctx);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+       }
+       return ret;
+}
+
+/* Called after one or more calls to xhci_add_endpoint() or
+ * xhci_drop_endpoint().  If this call fails, the USB core is expected
+ * to call xhci_reset_bandwidth().
+ *
+ * Since we are in the middle of changing either configuration or
+ * installing a new alt setting, the USB core won't allow URBs to be
+ * enqueued for any endpoint on the old config or interface.  Nothing
+ * else should be touching the xhci->devs[slot_id] structure, so we
+ * don't need to take the xhci->lock for manipulating that.
+ */
+int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
 {
        int i;
        int ret = 0;
@@ -1655,20 +2500,27 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
        if (ret <= 0)
                return ret;
        xhci = hcd_to_xhci(hcd);
+       if (xhci->xhc_state & XHCI_STATE_DYING)
+               return -ENODEV;
 
        xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
        virt_dev = xhci->devs[udev->slot_id];
 
        /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
        ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
-       ctrl_ctx->add_flags |= SLOT_FLAG;
-       ctrl_ctx->add_flags &= ~EP0_FLAG;
-       ctrl_ctx->drop_flags &= ~SLOT_FLAG;
-       ctrl_ctx->drop_flags &= ~EP0_FLAG;
+       ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
+       ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
+       ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
+
+       /* Don't issue the command if there's no endpoints to update. */
+       if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
+                       ctrl_ctx->drop_flags == 0)
+               return 0;
+
        xhci_dbg(xhci, "New Input Control Context:\n");
        slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
        xhci_dbg_ctx(xhci, virt_dev->in_ctx,
-                       LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
+                    LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
 
        ret = xhci_configure_endpoint(xhci, udev, NULL,
                        false, false);
@@ -1679,10 +2531,19 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
 
        xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
        xhci_dbg_ctx(xhci, virt_dev->out_ctx,
-                       LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
+                    LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
 
+       /* Free any rings that were dropped, but not changed. */
+       for (i = 1; i < 31; ++i) {
+               if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
+                   !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
+                       xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
+       }
        xhci_zero_in_ctx(xhci, virt_dev);
-       /* Install new rings and free or cache any old rings */
+       /*
+        * Install any rings for completely new endpoints or changed endpoints,
+        * and free or cache any old rings from changed endpoints.
+        */
        for (i = 1; i < 31; ++i) {
                if (!virt_dev->eps[i].new_ring)
                        continue;
@@ -1729,16 +2590,16 @@ static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
 {
        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;
+       ctrl_ctx->add_flags = cpu_to_le32(add_flags);
+       ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
        xhci_slot_copy(xhci, in_ctx, out_ctx);
-       ctrl_ctx->add_flags |= SLOT_FLAG;
+       ctrl_ctx->add_flags |= cpu_to_le32(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,
+static 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)
 {
@@ -1761,7 +2622,7 @@ void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
                                deq_state->new_deq_ptr);
                return;
        }
-       ep_ctx->deq = addr | deq_state->new_cycle_state;
+       ep_ctx->deq = cpu_to_le64(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,
@@ -1942,8 +2803,7 @@ static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
                if (ret < 0)
                        return ret;
 
-               max_streams = USB_SS_MAX_STREAMS(
-                               eps[i]->ss_ep_comp.bmAttributes);
+               max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
                if (max_streams < (*num_streams - 1)) {
                        xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
                                        eps[i]->desc.bEndpointAddress,
@@ -2066,7 +2926,7 @@ int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
                return -EINVAL;
        }
        vdev = xhci->devs[udev->slot_id];
-       /* Mark each endpoint as being in transistion, so
+       /* Mark each endpoint as being in transition, so
         * xhci_urb_enqueue() will reject all URBs.
         */
        for (i = 0; i < num_eps; i++) {
@@ -2237,6 +3097,34 @@ int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
 }
 
 /*
+ * Deletes endpoint resources for endpoints that were active before a Reset
+ * Device command, or a Disable Slot command.  The Reset Device command leaves
+ * the control endpoint intact, whereas the Disable Slot command deletes it.
+ *
+ * Must be called with xhci->lock held.
+ */
+void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
+       struct xhci_virt_device *virt_dev, bool drop_control_ep)
+{
+       int i;
+       unsigned int num_dropped_eps = 0;
+       unsigned int drop_flags = 0;
+
+       for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
+               if (virt_dev->eps[i].ring) {
+                       drop_flags |= 1 << i;
+                       num_dropped_eps++;
+               }
+       }
+       xhci->num_active_eps -= num_dropped_eps;
+       if (num_dropped_eps)
+               xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
+                               "%u now active.\n",
+                               num_dropped_eps, drop_flags,
+                               xhci->num_active_eps);
+}
+
+/*
  * This submits a Reset Device Command, which will set the device state to 0,
  * set the device address to 0, and disable all the endpoints except the default
  * control endpoint.  The USB core should come back and call
@@ -2264,6 +3152,8 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
        struct xhci_command *reset_device_cmd;
        int timeleft;
        int last_freed_endpoint;
+       struct xhci_slot_ctx *slot_ctx;
+       int old_active_eps = 0;
 
        ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
        if (ret <= 0)
@@ -2296,6 +3186,12 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
                        return -EINVAL;
        }
 
+       /* If device is not setup, there is no point in resetting it */
+       slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
+       if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
+                                               SLOT_STATE_DISABLED)
+               return 0;
+
        xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
        /* Allocate the command structure that holds the struct completion.
         * Assume we're in process context, since the normal device reset
@@ -2316,8 +3212,7 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
        /* Enqueue pointer can be left pointing to the link TRB,
         * we must handle that
         */
-       if ((reset_device_cmd->command_trb->link.control & TRB_TYPE_BITMASK)
-                       == TRB_TYPE(TRB_LINK))
+       if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
                reset_device_cmd->command_trb =
                        xhci->cmd_ring->enq_seg->next->trbs;
 
@@ -2377,14 +3272,41 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
                goto command_cleanup;
        }
 
+       /* Free up host controller endpoint resources */
+       if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
+               spin_lock_irqsave(&xhci->lock, flags);
+               /* Don't delete the default control endpoint resources */
+               xhci_free_device_endpoint_resources(xhci, virt_dev, false);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+       }
+
        /* Everything but endpoint 0 is disabled, so free or cache the rings. */
        last_freed_endpoint = 1;
        for (i = 1; i < 31; ++i) {
-               if (!virt_dev->eps[i].ring)
-                       continue;
-               xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
-               last_freed_endpoint = i;
-       }
+               struct xhci_virt_ep *ep = &virt_dev->eps[i];
+
+               if (ep->ep_state & EP_HAS_STREAMS) {
+                       xhci_free_stream_info(xhci, ep->stream_info);
+                       ep->stream_info = NULL;
+                       ep->ep_state &= ~EP_HAS_STREAMS;
+               }
+
+               if (ep->ring) {
+                       xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
+                       last_freed_endpoint = i;
+               }
+               if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
+                       xhci_drop_ep_from_interval_table(xhci,
+                                       &virt_dev->eps[i].bw_info,
+                                       virt_dev->bw_table,
+                                       udev,
+                                       &virt_dev->eps[i],
+                                       virt_dev->tt_info);
+               xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
+       }
+       /* If necessary, update the number of active TTs on this root port */
+       xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
+
        xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
        xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
        ret = 0;
@@ -2408,7 +3330,10 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
        int i, ret;
 
        ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
-       if (ret <= 0)
+       /* If the host is halted due to driver unload, we still need to free the
+        * device.
+        */
+       if (ret <= 0 && ret != -ENODEV)
                return;
 
        virt_dev = xhci->devs[udev->slot_id];
@@ -2419,10 +3344,16 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
                del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
        }
 
+       if (udev->usb2_hw_lpm_enabled) {
+               xhci_set_usb2_hardware_lpm(hcd, udev, 0);
+               udev->usb2_hw_lpm_enabled = 0;
+       }
+
        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)) {
+       if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
+                       (xhci->xhc_state & XHCI_STATE_HALTED)) {
                xhci_free_virt_device(xhci, udev->slot_id);
                spin_unlock_irqrestore(&xhci->lock, flags);
                return;
@@ -2442,6 +3373,27 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
 }
 
 /*
+ * Checks if we have enough host controller resources for the default control
+ * endpoint.
+ *
+ * Must be called with xhci->lock held.
+ */
+static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
+{
+       if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
+               xhci_dbg(xhci, "Not enough ep ctxs: "
+                               "%u active, need to add 1, limit is %u.\n",
+                               xhci->num_active_eps, xhci->limit_active_eps);
+               return -ENOMEM;
+       }
+       xhci->num_active_eps += 1;
+       xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
+                       xhci->num_active_eps);
+       return 0;
+}
+
+
+/*
  * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  * timed out, or allocating memory failed.  Returns 1 on success.
  */
@@ -2476,24 +3428,39 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
                xhci_err(xhci, "Error while assigning device slot ID\n");
                return 0;
        }
-       /* xhci_alloc_virt_device() does not touch rings; no need to lock.
-        * Use GFP_NOIO, since this function can be called from
+
+       if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
+               spin_lock_irqsave(&xhci->lock, flags);
+               ret = xhci_reserve_host_control_ep_resources(xhci);
+               if (ret) {
+                       spin_unlock_irqrestore(&xhci->lock, flags);
+                       xhci_warn(xhci, "Not enough host resources, "
+                                       "active endpoint contexts = %u\n",
+                                       xhci->num_active_eps);
+                       goto disable_slot;
+               }
+               spin_unlock_irqrestore(&xhci->lock, flags);
+       }
+       /* Use GFP_NOIO, since this function can be called from
         * xhci_discover_or_reset_device(), which may be called as part of
         * mass storage driver error handling.
         */
        if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
-               /* Disable slot, if we can do it without mem alloc */
                xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
-               spin_lock_irqsave(&xhci->lock, flags);
-               if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
-                       xhci_ring_cmd_db(xhci);
-               spin_unlock_irqrestore(&xhci->lock, flags);
-               return 0;
+               goto disable_slot;
        }
        udev->slot_id = xhci->slot_id;
        /* Is this a LS or FS device under a HS hub? */
        /* Hub or peripherial? */
        return 1;
+
+disable_slot:
+       /* Disable slot, if we can do it without mem alloc */
+       spin_lock_irqsave(&xhci->lock, flags);
+       if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
+               xhci_ring_cmd_db(xhci);
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       return 0;
 }
 
 /*
@@ -2523,6 +3490,17 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 
        virt_dev = xhci->devs[udev->slot_id];
 
+       if (WARN_ON(!virt_dev)) {
+               /*
+                * In plug/unplug torture test with an NEC controller,
+                * a zero-dereference was observed once due to virt_dev = 0.
+                * Print useful debug rather than crash if it is observed again!
+                */
+               xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
+                       udev->slot_id);
+               return -EINVAL;
+       }
+
        slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
        /*
         * If this is the first Set Address since device plug-in or
@@ -2534,6 +3512,10 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
        /* Otherwise, update the control endpoint ring enqueue pointer. */
        else
                xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
+       ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
+       ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
+       ctrl_ctx->drop_flags = 0;
+
        xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
        xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
 
@@ -2556,7 +3538,7 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
         * command on a timeout.
         */
        if (timeleft <= 0) {
-               xhci_warn(xhci, "%s while waiting for a slot\n",
+               xhci_warn(xhci, "%s while waiting for address device command\n",
                                timeleft == 0 ? "Timeout" : "Signal");
                /* FIXME cancel the address device command */
                return -ETIME;
@@ -2573,6 +3555,11 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
                dev_warn(&udev->dev, "Device not responding to set address.\n");
                ret = -EPROTO;
                break;
+       case COMP_DEV_ERR:
+               dev_warn(&udev->dev, "ERROR: Incompatible device for address "
+                               "device command.\n");
+               ret = -ENODEV;
+               break;
        case COMP_SUCCESS:
                xhci_dbg(xhci, "Successful Address Device command\n");
                break;
@@ -2590,10 +3577,10 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
        temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
        xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
        xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
-                       udev->slot_id,
-                       &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
-                       (unsigned long long)
-                               xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
+                udev->slot_id,
+                &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
+                (unsigned long long)
+                le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
        xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
                        (unsigned long long)virt_dev->out_ctx->dma);
        xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
@@ -2607,9 +3594,9 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
        slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
        /* Use kernel assigned address for devices; store xHC assigned
         * address locally. */
-       virt_dev->address = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
+       virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
+               + 1;
        /* Zero the input context control for later use */
-       ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
        ctrl_ctx->add_flags = 0;
        ctrl_ctx->drop_flags = 0;
 
@@ -2618,6 +3605,254 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
        return 0;
 }
 
+#ifdef CONFIG_USB_SUSPEND
+
+/* BESL to HIRD Encoding array for USB2 LPM */
+static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
+       3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
+
+/* Calculate HIRD/BESL for USB2 PORTPMSC*/
+static int xhci_calculate_hird_besl(int u2del, bool use_besl)
+{
+       int hird;
+
+       if (use_besl) {
+               for (hird = 0; hird < 16; hird++) {
+                       if (xhci_besl_encoding[hird] >= u2del)
+                               break;
+               }
+       } else {
+               if (u2del <= 50)
+                       hird = 0;
+               else
+                       hird = (u2del - 51) / 75 + 1;
+
+               if (hird > 15)
+                       hird = 15;
+       }
+
+       return hird;
+}
+
+static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
+                                       struct usb_device *udev)
+{
+       struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       struct dev_info *dev_info;
+       __le32 __iomem  **port_array;
+       __le32 __iomem  *addr, *pm_addr;
+       u32             temp, dev_id;
+       unsigned int    port_num;
+       unsigned long   flags;
+       int             u2del, hird;
+       int             ret;
+
+       if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
+                       !udev->lpm_capable)
+               return -EINVAL;
+
+       /* we only support lpm for non-hub device connected to root hub yet */
+       if (!udev->parent || udev->parent->parent ||
+                       udev->descriptor.bDeviceClass == USB_CLASS_HUB)
+               return -EINVAL;
+
+       spin_lock_irqsave(&xhci->lock, flags);
+
+       /* Look for devices in lpm_failed_devs list */
+       dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
+                       le16_to_cpu(udev->descriptor.idProduct);
+       list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
+               if (dev_info->dev_id == dev_id) {
+                       ret = -EINVAL;
+                       goto finish;
+               }
+       }
+
+       port_array = xhci->usb2_ports;
+       port_num = udev->portnum - 1;
+
+       if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
+               xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
+               ret = -EINVAL;
+               goto finish;
+       }
+
+       /*
+        * Test USB 2.0 software LPM.
+        * FIXME: some xHCI 1.0 hosts may implement a new register to set up
+        * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
+        * in the June 2011 errata release.
+        */
+       xhci_dbg(xhci, "test port %d software LPM\n", port_num);
+       /*
+        * Set L1 Device Slot and HIRD/BESL.
+        * Check device's USB 2.0 extension descriptor to determine whether
+        * HIRD or BESL shoule be used. See USB2.0 LPM errata.
+        */
+       pm_addr = port_array[port_num] + 1;
+       u2del = HCS_U2_LATENCY(xhci->hcs_params3);
+       if (le32_to_cpu(udev->bos->ext_cap->bmAttributes) & (1 << 2))
+               hird = xhci_calculate_hird_besl(u2del, 1);
+       else
+               hird = xhci_calculate_hird_besl(u2del, 0);
+
+       temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
+       xhci_writel(xhci, temp, pm_addr);
+
+       /* Set port link state to U2(L1) */
+       addr = port_array[port_num];
+       xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
+
+       /* wait for ACK */
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       msleep(10);
+       spin_lock_irqsave(&xhci->lock, flags);
+
+       /* Check L1 Status */
+       ret = handshake(xhci, pm_addr, PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
+       if (ret != -ETIMEDOUT) {
+               /* enter L1 successfully */
+               temp = xhci_readl(xhci, addr);
+               xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
+                               port_num, temp);
+               ret = 0;
+       } else {
+               temp = xhci_readl(xhci, pm_addr);
+               xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
+                               port_num, temp & PORT_L1S_MASK);
+               ret = -EINVAL;
+       }
+
+       /* Resume the port */
+       xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
+
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       msleep(10);
+       spin_lock_irqsave(&xhci->lock, flags);
+
+       /* Clear PLC */
+       xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
+
+       /* Check PORTSC to make sure the device is in the right state */
+       if (!ret) {
+               temp = xhci_readl(xhci, addr);
+               xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
+               if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
+                               (temp & PORT_PLS_MASK) != XDEV_U0) {
+                       xhci_dbg(xhci, "port L1 resume fail\n");
+                       ret = -EINVAL;
+               }
+       }
+
+       if (ret) {
+               /* Insert dev to lpm_failed_devs list */
+               xhci_warn(xhci, "device LPM test failed, may disconnect and "
+                               "re-enumerate\n");
+               dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
+               if (!dev_info) {
+                       ret = -ENOMEM;
+                       goto finish;
+               }
+               dev_info->dev_id = dev_id;
+               INIT_LIST_HEAD(&dev_info->list);
+               list_add(&dev_info->list, &xhci->lpm_failed_devs);
+       } else {
+               xhci_ring_device(xhci, udev->slot_id);
+       }
+
+finish:
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       return ret;
+}
+
+int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
+                       struct usb_device *udev, int enable)
+{
+       struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       __le32 __iomem  **port_array;
+       __le32 __iomem  *pm_addr;
+       u32             temp;
+       unsigned int    port_num;
+       unsigned long   flags;
+       int             u2del, hird;
+
+       if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
+                       !udev->lpm_capable)
+               return -EPERM;
+
+       if (!udev->parent || udev->parent->parent ||
+                       udev->descriptor.bDeviceClass == USB_CLASS_HUB)
+               return -EPERM;
+
+       if (udev->usb2_hw_lpm_capable != 1)
+               return -EPERM;
+
+       spin_lock_irqsave(&xhci->lock, flags);
+
+       port_array = xhci->usb2_ports;
+       port_num = udev->portnum - 1;
+       pm_addr = port_array[port_num] + 1;
+       temp = xhci_readl(xhci, pm_addr);
+
+       xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
+                       enable ? "enable" : "disable", port_num);
+
+       u2del = HCS_U2_LATENCY(xhci->hcs_params3);
+       if (le32_to_cpu(udev->bos->ext_cap->bmAttributes) & (1 << 2))
+               hird = xhci_calculate_hird_besl(u2del, 1);
+       else
+               hird = xhci_calculate_hird_besl(u2del, 0);
+
+       if (enable) {
+               temp &= ~PORT_HIRD_MASK;
+               temp |= PORT_HIRD(hird) | PORT_RWE;
+               xhci_writel(xhci, temp, pm_addr);
+               temp = xhci_readl(xhci, pm_addr);
+               temp |= PORT_HLE;
+               xhci_writel(xhci, temp, pm_addr);
+       } else {
+               temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
+               xhci_writel(xhci, temp, pm_addr);
+       }
+
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       return 0;
+}
+
+int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
+{
+       struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+       int             ret;
+
+       ret = xhci_usb2_software_lpm_test(hcd, udev);
+       if (!ret) {
+               xhci_dbg(xhci, "software LPM test succeed\n");
+               if (xhci->hw_lpm_support == 1) {
+                       udev->usb2_hw_lpm_capable = 1;
+                       ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
+                       if (!ret)
+                               udev->usb2_hw_lpm_enabled = 1;
+               }
+       }
+
+       return 0;
+}
+
+#else
+
+int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
+                               struct usb_device *udev, int enable)
+{
+       return 0;
+}
+
+int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
+{
+       return 0;
+}
+
+#endif /* CONFIG_USB_SUSPEND */
+
 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  * internal data structures for the device.
  */
@@ -2649,26 +3884,39 @@ int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
        }
 
        spin_lock_irqsave(&xhci->lock, flags);
+       if (hdev->speed == USB_SPEED_HIGH &&
+                       xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
+               xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
+               xhci_free_command(xhci, config_cmd);
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               return -ENOMEM;
+       }
+
        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;
+       ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
        slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
-       slot_ctx->dev_info |= DEV_HUB;
+       slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
        if (tt->multi)
-               slot_ctx->dev_info |= DEV_MTT;
+               slot_ctx->dev_info |= cpu_to_le32(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);
+               slot_ctx->dev_info2 |= cpu_to_le32(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.
+                *
+                * xHCI 1.0: this field shall be 0 if the device is not a
+                * High-spped hub.
                 */
                think_time = tt->think_time;
                if (think_time != 0)
                        think_time = (think_time / 666) - 1;
-               slot_ctx->tt_info |= TT_THINK_TIME(think_time);
+               if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
+                       slot_ctx->tt_info |=
+                               cpu_to_le32(TT_THINK_TIME(think_time));
        } else {
                xhci_dbg(xhci, "xHCI version %x doesn't need hub "
                                "TT think time or number of ports\n",
@@ -2707,22 +3955,108 @@ int xhci_get_frame(struct usb_hcd *hcd)
        return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
 }
 
+int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
+{
+       struct xhci_hcd         *xhci;
+       struct device           *dev = hcd->self.controller;
+       int                     retval;
+       u32                     temp;
+
+       hcd->self.sg_tablesize = TRBS_PER_SEGMENT - 2;
+
+       if (usb_hcd_is_primary_hcd(hcd)) {
+               xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
+               if (!xhci)
+                       return -ENOMEM;
+               *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
+               xhci->main_hcd = hcd;
+               /* Mark the first roothub as being USB 2.0.
+                * The xHCI driver will register the USB 3.0 roothub.
+                */
+               hcd->speed = HCD_USB2;
+               hcd->self.root_hub->speed = USB_SPEED_HIGH;
+               /*
+                * USB 2.0 roothub under xHCI has an integrated TT,
+                * (rate matching hub) as opposed to having an OHCI/UHCI
+                * companion controller.
+                */
+               hcd->has_tt = 1;
+       } else {
+               /* xHCI private pointer was set in xhci_pci_probe for the second
+                * registered roothub.
+                */
+               xhci = hcd_to_xhci(hcd);
+               temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
+               if (HCC_64BIT_ADDR(temp)) {
+                       xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
+                       dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
+               } else {
+                       dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
+               }
+               return 0;
+       }
+
+       xhci->cap_regs = hcd->regs;
+       xhci->op_regs = hcd->regs +
+               HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
+       xhci->run_regs = hcd->regs +
+               (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
+       /* Cache read-only capability registers */
+       xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
+       xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
+       xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
+       xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
+       xhci->hci_version = HC_VERSION(xhci->hcc_params);
+       xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
+       xhci_print_registers(xhci);
+
+       get_quirks(dev, xhci);
+
+       /* Make sure the HC is halted. */
+       retval = xhci_halt(xhci);
+       if (retval)
+               goto error;
+
+       xhci_dbg(xhci, "Resetting HCD\n");
+       /* Reset the internal HC memory state and registers. */
+       retval = xhci_reset(xhci);
+       if (retval)
+               goto error;
+       xhci_dbg(xhci, "Reset complete\n");
+
+       temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
+       if (HCC_64BIT_ADDR(temp)) {
+               xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
+               dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
+       } else {
+               dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
+       }
+
+       xhci_dbg(xhci, "Calling HCD init\n");
+       /* Initialize HCD and host controller data structures. */
+       retval = xhci_init(hcd);
+       if (retval)
+               goto error;
+       xhci_dbg(xhci, "Called HCD init\n");
+       return 0;
+error:
+       kfree(xhci);
+       return retval;
+}
+
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_LICENSE("GPL");
 
 static int __init xhci_hcd_init(void)
 {
-#ifdef CONFIG_PCI
-       int retval = 0;
+       int retval;
 
        retval = xhci_register_pci();
-
        if (retval < 0) {
                printk(KERN_DEBUG "Problem registering PCI driver.");
                return retval;
        }
-#endif
        /*
         * Check the compiler generated sizes of structures that must be laid
         * out in specific ways for hardware access.
@@ -2747,8 +4081,6 @@ module_init(xhci_hcd_init);
 
 static void __exit xhci_hcd_cleanup(void)
 {
-#ifdef CONFIG_PCI
        xhci_unregister_pci();
-#endif
 }
 module_exit(xhci_hcd_cleanup);