ehci-sched.c 65 KB

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