ehci-sched.c 65 KB

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