ehci-sched.c 64 KB

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