ehci-sched.c 62 KB

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