xhci: change xhci 1.0 only restrictions to support xhci 1.1
[pandora-kernel.git] / drivers / usb / host / xhci-ring.c
index 9f1d4b1..dc8e5a8 100644 (file)
@@ -85,7 +85,7 @@ dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
                return 0;
        /* offset in TRBs */
        segment_offset = trb - seg->trbs;
-       if (segment_offset > TRBS_PER_SEGMENT)
+       if (segment_offset >= TRBS_PER_SEGMENT)
                return 0;
        return seg->dma + (segment_offset * sizeof(*trb));
 }
@@ -145,25 +145,34 @@ static void next_trb(struct xhci_hcd *xhci,
  */
 static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
 {
-       union xhci_trb *next = ++(ring->dequeue);
        unsigned long long addr;
 
        ring->deq_updates++;
-       /* Update the dequeue pointer further if that was a link TRB or we're at
-        * the end of an event ring segment (which doesn't have link TRBS)
-        */
-       while (last_trb(xhci, ring, ring->deq_seg, next)) {
-               if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
-                       ring->cycle_state = (ring->cycle_state ? 0 : 1);
-                       if (!in_interrupt())
-                               xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
-                                               ring,
-                                               (unsigned int) ring->cycle_state);
+
+       do {
+               /*
+                * Update the dequeue pointer further if that was a link TRB or
+                * we're at the end of an event ring segment (which doesn't have
+                * link TRBS)
+                */
+               if (last_trb(xhci, ring, ring->deq_seg, ring->dequeue)) {
+                       if (consumer && last_trb_on_last_seg(xhci, ring,
+                                               ring->deq_seg, ring->dequeue)) {
+                               if (!in_interrupt())
+                                       xhci_dbg(xhci, "Toggle cycle state "
+                                                       "for ring %p = %i\n",
+                                                       ring,
+                                                       (unsigned int)
+                                                       ring->cycle_state);
+                               ring->cycle_state = (ring->cycle_state ? 0 : 1);
+                       }
+                       ring->deq_seg = ring->deq_seg->next;
+                       ring->dequeue = ring->deq_seg->trbs;
+               } else {
+                       ring->dequeue++;
                }
-               ring->deq_seg = ring->deq_seg->next;
-               ring->dequeue = ring->deq_seg->trbs;
-               next = ring->dequeue;
-       }
+       } while (last_trb(xhci, ring, ring->deq_seg, ring->dequeue));
+
        addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
 }
 
@@ -300,12 +309,132 @@ static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
 /* Ring the host controller doorbell after placing a command on the ring */
 void xhci_ring_cmd_db(struct xhci_hcd *xhci)
 {
+       if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING))
+               return;
+
        xhci_dbg(xhci, "// Ding dong!\n");
        xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
        /* Flush PCI posted writes */
        xhci_readl(xhci, &xhci->dba->doorbell[0]);
 }
 
+static int xhci_abort_cmd_ring(struct xhci_hcd *xhci)
+{
+       u64 temp_64;
+       int ret;
+
+       xhci_dbg(xhci, "Abort command ring\n");
+
+       if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) {
+               xhci_dbg(xhci, "The command ring isn't running, "
+                               "Have the command ring been stopped?\n");
+               return 0;
+       }
+
+       temp_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
+       if (!(temp_64 & CMD_RING_RUNNING)) {
+               xhci_dbg(xhci, "Command ring had been stopped\n");
+               return 0;
+       }
+       xhci->cmd_ring_state = CMD_RING_STATE_ABORTED;
+       xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
+                       &xhci->op_regs->cmd_ring);
+
+       /* Section 4.6.1.2 of xHCI 1.0 spec says software should
+        * time the completion od all xHCI commands, including
+        * the Command Abort operation. If software doesn't see
+        * CRR negated in a timely manner (e.g. longer than 5
+        * seconds), then it should assume that the there are
+        * larger problems with the xHC and assert HCRST.
+        */
+       ret = handshake(xhci, &xhci->op_regs->cmd_ring,
+                       CMD_RING_RUNNING, 0, 5 * 1000 * 1000);
+       if (ret < 0) {
+               /* we are about to kill xhci, give it one more chance */
+               xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
+                             &xhci->op_regs->cmd_ring);
+               udelay(1000);
+               ret = handshake(xhci, &xhci->op_regs->cmd_ring,
+                               CMD_RING_RUNNING, 0, 3 * 1000 * 1000);
+               if (ret == 0)
+                       return 0;
+
+               xhci_err(xhci, "Stopped the command ring failed, "
+                               "maybe the host is dead\n");
+               xhci->xhc_state |= XHCI_STATE_DYING;
+               xhci_quiesce(xhci);
+               xhci_halt(xhci);
+               return -ESHUTDOWN;
+       }
+
+       return 0;
+}
+
+static int xhci_queue_cd(struct xhci_hcd *xhci,
+               struct xhci_command *command,
+               union xhci_trb *cmd_trb)
+{
+       struct xhci_cd *cd;
+       cd = kzalloc(sizeof(struct xhci_cd), GFP_ATOMIC);
+       if (!cd)
+               return -ENOMEM;
+       INIT_LIST_HEAD(&cd->cancel_cmd_list);
+
+       cd->command = command;
+       cd->cmd_trb = cmd_trb;
+       list_add_tail(&cd->cancel_cmd_list, &xhci->cancel_cmd_list);
+
+       return 0;
+}
+
+/*
+ * Cancel the command which has issue.
+ *
+ * Some commands may hang due to waiting for acknowledgement from
+ * usb device. It is outside of the xHC's ability to control and
+ * will cause the command ring is blocked. When it occurs software
+ * should intervene to recover the command ring.
+ * See Section 4.6.1.1 and 4.6.1.2
+ */
+int xhci_cancel_cmd(struct xhci_hcd *xhci, struct xhci_command *command,
+               union xhci_trb *cmd_trb)
+{
+       int retval = 0;
+       unsigned long flags;
+
+       spin_lock_irqsave(&xhci->lock, flags);
+
+       if (xhci->xhc_state & XHCI_STATE_DYING) {
+               xhci_warn(xhci, "Abort the command ring,"
+                               " but the xHCI is dead.\n");
+               retval = -ESHUTDOWN;
+               goto fail;
+       }
+
+       /* queue the cmd desriptor to cancel_cmd_list */
+       retval = xhci_queue_cd(xhci, command, cmd_trb);
+       if (retval) {
+               xhci_warn(xhci, "Queuing command descriptor failed.\n");
+               goto fail;
+       }
+
+       /* abort command ring */
+       retval = xhci_abort_cmd_ring(xhci);
+       if (retval) {
+               xhci_err(xhci, "Abort command ring failed\n");
+               if (unlikely(retval == -ESHUTDOWN)) {
+                       spin_unlock_irqrestore(&xhci->lock, flags);
+                       usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
+                       xhci_dbg(xhci, "xHCI host controller is dead.\n");
+                       return retval;
+               }
+       }
+
+fail:
+       spin_unlock_irqrestore(&xhci->lock, flags);
+       return retval;
+}
+
 void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
                unsigned int slot_id,
                unsigned int ep_index,
@@ -343,7 +472,7 @@ static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
 
        /* A ring has pending URBs if its TD list is not empty */
        if (!(ep->ep_state & EP_HAS_STREAMS)) {
-               if (!(list_empty(&ep->ring->td_list)))
+               if (ep->ring && !(list_empty(&ep->ring->td_list)))
                        xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
                return;
        }
@@ -450,10 +579,14 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
                struct xhci_dequeue_state *state)
 {
        struct xhci_virt_device *dev = xhci->devs[slot_id];
+       struct xhci_virt_ep *ep = &dev->eps[ep_index];
        struct xhci_ring *ep_ring;
-       struct xhci_generic_trb *trb;
-       struct xhci_ep_ctx *ep_ctx;
+       struct xhci_segment *new_seg;
+       union xhci_trb *new_deq;
        dma_addr_t addr;
+       u64 hw_dequeue;
+       bool cycle_found = false;
+       bool td_last_trb_found = false;
 
        ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
                        ep_index, stream_id);
@@ -463,52 +596,63 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
                                stream_id);
                return;
        }
-       state->new_cycle_state = 0;
-       xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
-       state->new_deq_seg = find_trb_seg(cur_td->start_seg,
-                       dev->eps[ep_index].stopped_trb,
-                       &state->new_cycle_state);
-       if (!state->new_deq_seg) {
-               WARN_ON(1);
-               return;
-       }
 
        /* Dig out the cycle state saved by the xHC during the stop ep cmd */
        xhci_dbg(xhci, "Finding endpoint context\n");
-       ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
-       state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
-
-       state->new_deq_ptr = cur_td->last_trb;
-       xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
-       state->new_deq_seg = find_trb_seg(state->new_deq_seg,
-                       state->new_deq_ptr,
-                       &state->new_cycle_state);
-       if (!state->new_deq_seg) {
-               WARN_ON(1);
-               return;
+       /* 4.6.9 the css flag is written to the stream context for streams */
+       if (ep->ep_state & EP_HAS_STREAMS) {
+               struct xhci_stream_ctx *ctx =
+                       &ep->stream_info->stream_ctx_array[stream_id];
+               hw_dequeue = le64_to_cpu(ctx->stream_ring);
+       } else {
+               struct xhci_ep_ctx *ep_ctx
+                       = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
+               hw_dequeue = le64_to_cpu(ep_ctx->deq);
        }
 
-       trb = &state->new_deq_ptr->generic;
-       if (TRB_TYPE_LINK_LE32(trb->field[3]) &&
-           (trb->field[3] & cpu_to_le32(LINK_TOGGLE)))
-               state->new_cycle_state ^= 0x1;
-       next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
+       new_seg = ep_ring->deq_seg;
+       new_deq = ep_ring->dequeue;
+       state->new_cycle_state = hw_dequeue & 0x1;
 
        /*
-        * If there is only one segment in a ring, find_trb_seg()'s while loop
-        * will not run, and it will return before it has a chance to see if it
-        * needs to toggle the cycle bit.  It can't tell if the stalled transfer
-        * ended just before the link TRB on a one-segment ring, or if the TD
-        * wrapped around the top of the ring, because it doesn't have the TD in
-        * question.  Look for the one-segment case where stalled TRB's address
-        * is greater than the new dequeue pointer address.
+        * We want to find the pointer, segment and cycle state of the new trb
+        * (the one after current TD's last_trb). We know the cycle state at
+        * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are
+        * found.
         */
-       if (ep_ring->first_seg == ep_ring->first_seg->next &&
-                       state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
-               state->new_cycle_state ^= 0x1;
-       xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
+       do {
+               if (!cycle_found && xhci_trb_virt_to_dma(new_seg, new_deq)
+                   == (dma_addr_t)(hw_dequeue & ~0xf)) {
+                       cycle_found = true;
+                       if (td_last_trb_found)
+                               break;
+               }
+               if (new_deq == cur_td->last_trb)
+                       td_last_trb_found = true;
+
+               if (cycle_found &&
+                   TRB_TYPE_LINK_LE32(new_deq->generic.field[3]) &&
+                   new_deq->generic.field[3] & cpu_to_le32(LINK_TOGGLE))
+                       state->new_cycle_state ^= 0x1;
+
+               next_trb(xhci, ep_ring, &new_seg, &new_deq);
+
+               /* Search wrapped around, bail out */
+               if (new_deq == ep->ring->dequeue) {
+                       xhci_err(xhci, "Error: Failed finding new dequeue state\n");
+                       state->new_deq_seg = NULL;
+                       state->new_deq_ptr = NULL;
+                       return;
+               }
+
+       } while (!cycle_found || !td_last_trb_found);
+
+       state->new_deq_seg = new_seg;
+       state->new_deq_ptr = new_deq;
 
        /* Don't update the ring cycle state for the producer (us). */
+       xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
+
        xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
                        state->new_deq_seg);
        addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
@@ -693,7 +837,6 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
        if (list_empty(&ep->cancelled_td_list)) {
                xhci_stop_watchdog_timer_in_irq(xhci, ep);
                ep->stopped_td = NULL;
-               ep->stopped_trb = NULL;
                ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
                return;
        }
@@ -759,8 +902,10 @@ remove_finished_td:
                /* Otherwise ring the doorbell(s) to restart queued transfers */
                ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
        }
-       ep->stopped_td = NULL;
-       ep->stopped_trb = NULL;
+
+       /* Clear stopped_td if endpoint is not halted */
+       if (!(ep->ep_state & EP_HALTED))
+               ep->stopped_td = NULL;
 
        /*
         * Drop the lock and complete the URBs in the cancelled TD list.
@@ -1028,12 +1173,25 @@ static void handle_reset_ep_completion(struct xhci_hcd *xhci,
                                false);
                xhci_ring_cmd_db(xhci);
        } else {
-               /* Clear our internal halted state and restart the ring(s) */
+               /* Clear our internal halted state */
                xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
-               ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
        }
 }
 
+/* Complete the command and detele it from the devcie's command queue.
+ */
+static void xhci_complete_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
+               struct xhci_command *command, u32 status)
+{
+       command->status = status;
+       list_del(&command->cmd_list);
+       if (command->completion)
+               complete(command->completion);
+       else
+               xhci_free_command(xhci, command);
+}
+
+
 /* Check to see if a command in the device's command queue matches this one.
  * Signal the completion or free the command, and return 1.  Return 0 if the
  * completed command isn't at the head of the command list.
@@ -1052,15 +1210,155 @@ static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
        if (xhci->cmd_ring->dequeue != command->command_trb)
                return 0;
 
-       command->status = GET_COMP_CODE(le32_to_cpu(event->status));
-       list_del(&command->cmd_list);
-       if (command->completion)
-               complete(command->completion);
-       else
-               xhci_free_command(xhci, command);
+       xhci_complete_cmd_in_cmd_wait_list(xhci, command,
+                       GET_COMP_CODE(le32_to_cpu(event->status)));
        return 1;
 }
 
+/*
+ * Finding the command trb need to be cancelled and modifying it to
+ * NO OP command. And if the command is in device's command wait
+ * list, finishing and freeing it.
+ *
+ * If we can't find the command trb, we think it had already been
+ * executed.
+ */
+static void xhci_cmd_to_noop(struct xhci_hcd *xhci, struct xhci_cd *cur_cd)
+{
+       struct xhci_segment *cur_seg;
+       union xhci_trb *cmd_trb;
+       u32 cycle_state;
+
+       if (xhci->cmd_ring->dequeue == xhci->cmd_ring->enqueue)
+               return;
+
+       /* find the current segment of command ring */
+       cur_seg = find_trb_seg(xhci->cmd_ring->first_seg,
+                       xhci->cmd_ring->dequeue, &cycle_state);
+
+       if (!cur_seg) {
+               xhci_warn(xhci, "Command ring mismatch, dequeue = %p %llx (dma)\n",
+                               xhci->cmd_ring->dequeue,
+                               (unsigned long long)
+                               xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
+                                       xhci->cmd_ring->dequeue));
+               xhci_debug_ring(xhci, xhci->cmd_ring);
+               xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
+               return;
+       }
+
+       /* find the command trb matched by cd from command ring */
+       for (cmd_trb = xhci->cmd_ring->dequeue;
+                       cmd_trb != xhci->cmd_ring->enqueue;
+                       next_trb(xhci, xhci->cmd_ring, &cur_seg, &cmd_trb)) {
+               /* If the trb is link trb, continue */
+               if (TRB_TYPE_LINK_LE32(cmd_trb->generic.field[3]))
+                       continue;
+
+               if (cur_cd->cmd_trb == cmd_trb) {
+
+                       /* If the command in device's command list, we should
+                        * finish it and free the command structure.
+                        */
+                       if (cur_cd->command)
+                               xhci_complete_cmd_in_cmd_wait_list(xhci,
+                                       cur_cd->command, COMP_CMD_STOP);
+
+                       /* get cycle state from the origin command trb */
+                       cycle_state = le32_to_cpu(cmd_trb->generic.field[3])
+                               & TRB_CYCLE;
+
+                       /* modify the command trb to NO OP command */
+                       cmd_trb->generic.field[0] = 0;
+                       cmd_trb->generic.field[1] = 0;
+                       cmd_trb->generic.field[2] = 0;
+                       cmd_trb->generic.field[3] = cpu_to_le32(
+                                       TRB_TYPE(TRB_CMD_NOOP) | cycle_state);
+                       break;
+               }
+       }
+}
+
+static void xhci_cancel_cmd_in_cd_list(struct xhci_hcd *xhci)
+{
+       struct xhci_cd *cur_cd, *next_cd;
+
+       if (list_empty(&xhci->cancel_cmd_list))
+               return;
+
+       list_for_each_entry_safe(cur_cd, next_cd,
+                       &xhci->cancel_cmd_list, cancel_cmd_list) {
+               xhci_cmd_to_noop(xhci, cur_cd);
+               list_del(&cur_cd->cancel_cmd_list);
+               kfree(cur_cd);
+       }
+}
+
+/*
+ * traversing the cancel_cmd_list. If the command descriptor according
+ * to cmd_trb is found, the function free it and return 1, otherwise
+ * return 0.
+ */
+static int xhci_search_cmd_trb_in_cd_list(struct xhci_hcd *xhci,
+               union xhci_trb *cmd_trb)
+{
+       struct xhci_cd *cur_cd, *next_cd;
+
+       if (list_empty(&xhci->cancel_cmd_list))
+               return 0;
+
+       list_for_each_entry_safe(cur_cd, next_cd,
+                       &xhci->cancel_cmd_list, cancel_cmd_list) {
+               if (cur_cd->cmd_trb == cmd_trb) {
+                       if (cur_cd->command)
+                               xhci_complete_cmd_in_cmd_wait_list(xhci,
+                                       cur_cd->command, COMP_CMD_STOP);
+                       list_del(&cur_cd->cancel_cmd_list);
+                       kfree(cur_cd);
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+/*
+ * If the cmd_trb_comp_code is COMP_CMD_ABORT, we just check whether the
+ * trb pointed by the command ring dequeue pointer is the trb we want to
+ * cancel or not. And if the cmd_trb_comp_code is COMP_CMD_STOP, we will
+ * traverse the cancel_cmd_list to trun the all of the commands according
+ * to command descriptor to NO-OP trb.
+ */
+static int handle_stopped_cmd_ring(struct xhci_hcd *xhci,
+               int cmd_trb_comp_code)
+{
+       int cur_trb_is_good = 0;
+
+       /* Searching the cmd trb pointed by the command ring dequeue
+        * pointer in command descriptor list. If it is found, free it.
+        */
+       cur_trb_is_good = xhci_search_cmd_trb_in_cd_list(xhci,
+                       xhci->cmd_ring->dequeue);
+
+       if (cmd_trb_comp_code == COMP_CMD_ABORT)
+               xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
+       else if (cmd_trb_comp_code == COMP_CMD_STOP) {
+               /* traversing the cancel_cmd_list and canceling
+                * the command according to command descriptor
+                */
+               xhci_cancel_cmd_in_cd_list(xhci);
+
+               xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
+               /*
+                * ring command ring doorbell again to restart the
+                * command ring
+                */
+               if (xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue)
+                       xhci_ring_cmd_db(xhci);
+       }
+       return cur_trb_is_good;
+}
+
 static void handle_cmd_completion(struct xhci_hcd *xhci,
                struct xhci_event_cmd *event)
 {
@@ -1086,6 +1384,22 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
                xhci->error_bitmask |= 1 << 5;
                return;
        }
+
+       if ((GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_CMD_ABORT) ||
+               (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_CMD_STOP)) {
+               /* If the return value is 0, we think the trb pointed by
+                * command ring dequeue pointer is a good trb. The good
+                * trb means we don't want to cancel the trb, but it have
+                * been stopped by host. So we should handle it normally.
+                * Otherwise, driver should invoke inc_deq() and return.
+                */
+               if (handle_stopped_cmd_ring(xhci,
+                               GET_COMP_CODE(le32_to_cpu(event->status)))) {
+                       inc_deq(xhci, xhci->cmd_ring, false);
+                       return;
+               }
+       }
+
        switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])
                & TRB_TYPE_BITMASK) {
        case TRB_TYPE(TRB_ENABLE_SLOT):
@@ -1215,6 +1529,7 @@ static void handle_vendor_event(struct xhci_hcd *xhci,
  *
  * Returns a zero-based port number, which is suitable for indexing into each of
  * the split roothubs' port arrays and bus state arrays.
+ * Add one to it in order to call xhci_find_slot_id_by_port.
  */
 static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
                struct xhci_hcd *xhci, u32 port_id)
@@ -1335,7 +1650,7 @@ static void handle_port_status(struct xhci_hcd *xhci,
                        xhci_set_link_state(xhci, port_array, faked_port_index,
                                                XDEV_U0);
                        slot_id = xhci_find_slot_id_by_port(hcd, xhci,
-                                       faked_port_index);
+                                       faked_port_index + 1);
                        if (!slot_id) {
                                xhci_dbg(xhci, "slot_id is zero\n");
                                goto cleanup;
@@ -1370,6 +1685,15 @@ cleanup:
        if (bogus_port_status)
                return;
 
+       /*
+        * xHCI port-status-change events occur when the "or" of all the
+        * status-change bits in the portsc register changes from 0 to 1.
+        * New status changes won't cause an event if any other change
+        * bits are still set.  When an event occurs, switch over to
+        * polling to avoid losing status changes.
+        */
+       xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
+       set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
        spin_unlock(&xhci->lock);
        /* Pass this up to the core */
        usb_hcd_poll_rh_status(hcd);
@@ -1440,14 +1764,12 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
        struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
        ep->ep_state |= EP_HALTED;
        ep->stopped_td = td;
-       ep->stopped_trb = event_trb;
        ep->stopped_stream = stream_id;
 
        xhci_queue_reset_ep(xhci, slot_id, ep_index);
        xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
 
        ep->stopped_td = NULL;
-       ep->stopped_trb = NULL;
        ep->stopped_stream = 0;
 
        xhci_ring_cmd_db(xhci);
@@ -1529,26 +1851,15 @@ static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
                 * the ring dequeue pointer or take this TD off any lists yet.
                 */
                ep->stopped_td = td;
-               ep->stopped_trb = event_trb;
                return 0;
        } else {
-               if (trb_comp_code == COMP_STALL) {
-                       /* The transfer is completed from the driver's
-                        * perspective, but we need to issue a set dequeue
-                        * command for this stalled endpoint to move the dequeue
-                        * pointer past the TD.  We can't do that here because
-                        * the halt condition must be cleared first.  Let the
-                        * USB class driver clear the stall later.
-                        */
-                       ep->stopped_td = td;
-                       ep->stopped_trb = event_trb;
-                       ep->stopped_stream = ep_ring->stream_id;
-               } else if (xhci_requires_manual_halt_cleanup(xhci,
-                                       ep_ctx, trb_comp_code)) {
-                       /* Other types of errors halt the endpoint, but the
-                        * class driver doesn't call usb_reset_endpoint() unless
-                        * the error is -EPIPE.  Clear the halted status in the
-                        * xHCI hardware manually.
+               if (trb_comp_code == COMP_STALL ||
+                   xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
+                                                     trb_comp_code)) {
+                       /* Issue a reset endpoint command to clear the host side
+                        * halt, followed by a set dequeue command to move the
+                        * dequeue pointer past the TD.
+                        * The class driver clears the device side halt later.
                         */
                        xhci_cleanup_halted_endpoint(xhci,
                                        slot_id, ep_index, ep_ring->stream_id,
@@ -1643,7 +1954,6 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
                }
                break;
        case COMP_SHORT_TX:
-               xhci_warn(xhci, "WARN: short transfer on control ep\n");
                if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
                        *status = -EREMOTEIO;
                else
@@ -1665,14 +1975,12 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
                if (event_trb != ep_ring->dequeue &&
                                event_trb != td->last_trb)
                        td->urb->actual_length =
-                               td->urb->transfer_buffer_length
-                               TRB_LEN(le32_to_cpu(event->transfer_len));
+                               td->urb->transfer_buffer_length -
+                               EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
                else
                        td->urb->actual_length = 0;
 
-               xhci_cleanup_halted_endpoint(xhci,
-                       slot_id, ep_index, 0, td, event_trb);
-               return finish_td(xhci, td, event_trb, event, ep, status, true);
+               return finish_td(xhci, td, event_trb, event, ep, status, false);
        }
        /*
         * Did we transfer any data, despite the errors that might have
@@ -1681,7 +1989,7 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
        if (event_trb != ep_ring->dequeue) {
                /* The event was for the status stage */
                if (event_trb == td->last_trb) {
-                       if (td->urb->actual_length != 0) {
+                       if (td->urb_length_set) {
                                /* Don't overwrite a previously set error code
                                 */
                                if ((*status == -EINPROGRESS || *status == 0) &&
@@ -1695,10 +2003,16 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
                                        td->urb->transfer_buffer_length;
                        }
                } else {
-               /* Maybe the event was for the data stage? */
+                       /*
+                        * Maybe the event was for the data stage? If so, update
+                        * already the actual_length of the URB and flag it as
+                        * set, so that it is not overwritten in the event for
+                        * the last TRB.
+                        */
+                       td->urb_length_set = true;
                        td->urb->actual_length =
                                td->urb->transfer_buffer_length -
-                               TRB_LEN(le32_to_cpu(event->transfer_len));
+                               EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
                        xhci_dbg(xhci, "Waiting for status "
                                        "stage event\n");
                        return 0;
@@ -1734,8 +2048,12 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
        /* handle completion code */
        switch (trb_comp_code) {
        case COMP_SUCCESS:
-               frame->status = 0;
-               break;
+               if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0) {
+                       frame->status = 0;
+                       break;
+               }
+               if ((xhci->quirks & XHCI_TRUST_TX_LENGTH))
+                       trb_comp_code = COMP_SHORT_TX;
        case COMP_SHORT_TX:
                frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
                                -EREMOTEIO : 0;
@@ -1754,6 +2072,12 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
                frame->status = -EPROTO;
                skip_td = true;
                break;
+       case COMP_TX_ERR:
+               frame->status = -EPROTO;
+               if (event_trb != td->last_trb)
+                       return 0;
+               skip_td = true;
+               break;
        case COMP_STOP:
        case COMP_STOP_INVAL:
                break;
@@ -1774,7 +2098,7 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
                                len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
                }
                len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
-                       TRB_LEN(le32_to_cpu(event->transfer_len));
+                       EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
 
                if (trb_comp_code != COMP_STOP_INVAL) {
                        frame->actual_length = len;
@@ -1831,13 +2155,16 @@ static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
        switch (trb_comp_code) {
        case COMP_SUCCESS:
                /* Double check that the HW transferred everything. */
-               if (event_trb != td->last_trb) {
+               if (event_trb != td->last_trb ||
+                   EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
                        xhci_warn(xhci, "WARN Successful completion "
                                        "on short TX\n");
                        if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
                                *status = -EREMOTEIO;
                        else
                                *status = 0;
+                       if ((xhci->quirks & XHCI_TRUST_TX_LENGTH))
+                               trb_comp_code = COMP_SHORT_TX;
                } else {
                        *status = 0;
                }
@@ -1857,18 +2184,18 @@ static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
                                "%d bytes untransferred\n",
                                td->urb->ep->desc.bEndpointAddress,
                                td->urb->transfer_buffer_length,
-                               TRB_LEN(le32_to_cpu(event->transfer_len)));
+                               EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
        /* Fast path - was this the last TRB in the TD for this URB? */
        if (event_trb == td->last_trb) {
-               if (TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
+               if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
                        td->urb->actual_length =
                                td->urb->transfer_buffer_length -
-                               TRB_LEN(le32_to_cpu(event->transfer_len));
+                               EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
                        if (td->urb->transfer_buffer_length <
                                        td->urb->actual_length) {
                                xhci_warn(xhci, "HC gave bad length "
                                                "of %d bytes left\n",
-                                         TRB_LEN(le32_to_cpu(event->transfer_len)));
+                                         EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
                                td->urb->actual_length = 0;
                                if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
                                        *status = -EREMOTEIO;
@@ -1910,7 +2237,7 @@ static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
                if (trb_comp_code != COMP_STOP_INVAL)
                        td->urb->actual_length +=
                                TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
-                               TRB_LEN(le32_to_cpu(event->transfer_len));
+                               EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
        }
 
        return finish_td(xhci, td, event_trb, event, ep, status, false);
@@ -1976,6 +2303,13 @@ static int handle_tx_event(struct xhci_hcd *xhci,
         * transfer type
         */
        case COMP_SUCCESS:
+               if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0)
+                       break;
+               if (xhci->quirks & XHCI_TRUST_TX_LENGTH)
+                       trb_comp_code = COMP_SHORT_TX;
+               else
+                       xhci_warn(xhci, "WARN Successful completion on short TX: "
+                                       "needs XHCI_TRUST_TX_LENGTH quirk?\n");
        case COMP_SHORT_TX:
                break;
        case COMP_STOP:
@@ -1985,7 +2319,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
                break;
        case COMP_STALL:
-               xhci_warn(xhci, "WARN: Stalled endpoint\n");
+               xhci_dbg(xhci, "Stalled endpoint\n");
                ep->ep_state |= EP_HALTED;
                status = -EPIPE;
                break;
@@ -1995,11 +2329,11 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                break;
        case COMP_SPLIT_ERR:
        case COMP_TX_ERR:
-               xhci_warn(xhci, "WARN: transfer error on endpoint\n");
+               xhci_dbg(xhci, "Transfer error on endpoint\n");
                status = -EPROTO;
                break;
        case COMP_BABBLE:
-               xhci_warn(xhci, "WARN: babble error on endpoint\n");
+               xhci_dbg(xhci, "Babble error on endpoint\n");
                status = -EOVERFLOW;
                break;
        case COMP_DB_ERR:
@@ -2062,14 +2396,21 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                 * TD list.
                 */
                if (list_empty(&ep_ring->td_list)) {
-                       xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
-                                       "with no TDs queued?\n",
-                                 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
-                                 ep_index);
-                       xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
-                                (le32_to_cpu(event->flags) &
-                                 TRB_TYPE_BITMASK)>>10);
-                       xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
+                       /*
+                        * A stopped endpoint may generate an extra completion
+                        * event if the device was suspended.  Don't print
+                        * warnings.
+                        */
+                       if (!(trb_comp_code == COMP_STOP ||
+                                               trb_comp_code == COMP_STOP_INVAL)) {
+                               xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
+                                               TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
+                                               ep_index);
+                               xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
+                                               (le32_to_cpu(event->flags) &
+                                                TRB_TYPE_BITMASK)>>10);
+                               xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
+                       }
                        if (ep->skip) {
                                ep->skip = false;
                                xhci_dbg(xhci, "td_list is empty while skip "
@@ -2104,7 +2445,8 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                 * last TRB of the previous TD. The command completion handle
                 * will take care the rest.
                 */
-               if (!event_seg && trb_comp_code == COMP_STOP_INVAL) {
+               if (!event_seg && (trb_comp_code == COMP_STOP ||
+                                  trb_comp_code == COMP_STOP_INVAL)) {
                        ret = 0;
                        goto cleanup;
                }
@@ -2181,15 +2523,8 @@ cleanup:
                if (ret) {
                        urb = td->urb;
                        urb_priv = urb->hcpriv;
-                       /* Leave the TD around for the reset endpoint function
-                        * to use(but only if it's not a control endpoint,
-                        * since we already queued the Set TR dequeue pointer
-                        * command for stalled control endpoints).
-                        */
-                       if (usb_endpoint_xfer_control(&urb->ep->desc) ||
-                               (trb_comp_code != COMP_STALL &&
-                                       trb_comp_code != COMP_BABBLE))
-                               xhci_urb_free_priv(xhci, urb_priv);
+
+                       xhci_urb_free_priv(xhci, urb_priv);
 
                        usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
                        if ((urb->actual_length != urb->transfer_buffer_length &&
@@ -2325,7 +2660,7 @@ irqreturn_t xhci_irq(struct usb_hcd *hcd)
                xhci_halt(xhci);
 hw_died:
                spin_unlock(&xhci->lock);
-               return -ESHUTDOWN;
+               return IRQ_HANDLED;
        }
 
        /*
@@ -2342,7 +2677,7 @@ hw_died:
                u32 irq_pending;
                /* Acknowledge the PCI interrupt */
                irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
-               irq_pending |= 0x3;
+               irq_pending |= IMAN_IP;
                xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
        }
 
@@ -2561,7 +2896,7 @@ static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
        struct scatterlist *sg;
 
        sg = NULL;
-       num_sgs = urb->num_sgs;
+       num_sgs = urb->num_mapped_sgs;
        temp = urb->transfer_buffer_length;
 
        xhci_dbg(xhci, "count sg list trbs: \n");
@@ -2690,11 +3025,11 @@ static u32 xhci_td_remainder(unsigned int remainder)
 }
 
 /*
- * For xHCI 1.0 host controllers, TD size is the number of packets remaining in
- * the TD (*not* including this TRB).
+ * For xHCI 1.0 host controllers, TD size is the number of max packet sized
+ * packets remaining in the TD (*not* including this TRB).
  *
  * Total TD packet count = total_packet_count =
- *     roundup(TD size in bytes / wMaxPacketSize)
+ *     DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
  *
  * Packets transferred up to and including this TRB = packets_transferred =
  *     rounddown(total bytes transferred including this TRB / wMaxPacketSize)
@@ -2702,24 +3037,27 @@ static u32 xhci_td_remainder(unsigned int remainder)
  * TD size = total_packet_count - packets_transferred
  *
  * It must fit in bits 21:17, so it can't be bigger than 31.
+ * The last TRB in a TD must have the TD size set to zero.
  */
-
 static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
-               unsigned int total_packet_count, struct urb *urb)
+               unsigned int total_packet_count, struct urb *urb,
+               unsigned int num_trbs_left)
 {
        int packets_transferred;
 
        /* One TRB with a zero-length data packet. */
-       if (running_total == 0 && trb_buff_len == 0)
+       if (num_trbs_left == 0 || (running_total == 0 && trb_buff_len == 0))
                return 0;
 
        /* All the TRB queueing functions don't count the current TRB in
         * running_total.
         */
        packets_transferred = (running_total + trb_buff_len) /
-               usb_endpoint_maxp(&urb->ep->desc);
+               GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
 
-       return xhci_td_remainder(total_packet_count - packets_transferred);
+       if ((total_packet_count - packets_transferred) > 31)
+               return 31 << 17;
+       return (total_packet_count - packets_transferred) << 17;
 }
 
 static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
@@ -2745,8 +3083,8 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                return -EINVAL;
 
        num_trbs = count_sg_trbs_needed(xhci, urb);
-       num_sgs = urb->num_sgs;
-       total_packet_count = roundup(urb->transfer_buffer_length,
+       num_sgs = urb->num_mapped_sgs;
+       total_packet_count = DIV_ROUND_UP(urb->transfer_buffer_length,
                        usb_endpoint_maxp(&urb->ep->desc));
 
        trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
@@ -2836,7 +3174,8 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                                        running_total);
                } else {
                        remainder = xhci_v1_0_td_remainder(running_total,
-                                       trb_buff_len, total_packet_count, urb);
+                                       trb_buff_len, total_packet_count, urb,
+                                       num_trbs - 1);
                }
                length_field = TRB_LEN(trb_buff_len) |
                        remainder |
@@ -2953,7 +3292,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
        start_cycle = ep_ring->cycle_state;
 
        running_total = 0;
-       total_packet_count = roundup(urb->transfer_buffer_length,
+       total_packet_count = DIV_ROUND_UP(urb->transfer_buffer_length,
                        usb_endpoint_maxp(&urb->ep->desc));
        /* How much data is in the first TRB? */
        addr = (u64) urb->transfer_dma;
@@ -2999,7 +3338,8 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                                        running_total);
                } else {
                        remainder = xhci_v1_0_td_remainder(running_total,
-                                       trb_buff_len, total_packet_count, urb);
+                                       trb_buff_len, total_packet_count, urb,
+                                       num_trbs - 1);
                }
                length_field = TRB_LEN(trb_buff_len) |
                        remainder |
@@ -3092,8 +3432,8 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
        if (start_cycle == 0)
                field |= 0x1;
 
-       /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
-       if (xhci->hci_version == 0x100) {
+       /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */
+       if (xhci->hci_version >= 0x100) {
                if (urb->transfer_buffer_length > 0) {
                        if (setup->bRequestType & USB_DIR_IN)
                                field |= TRB_TX_TYPE(TRB_DATA_IN);
@@ -3185,7 +3525,7 @@ static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
                return 0;
 
        max_burst = urb->ep->ss_ep_comp.bMaxBurst;
-       return roundup(total_packet_count, max_burst + 1) - 1;
+       return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
 }
 
 /*
@@ -3274,8 +3614,9 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                addr = start_addr + urb->iso_frame_desc[i].offset;
                td_len = urb->iso_frame_desc[i].length;
                td_remain_len = td_len;
-               total_packet_count = roundup(td_len,
-                               usb_endpoint_maxp(&urb->ep->desc));
+               total_packet_count = DIV_ROUND_UP(td_len,
+                               GET_MAX_PACKET(
+                                       usb_endpoint_maxp(&urb->ep->desc)));
                /* A zero-length transfer still involves at least one packet. */
                if (total_packet_count == 0)
                        total_packet_count++;
@@ -3298,9 +3639,11 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                td = urb_priv->td[i];
                for (j = 0; j < trbs_per_td; j++) {
                        u32 remainder = 0;
-                       field = TRB_TBC(burst_count) | TRB_TLBPC(residue);
+                       field = 0;
 
                        if (first_trb) {
+                               field = TRB_TBC(burst_count) |
+                                       TRB_TLBPC(residue);
                                /* Queue the isoc TRB */
                                field |= TRB_TYPE(TRB_ISOC);
                                /* Assume URB_ISO_ASAP is set */
@@ -3331,7 +3674,9 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                        } else {
                                td->last_trb = ep_ring->enqueue;
                                field |= TRB_IOC;
-                               if (xhci->hci_version == 0x100) {
+                               if (xhci->hci_version == 0x100 &&
+                                               !(xhci->quirks &
+                                                       XHCI_AVOID_BEI)) {
                                        /* Set BEI bit except for the last td */
                                        if (i < num_tds - 1)
                                                field |= TRB_BEI;
@@ -3352,7 +3697,8 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                        } else {
                                remainder = xhci_v1_0_td_remainder(
                                                running_total, trb_buff_len,
-                                               total_packet_count, urb);
+                                               total_packet_count, urb,
+                                               (trbs_per_td - j - 1));
                        }
                        length_field = TRB_LEN(trb_buff_len) |
                                remainder |
@@ -3372,7 +3718,8 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
                /* Check TD length */
                if (running_total != td_len) {
                        xhci_err(xhci, "ISOC TD length unmatch\n");
-                       return -EINVAL;
+                       ret = -EINVAL;
+                       goto cleanup;
                }
        }