ehci-sched.c 64 KB

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