ehci-sched.c 66 KB

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