blob: 724ba7133c4fb93b7abae7a6838356f200b9d85e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2001-2004 by David Brownell
3 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
David Brownell53bd6a62006-08-30 14:50:06 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
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 */
46static union ehci_shadow *
Stefan Roese6dbd6822007-05-01 09:29:37 -070047periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
48 __hc32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Stefan Roese6dbd6822007-05-01 09:29:37 -070050 switch (hc32_to_cpu(ehci, tag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case Q_TYPE_QH:
52 return &periodic->qh->qh_next;
53 case Q_TYPE_FSTN:
54 return &periodic->fstn->fstn_next;
55 case Q_TYPE_ITD:
56 return &periodic->itd->itd_next;
57 // case Q_TYPE_SITD:
58 default:
59 return &periodic->sitd->sitd_next;
60 }
61}
62
Alek Du3807e262009-07-14 07:23:29 +080063static __hc32 *
64shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
65 __hc32 tag)
66{
67 switch (hc32_to_cpu(ehci, tag)) {
68 /* our ehci_shadow.qh is actually software part */
69 case Q_TYPE_QH:
70 return &periodic->qh->hw->hw_next;
71 /* others are hw parts */
72 default:
73 return periodic->hw_next;
74 }
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* caller must hold ehci->lock */
78static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
79{
Stefan Roese6dbd6822007-05-01 09:29:37 -070080 union ehci_shadow *prev_p = &ehci->pshadow[frame];
81 __hc32 *hw_p = &ehci->periodic[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 union ehci_shadow here = *prev_p;
83
84 /* find predecessor of "ptr"; hw and shadow lists are in sync */
85 while (here.ptr && here.ptr != ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -070086 prev_p = periodic_next_shadow(ehci, prev_p,
87 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +080088 hw_p = shadow_next_periodic(ehci, &here,
89 Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 here = *prev_p;
91 }
92 /* an interrupt entry (at list end) could have been shared */
93 if (!here.ptr)
94 return;
95
96 /* update shadow and hardware lists ... the old "next" pointers
97 * from ptr may still be in use, the caller updates them.
98 */
Stefan Roese6dbd6822007-05-01 09:29:37 -070099 *prev_p = *periodic_next_shadow(ehci, &here,
100 Q_NEXT_TYPE(ehci, *hw_p));
Alek Du3807e262009-07-14 07:23:29 +0800101 *hw_p = *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/* how many of the uframe's 125 usecs are allocated? */
105static unsigned short
106periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
107{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700108 __hc32 *hw_p = &ehci->periodic [frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 union ehci_shadow *q = &ehci->pshadow [frame];
110 unsigned usecs = 0;
Alek Du3807e262009-07-14 07:23:29 +0800111 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700114 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800116 hw = q->qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* is it in the S-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800118 if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 usecs += q->qh->usecs;
120 /* ... or C-mask? */
Alek Du3807e262009-07-14 07:23:29 +0800121 if (hw->hw_info2 & cpu_to_hc32(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700122 1 << (8 + uframe)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 usecs += q->qh->c_usecs;
Alek Du3807e262009-07-14 07:23:29 +0800124 hw_p = &hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 q = &q->qh->qh_next;
126 break;
127 // case Q_TYPE_FSTN:
128 default:
129 /* for "save place" FSTNs, count the relevant INTR
130 * bandwidth from the previous frame
131 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700132 if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 ehci_dbg (ehci, "ignoring FSTN cost ...\n");
134 }
135 hw_p = &q->fstn->hw_next;
136 q = &q->fstn->fstn_next;
137 break;
138 case Q_TYPE_ITD:
Karsten Wiese3b6fcfd2007-12-30 21:55:05 -0800139 if (q->itd->hw_transaction[uframe])
140 usecs += q->itd->stream->usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 hw_p = &q->itd->hw_next;
142 q = &q->itd->itd_next;
143 break;
144 case Q_TYPE_SITD:
145 /* is it in the S-mask? (count SPLIT, DATA) */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700146 if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
147 1 << uframe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (q->sitd->hw_fullspeed_ep &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700149 cpu_to_hc32(ehci, 1<<31))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 usecs += q->sitd->stream->usecs;
151 else /* worst case for OUT start-split */
152 usecs += HS_USECS_ISO (188);
153 }
154
155 /* ... C-mask? (count CSPLIT, DATA) */
156 if (q->sitd->hw_uframe &
Stefan Roese6dbd6822007-05-01 09:29:37 -0700157 cpu_to_hc32(ehci, 1 << (8 + uframe))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* worst case for IN complete-split */
159 usecs += q->sitd->stream->c_usecs;
160 }
161
162 hw_p = &q->sitd->hw_next;
163 q = &q->sitd->sitd_next;
164 break;
165 }
166 }
167#ifdef DEBUG
168 if (usecs > 100)
169 ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
170 frame * 8 + uframe, usecs);
171#endif
172 return usecs;
173}
174
175/*-------------------------------------------------------------------------*/
176
177static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
178{
179 if (!dev1->tt || !dev2->tt)
180 return 0;
181 if (dev1->tt != dev2->tt)
182 return 0;
183 if (dev1->tt->multi)
184 return dev1->ttport == dev2->ttport;
185 else
186 return 1;
187}
188
Dan Streetmanba47f662006-05-24 09:39:16 -0700189#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
190
191/* Which uframe does the low/fullspeed transfer start in?
192 *
193 * The parameter is the mask of ssplits in "H-frame" terms
194 * and this returns the transfer start uframe in "B-frame" terms,
195 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
196 * will cause a transfer in "B-frame" uframe 0. "B-frames" lag
197 * "H-frames" by 1 uframe. See the EHCI spec sec 4.5 and figure 4.7.
198 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700199static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
Dan Streetmanba47f662006-05-24 09:39:16 -0700200{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700201 unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
Dan Streetmanba47f662006-05-24 09:39:16 -0700202 if (!smask) {
203 ehci_err(ehci, "invalid empty smask!\n");
204 /* uframe 7 can't have bw so this will indicate failure */
205 return 7;
206 }
207 return ffs(smask) - 1;
208}
209
210static const unsigned char
211max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };
212
213/* carryover low/fullspeed bandwidth that crosses uframe boundries */
214static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
215{
216 int i;
217 for (i=0; i<7; i++) {
218 if (max_tt_usecs[i] < tt_usecs[i]) {
219 tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
220 tt_usecs[i] = max_tt_usecs[i];
221 }
222 }
223}
224
225/* How many of the tt's periodic downstream 1000 usecs are allocated?
226 *
227 * While this measures the bandwidth in terms of usecs/uframe,
228 * the low/fullspeed bus has no notion of uframes, so any particular
229 * low/fullspeed transfer can "carry over" from one uframe to the next,
230 * since the TT just performs downstream transfers in sequence.
231 *
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800232 * For example two separate 100 usec transfers can start in the same uframe,
Dan Streetmanba47f662006-05-24 09:39:16 -0700233 * and the second one would "carry over" 75 usecs into the next uframe.
234 */
235static void
236periodic_tt_usecs (
237 struct ehci_hcd *ehci,
238 struct usb_device *dev,
239 unsigned frame,
240 unsigned short tt_usecs[8]
241)
242{
Stefan Roese6dbd6822007-05-01 09:29:37 -0700243 __hc32 *hw_p = &ehci->periodic [frame];
Dan Streetmanba47f662006-05-24 09:39:16 -0700244 union ehci_shadow *q = &ehci->pshadow [frame];
245 unsigned char uf;
246
247 memset(tt_usecs, 0, 16);
248
249 while (q->ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700250 switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
Dan Streetmanba47f662006-05-24 09:39:16 -0700251 case Q_TYPE_ITD:
252 hw_p = &q->itd->hw_next;
253 q = &q->itd->itd_next;
254 continue;
255 case Q_TYPE_QH:
256 if (same_tt(dev, q->qh->dev)) {
Alek Du3807e262009-07-14 07:23:29 +0800257 uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
Dan Streetmanba47f662006-05-24 09:39:16 -0700258 tt_usecs[uf] += q->qh->tt_usecs;
259 }
Alek Du3807e262009-07-14 07:23:29 +0800260 hw_p = &q->qh->hw->hw_next;
Dan Streetmanba47f662006-05-24 09:39:16 -0700261 q = &q->qh->qh_next;
262 continue;
263 case Q_TYPE_SITD:
264 if (same_tt(dev, q->sitd->urb->dev)) {
265 uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
266 tt_usecs[uf] += q->sitd->stream->tt_usecs;
267 }
268 hw_p = &q->sitd->hw_next;
269 q = &q->sitd->sitd_next;
270 continue;
271 // case Q_TYPE_FSTN:
272 default:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700273 ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
274 frame);
Dan Streetmanba47f662006-05-24 09:39:16 -0700275 hw_p = &q->fstn->hw_next;
276 q = &q->fstn->fstn_next;
277 }
278 }
279
280 carryover_tt_bandwidth(tt_usecs);
281
282 if (max_tt_usecs[7] < tt_usecs[7])
283 ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
284 frame, tt_usecs[7] - max_tt_usecs[7]);
285}
286
287/*
288 * Return true if the device's tt's downstream bus is available for a
289 * periodic transfer of the specified length (usecs), starting at the
290 * specified frame/uframe. Note that (as summarized in section 11.19
291 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
292 * uframe.
293 *
294 * The uframe parameter is when the fullspeed/lowspeed transfer
295 * should be executed in "B-frame" terms, which is the same as the
296 * highspeed ssplit's uframe (which is in "H-frame" terms). For example
297 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
298 * See the EHCI spec sec 4.5 and fig 4.7.
299 *
300 * This checks if the full/lowspeed bus, at the specified starting uframe,
301 * has the specified bandwidth available, according to rules listed
302 * in USB 2.0 spec section 11.18.1 fig 11-60.
303 *
304 * This does not check if the transfer would exceed the max ssplit
305 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
306 * since proper scheduling limits ssplits to less than 16 per uframe.
307 */
308static int tt_available (
309 struct ehci_hcd *ehci,
310 unsigned period,
311 struct usb_device *dev,
312 unsigned frame,
313 unsigned uframe,
314 u16 usecs
315)
316{
317 if ((period == 0) || (uframe >= 7)) /* error */
318 return 0;
319
320 for (; frame < ehci->periodic_size; frame += period) {
321 unsigned short tt_usecs[8];
322
323 periodic_tt_usecs (ehci, dev, frame, tt_usecs);
324
325 ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
326 " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
327 frame, usecs, uframe,
328 tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
329 tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
330
331 if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
332 ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
333 frame, uframe);
334 return 0;
335 }
336
337 /* special case for isoc transfers larger than 125us:
338 * the first and each subsequent fully used uframe
339 * must be empty, so as to not illegally delay
340 * already scheduled transactions
341 */
342 if (125 < usecs) {
Dan Streetmanc065c602009-04-21 13:37:12 -0400343 int ufs = (usecs / 125);
Dan Streetmanba47f662006-05-24 09:39:16 -0700344 int i;
345 for (i = uframe; i < (uframe + ufs) && i < 8; i++)
346 if (0 < tt_usecs[i]) {
347 ehci_vdbg(ehci,
348 "multi-uframe xfer can't fit "
349 "in frame %d uframe %d\n",
350 frame, i);
351 return 0;
352 }
353 }
354
355 tt_usecs[uframe] += usecs;
356
357 carryover_tt_bandwidth(tt_usecs);
358
359 /* fail if the carryover pushed bw past the last uframe's limit */
360 if (max_tt_usecs[7] < tt_usecs[7]) {
361 ehci_vdbg(ehci,
362 "tt unavailable usecs %d frame %d uframe %d\n",
363 usecs, frame, uframe);
364 return 0;
365 }
366 }
367
368 return 1;
369}
370
371#else
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/* return true iff the device's transaction translator is available
374 * for a periodic transfer starting at the specified frame, using
375 * all the uframes in the mask.
376 */
377static int tt_no_collision (
378 struct ehci_hcd *ehci,
379 unsigned period,
380 struct usb_device *dev,
381 unsigned frame,
382 u32 uf_mask
383)
384{
385 if (period == 0) /* error */
386 return 0;
387
388 /* note bandwidth wastage: split never follows csplit
389 * (different dev or endpoint) until the next uframe.
390 * calling convention doesn't make that distinction.
391 */
392 for (; frame < ehci->periodic_size; frame += period) {
393 union ehci_shadow here;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700394 __hc32 type;
Alek Du3807e262009-07-14 07:23:29 +0800395 struct ehci_qh_hw *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 here = ehci->pshadow [frame];
Stefan Roese6dbd6822007-05-01 09:29:37 -0700398 type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700400 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case Q_TYPE_ITD:
Stefan Roese6dbd6822007-05-01 09:29:37 -0700402 type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 here = here.itd->itd_next;
404 continue;
405 case Q_TYPE_QH:
Alek Du3807e262009-07-14 07:23:29 +0800406 hw = here.qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (same_tt (dev, here.qh->dev)) {
408 u32 mask;
409
Stefan Roese6dbd6822007-05-01 09:29:37 -0700410 mask = hc32_to_cpu(ehci,
Alek Du3807e262009-07-14 07:23:29 +0800411 hw->hw_info2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* "knows" no gap is needed */
413 mask |= mask >> 8;
414 if (mask & uf_mask)
415 break;
416 }
Alek Du3807e262009-07-14 07:23:29 +0800417 type = Q_NEXT_TYPE(ehci, hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 here = here.qh->qh_next;
419 continue;
420 case Q_TYPE_SITD:
421 if (same_tt (dev, here.sitd->urb->dev)) {
422 u16 mask;
423
Stefan Roese6dbd6822007-05-01 09:29:37 -0700424 mask = hc32_to_cpu(ehci, here.sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ->hw_uframe);
426 /* FIXME assumes no gap for IN! */
427 mask |= mask >> 8;
428 if (mask & uf_mask)
429 break;
430 }
Stefan Roese6dbd6822007-05-01 09:29:37 -0700431 type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 here = here.sitd->sitd_next;
433 continue;
434 // case Q_TYPE_FSTN:
435 default:
436 ehci_dbg (ehci,
437 "periodic frame %d bogus type %d\n",
438 frame, type);
439 }
440
441 /* collision or error */
442 return 0;
443 }
444 }
445
446 /* no collision */
447 return 1;
448}
449
Dan Streetmanba47f662006-05-24 09:39:16 -0700450#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*-------------------------------------------------------------------------*/
453
454static int enable_periodic (struct ehci_hcd *ehci)
455{
456 u32 cmd;
457 int status;
458
David Brownell01c17142008-08-26 23:35:04 -0700459 if (ehci->periodic_sched++)
460 return 0;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* did clearing PSE did take effect yet?
463 * takes effect only at frame boundaries...
464 */
Karsten Wiesec765d4c2008-02-16 13:44:42 -0800465 status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
466 STS_PSS, 0, 9 * 125);
467 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100470 cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
471 ehci_writel(ehci, cmd, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* posted write ... PSS happens later */
473 ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
474
475 /* make sure ehci_work scans these */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100476 ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index)
477 % (ehci->periodic_size << 3);
Oliver Neukumee4ecb82009-11-27 15:17:59 +0100478 if (unlikely(ehci->broken_periodic))
479 ehci->last_periodic_enable = ktime_get_real();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481}
482
483static int disable_periodic (struct ehci_hcd *ehci)
484{
485 u32 cmd;
486 int status;
487
David Brownell01c17142008-08-26 23:35:04 -0700488 if (--ehci->periodic_sched)
489 return 0;
490
Oliver Neukumee4ecb82009-11-27 15:17:59 +0100491 if (unlikely(ehci->broken_periodic)) {
492 /* delay experimentally determined */
493 ktime_t safe = ktime_add_us(ehci->last_periodic_enable, 1000);
494 ktime_t now = ktime_get_real();
495 s64 delay = ktime_us_delta(safe, now);
496
497 if (unlikely(delay > 0))
498 udelay(delay);
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* did setting PSE not take effect yet?
502 * takes effect only at frame boundaries...
503 */
Karsten Wiesec765d4c2008-02-16 13:44:42 -0800504 status = handshake_on_error_set_halt(ehci, &ehci->regs->status,
505 STS_PSS, STS_PSS, 9 * 125);
506 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100509 cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE;
510 ehci_writel(ehci, cmd, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* posted write ... */
512
Alan Stern0e5f2312010-04-08 16:56:37 -0400513 free_cached_lists(ehci);
Dmitri Epshteind63c66d2009-12-14 17:17:33 +0200514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ehci->next_uframe = -1;
516 return 0;
517}
518
519/*-------------------------------------------------------------------------*/
520
521/* periodic schedule slots have iso tds (normal or split) first, then a
522 * sparse tree for active interrupt transfers.
523 *
524 * this just links in a qh; caller guarantees uframe masks are set right.
525 * no FSTN support (yet; ehci 0.96+)
526 */
527static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
528{
529 unsigned i;
530 unsigned period = qh->period;
531
532 dev_dbg (&qh->dev->dev,
533 "link qh%d-%04x/%p start %d [%d/%d us]\n",
Alek Du3807e262009-07-14 07:23:29 +0800534 period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
535 & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 qh, qh->start, qh->usecs, qh->c_usecs);
537
538 /* high bandwidth, or otherwise every microframe */
539 if (period == 0)
540 period = 1;
541
542 for (i = qh->start; i < ehci->periodic_size; i += period) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700543 union ehci_shadow *prev = &ehci->pshadow[i];
544 __hc32 *hw_p = &ehci->periodic[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 union ehci_shadow here = *prev;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700546 __hc32 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* skip the iso nodes at list head */
549 while (here.ptr) {
Stefan Roese6dbd6822007-05-01 09:29:37 -0700550 type = Q_NEXT_TYPE(ehci, *hw_p);
551 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700553 prev = periodic_next_shadow(ehci, prev, type);
Alek Du3807e262009-07-14 07:23:29 +0800554 hw_p = shadow_next_periodic(ehci, &here, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 here = *prev;
556 }
557
558 /* sorting each branch by period (slow-->fast)
559 * enables sharing interior tree nodes
560 */
561 while (here.ptr && qh != here.qh) {
562 if (qh->period > here.qh->period)
563 break;
564 prev = &here.qh->qh_next;
Alek Du3807e262009-07-14 07:23:29 +0800565 hw_p = &here.qh->hw->hw_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 here = *prev;
567 }
568 /* link in this qh, unless some earlier pass did that */
569 if (qh != here.qh) {
570 qh->qh_next = here;
571 if (here.qh)
Alek Du3807e262009-07-14 07:23:29 +0800572 qh->hw->hw_next = *hw_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 wmb ();
574 prev->qh = qh;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700575 *hw_p = QH_NEXT (ehci, qh->qh_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 }
578 qh->qh_state = QH_STATE_LINKED;
Alan Sternef4638f2009-07-31 10:41:40 -0400579 qh->xacterrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 qh_get (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 /* maybe enable periodic schedule processing */
David Brownell01c17142008-08-26 23:35:04 -0700588 return enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
David Brownell01c17142008-08-26 23:35:04 -0700591static int qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 unsigned i;
594 unsigned period;
595
596 // FIXME:
597 // IF this isn't high speed
598 // and this qh is active in the current uframe
599 // (and overlay token SplitXstate is false?)
600 // THEN
Harvey Harrison551509d2009-02-11 14:11:36 -0800601 // qh->hw_info1 |= cpu_to_hc32(1 << 7 /* "ignore" */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* high bandwidth, or otherwise part of every microframe */
604 if ((period = qh->period) == 0)
605 period = 1;
606
607 for (i = qh->start; i < ehci->periodic_size; i += period)
608 periodic_unlink (ehci, i, qh);
609
610 /* update per-qh bandwidth for usbfs */
611 ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
612 ? ((qh->usecs + qh->c_usecs) / qh->period)
613 : (qh->usecs * 8);
614
615 dev_dbg (&qh->dev->dev,
616 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
David Brownell7dedacf2005-08-04 18:06:41 -0700617 qh->period,
Alek Du3807e262009-07-14 07:23:29 +0800618 hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 qh, qh->start, qh->usecs, qh->c_usecs);
620
621 /* qh->qh_next still "live" to HC */
622 qh->qh_state = QH_STATE_UNLINK;
623 qh->qh_next.ptr = NULL;
624 qh_put (qh);
625
626 /* maybe turn off periodic schedule */
David Brownell01c17142008-08-26 23:35:04 -0700627 return disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
631{
Alan Sterna448c9d2009-08-19 12:22:44 -0400632 unsigned wait;
633 struct ehci_qh_hw *hw = qh->hw;
634 int rc;
635
636 /* If the QH isn't linked then there's nothing we can do
637 * unless we were called during a giveback, in which case
638 * qh_completions() has to deal with it.
639 */
640 if (qh->qh_state != QH_STATE_LINKED) {
641 if (qh->qh_state == QH_STATE_COMPLETING)
642 qh->needs_rescan = 1;
643 return;
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 qh_unlink_periodic (ehci, qh);
647
648 /* simple/paranoid: always delay, expecting the HC needs to read
649 * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
650 * expect khubd to clean up after any CSPLITs we won't issue.
651 * active high speed queues may need bigger delays...
652 */
653 if (list_empty (&qh->qtd_list)
Stefan Roese6dbd6822007-05-01 09:29:37 -0700654 || (cpu_to_hc32(ehci, QH_CMASK)
Alek Du3807e262009-07-14 07:23:29 +0800655 & hw->hw_info2) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 wait = 2;
657 else
658 wait = 55; /* worst case: 3 * 1024 */
659
660 udelay (wait);
661 qh->qh_state = QH_STATE_IDLE;
Alek Du3807e262009-07-14 07:23:29 +0800662 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 wmb ();
Alan Sterna448c9d2009-08-19 12:22:44 -0400664
665 qh_completions(ehci, qh);
666
667 /* reschedule QH iff another request is queued */
668 if (!list_empty(&qh->qtd_list) &&
669 HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
670 rc = qh_schedule(ehci, qh);
671
672 /* An error here likely indicates handshake failure
673 * or no space left in the schedule. Neither fault
674 * should happen often ...
675 *
676 * FIXME kill the now-dysfunctional queued urbs
677 */
678 if (rc != 0)
679 ehci_err(ehci, "can't reschedule qh %p, err %d\n",
680 qh, rc);
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/*-------------------------------------------------------------------------*/
685
686static int check_period (
David Brownell53bd6a62006-08-30 14:50:06 -0700687 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 unsigned frame,
689 unsigned uframe,
690 unsigned period,
691 unsigned usecs
692) {
693 int claimed;
694
695 /* complete split running into next frame?
696 * given FSTN support, we could sometimes check...
697 */
698 if (uframe >= 8)
699 return 0;
700
701 /*
702 * 80% periodic == 100 usec/uframe available
David Brownell53bd6a62006-08-30 14:50:06 -0700703 * convert "usecs we need" to "max already claimed"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
705 usecs = 100 - usecs;
706
707 /* we "know" 2 and 4 uframe intervals were rejected; so
708 * for period 0, check _every_ microframe in the schedule.
709 */
710 if (unlikely (period == 0)) {
711 do {
712 for (uframe = 0; uframe < 7; uframe++) {
713 claimed = periodic_usecs (ehci, frame, uframe);
714 if (claimed > usecs)
715 return 0;
716 }
717 } while ((frame += 1) < ehci->periodic_size);
718
719 /* just check the specified uframe, at that period */
720 } else {
721 do {
722 claimed = periodic_usecs (ehci, frame, uframe);
723 if (claimed > usecs)
724 return 0;
725 } while ((frame += period) < ehci->periodic_size);
726 }
727
728 // success!
729 return 1;
730}
731
732static int check_intr_schedule (
David Brownell53bd6a62006-08-30 14:50:06 -0700733 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned frame,
735 unsigned uframe,
736 const struct ehci_qh *qh,
Stefan Roese6dbd6822007-05-01 09:29:37 -0700737 __hc32 *c_maskp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738)
739{
David Brownell53bd6a62006-08-30 14:50:06 -0700740 int retval = -ENOSPC;
Dan Streetmanba47f662006-05-24 09:39:16 -0700741 u8 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
744 goto done;
745
746 if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
747 goto done;
748 if (!qh->c_usecs) {
749 retval = 0;
750 *c_maskp = 0;
751 goto done;
752 }
753
Dan Streetmanba47f662006-05-24 09:39:16 -0700754#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
755 if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
756 qh->tt_usecs)) {
757 unsigned i;
758
759 /* TODO : this may need FSTN for SSPLIT in uframe 5. */
760 for (i=uframe+1; i<8 && i<uframe+4; i++)
761 if (!check_period (ehci, frame, i,
762 qh->period, qh->c_usecs))
763 goto done;
764 else
765 mask |= 1 << i;
766
767 retval = 0;
768
Stefan Roese6dbd6822007-05-01 09:29:37 -0700769 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Dan Streetmanba47f662006-05-24 09:39:16 -0700770 }
771#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* Make sure this tt's buffer is also available for CSPLITs.
773 * We pessimize a bit; probably the typical full speed case
774 * doesn't need the second CSPLIT.
David Brownell53bd6a62006-08-30 14:50:06 -0700775 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * NOTE: both SPLIT and CSPLIT could be checked in just
777 * one smart pass...
778 */
779 mask = 0x03 << (uframe + qh->gap_uf);
Stefan Roese6dbd6822007-05-01 09:29:37 -0700780 *c_maskp = cpu_to_hc32(ehci, mask << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 mask |= 1 << uframe;
783 if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
784 if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
785 qh->period, qh->c_usecs))
786 goto done;
787 if (!check_period (ehci, frame, uframe + qh->gap_uf,
788 qh->period, qh->c_usecs))
789 goto done;
790 retval = 0;
791 }
Dan Streetmanba47f662006-05-24 09:39:16 -0700792#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793done:
794 return retval;
795}
796
797/* "first fit" scheduling policy used the first time through,
798 * or when the previous schedule slot can't be re-used.
799 */
Stefan Roese6dbd6822007-05-01 09:29:37 -0700800static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
David Brownell53bd6a62006-08-30 14:50:06 -0700802 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 unsigned uframe;
Stefan Roese6dbd6822007-05-01 09:29:37 -0700804 __hc32 c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
Alek Du3807e262009-07-14 07:23:29 +0800806 struct ehci_qh_hw *hw = qh->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 qh_refresh(ehci, qh);
Alek Du3807e262009-07-14 07:23:29 +0800809 hw->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 frame = qh->start;
811
812 /* reuse the previous schedule slots, if we can */
813 if (frame < qh->period) {
Alek Du3807e262009-07-14 07:23:29 +0800814 uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 status = check_intr_schedule (ehci, frame, --uframe,
816 qh, &c_mask);
817 } else {
818 uframe = 0;
819 c_mask = 0;
820 status = -ENOSPC;
821 }
822
823 /* else scan the schedule to find a group of slots such that all
824 * uframes have enough periodic bandwidth available.
825 */
826 if (status) {
827 /* "normal" case, uframing flexible except with splits */
828 if (qh->period) {
Alan Stern68335e82009-05-22 17:02:33 -0400829 int i;
830
831 for (i = qh->period; status && i > 0; --i) {
832 frame = ++ehci->random_frame % qh->period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 for (uframe = 0; uframe < 8; uframe++) {
834 status = check_intr_schedule (ehci,
835 frame, uframe, qh,
836 &c_mask);
837 if (status == 0)
838 break;
839 }
Alan Stern68335e82009-05-22 17:02:33 -0400840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* qh->period == 0 means every uframe */
843 } else {
844 frame = 0;
845 status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
846 }
847 if (status)
848 goto done;
849 qh->start = frame;
850
851 /* reset S-frame and (maybe) C-frame masks */
Alek Du3807e262009-07-14 07:23:29 +0800852 hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
853 hw->hw_info2 |= qh->period
Stefan Roese6dbd6822007-05-01 09:29:37 -0700854 ? cpu_to_hc32(ehci, 1 << uframe)
855 : cpu_to_hc32(ehci, QH_SMASK);
Alek Du3807e262009-07-14 07:23:29 +0800856 hw->hw_info2 |= c_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 } else
858 ehci_dbg (ehci, "reused qh %p schedule\n", qh);
859
860 /* stuff into the periodic schedule */
David Brownell53bd6a62006-08-30 14:50:06 -0700861 status = qh_link_periodic (ehci, qh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862done:
863 return status;
864}
865
866static int intr_submit (
867 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct urb *urb,
869 struct list_head *qtd_list,
Al Viro55016f12005-10-21 03:21:58 -0400870 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871) {
872 unsigned epnum;
873 unsigned long flags;
874 struct ehci_qh *qh;
Alan Sterne9df41c2007-08-08 11:48:02 -0400875 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct list_head empty;
877
878 /* get endpoint and transfer/schedule data */
Alan Sterne9df41c2007-08-08 11:48:02 -0400879 epnum = urb->ep->desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 spin_lock_irqsave (&ehci->lock, flags);
882
Alan Stern541c7d42010-06-22 16:39:10 -0400883 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100884 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -0400885 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100886 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400887 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
888 if (unlikely(status))
889 goto done_not_linked;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* get qh and force any scheduling errors */
892 INIT_LIST_HEAD (&empty);
Alan Sterne9df41c2007-08-08 11:48:02 -0400893 qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (qh == NULL) {
895 status = -ENOMEM;
896 goto done;
897 }
898 if (qh->qh_state == QH_STATE_IDLE) {
899 if ((status = qh_schedule (ehci, qh)) != 0)
900 goto done;
901 }
902
903 /* then queue the urb's tds to the qh */
Alan Sterne9df41c2007-08-08 11:48:02 -0400904 qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 BUG_ON (qh == NULL);
906
907 /* ... update usbfs periodic stats */
908 ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
909
910done:
Alan Sterne9df41c2007-08-08 11:48:02 -0400911 if (unlikely(status))
912 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
913done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 spin_unlock_irqrestore (&ehci->lock, flags);
915 if (status)
916 qtd_list_free (ehci, urb, qtd_list);
917
918 return status;
919}
920
921/*-------------------------------------------------------------------------*/
922
923/* ehci_iso_stream ops work with both ITD and SITD */
924
925static struct ehci_iso_stream *
Al Viro55016f12005-10-21 03:21:58 -0400926iso_stream_alloc (gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct ehci_iso_stream *stream;
929
Pekka Enberg7b842b62005-09-06 15:18:34 -0700930 stream = kzalloc(sizeof *stream, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (likely (stream != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 INIT_LIST_HEAD(&stream->td_list);
933 INIT_LIST_HEAD(&stream->free_list);
934 stream->next_uframe = -1;
935 stream->refcount = 1;
936 }
937 return stream;
938}
939
940static void
941iso_stream_init (
942 struct ehci_hcd *ehci,
943 struct ehci_iso_stream *stream,
944 struct usb_device *dev,
945 int pipe,
946 unsigned interval
947)
948{
949 static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
950
951 u32 buf1;
952 unsigned epnum, maxp;
953 int is_input;
954 long bandwidth;
955
956 /*
957 * this might be a "high bandwidth" highspeed endpoint,
958 * as encoded in the ep descriptor's wMaxPacket field
959 */
960 epnum = usb_pipeendpoint (pipe);
961 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
962 maxp = usb_maxpacket(dev, pipe, !is_input);
963 if (is_input) {
964 buf1 = (1 << 11);
965 } else {
966 buf1 = 0;
967 }
968
969 /* knows about ITD vs SITD */
970 if (dev->speed == USB_SPEED_HIGH) {
971 unsigned multi = hb_mult(maxp);
972
973 stream->highspeed = 1;
974
975 maxp = max_packet(maxp);
976 buf1 |= maxp;
977 maxp *= multi;
978
Stefan Roese6dbd6822007-05-01 09:29:37 -0700979 stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
980 stream->buf1 = cpu_to_hc32(ehci, buf1);
981 stream->buf2 = cpu_to_hc32(ehci, multi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /* usbfs wants to report the average usecs per frame tied up
984 * when transfers on this endpoint are scheduled ...
985 */
986 stream->usecs = HS_USECS_ISO (maxp);
987 bandwidth = stream->usecs * 8;
Alan Stern372dd6e2008-11-12 17:02:57 -0500988 bandwidth /= interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 } else {
991 u32 addr;
david-b@pacbell.netd0384202005-08-13 18:44:58 -0700992 int think_time;
Clemens Ladisch469d0222006-01-20 13:49:10 -0800993 int hs_transfers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 addr = dev->ttport << 24;
996 if (!ehci_is_TDI(ehci)
997 || (dev->tt->hub !=
998 ehci_to_hcd(ehci)->self.root_hub))
999 addr |= dev->tt->hub->devnum << 16;
1000 addr |= epnum << 8;
1001 addr |= dev->devnum;
1002 stream->usecs = HS_USECS_ISO (maxp);
david-b@pacbell.netd0384202005-08-13 18:44:58 -07001003 think_time = dev->tt ? dev->tt->think_time : 0;
1004 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
1005 dev->speed, is_input, 1, maxp));
Clemens Ladisch469d0222006-01-20 13:49:10 -08001006 hs_transfers = max (1u, (maxp + 187) / 188);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (is_input) {
1008 u32 tmp;
1009
1010 addr |= 1 << 31;
1011 stream->c_usecs = stream->usecs;
1012 stream->usecs = HS_USECS_ISO (1);
1013 stream->raw_mask = 1;
1014
Clemens Ladisch469d0222006-01-20 13:49:10 -08001015 /* c-mask as specified in USB 2.0 11.18.4 3.c */
1016 tmp = (1 << (hs_transfers + 2)) - 1;
1017 stream->raw_mask |= tmp << (8 + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 } else
Clemens Ladisch469d0222006-01-20 13:49:10 -08001019 stream->raw_mask = smask_out [hs_transfers - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 bandwidth = stream->usecs + stream->c_usecs;
Alan Stern372dd6e2008-11-12 17:02:57 -05001021 bandwidth /= interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* stream->splits gets created from raw_mask later */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001024 stream->address = cpu_to_hc32(ehci, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 stream->bandwidth = bandwidth;
1027
1028 stream->udev = dev;
1029
1030 stream->bEndpointAddress = is_input | epnum;
1031 stream->interval = interval;
1032 stream->maxp = maxp;
1033}
1034
1035static void
1036iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
1037{
1038 stream->refcount--;
1039
1040 /* free whenever just a dev->ep reference remains.
1041 * not like a QH -- no persistent state (toggle, halt)
1042 */
1043 if (stream->refcount == 1) {
1044 int is_in;
1045
1046 // BUG_ON (!list_empty(&stream->td_list));
1047
1048 while (!list_empty (&stream->free_list)) {
1049 struct list_head *entry;
1050
1051 entry = stream->free_list.next;
1052 list_del (entry);
1053
1054 /* knows about ITD vs SITD */
1055 if (stream->highspeed) {
1056 struct ehci_itd *itd;
1057
1058 itd = list_entry (entry, struct ehci_itd,
1059 itd_list);
1060 dma_pool_free (ehci->itd_pool, itd,
1061 itd->itd_dma);
1062 } else {
1063 struct ehci_sitd *sitd;
1064
1065 sitd = list_entry (entry, struct ehci_sitd,
1066 sitd_list);
1067 dma_pool_free (ehci->sitd_pool, sitd,
1068 sitd->sitd_dma);
1069 }
1070 }
1071
1072 is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
1073 stream->bEndpointAddress &= 0x0f;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001074 if (stream->ep)
1075 stream->ep->hcpriv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 kfree(stream);
1078 }
1079}
1080
1081static inline struct ehci_iso_stream *
1082iso_stream_get (struct ehci_iso_stream *stream)
1083{
1084 if (likely (stream != NULL))
1085 stream->refcount++;
1086 return stream;
1087}
1088
1089static struct ehci_iso_stream *
1090iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
1091{
1092 unsigned epnum;
1093 struct ehci_iso_stream *stream;
1094 struct usb_host_endpoint *ep;
1095 unsigned long flags;
1096
1097 epnum = usb_pipeendpoint (urb->pipe);
1098 if (usb_pipein(urb->pipe))
1099 ep = urb->dev->ep_in[epnum];
1100 else
1101 ep = urb->dev->ep_out[epnum];
1102
1103 spin_lock_irqsave (&ehci->lock, flags);
1104 stream = ep->hcpriv;
1105
1106 if (unlikely (stream == NULL)) {
1107 stream = iso_stream_alloc(GFP_ATOMIC);
1108 if (likely (stream != NULL)) {
1109 /* dev->ep owns the initial refcount */
1110 ep->hcpriv = stream;
1111 stream->ep = ep;
1112 iso_stream_init(ehci, stream, urb->dev, urb->pipe,
1113 urb->interval);
1114 }
1115
Clemens Ladisch1082f572010-03-01 17:18:56 +01001116 /* if dev->ep [epnum] is a QH, hw is set */
1117 } else if (unlikely (stream->hw != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
1119 urb->dev->devpath, epnum,
1120 usb_pipein(urb->pipe) ? "in" : "out");
1121 stream = NULL;
1122 }
1123
1124 /* caller guarantees an eventual matching iso_stream_put */
1125 stream = iso_stream_get (stream);
1126
1127 spin_unlock_irqrestore (&ehci->lock, flags);
1128 return stream;
1129}
1130
1131/*-------------------------------------------------------------------------*/
1132
1133/* ehci_iso_sched ops can be ITD-only or SITD-only */
1134
1135static struct ehci_iso_sched *
Al Viro55016f12005-10-21 03:21:58 -04001136iso_sched_alloc (unsigned packets, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct ehci_iso_sched *iso_sched;
1139 int size = sizeof *iso_sched;
1140
1141 size += packets * sizeof (struct ehci_iso_packet);
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001142 iso_sched = kzalloc(size, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (likely (iso_sched != NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 INIT_LIST_HEAD (&iso_sched->td_list);
1145 }
1146 return iso_sched;
1147}
1148
1149static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001150itd_sched_init(
1151 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct ehci_iso_sched *iso_sched,
1153 struct ehci_iso_stream *stream,
1154 struct urb *urb
1155)
1156{
1157 unsigned i;
1158 dma_addr_t dma = urb->transfer_dma;
1159
1160 /* how many uframes are needed for these transfers */
1161 iso_sched->span = urb->number_of_packets * stream->interval;
1162
1163 /* figure out per-uframe itd fields that we'll need later
1164 * when we fit new itds into the schedule.
1165 */
1166 for (i = 0; i < urb->number_of_packets; i++) {
1167 struct ehci_iso_packet *uframe = &iso_sched->packet [i];
1168 unsigned length;
1169 dma_addr_t buf;
1170 u32 trans;
1171
1172 length = urb->iso_frame_desc [i].length;
1173 buf = dma + urb->iso_frame_desc [i].offset;
1174
1175 trans = EHCI_ISOC_ACTIVE;
1176 trans |= buf & 0x0fff;
1177 if (unlikely (((i + 1) == urb->number_of_packets))
1178 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1179 trans |= EHCI_ITD_IOC;
1180 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001181 uframe->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
David Brownell77078572005-05-28 10:46:18 -07001183 /* might need to cross a buffer page within a uframe */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 uframe->bufp = (buf & ~(u64)0x0fff);
1185 buf += length;
1186 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
1187 uframe->cross = 1;
1188 }
1189}
1190
1191static void
1192iso_sched_free (
1193 struct ehci_iso_stream *stream,
1194 struct ehci_iso_sched *iso_sched
1195)
1196{
1197 if (!iso_sched)
1198 return;
1199 // caller must hold ehci->lock!
1200 list_splice (&iso_sched->td_list, &stream->free_list);
1201 kfree (iso_sched);
1202}
1203
1204static int
1205itd_urb_transaction (
1206 struct ehci_iso_stream *stream,
1207 struct ehci_hcd *ehci,
1208 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001209 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210)
1211{
1212 struct ehci_itd *itd;
1213 dma_addr_t itd_dma;
1214 int i;
1215 unsigned num_itds;
1216 struct ehci_iso_sched *sched;
1217 unsigned long flags;
1218
1219 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1220 if (unlikely (sched == NULL))
1221 return -ENOMEM;
1222
Stefan Roese6dbd6822007-05-01 09:29:37 -07001223 itd_sched_init(ehci, sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (urb->interval < 8)
1226 num_itds = 1 + (sched->span + 7) / 8;
1227 else
1228 num_itds = urb->number_of_packets;
1229
1230 /* allocate/init ITDs */
1231 spin_lock_irqsave (&ehci->lock, flags);
1232 for (i = 0; i < num_itds; i++) {
1233
1234 /* free_list.next might be cache-hot ... but maybe
1235 * the HC caches it too. avoid that issue for now.
1236 */
1237
1238 /* prefer previously-allocated itds */
1239 if (likely (!list_empty(&stream->free_list))) {
1240 itd = list_entry (stream->free_list.prev,
Stefan Roese6dbd6822007-05-01 09:29:37 -07001241 struct ehci_itd, itd_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 list_del (&itd->itd_list);
1243 itd_dma = itd->itd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001244 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 spin_unlock_irqrestore (&ehci->lock, flags);
1246 itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
1247 &itd_dma);
1248 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001249 if (!itd) {
1250 iso_sched_free(stream, sched);
1251 spin_unlock_irqrestore(&ehci->lock, flags);
1252 return -ENOMEM;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 memset (itd, 0, sizeof *itd);
1257 itd->itd_dma = itd_dma;
1258 list_add (&itd->itd_list, &sched->td_list);
1259 }
1260 spin_unlock_irqrestore (&ehci->lock, flags);
1261
1262 /* temporarily store schedule info in hcpriv */
1263 urb->hcpriv = sched;
1264 urb->error_count = 0;
1265 return 0;
1266}
1267
1268/*-------------------------------------------------------------------------*/
1269
1270static inline int
1271itd_slot_ok (
1272 struct ehci_hcd *ehci,
1273 u32 mod,
1274 u32 uframe,
1275 u8 usecs,
1276 u32 period
1277)
1278{
1279 uframe %= period;
1280 do {
1281 /* can't commit more than 80% periodic == 100 usec */
1282 if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
1283 > (100 - usecs))
1284 return 0;
1285
1286 /* we know urb->interval is 2^N uframes */
1287 uframe += period;
1288 } while (uframe < mod);
1289 return 1;
1290}
1291
1292static inline int
1293sitd_slot_ok (
1294 struct ehci_hcd *ehci,
1295 u32 mod,
1296 struct ehci_iso_stream *stream,
1297 u32 uframe,
1298 struct ehci_iso_sched *sched,
1299 u32 period_uframes
1300)
1301{
1302 u32 mask, tmp;
1303 u32 frame, uf;
1304
1305 mask = stream->raw_mask << (uframe & 7);
1306
1307 /* for IN, don't wrap CSPLIT into the next frame */
1308 if (mask & ~0xffff)
1309 return 0;
1310
1311 /* this multi-pass logic is simple, but performance may
1312 * suffer when the schedule data isn't cached.
1313 */
1314
1315 /* check bandwidth */
1316 uframe %= period_uframes;
1317 do {
1318 u32 max_used;
1319
1320 frame = uframe >> 3;
1321 uf = uframe & 7;
1322
Dan Streetmanba47f662006-05-24 09:39:16 -07001323#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
1324 /* The tt's fullspeed bus bandwidth must be available.
1325 * tt_available scheduling guarantees 10+% for control/bulk.
1326 */
1327 if (!tt_available (ehci, period_uframes << 3,
1328 stream->udev, frame, uf, stream->tt_usecs))
1329 return 0;
1330#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* tt must be idle for start(s), any gap, and csplit.
1332 * assume scheduling slop leaves 10+% for control/bulk.
1333 */
1334 if (!tt_no_collision (ehci, period_uframes << 3,
1335 stream->udev, frame, mask))
1336 return 0;
Dan Streetmanba47f662006-05-24 09:39:16 -07001337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* check starts (OUT uses more than one) */
1340 max_used = 100 - stream->usecs;
1341 for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
1342 if (periodic_usecs (ehci, frame, uf) > max_used)
1343 return 0;
1344 }
1345
1346 /* for IN, check CSPLIT */
1347 if (stream->c_usecs) {
Clemens Ladisch0c734622006-01-22 10:32:49 -08001348 uf = uframe & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 max_used = 100 - stream->c_usecs;
1350 do {
1351 tmp = 1 << uf;
1352 tmp <<= 8;
1353 if ((stream->raw_mask & tmp) == 0)
1354 continue;
1355 if (periodic_usecs (ehci, frame, uf)
1356 > max_used)
1357 return 0;
1358 } while (++uf < 8);
1359 }
1360
1361 /* we know urb->interval is 2^N uframes */
1362 uframe += period_uframes;
1363 } while (uframe < mod);
1364
Stefan Roese6dbd6822007-05-01 09:29:37 -07001365 stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 1;
1367}
1368
1369/*
1370 * This scheduler plans almost as far into the future as it has actual
1371 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
1372 * "as small as possible" to be cache-friendlier.) That limits the size
1373 * transfers you can stream reliably; avoid more than 64 msec per urb.
1374 * Also avoid queue depths of less than ehci's worst irq latency (affected
1375 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
1376 * and other factors); or more than about 230 msec total (for portability,
1377 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1378 */
1379
Sarah Sharpd7e055f2009-10-27 10:54:49 -07001380#define SCHEDULE_SLOP 80 /* microframes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382static int
1383iso_stream_schedule (
1384 struct ehci_hcd *ehci,
1385 struct urb *urb,
1386 struct ehci_iso_stream *stream
1387)
1388{
Alan Sternffda0802010-07-14 11:03:46 -04001389 u32 now, next, start, period, span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int status;
1391 unsigned mod = ehci->periodic_size << 3;
1392 struct ehci_iso_sched *sched = urb->hcpriv;
1393
Alan Sternffda0802010-07-14 11:03:46 -04001394 period = urb->interval;
1395 span = sched->span;
1396 if (!stream->highspeed) {
1397 period <<= 3;
1398 span <<= 3;
1399 }
1400
1401 if (span > mod - SCHEDULE_SLOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 ehci_dbg (ehci, "iso request %p too long\n", urb);
1403 status = -EFBIG;
1404 goto fail;
1405 }
1406
Alan Sternbccbefa2010-07-14 11:03:36 -04001407 now = ehci_readl(ehci, &ehci->regs->frame_index) & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Alan Sternb40e43f2008-05-20 16:59:10 -04001409 /* Typical case: reuse current schedule, stream is still active.
1410 * Hopefully there are no gaps from the host falling behind
1411 * (irq delays etc), but if there are we'll take the next
1412 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
1414 if (likely (!list_empty (&stream->td_list))) {
Alan Stern1fb2e052010-07-14 11:03:53 -04001415 u32 excess;
Sarah Sharpdccd5742009-10-27 10:55:05 -07001416
1417 /* For high speed devices, allow scheduling within the
Alan Sternae68a832010-07-14 11:03:23 -04001418 * isochronous scheduling threshold. For full speed devices
1419 * and Intel PCI-based controllers, don't (work around for
1420 * Intel ICH9 bug).
Sarah Sharpdccd5742009-10-27 10:55:05 -07001421 */
Alan Sternae68a832010-07-14 11:03:23 -04001422 if (!stream->highspeed && ehci->fs_i_thresh)
Sarah Sharpdccd5742009-10-27 10:55:05 -07001423 next = now + ehci->i_thresh;
1424 else
1425 next = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04001426
Alan Stern1fb2e052010-07-14 11:03:53 -04001427 /* Fell behind (by up to twice the slop amount)?
1428 * We decide based on the time of the last currently-scheduled
1429 * slot, not the time of the next available slot.
1430 */
1431 excess = (stream->next_uframe - period - next) & (mod - 1);
1432 if (excess >= mod - 2 * SCHEDULE_SLOP)
1433 start = next + excess - mod + period *
1434 DIV_ROUND_UP(mod - excess, period);
1435 else
1436 start = next + excess + period;
1437 if (start - now >= mod) {
1438 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1439 urb, start - now - period, period,
1440 mod);
Alan Sternb40e43f2008-05-20 16:59:10 -04001441 status = -EFBIG;
1442 goto fail;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
1446 /* need to schedule; when's the next (u)frame we could start?
1447 * this is bigger than ehci->i_thresh allows; scheduling itself
1448 * isn't free, the slop should handle reasonably slow cpus. it
1449 * can also help high bandwidth if the dma and irq loads don't
1450 * jump until after the queue is primed.
1451 */
Alan Stern1fb2e052010-07-14 11:03:53 -04001452 else {
1453 start = SCHEDULE_SLOP + (now & ~0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alan Stern1fb2e052010-07-14 11:03:53 -04001455 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Alan Stern1fb2e052010-07-14 11:03:53 -04001457 /* find a uframe slot with enough bandwidth */
1458 next = start + period;
1459 for (; start < next; start++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alan Stern1fb2e052010-07-14 11:03:53 -04001461 /* check schedule: enough space? */
1462 if (stream->highspeed) {
1463 if (itd_slot_ok(ehci, mod, start,
1464 stream->usecs, period))
1465 break;
1466 } else {
1467 if ((start % 8) >= 6)
1468 continue;
1469 if (sitd_slot_ok(ehci, mod, stream,
1470 start, sched, period))
1471 break;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
Alan Stern1fb2e052010-07-14 11:03:53 -04001475 /* no room in the schedule */
1476 if (start == next) {
1477 ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
1478 urb, now, now + mod);
1479 status = -ENOSPC;
1480 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 }
1483
Alan Stern1fb2e052010-07-14 11:03:53 -04001484 /* Tried to schedule too far into the future? */
1485 if (unlikely(start - now + span - period
1486 >= mod - 2 * SCHEDULE_SLOP)) {
1487 ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
1488 urb, start - now, span - period,
1489 mod - 2 * SCHEDULE_SLOP);
1490 status = -EFBIG;
1491 goto fail;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alan Stern1fb2e052010-07-14 11:03:53 -04001494 stream->next_uframe = start & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* report high speed start in uframes; full speed, in frames */
1497 urb->start_frame = stream->next_uframe;
1498 if (!stream->highspeed)
1499 urb->start_frame >>= 3;
1500 return 0;
Alan Stern1fb2e052010-07-14 11:03:53 -04001501
1502 fail:
1503 iso_sched_free(stream, sched);
1504 urb->hcpriv = NULL;
1505 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508/*-------------------------------------------------------------------------*/
1509
1510static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001511itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
1512 struct ehci_itd *itd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 int i;
1515
David Brownell77078572005-05-28 10:46:18 -07001516 /* it's been recently zeroed */
Stefan Roese6dbd6822007-05-01 09:29:37 -07001517 itd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 itd->hw_bufp [0] = stream->buf0;
1519 itd->hw_bufp [1] = stream->buf1;
1520 itd->hw_bufp [2] = stream->buf2;
1521
1522 for (i = 0; i < 8; i++)
1523 itd->index[i] = -1;
1524
1525 /* All other fields are filled when scheduling */
1526}
1527
1528static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001529itd_patch(
1530 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct ehci_itd *itd,
1532 struct ehci_iso_sched *iso_sched,
1533 unsigned index,
David Brownell77078572005-05-28 10:46:18 -07001534 u16 uframe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535)
1536{
1537 struct ehci_iso_packet *uf = &iso_sched->packet [index];
1538 unsigned pg = itd->pg;
1539
1540 // BUG_ON (pg == 6 && uf->cross);
1541
1542 uframe &= 0x07;
1543 itd->index [uframe] = index;
1544
Stefan Roese6dbd6822007-05-01 09:29:37 -07001545 itd->hw_transaction[uframe] = uf->transaction;
1546 itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
1547 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
1548 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* iso_frame_desc[].offset must be strictly increasing */
David Brownell77078572005-05-28 10:46:18 -07001551 if (unlikely (uf->cross)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 u64 bufp = uf->bufp + 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 itd->pg = ++pg;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001555 itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
1556 itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558}
1559
1560static inline void
1561itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
1562{
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001563 union ehci_shadow *prev = &ehci->pshadow[frame];
1564 __hc32 *hw_p = &ehci->periodic[frame];
1565 union ehci_shadow here = *prev;
1566 __hc32 type = 0;
1567
1568 /* skip any iso nodes which might belong to previous microframes */
1569 while (here.ptr) {
1570 type = Q_NEXT_TYPE(ehci, *hw_p);
1571 if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
1572 break;
1573 prev = periodic_next_shadow(ehci, prev, type);
1574 hw_p = shadow_next_periodic(ehci, &here, type);
1575 here = *prev;
1576 }
1577
1578 itd->itd_next = here;
1579 itd->hw_next = *hw_p;
1580 prev->itd = itd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 itd->frame = frame;
1582 wmb ();
Clemens Ladisch92bc3642010-03-01 09:12:50 +01001583 *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Alex He05570292010-12-07 10:10:08 +08001586#define AB_REG_BAR_LOW 0xe0
1587#define AB_REG_BAR_HIGH 0xe1
1588#define AB_INDX(addr) ((addr) + 0x00)
1589#define AB_DATA(addr) ((addr) + 0x04)
1590#define NB_PCIE_INDX_ADDR 0xe0
1591#define NB_PCIE_INDX_DATA 0xe4
1592#define NB_PIF0_PWRDOWN_0 0x01100012
1593#define NB_PIF0_PWRDOWN_1 0x01100013
1594
1595static void ehci_quirk_amd_L1(struct ehci_hcd *ehci, int disable)
1596{
1597 u32 addr, addr_low, addr_high, val;
1598
1599 outb_p(AB_REG_BAR_LOW, 0xcd6);
1600 addr_low = inb_p(0xcd7);
1601 outb_p(AB_REG_BAR_HIGH, 0xcd6);
1602 addr_high = inb_p(0xcd7);
1603 addr = addr_high << 8 | addr_low;
1604 outl_p(0x30, AB_INDX(addr));
1605 outl_p(0x40, AB_DATA(addr));
1606 outl_p(0x34, AB_INDX(addr));
1607 val = inl_p(AB_DATA(addr));
1608
1609 if (disable) {
1610 val &= ~0x8;
1611 val |= (1 << 4) | (1 << 9);
1612 } else {
1613 val |= 0x8;
1614 val &= ~((1 << 4) | (1 << 9));
1615 }
1616 outl_p(val, AB_DATA(addr));
1617
1618 if (amd_nb_dev) {
1619 addr = NB_PIF0_PWRDOWN_0;
1620 pci_write_config_dword(amd_nb_dev, NB_PCIE_INDX_ADDR, addr);
1621 pci_read_config_dword(amd_nb_dev, NB_PCIE_INDX_DATA, &val);
1622 if (disable)
1623 val &= ~(0x3f << 7);
1624 else
1625 val |= 0x3f << 7;
1626
1627 pci_write_config_dword(amd_nb_dev, NB_PCIE_INDX_DATA, val);
1628
1629 addr = NB_PIF0_PWRDOWN_1;
1630 pci_write_config_dword(amd_nb_dev, NB_PCIE_INDX_ADDR, addr);
1631 pci_read_config_dword(amd_nb_dev, NB_PCIE_INDX_DATA, &val);
1632 if (disable)
1633 val &= ~(0x3f << 7);
1634 else
1635 val |= 0x3f << 7;
1636
1637 pci_write_config_dword(amd_nb_dev, NB_PCIE_INDX_DATA, val);
1638 }
1639
1640 return;
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643/* fit urb's itds into the selected schedule slot; activate as needed */
1644static int
1645itd_link_urb (
1646 struct ehci_hcd *ehci,
1647 struct urb *urb,
1648 unsigned mod,
1649 struct ehci_iso_stream *stream
1650)
1651{
David Brownell77078572005-05-28 10:46:18 -07001652 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 unsigned next_uframe, uframe, frame;
1654 struct ehci_iso_sched *iso_sched = urb->hcpriv;
1655 struct ehci_itd *itd;
1656
Alan Sternbccbefa2010-07-14 11:03:36 -04001657 next_uframe = stream->next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (unlikely (list_empty(&stream->td_list))) {
1660 ehci_to_hcd(ehci)->self.bandwidth_allocated
1661 += stream->bandwidth;
1662 ehci_vdbg (ehci,
1663 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
1664 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
1665 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1666 urb->interval,
1667 next_uframe >> 3, next_uframe & 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Alex He05570292010-12-07 10:10:08 +08001669
1670 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
1671 if (ehci->amd_l1_fix == 1)
1672 ehci_quirk_amd_L1(ehci, 1);
1673 }
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
1676
1677 /* fill iTDs uframe by uframe */
1678 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
1679 if (itd == NULL) {
1680 /* ASSERT: we have all necessary itds */
1681 // BUG_ON (list_empty (&iso_sched->td_list));
1682
1683 /* ASSERT: no itds for this endpoint in this uframe */
1684
1685 itd = list_entry (iso_sched->td_list.next,
1686 struct ehci_itd, itd_list);
1687 list_move_tail (&itd->itd_list, &stream->td_list);
1688 itd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01001689 itd->urb = urb;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001690 itd_init (ehci, stream, itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
1693 uframe = next_uframe & 0x07;
1694 frame = next_uframe >> 3;
1695
Stefan Roese6dbd6822007-05-01 09:29:37 -07001696 itd_patch(ehci, itd, iso_sched, packet, uframe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 next_uframe += stream->interval;
Alan Sternbccbefa2010-07-14 11:03:36 -04001699 next_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 packet++;
1701
1702 /* link completed itds into the schedule */
1703 if (((next_uframe >> 3) != frame)
1704 || packet == urb->number_of_packets) {
Alan Sternbccbefa2010-07-14 11:03:36 -04001705 itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 itd = NULL;
1707 }
1708 }
1709 stream->next_uframe = next_uframe;
1710
1711 /* don't need that schedule data any more */
1712 iso_sched_free (stream, iso_sched);
1713 urb->hcpriv = NULL;
1714
1715 timer_action (ehci, TIMER_IO_WATCHDOG);
David Brownell01c17142008-08-26 23:35:04 -07001716 return enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
1720
David Brownell30bf54e2007-12-16 22:37:40 -08001721/* Process and recycle a completed ITD. Return true iff its urb completed,
1722 * and hence its completion callback probably added things to the hardware
1723 * schedule.
1724 *
1725 * Note that we carefully avoid recycling this descriptor until after any
1726 * completion callback runs, so that it won't be reused quickly. That is,
1727 * assuming (a) no more than two urbs per frame on this endpoint, and also
1728 * (b) only this endpoint's completions submit URBs. It seems some silicon
1729 * corrupts things if you reuse completed descriptors very quickly...
1730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731static unsigned
1732itd_complete (
1733 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01001734 struct ehci_itd *itd
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735) {
1736 struct urb *urb = itd->urb;
1737 struct usb_iso_packet_descriptor *desc;
1738 u32 t;
1739 unsigned uframe;
1740 int urb_index = -1;
1741 struct ehci_iso_stream *stream = itd->stream;
1742 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08001743 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* for each uframe with a packet */
1746 for (uframe = 0; uframe < 8; uframe++) {
1747 if (likely (itd->index[uframe] == -1))
1748 continue;
1749 urb_index = itd->index[uframe];
1750 desc = &urb->iso_frame_desc [urb_index];
1751
Stefan Roese6dbd6822007-05-01 09:29:37 -07001752 t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 itd->hw_transaction [uframe] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 /* report transfer status */
1756 if (unlikely (t & ISO_ERRS)) {
1757 urb->error_count++;
1758 if (t & EHCI_ISOC_BUF_ERR)
1759 desc->status = usb_pipein (urb->pipe)
1760 ? -ENOSR /* hc couldn't read */
1761 : -ECOMM; /* hc couldn't write */
1762 else if (t & EHCI_ISOC_BABBLE)
1763 desc->status = -EOVERFLOW;
1764 else /* (t & EHCI_ISOC_XACTERR) */
1765 desc->status = -EPROTO;
1766
1767 /* HC need not update length with this error */
Alan Sternec6d67e2009-06-29 14:34:59 -04001768 if (!(t & EHCI_ISOC_BABBLE)) {
1769 desc->actual_length = EHCI_ITD_LENGTH(t);
1770 urb->actual_length += desc->actual_length;
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1773 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04001774 desc->actual_length = EHCI_ITD_LENGTH(t);
1775 urb->actual_length += desc->actual_length;
Alan Sternb40e43f2008-05-20 16:59:10 -04001776 } else {
1777 /* URB was too late */
1778 desc->status = -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 }
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* handle completion now? */
1783 if (likely ((urb_index + 1) != urb->number_of_packets))
David Brownell30bf54e2007-12-16 22:37:40 -08001784 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* ASSERT: it's really the last itd for this urb
1787 list_for_each_entry (itd, &stream->td_list, itd_list)
1788 BUG_ON (itd->urb == urb);
1789 */
1790
David Brownellaa16ca32007-12-30 23:45:19 -08001791 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05001792 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04001793 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08001794 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 urb = NULL;
David Brownell01c17142008-08-26 23:35:04 -07001796 (void) disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1798
Alex He05570292010-12-07 10:10:08 +08001799 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
1800 if (ehci->amd_l1_fix == 1)
1801 ehci_quirk_amd_L1(ehci, 0);
1802 }
1803
Karsten Wiese508db8c2009-02-26 01:47:48 +01001804 if (unlikely(list_is_singular(&stream->td_list))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ehci_to_hcd(ehci)->self.bandwidth_allocated
1806 -= stream->bandwidth;
1807 ehci_vdbg (ehci,
1808 "deschedule devp %s ep%d%s-iso\n",
1809 dev->devpath, stream->bEndpointAddress & 0x0f,
1810 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1811 }
1812 iso_stream_put (ehci, stream);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001813
David Brownell30bf54e2007-12-16 22:37:40 -08001814done:
David Brownell30bf54e2007-12-16 22:37:40 -08001815 itd->urb = NULL;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08001816 if (ehci->clock_frame != itd->frame || itd->index[7] != -1) {
1817 /* OK to recycle this ITD now. */
1818 itd->stream = NULL;
1819 list_move(&itd->itd_list, &stream->free_list);
1820 iso_stream_put(ehci, stream);
1821 } else {
1822 /* HW might remember this ITD, so we can't recycle it yet.
1823 * Move it to a safe place until a new frame starts.
1824 */
1825 list_move(&itd->itd_list, &ehci->cached_itd_list);
1826 if (stream->refcount == 2) {
1827 /* If iso_stream_put() were called here, stream
1828 * would be freed. Instead, just prevent reuse.
1829 */
1830 stream->ep->hcpriv = NULL;
1831 stream->ep = NULL;
1832 }
1833 }
David Brownell30bf54e2007-12-16 22:37:40 -08001834 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837/*-------------------------------------------------------------------------*/
1838
Olav Kongas5db539e2005-06-23 20:25:36 +03001839static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001840 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 int status = -EINVAL;
1843 unsigned long flags;
1844 struct ehci_iso_stream *stream;
1845
1846 /* Get iso_stream head */
1847 stream = iso_stream_find (ehci, urb);
1848 if (unlikely (stream == NULL)) {
1849 ehci_dbg (ehci, "can't get iso stream\n");
1850 return -ENOMEM;
1851 }
1852 if (unlikely (urb->interval != stream->interval)) {
1853 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
1854 stream->interval, urb->interval);
1855 goto done;
1856 }
1857
1858#ifdef EHCI_URB_TRACE
1859 ehci_dbg (ehci,
1860 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001861 __func__, urb->dev->devpath, urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 usb_pipeendpoint (urb->pipe),
1863 usb_pipein (urb->pipe) ? "in" : "out",
1864 urb->transfer_buffer_length,
1865 urb->number_of_packets, urb->interval,
1866 stream);
1867#endif
1868
1869 /* allocate ITDs w/o locking anything */
1870 status = itd_urb_transaction (stream, ehci, urb, mem_flags);
1871 if (unlikely (status < 0)) {
1872 ehci_dbg (ehci, "can't init itds\n");
1873 goto done;
1874 }
1875
1876 /* schedule ... need to lock */
1877 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001878 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001879 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04001880 goto done_not_linked;
1881 }
1882 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
1883 if (unlikely(status))
1884 goto done_not_linked;
1885 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07001886 if (likely (status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04001888 else
1889 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1890done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 spin_unlock_irqrestore (&ehci->lock, flags);
1892
1893done:
1894 if (unlikely (status < 0))
1895 iso_stream_put (ehci, stream);
1896 return status;
1897}
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899/*-------------------------------------------------------------------------*/
1900
1901/*
1902 * "Split ISO TDs" ... used for USB 1.1 devices going through the
1903 * TTs in USB 2.0 hubs. These need microframe scheduling.
1904 */
1905
1906static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07001907sitd_sched_init(
1908 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct ehci_iso_sched *iso_sched,
1910 struct ehci_iso_stream *stream,
1911 struct urb *urb
1912)
1913{
1914 unsigned i;
1915 dma_addr_t dma = urb->transfer_dma;
1916
1917 /* how many frames are needed for these transfers */
1918 iso_sched->span = urb->number_of_packets * stream->interval;
1919
1920 /* figure out per-frame sitd fields that we'll need later
1921 * when we fit new sitds into the schedule.
1922 */
1923 for (i = 0; i < urb->number_of_packets; i++) {
1924 struct ehci_iso_packet *packet = &iso_sched->packet [i];
1925 unsigned length;
1926 dma_addr_t buf;
1927 u32 trans;
1928
1929 length = urb->iso_frame_desc [i].length & 0x03ff;
1930 buf = dma + urb->iso_frame_desc [i].offset;
1931
1932 trans = SITD_STS_ACTIVE;
1933 if (((i + 1) == urb->number_of_packets)
1934 && !(urb->transfer_flags & URB_NO_INTERRUPT))
1935 trans |= SITD_IOC;
1936 trans |= length << 16;
Stefan Roese6dbd6822007-05-01 09:29:37 -07001937 packet->transaction = cpu_to_hc32(ehci, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* might need to cross a buffer page within a td */
1940 packet->bufp = buf;
1941 packet->buf1 = (buf + length) & ~0x0fff;
1942 if (packet->buf1 != (buf & ~(u64)0x0fff))
1943 packet->cross = 1;
1944
David Brownell53bd6a62006-08-30 14:50:06 -07001945 /* OUT uses multiple start-splits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (stream->bEndpointAddress & USB_DIR_IN)
1947 continue;
1948 length = (length + 187) / 188;
1949 if (length > 1) /* BEGIN vs ALL */
1950 length |= 1 << 3;
1951 packet->buf1 |= length;
1952 }
1953}
1954
1955static int
1956sitd_urb_transaction (
1957 struct ehci_iso_stream *stream,
1958 struct ehci_hcd *ehci,
1959 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001960 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961)
1962{
1963 struct ehci_sitd *sitd;
1964 dma_addr_t sitd_dma;
1965 int i;
1966 struct ehci_iso_sched *iso_sched;
1967 unsigned long flags;
1968
1969 iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
1970 if (iso_sched == NULL)
1971 return -ENOMEM;
1972
Stefan Roese6dbd6822007-05-01 09:29:37 -07001973 sitd_sched_init(ehci, iso_sched, stream, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* allocate/init sITDs */
1976 spin_lock_irqsave (&ehci->lock, flags);
1977 for (i = 0; i < urb->number_of_packets; i++) {
1978
1979 /* NOTE: for now, we don't try to handle wraparound cases
1980 * for IN (using sitd->hw_backpointer, like a FSTN), which
1981 * means we never need two sitds for full speed packets.
1982 */
1983
1984 /* free_list.next might be cache-hot ... but maybe
1985 * the HC caches it too. avoid that issue for now.
1986 */
1987
1988 /* prefer previously-allocated sitds */
1989 if (!list_empty(&stream->free_list)) {
1990 sitd = list_entry (stream->free_list.prev,
1991 struct ehci_sitd, sitd_list);
1992 list_del (&sitd->sitd_list);
1993 sitd_dma = sitd->sitd_dma;
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001994 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 spin_unlock_irqrestore (&ehci->lock, flags);
1996 sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
1997 &sitd_dma);
1998 spin_lock_irqsave (&ehci->lock, flags);
Karsten Wiese3d01f0f2008-02-19 12:31:49 -08001999 if (!sitd) {
2000 iso_sched_free(stream, iso_sched);
2001 spin_unlock_irqrestore(&ehci->lock, flags);
2002 return -ENOMEM;
2003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 memset (sitd, 0, sizeof *sitd);
2007 sitd->sitd_dma = sitd_dma;
2008 list_add (&sitd->sitd_list, &iso_sched->td_list);
2009 }
2010
2011 /* temporarily store schedule info in hcpriv */
2012 urb->hcpriv = iso_sched;
2013 urb->error_count = 0;
2014
2015 spin_unlock_irqrestore (&ehci->lock, flags);
2016 return 0;
2017}
2018
2019/*-------------------------------------------------------------------------*/
2020
2021static inline void
Stefan Roese6dbd6822007-05-01 09:29:37 -07002022sitd_patch(
2023 struct ehci_hcd *ehci,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct ehci_iso_stream *stream,
2025 struct ehci_sitd *sitd,
2026 struct ehci_iso_sched *iso_sched,
2027 unsigned index
2028)
2029{
2030 struct ehci_iso_packet *uf = &iso_sched->packet [index];
2031 u64 bufp = uf->bufp;
2032
Stefan Roese6dbd6822007-05-01 09:29:37 -07002033 sitd->hw_next = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 sitd->hw_fullspeed_ep = stream->address;
2035 sitd->hw_uframe = stream->splits;
2036 sitd->hw_results = uf->transaction;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002037 sitd->hw_backpointer = EHCI_LIST_END(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 bufp = uf->bufp;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002040 sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
2041 sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Stefan Roese6dbd6822007-05-01 09:29:37 -07002043 sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (uf->cross)
2045 bufp += 4096;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002046 sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 sitd->index = index;
2048}
2049
2050static inline void
2051sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
2052{
2053 /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
2054 sitd->sitd_next = ehci->pshadow [frame];
2055 sitd->hw_next = ehci->periodic [frame];
2056 ehci->pshadow [frame].sitd = sitd;
2057 sitd->frame = frame;
2058 wmb ();
Stefan Roese6dbd6822007-05-01 09:29:37 -07002059 ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
2062/* fit urb's sitds into the selected schedule slot; activate as needed */
2063static int
2064sitd_link_urb (
2065 struct ehci_hcd *ehci,
2066 struct urb *urb,
2067 unsigned mod,
2068 struct ehci_iso_stream *stream
2069)
2070{
2071 int packet;
2072 unsigned next_uframe;
2073 struct ehci_iso_sched *sched = urb->hcpriv;
2074 struct ehci_sitd *sitd;
2075
2076 next_uframe = stream->next_uframe;
2077
2078 if (list_empty(&stream->td_list)) {
2079 /* usbfs ignores TT bandwidth */
2080 ehci_to_hcd(ehci)->self.bandwidth_allocated
2081 += stream->bandwidth;
2082 ehci_vdbg (ehci,
2083 "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
2084 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
2085 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
Alan Sternbccbefa2010-07-14 11:03:36 -04002086 (next_uframe >> 3) & (ehci->periodic_size - 1),
Stefan Roese6dbd6822007-05-01 09:29:37 -07002087 stream->interval, hc32_to_cpu(ehci, stream->splits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Alex He05570292010-12-07 10:10:08 +08002089
2090 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
2091 if (ehci->amd_l1_fix == 1)
2092 ehci_quirk_amd_L1(ehci, 1);
2093 }
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
2096
2097 /* fill sITDs frame by frame */
2098 for (packet = 0, sitd = NULL;
2099 packet < urb->number_of_packets;
2100 packet++) {
2101
2102 /* ASSERT: we have all necessary sitds */
2103 BUG_ON (list_empty (&sched->td_list));
2104
2105 /* ASSERT: no itds for this endpoint in this frame */
2106
2107 sitd = list_entry (sched->td_list.next,
2108 struct ehci_sitd, sitd_list);
2109 list_move_tail (&sitd->sitd_list, &stream->td_list);
2110 sitd->stream = iso_stream_get (stream);
Karsten Wiese508db8c2009-02-26 01:47:48 +01002111 sitd->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Stefan Roese6dbd6822007-05-01 09:29:37 -07002113 sitd_patch(ehci, stream, sitd, sched, packet);
Alan Sternbccbefa2010-07-14 11:03:36 -04002114 sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 sitd);
2116
2117 next_uframe += stream->interval << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002119 stream->next_uframe = next_uframe & (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* don't need that schedule data any more */
2122 iso_sched_free (stream, sched);
2123 urb->hcpriv = NULL;
2124
2125 timer_action (ehci, TIMER_IO_WATCHDOG);
David Brownell01c17142008-08-26 23:35:04 -07002126 return enable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129/*-------------------------------------------------------------------------*/
2130
2131#define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
David Brownell53bd6a62006-08-30 14:50:06 -07002132 | SITD_STS_XACT | SITD_STS_MMF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David Brownell30bf54e2007-12-16 22:37:40 -08002134/* Process and recycle a completed SITD. Return true iff its urb completed,
2135 * and hence its completion callback probably added things to the hardware
2136 * schedule.
2137 *
2138 * Note that we carefully avoid recycling this descriptor until after any
2139 * completion callback runs, so that it won't be reused quickly. That is,
2140 * assuming (a) no more than two urbs per frame on this endpoint, and also
2141 * (b) only this endpoint's completions submit URBs. It seems some silicon
2142 * corrupts things if you reuse completed descriptors very quickly...
2143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144static unsigned
2145sitd_complete (
2146 struct ehci_hcd *ehci,
David Howells7d12e782006-10-05 14:55:46 +01002147 struct ehci_sitd *sitd
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148) {
2149 struct urb *urb = sitd->urb;
2150 struct usb_iso_packet_descriptor *desc;
2151 u32 t;
2152 int urb_index = -1;
2153 struct ehci_iso_stream *stream = sitd->stream;
2154 struct usb_device *dev;
David Brownell30bf54e2007-12-16 22:37:40 -08002155 unsigned retval = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 urb_index = sitd->index;
2158 desc = &urb->iso_frame_desc [urb_index];
Stefan Roese6dbd6822007-05-01 09:29:37 -07002159 t = hc32_to_cpup(ehci, &sitd->hw_results);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 /* report transfer status */
2162 if (t & SITD_ERRS) {
2163 urb->error_count++;
2164 if (t & SITD_STS_DBE)
2165 desc->status = usb_pipein (urb->pipe)
2166 ? -ENOSR /* hc couldn't read */
2167 : -ECOMM; /* hc couldn't write */
2168 else if (t & SITD_STS_BABBLE)
2169 desc->status = -EOVERFLOW;
2170 else /* XACT, MMF, etc */
2171 desc->status = -EPROTO;
2172 } else {
2173 desc->status = 0;
Alan Sternec6d67e2009-06-29 14:34:59 -04002174 desc->actual_length = desc->length - SITD_LENGTH(t);
2175 urb->actual_length += desc->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 /* handle completion now? */
2179 if ((urb_index + 1) != urb->number_of_packets)
David Brownell30bf54e2007-12-16 22:37:40 -08002180 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* ASSERT: it's really the last sitd for this urb
2183 list_for_each_entry (sitd, &stream->td_list, sitd_list)
2184 BUG_ON (sitd->urb == urb);
2185 */
2186
David Brownellaa16ca32007-12-30 23:45:19 -08002187 /* give urb back to the driver; completion often (re)submits */
Alan Stern6a8e87b2006-01-19 10:46:27 -05002188 dev = urb->dev;
Alan Stern14c04c02007-08-24 15:40:19 -04002189 ehci_urb_done(ehci, urb, 0);
David Brownell30bf54e2007-12-16 22:37:40 -08002190 retval = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 urb = NULL;
David Brownell01c17142008-08-26 23:35:04 -07002192 (void) disable_periodic(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2194
Alex He05570292010-12-07 10:10:08 +08002195 if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
2196 if (ehci->amd_l1_fix == 1)
2197 ehci_quirk_amd_L1(ehci, 0);
2198 }
2199
Karsten Wiese508db8c2009-02-26 01:47:48 +01002200 if (list_is_singular(&stream->td_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 ehci_to_hcd(ehci)->self.bandwidth_allocated
2202 -= stream->bandwidth;
2203 ehci_vdbg (ehci,
2204 "deschedule devp %s ep%d%s-iso\n",
2205 dev->devpath, stream->bEndpointAddress & 0x0f,
2206 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
2207 }
2208 iso_stream_put (ehci, stream);
Alan Stern0e5f2312010-04-08 16:56:37 -04002209
David Brownell30bf54e2007-12-16 22:37:40 -08002210done:
David Brownell30bf54e2007-12-16 22:37:40 -08002211 sitd->urb = NULL;
Alan Stern0e5f2312010-04-08 16:56:37 -04002212 if (ehci->clock_frame != sitd->frame) {
2213 /* OK to recycle this SITD now. */
2214 sitd->stream = NULL;
2215 list_move(&sitd->sitd_list, &stream->free_list);
2216 iso_stream_put(ehci, stream);
2217 } else {
2218 /* HW might remember this SITD, so we can't recycle it yet.
2219 * Move it to a safe place until a new frame starts.
2220 */
2221 list_move(&sitd->sitd_list, &ehci->cached_sitd_list);
2222 if (stream->refcount == 2) {
2223 /* If iso_stream_put() were called here, stream
2224 * would be freed. Instead, just prevent reuse.
2225 */
2226 stream->ep->hcpriv = NULL;
2227 stream->ep = NULL;
2228 }
2229 }
David Brownell30bf54e2007-12-16 22:37:40 -08002230 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233
Olav Kongas5db539e2005-06-23 20:25:36 +03002234static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04002235 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 int status = -EINVAL;
2238 unsigned long flags;
2239 struct ehci_iso_stream *stream;
2240
2241 /* Get iso_stream head */
2242 stream = iso_stream_find (ehci, urb);
2243 if (stream == NULL) {
2244 ehci_dbg (ehci, "can't get iso stream\n");
2245 return -ENOMEM;
2246 }
2247 if (urb->interval != stream->interval) {
2248 ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
2249 stream->interval, urb->interval);
2250 goto done;
2251 }
2252
2253#ifdef EHCI_URB_TRACE
2254 ehci_dbg (ehci,
2255 "submit %p dev%s ep%d%s-iso len %d\n",
2256 urb, urb->dev->devpath,
2257 usb_pipeendpoint (urb->pipe),
2258 usb_pipein (urb->pipe) ? "in" : "out",
2259 urb->transfer_buffer_length);
2260#endif
2261
2262 /* allocate SITDs */
2263 status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
2264 if (status < 0) {
2265 ehci_dbg (ehci, "can't init sitds\n");
2266 goto done;
2267 }
2268
2269 /* schedule ... need to lock */
2270 spin_lock_irqsave (&ehci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04002271 if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002272 status = -ESHUTDOWN;
Alan Sterne9df41c2007-08-08 11:48:02 -04002273 goto done_not_linked;
2274 }
2275 status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
2276 if (unlikely(status))
2277 goto done_not_linked;
2278 status = iso_stream_schedule(ehci, urb, stream);
David Brownell53bd6a62006-08-30 14:50:06 -07002279 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
Alan Sterne9df41c2007-08-08 11:48:02 -04002281 else
2282 usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2283done_not_linked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 spin_unlock_irqrestore (&ehci->lock, flags);
2285
2286done:
2287 if (status < 0)
2288 iso_stream_put (ehci, stream);
2289 return status;
2290}
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292/*-------------------------------------------------------------------------*/
2293
Alan Stern0e5f2312010-04-08 16:56:37 -04002294static void free_cached_lists(struct ehci_hcd *ehci)
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002295{
2296 struct ehci_itd *itd, *n;
Alan Stern0e5f2312010-04-08 16:56:37 -04002297 struct ehci_sitd *sitd, *sn;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002298
2299 list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
2300 struct ehci_iso_stream *stream = itd->stream;
2301 itd->stream = NULL;
2302 list_move(&itd->itd_list, &stream->free_list);
2303 iso_stream_put(ehci, stream);
2304 }
Alan Stern0e5f2312010-04-08 16:56:37 -04002305
2306 list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
2307 struct ehci_iso_stream *stream = sitd->stream;
2308 sitd->stream = NULL;
2309 list_move(&sitd->sitd_list, &stream->free_list);
2310 iso_stream_put(ehci, stream);
2311 }
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002312}
2313
2314/*-------------------------------------------------------------------------*/
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static void
David Howells7d12e782006-10-05 14:55:46 +01002317scan_periodic (struct ehci_hcd *ehci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Alan Sternb40e43f2008-05-20 16:59:10 -04002319 unsigned now_uframe, frame, clock, clock_frame, mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 unsigned modified;
2321
2322 mod = ehci->periodic_size << 3;
2323
2324 /*
2325 * When running, scan from last scan point up to "now"
2326 * else clean up by scanning everything that's left.
2327 * Touches as few pages as possible: cache-friendly.
2328 */
2329 now_uframe = ehci->next_uframe;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002330 if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11002331 clock = ehci_readl(ehci, &ehci->regs->frame_index);
Alan Sternbccbefa2010-07-14 11:03:36 -04002332 clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002333 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 clock = now_uframe + mod - 1;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002335 clock_frame = -1;
2336 }
2337 if (ehci->clock_frame != clock_frame) {
Alan Stern0e5f2312010-04-08 16:56:37 -04002338 free_cached_lists(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002339 ehci->clock_frame = clock_frame;
2340 }
Alan Sternbccbefa2010-07-14 11:03:36 -04002341 clock &= mod - 1;
Alan Sternb40e43f2008-05-20 16:59:10 -04002342 clock_frame = clock >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 for (;;) {
2345 union ehci_shadow q, *q_p;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002346 __hc32 type, *hw_p;
David Brownell79592b722008-01-07 00:47:42 -08002347 unsigned incomplete = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 frame = now_uframe >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351restart:
2352 /* scan each element in frame's queue for completions */
2353 q_p = &ehci->pshadow [frame];
2354 hw_p = &ehci->periodic [frame];
2355 q.ptr = q_p->ptr;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002356 type = Q_NEXT_TYPE(ehci, *hw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 modified = 0;
2358
2359 while (q.ptr != NULL) {
2360 unsigned uf;
2361 union ehci_shadow temp;
2362 int live;
2363
2364 live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
Stefan Roese6dbd6822007-05-01 09:29:37 -07002365 switch (hc32_to_cpu(ehci, type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 case Q_TYPE_QH:
2367 /* handle any completions */
2368 temp.qh = qh_get (q.qh);
Alek Du3807e262009-07-14 07:23:29 +08002369 type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 q = q.qh->qh_next;
David Howells7d12e782006-10-05 14:55:46 +01002371 modified = qh_completions (ehci, temp.qh);
Alan Sterna448c9d2009-08-19 12:22:44 -04002372 if (unlikely(list_empty(&temp.qh->qtd_list) ||
2373 temp.qh->needs_rescan))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 intr_deschedule (ehci, temp.qh);
2375 qh_put (temp.qh);
2376 break;
2377 case Q_TYPE_FSTN:
2378 /* for "save place" FSTNs, look at QH entries
2379 * in the previous frame for completions.
2380 */
Stefan Roese6dbd6822007-05-01 09:29:37 -07002381 if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 dbg ("ignoring completions from FSTNs");
2383 }
Stefan Roese6dbd6822007-05-01 09:29:37 -07002384 type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 q = q.fstn->fstn_next;
2386 break;
2387 case Q_TYPE_ITD:
David Brownell79592b722008-01-07 00:47:42 -08002388 /* If this ITD is still active, leave it for
2389 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002390 * No need to check for activity unless the
2391 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002392 */
Alan Sternb40e43f2008-05-20 16:59:10 -04002393 if (frame == clock_frame && live) {
2394 rmb();
2395 for (uf = 0; uf < 8; uf++) {
2396 if (q.itd->hw_transaction[uf] &
2397 ITD_ACTIVE(ehci))
2398 break;
2399 }
2400 if (uf < 8) {
2401 incomplete = true;
2402 q_p = &q.itd->itd_next;
2403 hw_p = &q.itd->hw_next;
2404 type = Q_NEXT_TYPE(ehci,
Stefan Roese6dbd6822007-05-01 09:29:37 -07002405 q.itd->hw_next);
Alan Sternb40e43f2008-05-20 16:59:10 -04002406 q = *q_p;
2407 break;
2408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
David Brownell79592b722008-01-07 00:47:42 -08002411 /* Take finished ITDs out of the schedule
2412 * and process them: recycle, maybe report
2413 * URB completion. HC won't cache the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * pointer for much longer, if at all.
2415 */
2416 *q_p = q.itd->itd_next;
2417 *hw_p = q.itd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002418 type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002420 modified = itd_complete (ehci, q.itd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 q = *q_p;
2422 break;
2423 case Q_TYPE_SITD:
David Brownell79592b722008-01-07 00:47:42 -08002424 /* If this SITD is still active, leave it for
2425 * later processing ... check the next entry.
Alan Sternb40e43f2008-05-20 16:59:10 -04002426 * No need to check for activity unless the
2427 * frame is current.
David Brownell79592b722008-01-07 00:47:42 -08002428 */
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002429 if (((frame == clock_frame) ||
Alan Sternbccbefa2010-07-14 11:03:36 -04002430 (((frame + 1) & (ehci->periodic_size - 1))
Dmitri Epshtein22e18692009-12-14 17:17:34 +02002431 == clock_frame))
2432 && live
2433 && (q.sitd->hw_results &
2434 SITD_ACTIVE(ehci))) {
2435
David Brownell79592b722008-01-07 00:47:42 -08002436 incomplete = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 q_p = &q.sitd->sitd_next;
2438 hw_p = &q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002439 type = Q_NEXT_TYPE(ehci,
2440 q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 q = *q_p;
2442 break;
2443 }
David Brownell79592b722008-01-07 00:47:42 -08002444
2445 /* Take finished SITDs out of the schedule
2446 * and process them: recycle, maybe report
2447 * URB completion.
2448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 *q_p = q.sitd->sitd_next;
2450 *hw_p = q.sitd->hw_next;
Stefan Roese6dbd6822007-05-01 09:29:37 -07002451 type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 wmb();
David Howells7d12e782006-10-05 14:55:46 +01002453 modified = sitd_complete (ehci, q.sitd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 q = *q_p;
2455 break;
2456 default:
2457 dbg ("corrupt type %d frame %d shadow %p",
2458 type, frame, q.ptr);
2459 // BUG ();
2460 q.ptr = NULL;
2461 }
2462
2463 /* assume completion callbacks modify the queue */
David Brownellaa16ca32007-12-30 23:45:19 -08002464 if (unlikely (modified)) {
2465 if (likely(ehci->periodic_sched > 0))
2466 goto restart;
David Brownell01c17142008-08-26 23:35:04 -07002467 /* short-circuit this scan */
David Brownellaa16ca32007-12-30 23:45:19 -08002468 now_uframe = clock;
2469 break;
2470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472
David Brownell79592b722008-01-07 00:47:42 -08002473 /* If we can tell we caught up to the hardware, stop now.
2474 * We can't advance our scan without collecting the ISO
2475 * transfers that are still pending in this frame.
2476 */
2477 if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
2478 ehci->next_uframe = now_uframe;
2479 break;
2480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 // FIXME: this assumes we won't get lapped when
2483 // latencies climb; that should be rare, but...
2484 // detect it, and just go all the way around.
2485 // FLR might help detect this case, so long as latencies
2486 // don't exceed periodic_size msec (default 1.024 sec).
2487
2488 // FIXME: likewise assumes HC doesn't halt mid-scan
2489
2490 if (now_uframe == clock) {
2491 unsigned now;
2492
David Brownellaa16ca32007-12-30 23:45:19 -08002493 if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
2494 || ehci->periodic_sched == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 break;
2496 ehci->next_uframe = now_uframe;
Alan Sternbccbefa2010-07-14 11:03:36 -04002497 now = ehci_readl(ehci, &ehci->regs->frame_index) &
2498 (mod - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (now_uframe == now)
2500 break;
2501
2502 /* rescan the rest of this frame, then ... */
2503 clock = now;
Alan Sternb40e43f2008-05-20 16:59:10 -04002504 clock_frame = clock >> 3;
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002505 if (ehci->clock_frame != clock_frame) {
Alan Stern0e5f2312010-04-08 16:56:37 -04002506 free_cached_lists(ehci);
Karsten Wiese9aa09d22009-02-08 16:07:58 -08002507 ehci->clock_frame = clock_frame;
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 } else {
2510 now_uframe++;
Alan Sternbccbefa2010-07-14 11:03:36 -04002511 now_uframe &= mod - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
David Brownell53bd6a62006-08-30 14:50:06 -07002513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}