ehci-sched.c 59 KB

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