USB: fix usbmon and DMA mapping for scatter-gather URBs
[safe/jmp/linux-2.6] / drivers / usb / host / xhci-ring.c
index d7e10ea..c1359ed 100644 (file)
@@ -65,6 +65,7 @@
  */
 
 #include <linux/scatterlist.h>
+#include <linux/slab.h>
 #include "xhci.h"
 
 /*
@@ -241,10 +242,27 @@ static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
        int i;
        union xhci_trb *enq = ring->enqueue;
        struct xhci_segment *enq_seg = ring->enq_seg;
+       struct xhci_segment *cur_seg;
+       unsigned int left_on_ring;
 
        /* Check if ring is empty */
-       if (enq == ring->dequeue)
+       if (enq == ring->dequeue) {
+               /* Can't use link trbs */
+               left_on_ring = TRBS_PER_SEGMENT - 1;
+               for (cur_seg = enq_seg->next; cur_seg != enq_seg;
+                               cur_seg = cur_seg->next)
+                       left_on_ring += TRBS_PER_SEGMENT - 1;
+
+               /* Always need one TRB free in the ring. */
+               left_on_ring -= 1;
+               if (num_trbs > left_on_ring) {
+                       xhci_warn(xhci, "Not enough room on ring; "
+                                       "need %u TRBs, %u TRBs left\n",
+                                       num_trbs, left_on_ring);
+                       return 0;
+               }
                return 1;
+       }
        /* Make sure there's an extra empty TRB available */
        for (i = 0; i <= num_trbs; ++i) {
                if (enq == ring->dequeue)
@@ -577,6 +595,8 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
                /* Otherwise just ring the doorbell to restart the ring */
                ring_ep_doorbell(xhci, slot_id, ep_index);
        }
+       ep->stopped_td = NULL;
+       ep->stopped_trb = NULL;
 
        /*
         * Drop the lock and complete the URBs in the cancelled TD list.
@@ -903,28 +923,32 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
                                virt_dev->in_ctx);
                /* Input ctx add_flags are the endpoint index plus one */
                ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
-               ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
-               if (!ep_ring) {
-                       /* This must have been an initial configure endpoint */
-                       xhci->devs[slot_id]->cmd_status =
-                               GET_COMP_CODE(event->status);
-                       complete(&xhci->devs[slot_id]->cmd_completion);
-                       break;
-               }
-               ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
-               xhci_dbg(xhci, "Completed config ep cmd - last ep index = %d, "
-                               "state = %d\n", ep_index, ep_state);
+               /* A usb_set_interface() call directly after clearing a halted
+                * condition may race on this quirky hardware.
+                * Not worth worrying about, since this is prototype hardware.
+                */
                if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
-                               ep_state & EP_HALTED) {
+                               ep_index != (unsigned int) -1 &&
+                               ctrl_ctx->add_flags - SLOT_FLAG ==
+                                       ctrl_ctx->drop_flags) {
+                       ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
+                       ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
+                       if (!(ep_state & EP_HALTED))
+                               goto bandwidth_change;
+                       xhci_dbg(xhci, "Completed config ep cmd - "
+                                       "last ep index = %d, state = %d\n",
+                                       ep_index, ep_state);
                        /* Clear our internal halted state and restart ring */
                        xhci->devs[slot_id]->eps[ep_index].ep_state &=
                                ~EP_HALTED;
                        ring_ep_doorbell(xhci, slot_id, ep_index);
-               } else {
-                       xhci->devs[slot_id]->cmd_status =
-                               GET_COMP_CODE(event->status);
-                       complete(&xhci->devs[slot_id]->cmd_completion);
+                       break;
                }
+bandwidth_change:
+               xhci_dbg(xhci, "Completed config ep cmd\n");
+               xhci->devs[slot_id]->cmd_status =
+                       GET_COMP_CODE(event->status);
+               complete(&xhci->devs[slot_id]->cmd_completion);
                break;
        case TRB_TYPE(TRB_EVAL_CONTEXT):
                virt_dev = xhci->devs[slot_id];
@@ -949,6 +973,17 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
        case TRB_TYPE(TRB_RESET_EP):
                handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
                break;
+       case TRB_TYPE(TRB_RESET_DEV):
+               xhci_dbg(xhci, "Completed reset device command.\n");
+               slot_id = TRB_TO_SLOT_ID(
+                               xhci->cmd_ring->dequeue->generic.field[3]);
+               virt_dev = xhci->devs[slot_id];
+               if (virt_dev)
+                       handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
+               else
+                       xhci_warn(xhci, "Reset device command completion "
+                                       "for disabled slot %u\n", slot_id);
+               break;
        default:
                /* Skip over unknown commands on the event ring */
                xhci->error_bitmask |= 1 << 6;
@@ -1037,6 +1072,64 @@ struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
        return 0;
 }
 
+static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
+               unsigned int slot_id, unsigned int ep_index,
+               struct xhci_td *td, union xhci_trb *event_trb)
+{
+       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;
+
+       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;
+
+       xhci_ring_cmd_db(xhci);
+}
+
+/* Check if an error has halted the endpoint ring.  The class driver will
+ * cleanup the halt for a non-default control endpoint if we indicate a stall.
+ * However, a babble and other errors also halt the endpoint ring, and the class
+ * driver won't clear the halt in that case, so we need to issue a Set Transfer
+ * Ring Dequeue Pointer command manually.
+ */
+static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
+               struct xhci_ep_ctx *ep_ctx,
+               unsigned int trb_comp_code)
+{
+       /* TRB completion codes that may require a manual halt cleanup */
+       if (trb_comp_code == COMP_TX_ERR ||
+                       trb_comp_code == COMP_BABBLE ||
+                       trb_comp_code == COMP_SPLIT_ERR)
+               /* The 0.96 spec says a babbling control endpoint
+                * is not halted. The 0.96 spec says it is.  Some HW
+                * claims to be 0.95 compliant, but it halts the control
+                * endpoint anyway.  Check if a babble halted the
+                * endpoint.
+                */
+               if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED)
+                       return 1;
+
+       return 0;
+}
+
+int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
+{
+       if (trb_comp_code >= 224 && trb_comp_code <= 255) {
+               /* Vendor defined "informational" completion code,
+                * treat as not-an-error.
+                */
+               xhci_dbg(xhci, "Vendor defined info completion code %u\n",
+                               trb_comp_code);
+               xhci_dbg(xhci, "Treating code as success.\n");
+               return 1;
+       }
+       return 0;
+}
+
 /*
  * If this function returns an error condition, it means it got a Transfer
  * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
@@ -1139,6 +1232,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                xhci_warn(xhci, "WARN: TRB error on endpoint\n");
                status = -EILSEQ;
                break;
+       case COMP_SPLIT_ERR:
        case COMP_TX_ERR:
                xhci_warn(xhci, "WARN: transfer error on endpoint\n");
                status = -EPROTO;
@@ -1152,6 +1246,10 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                status = -ENOSR;
                break;
        default:
+               if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
+                       status = 0;
+                       break;
+               }
                xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
                urb = NULL;
                goto cleanup;
@@ -1180,15 +1278,14 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                        else
                                status = 0;
                        break;
-               case COMP_BABBLE:
-                       /* The 0.96 spec says a babbling control endpoint
-                        * is not halted. The 0.96 spec says it is.  Some HW
-                        * claims to be 0.95 compliant, but it halts the control
-                        * endpoint anyway.  Check if a babble halted the
-                        * endpoint.
-                        */
-                       if (ep_ctx->ep_info != EP_STATE_HALTED)
+
+               default:
+                       if (!xhci_requires_manual_halt_cleanup(xhci,
+                                               ep_ctx, trb_comp_code))
                                break;
+                       xhci_dbg(xhci, "TRB error code %u, "
+                                       "halted endpoint index = %u\n",
+                                       trb_comp_code, ep_index);
                        /* else fall through */
                case COMP_STALL:
                        /* Did we transfer part of the data (middle) phase? */
@@ -1200,15 +1297,9 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                        else
                                td->urb->actual_length = 0;
 
-                       ep->stopped_td = td;
-                       ep->stopped_trb = event_trb;
-                       xhci_queue_reset_ep(xhci, slot_id, ep_index);
-                       xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
-                       xhci_ring_cmd_db(xhci);
+                       xhci_cleanup_halted_endpoint(xhci,
+                                       slot_id, ep_index, td, event_trb);
                        goto td_cleanup;
-               default:
-                       /* Others already handled above */
-                       break;
                }
                /*
                 * Did we transfer any data, despite the errors that might have
@@ -1346,16 +1437,25 @@ static int handle_tx_event(struct xhci_hcd *xhci,
                ep->stopped_td = td;
                ep->stopped_trb = event_trb;
        } else {
-               if (trb_comp_code == COMP_STALL ||
-                               trb_comp_code == COMP_BABBLE) {
+               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.
+                        * 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;
+               } 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.
+                        */
+                       xhci_cleanup_halted_endpoint(xhci,
+                                       slot_id, ep_index, td, event_trb);
                } else {
                        /* Update ring dequeue pointer */
                        while (ep_ring->dequeue != td->last_trb)
@@ -1862,7 +1962,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
        int running_total, trb_buff_len, ret;
        u64 addr;
 
-       if (urb->sg)
+       if (urb->num_sgs)
                return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
 
        ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
@@ -2125,6 +2225,14 @@ int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
                        false);
 }
 
+/* Queue a reset device command TRB */
+int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
+{
+       return queue_command(xhci, 0, 0, 0,
+                       TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
+                       false);
+}
+
 /* Queue a configure endpoint command TRB */
 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
                u32 slot_id, bool command_must_succeed)