patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / usb / host / ehci-sched.c
1 /*
2  * Copyright (c) 2001-2003 by David Brownell
3  * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
4  * 
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /* this file is part of ehci-hcd.c */
21
22 /*-------------------------------------------------------------------------*/
23
24 /*
25  * EHCI scheduled transaction support:  interrupt, iso, split iso
26  * These are called "periodic" transactions in the EHCI spec.
27  *
28  * Note that for interrupt transfers, the QH/QTD manipulation is shared
29  * with the "asynchronous" transaction support (control/bulk transfers).
30  * The only real difference is in how interrupt transfers are scheduled.
31  *
32  * For ISO, we make an "iso_stream" head to serve the same role as a QH.
33  * It keeps track of every ITD (or SITD) that's linked, and holds enough
34  * pre-calculated schedule data to make appending to the queue be quick.
35  */
36
37 static int ehci_get_frame (struct usb_hcd *hcd);
38
39 /*-------------------------------------------------------------------------*/
40
41 /*
42  * periodic_next_shadow - return "next" pointer on shadow list
43  * @periodic: host pointer to qh/itd/sitd
44  * @tag: hardware tag for type of this record
45  */
46 static union ehci_shadow *
47 periodic_next_shadow (union ehci_shadow *periodic, int tag)
48 {
49         switch (tag) {
50         case Q_TYPE_QH:
51                 return &periodic->qh->qh_next;
52         case Q_TYPE_FSTN:
53                 return &periodic->fstn->fstn_next;
54         case Q_TYPE_ITD:
55                 return &periodic->itd->itd_next;
56         // case Q_TYPE_SITD:
57         default:
58                 return &periodic->sitd->sitd_next;
59         }
60 }
61
62 /* returns true after successful unlink */
63 /* caller must hold ehci->lock */
64 static int periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
65 {
66         union ehci_shadow       *prev_p = &ehci->pshadow [frame];
67         u32                     *hw_p = &ehci->periodic [frame];
68         union ehci_shadow       here = *prev_p;
69         union ehci_shadow       *next_p;
70
71         /* find predecessor of "ptr"; hw and shadow lists are in sync */
72         while (here.ptr && here.ptr != ptr) {
73                 prev_p = periodic_next_shadow (prev_p, Q_NEXT_TYPE (*hw_p));
74                 hw_p = &here.qh->hw_next;
75                 here = *prev_p;
76         }
77         /* an interrupt entry (at list end) could have been shared */
78         if (!here.ptr) {
79                 dbg ("entry %p no longer on frame [%d]", ptr, frame);
80                 return 0;
81         }
82         // vdbg ("periodic unlink %p from frame %d", ptr, frame);
83
84         /* update hardware list ... HC may still know the old structure, so
85          * don't change hw_next until it'll have purged its cache
86          */
87         next_p = periodic_next_shadow (&here, Q_NEXT_TYPE (*hw_p));
88         *hw_p = here.qh->hw_next;
89
90         /* unlink from shadow list; HCD won't see old structure again */
91         *prev_p = *next_p;
92         next_p->ptr = 0;
93
94         return 1;
95 }
96
97 /* how many of the uframe's 125 usecs are allocated? */
98 static unsigned short
99 periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
100 {
101         u32                     *hw_p = &ehci->periodic [frame];
102         union ehci_shadow       *q = &ehci->pshadow [frame];
103         unsigned                usecs = 0;
104
105         while (q->ptr) {
106                 switch (Q_NEXT_TYPE (*hw_p)) {
107                 case Q_TYPE_QH:
108                         /* is it in the S-mask? */
109                         if (q->qh->hw_info2 & cpu_to_le32 (1 << uframe))
110                                 usecs += q->qh->usecs;
111                         /* ... or C-mask? */
112                         if (q->qh->hw_info2 & cpu_to_le32 (1 << (8 + uframe)))
113                                 usecs += q->qh->c_usecs;
114                         hw_p = &q->qh->hw_next;
115                         q = &q->qh->qh_next;
116                         break;
117                 case Q_TYPE_FSTN:
118                         /* for "save place" FSTNs, count the relevant INTR
119                          * bandwidth from the previous frame
120                          */
121                         if (q->fstn->hw_prev != EHCI_LIST_END) {
122                                 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
123                         }
124                         hw_p = &q->fstn->hw_next;
125                         q = &q->fstn->fstn_next;
126                         break;
127                 case Q_TYPE_ITD:
128                         usecs += q->itd->usecs [uframe];
129                         hw_p = &q->itd->hw_next;
130                         q = &q->itd->itd_next;
131                         break;
132                 case Q_TYPE_SITD:
133                         /* is it in the S-mask?  (count SPLIT, DATA) */
134                         if (q->sitd->hw_uframe & cpu_to_le32 (1 << uframe)) {
135                                 if (q->sitd->hw_fullspeed_ep &
136                                                 __constant_cpu_to_le32 (1<<31))
137                                         usecs += q->sitd->stream->usecs;
138                                 else    /* worst case for OUT start-split */
139                                         usecs += HS_USECS_ISO (188);
140                         }
141
142                         /* ... C-mask?  (count CSPLIT, DATA) */
143                         if (q->sitd->hw_uframe &
144                                         cpu_to_le32 (1 << (8 + uframe))) {
145                                 /* worst case for IN complete-split */
146                                 usecs += q->sitd->stream->c_usecs;
147                         }
148
149                         hw_p = &q->sitd->hw_next;
150                         q = &q->sitd->sitd_next;
151                         break;
152                 default:
153                         BUG ();
154                 }
155         }
156 #ifdef  DEBUG
157         if (usecs > 100)
158                 err ("overallocated uframe %d, periodic is %d usecs",
159                         frame * 8 + uframe, usecs);
160 #endif
161         return usecs;
162 }
163
164 /*-------------------------------------------------------------------------*/
165
166 static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
167 {
168         if (!dev1->tt || !dev2->tt)
169                 return 0;
170         if (dev1->tt != dev2->tt)
171                 return 0;
172         if (dev1->tt->multi)
173                 return dev1->ttport == dev2->ttport;
174         else
175                 return 1;
176 }
177
178 /* return true iff the device's transaction translator is available
179  * for a periodic transfer starting at the specified frame, using
180  * all the uframes in the mask.
181  */
182 static int tt_no_collision (
183         struct ehci_hcd         *ehci,
184         unsigned                period,
185         struct usb_device       *dev,
186         unsigned                frame,
187         u32                     uf_mask
188 )
189 {
190         if (period == 0)        /* error */
191                 return 0;
192
193         /* note bandwidth wastage:  split never follows csplit
194          * (different dev or endpoint) until the next uframe.
195          * calling convention doesn't make that distinction.
196          */
197         for (; frame < ehci->periodic_size; frame += period) {
198                 union ehci_shadow       here;
199                 u32                     type;
200
201                 here = ehci->pshadow [frame];
202                 type = Q_NEXT_TYPE (ehci->periodic [frame]);
203                 while (here.ptr) {
204                         switch (type) {
205                         case Q_TYPE_ITD:
206                                 type = Q_NEXT_TYPE (here.itd->hw_next);
207                                 here = here.itd->itd_next;
208                                 continue;
209                         case Q_TYPE_QH:
210                                 if (same_tt (dev, here.qh->dev)) {
211                                         u32             mask;
212
213                                         mask = le32_to_cpu (here.qh->hw_info2);
214                                         /* "knows" no gap is needed */
215                                         mask |= mask >> 8;
216                                         if (mask & uf_mask)
217                                                 break;
218                                 }
219                                 type = Q_NEXT_TYPE (here.qh->hw_next);
220                                 here = here.qh->qh_next;
221                                 continue;
222                         case Q_TYPE_SITD:
223                                 if (same_tt (dev, here.itd->urb->dev)) {
224                                         u16             mask;
225
226                                         mask = le32_to_cpu (here.sitd
227                                                                 ->hw_uframe);
228                                         /* FIXME assumes no gap for IN! */
229                                         mask |= mask >> 8;
230                                         if (mask & uf_mask)
231                                                 break;
232                                 }
233                                 type = Q_NEXT_TYPE (here.qh->hw_next);
234                                 here = here.sitd->sitd_next;
235                                 continue;
236                         // case Q_TYPE_FSTN:
237                         default:
238                                 ehci_dbg (ehci,
239                                         "periodic frame %d bogus type %d\n",
240                                         frame, type);
241                         }
242
243                         /* collision or error */
244                         return 0;
245                 }
246         }
247
248         /* no collision */
249         return 1;
250 }
251
252 /*-------------------------------------------------------------------------*/
253
254 static int enable_periodic (struct ehci_hcd *ehci)
255 {
256         u32     cmd;
257         int     status;
258
259         /* did clearing PSE did take effect yet?
260          * takes effect only at frame boundaries...
261          */
262         status = handshake (&ehci->regs->status, STS_PSS, 0, 9 * 125);
263         if (status != 0) {
264                 ehci->hcd.state = USB_STATE_HALT;
265                 return status;
266         }
267
268         cmd = readl (&ehci->regs->command) | CMD_PSE;
269         writel (cmd, &ehci->regs->command);
270         /* posted write ... PSS happens later */
271         ehci->hcd.state = USB_STATE_RUNNING;
272
273         /* make sure ehci_work scans these */
274         ehci->next_uframe = readl (&ehci->regs->frame_index)
275                                 % (ehci->periodic_size << 3);
276         return 0;
277 }
278
279 static int disable_periodic (struct ehci_hcd *ehci)
280 {
281         u32     cmd;
282         int     status;
283
284         /* did setting PSE not take effect yet?
285          * takes effect only at frame boundaries...
286          */
287         status = handshake (&ehci->regs->status, STS_PSS, STS_PSS, 9 * 125);
288         if (status != 0) {
289                 ehci->hcd.state = USB_STATE_HALT;
290                 return status;
291         }
292
293         cmd = readl (&ehci->regs->command) & ~CMD_PSE;
294         writel (cmd, &ehci->regs->command);
295         /* posted write ... */
296
297         ehci->next_uframe = -1;
298         return 0;
299 }
300
301 /*-------------------------------------------------------------------------*/
302
303 // FIXME microframe periods not yet handled
304
305 static void intr_deschedule (
306         struct ehci_hcd *ehci,
307         struct ehci_qh  *qh,
308         int             wait
309 ) {
310         int             status;
311         unsigned        frame = qh->start;
312
313         do {
314                 periodic_unlink (ehci, frame, qh);
315                 qh_put (qh);
316                 frame += qh->period;
317         } while (frame < ehci->periodic_size);
318
319         qh->qh_state = QH_STATE_UNLINK;
320         qh->qh_next.ptr = 0;
321         ehci->periodic_sched--;
322
323         /* maybe turn off periodic schedule */
324         if (!ehci->periodic_sched)
325                 status = disable_periodic (ehci);
326         else {
327                 status = 0;
328                 vdbg ("periodic schedule still enabled");
329         }
330
331         /*
332          * If the hc may be looking at this qh, then delay a uframe
333          * (yeech!) to be sure it's done.
334          * No other threads may be mucking with this qh.
335          */
336         if (((ehci_get_frame (&ehci->hcd) - frame) % qh->period) == 0) {
337                 if (wait) {
338                         udelay (125);
339                         qh->hw_next = EHCI_LIST_END;
340                 } else {
341                         /* we may not be IDLE yet, but if the qh is empty
342                          * the race is very short.  then if qh also isn't
343                          * rescheduled soon, it won't matter.  otherwise...
344                          */
345                         vdbg ("intr_deschedule...");
346                 }
347         } else
348                 qh->hw_next = EHCI_LIST_END;
349
350         qh->qh_state = QH_STATE_IDLE;
351
352         /* update per-qh bandwidth utilization (for usbfs) */
353         hcd_to_bus (&ehci->hcd)->bandwidth_allocated -= 
354                 (qh->usecs + qh->c_usecs) / qh->period;
355
356         dbg ("descheduled qh %p, period = %d frame = %d count = %d, urbs = %d",
357                 qh, qh->period, frame,
358                 atomic_read (&qh->kref.refcount), ehci->periodic_sched);
359 }
360
361 static int check_period (
362         struct ehci_hcd *ehci, 
363         unsigned        frame,
364         unsigned        uframe,
365         unsigned        period,
366         unsigned        usecs
367 ) {
368         /* complete split running into next frame?
369          * given FSTN support, we could sometimes check...
370          */
371         if (uframe >= 8)
372                 return 0;
373
374         /*
375          * 80% periodic == 100 usec/uframe available
376          * convert "usecs we need" to "max already claimed" 
377          */
378         usecs = 100 - usecs;
379
380         do {
381                 int     claimed;
382
383 // FIXME delete when intr_submit handles non-empty queues
384 // this gives us a one intr/frame limit (vs N/uframe)
385 // ... and also lets us avoid tracking split transactions
386 // that might collide at a given TT/hub.
387                 if (ehci->pshadow [frame].ptr)
388                         return 0;
389
390                 claimed = periodic_usecs (ehci, frame, uframe);
391                 if (claimed > usecs)
392                         return 0;
393
394 // FIXME update to handle sub-frame periods
395         } while ((frame += period) < ehci->periodic_size);
396
397         // success!
398         return 1;
399 }
400
401 static int check_intr_schedule (
402         struct ehci_hcd         *ehci, 
403         unsigned                frame,
404         unsigned                uframe,
405         const struct ehci_qh    *qh,
406         u32                     *c_maskp
407 )
408 {
409         int             retval = -ENOSPC;
410
411         if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
412                 goto done;
413         if (!qh->c_usecs) {
414                 retval = 0;
415                 *c_maskp = cpu_to_le32 (0);
416                 goto done;
417         }
418
419         /* This is a split transaction; check the bandwidth available for
420          * the completion too.  Check both worst and best case gaps: worst
421          * case is SPLIT near uframe end, and CSPLIT near start ... best is
422          * vice versa.  Difference can be almost two uframe times, but we
423          * reserve unnecessary bandwidth (waste it) this way.  (Actually
424          * even better cases exist, like immediate device NAK.)
425          *
426          * FIXME don't even bother unless we know this TT is idle in that
427          * range of uframes ... for now, check_period() allows only one
428          * interrupt transfer per frame, so needn't check "TT busy" status
429          * when scheduling a split (QH, SITD, or FSTN).
430          *
431          * FIXME ehci 0.96 and above can use FSTNs
432          */
433         if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
434                                 qh->period, qh->c_usecs))
435                 goto done;
436         if (!check_period (ehci, frame, uframe + qh->gap_uf,
437                                 qh->period, qh->c_usecs))
438                 goto done;
439
440         *c_maskp = cpu_to_le32 (0x03 << (8 + uframe + qh->gap_uf));
441         retval = 0;
442 done:
443         return retval;
444 }
445
446 static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
447 {
448         int             status;
449         unsigned        uframe;
450         u32             c_mask;
451         unsigned        frame;          /* 0..(qh->period - 1), or NO_FRAME */
452
453         qh->hw_next = EHCI_LIST_END;
454         frame = qh->start;
455
456         /* reuse the previous schedule slots, if we can */
457         if (frame < qh->period) {
458                 uframe = ffs (le32_to_cpup (&qh->hw_info2) & 0x00ff);
459                 status = check_intr_schedule (ehci, frame, --uframe,
460                                 qh, &c_mask);
461         } else {
462                 uframe = 0;
463                 c_mask = 0;
464                 status = -ENOSPC;
465         }
466
467         /* else scan the schedule to find a group of slots such that all
468          * uframes have enough periodic bandwidth available.
469          */
470         if (status) {
471                 frame = qh->period - 1;
472                 do {
473                         for (uframe = 0; uframe < 8; uframe++) {
474                                 status = check_intr_schedule (ehci,
475                                                 frame, uframe, qh,
476                                                 &c_mask);
477                                 if (status == 0)
478                                         break;
479                         }
480                 } while (status && frame--);
481                 if (status)
482                         goto done;
483                 qh->start = frame;
484
485                 /* reset S-frame and (maybe) C-frame masks */
486                 qh->hw_info2 &= ~0xffff;
487                 qh->hw_info2 |= cpu_to_le32 (1 << uframe) | c_mask;
488         } else
489                 dbg ("reused previous qh %p schedule", qh);
490
491         /* stuff into the periodic schedule */
492         qh->qh_state = QH_STATE_LINKED;
493         dbg ("scheduled qh %p usecs %d/%d period %d.0 starting %d.%d (gap %d)",
494                 qh, qh->usecs, qh->c_usecs,
495                 qh->period, frame, uframe, qh->gap_uf);
496         do {
497                 if (unlikely (ehci->pshadow [frame].ptr != 0)) {
498
499 // FIXME -- just link toward the end, before any qh with a shorter period,
500 // AND accommodate it already having been linked here (after some other qh)
501 // AS WELL AS updating the schedule checking logic
502
503                         BUG ();
504                 } else {
505                         ehci->pshadow [frame].qh = qh_get (qh);
506                         ehci->periodic [frame] =
507                                 QH_NEXT (qh->qh_dma);
508                 }
509                 wmb ();
510                 frame += qh->period;
511         } while (frame < ehci->periodic_size);
512
513         /* update per-qh bandwidth for usbfs */
514         hcd_to_bus (&ehci->hcd)->bandwidth_allocated += 
515                 (qh->usecs + qh->c_usecs) / qh->period;
516
517         /* maybe enable periodic schedule processing */
518         if (!ehci->periodic_sched++)
519                 status = enable_periodic (ehci);
520 done:
521         return status;
522 }
523
524 static int intr_submit (
525         struct ehci_hcd         *ehci,
526         struct urb              *urb,
527         struct list_head        *qtd_list,
528         int                     mem_flags
529 ) {
530         unsigned                epnum;
531         unsigned long           flags;
532         struct ehci_qh          *qh;
533         struct hcd_dev          *dev;
534         int                     is_input;
535         int                     status = 0;
536         struct list_head        empty;
537
538         /* get endpoint and transfer/schedule data */
539         epnum = usb_pipeendpoint (urb->pipe);
540         is_input = usb_pipein (urb->pipe);
541         if (is_input)
542                 epnum |= 0x10;
543
544         spin_lock_irqsave (&ehci->lock, flags);
545         dev = (struct hcd_dev *)urb->dev->hcpriv;
546
547         /* get qh and force any scheduling errors */
548         INIT_LIST_HEAD (&empty);
549         qh = qh_append_tds (ehci, urb, &empty, epnum, &dev->ep [epnum]);
550         if (qh == 0) {
551                 status = -ENOMEM;
552                 goto done;
553         }
554         if (qh->qh_state == QH_STATE_IDLE) {
555                 if ((status = qh_schedule (ehci, qh)) != 0)
556                         goto done;
557         }
558
559         /* then queue the urb's tds to the qh */
560         qh = qh_append_tds (ehci, urb, qtd_list, epnum, &dev->ep [epnum]);
561         BUG_ON (qh == 0);
562
563         /* ... update usbfs periodic stats */
564         hcd_to_bus (&ehci->hcd)->bandwidth_int_reqs++;
565
566 done:
567         spin_unlock_irqrestore (&ehci->lock, flags);
568         if (status)
569                 qtd_list_free (ehci, urb, qtd_list);
570
571         return status;
572 }
573
574 /*-------------------------------------------------------------------------*/
575
576 /* ehci_iso_stream ops work with both ITD and SITD */
577
578 static struct ehci_iso_stream *
579 iso_stream_alloc (int mem_flags)
580 {
581         struct ehci_iso_stream *stream;
582
583         stream = kmalloc(sizeof *stream, mem_flags);
584         if (likely (stream != 0)) {
585                 memset (stream, 0, sizeof(*stream));
586                 INIT_LIST_HEAD(&stream->td_list);
587                 INIT_LIST_HEAD(&stream->free_list);
588                 stream->next_uframe = -1;
589                 stream->refcount = 1;
590         }
591         return stream;
592 }
593
594 static void
595 iso_stream_init (
596         struct ehci_iso_stream  *stream,
597         struct usb_device       *dev,
598         int                     pipe,
599         unsigned                interval
600 )
601 {
602         static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
603
604         u32                     buf1;
605         unsigned                epnum, maxp;
606         int                     is_input;
607         long                    bandwidth;
608
609         /*
610          * this might be a "high bandwidth" highspeed endpoint,
611          * as encoded in the ep descriptor's wMaxPacket field
612          */
613         epnum = usb_pipeendpoint (pipe);
614         is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
615         if (is_input) {
616                 maxp = dev->epmaxpacketin [epnum];
617                 buf1 = (1 << 11);
618         } else {
619                 maxp = dev->epmaxpacketout [epnum];
620                 buf1 = 0;
621         }
622
623         /* knows about ITD vs SITD */
624         if (dev->speed == USB_SPEED_HIGH) {
625                 unsigned multi = hb_mult(maxp);
626
627                 stream->highspeed = 1;
628
629                 maxp = max_packet(maxp);
630                 buf1 |= maxp;
631                 maxp *= multi;
632
633                 stream->buf0 = cpu_to_le32 ((epnum << 8) | dev->devnum);
634                 stream->buf1 = cpu_to_le32 (buf1);
635                 stream->buf2 = cpu_to_le32 (multi);
636
637                 /* usbfs wants to report the average usecs per frame tied up
638                  * when transfers on this endpoint are scheduled ...
639                  */
640                 stream->usecs = HS_USECS_ISO (maxp);
641                 bandwidth = stream->usecs * 8;
642                 bandwidth /= 1 << (interval - 1);
643
644         } else {
645                 u32             addr;
646
647                 addr = dev->ttport << 24;
648                 addr |= dev->tt->hub->devnum << 16;
649                 addr |= epnum << 8;
650                 addr |= dev->devnum;
651                 stream->usecs = HS_USECS_ISO (maxp);
652                 if (is_input) {
653                         u32     tmp;
654
655                         addr |= 1 << 31;
656                         stream->c_usecs = stream->usecs;
657                         stream->usecs = HS_USECS_ISO (1);
658                         stream->raw_mask = 1;
659
660                         /* pessimistic c-mask */
661                         tmp = usb_calc_bus_time (USB_SPEED_FULL, 1, 0, maxp)
662                                         / (125 * 1000);
663                         stream->raw_mask |= 3 << (tmp + 9);
664                 } else
665                         stream->raw_mask = smask_out [maxp / 188];
666                 bandwidth = stream->usecs + stream->c_usecs;
667                 bandwidth /= 1 << (interval + 2);
668
669                 /* stream->splits gets created from raw_mask later */
670                 stream->address = cpu_to_le32 (addr);
671         }
672         stream->bandwidth = bandwidth;
673
674         stream->udev = dev;
675
676         stream->bEndpointAddress = is_input | epnum;
677         stream->interval = interval;
678         stream->maxp = maxp;
679 }
680
681 static void
682 iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
683 {
684         stream->refcount--;
685
686         /* free whenever just a dev->ep reference remains.
687          * not like a QH -- no persistent state (toggle, halt)
688          */
689         if (stream->refcount == 1) {
690                 int             is_in;
691                 struct hcd_dev  *dev = stream->udev->hcpriv;
692
693                 // BUG_ON (!list_empty(&stream->td_list));
694
695                 while (!list_empty (&stream->free_list)) {
696                         struct list_head        *entry;
697
698                         entry = stream->free_list.next;
699                         list_del (entry);
700
701                         /* knows about ITD vs SITD */
702                         if (stream->highspeed) {
703                                 struct ehci_itd         *itd;
704
705                                 itd = list_entry (entry, struct ehci_itd,
706                                                 itd_list);
707                                 dma_pool_free (ehci->itd_pool, itd,
708                                                 itd->itd_dma);
709                         } else {
710                                 struct ehci_sitd        *sitd;
711
712                                 sitd = list_entry (entry, struct ehci_sitd,
713                                                 sitd_list);
714                                 dma_pool_free (ehci->sitd_pool, sitd,
715                                                 sitd->sitd_dma);
716                         }
717                 }
718
719                 is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
720                 stream->bEndpointAddress &= 0x0f;
721                 dev->ep [is_in + stream->bEndpointAddress] = 0;
722
723                 if (stream->rescheduled) {
724                         ehci_info (ehci, "ep%d%s-iso rescheduled "
725                                 "%lu times in %lu seconds\n",
726                                 stream->bEndpointAddress, is_in ? "in" : "out",
727                                 stream->rescheduled,
728                                 ((jiffies - stream->start)/HZ)
729                                 );
730                 }
731
732                 kfree(stream);
733         }
734 }
735
736 static inline struct ehci_iso_stream *
737 iso_stream_get (struct ehci_iso_stream *stream)
738 {
739         if (likely (stream != 0))
740                 stream->refcount++;
741         return stream;
742 }
743
744 static struct ehci_iso_stream *
745 iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
746 {
747         unsigned                epnum;
748         struct hcd_dev          *dev;
749         struct ehci_iso_stream  *stream;
750         unsigned long           flags;
751
752         epnum = usb_pipeendpoint (urb->pipe);
753         if (usb_pipein(urb->pipe))
754                 epnum += 0x10;
755
756         spin_lock_irqsave (&ehci->lock, flags);
757
758         dev = (struct hcd_dev *)urb->dev->hcpriv;
759         stream = dev->ep [epnum];
760
761         if (unlikely (stream == 0)) {
762                 stream = iso_stream_alloc(GFP_ATOMIC);
763                 if (likely (stream != 0)) {
764                         /* dev->ep owns the initial refcount */
765                         dev->ep[epnum] = stream;
766                         iso_stream_init(stream, urb->dev, urb->pipe,
767                                         urb->interval);
768                 }
769
770         /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
771         } else if (unlikely (stream->hw_info1 != 0)) {
772                 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
773                         urb->dev->devpath, epnum & 0x0f,
774                         (epnum & 0x10) ? "in" : "out");
775                 stream = 0;
776         }
777
778         /* caller guarantees an eventual matching iso_stream_put */
779         stream = iso_stream_get (stream);
780
781         spin_unlock_irqrestore (&ehci->lock, flags);
782         return stream;
783 }
784
785 /*-------------------------------------------------------------------------*/
786
787 /* ehci_iso_sched ops can be shared, ITD-only, or SITD-only */
788
789 static struct ehci_iso_sched *
790 iso_sched_alloc (unsigned packets, int mem_flags)
791 {
792         struct ehci_iso_sched   *iso_sched;
793         int                     size = sizeof *iso_sched;
794
795         size += packets * sizeof (struct ehci_iso_packet);
796         iso_sched = kmalloc (size, mem_flags);
797         if (likely (iso_sched != 0)) {
798                 memset(iso_sched, 0, size);
799                 INIT_LIST_HEAD (&iso_sched->td_list);
800         }
801         return iso_sched;
802 }
803
804 static inline void
805 itd_sched_init (
806         struct ehci_iso_sched   *iso_sched,
807         struct ehci_iso_stream  *stream,
808         struct urb              *urb
809 )
810 {
811         unsigned        i;
812         dma_addr_t      dma = urb->transfer_dma;
813
814         /* how many uframes are needed for these transfers */
815         iso_sched->span = urb->number_of_packets * stream->interval;
816
817         /* figure out per-uframe itd fields that we'll need later
818          * when we fit new itds into the schedule.
819          */
820         for (i = 0; i < urb->number_of_packets; i++) {
821                 struct ehci_iso_packet  *uframe = &iso_sched->packet [i];
822                 unsigned                length;
823                 dma_addr_t              buf;
824                 u32                     trans;
825
826                 length = urb->iso_frame_desc [i].length;
827                 buf = dma + urb->iso_frame_desc [i].offset;
828
829                 trans = EHCI_ISOC_ACTIVE;
830                 trans |= buf & 0x0fff;
831                 if (unlikely (((i + 1) == urb->number_of_packets))
832                                 && !(urb->transfer_flags & URB_NO_INTERRUPT))
833                         trans |= EHCI_ITD_IOC;
834                 trans |= length << 16;
835                 uframe->transaction = cpu_to_le32 (trans);
836
837                 /* might need to cross a buffer page within a td */
838                 uframe->bufp = (buf & ~(u64)0x0fff);
839                 buf += length;
840                 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
841                         uframe->cross = 1;
842         }
843 }
844
845 static void
846 iso_sched_free (
847         struct ehci_iso_stream  *stream,
848         struct ehci_iso_sched   *iso_sched
849 )
850 {
851         if (!iso_sched)
852                 return;
853         // caller must hold ehci->lock!
854         list_splice (&iso_sched->td_list, &stream->free_list);
855         kfree (iso_sched);
856 }
857
858 static int
859 itd_urb_transaction (
860         struct ehci_iso_stream  *stream,
861         struct ehci_hcd         *ehci,
862         struct urb              *urb,
863         int                     mem_flags
864 )
865 {
866         struct ehci_itd         *itd;
867         dma_addr_t              itd_dma;
868         int                     i;
869         unsigned                num_itds;
870         struct ehci_iso_sched   *sched;
871         unsigned long           flags;
872
873         sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
874         if (unlikely (sched == 0))
875                 return -ENOMEM;
876
877         itd_sched_init (sched, stream, urb);
878
879         if (urb->interval < 8)
880                 num_itds = 1 + (sched->span + 7) / 8;
881         else
882                 num_itds = urb->number_of_packets;
883
884         /* allocate/init ITDs */
885         spin_lock_irqsave (&ehci->lock, flags);
886         for (i = 0; i < num_itds; i++) {
887
888                 /* free_list.next might be cache-hot ... but maybe
889                  * the HC caches it too. avoid that issue for now.
890                  */
891
892                 /* prefer previously-allocated itds */
893                 if (likely (!list_empty(&stream->free_list))) {
894                         itd = list_entry (stream->free_list.prev,
895                                          struct ehci_itd, itd_list);
896                         list_del (&itd->itd_list);
897                         itd_dma = itd->itd_dma;
898                 } else
899                         itd = 0;
900
901                 if (!itd) {
902                         spin_unlock_irqrestore (&ehci->lock, flags);
903                         itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
904                                         &itd_dma);
905                         spin_lock_irqsave (&ehci->lock, flags);
906                 }
907
908                 if (unlikely (0 == itd)) {
909                         iso_sched_free (stream, sched);
910                         return -ENOMEM;
911                 }
912                 memset (itd, 0, sizeof *itd);
913                 itd->itd_dma = itd_dma;
914                 list_add (&itd->itd_list, &sched->td_list);
915         }
916         spin_unlock_irqrestore (&ehci->lock, flags);
917
918         /* temporarily store schedule info in hcpriv */
919         urb->hcpriv = sched;
920         urb->error_count = 0;
921         return 0;
922 }
923
924 /*-------------------------------------------------------------------------*/
925
926 static inline int
927 itd_slot_ok (
928         struct ehci_hcd         *ehci,
929         u32                     mod,
930         u32                     uframe,
931         u8                      usecs,
932         u32                     period
933 )
934 {
935         uframe %= period;
936         do {
937                 /* can't commit more than 80% periodic == 100 usec */
938                 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
939                                 > (100 - usecs))
940                         return 0;
941
942                 /* we know urb->interval is 2^N uframes */
943                 uframe += period;
944         } while (uframe < mod);
945         return 1;
946 }
947
948 static inline int
949 sitd_slot_ok (
950         struct ehci_hcd         *ehci,
951         u32                     mod,
952         struct ehci_iso_stream  *stream,
953         u32                     uframe,
954         struct ehci_iso_sched   *sched,
955         u32                     period_uframes
956 )
957 {
958         u32                     mask, tmp;
959         u32                     frame, uf;
960
961         mask = stream->raw_mask << (uframe & 7);
962
963         /* for IN, don't wrap CSPLIT into the next frame */
964         if (mask & ~0xffff)
965                 return 0;
966
967         /* this multi-pass logic is simple, but performance may
968          * suffer when the schedule data isn't cached.
969          */
970
971         /* check bandwidth */
972         uframe %= period_uframes;
973         do {
974                 u32             max_used;
975
976                 frame = uframe >> 3;
977                 uf = uframe & 7;
978
979                 /* tt must be idle for start(s), any gap, and csplit.
980                  * assume scheduling slop leaves 10+% for control/bulk.
981                  */
982                 if (!tt_no_collision (ehci, period_uframes << 3,
983                                 stream->udev, frame, mask))
984                         return 0;
985
986                 /* check starts (OUT uses more than one) */
987                 max_used = 100 - stream->usecs;
988                 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
989                         if (periodic_usecs (ehci, frame, uf) > max_used)
990                                 return 0;
991                 }
992
993                 /* for IN, check CSPLIT */
994                 if (stream->c_usecs) {
995                         max_used = 100 - stream->c_usecs;
996                         do {
997                                 tmp = 1 << uf;
998                                 tmp <<= 8;
999                                 if ((stream->raw_mask & tmp) == 0)
1000                                         continue;
1001                                 if (periodic_usecs (ehci, frame, uf)
1002                                                 > max_used)
1003                                         return 0;
1004                         } while (++uf < 8);
1005                 }
1006
1007                 /* we know urb->interval is 2^N uframes */
1008                 uframe += period_uframes;
1009         } while (uframe < mod);
1010
1011         stream->splits = stream->raw_mask << (uframe & 7);
1012         cpu_to_le32s (&stream->splits);
1013         return 1;
1014 }
1015
1016 /*
1017  * This scheduler plans almost as far into the future as it has actual
1018  * periodic schedule slots.  (Affected by TUNE_FLS, which defaults to
1019  * "as small as possible" to be cache-friendlier.)  That limits the size
1020  * transfers you can stream reliably; avoid more than 64 msec per urb.
1021  * Also avoid queue depths of less than ehci's worst irq latency (affected
1022  * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1023  * and other factors); or more than about 230 msec total (for portability,
1024  * given EHCI_TUNE_FLS and the slop).  Or, write a smarter scheduler!
1025  */
1026
1027 #define SCHEDULE_SLOP   10      /* frames */
1028
1029 static int
1030 iso_stream_schedule (
1031         struct ehci_hcd         *ehci,
1032         struct urb              *urb,
1033         struct ehci_iso_stream  *stream
1034 )
1035 {
1036         u32                     now, start, max, period;
1037         int                     status;
1038         unsigned                mod = ehci->periodic_size << 3;
1039         struct ehci_iso_sched   *sched = urb->hcpriv;
1040
1041         if (sched->span > (mod - 8 * SCHEDULE_SLOP)) {
1042                 ehci_dbg (ehci, "iso request %p too long\n", urb);
1043                 status = -EFBIG;
1044                 goto fail;
1045         }
1046
1047         if ((stream->depth + sched->span) > mod) {
1048                 ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
1049                         urb, stream->depth, sched->span, mod);
1050                 status = -EFBIG;
1051                 goto fail;
1052         }
1053
1054         now = readl (&ehci->regs->frame_index) % mod;
1055
1056         /* when's the last uframe this urb could start? */
1057         max = now + mod;
1058
1059         /* typical case: reuse current schedule. stream is still active,
1060          * and no gaps from host falling behind (irq delays etc)
1061          */
1062         if (likely (!list_empty (&stream->td_list))) {
1063                 start = stream->next_uframe;
1064                 if (start < now)
1065                         start += mod;
1066                 if (likely ((start + sched->span) < max))
1067                         goto ready;
1068                 /* else fell behind; someday, try to reschedule */
1069                 status = -EL2NSYNC;
1070                 goto fail;
1071         }
1072
1073         /* need to schedule; when's the next (u)frame we could start?
1074          * this is bigger than ehci->i_thresh allows; scheduling itself
1075          * isn't free, the slop should handle reasonably slow cpus.  it
1076          * can also help high bandwidth if the dma and irq loads don't
1077          * jump until after the queue is primed.
1078          */
1079         start = SCHEDULE_SLOP * 8 + (now & ~0x07);
1080         start %= mod;
1081         stream->next_uframe = start;
1082
1083         /* NOTE:  assumes URB_ISO_ASAP, to limit complexity/bugs */
1084
1085         period = urb->interval;
1086         if (!stream->highspeed)
1087                 period <<= 3;
1088
1089         /* find a uframe slot with enough bandwidth */
1090         for (; start < (stream->next_uframe + period); start++) {
1091                 int             enough_space;
1092
1093                 /* check schedule: enough space? */
1094                 if (stream->highspeed)
1095                         enough_space = itd_slot_ok (ehci, mod, start,
1096                                         stream->usecs, period);
1097                 else {
1098                         if ((start % 8) >= 6)
1099                                 continue;
1100                         enough_space = sitd_slot_ok (ehci, mod, stream,
1101                                         start, sched, period);
1102                 }
1103
1104                 /* schedule it here if there's enough bandwidth */
1105                 if (enough_space) {
1106                         stream->next_uframe = start % mod;
1107                         goto ready;
1108                 }
1109         }
1110
1111         /* no room in the schedule */
1112         ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
1113                 list_empty (&stream->td_list) ? "" : "re",
1114                 urb, now, max);
1115         status = -ENOSPC;
1116
1117 fail:
1118         iso_sched_free (stream, sched);
1119         urb->hcpriv = 0;
1120         return status;
1121
1122 ready:
1123         urb->start_frame = stream->next_uframe;
1124         return 0;
1125 }
1126
1127 /*-------------------------------------------------------------------------*/
1128
1129 static inline void
1130 itd_init (struct ehci_iso_stream *stream, struct ehci_itd *itd)
1131 {
1132         int i;
1133
1134         itd->hw_next = EHCI_LIST_END;
1135         itd->hw_bufp [0] = stream->buf0;
1136         itd->hw_bufp [1] = stream->buf1;
1137         itd->hw_bufp [2] = stream->buf2;
1138
1139         for (i = 0; i < 8; i++)
1140                 itd->index[i] = -1;
1141
1142         /* All other fields are filled when scheduling */
1143 }
1144
1145 static inline void
1146 itd_patch (
1147         struct ehci_itd         *itd,
1148         struct ehci_iso_sched   *iso_sched,
1149         unsigned                index,
1150         u16                     uframe,
1151         int                     first
1152 )
1153 {
1154         struct ehci_iso_packet  *uf = &iso_sched->packet [index];
1155         unsigned                pg = itd->pg;
1156
1157         // BUG_ON (pg == 6 && uf->cross);
1158
1159         uframe &= 0x07;
1160         itd->index [uframe] = index;
1161
1162         itd->hw_transaction [uframe] = uf->transaction;
1163         itd->hw_transaction [uframe] |= cpu_to_le32 (pg << 12);
1164         itd->hw_bufp [pg] |= cpu_to_le32 (uf->bufp & ~(u32)0);
1165         itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(uf->bufp >> 32));
1166
1167         /* iso_frame_desc[].offset must be strictly increasing */
1168         if (unlikely (!first && uf->cross)) {
1169                 u64     bufp = uf->bufp + 4096;
1170                 itd->pg = ++pg;
1171                 itd->hw_bufp [pg] |= cpu_to_le32 (bufp & ~(u32)0);
1172                 itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(bufp >> 32));
1173         }
1174 }
1175
1176 static inline void
1177 itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1178 {
1179         /* always prepend ITD/SITD ... only QH tree is order-sensitive */
1180         itd->itd_next = ehci->pshadow [frame];
1181         itd->hw_next = ehci->periodic [frame];
1182         ehci->pshadow [frame].itd = itd;
1183         itd->frame = frame;
1184         wmb ();
1185         ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;
1186 }
1187
1188 /* fit urb's itds into the selected schedule slot; activate as needed */
1189 static int
1190 itd_link_urb (
1191         struct ehci_hcd         *ehci,
1192         struct urb              *urb,
1193         unsigned                mod,
1194         struct ehci_iso_stream  *stream
1195 )
1196 {
1197         int                     packet, first = 1;
1198         unsigned                next_uframe, uframe, frame;
1199         struct ehci_iso_sched   *iso_sched = urb->hcpriv;
1200         struct ehci_itd         *itd;
1201
1202         next_uframe = stream->next_uframe % mod;
1203
1204         if (unlikely (list_empty(&stream->td_list))) {
1205                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1206                                 += stream->bandwidth;
1207                 ehci_vdbg (ehci,
1208                         "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1209                         urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1210                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1211                         urb->interval,
1212                         next_uframe >> 3, next_uframe & 0x7);
1213                 stream->start = jiffies;
1214         }
1215         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs++;
1216
1217         /* fill iTDs uframe by uframe */
1218         for (packet = 0, itd = 0; packet < urb->number_of_packets; ) {
1219                 if (itd == 0) {
1220                         /* ASSERT:  we have all necessary itds */
1221                         // BUG_ON (list_empty (&iso_sched->td_list));
1222
1223                         /* ASSERT:  no itds for this endpoint in this uframe */
1224
1225                         itd = list_entry (iso_sched->td_list.next,
1226                                         struct ehci_itd, itd_list);
1227                         list_move_tail (&itd->itd_list, &stream->td_list);
1228                         itd->stream = iso_stream_get (stream);
1229                         itd->urb = usb_get_urb (urb);
1230                         first = 1;
1231                         itd_init (stream, itd);
1232                 }
1233
1234                 uframe = next_uframe & 0x07;
1235                 frame = next_uframe >> 3;
1236
1237                 itd->usecs [uframe] = stream->usecs;
1238                 itd_patch (itd, iso_sched, packet, uframe, first);
1239                 first = 0;
1240
1241                 next_uframe += stream->interval;
1242                 stream->depth += stream->interval;
1243                 next_uframe %= mod;
1244                 packet++;
1245
1246                 /* link completed itds into the schedule */
1247                 if (((next_uframe >> 3) != frame)
1248                                 || packet == urb->number_of_packets) {
1249                         itd_link (ehci, frame % ehci->periodic_size, itd);
1250                         itd = 0;
1251                 }
1252         }
1253         stream->next_uframe = next_uframe;
1254
1255         /* don't need that schedule data any more */
1256         iso_sched_free (stream, iso_sched);
1257         urb->hcpriv = 0;
1258
1259         timer_action (ehci, TIMER_IO_WATCHDOG);
1260         if (unlikely (!ehci->periodic_sched++))
1261                 return enable_periodic (ehci);
1262         return 0;
1263 }
1264
1265 #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1266
1267 static unsigned
1268 itd_complete (
1269         struct ehci_hcd *ehci,
1270         struct ehci_itd *itd,
1271         struct pt_regs  *regs
1272 ) {
1273         struct urb                              *urb = itd->urb;
1274         struct usb_iso_packet_descriptor        *desc;
1275         u32                                     t;
1276         unsigned                                uframe;
1277         int                                     urb_index = -1;
1278         struct ehci_iso_stream                  *stream = itd->stream;
1279         struct usb_device                       *dev;
1280
1281         /* for each uframe with a packet */
1282         for (uframe = 0; uframe < 8; uframe++) {
1283                 if (likely (itd->index[uframe] == -1))
1284                         continue;
1285                 urb_index = itd->index[uframe];
1286                 desc = &urb->iso_frame_desc [urb_index];
1287
1288                 t = le32_to_cpup (&itd->hw_transaction [uframe]);
1289                 itd->hw_transaction [uframe] = 0;
1290                 stream->depth -= stream->interval;
1291
1292                 /* report transfer status */
1293                 if (unlikely (t & ISO_ERRS)) {
1294                         urb->error_count++;
1295                         if (t & EHCI_ISOC_BUF_ERR)
1296                                 desc->status = usb_pipein (urb->pipe)
1297                                         ? -ENOSR  /* hc couldn't read */
1298                                         : -ECOMM; /* hc couldn't write */
1299                         else if (t & EHCI_ISOC_BABBLE)
1300                                 desc->status = -EOVERFLOW;
1301                         else /* (t & EHCI_ISOC_XACTERR) */
1302                                 desc->status = -EPROTO;
1303
1304                         /* HC need not update length with this error */
1305                         if (!(t & EHCI_ISOC_BABBLE))
1306                                 desc->actual_length = EHCI_ITD_LENGTH (t);
1307                 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1308                         desc->status = 0;
1309                         desc->actual_length = EHCI_ITD_LENGTH (t);
1310                 }
1311         }
1312
1313         usb_put_urb (urb);
1314         itd->urb = 0;
1315         itd->stream = 0;
1316         list_move (&itd->itd_list, &stream->free_list);
1317         iso_stream_put (ehci, stream);
1318
1319         /* handle completion now? */
1320         if (likely ((urb_index + 1) != urb->number_of_packets))
1321                 return 0;
1322
1323         /* ASSERT: it's really the last itd for this urb
1324         list_for_each_entry (itd, &stream->td_list, itd_list)
1325                 BUG_ON (itd->urb == urb);
1326          */
1327
1328         /* give urb back to the driver ... can be out-of-order */
1329         dev = usb_get_dev (urb->dev);
1330         ehci_urb_done (ehci, urb, regs);
1331         urb = 0;
1332
1333         /* defer stopping schedule; completion can submit */
1334         ehci->periodic_sched--;
1335         if (unlikely (!ehci->periodic_sched))
1336                 (void) disable_periodic (ehci);
1337         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs--;
1338
1339         if (unlikely (list_empty (&stream->td_list))) {
1340                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1341                                 -= stream->bandwidth;
1342                 ehci_vdbg (ehci,
1343                         "deschedule devp %s ep%d%s-iso\n",
1344                         dev->devpath, stream->bEndpointAddress & 0x0f,
1345                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1346         }
1347         iso_stream_put (ehci, stream);
1348         usb_put_dev (dev);
1349
1350         return 1;
1351 }
1352
1353 /*-------------------------------------------------------------------------*/
1354
1355 static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1356 {
1357         int                     status = -EINVAL;
1358         unsigned long           flags;
1359         struct ehci_iso_stream  *stream;
1360
1361         /* Get iso_stream head */
1362         stream = iso_stream_find (ehci, urb);
1363         if (unlikely (stream == 0)) {
1364                 ehci_dbg (ehci, "can't get iso stream\n");
1365                 return -ENOMEM;
1366         }
1367         if (unlikely (urb->interval != stream->interval)) {
1368                 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1369                         stream->interval, urb->interval);
1370                 goto done;
1371         }
1372
1373 #ifdef EHCI_URB_TRACE
1374         ehci_dbg (ehci,
1375                 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1376                 __FUNCTION__, urb->dev->devpath, urb,
1377                 usb_pipeendpoint (urb->pipe),
1378                 usb_pipein (urb->pipe) ? "in" : "out",
1379                 urb->transfer_buffer_length,
1380                 urb->number_of_packets, urb->interval,
1381                 stream);
1382 #endif
1383
1384         /* allocate ITDs w/o locking anything */
1385         status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1386         if (unlikely (status < 0)) {
1387                 ehci_dbg (ehci, "can't init itds\n");
1388                 goto done;
1389         }
1390
1391         /* schedule ... need to lock */
1392         spin_lock_irqsave (&ehci->lock, flags);
1393         status = iso_stream_schedule (ehci, urb, stream);
1394         if (likely (status == 0))
1395                 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1396         spin_unlock_irqrestore (&ehci->lock, flags);
1397
1398 done:
1399         if (unlikely (status < 0))
1400                 iso_stream_put (ehci, stream);
1401         return status;
1402 }
1403
1404 #ifdef CONFIG_USB_EHCI_SPLIT_ISO
1405
1406 /*-------------------------------------------------------------------------*/
1407
1408 /*
1409  * "Split ISO TDs" ... used for USB 1.1 devices going through the
1410  * TTs in USB 2.0 hubs.  These need microframe scheduling.
1411  */
1412
1413 static inline void
1414 sitd_sched_init (
1415         struct ehci_iso_sched   *iso_sched,
1416         struct ehci_iso_stream  *stream,
1417         struct urb              *urb
1418 )
1419 {
1420         unsigned        i;
1421         dma_addr_t      dma = urb->transfer_dma;
1422
1423         /* how many frames are needed for these transfers */
1424         iso_sched->span = urb->number_of_packets * stream->interval;
1425
1426         /* figure out per-frame sitd fields that we'll need later
1427          * when we fit new sitds into the schedule.
1428          */
1429         for (i = 0; i < urb->number_of_packets; i++) {
1430                 struct ehci_iso_packet  *packet = &iso_sched->packet [i];
1431                 unsigned                length;
1432                 dma_addr_t              buf;
1433                 u32                     trans;
1434
1435                 length = urb->iso_frame_desc [i].length & 0x03ff;
1436                 buf = dma + urb->iso_frame_desc [i].offset;
1437
1438                 trans = SITD_STS_ACTIVE;
1439                 if (((i + 1) == urb->number_of_packets)
1440                                 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1441                         trans |= SITD_IOC;
1442                 trans |= length << 16;
1443                 packet->transaction = cpu_to_le32 (trans);
1444
1445                 /* might need to cross a buffer page within a td */
1446                 packet->bufp = buf;
1447                 buf += length;
1448                 packet->buf1 = buf & ~0x0fff;
1449                 if (packet->buf1 != (buf & ~(u64)0x0fff))
1450                         packet->cross = 1;
1451
1452                 /* OUT uses multiple start-splits */ 
1453                 if (stream->bEndpointAddress & USB_DIR_IN)
1454                         continue;
1455                 length = 1 + (length / 188);
1456                 packet->buf1 |= length;
1457                 if (length > 1) /* BEGIN vs ALL */
1458                         packet->buf1 |= 1 << 3;
1459         }
1460 }
1461
1462 static int
1463 sitd_urb_transaction (
1464         struct ehci_iso_stream  *stream,
1465         struct ehci_hcd         *ehci,
1466         struct urb              *urb,
1467         int                     mem_flags
1468 )
1469 {
1470         struct ehci_sitd        *sitd;
1471         dma_addr_t              sitd_dma;
1472         int                     i;
1473         struct ehci_iso_sched   *iso_sched;
1474         unsigned long           flags;
1475
1476         iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1477         if (iso_sched == 0)
1478                 return -ENOMEM;
1479
1480         sitd_sched_init (iso_sched, stream, urb);
1481
1482         /* allocate/init sITDs */
1483         spin_lock_irqsave (&ehci->lock, flags);
1484         for (i = 0; i < urb->number_of_packets; i++) {
1485
1486                 /* NOTE:  for now, we don't try to handle wraparound cases
1487                  * for IN (using sitd->hw_backpointer, like a FSTN), which
1488                  * means we never need two sitds for full speed packets.
1489                  */
1490
1491                 /* free_list.next might be cache-hot ... but maybe
1492                  * the HC caches it too. avoid that issue for now.
1493                  */
1494
1495                 /* prefer previously-allocated sitds */
1496                 if (!list_empty(&stream->free_list)) {
1497                         sitd = list_entry (stream->free_list.prev,
1498                                          struct ehci_sitd, sitd_list);
1499                         list_del (&sitd->sitd_list);
1500                         sitd_dma = sitd->sitd_dma;
1501                 } else
1502                         sitd = 0;
1503
1504                 if (!sitd) {
1505                         spin_unlock_irqrestore (&ehci->lock, flags);
1506                         sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1507                                         &sitd_dma);
1508                         spin_lock_irqsave (&ehci->lock, flags);
1509                 }
1510
1511                 if (!sitd) {
1512                         iso_sched_free (stream, iso_sched);
1513                         spin_unlock_irqrestore (&ehci->lock, flags);
1514                         return -ENOMEM;
1515                 }
1516                 memset (sitd, 0, sizeof *sitd);
1517                 sitd->sitd_dma = sitd_dma;
1518                 list_add (&sitd->sitd_list, &iso_sched->td_list);
1519         }
1520
1521         /* temporarily store schedule info in hcpriv */
1522         urb->hcpriv = iso_sched;
1523         urb->error_count = 0;
1524
1525         spin_unlock_irqrestore (&ehci->lock, flags);
1526         return 0;
1527 }
1528
1529 /*-------------------------------------------------------------------------*/
1530
1531 static inline void
1532 sitd_patch (
1533         struct ehci_iso_stream  *stream,
1534         struct ehci_sitd        *sitd,
1535         struct ehci_iso_sched   *iso_sched,
1536         unsigned                index
1537 )
1538 {
1539         struct ehci_iso_packet  *uf = &iso_sched->packet [index];
1540         u64                     bufp = uf->bufp;
1541
1542         sitd->hw_next = EHCI_LIST_END;
1543         sitd->hw_fullspeed_ep = stream->address;
1544         sitd->hw_uframe = stream->splits;
1545         sitd->hw_results = uf->transaction;
1546         sitd->hw_backpointer = EHCI_LIST_END;
1547
1548         bufp = uf->bufp;
1549         sitd->hw_buf [0] = cpu_to_le32 (bufp);
1550         sitd->hw_buf_hi [0] = cpu_to_le32 (bufp >> 32);
1551
1552         sitd->hw_buf [1] = cpu_to_le32 (uf->buf1);
1553         if (uf->cross) {
1554                 bufp += 4096;
1555                 sitd->hw_buf_hi [1] = cpu_to_le32 (bufp >> 32);
1556         }
1557         sitd->index = index;
1558 }
1559
1560 static inline void
1561 sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1562 {
1563         /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1564         sitd->sitd_next = ehci->pshadow [frame];
1565         sitd->hw_next = ehci->periodic [frame];
1566         ehci->pshadow [frame].sitd = sitd;
1567         sitd->frame = frame;
1568         wmb ();
1569         ehci->periodic [frame] = cpu_to_le32 (sitd->sitd_dma) | Q_TYPE_SITD;
1570 }
1571
1572 /* fit urb's sitds into the selected schedule slot; activate as needed */
1573 static int
1574 sitd_link_urb (
1575         struct ehci_hcd         *ehci,
1576         struct urb              *urb,
1577         unsigned                mod,
1578         struct ehci_iso_stream  *stream
1579 )
1580 {
1581         int                     packet;
1582         unsigned                next_uframe;
1583         struct ehci_iso_sched   *sched = urb->hcpriv;
1584         struct ehci_sitd        *sitd;
1585
1586         next_uframe = stream->next_uframe;
1587
1588         if (list_empty(&stream->td_list)) {
1589                 /* usbfs ignores TT bandwidth */
1590                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1591                                 += stream->bandwidth;
1592                 ehci_vdbg (ehci,
1593                         "sched dev%s ep%d%s-iso [%d] %dms/%04x\n",
1594                         urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1595                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1596                         (next_uframe >> 3) % ehci->periodic_size,
1597                         stream->interval, le32_to_cpu (stream->splits));
1598                 stream->start = jiffies;
1599         }
1600         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs++;
1601
1602         /* fill sITDs frame by frame */
1603         for (packet = 0, sitd = 0;
1604                         packet < urb->number_of_packets;
1605                         packet++) {
1606
1607                 /* ASSERT:  we have all necessary sitds */
1608                 BUG_ON (list_empty (&sched->td_list));
1609
1610                 /* ASSERT:  no itds for this endpoint in this frame */
1611
1612                 sitd = list_entry (sched->td_list.next,
1613                                 struct ehci_sitd, sitd_list);
1614                 list_move_tail (&sitd->sitd_list, &stream->td_list);
1615                 sitd->stream = iso_stream_get (stream);
1616                 sitd->urb = usb_get_urb (urb);
1617
1618                 sitd_patch (stream, sitd, sched, packet);
1619                 sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
1620                                 sitd);
1621
1622                 next_uframe += stream->interval << 3;
1623                 stream->depth += stream->interval << 3;
1624         }
1625         stream->next_uframe = next_uframe % mod;
1626
1627         /* don't need that schedule data any more */
1628         iso_sched_free (stream, sched);
1629         urb->hcpriv = 0;
1630
1631         timer_action (ehci, TIMER_IO_WATCHDOG);
1632         if (!ehci->periodic_sched++)
1633                 return enable_periodic (ehci);
1634         return 0;
1635 }
1636
1637 /*-------------------------------------------------------------------------*/
1638
1639 #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
1640                         | SITD_STS_XACT | SITD_STS_MMF | SITD_STS_STS)
1641
1642 static unsigned
1643 sitd_complete (
1644         struct ehci_hcd         *ehci,
1645         struct ehci_sitd        *sitd,
1646         struct pt_regs          *regs
1647 ) {
1648         struct urb                              *urb = sitd->urb;
1649         struct usb_iso_packet_descriptor        *desc;
1650         u32                                     t;
1651         int                                     urb_index = -1;
1652         struct ehci_iso_stream                  *stream = sitd->stream;
1653         struct usb_device                       *dev;
1654
1655         urb_index = sitd->index;
1656         desc = &urb->iso_frame_desc [urb_index];
1657         t = le32_to_cpup (&sitd->hw_results);
1658
1659         /* report transfer status */
1660         if (t & SITD_ERRS) {
1661                 urb->error_count++;
1662                 if (t & SITD_STS_DBE)
1663                         desc->status = usb_pipein (urb->pipe)
1664                                 ? -ENOSR  /* hc couldn't read */
1665                                 : -ECOMM; /* hc couldn't write */
1666                 else if (t & SITD_STS_BABBLE)
1667                         desc->status = -EOVERFLOW;
1668                 else /* XACT, MMF, etc */
1669                         desc->status = -EPROTO;
1670         } else {
1671                 desc->status = 0;
1672                 desc->actual_length = desc->length - SITD_LENGTH (t);
1673         }
1674
1675         usb_put_urb (urb);
1676         sitd->urb = 0;
1677         sitd->stream = 0;
1678         list_move (&sitd->sitd_list, &stream->free_list);
1679         stream->depth -= stream->interval << 3;
1680         iso_stream_put (ehci, stream);
1681
1682         /* handle completion now? */
1683         if ((urb_index + 1) != urb->number_of_packets)
1684                 return 0;
1685
1686         /* ASSERT: it's really the last sitd for this urb
1687         list_for_each_entry (sitd, &stream->td_list, sitd_list)
1688                 BUG_ON (sitd->urb == urb);
1689          */
1690
1691         /* give urb back to the driver */
1692         dev = usb_get_dev (urb->dev);
1693         ehci_urb_done (ehci, urb, regs);
1694         urb = 0;
1695
1696         /* defer stopping schedule; completion can submit */
1697         ehci->periodic_sched--;
1698         if (!ehci->periodic_sched)
1699                 (void) disable_periodic (ehci);
1700         hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs--;
1701
1702         if (list_empty (&stream->td_list)) {
1703                 hcd_to_bus (&ehci->hcd)->bandwidth_allocated
1704                                 -= stream->bandwidth;
1705                 ehci_vdbg (ehci,
1706                         "deschedule devp %s ep%d%s-iso\n",
1707                         dev->devpath, stream->bEndpointAddress & 0x0f,
1708                         (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1709         }
1710         iso_stream_put (ehci, stream);
1711         usb_put_dev (dev);
1712
1713         return 1;
1714 }
1715
1716
1717 static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1718 {
1719         int                     status = -EINVAL;
1720         unsigned long           flags;
1721         struct ehci_iso_stream  *stream;
1722
1723         // FIXME remove when csplits behave
1724         if (usb_pipein(urb->pipe)) {
1725                 ehci_dbg (ehci, "no iso-IN split transactions yet\n");
1726                 return -ENOMEM;
1727         }
1728
1729         /* Get iso_stream head */
1730         stream = iso_stream_find (ehci, urb);
1731         if (stream == 0) {
1732                 ehci_dbg (ehci, "can't get iso stream\n");
1733                 return -ENOMEM;
1734         }
1735         if (urb->interval != stream->interval) {
1736                 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1737                         stream->interval, urb->interval);
1738                 goto done;
1739         }
1740
1741 #ifdef EHCI_URB_TRACE
1742         ehci_dbg (ehci,
1743                 "submit %p dev%s ep%d%s-iso len %d\n",
1744                 urb, urb->dev->devpath,
1745                 usb_pipeendpoint (urb->pipe),
1746                 usb_pipein (urb->pipe) ? "in" : "out",
1747                 urb->transfer_buffer_length);
1748 #endif
1749
1750         /* allocate SITDs */
1751         status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
1752         if (status < 0) {
1753                 ehci_dbg (ehci, "can't init sitds\n");
1754                 goto done;
1755         }
1756
1757         /* schedule ... need to lock */
1758         spin_lock_irqsave (&ehci->lock, flags);
1759         status = iso_stream_schedule (ehci, urb, stream);
1760         if (status == 0)
1761                 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1762         spin_unlock_irqrestore (&ehci->lock, flags);
1763
1764 done:
1765         if (status < 0)
1766                 iso_stream_put (ehci, stream);
1767         return status;
1768 }
1769
1770 #else
1771
1772 static inline int
1773 sitd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags)
1774 {
1775         ehci_dbg (ehci, "split iso support is disabled\n");
1776         return -ENOSYS;
1777 }
1778
1779 static inline unsigned
1780 sitd_complete (
1781         struct ehci_hcd         *ehci,
1782         struct ehci_sitd        *sitd,
1783         struct pt_regs          *regs
1784 ) {
1785         ehci_err (ehci, "sitd_complete %p?\n", sitd);
1786         return 0;
1787 }
1788
1789 #endif /* USB_EHCI_SPLIT_ISO */
1790
1791 /*-------------------------------------------------------------------------*/
1792
1793 static void
1794 scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
1795 {
1796         unsigned        frame, clock, now_uframe, mod;
1797         unsigned        modified;
1798
1799         mod = ehci->periodic_size << 3;
1800
1801         /*
1802          * When running, scan from last scan point up to "now"
1803          * else clean up by scanning everything that's left.
1804          * Touches as few pages as possible:  cache-friendly.
1805          */
1806         now_uframe = ehci->next_uframe;
1807         if (HCD_IS_RUNNING (ehci->hcd.state))
1808                 clock = readl (&ehci->regs->frame_index);
1809         else
1810                 clock = now_uframe + mod - 1;
1811         clock %= mod;
1812
1813         for (;;) {
1814                 union ehci_shadow       q, *q_p;
1815                 u32                     type, *hw_p;
1816                 unsigned                uframes;
1817
1818                 /* don't scan past the live uframe */
1819                 frame = now_uframe >> 3;
1820                 if (frame == (clock >> 3))
1821                         uframes = now_uframe & 0x07;
1822                 else {
1823                         /* safe to scan the whole frame at once */
1824                         now_uframe |= 0x07;
1825                         uframes = 8;
1826                 }
1827
1828 restart:
1829                 /* scan each element in frame's queue for completions */
1830                 q_p = &ehci->pshadow [frame];
1831                 hw_p = &ehci->periodic [frame];
1832                 q.ptr = q_p->ptr;
1833                 type = Q_NEXT_TYPE (*hw_p);
1834                 modified = 0;
1835
1836                 while (q.ptr != 0) {
1837                         unsigned                uf;
1838                         union ehci_shadow       temp;
1839
1840                         switch (type) {
1841                         case Q_TYPE_QH:
1842                                 /* handle any completions */
1843                                 temp.qh = qh_get (q.qh);
1844                                 type = Q_NEXT_TYPE (q.qh->hw_next);
1845                                 q = q.qh->qh_next;
1846                                 modified = qh_completions (ehci, temp.qh, regs);
1847                                 if (unlikely (list_empty (&temp.qh->qtd_list)))
1848                                         intr_deschedule (ehci, temp.qh, 0);
1849                                 qh_put (temp.qh);
1850                                 break;
1851                         case Q_TYPE_FSTN:
1852                                 /* for "save place" FSTNs, look at QH entries
1853                                  * in the previous frame for completions.
1854                                  */
1855                                 if (q.fstn->hw_prev != EHCI_LIST_END) {
1856                                         dbg ("ignoring completions from FSTNs");
1857                                 }
1858                                 type = Q_NEXT_TYPE (q.fstn->hw_next);
1859                                 q = q.fstn->fstn_next;
1860                                 break;
1861                         case Q_TYPE_ITD:
1862                                 /* skip itds for later in the frame */
1863                                 rmb ();
1864                                 for (uf = uframes; uf < 8; uf++) {
1865                                         if (0 == (q.itd->hw_transaction [uf]
1866                                                         & ITD_ACTIVE))
1867                                                 continue;
1868                                         q_p = &q.itd->itd_next;
1869                                         hw_p = &q.itd->hw_next;
1870                                         type = Q_NEXT_TYPE (q.itd->hw_next);
1871                                         q = *q_p;
1872                                         break;
1873                                 }
1874                                 if (uf != 8)
1875                                         break;
1876
1877                                 /* this one's ready ... HC won't cache the
1878                                  * pointer for much longer, if at all.
1879                                  */
1880                                 *q_p = q.itd->itd_next;
1881                                 *hw_p = q.itd->hw_next;
1882                                 type = Q_NEXT_TYPE (q.itd->hw_next);
1883                                 wmb();
1884                                 modified = itd_complete (ehci, q.itd, regs);
1885                                 q = *q_p;
1886                                 break;
1887                         case Q_TYPE_SITD:
1888                                 if (q.sitd->hw_results & SITD_ACTIVE) {
1889                                         q_p = &q.sitd->sitd_next;
1890                                         hw_p = &q.sitd->hw_next;
1891                                         type = Q_NEXT_TYPE (q.sitd->hw_next);
1892                                         q = *q_p;
1893                                         break;
1894                                 }
1895                                 *q_p = q.sitd->sitd_next;
1896                                 *hw_p = q.sitd->hw_next;
1897                                 type = Q_NEXT_TYPE (q.sitd->hw_next);
1898                                 wmb();
1899                                 modified = sitd_complete (ehci, q.sitd, regs);
1900                                 q = *q_p;
1901                                 break;
1902                         default:
1903                                 dbg ("corrupt type %d frame %d shadow %p",
1904                                         type, frame, q.ptr);
1905                                 // BUG ();
1906                                 q.ptr = 0;
1907                         }
1908
1909                         /* assume completion callbacks modify the queue */
1910                         if (unlikely (modified))
1911                                 goto restart;
1912                 }
1913
1914                 /* stop when we catch up to the HC */
1915
1916                 // FIXME:  this assumes we won't get lapped when
1917                 // latencies climb; that should be rare, but...
1918                 // detect it, and just go all the way around.
1919                 // FLR might help detect this case, so long as latencies
1920                 // don't exceed periodic_size msec (default 1.024 sec).
1921
1922                 // FIXME:  likewise assumes HC doesn't halt mid-scan
1923
1924                 if (now_uframe == clock) {
1925                         unsigned        now;
1926
1927                         if (!HCD_IS_RUNNING (ehci->hcd.state))
1928                                 break;
1929                         ehci->next_uframe = now_uframe;
1930                         now = readl (&ehci->regs->frame_index) % mod;
1931                         if (now_uframe == now)
1932                                 break;
1933
1934                         /* rescan the rest of this frame, then ... */
1935                         clock = now;
1936                 } else {
1937                         now_uframe++;
1938                         now_uframe %= mod;
1939                 }
1940         } 
1941 }