ehci-sched.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  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(ehci, &ehci->regs->status, STS_PSS, 0, 9 * 125);
  398. if (status != 0) {
  399. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  400. return status;
  401. }
  402. cmd = ehci_readl(ehci, &ehci->regs->command) | CMD_PSE;
  403. ehci_writel(ehci, cmd, &ehci->regs->command);
  404. /* posted write ... PSS happens later */
  405. ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
  406. /* make sure ehci_work scans these */
  407. ehci->next_uframe = ehci_readl(ehci, &ehci->regs->frame_index)
  408. % (ehci->periodic_size << 3);
  409. return 0;
  410. }
  411. static int disable_periodic (struct ehci_hcd *ehci)
  412. {
  413. u32 cmd;
  414. int status;
  415. /* did setting PSE not take effect yet?
  416. * takes effect only at frame boundaries...
  417. */
  418. status = handshake(ehci, &ehci->regs->status, STS_PSS, STS_PSS, 9 * 125);
  419. if (status != 0) {
  420. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  421. return status;
  422. }
  423. cmd = ehci_readl(ehci, &ehci->regs->command) & ~CMD_PSE;
  424. ehci_writel(ehci, cmd, &ehci->regs->command);
  425. /* posted write ... */
  426. ehci->next_uframe = -1;
  427. return 0;
  428. }
  429. /*-------------------------------------------------------------------------*/
  430. /* periodic schedule slots have iso tds (normal or split) first, then a
  431. * sparse tree for active interrupt transfers.
  432. *
  433. * this just links in a qh; caller guarantees uframe masks are set right.
  434. * no FSTN support (yet; ehci 0.96+)
  435. */
  436. static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
  437. {
  438. unsigned i;
  439. unsigned period = qh->period;
  440. dev_dbg (&qh->dev->dev,
  441. "link qh%d-%04x/%p start %d [%d/%d us]\n",
  442. period, hc32_to_cpup(ehci, &qh->hw_info2) & (QH_CMASK | QH_SMASK),
  443. qh, qh->start, qh->usecs, qh->c_usecs);
  444. /* high bandwidth, or otherwise every microframe */
  445. if (period == 0)
  446. period = 1;
  447. for (i = qh->start; i < ehci->periodic_size; i += period) {
  448. union ehci_shadow *prev = &ehci->pshadow[i];
  449. __hc32 *hw_p = &ehci->periodic[i];
  450. union ehci_shadow here = *prev;
  451. __hc32 type = 0;
  452. /* skip the iso nodes at list head */
  453. while (here.ptr) {
  454. type = Q_NEXT_TYPE(ehci, *hw_p);
  455. if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
  456. break;
  457. prev = periodic_next_shadow(ehci, prev, type);
  458. hw_p = &here.qh->hw_next;
  459. here = *prev;
  460. }
  461. /* sorting each branch by period (slow-->fast)
  462. * enables sharing interior tree nodes
  463. */
  464. while (here.ptr && qh != here.qh) {
  465. if (qh->period > here.qh->period)
  466. break;
  467. prev = &here.qh->qh_next;
  468. hw_p = &here.qh->hw_next;
  469. here = *prev;
  470. }
  471. /* link in this qh, unless some earlier pass did that */
  472. if (qh != here.qh) {
  473. qh->qh_next = here;
  474. if (here.qh)
  475. qh->hw_next = *hw_p;
  476. wmb ();
  477. prev->qh = qh;
  478. *hw_p = QH_NEXT (ehci, qh->qh_dma);
  479. }
  480. }
  481. qh->qh_state = QH_STATE_LINKED;
  482. qh_get (qh);
  483. /* update per-qh bandwidth for usbfs */
  484. ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
  485. ? ((qh->usecs + qh->c_usecs) / qh->period)
  486. : (qh->usecs * 8);
  487. /* maybe enable periodic schedule processing */
  488. if (!ehci->periodic_sched++)
  489. return enable_periodic (ehci);
  490. return 0;
  491. }
  492. static void qh_unlink_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
  493. {
  494. unsigned i;
  495. unsigned period;
  496. // FIXME:
  497. // IF this isn't high speed
  498. // and this qh is active in the current uframe
  499. // (and overlay token SplitXstate is false?)
  500. // THEN
  501. // qh->hw_info1 |= __constant_cpu_to_hc32(1 << 7 /* "ignore" */);
  502. /* high bandwidth, or otherwise part of every microframe */
  503. if ((period = qh->period) == 0)
  504. period = 1;
  505. for (i = qh->start; i < ehci->periodic_size; i += period)
  506. periodic_unlink (ehci, i, qh);
  507. /* update per-qh bandwidth for usbfs */
  508. ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
  509. ? ((qh->usecs + qh->c_usecs) / qh->period)
  510. : (qh->usecs * 8);
  511. dev_dbg (&qh->dev->dev,
  512. "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
  513. qh->period,
  514. hc32_to_cpup(ehci, &qh->hw_info2) & (QH_CMASK | QH_SMASK),
  515. qh, qh->start, qh->usecs, qh->c_usecs);
  516. /* qh->qh_next still "live" to HC */
  517. qh->qh_state = QH_STATE_UNLINK;
  518. qh->qh_next.ptr = NULL;
  519. qh_put (qh);
  520. /* maybe turn off periodic schedule */
  521. ehci->periodic_sched--;
  522. if (!ehci->periodic_sched)
  523. (void) disable_periodic (ehci);
  524. }
  525. static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh)
  526. {
  527. unsigned wait;
  528. qh_unlink_periodic (ehci, qh);
  529. /* simple/paranoid: always delay, expecting the HC needs to read
  530. * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and
  531. * expect khubd to clean up after any CSPLITs we won't issue.
  532. * active high speed queues may need bigger delays...
  533. */
  534. if (list_empty (&qh->qtd_list)
  535. || (cpu_to_hc32(ehci, QH_CMASK)
  536. & qh->hw_info2) != 0)
  537. wait = 2;
  538. else
  539. wait = 55; /* worst case: 3 * 1024 */
  540. udelay (wait);
  541. qh->qh_state = QH_STATE_IDLE;
  542. qh->hw_next = EHCI_LIST_END(ehci);
  543. wmb ();
  544. }
  545. /*-------------------------------------------------------------------------*/
  546. static int check_period (
  547. struct ehci_hcd *ehci,
  548. unsigned frame,
  549. unsigned uframe,
  550. unsigned period,
  551. unsigned usecs
  552. ) {
  553. int claimed;
  554. /* complete split running into next frame?
  555. * given FSTN support, we could sometimes check...
  556. */
  557. if (uframe >= 8)
  558. return 0;
  559. /*
  560. * 80% periodic == 100 usec/uframe available
  561. * convert "usecs we need" to "max already claimed"
  562. */
  563. usecs = 100 - usecs;
  564. /* we "know" 2 and 4 uframe intervals were rejected; so
  565. * for period 0, check _every_ microframe in the schedule.
  566. */
  567. if (unlikely (period == 0)) {
  568. do {
  569. for (uframe = 0; uframe < 7; uframe++) {
  570. claimed = periodic_usecs (ehci, frame, uframe);
  571. if (claimed > usecs)
  572. return 0;
  573. }
  574. } while ((frame += 1) < ehci->periodic_size);
  575. /* just check the specified uframe, at that period */
  576. } else {
  577. do {
  578. claimed = periodic_usecs (ehci, frame, uframe);
  579. if (claimed > usecs)
  580. return 0;
  581. } while ((frame += period) < ehci->periodic_size);
  582. }
  583. // success!
  584. return 1;
  585. }
  586. static int check_intr_schedule (
  587. struct ehci_hcd *ehci,
  588. unsigned frame,
  589. unsigned uframe,
  590. const struct ehci_qh *qh,
  591. __hc32 *c_maskp
  592. )
  593. {
  594. int retval = -ENOSPC;
  595. u8 mask = 0;
  596. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
  597. goto done;
  598. if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
  599. goto done;
  600. if (!qh->c_usecs) {
  601. retval = 0;
  602. *c_maskp = 0;
  603. goto done;
  604. }
  605. #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
  606. if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
  607. qh->tt_usecs)) {
  608. unsigned i;
  609. /* TODO : this may need FSTN for SSPLIT in uframe 5. */
  610. for (i=uframe+1; i<8 && i<uframe+4; i++)
  611. if (!check_period (ehci, frame, i,
  612. qh->period, qh->c_usecs))
  613. goto done;
  614. else
  615. mask |= 1 << i;
  616. retval = 0;
  617. *c_maskp = cpu_to_hc32(ehci, mask << 8);
  618. }
  619. #else
  620. /* Make sure this tt's buffer is also available for CSPLITs.
  621. * We pessimize a bit; probably the typical full speed case
  622. * doesn't need the second CSPLIT.
  623. *
  624. * NOTE: both SPLIT and CSPLIT could be checked in just
  625. * one smart pass...
  626. */
  627. mask = 0x03 << (uframe + qh->gap_uf);
  628. *c_maskp = cpu_to_hc32(ehci, mask << 8);
  629. mask |= 1 << uframe;
  630. if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
  631. if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
  632. qh->period, qh->c_usecs))
  633. goto done;
  634. if (!check_period (ehci, frame, uframe + qh->gap_uf,
  635. qh->period, qh->c_usecs))
  636. goto done;
  637. retval = 0;
  638. }
  639. #endif
  640. done:
  641. return retval;
  642. }
  643. /* "first fit" scheduling policy used the first time through,
  644. * or when the previous schedule slot can't be re-used.
  645. */
  646. static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
  647. {
  648. int status;
  649. unsigned uframe;
  650. __hc32 c_mask;
  651. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
  652. qh_refresh(ehci, qh);
  653. qh->hw_next = EHCI_LIST_END(ehci);
  654. frame = qh->start;
  655. /* reuse the previous schedule slots, if we can */
  656. if (frame < qh->period) {
  657. uframe = ffs(hc32_to_cpup(ehci, &qh->hw_info2) & QH_SMASK);
  658. status = check_intr_schedule (ehci, frame, --uframe,
  659. qh, &c_mask);
  660. } else {
  661. uframe = 0;
  662. c_mask = 0;
  663. status = -ENOSPC;
  664. }
  665. /* else scan the schedule to find a group of slots such that all
  666. * uframes have enough periodic bandwidth available.
  667. */
  668. if (status) {
  669. /* "normal" case, uframing flexible except with splits */
  670. if (qh->period) {
  671. frame = qh->period - 1;
  672. do {
  673. for (uframe = 0; uframe < 8; uframe++) {
  674. status = check_intr_schedule (ehci,
  675. frame, uframe, qh,
  676. &c_mask);
  677. if (status == 0)
  678. break;
  679. }
  680. } while (status && frame--);
  681. /* qh->period == 0 means every uframe */
  682. } else {
  683. frame = 0;
  684. status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
  685. }
  686. if (status)
  687. goto done;
  688. qh->start = frame;
  689. /* reset S-frame and (maybe) C-frame masks */
  690. qh->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
  691. qh->hw_info2 |= qh->period
  692. ? cpu_to_hc32(ehci, 1 << uframe)
  693. : cpu_to_hc32(ehci, QH_SMASK);
  694. qh->hw_info2 |= c_mask;
  695. } else
  696. ehci_dbg (ehci, "reused qh %p schedule\n", qh);
  697. /* stuff into the periodic schedule */
  698. status = qh_link_periodic (ehci, qh);
  699. done:
  700. return status;
  701. }
  702. static int intr_submit (
  703. struct ehci_hcd *ehci,
  704. struct urb *urb,
  705. struct list_head *qtd_list,
  706. gfp_t mem_flags
  707. ) {
  708. unsigned epnum;
  709. unsigned long flags;
  710. struct ehci_qh *qh;
  711. int status;
  712. struct list_head empty;
  713. /* get endpoint and transfer/schedule data */
  714. epnum = urb->ep->desc.bEndpointAddress;
  715. spin_lock_irqsave (&ehci->lock, flags);
  716. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  717. &ehci_to_hcd(ehci)->flags))) {
  718. status = -ESHUTDOWN;
  719. goto done_not_linked;
  720. }
  721. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  722. if (unlikely(status))
  723. goto done_not_linked;
  724. /* get qh and force any scheduling errors */
  725. INIT_LIST_HEAD (&empty);
  726. qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
  727. if (qh == NULL) {
  728. status = -ENOMEM;
  729. goto done;
  730. }
  731. if (qh->qh_state == QH_STATE_IDLE) {
  732. if ((status = qh_schedule (ehci, qh)) != 0)
  733. goto done;
  734. }
  735. /* then queue the urb's tds to the qh */
  736. qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
  737. BUG_ON (qh == NULL);
  738. /* ... update usbfs periodic stats */
  739. ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
  740. done:
  741. if (unlikely(status))
  742. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  743. done_not_linked:
  744. spin_unlock_irqrestore (&ehci->lock, flags);
  745. if (status)
  746. qtd_list_free (ehci, urb, qtd_list);
  747. return status;
  748. }
  749. /*-------------------------------------------------------------------------*/
  750. /* ehci_iso_stream ops work with both ITD and SITD */
  751. static struct ehci_iso_stream *
  752. iso_stream_alloc (gfp_t mem_flags)
  753. {
  754. struct ehci_iso_stream *stream;
  755. stream = kzalloc(sizeof *stream, mem_flags);
  756. if (likely (stream != NULL)) {
  757. INIT_LIST_HEAD(&stream->td_list);
  758. INIT_LIST_HEAD(&stream->free_list);
  759. stream->next_uframe = -1;
  760. stream->refcount = 1;
  761. }
  762. return stream;
  763. }
  764. static void
  765. iso_stream_init (
  766. struct ehci_hcd *ehci,
  767. struct ehci_iso_stream *stream,
  768. struct usb_device *dev,
  769. int pipe,
  770. unsigned interval
  771. )
  772. {
  773. static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
  774. u32 buf1;
  775. unsigned epnum, maxp;
  776. int is_input;
  777. long bandwidth;
  778. /*
  779. * this might be a "high bandwidth" highspeed endpoint,
  780. * as encoded in the ep descriptor's wMaxPacket field
  781. */
  782. epnum = usb_pipeendpoint (pipe);
  783. is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
  784. maxp = usb_maxpacket(dev, pipe, !is_input);
  785. if (is_input) {
  786. buf1 = (1 << 11);
  787. } else {
  788. buf1 = 0;
  789. }
  790. /* knows about ITD vs SITD */
  791. if (dev->speed == USB_SPEED_HIGH) {
  792. unsigned multi = hb_mult(maxp);
  793. stream->highspeed = 1;
  794. maxp = max_packet(maxp);
  795. buf1 |= maxp;
  796. maxp *= multi;
  797. stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
  798. stream->buf1 = cpu_to_hc32(ehci, buf1);
  799. stream->buf2 = cpu_to_hc32(ehci, multi);
  800. /* usbfs wants to report the average usecs per frame tied up
  801. * when transfers on this endpoint are scheduled ...
  802. */
  803. stream->usecs = HS_USECS_ISO (maxp);
  804. bandwidth = stream->usecs * 8;
  805. bandwidth /= 1 << (interval - 1);
  806. } else {
  807. u32 addr;
  808. int think_time;
  809. int hs_transfers;
  810. addr = dev->ttport << 24;
  811. if (!ehci_is_TDI(ehci)
  812. || (dev->tt->hub !=
  813. ehci_to_hcd(ehci)->self.root_hub))
  814. addr |= dev->tt->hub->devnum << 16;
  815. addr |= epnum << 8;
  816. addr |= dev->devnum;
  817. stream->usecs = HS_USECS_ISO (maxp);
  818. think_time = dev->tt ? dev->tt->think_time : 0;
  819. stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
  820. dev->speed, is_input, 1, maxp));
  821. hs_transfers = max (1u, (maxp + 187) / 188);
  822. if (is_input) {
  823. u32 tmp;
  824. addr |= 1 << 31;
  825. stream->c_usecs = stream->usecs;
  826. stream->usecs = HS_USECS_ISO (1);
  827. stream->raw_mask = 1;
  828. /* c-mask as specified in USB 2.0 11.18.4 3.c */
  829. tmp = (1 << (hs_transfers + 2)) - 1;
  830. stream->raw_mask |= tmp << (8 + 2);
  831. } else
  832. stream->raw_mask = smask_out [hs_transfers - 1];
  833. bandwidth = stream->usecs + stream->c_usecs;
  834. bandwidth /= 1 << (interval + 2);
  835. /* stream->splits gets created from raw_mask later */
  836. stream->address = cpu_to_hc32(ehci, addr);
  837. }
  838. stream->bandwidth = bandwidth;
  839. stream->udev = dev;
  840. stream->bEndpointAddress = is_input | epnum;
  841. stream->interval = interval;
  842. stream->maxp = maxp;
  843. }
  844. static void
  845. iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
  846. {
  847. stream->refcount--;
  848. /* free whenever just a dev->ep reference remains.
  849. * not like a QH -- no persistent state (toggle, halt)
  850. */
  851. if (stream->refcount == 1) {
  852. int is_in;
  853. // BUG_ON (!list_empty(&stream->td_list));
  854. while (!list_empty (&stream->free_list)) {
  855. struct list_head *entry;
  856. entry = stream->free_list.next;
  857. list_del (entry);
  858. /* knows about ITD vs SITD */
  859. if (stream->highspeed) {
  860. struct ehci_itd *itd;
  861. itd = list_entry (entry, struct ehci_itd,
  862. itd_list);
  863. dma_pool_free (ehci->itd_pool, itd,
  864. itd->itd_dma);
  865. } else {
  866. struct ehci_sitd *sitd;
  867. sitd = list_entry (entry, struct ehci_sitd,
  868. sitd_list);
  869. dma_pool_free (ehci->sitd_pool, sitd,
  870. sitd->sitd_dma);
  871. }
  872. }
  873. is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
  874. stream->bEndpointAddress &= 0x0f;
  875. stream->ep->hcpriv = NULL;
  876. if (stream->rescheduled) {
  877. ehci_info (ehci, "ep%d%s-iso rescheduled "
  878. "%lu times in %lu seconds\n",
  879. stream->bEndpointAddress, is_in ? "in" : "out",
  880. stream->rescheduled,
  881. ((jiffies - stream->start)/HZ)
  882. );
  883. }
  884. kfree(stream);
  885. }
  886. }
  887. static inline struct ehci_iso_stream *
  888. iso_stream_get (struct ehci_iso_stream *stream)
  889. {
  890. if (likely (stream != NULL))
  891. stream->refcount++;
  892. return stream;
  893. }
  894. static struct ehci_iso_stream *
  895. iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
  896. {
  897. unsigned epnum;
  898. struct ehci_iso_stream *stream;
  899. struct usb_host_endpoint *ep;
  900. unsigned long flags;
  901. epnum = usb_pipeendpoint (urb->pipe);
  902. if (usb_pipein(urb->pipe))
  903. ep = urb->dev->ep_in[epnum];
  904. else
  905. ep = urb->dev->ep_out[epnum];
  906. spin_lock_irqsave (&ehci->lock, flags);
  907. stream = ep->hcpriv;
  908. if (unlikely (stream == NULL)) {
  909. stream = iso_stream_alloc(GFP_ATOMIC);
  910. if (likely (stream != NULL)) {
  911. /* dev->ep owns the initial refcount */
  912. ep->hcpriv = stream;
  913. stream->ep = ep;
  914. iso_stream_init(ehci, stream, urb->dev, urb->pipe,
  915. urb->interval);
  916. }
  917. /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
  918. } else if (unlikely (stream->hw_info1 != 0)) {
  919. ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
  920. urb->dev->devpath, epnum,
  921. usb_pipein(urb->pipe) ? "in" : "out");
  922. stream = NULL;
  923. }
  924. /* caller guarantees an eventual matching iso_stream_put */
  925. stream = iso_stream_get (stream);
  926. spin_unlock_irqrestore (&ehci->lock, flags);
  927. return stream;
  928. }
  929. /*-------------------------------------------------------------------------*/
  930. /* ehci_iso_sched ops can be ITD-only or SITD-only */
  931. static struct ehci_iso_sched *
  932. iso_sched_alloc (unsigned packets, gfp_t mem_flags)
  933. {
  934. struct ehci_iso_sched *iso_sched;
  935. int size = sizeof *iso_sched;
  936. size += packets * sizeof (struct ehci_iso_packet);
  937. iso_sched = kzalloc(size, mem_flags);
  938. if (likely (iso_sched != NULL)) {
  939. INIT_LIST_HEAD (&iso_sched->td_list);
  940. }
  941. return iso_sched;
  942. }
  943. static inline void
  944. itd_sched_init(
  945. struct ehci_hcd *ehci,
  946. struct ehci_iso_sched *iso_sched,
  947. struct ehci_iso_stream *stream,
  948. struct urb *urb
  949. )
  950. {
  951. unsigned i;
  952. dma_addr_t dma = urb->transfer_dma;
  953. /* how many uframes are needed for these transfers */
  954. iso_sched->span = urb->number_of_packets * stream->interval;
  955. /* figure out per-uframe itd fields that we'll need later
  956. * when we fit new itds into the schedule.
  957. */
  958. for (i = 0; i < urb->number_of_packets; i++) {
  959. struct ehci_iso_packet *uframe = &iso_sched->packet [i];
  960. unsigned length;
  961. dma_addr_t buf;
  962. u32 trans;
  963. length = urb->iso_frame_desc [i].length;
  964. buf = dma + urb->iso_frame_desc [i].offset;
  965. trans = EHCI_ISOC_ACTIVE;
  966. trans |= buf & 0x0fff;
  967. if (unlikely (((i + 1) == urb->number_of_packets))
  968. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  969. trans |= EHCI_ITD_IOC;
  970. trans |= length << 16;
  971. uframe->transaction = cpu_to_hc32(ehci, trans);
  972. /* might need to cross a buffer page within a uframe */
  973. uframe->bufp = (buf & ~(u64)0x0fff);
  974. buf += length;
  975. if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
  976. uframe->cross = 1;
  977. }
  978. }
  979. static void
  980. iso_sched_free (
  981. struct ehci_iso_stream *stream,
  982. struct ehci_iso_sched *iso_sched
  983. )
  984. {
  985. if (!iso_sched)
  986. return;
  987. // caller must hold ehci->lock!
  988. list_splice (&iso_sched->td_list, &stream->free_list);
  989. kfree (iso_sched);
  990. }
  991. static int
  992. itd_urb_transaction (
  993. struct ehci_iso_stream *stream,
  994. struct ehci_hcd *ehci,
  995. struct urb *urb,
  996. gfp_t mem_flags
  997. )
  998. {
  999. struct ehci_itd *itd;
  1000. dma_addr_t itd_dma;
  1001. int i;
  1002. unsigned num_itds;
  1003. struct ehci_iso_sched *sched;
  1004. unsigned long flags;
  1005. sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  1006. if (unlikely (sched == NULL))
  1007. return -ENOMEM;
  1008. itd_sched_init(ehci, sched, stream, urb);
  1009. if (urb->interval < 8)
  1010. num_itds = 1 + (sched->span + 7) / 8;
  1011. else
  1012. num_itds = urb->number_of_packets;
  1013. /* allocate/init ITDs */
  1014. spin_lock_irqsave (&ehci->lock, flags);
  1015. for (i = 0; i < num_itds; i++) {
  1016. /* free_list.next might be cache-hot ... but maybe
  1017. * the HC caches it too. avoid that issue for now.
  1018. */
  1019. /* prefer previously-allocated itds */
  1020. if (likely (!list_empty(&stream->free_list))) {
  1021. itd = list_entry (stream->free_list.prev,
  1022. struct ehci_itd, itd_list);
  1023. list_del (&itd->itd_list);
  1024. itd_dma = itd->itd_dma;
  1025. } else
  1026. itd = NULL;
  1027. if (!itd) {
  1028. spin_unlock_irqrestore (&ehci->lock, flags);
  1029. itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
  1030. &itd_dma);
  1031. spin_lock_irqsave (&ehci->lock, flags);
  1032. }
  1033. if (unlikely (NULL == itd)) {
  1034. iso_sched_free (stream, sched);
  1035. spin_unlock_irqrestore (&ehci->lock, flags);
  1036. return -ENOMEM;
  1037. }
  1038. memset (itd, 0, sizeof *itd);
  1039. itd->itd_dma = itd_dma;
  1040. list_add (&itd->itd_list, &sched->td_list);
  1041. }
  1042. spin_unlock_irqrestore (&ehci->lock, flags);
  1043. /* temporarily store schedule info in hcpriv */
  1044. urb->hcpriv = sched;
  1045. urb->error_count = 0;
  1046. return 0;
  1047. }
  1048. /*-------------------------------------------------------------------------*/
  1049. static inline int
  1050. itd_slot_ok (
  1051. struct ehci_hcd *ehci,
  1052. u32 mod,
  1053. u32 uframe,
  1054. u8 usecs,
  1055. u32 period
  1056. )
  1057. {
  1058. uframe %= period;
  1059. do {
  1060. /* can't commit more than 80% periodic == 100 usec */
  1061. if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
  1062. > (100 - usecs))
  1063. return 0;
  1064. /* we know urb->interval is 2^N uframes */
  1065. uframe += period;
  1066. } while (uframe < mod);
  1067. return 1;
  1068. }
  1069. static inline int
  1070. sitd_slot_ok (
  1071. struct ehci_hcd *ehci,
  1072. u32 mod,
  1073. struct ehci_iso_stream *stream,
  1074. u32 uframe,
  1075. struct ehci_iso_sched *sched,
  1076. u32 period_uframes
  1077. )
  1078. {
  1079. u32 mask, tmp;
  1080. u32 frame, uf;
  1081. mask = stream->raw_mask << (uframe & 7);
  1082. /* for IN, don't wrap CSPLIT into the next frame */
  1083. if (mask & ~0xffff)
  1084. return 0;
  1085. /* this multi-pass logic is simple, but performance may
  1086. * suffer when the schedule data isn't cached.
  1087. */
  1088. /* check bandwidth */
  1089. uframe %= period_uframes;
  1090. do {
  1091. u32 max_used;
  1092. frame = uframe >> 3;
  1093. uf = uframe & 7;
  1094. #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
  1095. /* The tt's fullspeed bus bandwidth must be available.
  1096. * tt_available scheduling guarantees 10+% for control/bulk.
  1097. */
  1098. if (!tt_available (ehci, period_uframes << 3,
  1099. stream->udev, frame, uf, stream->tt_usecs))
  1100. return 0;
  1101. #else
  1102. /* tt must be idle for start(s), any gap, and csplit.
  1103. * assume scheduling slop leaves 10+% for control/bulk.
  1104. */
  1105. if (!tt_no_collision (ehci, period_uframes << 3,
  1106. stream->udev, frame, mask))
  1107. return 0;
  1108. #endif
  1109. /* check starts (OUT uses more than one) */
  1110. max_used = 100 - stream->usecs;
  1111. for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
  1112. if (periodic_usecs (ehci, frame, uf) > max_used)
  1113. return 0;
  1114. }
  1115. /* for IN, check CSPLIT */
  1116. if (stream->c_usecs) {
  1117. uf = uframe & 7;
  1118. max_used = 100 - stream->c_usecs;
  1119. do {
  1120. tmp = 1 << uf;
  1121. tmp <<= 8;
  1122. if ((stream->raw_mask & tmp) == 0)
  1123. continue;
  1124. if (periodic_usecs (ehci, frame, uf)
  1125. > max_used)
  1126. return 0;
  1127. } while (++uf < 8);
  1128. }
  1129. /* we know urb->interval is 2^N uframes */
  1130. uframe += period_uframes;
  1131. } while (uframe < mod);
  1132. stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
  1133. return 1;
  1134. }
  1135. /*
  1136. * This scheduler plans almost as far into the future as it has actual
  1137. * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  1138. * "as small as possible" to be cache-friendlier.) That limits the size
  1139. * transfers you can stream reliably; avoid more than 64 msec per urb.
  1140. * Also avoid queue depths of less than ehci's worst irq latency (affected
  1141. * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  1142. * and other factors); or more than about 230 msec total (for portability,
  1143. * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
  1144. */
  1145. #define SCHEDULE_SLOP 10 /* frames */
  1146. static int
  1147. iso_stream_schedule (
  1148. struct ehci_hcd *ehci,
  1149. struct urb *urb,
  1150. struct ehci_iso_stream *stream
  1151. )
  1152. {
  1153. u32 now, start, max, period;
  1154. int status;
  1155. unsigned mod = ehci->periodic_size << 3;
  1156. struct ehci_iso_sched *sched = urb->hcpriv;
  1157. if (sched->span > (mod - 8 * SCHEDULE_SLOP)) {
  1158. ehci_dbg (ehci, "iso request %p too long\n", urb);
  1159. status = -EFBIG;
  1160. goto fail;
  1161. }
  1162. if ((stream->depth + sched->span) > mod) {
  1163. ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
  1164. urb, stream->depth, sched->span, mod);
  1165. status = -EFBIG;
  1166. goto fail;
  1167. }
  1168. now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
  1169. /* when's the last uframe this urb could start? */
  1170. max = now + mod;
  1171. /* typical case: reuse current schedule. stream is still active,
  1172. * and no gaps from host falling behind (irq delays etc)
  1173. */
  1174. if (likely (!list_empty (&stream->td_list))) {
  1175. start = stream->next_uframe;
  1176. if (start < now)
  1177. start += mod;
  1178. if (likely ((start + sched->span) < max))
  1179. goto ready;
  1180. /* else fell behind; someday, try to reschedule */
  1181. status = -EL2NSYNC;
  1182. goto fail;
  1183. }
  1184. /* need to schedule; when's the next (u)frame we could start?
  1185. * this is bigger than ehci->i_thresh allows; scheduling itself
  1186. * isn't free, the slop should handle reasonably slow cpus. it
  1187. * can also help high bandwidth if the dma and irq loads don't
  1188. * jump until after the queue is primed.
  1189. */
  1190. start = SCHEDULE_SLOP * 8 + (now & ~0x07);
  1191. start %= mod;
  1192. stream->next_uframe = start;
  1193. /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  1194. period = urb->interval;
  1195. if (!stream->highspeed)
  1196. period <<= 3;
  1197. /* find a uframe slot with enough bandwidth */
  1198. for (; start < (stream->next_uframe + period); start++) {
  1199. int enough_space;
  1200. /* check schedule: enough space? */
  1201. if (stream->highspeed)
  1202. enough_space = itd_slot_ok (ehci, mod, start,
  1203. stream->usecs, period);
  1204. else {
  1205. if ((start % 8) >= 6)
  1206. continue;
  1207. enough_space = sitd_slot_ok (ehci, mod, stream,
  1208. start, sched, period);
  1209. }
  1210. /* schedule it here if there's enough bandwidth */
  1211. if (enough_space) {
  1212. stream->next_uframe = start % mod;
  1213. goto ready;
  1214. }
  1215. }
  1216. /* no room in the schedule */
  1217. ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
  1218. list_empty (&stream->td_list) ? "" : "re",
  1219. urb, now, max);
  1220. status = -ENOSPC;
  1221. fail:
  1222. iso_sched_free (stream, sched);
  1223. urb->hcpriv = NULL;
  1224. return status;
  1225. ready:
  1226. /* report high speed start in uframes; full speed, in frames */
  1227. urb->start_frame = stream->next_uframe;
  1228. if (!stream->highspeed)
  1229. urb->start_frame >>= 3;
  1230. return 0;
  1231. }
  1232. /*-------------------------------------------------------------------------*/
  1233. static inline void
  1234. itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
  1235. struct ehci_itd *itd)
  1236. {
  1237. int i;
  1238. /* it's been recently zeroed */
  1239. itd->hw_next = EHCI_LIST_END(ehci);
  1240. itd->hw_bufp [0] = stream->buf0;
  1241. itd->hw_bufp [1] = stream->buf1;
  1242. itd->hw_bufp [2] = stream->buf2;
  1243. for (i = 0; i < 8; i++)
  1244. itd->index[i] = -1;
  1245. /* All other fields are filled when scheduling */
  1246. }
  1247. static inline void
  1248. itd_patch(
  1249. struct ehci_hcd *ehci,
  1250. struct ehci_itd *itd,
  1251. struct ehci_iso_sched *iso_sched,
  1252. unsigned index,
  1253. u16 uframe
  1254. )
  1255. {
  1256. struct ehci_iso_packet *uf = &iso_sched->packet [index];
  1257. unsigned pg = itd->pg;
  1258. // BUG_ON (pg == 6 && uf->cross);
  1259. uframe &= 0x07;
  1260. itd->index [uframe] = index;
  1261. itd->hw_transaction[uframe] = uf->transaction;
  1262. itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
  1263. itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
  1264. itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
  1265. /* iso_frame_desc[].offset must be strictly increasing */
  1266. if (unlikely (uf->cross)) {
  1267. u64 bufp = uf->bufp + 4096;
  1268. itd->pg = ++pg;
  1269. itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
  1270. itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
  1271. }
  1272. }
  1273. static inline void
  1274. itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
  1275. {
  1276. /* always prepend ITD/SITD ... only QH tree is order-sensitive */
  1277. itd->itd_next = ehci->pshadow [frame];
  1278. itd->hw_next = ehci->periodic [frame];
  1279. ehci->pshadow [frame].itd = itd;
  1280. itd->frame = frame;
  1281. wmb ();
  1282. ehci->periodic[frame] = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
  1283. }
  1284. /* fit urb's itds into the selected schedule slot; activate as needed */
  1285. static int
  1286. itd_link_urb (
  1287. struct ehci_hcd *ehci,
  1288. struct urb *urb,
  1289. unsigned mod,
  1290. struct ehci_iso_stream *stream
  1291. )
  1292. {
  1293. int packet;
  1294. unsigned next_uframe, uframe, frame;
  1295. struct ehci_iso_sched *iso_sched = urb->hcpriv;
  1296. struct ehci_itd *itd;
  1297. next_uframe = stream->next_uframe % mod;
  1298. if (unlikely (list_empty(&stream->td_list))) {
  1299. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1300. += stream->bandwidth;
  1301. ehci_vdbg (ehci,
  1302. "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  1303. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  1304. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  1305. urb->interval,
  1306. next_uframe >> 3, next_uframe & 0x7);
  1307. stream->start = jiffies;
  1308. }
  1309. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
  1310. /* fill iTDs uframe by uframe */
  1311. for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
  1312. if (itd == NULL) {
  1313. /* ASSERT: we have all necessary itds */
  1314. // BUG_ON (list_empty (&iso_sched->td_list));
  1315. /* ASSERT: no itds for this endpoint in this uframe */
  1316. itd = list_entry (iso_sched->td_list.next,
  1317. struct ehci_itd, itd_list);
  1318. list_move_tail (&itd->itd_list, &stream->td_list);
  1319. itd->stream = iso_stream_get (stream);
  1320. itd->urb = usb_get_urb (urb);
  1321. itd_init (ehci, stream, itd);
  1322. }
  1323. uframe = next_uframe & 0x07;
  1324. frame = next_uframe >> 3;
  1325. itd_patch(ehci, itd, iso_sched, packet, uframe);
  1326. next_uframe += stream->interval;
  1327. stream->depth += stream->interval;
  1328. next_uframe %= mod;
  1329. packet++;
  1330. /* link completed itds into the schedule */
  1331. if (((next_uframe >> 3) != frame)
  1332. || packet == urb->number_of_packets) {
  1333. itd_link (ehci, frame % ehci->periodic_size, itd);
  1334. itd = NULL;
  1335. }
  1336. }
  1337. stream->next_uframe = next_uframe;
  1338. /* don't need that schedule data any more */
  1339. iso_sched_free (stream, iso_sched);
  1340. urb->hcpriv = NULL;
  1341. timer_action (ehci, TIMER_IO_WATCHDOG);
  1342. if (unlikely (!ehci->periodic_sched++))
  1343. return enable_periodic (ehci);
  1344. return 0;
  1345. }
  1346. #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
  1347. /* Process and recycle a completed ITD. Return true iff its urb completed,
  1348. * and hence its completion callback probably added things to the hardware
  1349. * schedule.
  1350. *
  1351. * Note that we carefully avoid recycling this descriptor until after any
  1352. * completion callback runs, so that it won't be reused quickly. That is,
  1353. * assuming (a) no more than two urbs per frame on this endpoint, and also
  1354. * (b) only this endpoint's completions submit URBs. It seems some silicon
  1355. * corrupts things if you reuse completed descriptors very quickly...
  1356. */
  1357. static unsigned
  1358. itd_complete (
  1359. struct ehci_hcd *ehci,
  1360. struct ehci_itd *itd
  1361. ) {
  1362. struct urb *urb = itd->urb;
  1363. struct usb_iso_packet_descriptor *desc;
  1364. u32 t;
  1365. unsigned uframe;
  1366. int urb_index = -1;
  1367. struct ehci_iso_stream *stream = itd->stream;
  1368. struct usb_device *dev;
  1369. unsigned retval = false;
  1370. /* for each uframe with a packet */
  1371. for (uframe = 0; uframe < 8; uframe++) {
  1372. if (likely (itd->index[uframe] == -1))
  1373. continue;
  1374. urb_index = itd->index[uframe];
  1375. desc = &urb->iso_frame_desc [urb_index];
  1376. t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
  1377. itd->hw_transaction [uframe] = 0;
  1378. stream->depth -= stream->interval;
  1379. /* report transfer status */
  1380. if (unlikely (t & ISO_ERRS)) {
  1381. urb->error_count++;
  1382. if (t & EHCI_ISOC_BUF_ERR)
  1383. desc->status = usb_pipein (urb->pipe)
  1384. ? -ENOSR /* hc couldn't read */
  1385. : -ECOMM; /* hc couldn't write */
  1386. else if (t & EHCI_ISOC_BABBLE)
  1387. desc->status = -EOVERFLOW;
  1388. else /* (t & EHCI_ISOC_XACTERR) */
  1389. desc->status = -EPROTO;
  1390. /* HC need not update length with this error */
  1391. if (!(t & EHCI_ISOC_BABBLE))
  1392. desc->actual_length = EHCI_ITD_LENGTH (t);
  1393. } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
  1394. desc->status = 0;
  1395. desc->actual_length = EHCI_ITD_LENGTH (t);
  1396. }
  1397. }
  1398. /* handle completion now? */
  1399. if (likely ((urb_index + 1) != urb->number_of_packets))
  1400. goto done;
  1401. /* ASSERT: it's really the last itd for this urb
  1402. list_for_each_entry (itd, &stream->td_list, itd_list)
  1403. BUG_ON (itd->urb == urb);
  1404. */
  1405. /* give urb back to the driver ... can be out-of-order */
  1406. dev = urb->dev;
  1407. ehci_urb_done(ehci, urb, 0);
  1408. retval = true;
  1409. urb = NULL;
  1410. /* defer stopping schedule; completion can submit */
  1411. ehci->periodic_sched--;
  1412. if (unlikely (!ehci->periodic_sched))
  1413. (void) disable_periodic (ehci);
  1414. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1415. if (unlikely (list_empty (&stream->td_list))) {
  1416. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1417. -= stream->bandwidth;
  1418. ehci_vdbg (ehci,
  1419. "deschedule devp %s ep%d%s-iso\n",
  1420. dev->devpath, stream->bEndpointAddress & 0x0f,
  1421. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1422. }
  1423. iso_stream_put (ehci, stream);
  1424. /* OK to recycle this ITD now that its completion callback ran. */
  1425. done:
  1426. usb_put_urb(urb);
  1427. itd->urb = NULL;
  1428. itd->stream = NULL;
  1429. list_move(&itd->itd_list, &stream->free_list);
  1430. iso_stream_put(ehci, stream);
  1431. return retval;
  1432. }
  1433. /*-------------------------------------------------------------------------*/
  1434. static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1435. gfp_t mem_flags)
  1436. {
  1437. int status = -EINVAL;
  1438. unsigned long flags;
  1439. struct ehci_iso_stream *stream;
  1440. /* Get iso_stream head */
  1441. stream = iso_stream_find (ehci, urb);
  1442. if (unlikely (stream == NULL)) {
  1443. ehci_dbg (ehci, "can't get iso stream\n");
  1444. return -ENOMEM;
  1445. }
  1446. if (unlikely (urb->interval != stream->interval)) {
  1447. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1448. stream->interval, urb->interval);
  1449. goto done;
  1450. }
  1451. #ifdef EHCI_URB_TRACE
  1452. ehci_dbg (ehci,
  1453. "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
  1454. __FUNCTION__, urb->dev->devpath, urb,
  1455. usb_pipeendpoint (urb->pipe),
  1456. usb_pipein (urb->pipe) ? "in" : "out",
  1457. urb->transfer_buffer_length,
  1458. urb->number_of_packets, urb->interval,
  1459. stream);
  1460. #endif
  1461. /* allocate ITDs w/o locking anything */
  1462. status = itd_urb_transaction (stream, ehci, urb, mem_flags);
  1463. if (unlikely (status < 0)) {
  1464. ehci_dbg (ehci, "can't init itds\n");
  1465. goto done;
  1466. }
  1467. /* schedule ... need to lock */
  1468. spin_lock_irqsave (&ehci->lock, flags);
  1469. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  1470. &ehci_to_hcd(ehci)->flags))) {
  1471. status = -ESHUTDOWN;
  1472. goto done_not_linked;
  1473. }
  1474. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  1475. if (unlikely(status))
  1476. goto done_not_linked;
  1477. status = iso_stream_schedule(ehci, urb, stream);
  1478. if (likely (status == 0))
  1479. itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1480. else
  1481. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  1482. done_not_linked:
  1483. spin_unlock_irqrestore (&ehci->lock, flags);
  1484. done:
  1485. if (unlikely (status < 0))
  1486. iso_stream_put (ehci, stream);
  1487. return status;
  1488. }
  1489. #ifdef CONFIG_USB_EHCI_SPLIT_ISO
  1490. /*-------------------------------------------------------------------------*/
  1491. /*
  1492. * "Split ISO TDs" ... used for USB 1.1 devices going through the
  1493. * TTs in USB 2.0 hubs. These need microframe scheduling.
  1494. */
  1495. static inline void
  1496. sitd_sched_init(
  1497. struct ehci_hcd *ehci,
  1498. struct ehci_iso_sched *iso_sched,
  1499. struct ehci_iso_stream *stream,
  1500. struct urb *urb
  1501. )
  1502. {
  1503. unsigned i;
  1504. dma_addr_t dma = urb->transfer_dma;
  1505. /* how many frames are needed for these transfers */
  1506. iso_sched->span = urb->number_of_packets * stream->interval;
  1507. /* figure out per-frame sitd fields that we'll need later
  1508. * when we fit new sitds into the schedule.
  1509. */
  1510. for (i = 0; i < urb->number_of_packets; i++) {
  1511. struct ehci_iso_packet *packet = &iso_sched->packet [i];
  1512. unsigned length;
  1513. dma_addr_t buf;
  1514. u32 trans;
  1515. length = urb->iso_frame_desc [i].length & 0x03ff;
  1516. buf = dma + urb->iso_frame_desc [i].offset;
  1517. trans = SITD_STS_ACTIVE;
  1518. if (((i + 1) == urb->number_of_packets)
  1519. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  1520. trans |= SITD_IOC;
  1521. trans |= length << 16;
  1522. packet->transaction = cpu_to_hc32(ehci, trans);
  1523. /* might need to cross a buffer page within a td */
  1524. packet->bufp = buf;
  1525. packet->buf1 = (buf + length) & ~0x0fff;
  1526. if (packet->buf1 != (buf & ~(u64)0x0fff))
  1527. packet->cross = 1;
  1528. /* OUT uses multiple start-splits */
  1529. if (stream->bEndpointAddress & USB_DIR_IN)
  1530. continue;
  1531. length = (length + 187) / 188;
  1532. if (length > 1) /* BEGIN vs ALL */
  1533. length |= 1 << 3;
  1534. packet->buf1 |= length;
  1535. }
  1536. }
  1537. static int
  1538. sitd_urb_transaction (
  1539. struct ehci_iso_stream *stream,
  1540. struct ehci_hcd *ehci,
  1541. struct urb *urb,
  1542. gfp_t mem_flags
  1543. )
  1544. {
  1545. struct ehci_sitd *sitd;
  1546. dma_addr_t sitd_dma;
  1547. int i;
  1548. struct ehci_iso_sched *iso_sched;
  1549. unsigned long flags;
  1550. iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  1551. if (iso_sched == NULL)
  1552. return -ENOMEM;
  1553. sitd_sched_init(ehci, iso_sched, stream, urb);
  1554. /* allocate/init sITDs */
  1555. spin_lock_irqsave (&ehci->lock, flags);
  1556. for (i = 0; i < urb->number_of_packets; i++) {
  1557. /* NOTE: for now, we don't try to handle wraparound cases
  1558. * for IN (using sitd->hw_backpointer, like a FSTN), which
  1559. * means we never need two sitds for full speed packets.
  1560. */
  1561. /* free_list.next might be cache-hot ... but maybe
  1562. * the HC caches it too. avoid that issue for now.
  1563. */
  1564. /* prefer previously-allocated sitds */
  1565. if (!list_empty(&stream->free_list)) {
  1566. sitd = list_entry (stream->free_list.prev,
  1567. struct ehci_sitd, sitd_list);
  1568. list_del (&sitd->sitd_list);
  1569. sitd_dma = sitd->sitd_dma;
  1570. } else
  1571. sitd = NULL;
  1572. if (!sitd) {
  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. }
  1578. if (!sitd) {
  1579. iso_sched_free (stream, iso_sched);
  1580. spin_unlock_irqrestore (&ehci->lock, flags);
  1581. return -ENOMEM;
  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 */
  1736. dev = urb->dev;
  1737. ehci_urb_done(ehci, urb, 0);
  1738. retval = true;
  1739. urb = NULL;
  1740. /* defer stopping schedule; completion can submit */
  1741. ehci->periodic_sched--;
  1742. if (!ehci->periodic_sched)
  1743. (void) disable_periodic (ehci);
  1744. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1745. if (list_empty (&stream->td_list)) {
  1746. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1747. -= stream->bandwidth;
  1748. ehci_vdbg (ehci,
  1749. "deschedule devp %s ep%d%s-iso\n",
  1750. dev->devpath, stream->bEndpointAddress & 0x0f,
  1751. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1752. }
  1753. iso_stream_put (ehci, stream);
  1754. /* OK to recycle this SITD now that its completion callback ran. */
  1755. done:
  1756. usb_put_urb(urb);
  1757. sitd->urb = NULL;
  1758. sitd->stream = NULL;
  1759. list_move(&sitd->sitd_list, &stream->free_list);
  1760. iso_stream_put(ehci, stream);
  1761. return retval;
  1762. }
  1763. static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1764. gfp_t mem_flags)
  1765. {
  1766. int status = -EINVAL;
  1767. unsigned long flags;
  1768. struct ehci_iso_stream *stream;
  1769. /* Get iso_stream head */
  1770. stream = iso_stream_find (ehci, urb);
  1771. if (stream == NULL) {
  1772. ehci_dbg (ehci, "can't get iso stream\n");
  1773. return -ENOMEM;
  1774. }
  1775. if (urb->interval != stream->interval) {
  1776. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1777. stream->interval, urb->interval);
  1778. goto done;
  1779. }
  1780. #ifdef EHCI_URB_TRACE
  1781. ehci_dbg (ehci,
  1782. "submit %p dev%s ep%d%s-iso len %d\n",
  1783. urb, urb->dev->devpath,
  1784. usb_pipeendpoint (urb->pipe),
  1785. usb_pipein (urb->pipe) ? "in" : "out",
  1786. urb->transfer_buffer_length);
  1787. #endif
  1788. /* allocate SITDs */
  1789. status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
  1790. if (status < 0) {
  1791. ehci_dbg (ehci, "can't init sitds\n");
  1792. goto done;
  1793. }
  1794. /* schedule ... need to lock */
  1795. spin_lock_irqsave (&ehci->lock, flags);
  1796. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  1797. &ehci_to_hcd(ehci)->flags))) {
  1798. status = -ESHUTDOWN;
  1799. goto done_not_linked;
  1800. }
  1801. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  1802. if (unlikely(status))
  1803. goto done_not_linked;
  1804. status = iso_stream_schedule(ehci, urb, stream);
  1805. if (status == 0)
  1806. sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1807. else
  1808. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  1809. done_not_linked:
  1810. spin_unlock_irqrestore (&ehci->lock, flags);
  1811. done:
  1812. if (status < 0)
  1813. iso_stream_put (ehci, stream);
  1814. return status;
  1815. }
  1816. #else
  1817. static inline int
  1818. sitd_submit (struct ehci_hcd *ehci, struct urb *urb, gfp_t mem_flags)
  1819. {
  1820. ehci_dbg (ehci, "split iso support is disabled\n");
  1821. return -ENOSYS;
  1822. }
  1823. static inline unsigned
  1824. sitd_complete (
  1825. struct ehci_hcd *ehci,
  1826. struct ehci_sitd *sitd
  1827. ) {
  1828. ehci_err (ehci, "sitd_complete %p?\n", sitd);
  1829. return 0;
  1830. }
  1831. #endif /* USB_EHCI_SPLIT_ISO */
  1832. /*-------------------------------------------------------------------------*/
  1833. static void
  1834. scan_periodic (struct ehci_hcd *ehci)
  1835. {
  1836. unsigned frame, clock, now_uframe, mod;
  1837. unsigned modified;
  1838. mod = ehci->periodic_size << 3;
  1839. /*
  1840. * When running, scan from last scan point up to "now"
  1841. * else clean up by scanning everything that's left.
  1842. * Touches as few pages as possible: cache-friendly.
  1843. */
  1844. now_uframe = ehci->next_uframe;
  1845. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  1846. clock = ehci_readl(ehci, &ehci->regs->frame_index);
  1847. else
  1848. clock = now_uframe + mod - 1;
  1849. clock %= mod;
  1850. for (;;) {
  1851. union ehci_shadow q, *q_p;
  1852. __hc32 type, *hw_p;
  1853. unsigned uframes;
  1854. /* don't scan past the live uframe */
  1855. frame = now_uframe >> 3;
  1856. if (frame == (clock >> 3))
  1857. uframes = now_uframe & 0x07;
  1858. else {
  1859. /* safe to scan the whole frame at once */
  1860. now_uframe |= 0x07;
  1861. uframes = 8;
  1862. }
  1863. restart:
  1864. /* scan each element in frame's queue for completions */
  1865. q_p = &ehci->pshadow [frame];
  1866. hw_p = &ehci->periodic [frame];
  1867. q.ptr = q_p->ptr;
  1868. type = Q_NEXT_TYPE(ehci, *hw_p);
  1869. modified = 0;
  1870. while (q.ptr != NULL) {
  1871. unsigned uf;
  1872. union ehci_shadow temp;
  1873. int live;
  1874. live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
  1875. switch (hc32_to_cpu(ehci, type)) {
  1876. case Q_TYPE_QH:
  1877. /* handle any completions */
  1878. temp.qh = qh_get (q.qh);
  1879. type = Q_NEXT_TYPE(ehci, q.qh->hw_next);
  1880. q = q.qh->qh_next;
  1881. modified = qh_completions (ehci, temp.qh);
  1882. if (unlikely (list_empty (&temp.qh->qtd_list)))
  1883. intr_deschedule (ehci, temp.qh);
  1884. qh_put (temp.qh);
  1885. break;
  1886. case Q_TYPE_FSTN:
  1887. /* for "save place" FSTNs, look at QH entries
  1888. * in the previous frame for completions.
  1889. */
  1890. if (q.fstn->hw_prev != EHCI_LIST_END(ehci)) {
  1891. dbg ("ignoring completions from FSTNs");
  1892. }
  1893. type = Q_NEXT_TYPE(ehci, q.fstn->hw_next);
  1894. q = q.fstn->fstn_next;
  1895. break;
  1896. case Q_TYPE_ITD:
  1897. /* skip itds for later in the frame */
  1898. rmb ();
  1899. for (uf = live ? uframes : 8; uf < 8; uf++) {
  1900. if (0 == (q.itd->hw_transaction [uf]
  1901. & ITD_ACTIVE(ehci)))
  1902. continue;
  1903. q_p = &q.itd->itd_next;
  1904. hw_p = &q.itd->hw_next;
  1905. type = Q_NEXT_TYPE(ehci,
  1906. q.itd->hw_next);
  1907. q = *q_p;
  1908. break;
  1909. }
  1910. if (uf != 8)
  1911. break;
  1912. /* this one's ready ... HC won't cache the
  1913. * pointer for much longer, if at all.
  1914. */
  1915. *q_p = q.itd->itd_next;
  1916. *hw_p = q.itd->hw_next;
  1917. type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
  1918. wmb();
  1919. modified = itd_complete (ehci, q.itd);
  1920. q = *q_p;
  1921. break;
  1922. case Q_TYPE_SITD:
  1923. if ((q.sitd->hw_results & SITD_ACTIVE(ehci))
  1924. && live) {
  1925. q_p = &q.sitd->sitd_next;
  1926. hw_p = &q.sitd->hw_next;
  1927. type = Q_NEXT_TYPE(ehci,
  1928. q.sitd->hw_next);
  1929. q = *q_p;
  1930. break;
  1931. }
  1932. *q_p = q.sitd->sitd_next;
  1933. *hw_p = q.sitd->hw_next;
  1934. type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
  1935. wmb();
  1936. modified = sitd_complete (ehci, q.sitd);
  1937. q = *q_p;
  1938. break;
  1939. default:
  1940. dbg ("corrupt type %d frame %d shadow %p",
  1941. type, frame, q.ptr);
  1942. // BUG ();
  1943. q.ptr = NULL;
  1944. }
  1945. /* assume completion callbacks modify the queue */
  1946. if (unlikely (modified))
  1947. goto restart;
  1948. }
  1949. /* stop when we catch up to the HC */
  1950. // FIXME: this assumes we won't get lapped when
  1951. // latencies climb; that should be rare, but...
  1952. // detect it, and just go all the way around.
  1953. // FLR might help detect this case, so long as latencies
  1954. // don't exceed periodic_size msec (default 1.024 sec).
  1955. // FIXME: likewise assumes HC doesn't halt mid-scan
  1956. if (now_uframe == clock) {
  1957. unsigned now;
  1958. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  1959. break;
  1960. ehci->next_uframe = now_uframe;
  1961. now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
  1962. if (now_uframe == now)
  1963. break;
  1964. /* rescan the rest of this frame, then ... */
  1965. clock = now;
  1966. } else {
  1967. now_uframe++;
  1968. now_uframe %= mod;
  1969. }
  1970. }
  1971. }