ehci-sched.c 50 KB

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