ehci-sched.c 60 KB

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