xhci: Don't write zeroed pointers to xHC registers.
[linux-flexiantxendom0.git] / drivers / usb / host / xhci-mem.c
index d299ffa..01c3800 100644 (file)
@@ -41,13 +41,13 @@ static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flag
 
        seg = kzalloc(sizeof *seg, flags);
        if (!seg)
-               return 0;
+               return NULL;
        xhci_dbg(xhci, "Allocating priv segment structure at %p\n", seg);
 
        seg->trbs = dma_pool_alloc(xhci->segment_pool, flags, &dma);
        if (!seg->trbs) {
                kfree(seg);
-               return 0;
+               return NULL;
        }
        xhci_dbg(xhci, "// Allocating segment at %p (virtual) 0x%llx (DMA)\n",
                        seg->trbs, (unsigned long long)dma);
@@ -61,8 +61,6 @@ static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flag
 
 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
 {
-       if (!seg)
-               return;
        if (seg->trbs) {
                xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n",
                                seg->trbs, (unsigned long long)seg->dma);
@@ -81,7 +79,7 @@ static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
  * related flags, such as End TRB, Toggle Cycle, and no snoop.
  */
 static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
-               struct xhci_segment *next, bool link_trbs)
+               struct xhci_segment *next, bool link_trbs, bool isoc)
 {
        u32 val;
 
@@ -89,16 +87,19 @@ static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
                return;
        prev->next = next;
        if (link_trbs) {
-               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = next->dma;
+               prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
+                       cpu_to_le64(next->dma);
 
                /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
-               val = prev->trbs[TRBS_PER_SEGMENT-1].link.control;
+               val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
                val &= ~TRB_TYPE_BITMASK;
                val |= TRB_TYPE(TRB_LINK);
                /* Always set the chain bit with 0.95 hardware */
-               if (xhci_link_trb_quirk(xhci))
+               /* Set chain bit for isoc rings on AMD 0.96 host */
+               if (xhci_link_trb_quirk(xhci) ||
+                               (isoc && (xhci->quirks & XHCI_AMD_0x96_HOST)))
                        val |= TRB_CHAIN;
-               prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
+               prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
        }
        xhci_dbg(xhci, "Linking segment 0x%llx to segment 0x%llx (DMA)\n",
                        (unsigned long long)prev->dma,
@@ -111,18 +112,20 @@ void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
        struct xhci_segment *seg;
        struct xhci_segment *first_seg;
 
-       if (!ring || !ring->first_seg)
+       if (!ring)
                return;
-       first_seg = ring->first_seg;
-       seg = first_seg->next;
-       xhci_dbg(xhci, "Freeing ring at %p\n", ring);
-       while (seg != first_seg) {
-               struct xhci_segment *next = seg->next;
-               xhci_segment_free(xhci, seg);
-               seg = next;
+       if (ring->first_seg) {
+               first_seg = ring->first_seg;
+               seg = first_seg->next;
+               xhci_dbg(xhci, "Freeing ring at %p\n", ring);
+               while (seg != first_seg) {
+                       struct xhci_segment *next = seg->next;
+                       xhci_segment_free(xhci, seg);
+                       seg = next;
+               }
+               xhci_segment_free(xhci, first_seg);
+               ring->first_seg = NULL;
        }
-       xhci_segment_free(xhci, first_seg);
-       ring->first_seg = NULL;
        kfree(ring);
 }
 
@@ -151,7 +154,7 @@ static void xhci_initialize_ring_info(struct xhci_ring *ring)
  * See section 4.9.1 and figures 15 and 16.
  */
 static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
-               unsigned int num_segs, bool link_trbs, gfp_t flags)
+               unsigned int num_segs, bool link_trbs, bool isoc, gfp_t flags)
 {
        struct xhci_ring        *ring;
        struct xhci_segment     *prev;
@@ -159,7 +162,7 @@ static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
        ring = kzalloc(sizeof *(ring), flags);
        xhci_dbg(xhci, "Allocating ring at %p\n", ring);
        if (!ring)
-               return 0;
+               return NULL;
 
        INIT_LIST_HEAD(&ring->td_list);
        if (num_segs == 0)
@@ -177,16 +180,17 @@ static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
                next = xhci_segment_alloc(xhci, flags);
                if (!next)
                        goto fail;
-               xhci_link_segments(xhci, prev, next, link_trbs);
+               xhci_link_segments(xhci, prev, next, link_trbs, isoc);
 
                prev = next;
                num_segs--;
        }
-       xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
+       xhci_link_segments(xhci, prev, ring->first_seg, link_trbs, isoc);
 
        if (link_trbs) {
                /* See section 4.9.2.1 and 6.4.4.1 */
-               prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
+               prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
+                       cpu_to_le32(LINK_TOGGLE);
                xhci_dbg(xhci, "Wrote link toggle flag to"
                                " segment %p (virtual), 0x%llx (DMA)\n",
                                prev, (unsigned long long)prev->dma);
@@ -196,7 +200,7 @@ static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
 
 fail:
        xhci_ring_free(xhci, ring);
-       return 0;
+       return NULL;
 }
 
 void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
@@ -207,14 +211,13 @@ void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
 
        rings_cached = virt_dev->num_rings_cached;
        if (rings_cached < XHCI_MAX_RINGS_CACHED) {
-               virt_dev->num_rings_cached++;
-               rings_cached = virt_dev->num_rings_cached;
                virt_dev->ring_cache[rings_cached] =
                        virt_dev->eps[ep_index].ring;
+               virt_dev->num_rings_cached++;
                xhci_dbg(xhci, "Cached old ring, "
                                "%d ring%s cached\n",
-                               rings_cached,
-                               (rings_cached > 1) ? "s" : "");
+                               virt_dev->num_rings_cached,
+                               (virt_dev->num_rings_cached > 1) ? "s" : "");
        } else {
                xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
                xhci_dbg(xhci, "Ring cache full (%d rings), "
@@ -228,14 +231,14 @@ void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
  * pointers to the beginning of the ring.
  */
 static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
-               struct xhci_ring *ring)
+               struct xhci_ring *ring, bool isoc)
 {
        struct xhci_segment     *seg = ring->first_seg;
        do {
                memset(seg->trbs, 0,
                                sizeof(union xhci_trb)*TRBS_PER_SEGMENT);
                /* All endpoint rings have link TRBs */
-               xhci_link_segments(xhci, seg, seg->next, 1);
+               xhci_link_segments(xhci, seg, seg->next, 1, isoc);
                seg = seg->next;
        } while (seg != ring->first_seg);
        xhci_initialize_ring_info(ring);
@@ -247,7 +250,7 @@ static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
 
 #define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
 
-struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
+static struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
                                                    int type, gfp_t flags)
 {
        struct xhci_container_ctx *ctx = kzalloc(sizeof(*ctx), flags);
@@ -265,7 +268,7 @@ struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
        return ctx;
 }
 
-void xhci_free_container_ctx(struct xhci_hcd *xhci,
+static void xhci_free_container_ctx(struct xhci_hcd *xhci,
                             struct xhci_container_ctx *ctx)
 {
        if (!ctx)
@@ -307,14 +310,14 @@ struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
 
 /***************** Streams structures manipulation *************************/
 
-void xhci_free_stream_ctx(struct xhci_hcd *xhci,
+static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
                unsigned int num_stream_ctxs,
                struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
 {
        struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
 
        if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
-               pci_free_consistent(pdev,
+               dma_free_coherent(&pdev->dev,
                                sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
                                stream_ctx, dma);
        else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
@@ -335,16 +338,16 @@ void xhci_free_stream_ctx(struct xhci_hcd *xhci,
  * The stream context array must be a power of 2, and can be as small as
  * 64 bytes or as large as 1MB.
  */
-struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
+static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
                unsigned int num_stream_ctxs, dma_addr_t *dma,
                gfp_t mem_flags)
 {
        struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
 
        if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
-               return pci_alloc_consistent(pdev,
+               return dma_alloc_coherent(&pdev->dev,
                                sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
-                               dma);
+                               dma, mem_flags);
        else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
                return dma_pool_alloc(xhci->small_streams_pool,
                                mem_flags, dma);
@@ -353,8 +356,19 @@ struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
                                mem_flags, dma);
 }
 
+struct xhci_ring *xhci_dma_to_transfer_ring(
+               struct xhci_virt_ep *ep,
+               u64 address)
+{
+       if (ep->ep_state & EP_HAS_STREAMS)
+               return radix_tree_lookup(&ep->stream_info->trb_address_map,
+                               address >> SEGMENT_SHIFT);
+       return ep->ring;
+}
+
+/* Only use this when you know stream_info is valid */
 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
-struct xhci_ring *dma_to_stream_ring(
+static struct xhci_ring *dma_to_stream_ring(
                struct xhci_stream_info *stream_info,
                u64 address)
 {
@@ -363,6 +377,23 @@ struct xhci_ring *dma_to_stream_ring(
 }
 #endif /* CONFIG_USB_XHCI_HCD_DEBUGGING */
 
+struct xhci_ring *xhci_stream_id_to_ring(
+               struct xhci_virt_device *dev,
+               unsigned int ep_index,
+               unsigned int stream_id)
+{
+       struct xhci_virt_ep *ep = &dev->eps[ep_index];
+
+       if (stream_id == 0)
+               return ep->ring;
+       if (!ep->stream_info)
+               return NULL;
+
+       if (stream_id > ep->stream_info->num_streams)
+               return NULL;
+       return ep->stream_info->stream_rings[stream_id];
+}
+
 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
 static int xhci_test_radix_tree(struct xhci_hcd *xhci,
                unsigned int num_streams,
@@ -511,15 +542,17 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
         */
        for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
                stream_info->stream_rings[cur_stream] =
-                       xhci_ring_alloc(xhci, 1, true, mem_flags);
+                       xhci_ring_alloc(xhci, 1, true, false, mem_flags);
                cur_ring = stream_info->stream_rings[cur_stream];
                if (!cur_ring)
                        goto cleanup_rings;
+               cur_ring->stream_id = cur_stream;
                /* Set deq ptr, cycle bit, and stream context type */
                addr = cur_ring->first_seg->dma |
                        SCT_FOR_CTX(SCT_PRI_TR) |
                        cur_ring->cycle_state;
-               stream_info->stream_ctx_array[cur_stream].stream_ring = addr;
+               stream_info->stream_ctx_array[cur_stream].stream_ring =
+                       cpu_to_le64(addr);
                xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
                                cur_stream, (unsigned long long) addr);
 
@@ -585,10 +618,10 @@ void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
        max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
        xhci_dbg(xhci, "Setting number of stream ctx array entries to %u\n",
                        1 << (max_primary_streams + 1));
-       ep_ctx->ep_info &= ~EP_MAXPSTREAMS_MASK;
-       ep_ctx->ep_info |= EP_MAXPSTREAMS(max_primary_streams);
-       ep_ctx->ep_info |= EP_HAS_LSA;
-       ep_ctx->deq  = stream_info->ctx_array_dma;
+       ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
+       ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
+                                      | EP_HAS_LSA);
+       ep_ctx->deq  = cpu_to_le64(stream_info->ctx_array_dma);
 }
 
 /*
@@ -601,10 +634,9 @@ void xhci_setup_no_streams_ep_input_ctx(struct xhci_hcd *xhci,
                struct xhci_virt_ep *ep)
 {
        dma_addr_t addr;
-       ep_ctx->ep_info &= ~EP_MAXPSTREAMS_MASK;
-       ep_ctx->ep_info &= ~EP_HAS_LSA;
+       ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA));
        addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
-       ep_ctx->deq  = addr | ep->ring->cycle_state;
+       ep_ctx->deq  = cpu_to_le64(addr | ep->ring->cycle_state);
 }
 
 /* Frees all stream contexts associated with the endpoint,
@@ -657,11 +689,103 @@ static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
        ep->xhci = xhci;
 }
 
-/* All the xhci_tds in the ring's TD list should be freed at this point */
+static void xhci_free_tt_info(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev,
+               int slot_id)
+{
+       struct list_head *tt;
+       struct list_head *tt_list_head;
+       struct list_head *tt_next;
+       struct xhci_tt_bw_info *tt_info;
+
+       /* If the device never made it past the Set Address stage,
+        * it may not have the real_port set correctly.
+        */
+       if (virt_dev->real_port == 0 ||
+                       virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
+               xhci_dbg(xhci, "Bad real port.\n");
+               return;
+       }
+
+       tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts);
+       if (list_empty(tt_list_head))
+               return;
+
+       list_for_each(tt, tt_list_head) {
+               tt_info = list_entry(tt, struct xhci_tt_bw_info, tt_list);
+               if (tt_info->slot_id == slot_id)
+                       break;
+       }
+       /* Cautionary measure in case the hub was disconnected before we
+        * stored the TT information.
+        */
+       if (tt_info->slot_id != slot_id)
+               return;
+
+       tt_next = tt->next;
+       tt_info = list_entry(tt, struct xhci_tt_bw_info,
+                       tt_list);
+       /* Multi-TT hubs will have more than one entry */
+       do {
+               list_del(tt);
+               kfree(tt_info);
+               tt = tt_next;
+               if (list_empty(tt_list_head))
+                       break;
+               tt_next = tt->next;
+               tt_info = list_entry(tt, struct xhci_tt_bw_info,
+                               tt_list);
+       } while (tt_info->slot_id == slot_id);
+}
+
+int xhci_alloc_tt_info(struct xhci_hcd *xhci,
+               struct xhci_virt_device *virt_dev,
+               struct usb_device *hdev,
+               struct usb_tt *tt, gfp_t mem_flags)
+{
+       struct xhci_tt_bw_info          *tt_info;
+       unsigned int                    num_ports;
+       int                             i, j;
+
+       if (!tt->multi)
+               num_ports = 1;
+       else
+               num_ports = hdev->maxchild;
+
+       for (i = 0; i < num_ports; i++, tt_info++) {
+               struct xhci_interval_bw_table *bw_table;
+
+               tt_info = kzalloc(sizeof(*tt_info), mem_flags);
+               if (!tt_info)
+                       goto free_tts;
+               INIT_LIST_HEAD(&tt_info->tt_list);
+               list_add(&tt_info->tt_list,
+                               &xhci->rh_bw[virt_dev->real_port - 1].tts);
+               tt_info->slot_id = virt_dev->udev->slot_id;
+               if (tt->multi)
+                       tt_info->ttport = i+1;
+               bw_table = &tt_info->bw_table;
+               for (j = 0; j < XHCI_MAX_INTERVAL; j++)
+                       INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
+       }
+       return 0;
+
+free_tts:
+       xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id);
+       return -ENOMEM;
+}
+
+
+/* All the xhci_tds in the ring's TD list should be freed at this point.
+ * Should be called with xhci->lock held if there is any chance the TT lists
+ * will be manipulated by the configure endpoint, allocate device, or update
+ * hub functions while this function is removing the TT entries from the list.
+ */
 void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
 {
        struct xhci_virt_device *dev;
        int i;
+       int old_active_eps = 0;
 
        /* Slot ID 0 is reserved */
        if (slot_id == 0 || !xhci->devs[slot_id])
@@ -672,13 +796,29 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
        if (!dev)
                return;
 
+       if (dev->tt_info)
+               old_active_eps = dev->tt_info->active_eps;
+
        for (i = 0; i < 31; ++i) {
                if (dev->eps[i].ring)
                        xhci_ring_free(xhci, dev->eps[i].ring);
                if (dev->eps[i].stream_info)
                        xhci_free_stream_info(xhci,
                                        dev->eps[i].stream_info);
+               /* Endpoints on the TT/root port lists should have been removed
+                * when usb_disable_device() was called for the device.
+                * We can't drop them anyway, because the udev might have gone
+                * away by this point, and we can't tell what speed it was.
+                */
+               if (!list_empty(&dev->eps[i].bw_endpoint_list))
+                       xhci_warn(xhci, "Slot %u endpoint %u "
+                                       "not removed from BW list!\n",
+                                       slot_id, i);
        }
+       /* If this is a hub, free the TT(s) from the TT list */
+       xhci_free_tt_info(xhci, dev, slot_id);
+       /* If necessary, update the number of active TTs on this root port */
+       xhci_update_tt_active_eps(xhci, dev, old_active_eps);
 
        if (dev->ring_cache) {
                for (i = 0; i < dev->num_rings_cached; i++)
@@ -692,7 +832,7 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
                xhci_free_container_ctx(xhci, dev->out_ctx);
 
        kfree(xhci->devs[slot_id]);
-       xhci->devs[slot_id] = 0;
+       xhci->devs[slot_id] = NULL;
 }
 
 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
@@ -732,10 +872,11 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
        for (i = 0; i < 31; i++) {
                xhci_init_endpoint_timer(xhci, &dev->eps[i]);
                INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
+               INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list);
        }
 
        /* Allocate endpoint 0 ring */
-       dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
+       dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, false, flags);
        if (!dev->eps[0].ring)
                goto fail;
 
@@ -749,13 +890,14 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 
        init_completion(&dev->cmd_completion);
        INIT_LIST_HEAD(&dev->cmd_list);
+       dev->udev = udev;
 
        /* Point to output device context in dcbaa. */
-       xhci->dcbaa->dev_context_ptrs[slot_id] = dev->out_ctx->dma;
+       xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma);
        xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
-                       slot_id,
-                       &xhci->dcbaa->dev_context_ptrs[slot_id],
-                       (unsigned long long) xhci->dcbaa->dev_context_ptrs[slot_id]);
+                slot_id,
+                &xhci->dcbaa->dev_context_ptrs[slot_id],
+                le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id]));
 
        return 1;
 fail:
@@ -763,14 +905,85 @@ fail:
        return 0;
 }
 
+void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
+               struct usb_device *udev)
+{
+       struct xhci_virt_device *virt_dev;
+       struct xhci_ep_ctx      *ep0_ctx;
+       struct xhci_ring        *ep_ring;
+
+       virt_dev = xhci->devs[udev->slot_id];
+       ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
+       ep_ring = virt_dev->eps[0].ring;
+       /*
+        * FIXME we don't keep track of the dequeue pointer very well after a
+        * Set TR dequeue pointer, so we're setting the dequeue pointer of the
+        * host to our enqueue pointer.  This should only be called after a
+        * configured device has reset, so all control transfers should have
+        * been completed or cancelled before the reset.
+        */
+       ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg,
+                                                       ep_ring->enqueue)
+                                  | ep_ring->cycle_state);
+}
+
+/*
+ * The xHCI roothub may have ports of differing speeds in any order in the port
+ * status registers.  xhci->port_array provides an array of the port speed for
+ * each offset into the port status registers.
+ *
+ * The xHCI hardware wants to know the roothub port number that the USB device
+ * is attached to (or the roothub port its ancestor hub is attached to).  All we
+ * know is the index of that port under either the USB 2.0 or the USB 3.0
+ * roothub, but that doesn't give us the real index into the HW port status
+ * registers.  Scan through the xHCI roothub port array, looking for the Nth
+ * entry of the correct port speed.  Return the port number of that entry.
+ */
+static u32 xhci_find_real_port_number(struct xhci_hcd *xhci,
+               struct usb_device *udev)
+{
+       struct usb_device *top_dev;
+       unsigned int num_similar_speed_ports;
+       unsigned int faked_port_num;
+       int i;
+
+       for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
+                       top_dev = top_dev->parent)
+               /* Found device below root hub */;
+       faked_port_num = top_dev->portnum;
+       for (i = 0, num_similar_speed_ports = 0;
+                       i < HCS_MAX_PORTS(xhci->hcs_params1); i++) {
+               u8 port_speed = xhci->port_array[i];
+
+               /*
+                * Skip ports that don't have known speeds, or have duplicate
+                * Extended Capabilities port speed entries.
+                */
+               if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
+                       continue;
+
+               /*
+                * USB 3.0 ports are always under a USB 3.0 hub.  USB 2.0 and
+                * 1.1 ports are under the USB 2.0 hub.  If the port speed
+                * matches the device speed, it's a similar speed port.
+                */
+               if ((port_speed == 0x03) == (udev->speed == USB_SPEED_SUPER))
+                       num_similar_speed_ports++;
+               if (num_similar_speed_ports == faked_port_num)
+                       /* Roothub ports are numbered from 1 to N */
+                       return i+1;
+       }
+       return 0;
+}
+
 /* Setup an xHCI virtual device for a Set Address command */
 int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
 {
        struct xhci_virt_device *dev;
        struct xhci_ep_ctx      *ep0_ctx;
-       struct usb_device       *top_dev;
        struct xhci_slot_ctx    *slot_ctx;
-       struct xhci_input_control_ctx *ctrl_ctx;
+       u32                     port_num;
+       struct usb_device *top_dev;
 
        dev = xhci->devs[udev->slot_id];
        /* Slot ID 0 is reserved */
@@ -780,28 +993,22 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
                return -EINVAL;
        }
        ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
-       ctrl_ctx = xhci_get_input_control_ctx(xhci, dev->in_ctx);
        slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
 
-       /* 2) New slot context and endpoint 0 context are valid*/
-       ctrl_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
-
        /* 3) Only the control endpoint is valid - one endpoint context */
-       slot_ctx->dev_info |= LAST_CTX(1);
-
-       slot_ctx->dev_info |= (u32) udev->route;
+       slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route);
        switch (udev->speed) {
        case USB_SPEED_SUPER:
-               slot_ctx->dev_info |= (u32) SLOT_SPEED_SS;
+               slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
                break;
        case USB_SPEED_HIGH:
-               slot_ctx->dev_info |= (u32) SLOT_SPEED_HS;
+               slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
                break;
        case USB_SPEED_FULL:
-               slot_ctx->dev_info |= (u32) SLOT_SPEED_FS;
+               slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
                break;
        case USB_SPEED_LOW:
-               slot_ctx->dev_info |= (u32) SLOT_SPEED_LS;
+               slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
                break;
        case USB_SPEED_WIRELESS:
                xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
@@ -812,40 +1019,76 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
                BUG();
        }
        /* Find the root hub port this device is under */
+       port_num = xhci_find_real_port_number(xhci, udev);
+       if (!port_num)
+               return -EINVAL;
+       slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(port_num));
+       /* Set the port number in the virtual_device to the faked port number */
        for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
                        top_dev = top_dev->parent)
                /* Found device below root hub */;
-       slot_ctx->dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
-       xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
-
-       /* Is this a LS/FS device under a HS hub? */
-       if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
-                       udev->tt) {
-               slot_ctx->tt_info = udev->tt->hub->slot_id;
-               slot_ctx->tt_info |= udev->ttport << 8;
+       dev->fake_port = top_dev->portnum;
+       dev->real_port = port_num;
+       xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num);
+       xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port);
+
+       /* Find the right bandwidth table that this device will be a part of.
+        * If this is a full speed device attached directly to a root port (or a
+        * decendent of one), it counts as a primary bandwidth domain, not a
+        * secondary bandwidth domain under a TT.  An xhci_tt_info structure
+        * will never be created for the HS root hub.
+        */
+       if (!udev->tt || !udev->tt->hub->parent) {
+               dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table;
+       } else {
+               struct xhci_root_port_bw_info *rh_bw;
+               struct xhci_tt_bw_info *tt_bw;
+
+               rh_bw = &xhci->rh_bw[port_num - 1];
+               /* Find the right TT. */
+               list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) {
+                       if (tt_bw->slot_id != udev->tt->hub->slot_id)
+                               continue;
+
+                       if (!dev->udev->tt->multi ||
+                                       (udev->tt->multi &&
+                                        tt_bw->ttport == dev->udev->ttport)) {
+                               dev->bw_table = &tt_bw->bw_table;
+                               dev->tt_info = tt_bw;
+                               break;
+                       }
+               }
+               if (!dev->tt_info)
+                       xhci_warn(xhci, "WARN: Didn't find a matching TT\n");
+       }
+
+       /* Is this a LS/FS device under an external HS hub? */
+       if (udev->tt && udev->tt->hub->parent) {
+               slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id |
+                                               (udev->ttport << 8));
                if (udev->tt->multi)
-                       slot_ctx->dev_info |= DEV_MTT;
+                       slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
        }
        xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
        xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
 
        /* Step 4 - ring already allocated */
        /* Step 5 */
-       ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
+       ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
        /*
         * XXX: Not sure about wireless USB devices.
         */
        switch (udev->speed) {
        case USB_SPEED_SUPER:
-               ep0_ctx->ep_info2 |= MAX_PACKET(512);
+               ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(512));
                break;
        case USB_SPEED_HIGH:
        /* USB core guesses at a 64-byte max packet first for FS devices */
        case USB_SPEED_FULL:
-               ep0_ctx->ep_info2 |= MAX_PACKET(64);
+               ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(64));
                break;
        case USB_SPEED_LOW:
-               ep0_ctx->ep_info2 |= MAX_PACKET(8);
+               ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(8));
                break;
        case USB_SPEED_WIRELESS:
                xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
@@ -856,18 +1099,83 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
                BUG();
        }
        /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
-       ep0_ctx->ep_info2 |= MAX_BURST(0);
-       ep0_ctx->ep_info2 |= ERROR_COUNT(3);
+       ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3));
 
-       ep0_ctx->deq =
-               dev->eps[0].ring->first_seg->dma;
-       ep0_ctx->deq |= dev->eps[0].ring->cycle_state;
+       ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma |
+                                  dev->eps[0].ring->cycle_state);
 
        /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
 
        return 0;
 }
 
+/*
+ * Convert interval expressed as 2^(bInterval - 1) == interval into
+ * straight exponent value 2^n == interval.
+ *
+ */
+static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
+               struct usb_host_endpoint *ep)
+{
+       unsigned int interval;
+
+       interval = clamp_val(ep->desc.bInterval, 1, 16) - 1;
+       if (interval != ep->desc.bInterval - 1)
+               dev_warn(&udev->dev,
+                        "ep %#x - rounding interval to %d %sframes\n",
+                        ep->desc.bEndpointAddress,
+                        1 << interval,
+                        udev->speed == USB_SPEED_FULL ? "" : "micro");
+
+       if (udev->speed == USB_SPEED_FULL) {
+               /*
+                * Full speed isoc endpoints specify interval in frames,
+                * not microframes. We are using microframes everywhere,
+                * so adjust accordingly.
+                */
+               interval += 3;  /* 1 frame = 2^3 uframes */
+       }
+
+       return interval;
+}
+
+/*
+ * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
+ * microframes, rounded down to nearest power of 2.
+ */
+static unsigned int xhci_microframes_to_exponent(struct usb_device *udev,
+               struct usb_host_endpoint *ep, unsigned int desc_interval,
+               unsigned int min_exponent, unsigned int max_exponent)
+{
+       unsigned int interval;
+
+       interval = fls(desc_interval) - 1;
+       interval = clamp_val(interval, min_exponent, max_exponent);
+       if ((1 << interval) != desc_interval)
+               dev_warn(&udev->dev,
+                        "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n",
+                        ep->desc.bEndpointAddress,
+                        1 << interval,
+                        desc_interval);
+
+       return interval;
+}
+
+static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
+               struct usb_host_endpoint *ep)
+{
+       return xhci_microframes_to_exponent(udev, ep,
+                       ep->desc.bInterval, 0, 15);
+}
+
+
+static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
+               struct usb_host_endpoint *ep)
+{
+       return xhci_microframes_to_exponent(udev, ep,
+                       ep->desc.bInterval * 8, 3, 10);
+}
+
 /* Return the polling or NAK interval.
  *
  * The polling interval is expressed in "microframes".  If xHCI's Interval field
@@ -876,7 +1184,7 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
  * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  * is set to 0.
  */
-static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
+static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
                struct usb_host_endpoint *ep)
 {
        unsigned int interval = 0;
@@ -885,65 +1193,59 @@ static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
        case USB_SPEED_HIGH:
                /* Max NAK rate */
                if (usb_endpoint_xfer_control(&ep->desc) ||
-                               usb_endpoint_xfer_bulk(&ep->desc))
-                       interval = ep->desc.bInterval;
+                   usb_endpoint_xfer_bulk(&ep->desc)) {
+                       interval = xhci_parse_microframe_interval(udev, ep);
+                       break;
+               }
                /* Fall through - SS and HS isoc/int have same decoding */
+
        case USB_SPEED_SUPER:
                if (usb_endpoint_xfer_int(&ep->desc) ||
-                               usb_endpoint_xfer_isoc(&ep->desc)) {
-                       if (ep->desc.bInterval == 0)
-                               interval = 0;
-                       else
-                               interval = ep->desc.bInterval - 1;
-                       if (interval > 15)
-                               interval = 15;
-                       if (interval != ep->desc.bInterval + 1)
-                               dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
-                                               ep->desc.bEndpointAddress, 1 << interval);
+                   usb_endpoint_xfer_isoc(&ep->desc)) {
+                       interval = xhci_parse_exponent_interval(udev, ep);
                }
                break;
-       /* Convert bInterval (in 1-255 frames) to microframes and round down to
-        * nearest power of 2.
-        */
+
        case USB_SPEED_FULL:
+               if (usb_endpoint_xfer_isoc(&ep->desc)) {
+                       interval = xhci_parse_exponent_interval(udev, ep);
+                       break;
+               }
+               /*
+                * Fall through for interrupt endpoint interval decoding
+                * since it uses the same rules as low speed interrupt
+                * endpoints.
+                */
+
        case USB_SPEED_LOW:
                if (usb_endpoint_xfer_int(&ep->desc) ||
-                               usb_endpoint_xfer_isoc(&ep->desc)) {
-                       interval = fls(8*ep->desc.bInterval) - 1;
-                       if (interval > 10)
-                               interval = 10;
-                       if (interval < 3)
-                               interval = 3;
-                       if ((1 << interval) != 8*ep->desc.bInterval)
-                               dev_warn(&udev->dev,
-                                               "ep %#x - rounding interval"
-                                               " to %d microframes, "
-                                               "ep desc says %d microframes\n",
-                                               ep->desc.bEndpointAddress,
-                                               1 << interval,
-                                               8*ep->desc.bInterval);
+                   usb_endpoint_xfer_isoc(&ep->desc)) {
+
+                       interval = xhci_parse_frame_interval(udev, ep);
                }
                break;
+
        default:
                BUG();
        }
        return EP_INTERVAL(interval);
 }
 
-/* The "Mult" field in the endpoint context is only set for SuperSpeed devices.
+/* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  * High speed endpoint descriptors can define "the number of additional
  * transaction opportunities per microframe", but that goes in the Max Burst
  * endpoint context field.
  */
-static inline u32 xhci_get_endpoint_mult(struct usb_device *udev,
+static u32 xhci_get_endpoint_mult(struct usb_device *udev,
                struct usb_host_endpoint *ep)
 {
-       if (udev->speed != USB_SPEED_SUPER || !ep->ss_ep_comp)
+       if (udev->speed != USB_SPEED_SUPER ||
+                       !usb_endpoint_xfer_isoc(&ep->desc))
                return 0;
-       return ep->ss_ep_comp->desc.bmAttributes;
+       return ep->ss_ep_comp.bmAttributes;
 }
 
-static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
+static u32 xhci_get_endpoint_type(struct usb_device *udev,
                struct usb_host_endpoint *ep)
 {
        int in;
@@ -977,7 +1279,7 @@ static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  * Basically, this is the maxpacket size, multiplied by the burst size
  * and mult size.
  */
-static inline u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
+static u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
                struct usb_device *udev,
                struct usb_host_endpoint *ep)
 {
@@ -989,16 +1291,11 @@ static inline u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
                        usb_endpoint_xfer_bulk(&ep->desc))
                return 0;
 
-       if (udev->speed == USB_SPEED_SUPER) {
-               if (ep->ss_ep_comp)
-                       return ep->ss_ep_comp->desc.wBytesPerInterval;
-               xhci_warn(xhci, "WARN no SS endpoint companion descriptor.\n");
-               /* Assume no bursts, no multiple opportunities to send. */
-               return ep->desc.wMaxPacketSize;
-       }
+       if (udev->speed == USB_SPEED_SUPER)
+               return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
 
-       max_packet = ep->desc.wMaxPacketSize & 0x3ff;
-       max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
+       max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&ep->desc));
+       max_burst = (usb_endpoint_maxp(&ep->desc) & 0x1800) >> 11;
        /* A 0 in max burst means 1 transfer per ESIT */
        return max_packet * (max_burst + 1);
 }
@@ -1023,8 +1320,18 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
        ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
 
        /* Set up the endpoint ring */
-       virt_dev->eps[ep_index].new_ring =
-               xhci_ring_alloc(xhci, 1, true, mem_flags);
+       /*
+        * Isochronous endpoint ring needs bigger size because one isoc URB
+        * carries multiple packets and it will insert multiple tds to the
+        * ring.
+        * This should be replaced with dynamic ring resizing in the future.
+        */
+       if (usb_endpoint_xfer_isoc(&ep->desc))
+               virt_dev->eps[ep_index].new_ring =
+                       xhci_ring_alloc(xhci, 8, true, true, mem_flags);
+       else
+               virt_dev->eps[ep_index].new_ring =
+                       xhci_ring_alloc(xhci, 1, true, false, mem_flags);
        if (!virt_dev->eps[ep_index].new_ring) {
                /* Attempt to use the ring cache */
                if (virt_dev->num_rings_cached == 0)
@@ -1033,39 +1340,36 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
                        virt_dev->ring_cache[virt_dev->num_rings_cached];
                virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
                virt_dev->num_rings_cached--;
-               xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
+               xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring,
+                       usb_endpoint_xfer_isoc(&ep->desc) ? true : false);
        }
+       virt_dev->eps[ep_index].skip = false;
        ep_ring = virt_dev->eps[ep_index].new_ring;
-       ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
+       ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma | ep_ring->cycle_state);
 
-       ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
-       ep_ctx->ep_info |= EP_MULT(xhci_get_endpoint_mult(udev, ep));
+       ep_ctx->ep_info = cpu_to_le32(xhci_get_endpoint_interval(udev, ep)
+                                     | EP_MULT(xhci_get_endpoint_mult(udev, ep)));
 
        /* FIXME dig Mult and streams info out of ep companion desc */
 
        /* Allow 3 retries for everything but isoc;
-        * error count = 0 means infinite retries.
+        * CErr shall be set to 0 for Isoch endpoints.
         */
        if (!usb_endpoint_xfer_isoc(&ep->desc))
-               ep_ctx->ep_info2 = ERROR_COUNT(3);
+               ep_ctx->ep_info2 = cpu_to_le32(ERROR_COUNT(3));
        else
-               ep_ctx->ep_info2 = ERROR_COUNT(1);
+               ep_ctx->ep_info2 = cpu_to_le32(ERROR_COUNT(0));
 
-       ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
+       ep_ctx->ep_info2 |= cpu_to_le32(xhci_get_endpoint_type(udev, ep));
 
        /* Set the max packet size and max burst */
        switch (udev->speed) {
        case USB_SPEED_SUPER:
-               max_packet = ep->desc.wMaxPacketSize;
-               ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
+               max_packet = usb_endpoint_maxp(&ep->desc);
+               ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet));
                /* dig out max burst from ep companion desc */
-               if (!ep->ss_ep_comp) {
-                       xhci_warn(xhci, "WARN no SS endpoint companion descriptor.\n");
-                       max_packet = 0;
-               } else {
-                       max_packet = ep->ss_ep_comp->desc.bMaxBurst;
-               }
-               ep_ctx->ep_info2 |= MAX_BURST(max_packet);
+               max_packet = ep->ss_ep_comp.bMaxBurst;
+               ep_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(max_packet));
                break;
        case USB_SPEED_HIGH:
                /* bits 11:12 specify the number of additional transaction
@@ -1073,20 +1377,21 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
                 */
                if (usb_endpoint_xfer_isoc(&ep->desc) ||
                                usb_endpoint_xfer_int(&ep->desc)) {
-                       max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
-                       ep_ctx->ep_info2 |= MAX_BURST(max_burst);
+                       max_burst = (usb_endpoint_maxp(&ep->desc)
+                                    & 0x1800) >> 11;
+                       ep_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(max_burst));
                }
                /* Fall through */
        case USB_SPEED_FULL:
        case USB_SPEED_LOW:
-               max_packet = ep->desc.wMaxPacketSize & 0x3ff;
-               ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
+               max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&ep->desc));
+               ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet));
                break;
        default:
                BUG();
        }
        max_esit_payload = xhci_get_max_esit_payload(xhci, udev, ep);
-       ep_ctx->tx_info = MAX_ESIT_PAYLOAD_FOR_EP(max_esit_payload);
+       ep_ctx->tx_info = cpu_to_le32(MAX_ESIT_PAYLOAD_FOR_EP(max_esit_payload));
 
        /*
         * XXX no idea how to calculate the average TRB buffer length for bulk
@@ -1102,8 +1407,15 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
         * including link TRBs, No-op TRBs, and Event data TRBs.  Since we don't
         * use Event Data TRBs, and we don't chain in a link TRB on short
         * transfers, we're basically dividing by 1.
+        *
+        * xHCI 1.0 specification indicates that the Average TRB Length should
+        * be set to 8 for control endpoints.
         */
-       ep_ctx->tx_info |= AVG_TRB_LENGTH_FOR_EP(max_esit_payload);
+       if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version == 0x100)
+               ep_ctx->tx_info |= cpu_to_le32(AVG_TRB_LENGTH_FOR_EP(8));
+       else
+               ep_ctx->tx_info |=
+                        cpu_to_le32(AVG_TRB_LENGTH_FOR_EP(max_esit_payload));
 
        /* FIXME Debug endpoint context */
        return 0;
@@ -1128,6 +1440,70 @@ void xhci_endpoint_zero(struct xhci_hcd *xhci,
         */
 }
 
+void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info)
+{
+       bw_info->ep_interval = 0;
+       bw_info->mult = 0;
+       bw_info->num_packets = 0;
+       bw_info->max_packet_size = 0;
+       bw_info->type = 0;
+       bw_info->max_esit_payload = 0;
+}
+
+void xhci_update_bw_info(struct xhci_hcd *xhci,
+               struct xhci_container_ctx *in_ctx,
+               struct xhci_input_control_ctx *ctrl_ctx,
+               struct xhci_virt_device *virt_dev)
+{
+       struct xhci_bw_info *bw_info;
+       struct xhci_ep_ctx *ep_ctx;
+       unsigned int ep_type;
+       int i;
+
+       for (i = 1; i < 31; ++i) {
+               bw_info = &virt_dev->eps[i].bw_info;
+
+               /* We can't tell what endpoint type is being dropped, but
+                * unconditionally clearing the bandwidth info for non-periodic
+                * endpoints should be harmless because the info will never be
+                * set in the first place.
+                */
+               if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) {
+                       /* Dropped endpoint */
+                       xhci_clear_endpoint_bw_info(bw_info);
+                       continue;
+               }
+
+               if (EP_IS_ADDED(ctrl_ctx, i)) {
+                       ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i);
+                       ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
+
+                       /* Ignore non-periodic endpoints */
+                       if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
+                                       ep_type != ISOC_IN_EP &&
+                                       ep_type != INT_IN_EP)
+                               continue;
+
+                       /* Added or changed endpoint */
+                       bw_info->ep_interval = CTX_TO_EP_INTERVAL(
+                                       le32_to_cpu(ep_ctx->ep_info));
+                       /* Number of packets and mult are zero-based in the
+                        * input context, but we want one-based for the
+                        * interval table.
+                        */
+                       bw_info->mult = CTX_TO_EP_MULT(
+                                       le32_to_cpu(ep_ctx->ep_info)) + 1;
+                       bw_info->num_packets = CTX_TO_MAX_BURST(
+                                       le32_to_cpu(ep_ctx->ep_info2)) + 1;
+                       bw_info->max_packet_size = MAX_PACKET_DECODED(
+                                       le32_to_cpu(ep_ctx->ep_info2));
+                       bw_info->type = ep_type;
+                       bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD(
+                                       le32_to_cpu(ep_ctx->tx_info));
+               }
+       }
+}
+
 /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  * Useful when you want to change one particular aspect of the endpoint and then
  * issue a configure endpoint command.
@@ -1186,10 +1562,9 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
        if (!xhci->scratchpad)
                goto fail_sp;
 
-       xhci->scratchpad->sp_array =
-               pci_alloc_consistent(to_pci_dev(dev),
+       xhci->scratchpad->sp_array = dma_alloc_coherent(dev,
                                     num_sp * sizeof(u64),
-                                    &xhci->scratchpad->sp_dma);
+                                    &xhci->scratchpad->sp_dma, flags);
        if (!xhci->scratchpad->sp_array)
                goto fail_sp2;
 
@@ -1203,11 +1578,11 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
        if (!xhci->scratchpad->sp_dma_buffers)
                goto fail_sp4;
 
-       xhci->dcbaa->dev_context_ptrs[0] = xhci->scratchpad->sp_dma;
+       xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma);
        for (i = 0; i < num_sp; i++) {
                dma_addr_t dma;
-               void *buf = pci_alloc_consistent(to_pci_dev(dev),
-                                                xhci->page_size, &dma);
+               void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma,
+                               flags);
                if (!buf)
                        goto fail_sp5;
 
@@ -1220,7 +1595,7 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
 
  fail_sp5:
        for (i = i - 1; i >= 0; i--) {
-               pci_free_consistent(to_pci_dev(dev), xhci->page_size,
+               dma_free_coherent(dev, xhci->page_size,
                                    xhci->scratchpad->sp_buffers[i],
                                    xhci->scratchpad->sp_dma_buffers[i]);
        }
@@ -1230,7 +1605,7 @@ static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
        kfree(xhci->scratchpad->sp_buffers);
 
  fail_sp3:
-       pci_free_consistent(to_pci_dev(dev), num_sp * sizeof(u64),
+       dma_free_coherent(dev, num_sp * sizeof(u64),
                            xhci->scratchpad->sp_array,
                            xhci->scratchpad->sp_dma);
 
@@ -1254,13 +1629,13 @@ static void scratchpad_free(struct xhci_hcd *xhci)
        num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
 
        for (i = 0; i < num_sp; i++) {
-               pci_free_consistent(pdev, xhci->page_size,
+               dma_free_coherent(&pdev->dev, xhci->page_size,
                                    xhci->scratchpad->sp_buffers[i],
                                    xhci->scratchpad->sp_dma_buffers[i]);
        }
        kfree(xhci->scratchpad->sp_dma_buffers);
        kfree(xhci->scratchpad->sp_buffers);
-       pci_free_consistent(pdev, num_sp * sizeof(u64),
+       dma_free_coherent(&pdev->dev, num_sp * sizeof(u64),
                            xhci->scratchpad->sp_array,
                            xhci->scratchpad->sp_dma);
        kfree(xhci->scratchpad);
@@ -1303,6 +1678,14 @@ struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
        return command;
 }
 
+void xhci_urb_free_priv(struct xhci_hcd *xhci, struct urb_priv *urb_priv)
+{
+       if (urb_priv) {
+               kfree(urb_priv->td[0]);
+               kfree(urb_priv);
+       }
+}
+
 void xhci_free_command(struct xhci_hcd *xhci,
                struct xhci_command *command)
 {
@@ -1315,18 +1698,15 @@ void xhci_free_command(struct xhci_hcd *xhci,
 void xhci_mem_cleanup(struct xhci_hcd *xhci)
 {
        struct pci_dev  *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
+       struct dev_info *dev_info, *next;
+       unsigned long   flags;
        int size;
        int i;
 
        /* Free the Event Ring Segment Table and the actual Event Ring */
-       if (xhci->ir_set) {
-               xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
-               xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
-               xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
-       }
        size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
        if (xhci->erst.entries)
-               pci_free_consistent(pdev, size,
+               dma_free_coherent(&pdev->dev, size,
                                xhci->erst.entries, xhci->erst.erst_dma_addr);
        xhci->erst.entries = NULL;
        xhci_dbg(xhci, "Freed ERST\n");
@@ -1335,7 +1715,6 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
        xhci->event_ring = NULL;
        xhci_dbg(xhci, "Freed event ring\n");
 
-       xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
        if (xhci->cmd_ring)
                xhci_ring_free(xhci, xhci->cmd_ring);
        xhci->cmd_ring = NULL;
@@ -1364,15 +1743,31 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
        xhci->medium_streams_pool = NULL;
        xhci_dbg(xhci, "Freed medium stream array pool\n");
 
-       xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
        if (xhci->dcbaa)
-               pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
+               dma_free_coherent(&pdev->dev, sizeof(*xhci->dcbaa),
                                xhci->dcbaa, xhci->dcbaa->dma);
        xhci->dcbaa = NULL;
 
        scratchpad_free(xhci);
+
+       spin_lock_irqsave(&xhci->lock, flags);
+       list_for_each_entry_safe(dev_info, next, &xhci->lpm_failed_devs, list) {
+               list_del(&dev_info->list);
+               kfree(dev_info);
+       }
+       spin_unlock_irqrestore(&xhci->lock, flags);
+
+       xhci->num_usb2_ports = 0;
+       xhci->num_usb3_ports = 0;
+       kfree(xhci->usb2_ports);
+       kfree(xhci->usb3_ports);
+       kfree(xhci->port_array);
+       kfree(xhci->rh_bw);
+
        xhci->page_size = 0;
        xhci->page_shift = 0;
+       xhci->bus_state[0].bus_suspended = 0;
+       xhci->bus_state[1].bus_suspended = 0;
 }
 
 static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
@@ -1502,7 +1897,7 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
        unsigned int num_tests;
        int i, ret;
 
-       num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]);
+       num_tests = ARRAY_SIZE(simple_test_vector);
        for (i = 0; i < num_tests; i++) {
                ret = xhci_test_trb_in_td(xhci,
                                xhci->event_ring->first_seg,
@@ -1515,7 +1910,7 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
                        return ret;
        }
 
-       num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]);
+       num_tests = ARRAY_SIZE(complex_test_vector);
        for (i = 0; i < num_tests; i++) {
                ret = xhci_test_trb_in_td(xhci,
                                complex_test_vector[i].input_seg,
@@ -1531,6 +1926,236 @@ static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
        return 0;
 }
 
+static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
+{
+       u64 temp;
+       dma_addr_t deq;
+
+       deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
+                       xhci->event_ring->dequeue);
+       if (deq == 0 && !in_interrupt())
+               xhci_warn(xhci, "WARN something wrong with SW event ring "
+                               "dequeue ptr.\n");
+       /* Update HC event ring dequeue pointer */
+       temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
+       temp &= ERST_PTR_MASK;
+       /* Don't clear the EHB bit (which is RW1C) because
+        * there might be more events to service.
+        */
+       temp &= ~ERST_EHB;
+       xhci_dbg(xhci, "// Write event ring dequeue pointer, "
+                       "preserving EHB bit\n");
+       xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
+                       &xhci->ir_set->erst_dequeue);
+}
+
+static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
+               __le32 __iomem *addr, u8 major_revision)
+{
+       u32 temp, port_offset, port_count;
+       int i;
+
+       if (major_revision > 0x03) {
+               xhci_warn(xhci, "Ignoring unknown port speed, "
+                               "Ext Cap %p, revision = 0x%x\n",
+                               addr, major_revision);
+               /* Ignoring port protocol we can't understand. FIXME */
+               return;
+       }
+
+       /* Port offset and count in the third dword, see section 7.2 */
+       temp = xhci_readl(xhci, addr + 2);
+       port_offset = XHCI_EXT_PORT_OFF(temp);
+       port_count = XHCI_EXT_PORT_COUNT(temp);
+       xhci_dbg(xhci, "Ext Cap %p, port offset = %u, "
+                       "count = %u, revision = 0x%x\n",
+                       addr, port_offset, port_count, major_revision);
+       /* Port count includes the current port offset */
+       if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
+               /* WTF? "Valid values are ‘1’ to MaxPorts" */
+               return;
+
+       /* Check the host's USB2 LPM capability */
+       if ((xhci->hci_version == 0x96) && (major_revision != 0x03) &&
+                       (temp & XHCI_L1C)) {
+               xhci_dbg(xhci, "xHCI 0.96: support USB2 software lpm\n");
+               xhci->sw_lpm_support = 1;
+       }
+
+       if ((xhci->hci_version >= 0x100) && (major_revision != 0x03)) {
+               xhci_dbg(xhci, "xHCI 1.0: support USB2 software lpm\n");
+               xhci->sw_lpm_support = 1;
+               if (temp & XHCI_HLC) {
+                       xhci_dbg(xhci, "xHCI 1.0: support USB2 hardware lpm\n");
+                       xhci->hw_lpm_support = 1;
+               }
+       }
+
+       port_offset--;
+       for (i = port_offset; i < (port_offset + port_count); i++) {
+               /* Duplicate entry.  Ignore the port if the revisions differ. */
+               if (xhci->port_array[i] != 0) {
+                       xhci_warn(xhci, "Duplicate port entry, Ext Cap %p,"
+                                       " port %u\n", addr, i);
+                       xhci_warn(xhci, "Port was marked as USB %u, "
+                                       "duplicated as USB %u\n",
+                                       xhci->port_array[i], major_revision);
+                       /* Only adjust the roothub port counts if we haven't
+                        * found a similar duplicate.
+                        */
+                       if (xhci->port_array[i] != major_revision &&
+                               xhci->port_array[i] != DUPLICATE_ENTRY) {
+                               if (xhci->port_array[i] == 0x03)
+                                       xhci->num_usb3_ports--;
+                               else
+                                       xhci->num_usb2_ports--;
+                               xhci->port_array[i] = DUPLICATE_ENTRY;
+                       }
+                       /* FIXME: Should we disable the port? */
+                       continue;
+               }
+               xhci->port_array[i] = major_revision;
+               if (major_revision == 0x03)
+                       xhci->num_usb3_ports++;
+               else
+                       xhci->num_usb2_ports++;
+       }
+       /* FIXME: Should we disable ports not in the Extended Capabilities? */
+}
+
+/*
+ * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
+ * specify what speeds each port is supposed to be.  We can't count on the port
+ * speed bits in the PORTSC register being correct until a device is connected,
+ * but we need to set up the two fake roothubs with the correct number of USB
+ * 3.0 and USB 2.0 ports at host controller initialization time.
+ */
+static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
+{
+       __le32 __iomem *addr;
+       u32 offset;
+       unsigned int num_ports;
+       int i, j, port_index;
+
+       addr = &xhci->cap_regs->hcc_params;
+       offset = XHCI_HCC_EXT_CAPS(xhci_readl(xhci, addr));
+       if (offset == 0) {
+               xhci_err(xhci, "No Extended Capability registers, "
+                               "unable to set up roothub.\n");
+               return -ENODEV;
+       }
+
+       num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
+       xhci->port_array = kzalloc(sizeof(*xhci->port_array)*num_ports, flags);
+       if (!xhci->port_array)
+               return -ENOMEM;
+
+       xhci->rh_bw = kzalloc(sizeof(*xhci->rh_bw)*num_ports, flags);
+       if (!xhci->rh_bw)
+               return -ENOMEM;
+       for (i = 0; i < num_ports; i++) {
+               struct xhci_interval_bw_table *bw_table;
+
+               INIT_LIST_HEAD(&xhci->rh_bw[i].tts);
+               bw_table = &xhci->rh_bw[i].bw_table;
+               for (j = 0; j < XHCI_MAX_INTERVAL; j++)
+                       INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
+       }
+
+       /*
+        * For whatever reason, the first capability offset is from the
+        * capability register base, not from the HCCPARAMS register.
+        * See section 5.3.6 for offset calculation.
+        */
+       addr = &xhci->cap_regs->hc_capbase + offset;
+       while (1) {
+               u32 cap_id;
+
+               cap_id = xhci_readl(xhci, addr);
+               if (XHCI_EXT_CAPS_ID(cap_id) == XHCI_EXT_CAPS_PROTOCOL)
+                       xhci_add_in_port(xhci, num_ports, addr,
+                                       (u8) XHCI_EXT_PORT_MAJOR(cap_id));
+               offset = XHCI_EXT_CAPS_NEXT(cap_id);
+               if (!offset || (xhci->num_usb2_ports + xhci->num_usb3_ports)
+                               == num_ports)
+                       break;
+               /*
+                * Once you're into the Extended Capabilities, the offset is
+                * always relative to the register holding the offset.
+                */
+               addr += offset;
+       }
+
+       if (xhci->num_usb2_ports == 0 && xhci->num_usb3_ports == 0) {
+               xhci_warn(xhci, "No ports on the roothubs?\n");
+               return -ENODEV;
+       }
+       xhci_dbg(xhci, "Found %u USB 2.0 ports and %u USB 3.0 ports.\n",
+                       xhci->num_usb2_ports, xhci->num_usb3_ports);
+
+       /* Place limits on the number of roothub ports so that the hub
+        * descriptors aren't longer than the USB core will allocate.
+        */
+       if (xhci->num_usb3_ports > 15) {
+               xhci_dbg(xhci, "Limiting USB 3.0 roothub ports to 15.\n");
+               xhci->num_usb3_ports = 15;
+       }
+       if (xhci->num_usb2_ports > USB_MAXCHILDREN) {
+               xhci_dbg(xhci, "Limiting USB 2.0 roothub ports to %u.\n",
+                               USB_MAXCHILDREN);
+               xhci->num_usb2_ports = USB_MAXCHILDREN;
+       }
+
+       /*
+        * Note we could have all USB 3.0 ports, or all USB 2.0 ports.
+        * Not sure how the USB core will handle a hub with no ports...
+        */
+       if (xhci->num_usb2_ports) {
+               xhci->usb2_ports = kmalloc(sizeof(*xhci->usb2_ports)*
+                               xhci->num_usb2_ports, flags);
+               if (!xhci->usb2_ports)
+                       return -ENOMEM;
+
+               port_index = 0;
+               for (i = 0; i < num_ports; i++) {
+                       if (xhci->port_array[i] == 0x03 ||
+                                       xhci->port_array[i] == 0 ||
+                                       xhci->port_array[i] == DUPLICATE_ENTRY)
+                               continue;
+
+                       xhci->usb2_ports[port_index] =
+                               &xhci->op_regs->port_status_base +
+                               NUM_PORT_REGS*i;
+                       xhci_dbg(xhci, "USB 2.0 port at index %u, "
+                                       "addr = %p\n", i,
+                                       xhci->usb2_ports[port_index]);
+                       port_index++;
+                       if (port_index == xhci->num_usb2_ports)
+                               break;
+               }
+       }
+       if (xhci->num_usb3_ports) {
+               xhci->usb3_ports = kmalloc(sizeof(*xhci->usb3_ports)*
+                               xhci->num_usb3_ports, flags);
+               if (!xhci->usb3_ports)
+                       return -ENOMEM;
+
+               port_index = 0;
+               for (i = 0; i < num_ports; i++)
+                       if (xhci->port_array[i] == 0x03) {
+                               xhci->usb3_ports[port_index] =
+                                       &xhci->op_regs->port_status_base +
+                                       NUM_PORT_REGS*i;
+                               xhci_dbg(xhci, "USB 3.0 port at index %u, "
+                                               "addr = %p\n", i,
+                                               xhci->usb3_ports[port_index]);
+                               port_index++;
+                               if (port_index == xhci->num_usb3_ports)
+                                       break;
+                       }
+       }
+       return 0;
+}
 
 int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 {
@@ -1575,8 +2200,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
         * Section 5.4.8 - doorbell array must be
         * "physically contiguous and 64-byte (cache line) aligned".
         */
-       xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
-                       sizeof(*xhci->dcbaa), &dma);
+       xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma,
+                       GFP_KERNEL);
        if (!xhci->dcbaa)
                goto fail;
        memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
@@ -1610,14 +2235,14 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
                dma_pool_create("xHCI 1KB stream ctx arrays",
                        dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
        /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
-        * will be allocated with pci_alloc_consistent()
+        * will be allocated with dma_alloc_coherent()
         */
 
        if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
                goto fail;
 
        /* Set up the command ring to have one segments for now. */
-       xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
+       xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, false, flags);
        if (!xhci->cmd_ring)
                goto fail;
        xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
@@ -1637,25 +2262,27 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
        val &= DBOFF_MASK;
        xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
                        " from cap regs base addr\n", val);
-       xhci->dba = (void *) xhci->cap_regs + val;
+       xhci->dba = (void __iomem *) xhci->cap_regs + val;
        xhci_dbg_regs(xhci);
        xhci_print_run_regs(xhci);
        /* Set ir_set to interrupt register set 0 */
-       xhci->ir_set = (void *) xhci->run_regs->ir_set;
+       xhci->ir_set = &xhci->run_regs->ir_set[0];
 
        /*
         * Event ring setup: Allocate a normal ring, but also setup
         * the event ring segment table (ERST).  Section 4.9.3.
         */
        xhci_dbg(xhci, "// Allocating event ring\n");
-       xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
+       xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, false,
+                                               flags);
        if (!xhci->event_ring)
                goto fail;
        if (xhci_check_trb_in_td_math(xhci, flags) < 0)
                goto fail;
 
-       xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
-                       sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
+       xhci->erst.entries = dma_alloc_coherent(dev,
+                       sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS, &dma,
+                       GFP_KERNEL);
        if (!xhci->erst.entries)
                goto fail;
        xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
@@ -1672,8 +2299,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
        /* set ring base address and size for each segment table entry */
        for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
                struct xhci_erst_entry *entry = &xhci->erst.entries[val];
-               entry->seg_addr = seg->dma;
-               entry->seg_size = TRBS_PER_SEGMENT;
+               entry->seg_addr = cpu_to_le64(seg->dma);
+               entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
                entry->rsvd = 0;
                seg = seg->next;
        }
@@ -1698,7 +2325,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
        /* Set the event ring dequeue address */
        xhci_set_hc_event_deq(xhci);
        xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
-       xhci_print_ir_set(xhci, xhci->ir_set, 0);
+       xhci_print_ir_set(xhci, 0);
 
        /*
         * XXX: Might need to set the Interrupter Moderation Register to
@@ -1707,15 +2334,25 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
         */
        init_completion(&xhci->addr_dev);
        for (i = 0; i < MAX_HC_SLOTS; ++i)
-               xhci->devs[i] = 0;
+               xhci->devs[i] = NULL;
+       for (i = 0; i < USB_MAXCHILDREN; ++i) {
+               xhci->bus_state[0].resume_done[i] = 0;
+               xhci->bus_state[1].resume_done[i] = 0;
+       }
 
        if (scratchpad_alloc(xhci, flags))
                goto fail;
+       if (xhci_setup_port_arrays(xhci, flags))
+               goto fail;
+
+       INIT_LIST_HEAD(&xhci->lpm_failed_devs);
 
        return 0;
 
 fail:
        xhci_warn(xhci, "Couldn't initialize memory\n");
+       xhci_halt(xhci);
+       xhci_reset(xhci);
        xhci_mem_cleanup(xhci);
        return -ENOMEM;
 }