ehci-sched.c 65 KB

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