ehci-sched.c 61 KB

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