]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/host/ehci-sched.c
Merge remote-tracking branch 'asoc/fix/wm2200' into tmp
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / host / ehci-sched.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2001-2004 by David Brownell
3 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
53bd6a60 4 *
1da177e4
LT
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
37static int ehci_get_frame (struct usb_hcd *hcd);
38
1da177e4
LT
39/*
40 * periodic_next_shadow - return "next" pointer on shadow list
41 * @periodic: host pointer to qh/itd/sitd
42 * @tag: hardware tag for type of this record
43 */
44static union ehci_shadow *
6dbd682b
SR
45periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
46 __hc32 tag)
1da177e4 47{
6dbd682b 48 switch (hc32_to_cpu(ehci, tag)) {
1da177e4
LT
49 case Q_TYPE_QH:
50 return &periodic->qh->qh_next;
51 case Q_TYPE_FSTN:
52 return &periodic->fstn->fstn_next;
53 case Q_TYPE_ITD:
54 return &periodic->itd->itd_next;
55 // case Q_TYPE_SITD:
56 default:
57 return &periodic->sitd->sitd_next;
58 }
59}
60
3807e26d
AD
61static __hc32 *
62shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
63 __hc32 tag)
64{
65 switch (hc32_to_cpu(ehci, tag)) {
66 /* our ehci_shadow.qh is actually software part */
67 case Q_TYPE_QH:
68 return &periodic->qh->hw->hw_next;
69 /* others are hw parts */
70 default:
71 return periodic->hw_next;
72 }
73}
74
1da177e4
LT
75/* caller must hold ehci->lock */
76static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
77{
6dbd682b
SR
78 union ehci_shadow *prev_p = &ehci->pshadow[frame];
79 __hc32 *hw_p = &ehci->periodic[frame];
1da177e4
LT
80 union ehci_shadow here = *prev_p;
81
82 /* find predecessor of "ptr"; hw and shadow lists are in sync */
83 while (here.ptr && here.ptr != ptr) {
6dbd682b
SR
84 prev_p = periodic_next_shadow(ehci, prev_p,
85 Q_NEXT_TYPE(ehci, *hw_p));
3807e26d
AD
86 hw_p = shadow_next_periodic(ehci, &here,
87 Q_NEXT_TYPE(ehci, *hw_p));
1da177e4
LT
88 here = *prev_p;
89 }
90 /* an interrupt entry (at list end) could have been shared */
91 if (!here.ptr)
92 return;
93
94 /* update shadow and hardware lists ... the old "next" pointers
95 * from ptr may still be in use, the caller updates them.
96 */
6dbd682b
SR
97 *prev_p = *periodic_next_shadow(ehci, &here,
98 Q_NEXT_TYPE(ehci, *hw_p));
3d091a6f
AX
99
100 if (!ehci->use_dummy_qh ||
101 *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p))
102 != EHCI_LIST_END(ehci))
103 *hw_p = *shadow_next_periodic(ehci, &here,
104 Q_NEXT_TYPE(ehci, *hw_p));
105 else
106 *hw_p = ehci->dummy->qh_dma;
1da177e4
LT
107}
108
109/* how many of the uframe's 125 usecs are allocated? */
110static unsigned short
111periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
112{
6dbd682b 113 __hc32 *hw_p = &ehci->periodic [frame];
1da177e4
LT
114 union ehci_shadow *q = &ehci->pshadow [frame];
115 unsigned usecs = 0;
3807e26d 116 struct ehci_qh_hw *hw;
1da177e4
LT
117
118 while (q->ptr) {
6dbd682b 119 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
1da177e4 120 case Q_TYPE_QH:
3807e26d 121 hw = q->qh->hw;
1da177e4 122 /* is it in the S-mask? */
3807e26d 123 if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
1da177e4
LT
124 usecs += q->qh->usecs;
125 /* ... or C-mask? */
3807e26d 126 if (hw->hw_info2 & cpu_to_hc32(ehci,
6dbd682b 127 1 << (8 + uframe)))
1da177e4 128 usecs += q->qh->c_usecs;
3807e26d 129 hw_p = &hw->hw_next;
1da177e4
LT
130 q = &q->qh->qh_next;
131 break;
132 // case Q_TYPE_FSTN:
133 default:
134 /* for "save place" FSTNs, count the relevant INTR
135 * bandwidth from the previous frame
136 */
6dbd682b 137 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
1da177e4
LT
138 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
139 }
140 hw_p = &q->fstn->hw_next;
141 q = &q->fstn->fstn_next;
142 break;
143 case Q_TYPE_ITD:
3b6fcfd0
KW
144 if (q->itd->hw_transaction[uframe])
145 usecs += q->itd->stream->usecs;
1da177e4
LT
146 hw_p = &q->itd->hw_next;
147 q = &q->itd->itd_next;
148 break;
149 case Q_TYPE_SITD:
150 /* is it in the S-mask? (count SPLIT, DATA) */
6dbd682b
SR
151 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
152 1 << uframe)) {
1da177e4 153 if (q->sitd->hw_fullspeed_ep &
6dbd682b 154 cpu_to_hc32(ehci, 1<<31))
1da177e4
LT
155 usecs += q->sitd->stream->usecs;
156 else /* worst case for OUT start-split */
157 usecs += HS_USECS_ISO (188);
158 }
159
160 /* ... C-mask? (count CSPLIT, DATA) */
161 if (q->sitd->hw_uframe &
6dbd682b 162 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
1da177e4
LT
163 /* worst case for IN complete-split */
164 usecs += q->sitd->stream->c_usecs;
165 }
166
167 hw_p = &q->sitd->hw_next;
168 q = &q->sitd->sitd_next;
169 break;
170 }
171 }
172#ifdef DEBUG
cc62a7eb 173 if (usecs > ehci->uframe_periodic_max)
1da177e4
LT
174 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
175 frame * 8 + uframe, usecs);
176#endif
177 return usecs;
178}
179
180/*-------------------------------------------------------------------------*/
181
182static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
183{
184 if (!dev1->tt || !dev2->tt)
185 return 0;
186 if (dev1->tt != dev2->tt)
187 return 0;
188 if (dev1->tt->multi)
189 return dev1->ttport == dev2->ttport;
190 else
191 return 1;
192}
193
ba47f66b
DS
194#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
195
196/* Which uframe does the low/fullspeed transfer start in?
197 *
198 * The parameter is the mask of ssplits in "H-frame" terms
199 * and this returns the transfer start uframe in "B-frame" terms,
200 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
201 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
202 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
203 */
6dbd682b 204static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
ba47f66b 205{
6dbd682b 206 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
ba47f66b
DS
207 if (!smask) {
208 ehci_err(ehci, "invalid empty smask!\n");
209 /* uframe 7 can't have bw so this will indicate failure */
210 return 7;
211 }
212 return ffs(smask) - 1;
213}
214
215static const unsigned char
216max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
217
218/* carryover low/fullspeed bandwidth that crosses uframe boundries */
219static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
220{
221 int i;
222 for (i=0; i<7; i++) {
223 if (max_tt_usecs[i] < tt_usecs[i]) {
224 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
225 tt_usecs[i] = max_tt_usecs[i];
226 }
227 }
228}
229
230/* How many of the tt's periodic downstream 1000 usecs are allocated?
231 *
232 * While this measures the bandwidth in terms of usecs/uframe,
233 * the low/fullspeed bus has no notion of uframes, so any particular
234 * low/fullspeed transfer can "carry over" from one uframe to the next,
235 * since the TT just performs downstream transfers in sequence.
236 *
dc0d5c1e 237 * For example two separate 100 usec transfers can start in the same uframe,
ba47f66b
DS
238 * and the second one would "carry over" 75 usecs into the next uframe.
239 */
240static void
241periodic_tt_usecs (
242 struct ehci_hcd *ehci,
243 struct usb_device *dev,
244 unsigned frame,
245 unsigned short tt_usecs[8]
246)
247{
6dbd682b 248 __hc32 *hw_p = &ehci->periodic [frame];
ba47f66b
DS
249 union ehci_shadow *q = &ehci->pshadow [frame];
250 unsigned char uf;
251
252 memset(tt_usecs, 0, 16);
253
254 while (q->ptr) {
6dbd682b 255 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
ba47f66b
DS
256 case Q_TYPE_ITD:
257 hw_p = &q->itd->hw_next;
258 q = &q->itd->itd_next;
259 continue;
260 case Q_TYPE_QH:
261 if (same_tt(dev, q->qh->dev)) {
3807e26d 262 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
ba47f66b
DS
263 tt_usecs[uf] += q->qh->tt_usecs;
264 }
3807e26d 265 hw_p = &q->qh->hw->hw_next;
ba47f66b
DS
266 q = &q->qh->qh_next;
267 continue;
268 case Q_TYPE_SITD:
269 if (same_tt(dev, q->sitd->urb->dev)) {
270 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
271 tt_usecs[uf] += q->sitd->stream->tt_usecs;
272 }
273 hw_p = &q->sitd->hw_next;
274 q = &q->sitd->sitd_next;
275 continue;
276 // case Q_TYPE_FSTN:
277 default:
6dbd682b
SR
278 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
279 frame);
ba47f66b
DS
280 hw_p = &q->fstn->hw_next;
281 q = &q->fstn->fstn_next;
282 }
283 }
284
285 carryover_tt_bandwidth(tt_usecs);
286
287 if (max_tt_usecs[7] < tt_usecs[7])
288 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
289 frame, tt_usecs[7] - max_tt_usecs[7]);
290}
291
292/*
293 * Return true if the device's tt's downstream bus is available for a
294 * periodic transfer of the specified length (usecs), starting at the
295 * specified frame/uframe. Note that (as summarized in section 11.19
296 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
297 * uframe.
298 *
299 * The uframe parameter is when the fullspeed/lowspeed transfer
300 * should be executed in "B-frame" terms, which is the same as the
301 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
302 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
303 * See the EHCI spec sec 4.5 and fig 4.7.
304 *
305 * This checks if the full/lowspeed bus, at the specified starting uframe,
306 * has the specified bandwidth available, according to rules listed
307 * in USB 2.0 spec section 11.18.1 fig 11-60.
308 *
309 * This does not check if the transfer would exceed the max ssplit
310 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
311 * since proper scheduling limits ssplits to less than 16 per uframe.
312 */
313static int tt_available (
314 struct ehci_hcd *ehci,
315 unsigned period,
316 struct usb_device *dev,
317 unsigned frame,
318 unsigned uframe,
319 u16 usecs
320)
321{
322 if ((period == 0) || (uframe >= 7)) /* error */
323 return 0;
324
325 for (; frame < ehci->periodic_size; frame += period) {
326 unsigned short tt_usecs[8];
327
328 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
329
330 ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
331 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
332 frame, usecs, uframe,
333 tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
334 tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
335
336 if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
337 ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
338 frame, uframe);
339 return 0;
340 }
341
342 /* special case for isoc transfers larger than 125us:
343 * the first and each subsequent fully used uframe
344 * must be empty, so as to not illegally delay
345 * already scheduled transactions
346 */
347 if (125 < usecs) {
c065c60e 348 int ufs = (usecs / 125);
ba47f66b
DS
349 int i;
350 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
351 if (0 < tt_usecs[i]) {
352 ehci_vdbg(ehci,
353 "multi-uframe xfer can't fit "
354 "in frame %d uframe %d\n",
355 frame, i);
356 return 0;
357 }
358 }
359
360 tt_usecs[uframe] += usecs;
361
362 carryover_tt_bandwidth(tt_usecs);
363
364 /* fail if the carryover pushed bw past the last uframe's limit */
365 if (max_tt_usecs[7] < tt_usecs[7]) {
366 ehci_vdbg(ehci,
367 "tt unavailable usecs %d frame %d uframe %d\n",
368 usecs, frame, uframe);
369 return 0;
370 }
371 }
372
373 return 1;
374}
375
376#else
377
1da177e4
LT
378/* return true iff the device's transaction translator is available
379 * for a periodic transfer starting at the specified frame, using
380 * all the uframes in the mask.
381 */
382static int tt_no_collision (
383 struct ehci_hcd *ehci,
384 unsigned period,
385 struct usb_device *dev,
386 unsigned frame,
387 u32 uf_mask
388)
389{
390 if (period == 0) /* error */
391 return 0;
392
393 /* note bandwidth wastage: split never follows csplit
394 * (different dev or endpoint) until the next uframe.
395 * calling convention doesn't make that distinction.
396 */
397 for (; frame < ehci->periodic_size; frame += period) {
398 union ehci_shadow here;
6dbd682b 399 __hc32 type;
3807e26d 400 struct ehci_qh_hw *hw;
1da177e4
LT
401
402 here = ehci->pshadow [frame];
6dbd682b 403 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
1da177e4 404 while (here.ptr) {
6dbd682b 405 switch (hc32_to_cpu(ehci, type)) {
1da177e4 406 case Q_TYPE_ITD:
6dbd682b 407 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
1da177e4
LT
408 here = here.itd->itd_next;
409 continue;
410 case Q_TYPE_QH:
3807e26d 411 hw = here.qh->hw;
1da177e4
LT
412 if (same_tt (dev, here.qh->dev)) {
413 u32 mask;
414
6dbd682b 415 mask = hc32_to_cpu(ehci,
3807e26d 416 hw->hw_info2);
1da177e4
LT
417 /* "knows" no gap is needed */
418 mask |= mask >> 8;
419 if (mask & uf_mask)
420 break;
421 }
3807e26d 422 type = Q_NEXT_TYPE(ehci, hw->hw_next);
1da177e4
LT
423 here = here.qh->qh_next;
424 continue;
425 case Q_TYPE_SITD:
426 if (same_tt (dev, here.sitd->urb->dev)) {
427 u16 mask;
428
6dbd682b 429 mask = hc32_to_cpu(ehci, here.sitd
1da177e4
LT
430 ->hw_uframe);
431 /* FIXME assumes no gap for IN! */
432 mask |= mask >> 8;
433 if (mask & uf_mask)
434 break;
435 }
6dbd682b 436 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
1da177e4
LT
437 here = here.sitd->sitd_next;
438 continue;
439 // case Q_TYPE_FSTN:
440 default:
441 ehci_dbg (ehci,
442 "periodic frame %d bogus type %d\n",
443 frame, type);
444 }
445
446 /* collision or error */
447 return 0;
448 }
449 }
450
451 /* no collision */
452 return 1;
453}
454
ba47f66b
DS
455#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
456
1da177e4
LT
457/*-------------------------------------------------------------------------*/
458
b015cb79 459static void enable_periodic(struct ehci_hcd *ehci)
1da177e4 460{
3ca9aeba 461 if (ehci->periodic_count++)
b015cb79 462 return;
01c17142 463
3ca9aeba
AS
464 /* Stop waiting to turn off the periodic schedule */
465 ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
1da177e4 466
3ca9aeba
AS
467 /* Don't start the schedule until PSS is 0 */
468 ehci_poll_PSS(ehci);
18aafe64 469 turn_on_io_watchdog(ehci);
1da177e4
LT
470}
471
b015cb79 472static void disable_periodic(struct ehci_hcd *ehci)
1da177e4 473{
3ca9aeba 474 if (--ehci->periodic_count)
b015cb79 475 return;
01c17142 476
3ca9aeba
AS
477 /* Don't turn off the schedule until PSS is 1 */
478 ehci_poll_PSS(ehci);
1da177e4
LT
479}
480
481/*-------------------------------------------------------------------------*/
482
483/* periodic schedule slots have iso tds (normal or split) first, then a
484 * sparse tree for active interrupt transfers.
485 *
486 * this just links in a qh; caller guarantees uframe masks are set right.
487 * no FSTN support (yet; ehci 0.96+)
488 */
b015cb79 489static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
1da177e4
LT
490{
491 unsigned i;
492 unsigned period = qh->period;
493
494 dev_dbg (&qh->dev->dev,
495 "link qh%d-%04x/%p start %d [%d/%d us]\n",
3807e26d
AD
496 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
497 & (QH_CMASK | QH_SMASK),
1da177e4
LT
498 qh, qh->start, qh->usecs, qh->c_usecs);
499
500 /* high bandwidth, or otherwise every microframe */
501 if (period == 0)
502 period = 1;
503
504 for (i = qh->start; i < ehci->periodic_size; i += period) {
6dbd682b
SR
505 union ehci_shadow *prev = &ehci->pshadow[i];
506 __hc32 *hw_p = &ehci->periodic[i];
1da177e4 507 union ehci_shadow here = *prev;
6dbd682b 508 __hc32 type = 0;
1da177e4
LT
509
510 /* skip the iso nodes at list head */
511 while (here.ptr) {
6dbd682b
SR
512 type = Q_NEXT_TYPE(ehci, *hw_p);
513 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1da177e4 514 break;
6dbd682b 515 prev = periodic_next_shadow(ehci, prev, type);
3807e26d 516 hw_p = shadow_next_periodic(ehci, &here, type);
1da177e4
LT
517 here = *prev;
518 }
519
520 /* sorting each branch by period (slow-->fast)
521 * enables sharing interior tree nodes
522 */
523 while (here.ptr && qh != here.qh) {
524 if (qh->period > here.qh->period)
525 break;
526 prev = &here.qh->qh_next;
3807e26d 527 hw_p = &here.qh->hw->hw_next;
1da177e4
LT
528 here = *prev;
529 }
530 /* link in this qh, unless some earlier pass did that */
531 if (qh != here.qh) {
532 qh->qh_next = here;
533 if (here.qh)
3807e26d 534 qh->hw->hw_next = *hw_p;
1da177e4
LT
535 wmb ();
536 prev->qh = qh;
6dbd682b 537 *hw_p = QH_NEXT (ehci, qh->qh_dma);
1da177e4
LT
538 }
539 }
540 qh->qh_state = QH_STATE_LINKED;
ef4638f9 541 qh->xacterrs = 0;
1da177e4
LT
542
543 /* update per-qh bandwidth for usbfs */
544 ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
545 ? ((qh->usecs + qh->c_usecs) / qh->period)
546 : (qh->usecs * 8);
547
569b394f
AS
548 list_add(&qh->intr_node, &ehci->intr_qh_list);
549
1da177e4 550 /* maybe enable periodic schedule processing */
569b394f 551 ++ehci->intr_count;
b015cb79 552 enable_periodic(ehci);
1da177e4
LT
553}
554
b015cb79 555static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
1da177e4
LT
556{
557 unsigned i;
558 unsigned period;
559
df202255
AS
560 /*
561 * If qh is for a low/full-speed device, simply unlinking it
562 * could interfere with an ongoing split transaction. To unlink
563 * it safely would require setting the QH_INACTIVATE bit and
564 * waiting at least one frame, as described in EHCI 4.12.2.5.
565 *
566 * We won't bother with any of this. Instead, we assume that the
567 * only reason for unlinking an interrupt QH while the current URB
568 * is still active is to dequeue all the URBs (flush the whole
569 * endpoint queue).
570 *
571 * If rebalancing the periodic schedule is ever implemented, this
572 * approach will no longer be valid.
573 */
1da177e4
LT
574
575 /* high bandwidth, or otherwise part of every microframe */
576 if ((period = qh->period) == 0)
577 period = 1;
578
579 for (i = qh->start; i < ehci->periodic_size; i += period)
580 periodic_unlink (ehci, i, qh);
581
582 /* update per-qh bandwidth for usbfs */
583 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
584 ? ((qh->usecs + qh->c_usecs) / qh->period)
585 : (qh->usecs * 8);
586
587 dev_dbg (&qh->dev->dev,
588 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
7dedacf4 589 qh->period,
3807e26d 590 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
1da177e4
LT
591 qh, qh->start, qh->usecs, qh->c_usecs);
592
593 /* qh->qh_next still "live" to HC */
594 qh->qh_state = QH_STATE_UNLINK;
595 qh->qh_next.ptr = NULL;
569b394f
AS
596
597 if (ehci->qh_scan_next == qh)
598 ehci->qh_scan_next = list_entry(qh->intr_node.next,
599 struct ehci_qh, intr_node);
600 list_del(&qh->intr_node);
1da177e4
LT
601}
602
df202255 603static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
1da177e4 604{
a448c9d8
AS
605 /* If the QH isn't linked then there's nothing we can do
606 * unless we were called during a giveback, in which case
607 * qh_completions() has to deal with it.
608 */
609 if (qh->qh_state != QH_STATE_LINKED) {
610 if (qh->qh_state == QH_STATE_COMPLETING)
611 qh->needs_rescan = 1;
612 return;
613 }
1da177e4
LT
614
615 qh_unlink_periodic (ehci, qh);
616
df202255
AS
617 /* Make sure the unlinks are visible before starting the timer */
618 wmb();
619
620 /*
621 * The EHCI spec doesn't say how long it takes the controller to
622 * stop accessing an unlinked interrupt QH. The timer delay is
623 * 9 uframes; presumably that will be long enough.
1da177e4 624 */
df202255
AS
625 qh->unlink_cycle = ehci->intr_unlink_cycle;
626
627 /* New entries go at the end of the intr_unlink list */
628 if (ehci->intr_unlink)
629 ehci->intr_unlink_last->unlink_next = qh;
1da177e4 630 else
df202255
AS
631 ehci->intr_unlink = qh;
632 ehci->intr_unlink_last = qh;
633
634 if (ehci->intr_unlinking)
635 ; /* Avoid recursive calls */
636 else if (ehci->rh_state < EHCI_RH_RUNNING)
637 ehci_handle_intr_unlinks(ehci);
638 else if (ehci->intr_unlink == qh) {
639 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
640 ++ehci->intr_unlink_cycle;
641 }
642}
643
644static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
645{
646 struct ehci_qh_hw *hw = qh->hw;
647 int rc;
1da177e4 648
1da177e4 649 qh->qh_state = QH_STATE_IDLE;
3807e26d 650 hw->hw_next = EHCI_LIST_END(ehci);
a448c9d8
AS
651
652 qh_completions(ehci, qh);
653
654 /* reschedule QH iff another request is queued */
df202255 655 if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
a448c9d8
AS
656 rc = qh_schedule(ehci, qh);
657
658 /* An error here likely indicates handshake failure
659 * or no space left in the schedule. Neither fault
660 * should happen often ...
661 *
662 * FIXME kill the now-dysfunctional queued urbs
663 */
664 if (rc != 0)
665 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
666 qh, rc);
667 }
3ca9aeba
AS
668
669 /* maybe turn off periodic schedule */
569b394f 670 --ehci->intr_count;
3ca9aeba 671 disable_periodic(ehci);
1da177e4
LT
672}
673
674/*-------------------------------------------------------------------------*/
675
676static int check_period (
53bd6a60 677 struct ehci_hcd *ehci,
1da177e4
LT
678 unsigned frame,
679 unsigned uframe,
680 unsigned period,
681 unsigned usecs
682) {
683 int claimed;
684
685 /* complete split running into next frame?
686 * given FSTN support, we could sometimes check...
687 */
688 if (uframe >= 8)
689 return 0;
690
cc62a7eb
KS
691 /* convert "usecs we need" to "max already claimed" */
692 usecs = ehci->uframe_periodic_max - usecs;
1da177e4
LT
693
694 /* we "know" 2 and 4 uframe intervals were rejected; so
695 * for period 0, check _every_ microframe in the schedule.
696 */
697 if (unlikely (period == 0)) {
698 do {
699 for (uframe = 0; uframe < 7; uframe++) {
700 claimed = periodic_usecs (ehci, frame, uframe);
701 if (claimed > usecs)
702 return 0;
703 }
704 } while ((frame += 1) < ehci->periodic_size);
705
706 /* just check the specified uframe, at that period */
707 } else {
708 do {
709 claimed = periodic_usecs (ehci, frame, uframe);
710 if (claimed > usecs)
711 return 0;
712 } while ((frame += period) < ehci->periodic_size);
713 }
714
715 // success!
716 return 1;
717}
718
719static int check_intr_schedule (
53bd6a60 720 struct ehci_hcd *ehci,
1da177e4
LT
721 unsigned frame,
722 unsigned uframe,
723 const struct ehci_qh *qh,
6dbd682b 724 __hc32 *c_maskp
1da177e4
LT
725)
726{
53bd6a60 727 int retval = -ENOSPC;
ba47f66b 728 u8 mask = 0;
1da177e4
LT
729
730 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
731 goto done;
732
733 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
734 goto done;
735 if (!qh->c_usecs) {
736 retval = 0;
737 *c_maskp = 0;
738 goto done;
739 }
740
ba47f66b
DS
741#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
742 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
743 qh->tt_usecs)) {
744 unsigned i;
745
746 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
747 for (i=uframe+1; i<8 && i<uframe+4; i++)
748 if (!check_period (ehci, frame, i,
749 qh->period, qh->c_usecs))
750 goto done;
751 else
752 mask |= 1 << i;
753
754 retval = 0;
755
6dbd682b 756 *c_maskp = cpu_to_hc32(ehci, mask << 8);
ba47f66b
DS
757 }
758#else
1da177e4
LT
759 /* Make sure this tt's buffer is also available for CSPLITs.
760 * We pessimize a bit; probably the typical full speed case
761 * doesn't need the second CSPLIT.
53bd6a60 762 *
1da177e4
LT
763 * NOTE: both SPLIT and CSPLIT could be checked in just
764 * one smart pass...
765 */
766 mask = 0x03 << (uframe + qh->gap_uf);
6dbd682b 767 *c_maskp = cpu_to_hc32(ehci, mask << 8);
1da177e4
LT
768
769 mask |= 1 << uframe;
770 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
771 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
772 qh->period, qh->c_usecs))
773 goto done;
774 if (!check_period (ehci, frame, uframe + qh->gap_uf,
775 qh->period, qh->c_usecs))
776 goto done;
777 retval = 0;
778 }
ba47f66b 779#endif
1da177e4
LT
780done:
781 return retval;
782}
783
784/* "first fit" scheduling policy used the first time through,
785 * or when the previous schedule slot can't be re-used.
786 */
6dbd682b 787static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
1da177e4 788{
53bd6a60 789 int status;
1da177e4 790 unsigned uframe;
6dbd682b 791 __hc32 c_mask;
1da177e4 792 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3807e26d 793 struct ehci_qh_hw *hw = qh->hw;
1da177e4
LT
794
795 qh_refresh(ehci, qh);
3807e26d 796 hw->hw_next = EHCI_LIST_END(ehci);
1da177e4
LT
797 frame = qh->start;
798
799 /* reuse the previous schedule slots, if we can */
800 if (frame < qh->period) {
3807e26d 801 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
1da177e4
LT
802 status = check_intr_schedule (ehci, frame, --uframe,
803 qh, &c_mask);
804 } else {
805 uframe = 0;
806 c_mask = 0;
807 status = -ENOSPC;
808 }
809
810 /* else scan the schedule to find a group of slots such that all
811 * uframes have enough periodic bandwidth available.
812 */
813 if (status) {
814 /* "normal" case, uframing flexible except with splits */
815 if (qh->period) {
68335e81
AS
816 int i;
817
818 for (i = qh->period; status && i > 0; --i) {
819 frame = ++ehci->random_frame % qh->period;
1da177e4
LT
820 for (uframe = 0; uframe < 8; uframe++) {
821 status = check_intr_schedule (ehci,
822 frame, uframe, qh,
823 &c_mask);
824 if (status == 0)
825 break;
826 }
68335e81 827 }
1da177e4
LT
828
829 /* qh->period == 0 means every uframe */
830 } else {
831 frame = 0;
832 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
833 }
834 if (status)
835 goto done;
836 qh->start = frame;
837
838 /* reset S-frame and (maybe) C-frame masks */
3807e26d
AD
839 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
840 hw->hw_info2 |= qh->period
6dbd682b
SR
841 ? cpu_to_hc32(ehci, 1 << uframe)
842 : cpu_to_hc32(ehci, QH_SMASK);
3807e26d 843 hw->hw_info2 |= c_mask;
1da177e4
LT
844 } else
845 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
846
847 /* stuff into the periodic schedule */
b015cb79 848 qh_link_periodic(ehci, qh);
1da177e4
LT
849done:
850 return status;
851}
852
853static int intr_submit (
854 struct ehci_hcd *ehci,
1da177e4
LT
855 struct urb *urb,
856 struct list_head *qtd_list,
55016f10 857 gfp_t mem_flags
1da177e4
LT
858) {
859 unsigned epnum;
860 unsigned long flags;
861 struct ehci_qh *qh;
e9df41c5 862 int status;
1da177e4
LT
863 struct list_head empty;
864
865 /* get endpoint and transfer/schedule data */
e9df41c5 866 epnum = urb->ep->desc.bEndpointAddress;
1da177e4
LT
867
868 spin_lock_irqsave (&ehci->lock, flags);
869
541c7d43 870 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
8de98402 871 status = -ESHUTDOWN;
e9df41c5 872 goto done_not_linked;
8de98402 873 }
e9df41c5
AS
874 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
875 if (unlikely(status))
876 goto done_not_linked;
8de98402 877
1da177e4
LT
878 /* get qh and force any scheduling errors */
879 INIT_LIST_HEAD (&empty);
e9df41c5 880 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
1da177e4
LT
881 if (qh == NULL) {
882 status = -ENOMEM;
883 goto done;
884 }
885 if (qh->qh_state == QH_STATE_IDLE) {
886 if ((status = qh_schedule (ehci, qh)) != 0)
887 goto done;
888 }
889
890 /* then queue the urb's tds to the qh */
e9df41c5 891 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
1da177e4
LT
892 BUG_ON (qh == NULL);
893
894 /* ... update usbfs periodic stats */
895 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
896
897done:
e9df41c5
AS
898 if (unlikely(status))
899 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
900done_not_linked:
1da177e4
LT
901 spin_unlock_irqrestore (&ehci->lock, flags);
902 if (status)
903 qtd_list_free (ehci, urb, qtd_list);
904
905 return status;
906}
907
569b394f
AS
908static void scan_intr(struct ehci_hcd *ehci)
909{
910 struct ehci_qh *qh;
911
912 list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
913 intr_node) {
914 rescan:
915 /* clean any finished work for this qh */
916 if (!list_empty(&qh->qtd_list)) {
917 int temp;
918
919 /*
920 * Unlinks could happen here; completion reporting
921 * drops the lock. That's why ehci->qh_scan_next
922 * always holds the next qh to scan; if the next qh
923 * gets unlinked then ehci->qh_scan_next is adjusted
924 * in qh_unlink_periodic().
925 */
926 temp = qh_completions(ehci, qh);
927 if (unlikely(qh->needs_rescan ||
928 (list_empty(&qh->qtd_list) &&
929 qh->qh_state == QH_STATE_LINKED)))
930 start_unlink_intr(ehci, qh);
931 else if (temp != 0)
932 goto rescan;
933 }
934 }
935}
936
1da177e4
LT
937/*-------------------------------------------------------------------------*/
938
939/* ehci_iso_stream ops work with both ITD and SITD */
940
941static struct ehci_iso_stream *
55016f10 942iso_stream_alloc (gfp_t mem_flags)
1da177e4
LT
943{
944 struct ehci_iso_stream *stream;
945
7b842b6e 946 stream = kzalloc(sizeof *stream, mem_flags);
1da177e4 947 if (likely (stream != NULL)) {
1da177e4
LT
948 INIT_LIST_HEAD(&stream->td_list);
949 INIT_LIST_HEAD(&stream->free_list);
950 stream->next_uframe = -1;
1da177e4
LT
951 }
952 return stream;
953}
954
955static void
956iso_stream_init (
957 struct ehci_hcd *ehci,
958 struct ehci_iso_stream *stream,
959 struct usb_device *dev,
960 int pipe,
961 unsigned interval
962)
963{
964 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
965
966 u32 buf1;
967 unsigned epnum, maxp;
968 int is_input;
969 long bandwidth;
970
971 /*
972 * this might be a "high bandwidth" highspeed endpoint,
973 * as encoded in the ep descriptor's wMaxPacket field
974 */
975 epnum = usb_pipeendpoint (pipe);
976 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
977 maxp = usb_maxpacket(dev, pipe, !is_input);
978 if (is_input) {
979 buf1 = (1 << 11);
980 } else {
981 buf1 = 0;
982 }
983
984 /* knows about ITD vs SITD */
985 if (dev->speed == USB_SPEED_HIGH) {
986 unsigned multi = hb_mult(maxp);
987
988 stream->highspeed = 1;
989
990 maxp = max_packet(maxp);
991 buf1 |= maxp;
992 maxp *= multi;
993
6dbd682b
SR
994 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
995 stream->buf1 = cpu_to_hc32(ehci, buf1);
996 stream->buf2 = cpu_to_hc32(ehci, multi);
1da177e4
LT
997
998 /* usbfs wants to report the average usecs per frame tied up
999 * when transfers on this endpoint are scheduled ...
1000 */
1001 stream->usecs = HS_USECS_ISO (maxp);
1002 bandwidth = stream->usecs * 8;
372dd6e8 1003 bandwidth /= interval;
1da177e4
LT
1004
1005 } else {
1006 u32 addr;
d0384200 1007 int think_time;
469d0229 1008 int hs_transfers;
1da177e4
LT
1009
1010 addr = dev->ttport << 24;
1011 if (!ehci_is_TDI(ehci)
1012 || (dev->tt->hub !=
1013 ehci_to_hcd(ehci)->self.root_hub))
1014 addr |= dev->tt->hub->devnum << 16;
1015 addr |= epnum << 8;
1016 addr |= dev->devnum;
1017 stream->usecs = HS_USECS_ISO (maxp);
d0384200
DB
1018 think_time = dev->tt ? dev->tt->think_time : 0;
1019 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1020 dev->speed, is_input, 1, maxp));
469d0229 1021 hs_transfers = max (1u, (maxp + 187) / 188);
1da177e4
LT
1022 if (is_input) {
1023 u32 tmp;
1024
1025 addr |= 1 << 31;
1026 stream->c_usecs = stream->usecs;
1027 stream->usecs = HS_USECS_ISO (1);
1028 stream->raw_mask = 1;
1029
469d0229
CL
1030 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1031 tmp = (1 << (hs_transfers + 2)) - 1;
1032 stream->raw_mask |= tmp << (8 + 2);
1da177e4 1033 } else
469d0229 1034 stream->raw_mask = smask_out [hs_transfers - 1];
1da177e4 1035 bandwidth = stream->usecs + stream->c_usecs;
372dd6e8 1036 bandwidth /= interval << 3;
1da177e4
LT
1037
1038 /* stream->splits gets created from raw_mask later */
6dbd682b 1039 stream->address = cpu_to_hc32(ehci, addr);
1da177e4
LT
1040 }
1041 stream->bandwidth = bandwidth;
1042
1043 stream->udev = dev;
1044
1045 stream->bEndpointAddress = is_input | epnum;
1046 stream->interval = interval;
1047 stream->maxp = maxp;
1048}
1049
1da177e4
LT
1050static struct ehci_iso_stream *
1051iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1052{
1053 unsigned epnum;
1054 struct ehci_iso_stream *stream;
1055 struct usb_host_endpoint *ep;
1056 unsigned long flags;
1057
1058 epnum = usb_pipeendpoint (urb->pipe);
1059 if (usb_pipein(urb->pipe))
1060 ep = urb->dev->ep_in[epnum];
1061 else
1062 ep = urb->dev->ep_out[epnum];
1063
1064 spin_lock_irqsave (&ehci->lock, flags);
1065 stream = ep->hcpriv;
1066
1067 if (unlikely (stream == NULL)) {
1068 stream = iso_stream_alloc(GFP_ATOMIC);
1069 if (likely (stream != NULL)) {
1da177e4
LT
1070 ep->hcpriv = stream;
1071 stream->ep = ep;
1072 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1073 urb->interval);
1074 }
1075
1082f57a
CL
1076 /* if dev->ep [epnum] is a QH, hw is set */
1077 } else if (unlikely (stream->hw != NULL)) {
1da177e4
LT
1078 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1079 urb->dev->devpath, epnum,
1080 usb_pipein(urb->pipe) ? "in" : "out");
1081 stream = NULL;
1082 }
1083
1da177e4
LT
1084 spin_unlock_irqrestore (&ehci->lock, flags);
1085 return stream;
1086}
1087
1088/*-------------------------------------------------------------------------*/
1089
1090/* ehci_iso_sched ops can be ITD-only or SITD-only */
1091
1092static struct ehci_iso_sched *
55016f10 1093iso_sched_alloc (unsigned packets, gfp_t mem_flags)
1da177e4
LT
1094{
1095 struct ehci_iso_sched *iso_sched;
1096 int size = sizeof *iso_sched;
1097
1098 size += packets * sizeof (struct ehci_iso_packet);
80b6ca48 1099 iso_sched = kzalloc(size, mem_flags);
1da177e4 1100 if (likely (iso_sched != NULL)) {
1da177e4
LT
1101 INIT_LIST_HEAD (&iso_sched->td_list);
1102 }
1103 return iso_sched;
1104}
1105
1106static inline void
6dbd682b
SR
1107itd_sched_init(
1108 struct ehci_hcd *ehci,
1da177e4
LT
1109 struct ehci_iso_sched *iso_sched,
1110 struct ehci_iso_stream *stream,
1111 struct urb *urb
1112)
1113{
1114 unsigned i;
1115 dma_addr_t dma = urb->transfer_dma;
1116
1117 /* how many uframes are needed for these transfers */
1118 iso_sched->span = urb->number_of_packets * stream->interval;
1119
1120 /* figure out per-uframe itd fields that we'll need later
1121 * when we fit new itds into the schedule.
1122 */
1123 for (i = 0; i < urb->number_of_packets; i++) {
1124 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1125 unsigned length;
1126 dma_addr_t buf;
1127 u32 trans;
1128
1129 length = urb->iso_frame_desc [i].length;
1130 buf = dma + urb->iso_frame_desc [i].offset;
1131
1132 trans = EHCI_ISOC_ACTIVE;
1133 trans |= buf & 0x0fff;
1134 if (unlikely (((i + 1) == urb->number_of_packets))
1135 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1136 trans |= EHCI_ITD_IOC;
1137 trans |= length << 16;
6dbd682b 1138 uframe->transaction = cpu_to_hc32(ehci, trans);
1da177e4 1139
77078570 1140 /* might need to cross a buffer page within a uframe */
1da177e4
LT
1141 uframe->bufp = (buf & ~(u64)0x0fff);
1142 buf += length;
1143 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1144 uframe->cross = 1;
1145 }
1146}
1147
1148static void
1149iso_sched_free (
1150 struct ehci_iso_stream *stream,
1151 struct ehci_iso_sched *iso_sched
1152)
1153{
1154 if (!iso_sched)
1155 return;
1156 // caller must hold ehci->lock!
1157 list_splice (&iso_sched->td_list, &stream->free_list);
1158 kfree (iso_sched);
1159}
1160
1161static int
1162itd_urb_transaction (
1163 struct ehci_iso_stream *stream,
1164 struct ehci_hcd *ehci,
1165 struct urb *urb,
55016f10 1166 gfp_t mem_flags
1da177e4
LT
1167)
1168{
1169 struct ehci_itd *itd;
1170 dma_addr_t itd_dma;
1171 int i;
1172 unsigned num_itds;
1173 struct ehci_iso_sched *sched;
1174 unsigned long flags;
1175
1176 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1177 if (unlikely (sched == NULL))
1178 return -ENOMEM;
1179
6dbd682b 1180 itd_sched_init(ehci, sched, stream, urb);
1da177e4
LT
1181
1182 if (urb->interval < 8)
1183 num_itds = 1 + (sched->span + 7) / 8;
1184 else
1185 num_itds = urb->number_of_packets;
1186
1187 /* allocate/init ITDs */
1188 spin_lock_irqsave (&ehci->lock, flags);
1189 for (i = 0; i < num_itds; i++) {
1190
55934eb3
AS
1191 /*
1192 * Use iTDs from the free list, but not iTDs that may
1193 * still be in use by the hardware.
1da177e4 1194 */
55934eb3
AS
1195 if (likely(!list_empty(&stream->free_list))) {
1196 itd = list_first_entry(&stream->free_list,
6dbd682b 1197 struct ehci_itd, itd_list);
f4289078 1198 if (itd->frame == ehci->now_frame)
55934eb3 1199 goto alloc_itd;
1da177e4
LT
1200 list_del (&itd->itd_list);
1201 itd_dma = itd->itd_dma;
3d01f0fe 1202 } else {
55934eb3 1203 alloc_itd:
1da177e4
LT
1204 spin_unlock_irqrestore (&ehci->lock, flags);
1205 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1206 &itd_dma);
1207 spin_lock_irqsave (&ehci->lock, flags);
3d01f0fe
KW
1208 if (!itd) {
1209 iso_sched_free(stream, sched);
1210 spin_unlock_irqrestore(&ehci->lock, flags);
1211 return -ENOMEM;
1212 }
1da177e4
LT
1213 }
1214
1da177e4
LT
1215 memset (itd, 0, sizeof *itd);
1216 itd->itd_dma = itd_dma;
1217 list_add (&itd->itd_list, &sched->td_list);
1218 }
1219 spin_unlock_irqrestore (&ehci->lock, flags);
1220
1221 /* temporarily store schedule info in hcpriv */
1222 urb->hcpriv = sched;
1223 urb->error_count = 0;
1224 return 0;
1225}
1226
1227/*-------------------------------------------------------------------------*/
1228
1229static inline int
1230itd_slot_ok (
1231 struct ehci_hcd *ehci,
1232 u32 mod,
1233 u32 uframe,
1234 u8 usecs,
1235 u32 period
1236)
1237{
1238 uframe %= period;
1239 do {
cc62a7eb 1240 /* can't commit more than uframe_periodic_max usec */
1da177e4 1241 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
cc62a7eb 1242 > (ehci->uframe_periodic_max - usecs))
1da177e4
LT
1243 return 0;
1244
1245 /* we know urb->interval is 2^N uframes */
1246 uframe += period;
1247 } while (uframe < mod);
1248 return 1;
1249}
1250
1251static inline int
1252sitd_slot_ok (
1253 struct ehci_hcd *ehci,
1254 u32 mod,
1255 struct ehci_iso_stream *stream,
1256 u32 uframe,
1257 struct ehci_iso_sched *sched,
1258 u32 period_uframes
1259)
1260{
1261 u32 mask, tmp;
1262 u32 frame, uf;
1263
1264 mask = stream->raw_mask << (uframe & 7);
1265
1266 /* for IN, don't wrap CSPLIT into the next frame */
1267 if (mask & ~0xffff)
1268 return 0;
1269
65b8e5cb
AS
1270 /* check bandwidth */
1271 uframe %= period_uframes;
1272 frame = uframe >> 3;
1273
1274#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1275 /* The tt's fullspeed bus bandwidth must be available.
1276 * tt_available scheduling guarantees 10+% for control/bulk.
1277 */
1278 uf = uframe & 7;
1279 if (!tt_available(ehci, period_uframes >> 3,
1280 stream->udev, frame, uf, stream->tt_usecs))
1281 return 0;
1282#else
1283 /* tt must be idle for start(s), any gap, and csplit.
1284 * assume scheduling slop leaves 10+% for control/bulk.
1285 */
1286 if (!tt_no_collision(ehci, period_uframes >> 3,
1287 stream->udev, frame, mask))
1288 return 0;
1289#endif
1290
1da177e4
LT
1291 /* this multi-pass logic is simple, but performance may
1292 * suffer when the schedule data isn't cached.
1293 */
1da177e4
LT
1294 do {
1295 u32 max_used;
1296
1297 frame = uframe >> 3;
1298 uf = uframe & 7;
1299
1da177e4 1300 /* check starts (OUT uses more than one) */
cc62a7eb 1301 max_used = ehci->uframe_periodic_max - stream->usecs;
1da177e4
LT
1302 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1303 if (periodic_usecs (ehci, frame, uf) > max_used)
1304 return 0;
1305 }
1306
1307 /* for IN, check CSPLIT */
1308 if (stream->c_usecs) {
0c734622 1309 uf = uframe & 7;
cc62a7eb 1310 max_used = ehci->uframe_periodic_max - stream->c_usecs;
1da177e4
LT
1311 do {
1312 tmp = 1 << uf;
1313 tmp <<= 8;
1314 if ((stream->raw_mask & tmp) == 0)
1315 continue;
1316 if (periodic_usecs (ehci, frame, uf)
1317 > max_used)
1318 return 0;
1319 } while (++uf < 8);
1320 }
1321
1322 /* we know urb->interval is 2^N uframes */
1323 uframe += period_uframes;
1324 } while (uframe < mod);
1325
6dbd682b 1326 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
1da177e4
LT
1327 return 1;
1328}
1329
1330/*
1331 * This scheduler plans almost as far into the future as it has actual
1332 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1333 * "as small as possible" to be cache-friendlier.) That limits the size
1334 * transfers you can stream reliably; avoid more than 64 msec per urb.
1335 * Also avoid queue depths of less than ehci's worst irq latency (affected
1336 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1337 * and other factors); or more than about 230 msec total (for portability,
1338 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1339 */
1340
c3ee9b76 1341#define SCHEDULING_DELAY 40 /* microframes */
1da177e4
LT
1342
1343static int
1344iso_stream_schedule (
1345 struct ehci_hcd *ehci,
1346 struct urb *urb,
1347 struct ehci_iso_stream *stream
1348)
1349{
c3ee9b76 1350 u32 now, base, next, start, period, span;
1da177e4
LT
1351 int status;
1352 unsigned mod = ehci->periodic_size << 3;
1353 struct ehci_iso_sched *sched = urb->hcpriv;
1354
ffda0803
AS
1355 period = urb->interval;
1356 span = sched->span;
1357 if (!stream->highspeed) {
1358 period <<= 3;
1359 span <<= 3;
1360 }
1361
68aa95d5 1362 now = ehci_read_frame_index(ehci) & (mod - 1);
1da177e4 1363
b40e43fc
AS
1364 /* Typical case: reuse current schedule, stream is still active.
1365 * Hopefully there are no gaps from the host falling behind
4005ad43
AS
1366 * (irq delays etc). If there are, the behavior depends on
1367 * whether URB_ISO_ASAP is set.
1da177e4
LT
1368 */
1369 if (likely (!list_empty (&stream->td_list))) {
dccd574c 1370
98cae42d
AS
1371 /* Take the isochronous scheduling threshold into account */
1372 if (ehci->i_thresh)
1373 next = now + ehci->i_thresh; /* uframe cache */
dccd574c 1374 else
98cae42d 1375 next = (now + 2 + 7) & ~0x07; /* full frame cache */
b40e43fc 1376
c3ee9b76
AS
1377 /*
1378 * Use ehci->last_iso_frame as the base. There can't be any
1379 * TDs scheduled for earlier than that.
1fb2e055 1380 */
c3ee9b76
AS
1381 base = ehci->last_iso_frame << 3;
1382 next = (next - base) & (mod - 1);
1383 start = (stream->next_uframe - base) & (mod - 1);
1384
1385 /* Is the schedule already full? */
1386 if (unlikely(start < period)) {
1387 ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
1388 urb, stream->next_uframe, base,
1389 period, mod);
1390 status = -ENOSPC;
b40e43fc
AS
1391 goto fail;
1392 }
c3ee9b76 1393
4005ad43
AS
1394 /* Behind the scheduling threshold? */
1395 if (unlikely(start < next)) {
1396
1397 /* USB_ISO_ASAP: Round up to the first available slot */
1398 if (urb->transfer_flags & URB_ISO_ASAP)
1399 start += (next - start + period - 1) & -period;
1400
1401 /*
1402 * Not ASAP: Use the next slot in the stream. If
1403 * the entire URB falls before the threshold, fail.
1404 */
1405 else if (start + span - period < next) {
1406 ehci_dbg(ehci, "iso urb late %p (%u+%u < %u)\n",
1407 urb, start + base,
1408 span - period, next + base);
1409 status = -EXDEV;
1410 goto fail;
1411 }
1412 }
c3ee9b76
AS
1413
1414 start += base;
1da177e4
LT
1415 }
1416
1417 /* need to schedule; when's the next (u)frame we could start?
1418 * this is bigger than ehci->i_thresh allows; scheduling itself
c3ee9b76 1419 * isn't free, the delay should handle reasonably slow cpus. it
1da177e4
LT
1420 * can also help high bandwidth if the dma and irq loads don't
1421 * jump until after the queue is primed.
1422 */
1fb2e055 1423 else {
e3420901 1424 int done = 0;
1fb2e055 1425
c3ee9b76
AS
1426 base = now & ~0x07;
1427 start = base + SCHEDULING_DELAY;
1fb2e055 1428
811c926c
TP
1429 /* find a uframe slot with enough bandwidth.
1430 * Early uframes are more precious because full-speed
1431 * iso IN transfers can't use late uframes,
1432 * and therefore they should be allocated last.
1433 */
1434 next = start;
1435 start += period;
1436 do {
1437 start--;
1fb2e055
AS
1438 /* check schedule: enough space? */
1439 if (stream->highspeed) {
1440 if (itd_slot_ok(ehci, mod, start,
1441 stream->usecs, period))
e3420901 1442 done = 1;
1fb2e055
AS
1443 } else {
1444 if ((start % 8) >= 6)
1445 continue;
1446 if (sitd_slot_ok(ehci, mod, stream,
1447 start, sched, period))
e3420901 1448 done = 1;
1fb2e055 1449 }
e3420901 1450 } while (start > next && !done);
1da177e4 1451
1fb2e055 1452 /* no room in the schedule */
e3420901 1453 if (!done) {
c3ee9b76 1454 ehci_dbg(ehci, "iso sched full %p", urb);
1fb2e055
AS
1455 status = -ENOSPC;
1456 goto fail;
1da177e4
LT
1457 }
1458 }
1459
1fb2e055 1460 /* Tried to schedule too far into the future? */
c3ee9b76
AS
1461 if (unlikely(start - base + span - period >= mod)) {
1462 ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
1463 urb, start - base, span - period, mod);
1fb2e055
AS
1464 status = -EFBIG;
1465 goto fail;
1466 }
1da177e4 1467
1fb2e055 1468 stream->next_uframe = start & (mod - 1);
1da177e4 1469
1da177e4
LT
1470 /* report high speed start in uframes; full speed, in frames */
1471 urb->start_frame = stream->next_uframe;
1472 if (!stream->highspeed)
1473 urb->start_frame >>= 3;
569b394f
AS
1474
1475 /* Make sure scan_isoc() sees these */
1476 if (ehci->isoc_count == 0)
c3ee9b76 1477 ehci->last_iso_frame = now >> 3;
1da177e4 1478 return 0;
1fb2e055
AS
1479
1480 fail:
1481 iso_sched_free(stream, sched);
1482 urb->hcpriv = NULL;
1483 return status;
1da177e4
LT
1484}
1485
1486/*-------------------------------------------------------------------------*/
1487
1488static inline void
6dbd682b
SR
1489itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1490 struct ehci_itd *itd)
1da177e4
LT
1491{
1492 int i;
1493
77078570 1494 /* it's been recently zeroed */
6dbd682b 1495 itd->hw_next = EHCI_LIST_END(ehci);
1da177e4
LT
1496 itd->hw_bufp [0] = stream->buf0;
1497 itd->hw_bufp [1] = stream->buf1;
1498 itd->hw_bufp [2] = stream->buf2;
1499
1500 for (i = 0; i < 8; i++)
1501 itd->index[i] = -1;
1502
1503 /* All other fields are filled when scheduling */
1504}
1505
1506static inline void
6dbd682b
SR
1507itd_patch(
1508 struct ehci_hcd *ehci,
1da177e4
LT
1509 struct ehci_itd *itd,
1510 struct ehci_iso_sched *iso_sched,
1511 unsigned index,
77078570 1512 u16 uframe
1da177e4
LT
1513)
1514{
1515 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1516 unsigned pg = itd->pg;
1517
1518 // BUG_ON (pg == 6 && uf->cross);
1519
1520 uframe &= 0x07;
1521 itd->index [uframe] = index;
1522
6dbd682b
SR
1523 itd->hw_transaction[uframe] = uf->transaction;
1524 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1525 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1526 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
1da177e4
LT
1527
1528 /* iso_frame_desc[].offset must be strictly increasing */
77078570 1529 if (unlikely (uf->cross)) {
1da177e4 1530 u64 bufp = uf->bufp + 4096;
6dbd682b 1531
1da177e4 1532 itd->pg = ++pg;
6dbd682b
SR
1533 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1534 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
1da177e4
LT
1535 }
1536}
1537
1538static inline void
1539itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1540{
92bc3648
CL
1541 union ehci_shadow *prev = &ehci->pshadow[frame];
1542 __hc32 *hw_p = &ehci->periodic[frame];
1543 union ehci_shadow here = *prev;
1544 __hc32 type = 0;
1545
1546 /* skip any iso nodes which might belong to previous microframes */
1547 while (here.ptr) {
1548 type = Q_NEXT_TYPE(ehci, *hw_p);
1549 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1550 break;
1551 prev = periodic_next_shadow(ehci, prev, type);
1552 hw_p = shadow_next_periodic(ehci, &here, type);
1553 here = *prev;
1554 }
1555
1556 itd->itd_next = here;
1557 itd->hw_next = *hw_p;
1558 prev->itd = itd;
1da177e4
LT
1559 itd->frame = frame;
1560 wmb ();
92bc3648 1561 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
1da177e4
LT
1562}
1563
1564/* fit urb's itds into the selected schedule slot; activate as needed */
b015cb79 1565static void itd_link_urb(
1da177e4
LT
1566 struct ehci_hcd *ehci,
1567 struct urb *urb,
1568 unsigned mod,
1569 struct ehci_iso_stream *stream
1570)
1571{
77078570 1572 int packet;
1da177e4
LT
1573 unsigned next_uframe, uframe, frame;
1574 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1575 struct ehci_itd *itd;
1576
bccbefaa 1577 next_uframe = stream->next_uframe & (mod - 1);
1da177e4
LT
1578
1579 if (unlikely (list_empty(&stream->td_list))) {
1580 ehci_to_hcd(ehci)->self.bandwidth_allocated
1581 += stream->bandwidth;
1582 ehci_vdbg (ehci,
1583 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1584 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1585 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1586 urb->interval,
1587 next_uframe >> 3, next_uframe & 0x7);
1da177e4 1588 }
05570297
AH
1589
1590 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
ad93562b
AX
1591 if (ehci->amd_pll_fix == 1)
1592 usb_amd_quirk_pll_disable();
05570297
AH
1593 }
1594
1da177e4
LT
1595 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1596
1597 /* fill iTDs uframe by uframe */
1598 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1599 if (itd == NULL) {
1600 /* ASSERT: we have all necessary itds */
1601 // BUG_ON (list_empty (&iso_sched->td_list));
1602
1603 /* ASSERT: no itds for this endpoint in this uframe */
1604
1605 itd = list_entry (iso_sched->td_list.next,
1606 struct ehci_itd, itd_list);
1607 list_move_tail (&itd->itd_list, &stream->td_list);
8c5bf7be 1608 itd->stream = stream;
508db8c9 1609 itd->urb = urb;
6dbd682b 1610 itd_init (ehci, stream, itd);
1da177e4
LT
1611 }
1612
1613 uframe = next_uframe & 0x07;
1614 frame = next_uframe >> 3;
1615
6dbd682b 1616 itd_patch(ehci, itd, iso_sched, packet, uframe);
1da177e4
LT
1617
1618 next_uframe += stream->interval;
bccbefaa 1619 next_uframe &= mod - 1;
1da177e4
LT
1620 packet++;
1621
1622 /* link completed itds into the schedule */
1623 if (((next_uframe >> 3) != frame)
1624 || packet == urb->number_of_packets) {
bccbefaa 1625 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
1da177e4
LT
1626 itd = NULL;
1627 }
1628 }
1629 stream->next_uframe = next_uframe;
1630
1631 /* don't need that schedule data any more */
1632 iso_sched_free (stream, iso_sched);
2656a9ab 1633 urb->hcpriv = stream;
1da177e4 1634
569b394f 1635 ++ehci->isoc_count;
b015cb79 1636 enable_periodic(ehci);
1da177e4
LT
1637}
1638
1639#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1640
30bf54e6
DB
1641/* Process and recycle a completed ITD. Return true iff its urb completed,
1642 * and hence its completion callback probably added things to the hardware
1643 * schedule.
1644 *
1645 * Note that we carefully avoid recycling this descriptor until after any
1646 * completion callback runs, so that it won't be reused quickly. That is,
1647 * assuming (a) no more than two urbs per frame on this endpoint, and also
1648 * (b) only this endpoint's completions submit URBs. It seems some silicon
1649 * corrupts things if you reuse completed descriptors very quickly...
1650 */
f4289078
AS
1651static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
1652{
1da177e4
LT
1653 struct urb *urb = itd->urb;
1654 struct usb_iso_packet_descriptor *desc;
1655 u32 t;
1656 unsigned uframe;
1657 int urb_index = -1;
1658 struct ehci_iso_stream *stream = itd->stream;
1659 struct usb_device *dev;
f4289078 1660 bool retval = false;
1da177e4
LT
1661
1662 /* for each uframe with a packet */
1663 for (uframe = 0; uframe < 8; uframe++) {
1664 if (likely (itd->index[uframe] == -1))
1665 continue;
1666 urb_index = itd->index[uframe];
1667 desc = &urb->iso_frame_desc [urb_index];
1668
6dbd682b 1669 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
1da177e4 1670 itd->hw_transaction [uframe] = 0;
1da177e4
LT
1671
1672 /* report transfer status */
1673 if (unlikely (t & ISO_ERRS)) {
1674 urb->error_count++;
1675 if (t & EHCI_ISOC_BUF_ERR)
1676 desc->status = usb_pipein (urb->pipe)
1677 ? -ENOSR /* hc couldn't read */
1678 : -ECOMM; /* hc couldn't write */
1679 else if (t & EHCI_ISOC_BABBLE)
1680 desc->status = -EOVERFLOW;
1681 else /* (t & EHCI_ISOC_XACTERR) */
1682 desc->status = -EPROTO;
1683
1684 /* HC need not update length with this error */
ec6d67e3
AS
1685 if (!(t & EHCI_ISOC_BABBLE)) {
1686 desc->actual_length = EHCI_ITD_LENGTH(t);
1687 urb->actual_length += desc->actual_length;
1688 }
1da177e4
LT
1689 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1690 desc->status = 0;
ec6d67e3
AS
1691 desc->actual_length = EHCI_ITD_LENGTH(t);
1692 urb->actual_length += desc->actual_length;
b40e43fc
AS
1693 } else {
1694 /* URB was too late */
4005ad43 1695 urb->error_count++;
1da177e4
LT
1696 }
1697 }
1698
1da177e4
LT
1699 /* handle completion now? */
1700 if (likely ((urb_index + 1) != urb->number_of_packets))
30bf54e6 1701 goto done;
1da177e4
LT
1702
1703 /* ASSERT: it's really the last itd for this urb
1704 list_for_each_entry (itd, &stream->td_list, itd_list)
1705 BUG_ON (itd->urb == urb);
1706 */
1707
aa16ca30 1708 /* give urb back to the driver; completion often (re)submits */
6a8e87b2 1709 dev = urb->dev;
14c04c0f 1710 ehci_urb_done(ehci, urb, 0);
30bf54e6 1711 retval = true;
1da177e4 1712 urb = NULL;
569b394f
AS
1713
1714 --ehci->isoc_count;
b015cb79 1715 disable_periodic(ehci);
1da177e4 1716
569b394f 1717 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
05570297 1718 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
ad93562b
AX
1719 if (ehci->amd_pll_fix == 1)
1720 usb_amd_quirk_pll_enable();
05570297
AH
1721 }
1722
508db8c9 1723 if (unlikely(list_is_singular(&stream->td_list))) {
1da177e4
LT
1724 ehci_to_hcd(ehci)->self.bandwidth_allocated
1725 -= stream->bandwidth;
1726 ehci_vdbg (ehci,
1727 "deschedule devp %s ep%d%s-iso\n",
1728 dev->devpath, stream->bEndpointAddress & 0x0f,
1729 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1730 }
9aa09d2f 1731
30bf54e6 1732done:
30bf54e6 1733 itd->urb = NULL;
55934eb3
AS
1734
1735 /* Add to the end of the free list for later reuse */
1736 list_move_tail(&itd->itd_list, &stream->free_list);
1737
1738 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
1739 if (list_empty(&stream->td_list)) {
1740 list_splice_tail_init(&stream->free_list,
1741 &ehci->cached_itd_list);
1742 start_free_itds(ehci);
9aa09d2f 1743 }
55934eb3 1744
30bf54e6 1745 return retval;
1da177e4
LT
1746}
1747
1748/*-------------------------------------------------------------------------*/
1749
5db539e4 1750static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
55016f10 1751 gfp_t mem_flags)
1da177e4
LT
1752{
1753 int status = -EINVAL;
1754 unsigned long flags;
1755 struct ehci_iso_stream *stream;
1756
1757 /* Get iso_stream head */
1758 stream = iso_stream_find (ehci, urb);
1759 if (unlikely (stream == NULL)) {
1760 ehci_dbg (ehci, "can't get iso stream\n");
1761 return -ENOMEM;
1762 }
1763 if (unlikely (urb->interval != stream->interval)) {
1764 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1765 stream->interval, urb->interval);
1766 goto done;
1767 }
1768
1769#ifdef EHCI_URB_TRACE
1770 ehci_dbg (ehci,
1771 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
441b62c1 1772 __func__, urb->dev->devpath, urb,
1da177e4
LT
1773 usb_pipeendpoint (urb->pipe),
1774 usb_pipein (urb->pipe) ? "in" : "out",
1775 urb->transfer_buffer_length,
1776 urb->number_of_packets, urb->interval,
1777 stream);
1778#endif
1779
1780 /* allocate ITDs w/o locking anything */
1781 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1782 if (unlikely (status < 0)) {
1783 ehci_dbg (ehci, "can't init itds\n");
1784 goto done;
1785 }
1786
1787 /* schedule ... need to lock */
1788 spin_lock_irqsave (&ehci->lock, flags);
541c7d43 1789 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
8de98402 1790 status = -ESHUTDOWN;
e9df41c5
AS
1791 goto done_not_linked;
1792 }
1793 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1794 if (unlikely(status))
1795 goto done_not_linked;
1796 status = iso_stream_schedule(ehci, urb, stream);
53bd6a60 1797 if (likely (status == 0))
1da177e4 1798 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
e9df41c5
AS
1799 else
1800 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
8c5bf7be 1801 done_not_linked:
1da177e4 1802 spin_unlock_irqrestore (&ehci->lock, flags);
8c5bf7be 1803 done:
1da177e4
LT
1804 return status;
1805}
1806
1da177e4
LT
1807/*-------------------------------------------------------------------------*/
1808
1809/*
1810 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1811 * TTs in USB 2.0 hubs. These need microframe scheduling.
1812 */
1813
1814static inline void
6dbd682b
SR
1815sitd_sched_init(
1816 struct ehci_hcd *ehci,
1da177e4
LT
1817 struct ehci_iso_sched *iso_sched,
1818 struct ehci_iso_stream *stream,
1819 struct urb *urb
1820)
1821{
1822 unsigned i;
1823 dma_addr_t dma = urb->transfer_dma;
1824
1825 /* how many frames are needed for these transfers */
1826 iso_sched->span = urb->number_of_packets * stream->interval;
1827
1828 /* figure out per-frame sitd fields that we'll need later
1829 * when we fit new sitds into the schedule.
1830 */
1831 for (i = 0; i < urb->number_of_packets; i++) {
1832 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1833 unsigned length;
1834 dma_addr_t buf;
1835 u32 trans;
1836
1837 length = urb->iso_frame_desc [i].length & 0x03ff;
1838 buf = dma + urb->iso_frame_desc [i].offset;
1839
1840 trans = SITD_STS_ACTIVE;
1841 if (((i + 1) == urb->number_of_packets)
1842 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1843 trans |= SITD_IOC;
1844 trans |= length << 16;
6dbd682b 1845 packet->transaction = cpu_to_hc32(ehci, trans);
1da177e4
LT
1846
1847 /* might need to cross a buffer page within a td */
1848 packet->bufp = buf;
1849 packet->buf1 = (buf + length) & ~0x0fff;
1850 if (packet->buf1 != (buf & ~(u64)0x0fff))
1851 packet->cross = 1;
1852
53bd6a60 1853 /* OUT uses multiple start-splits */
1da177e4
LT
1854 if (stream->bEndpointAddress & USB_DIR_IN)
1855 continue;
1856 length = (length + 187) / 188;
1857 if (length > 1) /* BEGIN vs ALL */
1858 length |= 1 << 3;
1859 packet->buf1 |= length;
1860 }
1861}
1862
1863static int
1864sitd_urb_transaction (
1865 struct ehci_iso_stream *stream,
1866 struct ehci_hcd *ehci,
1867 struct urb *urb,
55016f10 1868 gfp_t mem_flags
1da177e4
LT
1869)
1870{
1871 struct ehci_sitd *sitd;
1872 dma_addr_t sitd_dma;
1873 int i;
1874 struct ehci_iso_sched *iso_sched;
1875 unsigned long flags;
1876
1877 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1878 if (iso_sched == NULL)
1879 return -ENOMEM;
1880
6dbd682b 1881 sitd_sched_init(ehci, iso_sched, stream, urb);
1da177e4
LT
1882
1883 /* allocate/init sITDs */
1884 spin_lock_irqsave (&ehci->lock, flags);
1885 for (i = 0; i < urb->number_of_packets; i++) {
1886
1887 /* NOTE: for now, we don't try to handle wraparound cases
1888 * for IN (using sitd->hw_backpointer, like a FSTN), which
1889 * means we never need two sitds for full speed packets.
1890 */
1891
55934eb3
AS
1892 /*
1893 * Use siTDs from the free list, but not siTDs that may
1894 * still be in use by the hardware.
1da177e4 1895 */
55934eb3
AS
1896 if (likely(!list_empty(&stream->free_list))) {
1897 sitd = list_first_entry(&stream->free_list,
1da177e4 1898 struct ehci_sitd, sitd_list);
f4289078 1899 if (sitd->frame == ehci->now_frame)
55934eb3 1900 goto alloc_sitd;
1da177e4
LT
1901 list_del (&sitd->sitd_list);
1902 sitd_dma = sitd->sitd_dma;
3d01f0fe 1903 } else {
55934eb3 1904 alloc_sitd:
1da177e4
LT
1905 spin_unlock_irqrestore (&ehci->lock, flags);
1906 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1907 &sitd_dma);
1908 spin_lock_irqsave (&ehci->lock, flags);
3d01f0fe
KW
1909 if (!sitd) {
1910 iso_sched_free(stream, iso_sched);
1911 spin_unlock_irqrestore(&ehci->lock, flags);
1912 return -ENOMEM;
1913 }
1da177e4
LT
1914 }
1915
1da177e4
LT
1916 memset (sitd, 0, sizeof *sitd);
1917 sitd->sitd_dma = sitd_dma;
1918 list_add (&sitd->sitd_list, &iso_sched->td_list);
1919 }
1920
1921 /* temporarily store schedule info in hcpriv */
1922 urb->hcpriv = iso_sched;
1923 urb->error_count = 0;
1924
1925 spin_unlock_irqrestore (&ehci->lock, flags);
1926 return 0;
1927}
1928
1929/*-------------------------------------------------------------------------*/
1930
1931static inline void
6dbd682b
SR
1932sitd_patch(
1933 struct ehci_hcd *ehci,
1da177e4
LT
1934 struct ehci_iso_stream *stream,
1935 struct ehci_sitd *sitd,
1936 struct ehci_iso_sched *iso_sched,
1937 unsigned index
1938)
1939{
1940 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1941 u64 bufp = uf->bufp;
1942
6dbd682b 1943 sitd->hw_next = EHCI_LIST_END(ehci);
1da177e4
LT
1944 sitd->hw_fullspeed_ep = stream->address;
1945 sitd->hw_uframe = stream->splits;
1946 sitd->hw_results = uf->transaction;
6dbd682b 1947 sitd->hw_backpointer = EHCI_LIST_END(ehci);
1da177e4
LT
1948
1949 bufp = uf->bufp;
6dbd682b
SR
1950 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
1951 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
1da177e4 1952
6dbd682b 1953 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
1da177e4
LT
1954 if (uf->cross)
1955 bufp += 4096;
6dbd682b 1956 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
1da177e4
LT
1957 sitd->index = index;
1958}
1959
1960static inline void
1961sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
1962{
1963 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
1964 sitd->sitd_next = ehci->pshadow [frame];
1965 sitd->hw_next = ehci->periodic [frame];
1966 ehci->pshadow [frame].sitd = sitd;
1967 sitd->frame = frame;
1968 wmb ();
6dbd682b 1969 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
1da177e4
LT
1970}
1971
1972/* fit urb's sitds into the selected schedule slot; activate as needed */
b015cb79 1973static void sitd_link_urb(
1da177e4
LT
1974 struct ehci_hcd *ehci,
1975 struct urb *urb,
1976 unsigned mod,
1977 struct ehci_iso_stream *stream
1978)
1979{
1980 int packet;
1981 unsigned next_uframe;
1982 struct ehci_iso_sched *sched = urb->hcpriv;
1983 struct ehci_sitd *sitd;
1984
1985 next_uframe = stream->next_uframe;
1986
1987 if (list_empty(&stream->td_list)) {
1988 /* usbfs ignores TT bandwidth */
1989 ehci_to_hcd(ehci)->self.bandwidth_allocated
1990 += stream->bandwidth;
1991 ehci_vdbg (ehci,
1992 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
1993 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1994 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
bccbefaa 1995 (next_uframe >> 3) & (ehci->periodic_size - 1),
6dbd682b 1996 stream->interval, hc32_to_cpu(ehci, stream->splits));
1da177e4 1997 }
05570297
AH
1998
1999 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
ad93562b
AX
2000 if (ehci->amd_pll_fix == 1)
2001 usb_amd_quirk_pll_disable();
05570297
AH
2002 }
2003
1da177e4
LT
2004 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2005
2006 /* fill sITDs frame by frame */
2007 for (packet = 0, sitd = NULL;
2008 packet < urb->number_of_packets;
2009 packet++) {
2010
2011 /* ASSERT: we have all necessary sitds */
2012 BUG_ON (list_empty (&sched->td_list));
2013
2014 /* ASSERT: no itds for this endpoint in this frame */
2015
2016 sitd = list_entry (sched->td_list.next,
2017 struct ehci_sitd, sitd_list);
2018 list_move_tail (&sitd->sitd_list, &stream->td_list);
8c5bf7be 2019 sitd->stream = stream;
508db8c9 2020 sitd->urb = urb;
1da177e4 2021
6dbd682b 2022 sitd_patch(ehci, stream, sitd, sched, packet);
bccbefaa 2023 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
1da177e4
LT
2024 sitd);
2025
2026 next_uframe += stream->interval << 3;
1da177e4 2027 }
bccbefaa 2028 stream->next_uframe = next_uframe & (mod - 1);
1da177e4
LT
2029
2030 /* don't need that schedule data any more */
2031 iso_sched_free (stream, sched);
2656a9ab 2032 urb->hcpriv = stream;
1da177e4 2033
569b394f 2034 ++ehci->isoc_count;
b015cb79 2035 enable_periodic(ehci);
1da177e4
LT
2036}
2037
2038/*-------------------------------------------------------------------------*/
2039
2040#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
53bd6a60 2041 | SITD_STS_XACT | SITD_STS_MMF)
1da177e4 2042
30bf54e6
DB
2043/* Process and recycle a completed SITD. Return true iff its urb completed,
2044 * and hence its completion callback probably added things to the hardware
2045 * schedule.
2046 *
2047 * Note that we carefully avoid recycling this descriptor until after any
2048 * completion callback runs, so that it won't be reused quickly. That is,
2049 * assuming (a) no more than two urbs per frame on this endpoint, and also
2050 * (b) only this endpoint's completions submit URBs. It seems some silicon
2051 * corrupts things if you reuse completed descriptors very quickly...
2052 */
f4289078
AS
2053static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
2054{
1da177e4
LT
2055 struct urb *urb = sitd->urb;
2056 struct usb_iso_packet_descriptor *desc;
2057 u32 t;
2058 int urb_index = -1;
2059 struct ehci_iso_stream *stream = sitd->stream;
2060 struct usb_device *dev;
f4289078 2061 bool retval = false;
1da177e4
LT
2062
2063 urb_index = sitd->index;
2064 desc = &urb->iso_frame_desc [urb_index];
6dbd682b 2065 t = hc32_to_cpup(ehci, &sitd->hw_results);
1da177e4
LT
2066
2067 /* report transfer status */
4005ad43 2068 if (unlikely(t & SITD_ERRS)) {
1da177e4
LT
2069 urb->error_count++;
2070 if (t & SITD_STS_DBE)
2071 desc->status = usb_pipein (urb->pipe)
2072 ? -ENOSR /* hc couldn't read */
2073 : -ECOMM; /* hc couldn't write */
2074 else if (t & SITD_STS_BABBLE)
2075 desc->status = -EOVERFLOW;
2076 else /* XACT, MMF, etc */
2077 desc->status = -EPROTO;
4005ad43
AS
2078 } else if (unlikely(t & SITD_STS_ACTIVE)) {
2079 /* URB was too late */
2080 urb->error_count++;
1da177e4
LT
2081 } else {
2082 desc->status = 0;
ec6d67e3
AS
2083 desc->actual_length = desc->length - SITD_LENGTH(t);
2084 urb->actual_length += desc->actual_length;
1da177e4 2085 }
1da177e4
LT
2086
2087 /* handle completion now? */
2088 if ((urb_index + 1) != urb->number_of_packets)
30bf54e6 2089 goto done;
1da177e4
LT
2090
2091 /* ASSERT: it's really the last sitd for this urb
2092 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2093 BUG_ON (sitd->urb == urb);
2094 */
2095
aa16ca30 2096 /* give urb back to the driver; completion often (re)submits */
6a8e87b2 2097 dev = urb->dev;
14c04c0f 2098 ehci_urb_done(ehci, urb, 0);
30bf54e6 2099 retval = true;
1da177e4 2100 urb = NULL;
569b394f
AS
2101
2102 --ehci->isoc_count;
b015cb79 2103 disable_periodic(ehci);
1da177e4 2104
569b394f 2105 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
05570297 2106 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
ad93562b
AX
2107 if (ehci->amd_pll_fix == 1)
2108 usb_amd_quirk_pll_enable();
05570297
AH
2109 }
2110
508db8c9 2111 if (list_is_singular(&stream->td_list)) {
1da177e4
LT
2112 ehci_to_hcd(ehci)->self.bandwidth_allocated
2113 -= stream->bandwidth;
2114 ehci_vdbg (ehci,
2115 "deschedule devp %s ep%d%s-iso\n",
2116 dev->devpath, stream->bEndpointAddress & 0x0f,
2117 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2118 }
0e5f231b 2119
30bf54e6 2120done:
30bf54e6 2121 sitd->urb = NULL;
55934eb3
AS
2122
2123 /* Add to the end of the free list for later reuse */
2124 list_move_tail(&sitd->sitd_list, &stream->free_list);
2125
2126 /* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
2127 if (list_empty(&stream->td_list)) {
2128 list_splice_tail_init(&stream->free_list,
2129 &ehci->cached_sitd_list);
2130 start_free_itds(ehci);
0e5f231b 2131 }
55934eb3 2132
30bf54e6 2133 return retval;
1da177e4
LT
2134}
2135
2136
5db539e4 2137static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
55016f10 2138 gfp_t mem_flags)
1da177e4
LT
2139{
2140 int status = -EINVAL;
2141 unsigned long flags;
2142 struct ehci_iso_stream *stream;
2143
2144 /* Get iso_stream head */
2145 stream = iso_stream_find (ehci, urb);
2146 if (stream == NULL) {
2147 ehci_dbg (ehci, "can't get iso stream\n");
2148 return -ENOMEM;
2149 }
2150 if (urb->interval != stream->interval) {
2151 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2152 stream->interval, urb->interval);
2153 goto done;
2154 }
2155
2156#ifdef EHCI_URB_TRACE
2157 ehci_dbg (ehci,
2158 "submit %p dev%s ep%d%s-iso len %d\n",
2159 urb, urb->dev->devpath,
2160 usb_pipeendpoint (urb->pipe),
2161 usb_pipein (urb->pipe) ? "in" : "out",
2162 urb->transfer_buffer_length);
2163#endif
2164
2165 /* allocate SITDs */
2166 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2167 if (status < 0) {
2168 ehci_dbg (ehci, "can't init sitds\n");
2169 goto done;
2170 }
2171
2172 /* schedule ... need to lock */
2173 spin_lock_irqsave (&ehci->lock, flags);
541c7d43 2174 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
8de98402 2175 status = -ESHUTDOWN;
e9df41c5
AS
2176 goto done_not_linked;
2177 }
2178 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2179 if (unlikely(status))
2180 goto done_not_linked;
2181 status = iso_stream_schedule(ehci, urb, stream);
53bd6a60 2182 if (status == 0)
1da177e4 2183 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
e9df41c5
AS
2184 else
2185 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
8c5bf7be 2186 done_not_linked:
1da177e4 2187 spin_unlock_irqrestore (&ehci->lock, flags);
8c5bf7be 2188 done:
1da177e4
LT
2189 return status;
2190}
2191
1da177e4
LT
2192/*-------------------------------------------------------------------------*/
2193
569b394f 2194static void scan_isoc(struct ehci_hcd *ehci)
1da177e4 2195{
f4289078
AS
2196 unsigned uf, now_frame, frame;
2197 unsigned fmask = ehci->periodic_size - 1;
2198 bool modified, live;
1da177e4
LT
2199
2200 /*
2201 * When running, scan from last scan point up to "now"
2202 * else clean up by scanning everything that's left.
2203 * Touches as few pages as possible: cache-friendly.
2204 */
c0c53dbc 2205 if (ehci->rh_state >= EHCI_RH_RUNNING) {
f4289078
AS
2206 uf = ehci_read_frame_index(ehci);
2207 now_frame = (uf >> 3) & fmask;
2208 live = true;
9aa09d2f 2209 } else {
c3ee9b76 2210 now_frame = (ehci->last_iso_frame - 1) & fmask;
f4289078 2211 live = false;
9aa09d2f 2212 }
f4289078 2213 ehci->now_frame = now_frame;
1da177e4
LT
2214
2215 for (;;) {
2216 union ehci_shadow q, *q_p;
6dbd682b 2217 __hc32 type, *hw_p;
1da177e4 2218
c3ee9b76 2219 frame = ehci->last_iso_frame;
1da177e4
LT
2220restart:
2221 /* scan each element in frame's queue for completions */
2222 q_p = &ehci->pshadow [frame];
2223 hw_p = &ehci->periodic [frame];
2224 q.ptr = q_p->ptr;
6dbd682b 2225 type = Q_NEXT_TYPE(ehci, *hw_p);
f4289078 2226 modified = false;
1da177e4
LT
2227
2228 while (q.ptr != NULL) {
6dbd682b 2229 switch (hc32_to_cpu(ehci, type)) {
1da177e4 2230 case Q_TYPE_ITD:
79592b72
DB
2231 /* If this ITD is still active, leave it for
2232 * later processing ... check the next entry.
b40e43fc
AS
2233 * No need to check for activity unless the
2234 * frame is current.
79592b72 2235 */
f4289078 2236 if (frame == now_frame && live) {
b40e43fc
AS
2237 rmb();
2238 for (uf = 0; uf < 8; uf++) {
2239 if (q.itd->hw_transaction[uf] &
2240 ITD_ACTIVE(ehci))
2241 break;
2242 }
2243 if (uf < 8) {
b40e43fc
AS
2244 q_p = &q.itd->itd_next;
2245 hw_p = &q.itd->hw_next;
2246 type = Q_NEXT_TYPE(ehci,
6dbd682b 2247 q.itd->hw_next);
b40e43fc
AS
2248 q = *q_p;
2249 break;
2250 }
1da177e4 2251 }
1da177e4 2252
79592b72
DB
2253 /* Take finished ITDs out of the schedule
2254 * and process them: recycle, maybe report
2255 * URB completion. HC won't cache the
1da177e4
LT
2256 * pointer for much longer, if at all.
2257 */
2258 *q_p = q.itd->itd_next;
3d091a6f
AX
2259 if (!ehci->use_dummy_qh ||
2260 q.itd->hw_next != EHCI_LIST_END(ehci))
2261 *hw_p = q.itd->hw_next;
2262 else
2263 *hw_p = ehci->dummy->qh_dma;
6dbd682b 2264 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
1da177e4 2265 wmb();
7d12e780 2266 modified = itd_complete (ehci, q.itd);
1da177e4
LT
2267 q = *q_p;
2268 break;
2269 case Q_TYPE_SITD:
79592b72
DB
2270 /* If this SITD is still active, leave it for
2271 * later processing ... check the next entry.
b40e43fc
AS
2272 * No need to check for activity unless the
2273 * frame is current.
79592b72 2274 */
f4289078
AS
2275 if (((frame == now_frame) ||
2276 (((frame + 1) & fmask) == now_frame))
22e18694
DE
2277 && live
2278 && (q.sitd->hw_results &
2279 SITD_ACTIVE(ehci))) {
2280
1da177e4
LT
2281 q_p = &q.sitd->sitd_next;
2282 hw_p = &q.sitd->hw_next;
6dbd682b
SR
2283 type = Q_NEXT_TYPE(ehci,
2284 q.sitd->hw_next);
1da177e4
LT
2285 q = *q_p;
2286 break;
2287 }
79592b72
DB
2288
2289 /* Take finished SITDs out of the schedule
2290 * and process them: recycle, maybe report
2291 * URB completion.
2292 */
1da177e4 2293 *q_p = q.sitd->sitd_next;
3d091a6f
AX
2294 if (!ehci->use_dummy_qh ||
2295 q.sitd->hw_next != EHCI_LIST_END(ehci))
2296 *hw_p = q.sitd->hw_next;
2297 else
2298 *hw_p = ehci->dummy->qh_dma;
6dbd682b 2299 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
1da177e4 2300 wmb();
7d12e780 2301 modified = sitd_complete (ehci, q.sitd);
1da177e4
LT
2302 q = *q_p;
2303 break;
2304 default:
2d0fe1bb 2305 ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
1da177e4
LT
2306 type, frame, q.ptr);
2307 // BUG ();
569b394f
AS
2308 /* FALL THROUGH */
2309 case Q_TYPE_QH:
2310 case Q_TYPE_FSTN:
2311 /* End of the iTDs and siTDs */
1da177e4 2312 q.ptr = NULL;
569b394f 2313 break;
1da177e4
LT
2314 }
2315
2316 /* assume completion callbacks modify the queue */
f4289078
AS
2317 if (unlikely(modified && ehci->isoc_count > 0))
2318 goto restart;
1da177e4
LT
2319 }
2320
f4289078
AS
2321 /* Stop when we have reached the current frame */
2322 if (frame == now_frame)
79592b72 2323 break;
c3ee9b76 2324 ehci->last_iso_frame = (frame + 1) & fmask;
53bd6a60 2325 }
1da177e4 2326}