ehci-sched.c 65 KB

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