]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/usb/host/ehci-sched.c
USB: EHCI: fix bug in Iso scheduling
[linux-2.6-omap-h63xx.git] / drivers / usb / host / ehci-sched.c
index 80d99bce2b38fb3d5a6459cdff8eeb5642fa0b06..b7853c8bac0fb58c3c7f74e18145362b6f2002cd 100644 (file)
@@ -119,7 +119,8 @@ periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
                        q = &q->fstn->fstn_next;
                        break;
                case Q_TYPE_ITD:
-                       usecs += q->itd->usecs [uframe];
+                       if (q->itd->hw_transaction[uframe])
+                               usecs += q->itd->stream->usecs;
                        hw_p = &q->itd->hw_next;
                        q = &q->itd->itd_next;
                        break;
@@ -211,7 +212,7 @@ static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
  * low/fullspeed transfer can "carry over" from one uframe to the next,
  * since the TT just performs downstream transfers in sequence.
  *
- * For example two seperate 100 usec transfers can start in the same uframe,
+ * For example two separate 100 usec transfers can start in the same uframe,
  * and the second one would "carry over" 75 usecs into the next uframe.
  */
 static void
@@ -439,11 +440,10 @@ static int enable_periodic (struct ehci_hcd *ehci)
        /* did clearing PSE did take effect yet?
         * takes effect only at frame boundaries...
         */
-       status = handshake(ehci, &ehci->regs->status, STS_PSS, 0, 9 * 125);
-       if (status != 0) {
-               ehci_to_hcd(ehci)->state = HC_STATE_HALT;
+       status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
+                                            STS_PSS, 0, 9 * 125);
+       if (status)
                return status;
-       }
 
        cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
        ehci_writel(ehci, cmd, &ehci->regs->command);
@@ -464,11 +464,10 @@ static int disable_periodic (struct ehci_hcd *ehci)
        /* did setting PSE not take effect yet?
         * takes effect only at frame boundaries...
         */
-       status = handshake(ehci, &ehci->regs->status, STS_PSS, STS_PSS, 9 * 125);
-       if (status != 0) {
-               ehci_to_hcd(ehci)->state = HC_STATE_HALT;
+       status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
+                                            STS_PSS, STS_PSS, 9 * 125);
+       if (status)
                return status;
-       }
 
        cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE;
        ehci_writel(ehci, cmd, &ehci->regs->command);
@@ -1182,21 +1181,18 @@ itd_urb_transaction (
                                        struct ehci_itd, itd_list);
                        list_del (&itd->itd_list);
                        itd_dma = itd->itd_dma;
-               } else
-                       itd = NULL;
-
-               if (!itd) {
+               } else {
                        spin_unlock_irqrestore (&ehci->lock, flags);
                        itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
                                        &itd_dma);
                        spin_lock_irqsave (&ehci->lock, flags);
+                       if (!itd) {
+                               iso_sched_free(stream, sched);
+                               spin_unlock_irqrestore(&ehci->lock, flags);
+                               return -ENOMEM;
+                       }
                }
 
-               if (unlikely (NULL == itd)) {
-                       iso_sched_free (stream, sched);
-                       spin_unlock_irqrestore (&ehci->lock, flags);
-                       return -ENOMEM;
-               }
                memset (itd, 0, sizeof *itd);
                itd->itd_dma = itd_dma;
                list_add (&itd->itd_list, &sched->td_list);
@@ -1353,18 +1349,27 @@ iso_stream_schedule (
        /* when's the last uframe this urb could start? */
        max = now + mod;
 
-       /* typical case: reuse current schedule. stream is still active,
-        * and no gaps from host falling behind (irq delays etc)
+       /* Typical case: reuse current schedule, stream is still active.
+        * Hopefully there are no gaps from the host falling behind
+        * (irq delays etc), but if there are we'll take the next
+        * slot in the schedule, implicitly assuming URB_ISO_ASAP.
         */
        if (likely (!list_empty (&stream->td_list))) {
                start = stream->next_uframe;
                if (start < now)
                        start += mod;
-               if (likely ((start + sched->span) < max))
-                       goto ready;
-               /* else fell behind; someday, try to reschedule */
-               status = -EL2NSYNC;
-               goto fail;
+
+               /* Fell behind (by up to twice the slop amount)? */
+               if (start >= max - 2 * 8 * SCHEDULE_SLOP)
+                       start += stream->interval * DIV_ROUND_UP(
+                                       max - start, stream->interval) - mod;
+
+               /* Tried to schedule too far into the future? */
+               if (unlikely((start + sched->span) >= max)) {
+                       status = -EFBIG;
+                       goto fail;
+               }
+               goto ready;
        }
 
        /* need to schedule; when's the next (u)frame we could start?
@@ -1536,7 +1541,6 @@ itd_link_urb (
                uframe = next_uframe & 0x07;
                frame = next_uframe >> 3;
 
-               itd->usecs [uframe] = stream->usecs;
                itd_patch(ehci, itd, iso_sched, packet, uframe);
 
                next_uframe += stream->interval;
@@ -1565,6 +1569,16 @@ itd_link_urb (
 
 #define        ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
 
+/* Process and recycle a completed ITD.  Return true iff its urb completed,
+ * and hence its completion callback probably added things to the hardware
+ * schedule.
+ *
+ * Note that we carefully avoid recycling this descriptor until after any
+ * completion callback runs, so that it won't be reused quickly.  That is,
+ * assuming (a) no more than two urbs per frame on this endpoint, and also
+ * (b) only this endpoint's completions submit URBs.  It seems some silicon
+ * corrupts things if you reuse completed descriptors very quickly...
+ */
 static unsigned
 itd_complete (
        struct ehci_hcd *ehci,
@@ -1577,6 +1591,7 @@ itd_complete (
        int                                     urb_index = -1;
        struct ehci_iso_stream                  *stream = itd->stream;
        struct usb_device                       *dev;
+       unsigned                                retval = false;
 
        /* for each uframe with a packet */
        for (uframe = 0; uframe < 8; uframe++) {
@@ -1607,33 +1622,27 @@ itd_complete (
                } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
                        desc->status = 0;
                        desc->actual_length = EHCI_ITD_LENGTH (t);
+               } else {
+                       /* URB was too late */
+                       desc->status = -EXDEV;
                }
        }
 
-       usb_put_urb (urb);
-       itd->urb = NULL;
-       itd->stream = NULL;
-       list_move (&itd->itd_list, &stream->free_list);
-       iso_stream_put (ehci, stream);
-
        /* handle completion now? */
        if (likely ((urb_index + 1) != urb->number_of_packets))
-               return 0;
+               goto done;
 
        /* ASSERT: it's really the last itd for this urb
        list_for_each_entry (itd, &stream->td_list, itd_list)
                BUG_ON (itd->urb == urb);
         */
 
-       /* give urb back to the driver ... can be out-of-order */
+       /* give urb back to the driver; completion often (re)submits */
        dev = urb->dev;
        ehci_urb_done(ehci, urb, 0);
+       retval = true;
        urb = NULL;
-
-       /* defer stopping schedule; completion can submit */
        ehci->periodic_sched--;
-       if (unlikely (!ehci->periodic_sched))
-               (void) disable_periodic (ehci);
        ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
 
        if (unlikely (list_empty (&stream->td_list))) {
@@ -1645,8 +1654,15 @@ itd_complete (
                        (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
        }
        iso_stream_put (ehci, stream);
+       /* OK to recycle this ITD now that its completion callback ran. */
+done:
+       usb_put_urb(urb);
+       itd->urb = NULL;
+       itd->stream = NULL;
+       list_move(&itd->itd_list, &stream->free_list);
+       iso_stream_put(ehci, stream);
 
-       return 1;
+       return retval;
 }
 
 /*-------------------------------------------------------------------------*/
@@ -1673,7 +1689,7 @@ static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
 #ifdef EHCI_URB_TRACE
        ehci_dbg (ehci,
                "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
-               __FUNCTION__, urb->dev->devpath, urb,
+               __func__, urb->dev->devpath, urb,
                usb_pipeendpoint (urb->pipe),
                usb_pipein (urb->pipe) ? "in" : "out",
                urb->transfer_buffer_length,
@@ -1712,8 +1728,6 @@ done:
        return status;
 }
 
-#ifdef CONFIG_USB_EHCI_SPLIT_ISO
-
 /*-------------------------------------------------------------------------*/
 
 /*
@@ -1809,21 +1823,18 @@ sitd_urb_transaction (
                                         struct ehci_sitd, sitd_list);
                        list_del (&sitd->sitd_list);
                        sitd_dma = sitd->sitd_dma;
-               } else
-                       sitd = NULL;
-
-               if (!sitd) {
+               } else {
                        spin_unlock_irqrestore (&ehci->lock, flags);
                        sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
                                        &sitd_dma);
                        spin_lock_irqsave (&ehci->lock, flags);
+                       if (!sitd) {
+                               iso_sched_free(stream, iso_sched);
+                               spin_unlock_irqrestore(&ehci->lock, flags);
+                               return -ENOMEM;
+                       }
                }
 
-               if (!sitd) {
-                       iso_sched_free (stream, iso_sched);
-                       spin_unlock_irqrestore (&ehci->lock, flags);
-                       return -ENOMEM;
-               }
                memset (sitd, 0, sizeof *sitd);
                sitd->sitd_dma = sitd_dma;
                list_add (&sitd->sitd_list, &iso_sched->td_list);
@@ -1950,6 +1961,16 @@ sitd_link_urb (
 #define        SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
                                | SITD_STS_XACT | SITD_STS_MMF)
 
+/* Process and recycle a completed SITD.  Return true iff its urb completed,
+ * and hence its completion callback probably added things to the hardware
+ * schedule.
+ *
+ * Note that we carefully avoid recycling this descriptor until after any
+ * completion callback runs, so that it won't be reused quickly.  That is,
+ * assuming (a) no more than two urbs per frame on this endpoint, and also
+ * (b) only this endpoint's completions submit URBs.  It seems some silicon
+ * corrupts things if you reuse completed descriptors very quickly...
+ */
 static unsigned
 sitd_complete (
        struct ehci_hcd         *ehci,
@@ -1961,6 +1982,7 @@ sitd_complete (
        int                                     urb_index = -1;
        struct ehci_iso_stream                  *stream = sitd->stream;
        struct usb_device                       *dev;
+       unsigned                                retval = false;
 
        urb_index = sitd->index;
        desc = &urb->iso_frame_desc [urb_index];
@@ -1981,32 +2003,23 @@ sitd_complete (
                desc->status = 0;
                desc->actual_length = desc->length - SITD_LENGTH (t);
        }
-
-       usb_put_urb (urb);
-       sitd->urb = NULL;
-       sitd->stream = NULL;
-       list_move (&sitd->sitd_list, &stream->free_list);
        stream->depth -= stream->interval << 3;
-       iso_stream_put (ehci, stream);
 
        /* handle completion now? */
        if ((urb_index + 1) != urb->number_of_packets)
-               return 0;
+               goto done;
 
        /* ASSERT: it's really the last sitd for this urb
        list_for_each_entry (sitd, &stream->td_list, sitd_list)
                BUG_ON (sitd->urb == urb);
         */
 
-       /* give urb back to the driver */
+       /* give urb back to the driver; completion often (re)submits */
        dev = urb->dev;
        ehci_urb_done(ehci, urb, 0);
+       retval = true;
        urb = NULL;
-
-       /* defer stopping schedule; completion can submit */
        ehci->periodic_sched--;
-       if (!ehci->periodic_sched)
-               (void) disable_periodic (ehci);
        ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
 
        if (list_empty (&stream->td_list)) {
@@ -2018,8 +2031,15 @@ sitd_complete (
                        (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
        }
        iso_stream_put (ehci, stream);
+       /* OK to recycle this SITD now that its completion callback ran. */
+done:
+       usb_put_urb(urb);
+       sitd->urb = NULL;
+       sitd->stream = NULL;
+       list_move(&sitd->sitd_list, &stream->free_list);
+       iso_stream_put(ehci, stream);
 
-       return 1;
+       return retval;
 }
 
 
@@ -2082,32 +2102,12 @@ done:
        return status;
 }
 
-#else
-
-static inline int
-sitd_submit (struct ehci_hcd *ehci, struct urb *urb, gfp_t mem_flags)
-{
-       ehci_dbg (ehci, "split iso support is disabled\n");
-       return -ENOSYS;
-}
-
-static inline unsigned
-sitd_complete (
-       struct ehci_hcd         *ehci,
-       struct ehci_sitd        *sitd
-) {
-       ehci_err (ehci, "sitd_complete %p?\n", sitd);
-       return 0;
-}
-
-#endif /* USB_EHCI_SPLIT_ISO */
-
 /*-------------------------------------------------------------------------*/
 
 static void
 scan_periodic (struct ehci_hcd *ehci)
 {
-       unsigned        frame, clock, now_uframe, mod;
+       unsigned        now_uframe, frame, clock, clock_frame, mod;
        unsigned        modified;
 
        mod = ehci->periodic_size << 3;
@@ -2123,21 +2123,14 @@ scan_periodic (struct ehci_hcd *ehci)
        else
                clock = now_uframe + mod - 1;
        clock %= mod;
+       clock_frame = clock >> 3;
 
        for (;;) {
                union ehci_shadow       q, *q_p;
                __hc32                  type, *hw_p;
-               unsigned                uframes;
+               unsigned                incomplete = false;
 
-               /* don't scan past the live uframe */
                frame = now_uframe >> 3;
-               if (frame == (clock >> 3))
-                       uframes = now_uframe & 0x07;
-               else {
-                       /* safe to scan the whole frame at once */
-                       now_uframe |= 0x07;
-                       uframes = 8;
-               }
 
 restart:
                /* scan each element in frame's queue for completions */
@@ -2175,23 +2168,32 @@ restart:
                                q = q.fstn->fstn_next;
                                break;
                        case Q_TYPE_ITD:
-                               /* skip itds for later in the frame */
-                               rmb ();
-                               for (uf = live ? uframes : 8; uf < 8; uf++) {
-                                       if (0 == (q.itd->hw_transaction [uf]
-                                                       & ITD_ACTIVE(ehci)))
-                                               continue;
-                                       q_p = &q.itd->itd_next;
-                                       hw_p = &q.itd->hw_next;
-                                       type = Q_NEXT_TYPE(ehci,
+                               /* If this ITD is still active, leave it for
+                                * later processing ... check the next entry.
+                                * No need to check for activity unless the
+                                * frame is current.
+                                */
+                               if (frame == clock_frame && live) {
+                                       rmb();
+                                       for (uf = 0; uf < 8; uf++) {
+                                               if (q.itd->hw_transaction[uf] &
+                                                           ITD_ACTIVE(ehci))
+                                                       break;
+                                       }
+                                       if (uf < 8) {
+                                               incomplete = true;
+                                               q_p = &q.itd->itd_next;
+                                               hw_p = &q.itd->hw_next;
+                                               type = Q_NEXT_TYPE(ehci,
                                                        q.itd->hw_next);
-                                       q = *q_p;
-                                       break;
+                                               q = *q_p;
+                                               break;
+                                       }
                                }
-                               if (uf != 8)
-                                       break;
 
-                               /* this one's ready ... HC won't cache the
+                               /* Take finished ITDs out of the schedule
+                                * and process them:  recycle, maybe report
+                                * URB completion.  HC won't cache the
                                 * pointer for much longer, if at all.
                                 */
                                *q_p = q.itd->itd_next;
@@ -2202,8 +2204,15 @@ restart:
                                q = *q_p;
                                break;
                        case Q_TYPE_SITD:
-                               if ((q.sitd->hw_results & SITD_ACTIVE(ehci))
-                                               && live) {
+                               /* If this SITD is still active, leave it for
+                                * later processing ... check the next entry.
+                                * No need to check for activity unless the
+                                * frame is current.
+                                */
+                               if (frame == clock_frame && live &&
+                                               (q.sitd->hw_results &
+                                                       SITD_ACTIVE(ehci))) {
+                                       incomplete = true;
                                        q_p = &q.sitd->sitd_next;
                                        hw_p = &q.sitd->hw_next;
                                        type = Q_NEXT_TYPE(ehci,
@@ -2211,6 +2220,11 @@ restart:
                                        q = *q_p;
                                        break;
                                }
+
+                               /* Take finished SITDs out of the schedule
+                                * and process them:  recycle, maybe report
+                                * URB completion.
+                                */
                                *q_p = q.sitd->sitd_next;
                                *hw_p = q.sitd->hw_next;
                                type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
@@ -2226,11 +2240,24 @@ restart:
                        }
 
                        /* assume completion callbacks modify the queue */
-                       if (unlikely (modified))
-                               goto restart;
+                       if (unlikely (modified)) {
+                               if (likely(ehci->periodic_sched > 0))
+                                       goto restart;
+                               /* maybe we can short-circuit this scan! */
+                               disable_periodic(ehci);
+                               now_uframe = clock;
+                               break;
+                       }
                }
 
-               /* stop when we catch up to the HC */
+               /* If we can tell we caught up to the hardware, stop now.
+                * We can't advance our scan without collecting the ISO
+                * transfers that are still pending in this frame.
+                */
+               if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
+                       ehci->next_uframe = now_uframe;
+                       break;
+               }
 
                // FIXME:  this assumes we won't get lapped when
                // latencies climb; that should be rare, but...
@@ -2243,7 +2270,8 @@ restart:
                if (now_uframe == clock) {
                        unsigned        now;
 
-                       if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
+                       if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
+                                       || ehci->periodic_sched == 0)
                                break;
                        ehci->next_uframe = now_uframe;
                        now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
@@ -2252,6 +2280,7 @@ restart:
 
                        /* rescan the rest of this frame, then ... */
                        clock = now;
+                       clock_frame = clock >> 3;
                } else {
                        now_uframe++;
                        now_uframe %= mod;