ehci-sched.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  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 > 100)
  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. /*
  625. * 80% periodic == 100 usec/uframe available
  626. * convert "usecs we need" to "max already claimed"
  627. */
  628. usecs = 100 - usecs;
  629. /* we "know" 2 and 4 uframe intervals were rejected; so
  630. * for period 0, check _every_ microframe in the schedule.
  631. */
  632. if (unlikely (period == 0)) {
  633. do {
  634. for (uframe = 0; uframe < 7; uframe++) {
  635. claimed = periodic_usecs (ehci, frame, uframe);
  636. if (claimed > usecs)
  637. return 0;
  638. }
  639. } while ((frame += 1) < ehci->periodic_size);
  640. /* just check the specified uframe, at that period */
  641. } else {
  642. do {
  643. claimed = periodic_usecs (ehci, frame, uframe);
  644. if (claimed > usecs)
  645. return 0;
  646. } while ((frame += period) < ehci->periodic_size);
  647. }
  648. // success!
  649. return 1;
  650. }
  651. static int check_intr_schedule (
  652. struct ehci_hcd *ehci,
  653. unsigned frame,
  654. unsigned uframe,
  655. const struct ehci_qh *qh,
  656. __hc32 *c_maskp
  657. )
  658. {
  659. int retval = -ENOSPC;
  660. u8 mask = 0;
  661. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  662. goto done;
  663. if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
  664. goto done;
  665. if (!qh->c_usecs) {
  666. retval = 0;
  667. *c_maskp = 0;
  668. goto done;
  669. }
  670. #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
  671. if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
  672. qh->tt_usecs)) {
  673. unsigned i;
  674. /* TODO : this may need FSTN for SSPLIT in uframe 5. */
  675. for (i=uframe+1; i<8 && i<uframe+4; i++)
  676. if (!check_period (ehci, frame, i,
  677. qh->period, qh->c_usecs))
  678. goto done;
  679. else
  680. mask |= 1 << i;
  681. retval = 0;
  682. *c_maskp = cpu_to_hc32(ehci, mask << 8);
  683. }
  684. #else
  685. /* Make sure this tt's buffer is also available for CSPLITs.
  686. * We pessimize a bit; probably the typical full speed case
  687. * doesn't need the second CSPLIT.
  688. *
  689. * NOTE: both SPLIT and CSPLIT could be checked in just
  690. * one smart pass...
  691. */
  692. mask = 0x03 << (uframe + qh->gap_uf);
  693. *c_maskp = cpu_to_hc32(ehci, mask << 8);
  694. mask |= 1 << uframe;
  695. if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
  696. if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
  697. qh->period, qh->c_usecs))
  698. goto done;
  699. if (!check_period (ehci, frame, uframe + qh->gap_uf,
  700. qh->period, qh->c_usecs))
  701. goto done;
  702. retval = 0;
  703. }
  704. #endif
  705. done:
  706. return retval;
  707. }
  708. /* "first fit" scheduling policy used the first time through,
  709. * or when the previous schedule slot can't be re-used.
  710. */
  711. static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
  712. {
  713. int status;
  714. unsigned uframe;
  715. __hc32 c_mask;
  716. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  717. struct ehci_qh_hw *hw = qh->hw;
  718. qh_refresh(ehci, qh);
  719. hw->hw_next = EHCI_LIST_END(ehci);
  720. frame = qh->start;
  721. /* reuse the previous schedule slots, if we can */
  722. if (frame < qh->period) {
  723. uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
  724. status = check_intr_schedule (ehci, frame, --uframe,
  725. qh, &c_mask);
  726. } else {
  727. uframe = 0;
  728. c_mask = 0;
  729. status = -ENOSPC;
  730. }
  731. /* else scan the schedule to find a group of slots such that all
  732. * uframes have enough periodic bandwidth available.
  733. */
  734. if (status) {
  735. /* "normal" case, uframing flexible except with splits */
  736. if (qh->period) {
  737. int i;
  738. for (i = qh->period; status && i > 0; --i) {
  739. frame = ++ehci->random_frame % qh->period;
  740. for (uframe = 0; uframe < 8; uframe++) {
  741. status = check_intr_schedule (ehci,
  742. frame, uframe, qh,
  743. &c_mask);
  744. if (status == 0)
  745. break;
  746. }
  747. }
  748. /* qh->period == 0 means every uframe */
  749. } else {
  750. frame = 0;
  751. status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
  752. }
  753. if (status)
  754. goto done;
  755. qh->start = frame;
  756. /* reset S-frame and (maybe) C-frame masks */
  757. hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
  758. hw->hw_info2 |= qh->period
  759. ? cpu_to_hc32(ehci, 1 << uframe)
  760. : cpu_to_hc32(ehci, QH_SMASK);
  761. hw->hw_info2 |= c_mask;
  762. } else
  763. ehci_dbg (ehci, "reused qh %p schedule\n", qh);
  764. /* stuff into the periodic schedule */
  765. status = qh_link_periodic (ehci, qh);
  766. done:
  767. return status;
  768. }
  769. static int intr_submit (
  770. struct ehci_hcd *ehci,
  771. struct urb *urb,
  772. struct list_head *qtd_list,
  773. gfp_t mem_flags
  774. ) {
  775. unsigned epnum;
  776. unsigned long flags;
  777. struct ehci_qh *qh;
  778. int status;
  779. struct list_head empty;
  780. /* get endpoint and transfer/schedule data */
  781. epnum = urb->ep->desc.bEndpointAddress;
  782. spin_lock_irqsave (&ehci->lock, flags);
  783. if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
  784. status = -ESHUTDOWN;
  785. goto done_not_linked;
  786. }
  787. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  788. if (unlikely(status))
  789. goto done_not_linked;
  790. /* get qh and force any scheduling errors */
  791. INIT_LIST_HEAD (&empty);
  792. qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
  793. if (qh == NULL) {
  794. status = -ENOMEM;
  795. goto done;
  796. }
  797. if (qh->qh_state == QH_STATE_IDLE) {
  798. if ((status = qh_schedule (ehci, qh)) != 0)
  799. goto done;
  800. }
  801. /* then queue the urb's tds to the qh */
  802. qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
  803. BUG_ON (qh == NULL);
  804. /* ... update usbfs periodic stats */
  805. ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
  806. done:
  807. if (unlikely(status))
  808. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  809. done_not_linked:
  810. spin_unlock_irqrestore (&ehci->lock, flags);
  811. if (status)
  812. qtd_list_free (ehci, urb, qtd_list);
  813. return status;
  814. }
  815. /*-------------------------------------------------------------------------*/
  816. /* ehci_iso_stream ops work with both ITD and SITD */
  817. static struct ehci_iso_stream *
  818. iso_stream_alloc (gfp_t mem_flags)
  819. {
  820. struct ehci_iso_stream *stream;
  821. stream = kzalloc(sizeof *stream, mem_flags);
  822. if (likely (stream != NULL)) {
  823. INIT_LIST_HEAD(&stream->td_list);
  824. INIT_LIST_HEAD(&stream->free_list);
  825. stream->next_uframe = -1;
  826. stream->refcount = 1;
  827. }
  828. return stream;
  829. }
  830. static void
  831. iso_stream_init (
  832. struct ehci_hcd *ehci,
  833. struct ehci_iso_stream *stream,
  834. struct usb_device *dev,
  835. int pipe,
  836. unsigned interval
  837. )
  838. {
  839. static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
  840. u32 buf1;
  841. unsigned epnum, maxp;
  842. int is_input;
  843. long bandwidth;
  844. /*
  845. * this might be a "high bandwidth" highspeed endpoint,
  846. * as encoded in the ep descriptor's wMaxPacket field
  847. */
  848. epnum = usb_pipeendpoint (pipe);
  849. is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
  850. maxp = usb_maxpacket(dev, pipe, !is_input);
  851. if (is_input) {
  852. buf1 = (1 << 11);
  853. } else {
  854. buf1 = 0;
  855. }
  856. /* knows about ITD vs SITD */
  857. if (dev->speed == USB_SPEED_HIGH) {
  858. unsigned multi = hb_mult(maxp);
  859. stream->highspeed = 1;
  860. maxp = max_packet(maxp);
  861. buf1 |= maxp;
  862. maxp *= multi;
  863. stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
  864. stream->buf1 = cpu_to_hc32(ehci, buf1);
  865. stream->buf2 = cpu_to_hc32(ehci, multi);
  866. /* usbfs wants to report the average usecs per frame tied up
  867. * when transfers on this endpoint are scheduled ...
  868. */
  869. stream->usecs = HS_USECS_ISO (maxp);
  870. bandwidth = stream->usecs * 8;
  871. bandwidth /= interval;
  872. } else {
  873. u32 addr;
  874. int think_time;
  875. int hs_transfers;
  876. addr = dev->ttport << 24;
  877. if (!ehci_is_TDI(ehci)
  878. || (dev->tt->hub !=
  879. ehci_to_hcd(ehci)->self.root_hub))
  880. addr |= dev->tt->hub->devnum << 16;
  881. addr |= epnum << 8;
  882. addr |= dev->devnum;
  883. stream->usecs = HS_USECS_ISO (maxp);
  884. think_time = dev->tt ? dev->tt->think_time : 0;
  885. stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
  886. dev->speed, is_input, 1, maxp));
  887. hs_transfers = max (1u, (maxp + 187) / 188);
  888. if (is_input) {
  889. u32 tmp;
  890. addr |= 1 << 31;
  891. stream->c_usecs = stream->usecs;
  892. stream->usecs = HS_USECS_ISO (1);
  893. stream->raw_mask = 1;
  894. /* c-mask as specified in USB 2.0 11.18.4 3.c */
  895. tmp = (1 << (hs_transfers + 2)) - 1;
  896. stream->raw_mask |= tmp << (8 + 2);
  897. } else
  898. stream->raw_mask = smask_out [hs_transfers - 1];
  899. bandwidth = stream->usecs + stream->c_usecs;
  900. bandwidth /= interval << 3;
  901. /* stream->splits gets created from raw_mask later */
  902. stream->address = cpu_to_hc32(ehci, addr);
  903. }
  904. stream->bandwidth = bandwidth;
  905. stream->udev = dev;
  906. stream->bEndpointAddress = is_input | epnum;
  907. stream->interval = interval;
  908. stream->maxp = maxp;
  909. }
  910. static void
  911. iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
  912. {
  913. stream->refcount--;
  914. /* free whenever just a dev->ep reference remains.
  915. * not like a QH -- no persistent state (toggle, halt)
  916. */
  917. if (stream->refcount == 1) {
  918. // BUG_ON (!list_empty(&stream->td_list));
  919. while (!list_empty (&stream->free_list)) {
  920. struct list_head *entry;
  921. entry = stream->free_list.next;
  922. list_del (entry);
  923. /* knows about ITD vs SITD */
  924. if (stream->highspeed) {
  925. struct ehci_itd *itd;
  926. itd = list_entry (entry, struct ehci_itd,
  927. itd_list);
  928. dma_pool_free (ehci->itd_pool, itd,
  929. itd->itd_dma);
  930. } else {
  931. struct ehci_sitd *sitd;
  932. sitd = list_entry (entry, struct ehci_sitd,
  933. sitd_list);
  934. dma_pool_free (ehci->sitd_pool, sitd,
  935. sitd->sitd_dma);
  936. }
  937. }
  938. stream->bEndpointAddress &= 0x0f;
  939. if (stream->ep)
  940. stream->ep->hcpriv = NULL;
  941. kfree(stream);
  942. }
  943. }
  944. static inline struct ehci_iso_stream *
  945. iso_stream_get (struct ehci_iso_stream *stream)
  946. {
  947. if (likely (stream != NULL))
  948. stream->refcount++;
  949. return stream;
  950. }
  951. static struct ehci_iso_stream *
  952. iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
  953. {
  954. unsigned epnum;
  955. struct ehci_iso_stream *stream;
  956. struct usb_host_endpoint *ep;
  957. unsigned long flags;
  958. epnum = usb_pipeendpoint (urb->pipe);
  959. if (usb_pipein(urb->pipe))
  960. ep = urb->dev->ep_in[epnum];
  961. else
  962. ep = urb->dev->ep_out[epnum];
  963. spin_lock_irqsave (&ehci->lock, flags);
  964. stream = ep->hcpriv;
  965. if (unlikely (stream == NULL)) {
  966. stream = iso_stream_alloc(GFP_ATOMIC);
  967. if (likely (stream != NULL)) {
  968. /* dev->ep owns the initial refcount */
  969. ep->hcpriv = stream;
  970. stream->ep = ep;
  971. iso_stream_init(ehci, stream, urb->dev, urb->pipe,
  972. urb->interval);
  973. }
  974. /* if dev->ep [epnum] is a QH, hw is set */
  975. } else if (unlikely (stream->hw != NULL)) {
  976. ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
  977. urb->dev->devpath, epnum,
  978. usb_pipein(urb->pipe) ? "in" : "out");
  979. stream = NULL;
  980. }
  981. /* caller guarantees an eventual matching iso_stream_put */
  982. stream = iso_stream_get (stream);
  983. spin_unlock_irqrestore (&ehci->lock, flags);
  984. return stream;
  985. }
  986. /*-------------------------------------------------------------------------*/
  987. /* ehci_iso_sched ops can be ITD-only or SITD-only */
  988. static struct ehci_iso_sched *
  989. iso_sched_alloc (unsigned packets, gfp_t mem_flags)
  990. {
  991. struct ehci_iso_sched *iso_sched;
  992. int size = sizeof *iso_sched;
  993. size += packets * sizeof (struct ehci_iso_packet);
  994. iso_sched = kzalloc(size, mem_flags);
  995. if (likely (iso_sched != NULL)) {
  996. INIT_LIST_HEAD (&iso_sched->td_list);
  997. }
  998. return iso_sched;
  999. }
  1000. static inline void
  1001. itd_sched_init(
  1002. struct ehci_hcd *ehci,
  1003. struct ehci_iso_sched *iso_sched,
  1004. struct ehci_iso_stream *stream,
  1005. struct urb *urb
  1006. )
  1007. {
  1008. unsigned i;
  1009. dma_addr_t dma = urb->transfer_dma;
  1010. /* how many uframes are needed for these transfers */
  1011. iso_sched->span = urb->number_of_packets * stream->interval;
  1012. /* figure out per-uframe itd fields that we'll need later
  1013. * when we fit new itds into the schedule.
  1014. */
  1015. for (i = 0; i < urb->number_of_packets; i++) {
  1016. struct ehci_iso_packet *uframe = &iso_sched->packet [i];
  1017. unsigned length;
  1018. dma_addr_t buf;
  1019. u32 trans;
  1020. length = urb->iso_frame_desc [i].length;
  1021. buf = dma + urb->iso_frame_desc [i].offset;
  1022. trans = EHCI_ISOC_ACTIVE;
  1023. trans |= buf & 0x0fff;
  1024. if (unlikely (((i + 1) == urb->number_of_packets))
  1025. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  1026. trans |= EHCI_ITD_IOC;
  1027. trans |= length << 16;
  1028. uframe->transaction = cpu_to_hc32(ehci, trans);
  1029. /* might need to cross a buffer page within a uframe */
  1030. uframe->bufp = (buf & ~(u64)0x0fff);
  1031. buf += length;
  1032. if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
  1033. uframe->cross = 1;
  1034. }
  1035. }
  1036. static void
  1037. iso_sched_free (
  1038. struct ehci_iso_stream *stream,
  1039. struct ehci_iso_sched *iso_sched
  1040. )
  1041. {
  1042. if (!iso_sched)
  1043. return;
  1044. // caller must hold ehci->lock!
  1045. list_splice (&iso_sched->td_list, &stream->free_list);
  1046. kfree (iso_sched);
  1047. }
  1048. static int
  1049. itd_urb_transaction (
  1050. struct ehci_iso_stream *stream,
  1051. struct ehci_hcd *ehci,
  1052. struct urb *urb,
  1053. gfp_t mem_flags
  1054. )
  1055. {
  1056. struct ehci_itd *itd;
  1057. dma_addr_t itd_dma;
  1058. int i;
  1059. unsigned num_itds;
  1060. struct ehci_iso_sched *sched;
  1061. unsigned long flags;
  1062. sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  1063. if (unlikely (sched == NULL))
  1064. return -ENOMEM;
  1065. itd_sched_init(ehci, sched, stream, urb);
  1066. if (urb->interval < 8)
  1067. num_itds = 1 + (sched->span + 7) / 8;
  1068. else
  1069. num_itds = urb->number_of_packets;
  1070. /* allocate/init ITDs */
  1071. spin_lock_irqsave (&ehci->lock, flags);
  1072. for (i = 0; i < num_itds; i++) {
  1073. /* free_list.next might be cache-hot ... but maybe
  1074. * the HC caches it too. avoid that issue for now.
  1075. */
  1076. /* prefer previously-allocated itds */
  1077. if (likely (!list_empty(&stream->free_list))) {
  1078. itd = list_entry (stream->free_list.prev,
  1079. struct ehci_itd, itd_list);
  1080. list_del (&itd->itd_list);
  1081. itd_dma = itd->itd_dma;
  1082. } else {
  1083. spin_unlock_irqrestore (&ehci->lock, flags);
  1084. itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
  1085. &itd_dma);
  1086. spin_lock_irqsave (&ehci->lock, flags);
  1087. if (!itd) {
  1088. iso_sched_free(stream, sched);
  1089. spin_unlock_irqrestore(&ehci->lock, flags);
  1090. return -ENOMEM;
  1091. }
  1092. }
  1093. memset (itd, 0, sizeof *itd);
  1094. itd->itd_dma = itd_dma;
  1095. list_add (&itd->itd_list, &sched->td_list);
  1096. }
  1097. spin_unlock_irqrestore (&ehci->lock, flags);
  1098. /* temporarily store schedule info in hcpriv */
  1099. urb->hcpriv = sched;
  1100. urb->error_count = 0;
  1101. return 0;
  1102. }
  1103. /*-------------------------------------------------------------------------*/
  1104. static inline int
  1105. itd_slot_ok (
  1106. struct ehci_hcd *ehci,
  1107. u32 mod,
  1108. u32 uframe,
  1109. u8 usecs,
  1110. u32 period
  1111. )
  1112. {
  1113. uframe %= period;
  1114. do {
  1115. /* can't commit more than 80% periodic == 100 usec */
  1116. if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
  1117. > (100 - usecs))
  1118. return 0;
  1119. /* we know urb->interval is 2^N uframes */
  1120. uframe += period;
  1121. } while (uframe < mod);
  1122. return 1;
  1123. }
  1124. static inline int
  1125. sitd_slot_ok (
  1126. struct ehci_hcd *ehci,
  1127. u32 mod,
  1128. struct ehci_iso_stream *stream,
  1129. u32 uframe,
  1130. struct ehci_iso_sched *sched,
  1131. u32 period_uframes
  1132. )
  1133. {
  1134. u32 mask, tmp;
  1135. u32 frame, uf;
  1136. mask = stream->raw_mask << (uframe & 7);
  1137. /* for IN, don't wrap CSPLIT into the next frame */
  1138. if (mask & ~0xffff)
  1139. return 0;
  1140. /* this multi-pass logic is simple, but performance may
  1141. * suffer when the schedule data isn't cached.
  1142. */
  1143. /* check bandwidth */
  1144. uframe %= period_uframes;
  1145. do {
  1146. u32 max_used;
  1147. frame = uframe >> 3;
  1148. uf = uframe & 7;
  1149. #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
  1150. /* The tt's fullspeed bus bandwidth must be available.
  1151. * tt_available scheduling guarantees 10+% for control/bulk.
  1152. */
  1153. if (!tt_available (ehci, period_uframes << 3,
  1154. stream->udev, frame, uf, stream->tt_usecs))
  1155. return 0;
  1156. #else
  1157. /* tt must be idle for start(s), any gap, and csplit.
  1158. * assume scheduling slop leaves 10+% for control/bulk.
  1159. */
  1160. if (!tt_no_collision (ehci, period_uframes << 3,
  1161. stream->udev, frame, mask))
  1162. return 0;
  1163. #endif
  1164. /* check starts (OUT uses more than one) */
  1165. max_used = 100 - stream->usecs;
  1166. for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
  1167. if (periodic_usecs (ehci, frame, uf) > max_used)
  1168. return 0;
  1169. }
  1170. /* for IN, check CSPLIT */
  1171. if (stream->c_usecs) {
  1172. uf = uframe & 7;
  1173. max_used = 100 - stream->c_usecs;
  1174. do {
  1175. tmp = 1 << uf;
  1176. tmp <<= 8;
  1177. if ((stream->raw_mask & tmp) == 0)
  1178. continue;
  1179. if (periodic_usecs (ehci, frame, uf)
  1180. > max_used)
  1181. return 0;
  1182. } while (++uf < 8);
  1183. }
  1184. /* we know urb->interval is 2^N uframes */
  1185. uframe += period_uframes;
  1186. } while (uframe < mod);
  1187. stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
  1188. return 1;
  1189. }
  1190. /*
  1191. * This scheduler plans almost as far into the future as it has actual
  1192. * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  1193. * "as small as possible" to be cache-friendlier.) That limits the size
  1194. * transfers you can stream reliably; avoid more than 64 msec per urb.
  1195. * Also avoid queue depths of less than ehci's worst irq latency (affected
  1196. * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  1197. * and other factors); or more than about 230 msec total (for portability,
  1198. * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
  1199. */
  1200. #define SCHEDULE_SLOP 80 /* microframes */
  1201. static int
  1202. iso_stream_schedule (
  1203. struct ehci_hcd *ehci,
  1204. struct urb *urb,
  1205. struct ehci_iso_stream *stream
  1206. )
  1207. {
  1208. u32 now, next, start, period, span;
  1209. int status;
  1210. unsigned mod = ehci->periodic_size << 3;
  1211. struct ehci_iso_sched *sched = urb->hcpriv;
  1212. period = urb->interval;
  1213. span = sched->span;
  1214. if (!stream->highspeed) {
  1215. period <<= 3;
  1216. span <<= 3;
  1217. }
  1218. if (span > mod - SCHEDULE_SLOP) {
  1219. ehci_dbg (ehci, "iso request %p too long\n", urb);
  1220. status = -EFBIG;
  1221. goto fail;
  1222. }
  1223. now = ehci_readl(ehci, &ehci->regs->frame_index) & (mod - 1);
  1224. /* Typical case: reuse current schedule, stream is still active.
  1225. * Hopefully there are no gaps from the host falling behind
  1226. * (irq delays etc), but if there are we'll take the next
  1227. * slot in the schedule, implicitly assuming URB_ISO_ASAP.
  1228. */
  1229. if (likely (!list_empty (&stream->td_list))) {
  1230. u32 excess;
  1231. /* For high speed devices, allow scheduling within the
  1232. * isochronous scheduling threshold. For full speed devices
  1233. * and Intel PCI-based controllers, don't (work around for
  1234. * Intel ICH9 bug).
  1235. */
  1236. if (!stream->highspeed && ehci->fs_i_thresh)
  1237. next = now + ehci->i_thresh;
  1238. else
  1239. next = now;
  1240. /* Fell behind (by up to twice the slop amount)?
  1241. * We decide based on the time of the last currently-scheduled
  1242. * slot, not the time of the next available slot.
  1243. */
  1244. excess = (stream->next_uframe - period - next) & (mod - 1);
  1245. if (excess >= mod - 2 * SCHEDULE_SLOP)
  1246. start = next + excess - mod + period *
  1247. DIV_ROUND_UP(mod - excess, period);
  1248. else
  1249. start = next + excess + period;
  1250. if (start - now >= mod) {
  1251. ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
  1252. urb, start - now - period, period,
  1253. mod);
  1254. status = -EFBIG;
  1255. goto fail;
  1256. }
  1257. }
  1258. /* need to schedule; when's the next (u)frame we could start?
  1259. * this is bigger than ehci->i_thresh allows; scheduling itself
  1260. * isn't free, the slop should handle reasonably slow cpus. it
  1261. * can also help high bandwidth if the dma and irq loads don't
  1262. * jump until after the queue is primed.
  1263. */
  1264. else {
  1265. start = SCHEDULE_SLOP + (now & ~0x07);
  1266. /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  1267. /* find a uframe slot with enough bandwidth */
  1268. next = start + period;
  1269. for (; start < next; start++) {
  1270. /* check schedule: enough space? */
  1271. if (stream->highspeed) {
  1272. if (itd_slot_ok(ehci, mod, start,
  1273. stream->usecs, period))
  1274. break;
  1275. } else {
  1276. if ((start % 8) >= 6)
  1277. continue;
  1278. if (sitd_slot_ok(ehci, mod, stream,
  1279. start, sched, period))
  1280. break;
  1281. }
  1282. }
  1283. /* no room in the schedule */
  1284. if (start == next) {
  1285. ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
  1286. urb, now, now + mod);
  1287. status = -ENOSPC;
  1288. goto fail;
  1289. }
  1290. }
  1291. /* Tried to schedule too far into the future? */
  1292. if (unlikely(start - now + span - period
  1293. >= mod - 2 * SCHEDULE_SLOP)) {
  1294. ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
  1295. urb, start - now, span - period,
  1296. mod - 2 * SCHEDULE_SLOP);
  1297. status = -EFBIG;
  1298. goto fail;
  1299. }
  1300. stream->next_uframe = start & (mod - 1);
  1301. /* report high speed start in uframes; full speed, in frames */
  1302. urb->start_frame = stream->next_uframe;
  1303. if (!stream->highspeed)
  1304. urb->start_frame >>= 3;
  1305. return 0;
  1306. fail:
  1307. iso_sched_free(stream, sched);
  1308. urb->hcpriv = NULL;
  1309. return status;
  1310. }
  1311. /*-------------------------------------------------------------------------*/
  1312. static inline void
  1313. itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
  1314. struct ehci_itd *itd)
  1315. {
  1316. int i;
  1317. /* it's been recently zeroed */
  1318. itd->hw_next = EHCI_LIST_END(ehci);
  1319. itd->hw_bufp [0] = stream->buf0;
  1320. itd->hw_bufp [1] = stream->buf1;
  1321. itd->hw_bufp [2] = stream->buf2;
  1322. for (i = 0; i < 8; i++)
  1323. itd->index[i] = -1;
  1324. /* All other fields are filled when scheduling */
  1325. }
  1326. static inline void
  1327. itd_patch(
  1328. struct ehci_hcd *ehci,
  1329. struct ehci_itd *itd,
  1330. struct ehci_iso_sched *iso_sched,
  1331. unsigned index,
  1332. u16 uframe
  1333. )
  1334. {
  1335. struct ehci_iso_packet *uf = &iso_sched->packet [index];
  1336. unsigned pg = itd->pg;
  1337. // BUG_ON (pg == 6 && uf->cross);
  1338. uframe &= 0x07;
  1339. itd->index [uframe] = index;
  1340. itd->hw_transaction[uframe] = uf->transaction;
  1341. itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
  1342. itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
  1343. itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
  1344. /* iso_frame_desc[].offset must be strictly increasing */
  1345. if (unlikely (uf->cross)) {
  1346. u64 bufp = uf->bufp + 4096;
  1347. itd->pg = ++pg;
  1348. itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
  1349. itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
  1350. }
  1351. }
  1352. static inline void
  1353. itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
  1354. {
  1355. union ehci_shadow *prev = &ehci->pshadow[frame];
  1356. __hc32 *hw_p = &ehci->periodic[frame];
  1357. union ehci_shadow here = *prev;
  1358. __hc32 type = 0;
  1359. /* skip any iso nodes which might belong to previous microframes */
  1360. while (here.ptr) {
  1361. type = Q_NEXT_TYPE(ehci, *hw_p);
  1362. if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
  1363. break;
  1364. prev = periodic_next_shadow(ehci, prev, type);
  1365. hw_p = shadow_next_periodic(ehci, &here, type);
  1366. here = *prev;
  1367. }
  1368. itd->itd_next = here;
  1369. itd->hw_next = *hw_p;
  1370. prev->itd = itd;
  1371. itd->frame = frame;
  1372. wmb ();
  1373. *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
  1374. }
  1375. /* fit urb's itds into the selected schedule slot; activate as needed */
  1376. static int
  1377. itd_link_urb (
  1378. struct ehci_hcd *ehci,
  1379. struct urb *urb,
  1380. unsigned mod,
  1381. struct ehci_iso_stream *stream
  1382. )
  1383. {
  1384. int packet;
  1385. unsigned next_uframe, uframe, frame;
  1386. struct ehci_iso_sched *iso_sched = urb->hcpriv;
  1387. struct ehci_itd *itd;
  1388. next_uframe = stream->next_uframe & (mod - 1);
  1389. if (unlikely (list_empty(&stream->td_list))) {
  1390. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1391. += stream->bandwidth;
  1392. ehci_vdbg (ehci,
  1393. "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  1394. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  1395. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  1396. urb->interval,
  1397. next_uframe >> 3, next_uframe & 0x7);
  1398. }
  1399. if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
  1400. if (ehci->amd_pll_fix == 1)
  1401. usb_amd_quirk_pll_disable();
  1402. }
  1403. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
  1404. /* fill iTDs uframe by uframe */
  1405. for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
  1406. if (itd == NULL) {
  1407. /* ASSERT: we have all necessary itds */
  1408. // BUG_ON (list_empty (&iso_sched->td_list));
  1409. /* ASSERT: no itds for this endpoint in this uframe */
  1410. itd = list_entry (iso_sched->td_list.next,
  1411. struct ehci_itd, itd_list);
  1412. list_move_tail (&itd->itd_list, &stream->td_list);
  1413. itd->stream = iso_stream_get (stream);
  1414. itd->urb = urb;
  1415. itd_init (ehci, stream, itd);
  1416. }
  1417. uframe = next_uframe & 0x07;
  1418. frame = next_uframe >> 3;
  1419. itd_patch(ehci, itd, iso_sched, packet, uframe);
  1420. next_uframe += stream->interval;
  1421. next_uframe &= mod - 1;
  1422. packet++;
  1423. /* link completed itds into the schedule */
  1424. if (((next_uframe >> 3) != frame)
  1425. || packet == urb->number_of_packets) {
  1426. itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
  1427. itd = NULL;
  1428. }
  1429. }
  1430. stream->next_uframe = next_uframe;
  1431. /* don't need that schedule data any more */
  1432. iso_sched_free (stream, iso_sched);
  1433. urb->hcpriv = NULL;
  1434. timer_action (ehci, TIMER_IO_WATCHDOG);
  1435. return enable_periodic(ehci);
  1436. }
  1437. #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
  1438. /* Process and recycle a completed ITD. Return true iff its urb completed,
  1439. * and hence its completion callback probably added things to the hardware
  1440. * schedule.
  1441. *
  1442. * Note that we carefully avoid recycling this descriptor until after any
  1443. * completion callback runs, so that it won't be reused quickly. That is,
  1444. * assuming (a) no more than two urbs per frame on this endpoint, and also
  1445. * (b) only this endpoint's completions submit URBs. It seems some silicon
  1446. * corrupts things if you reuse completed descriptors very quickly...
  1447. */
  1448. static unsigned
  1449. itd_complete (
  1450. struct ehci_hcd *ehci,
  1451. struct ehci_itd *itd
  1452. ) {
  1453. struct urb *urb = itd->urb;
  1454. struct usb_iso_packet_descriptor *desc;
  1455. u32 t;
  1456. unsigned uframe;
  1457. int urb_index = -1;
  1458. struct ehci_iso_stream *stream = itd->stream;
  1459. struct usb_device *dev;
  1460. unsigned retval = false;
  1461. /* for each uframe with a packet */
  1462. for (uframe = 0; uframe < 8; uframe++) {
  1463. if (likely (itd->index[uframe] == -1))
  1464. continue;
  1465. urb_index = itd->index[uframe];
  1466. desc = &urb->iso_frame_desc [urb_index];
  1467. t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
  1468. itd->hw_transaction [uframe] = 0;
  1469. /* report transfer status */
  1470. if (unlikely (t & ISO_ERRS)) {
  1471. urb->error_count++;
  1472. if (t & EHCI_ISOC_BUF_ERR)
  1473. desc->status = usb_pipein (urb->pipe)
  1474. ? -ENOSR /* hc couldn't read */
  1475. : -ECOMM; /* hc couldn't write */
  1476. else if (t & EHCI_ISOC_BABBLE)
  1477. desc->status = -EOVERFLOW;
  1478. else /* (t & EHCI_ISOC_XACTERR) */
  1479. desc->status = -EPROTO;
  1480. /* HC need not update length with this error */
  1481. if (!(t & EHCI_ISOC_BABBLE)) {
  1482. desc->actual_length = EHCI_ITD_LENGTH(t);
  1483. urb->actual_length += desc->actual_length;
  1484. }
  1485. } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
  1486. desc->status = 0;
  1487. desc->actual_length = EHCI_ITD_LENGTH(t);
  1488. urb->actual_length += desc->actual_length;
  1489. } else {
  1490. /* URB was too late */
  1491. desc->status = -EXDEV;
  1492. }
  1493. }
  1494. /* handle completion now? */
  1495. if (likely ((urb_index + 1) != urb->number_of_packets))
  1496. goto done;
  1497. /* ASSERT: it's really the last itd for this urb
  1498. list_for_each_entry (itd, &stream->td_list, itd_list)
  1499. BUG_ON (itd->urb == urb);
  1500. */
  1501. /* give urb back to the driver; completion often (re)submits */
  1502. dev = urb->dev;
  1503. ehci_urb_done(ehci, urb, 0);
  1504. retval = true;
  1505. urb = NULL;
  1506. (void) disable_periodic(ehci);
  1507. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1508. if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
  1509. if (ehci->amd_pll_fix == 1)
  1510. usb_amd_quirk_pll_enable();
  1511. }
  1512. if (unlikely(list_is_singular(&stream->td_list))) {
  1513. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1514. -= stream->bandwidth;
  1515. ehci_vdbg (ehci,
  1516. "deschedule devp %s ep%d%s-iso\n",
  1517. dev->devpath, stream->bEndpointAddress & 0x0f,
  1518. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1519. }
  1520. iso_stream_put (ehci, stream);
  1521. done:
  1522. itd->urb = NULL;
  1523. if (ehci->clock_frame != itd->frame || itd->index[7] != -1) {
  1524. /* OK to recycle this ITD now. */
  1525. itd->stream = NULL;
  1526. list_move(&itd->itd_list, &stream->free_list);
  1527. iso_stream_put(ehci, stream);
  1528. } else {
  1529. /* HW might remember this ITD, so we can't recycle it yet.
  1530. * Move it to a safe place until a new frame starts.
  1531. */
  1532. list_move(&itd->itd_list, &ehci->cached_itd_list);
  1533. if (stream->refcount == 2) {
  1534. /* If iso_stream_put() were called here, stream
  1535. * would be freed. Instead, just prevent reuse.
  1536. */
  1537. stream->ep->hcpriv = NULL;
  1538. stream->ep = NULL;
  1539. }
  1540. }
  1541. return retval;
  1542. }
  1543. /*-------------------------------------------------------------------------*/
  1544. static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1545. gfp_t mem_flags)
  1546. {
  1547. int status = -EINVAL;
  1548. unsigned long flags;
  1549. struct ehci_iso_stream *stream;
  1550. /* Get iso_stream head */
  1551. stream = iso_stream_find (ehci, urb);
  1552. if (unlikely (stream == NULL)) {
  1553. ehci_dbg (ehci, "can't get iso stream\n");
  1554. return -ENOMEM;
  1555. }
  1556. if (unlikely (urb->interval != stream->interval)) {
  1557. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1558. stream->interval, urb->interval);
  1559. goto done;
  1560. }
  1561. #ifdef EHCI_URB_TRACE
  1562. ehci_dbg (ehci,
  1563. "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
  1564. __func__, urb->dev->devpath, urb,
  1565. usb_pipeendpoint (urb->pipe),
  1566. usb_pipein (urb->pipe) ? "in" : "out",
  1567. urb->transfer_buffer_length,
  1568. urb->number_of_packets, urb->interval,
  1569. stream);
  1570. #endif
  1571. /* allocate ITDs w/o locking anything */
  1572. status = itd_urb_transaction (stream, ehci, urb, mem_flags);
  1573. if (unlikely (status < 0)) {
  1574. ehci_dbg (ehci, "can't init itds\n");
  1575. goto done;
  1576. }
  1577. /* schedule ... need to lock */
  1578. spin_lock_irqsave (&ehci->lock, flags);
  1579. if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
  1580. status = -ESHUTDOWN;
  1581. goto done_not_linked;
  1582. }
  1583. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  1584. if (unlikely(status))
  1585. goto done_not_linked;
  1586. status = iso_stream_schedule(ehci, urb, stream);
  1587. if (likely (status == 0))
  1588. itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1589. else
  1590. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  1591. done_not_linked:
  1592. spin_unlock_irqrestore (&ehci->lock, flags);
  1593. done:
  1594. if (unlikely (status < 0))
  1595. iso_stream_put (ehci, stream);
  1596. return status;
  1597. }
  1598. /*-------------------------------------------------------------------------*/
  1599. /*
  1600. * "Split ISO TDs" ... used for USB 1.1 devices going through the
  1601. * TTs in USB 2.0 hubs. These need microframe scheduling.
  1602. */
  1603. static inline void
  1604. sitd_sched_init(
  1605. struct ehci_hcd *ehci,
  1606. struct ehci_iso_sched *iso_sched,
  1607. struct ehci_iso_stream *stream,
  1608. struct urb *urb
  1609. )
  1610. {
  1611. unsigned i;
  1612. dma_addr_t dma = urb->transfer_dma;
  1613. /* how many frames are needed for these transfers */
  1614. iso_sched->span = urb->number_of_packets * stream->interval;
  1615. /* figure out per-frame sitd fields that we'll need later
  1616. * when we fit new sitds into the schedule.
  1617. */
  1618. for (i = 0; i < urb->number_of_packets; i++) {
  1619. struct ehci_iso_packet *packet = &iso_sched->packet [i];
  1620. unsigned length;
  1621. dma_addr_t buf;
  1622. u32 trans;
  1623. length = urb->iso_frame_desc [i].length & 0x03ff;
  1624. buf = dma + urb->iso_frame_desc [i].offset;
  1625. trans = SITD_STS_ACTIVE;
  1626. if (((i + 1) == urb->number_of_packets)
  1627. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  1628. trans |= SITD_IOC;
  1629. trans |= length << 16;
  1630. packet->transaction = cpu_to_hc32(ehci, trans);
  1631. /* might need to cross a buffer page within a td */
  1632. packet->bufp = buf;
  1633. packet->buf1 = (buf + length) & ~0x0fff;
  1634. if (packet->buf1 != (buf & ~(u64)0x0fff))
  1635. packet->cross = 1;
  1636. /* OUT uses multiple start-splits */
  1637. if (stream->bEndpointAddress & USB_DIR_IN)
  1638. continue;
  1639. length = (length + 187) / 188;
  1640. if (length > 1) /* BEGIN vs ALL */
  1641. length |= 1 << 3;
  1642. packet->buf1 |= length;
  1643. }
  1644. }
  1645. static int
  1646. sitd_urb_transaction (
  1647. struct ehci_iso_stream *stream,
  1648. struct ehci_hcd *ehci,
  1649. struct urb *urb,
  1650. gfp_t mem_flags
  1651. )
  1652. {
  1653. struct ehci_sitd *sitd;
  1654. dma_addr_t sitd_dma;
  1655. int i;
  1656. struct ehci_iso_sched *iso_sched;
  1657. unsigned long flags;
  1658. iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  1659. if (iso_sched == NULL)
  1660. return -ENOMEM;
  1661. sitd_sched_init(ehci, iso_sched, stream, urb);
  1662. /* allocate/init sITDs */
  1663. spin_lock_irqsave (&ehci->lock, flags);
  1664. for (i = 0; i < urb->number_of_packets; i++) {
  1665. /* NOTE: for now, we don't try to handle wraparound cases
  1666. * for IN (using sitd->hw_backpointer, like a FSTN), which
  1667. * means we never need two sitds for full speed packets.
  1668. */
  1669. /* free_list.next might be cache-hot ... but maybe
  1670. * the HC caches it too. avoid that issue for now.
  1671. */
  1672. /* prefer previously-allocated sitds */
  1673. if (!list_empty(&stream->free_list)) {
  1674. sitd = list_entry (stream->free_list.prev,
  1675. struct ehci_sitd, sitd_list);
  1676. list_del (&sitd->sitd_list);
  1677. sitd_dma = sitd->sitd_dma;
  1678. } else {
  1679. spin_unlock_irqrestore (&ehci->lock, flags);
  1680. sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
  1681. &sitd_dma);
  1682. spin_lock_irqsave (&ehci->lock, flags);
  1683. if (!sitd) {
  1684. iso_sched_free(stream, iso_sched);
  1685. spin_unlock_irqrestore(&ehci->lock, flags);
  1686. return -ENOMEM;
  1687. }
  1688. }
  1689. memset (sitd, 0, sizeof *sitd);
  1690. sitd->sitd_dma = sitd_dma;
  1691. list_add (&sitd->sitd_list, &iso_sched->td_list);
  1692. }
  1693. /* temporarily store schedule info in hcpriv */
  1694. urb->hcpriv = iso_sched;
  1695. urb->error_count = 0;
  1696. spin_unlock_irqrestore (&ehci->lock, flags);
  1697. return 0;
  1698. }
  1699. /*-------------------------------------------------------------------------*/
  1700. static inline void
  1701. sitd_patch(
  1702. struct ehci_hcd *ehci,
  1703. struct ehci_iso_stream *stream,
  1704. struct ehci_sitd *sitd,
  1705. struct ehci_iso_sched *iso_sched,
  1706. unsigned index
  1707. )
  1708. {
  1709. struct ehci_iso_packet *uf = &iso_sched->packet [index];
  1710. u64 bufp = uf->bufp;
  1711. sitd->hw_next = EHCI_LIST_END(ehci);
  1712. sitd->hw_fullspeed_ep = stream->address;
  1713. sitd->hw_uframe = stream->splits;
  1714. sitd->hw_results = uf->transaction;
  1715. sitd->hw_backpointer = EHCI_LIST_END(ehci);
  1716. bufp = uf->bufp;
  1717. sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
  1718. sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
  1719. sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
  1720. if (uf->cross)
  1721. bufp += 4096;
  1722. sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
  1723. sitd->index = index;
  1724. }
  1725. static inline void
  1726. sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
  1727. {
  1728. /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
  1729. sitd->sitd_next = ehci->pshadow [frame];
  1730. sitd->hw_next = ehci->periodic [frame];
  1731. ehci->pshadow [frame].sitd = sitd;
  1732. sitd->frame = frame;
  1733. wmb ();
  1734. ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
  1735. }
  1736. /* fit urb's sitds into the selected schedule slot; activate as needed */
  1737. static int
  1738. sitd_link_urb (
  1739. struct ehci_hcd *ehci,
  1740. struct urb *urb,
  1741. unsigned mod,
  1742. struct ehci_iso_stream *stream
  1743. )
  1744. {
  1745. int packet;
  1746. unsigned next_uframe;
  1747. struct ehci_iso_sched *sched = urb->hcpriv;
  1748. struct ehci_sitd *sitd;
  1749. next_uframe = stream->next_uframe;
  1750. if (list_empty(&stream->td_list)) {
  1751. /* usbfs ignores TT bandwidth */
  1752. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1753. += stream->bandwidth;
  1754. ehci_vdbg (ehci,
  1755. "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
  1756. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  1757. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  1758. (next_uframe >> 3) & (ehci->periodic_size - 1),
  1759. stream->interval, hc32_to_cpu(ehci, stream->splits));
  1760. }
  1761. if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
  1762. if (ehci->amd_pll_fix == 1)
  1763. usb_amd_quirk_pll_disable();
  1764. }
  1765. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
  1766. /* fill sITDs frame by frame */
  1767. for (packet = 0, sitd = NULL;
  1768. packet < urb->number_of_packets;
  1769. packet++) {
  1770. /* ASSERT: we have all necessary sitds */
  1771. BUG_ON (list_empty (&sched->td_list));
  1772. /* ASSERT: no itds for this endpoint in this frame */
  1773. sitd = list_entry (sched->td_list.next,
  1774. struct ehci_sitd, sitd_list);
  1775. list_move_tail (&sitd->sitd_list, &stream->td_list);
  1776. sitd->stream = iso_stream_get (stream);
  1777. sitd->urb = urb;
  1778. sitd_patch(ehci, stream, sitd, sched, packet);
  1779. sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
  1780. sitd);
  1781. next_uframe += stream->interval << 3;
  1782. }
  1783. stream->next_uframe = next_uframe & (mod - 1);
  1784. /* don't need that schedule data any more */
  1785. iso_sched_free (stream, sched);
  1786. urb->hcpriv = NULL;
  1787. timer_action (ehci, TIMER_IO_WATCHDOG);
  1788. return enable_periodic(ehci);
  1789. }
  1790. /*-------------------------------------------------------------------------*/
  1791. #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
  1792. | SITD_STS_XACT | SITD_STS_MMF)
  1793. /* Process and recycle a completed SITD. Return true iff its urb completed,
  1794. * and hence its completion callback probably added things to the hardware
  1795. * schedule.
  1796. *
  1797. * Note that we carefully avoid recycling this descriptor until after any
  1798. * completion callback runs, so that it won't be reused quickly. That is,
  1799. * assuming (a) no more than two urbs per frame on this endpoint, and also
  1800. * (b) only this endpoint's completions submit URBs. It seems some silicon
  1801. * corrupts things if you reuse completed descriptors very quickly...
  1802. */
  1803. static unsigned
  1804. sitd_complete (
  1805. struct ehci_hcd *ehci,
  1806. struct ehci_sitd *sitd
  1807. ) {
  1808. struct urb *urb = sitd->urb;
  1809. struct usb_iso_packet_descriptor *desc;
  1810. u32 t;
  1811. int urb_index = -1;
  1812. struct ehci_iso_stream *stream = sitd->stream;
  1813. struct usb_device *dev;
  1814. unsigned retval = false;
  1815. urb_index = sitd->index;
  1816. desc = &urb->iso_frame_desc [urb_index];
  1817. t = hc32_to_cpup(ehci, &sitd->hw_results);
  1818. /* report transfer status */
  1819. if (t & SITD_ERRS) {
  1820. urb->error_count++;
  1821. if (t & SITD_STS_DBE)
  1822. desc->status = usb_pipein (urb->pipe)
  1823. ? -ENOSR /* hc couldn't read */
  1824. : -ECOMM; /* hc couldn't write */
  1825. else if (t & SITD_STS_BABBLE)
  1826. desc->status = -EOVERFLOW;
  1827. else /* XACT, MMF, etc */
  1828. desc->status = -EPROTO;
  1829. } else {
  1830. desc->status = 0;
  1831. desc->actual_length = desc->length - SITD_LENGTH(t);
  1832. urb->actual_length += desc->actual_length;
  1833. }
  1834. /* handle completion now? */
  1835. if ((urb_index + 1) != urb->number_of_packets)
  1836. goto done;
  1837. /* ASSERT: it's really the last sitd for this urb
  1838. list_for_each_entry (sitd, &stream->td_list, sitd_list)
  1839. BUG_ON (sitd->urb == urb);
  1840. */
  1841. /* give urb back to the driver; completion often (re)submits */
  1842. dev = urb->dev;
  1843. ehci_urb_done(ehci, urb, 0);
  1844. retval = true;
  1845. urb = NULL;
  1846. (void) disable_periodic(ehci);
  1847. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1848. if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
  1849. if (ehci->amd_pll_fix == 1)
  1850. usb_amd_quirk_pll_enable();
  1851. }
  1852. if (list_is_singular(&stream->td_list)) {
  1853. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1854. -= stream->bandwidth;
  1855. ehci_vdbg (ehci,
  1856. "deschedule devp %s ep%d%s-iso\n",
  1857. dev->devpath, stream->bEndpointAddress & 0x0f,
  1858. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1859. }
  1860. iso_stream_put (ehci, stream);
  1861. done:
  1862. sitd->urb = NULL;
  1863. if (ehci->clock_frame != sitd->frame) {
  1864. /* OK to recycle this SITD now. */
  1865. sitd->stream = NULL;
  1866. list_move(&sitd->sitd_list, &stream->free_list);
  1867. iso_stream_put(ehci, stream);
  1868. } else {
  1869. /* HW might remember this SITD, so we can't recycle it yet.
  1870. * Move it to a safe place until a new frame starts.
  1871. */
  1872. list_move(&sitd->sitd_list, &ehci->cached_sitd_list);
  1873. if (stream->refcount == 2) {
  1874. /* If iso_stream_put() were called here, stream
  1875. * would be freed. Instead, just prevent reuse.
  1876. */
  1877. stream->ep->hcpriv = NULL;
  1878. stream->ep = NULL;
  1879. }
  1880. }
  1881. return retval;
  1882. }
  1883. static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1884. gfp_t mem_flags)
  1885. {
  1886. int status = -EINVAL;
  1887. unsigned long flags;
  1888. struct ehci_iso_stream *stream;
  1889. /* Get iso_stream head */
  1890. stream = iso_stream_find (ehci, urb);
  1891. if (stream == NULL) {
  1892. ehci_dbg (ehci, "can't get iso stream\n");
  1893. return -ENOMEM;
  1894. }
  1895. if (urb->interval != stream->interval) {
  1896. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1897. stream->interval, urb->interval);
  1898. goto done;
  1899. }
  1900. #ifdef EHCI_URB_TRACE
  1901. ehci_dbg (ehci,
  1902. "submit %p dev%s ep%d%s-iso len %d\n",
  1903. urb, urb->dev->devpath,
  1904. usb_pipeendpoint (urb->pipe),
  1905. usb_pipein (urb->pipe) ? "in" : "out",
  1906. urb->transfer_buffer_length);
  1907. #endif
  1908. /* allocate SITDs */
  1909. status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
  1910. if (status < 0) {
  1911. ehci_dbg (ehci, "can't init sitds\n");
  1912. goto done;
  1913. }
  1914. /* schedule ... need to lock */
  1915. spin_lock_irqsave (&ehci->lock, flags);
  1916. if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
  1917. status = -ESHUTDOWN;
  1918. goto done_not_linked;
  1919. }
  1920. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  1921. if (unlikely(status))
  1922. goto done_not_linked;
  1923. status = iso_stream_schedule(ehci, urb, stream);
  1924. if (status == 0)
  1925. sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1926. else
  1927. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  1928. done_not_linked:
  1929. spin_unlock_irqrestore (&ehci->lock, flags);
  1930. done:
  1931. if (status < 0)
  1932. iso_stream_put (ehci, stream);
  1933. return status;
  1934. }
  1935. /*-------------------------------------------------------------------------*/
  1936. static void free_cached_lists(struct ehci_hcd *ehci)
  1937. {
  1938. struct ehci_itd *itd, *n;
  1939. struct ehci_sitd *sitd, *sn;
  1940. list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
  1941. struct ehci_iso_stream *stream = itd->stream;
  1942. itd->stream = NULL;
  1943. list_move(&itd->itd_list, &stream->free_list);
  1944. iso_stream_put(ehci, stream);
  1945. }
  1946. list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
  1947. struct ehci_iso_stream *stream = sitd->stream;
  1948. sitd->stream = NULL;
  1949. list_move(&sitd->sitd_list, &stream->free_list);
  1950. iso_stream_put(ehci, stream);
  1951. }
  1952. }
  1953. /*-------------------------------------------------------------------------*/
  1954. static void
  1955. scan_periodic (struct ehci_hcd *ehci)
  1956. {
  1957. unsigned now_uframe, frame, clock, clock_frame, mod;
  1958. unsigned modified;
  1959. mod = ehci->periodic_size << 3;
  1960. /*
  1961. * When running, scan from last scan point up to "now"
  1962. * else clean up by scanning everything that's left.
  1963. * Touches as few pages as possible: cache-friendly.
  1964. */
  1965. now_uframe = ehci->next_uframe;
  1966. if (HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
  1967. clock = ehci_readl(ehci, &ehci->regs->frame_index);
  1968. clock_frame = (clock >> 3) & (ehci->periodic_size - 1);
  1969. } else {
  1970. clock = now_uframe + mod - 1;
  1971. clock_frame = -1;
  1972. }
  1973. if (ehci->clock_frame != clock_frame) {
  1974. free_cached_lists(ehci);
  1975. ehci->clock_frame = clock_frame;
  1976. }
  1977. clock &= mod - 1;
  1978. clock_frame = clock >> 3;
  1979. ++ehci->periodic_stamp;
  1980. for (;;) {
  1981. union ehci_shadow q, *q_p;
  1982. __hc32 type, *hw_p;
  1983. unsigned incomplete = false;
  1984. frame = now_uframe >> 3;
  1985. restart:
  1986. /* scan each element in frame's queue for completions */
  1987. q_p = &ehci->pshadow [frame];
  1988. hw_p = &ehci->periodic [frame];
  1989. q.ptr = q_p->ptr;
  1990. type = Q_NEXT_TYPE(ehci, *hw_p);
  1991. modified = 0;
  1992. while (q.ptr != NULL) {
  1993. unsigned uf;
  1994. union ehci_shadow temp;
  1995. int live;
  1996. live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
  1997. switch (hc32_to_cpu(ehci, type)) {
  1998. case Q_TYPE_QH:
  1999. /* handle any completions */
  2000. temp.qh = qh_get (q.qh);
  2001. type = Q_NEXT_TYPE(ehci, q.qh->hw->hw_next);
  2002. q = q.qh->qh_next;
  2003. if (temp.qh->stamp != ehci->periodic_stamp) {
  2004. modified = qh_completions(ehci, temp.qh);
  2005. if (!modified)
  2006. temp.qh->stamp = ehci->periodic_stamp;
  2007. if (unlikely(list_empty(&temp.qh->qtd_list) ||
  2008. temp.qh->needs_rescan))
  2009. intr_deschedule(ehci, temp.qh);
  2010. }
  2011. qh_put (temp.qh);
  2012. break;
  2013. case Q_TYPE_FSTN:
  2014. /* for "save place" FSTNs, look at QH entries
  2015. * in the previous frame for completions.
  2016. */
  2017. if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
  2018. dbg ("ignoring completions from FSTNs");
  2019. }
  2020. type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
  2021. q = q.fstn->fstn_next;
  2022. break;
  2023. case Q_TYPE_ITD:
  2024. /* If this ITD is still active, leave it for
  2025. * later processing ... check the next entry.
  2026. * No need to check for activity unless the
  2027. * frame is current.
  2028. */
  2029. if (frame == clock_frame && live) {
  2030. rmb();
  2031. for (uf = 0; uf < 8; uf++) {
  2032. if (q.itd->hw_transaction[uf] &
  2033. ITD_ACTIVE(ehci))
  2034. break;
  2035. }
  2036. if (uf < 8) {
  2037. incomplete = true;
  2038. q_p = &q.itd->itd_next;
  2039. hw_p = &q.itd->hw_next;
  2040. type = Q_NEXT_TYPE(ehci,
  2041. q.itd->hw_next);
  2042. q = *q_p;
  2043. break;
  2044. }
  2045. }
  2046. /* Take finished ITDs out of the schedule
  2047. * and process them: recycle, maybe report
  2048. * URB completion. HC won't cache the
  2049. * pointer for much longer, if at all.
  2050. */
  2051. *q_p = q.itd->itd_next;
  2052. if (!ehci->use_dummy_qh ||
  2053. q.itd->hw_next != EHCI_LIST_END(ehci))
  2054. *hw_p = q.itd->hw_next;
  2055. else
  2056. *hw_p = ehci->dummy->qh_dma;
  2057. type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
  2058. wmb();
  2059. modified = itd_complete (ehci, q.itd);
  2060. q = *q_p;
  2061. break;
  2062. case Q_TYPE_SITD:
  2063. /* If this SITD is still active, leave it for
  2064. * later processing ... check the next entry.
  2065. * No need to check for activity unless the
  2066. * frame is current.
  2067. */
  2068. if (((frame == clock_frame) ||
  2069. (((frame + 1) & (ehci->periodic_size - 1))
  2070. == clock_frame))
  2071. && live
  2072. && (q.sitd->hw_results &
  2073. SITD_ACTIVE(ehci))) {
  2074. incomplete = true;
  2075. q_p = &q.sitd->sitd_next;
  2076. hw_p = &q.sitd->hw_next;
  2077. type = Q_NEXT_TYPE(ehci,
  2078. q.sitd->hw_next);
  2079. q = *q_p;
  2080. break;
  2081. }
  2082. /* Take finished SITDs out of the schedule
  2083. * and process them: recycle, maybe report
  2084. * URB completion.
  2085. */
  2086. *q_p = q.sitd->sitd_next;
  2087. if (!ehci->use_dummy_qh ||
  2088. q.sitd->hw_next != EHCI_LIST_END(ehci))
  2089. *hw_p = q.sitd->hw_next;
  2090. else
  2091. *hw_p = ehci->dummy->qh_dma;
  2092. type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
  2093. wmb();
  2094. modified = sitd_complete (ehci, q.sitd);
  2095. q = *q_p;
  2096. break;
  2097. default:
  2098. dbg ("corrupt type %d frame %d shadow %p",
  2099. type, frame, q.ptr);
  2100. // BUG ();
  2101. q.ptr = NULL;
  2102. }
  2103. /* assume completion callbacks modify the queue */
  2104. if (unlikely (modified)) {
  2105. if (likely(ehci->periodic_sched > 0))
  2106. goto restart;
  2107. /* short-circuit this scan */
  2108. now_uframe = clock;
  2109. break;
  2110. }
  2111. }
  2112. /* If we can tell we caught up to the hardware, stop now.
  2113. * We can't advance our scan without collecting the ISO
  2114. * transfers that are still pending in this frame.
  2115. */
  2116. if (incomplete && HC_IS_RUNNING(ehci_to_hcd(ehci)->state)) {
  2117. ehci->next_uframe = now_uframe;
  2118. break;
  2119. }
  2120. // FIXME: this assumes we won't get lapped when
  2121. // latencies climb; that should be rare, but...
  2122. // detect it, and just go all the way around.
  2123. // FLR might help detect this case, so long as latencies
  2124. // don't exceed periodic_size msec (default 1.024 sec).
  2125. // FIXME: likewise assumes HC doesn't halt mid-scan
  2126. if (now_uframe == clock) {
  2127. unsigned now;
  2128. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
  2129. || ehci->periodic_sched == 0)
  2130. break;
  2131. ehci->next_uframe = now_uframe;
  2132. now = ehci_readl(ehci, &ehci->regs->frame_index) &
  2133. (mod - 1);
  2134. if (now_uframe == now)
  2135. break;
  2136. /* rescan the rest of this frame, then ... */
  2137. clock = now;
  2138. clock_frame = clock >> 3;
  2139. if (ehci->clock_frame != clock_frame) {
  2140. free_cached_lists(ehci);
  2141. ehci->clock_frame = clock_frame;
  2142. ++ehci->periodic_stamp;
  2143. }
  2144. } else {
  2145. now_uframe++;
  2146. now_uframe &= mod - 1;
  2147. }
  2148. }
  2149. }