ehci-sched.c 65 KB

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