ehci-sched.c 50 KB

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