ehci-sched.c 61 KB

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