ehci-sched.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  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. gfp_t 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. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  530. &ehci_to_hcd(ehci)->flags))) {
  531. status = -ESHUTDOWN;
  532. goto done;
  533. }
  534. /* get qh and force any scheduling errors */
  535. INIT_LIST_HEAD (&empty);
  536. qh = qh_append_tds (ehci, urb, &empty, epnum, &ep->hcpriv);
  537. if (qh == NULL) {
  538. status = -ENOMEM;
  539. goto done;
  540. }
  541. if (qh->qh_state == QH_STATE_IDLE) {
  542. if ((status = qh_schedule (ehci, qh)) != 0)
  543. goto done;
  544. }
  545. /* then queue the urb's tds to the qh */
  546. qh = qh_append_tds (ehci, urb, qtd_list, epnum, &ep->hcpriv);
  547. BUG_ON (qh == NULL);
  548. /* ... update usbfs periodic stats */
  549. ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
  550. done:
  551. spin_unlock_irqrestore (&ehci->lock, flags);
  552. if (status)
  553. qtd_list_free (ehci, urb, qtd_list);
  554. return status;
  555. }
  556. /*-------------------------------------------------------------------------*/
  557. /* ehci_iso_stream ops work with both ITD and SITD */
  558. static struct ehci_iso_stream *
  559. iso_stream_alloc (gfp_t mem_flags)
  560. {
  561. struct ehci_iso_stream *stream;
  562. stream = kzalloc(sizeof *stream, mem_flags);
  563. if (likely (stream != NULL)) {
  564. INIT_LIST_HEAD(&stream->td_list);
  565. INIT_LIST_HEAD(&stream->free_list);
  566. stream->next_uframe = -1;
  567. stream->refcount = 1;
  568. }
  569. return stream;
  570. }
  571. static void
  572. iso_stream_init (
  573. struct ehci_hcd *ehci,
  574. struct ehci_iso_stream *stream,
  575. struct usb_device *dev,
  576. int pipe,
  577. unsigned interval
  578. )
  579. {
  580. static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };
  581. u32 buf1;
  582. unsigned epnum, maxp;
  583. int is_input;
  584. long bandwidth;
  585. /*
  586. * this might be a "high bandwidth" highspeed endpoint,
  587. * as encoded in the ep descriptor's wMaxPacket field
  588. */
  589. epnum = usb_pipeendpoint (pipe);
  590. is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
  591. maxp = usb_maxpacket(dev, pipe, !is_input);
  592. if (is_input) {
  593. buf1 = (1 << 11);
  594. } else {
  595. buf1 = 0;
  596. }
  597. /* knows about ITD vs SITD */
  598. if (dev->speed == USB_SPEED_HIGH) {
  599. unsigned multi = hb_mult(maxp);
  600. stream->highspeed = 1;
  601. maxp = max_packet(maxp);
  602. buf1 |= maxp;
  603. maxp *= multi;
  604. stream->buf0 = cpu_to_le32 ((epnum << 8) | dev->devnum);
  605. stream->buf1 = cpu_to_le32 (buf1);
  606. stream->buf2 = cpu_to_le32 (multi);
  607. /* usbfs wants to report the average usecs per frame tied up
  608. * when transfers on this endpoint are scheduled ...
  609. */
  610. stream->usecs = HS_USECS_ISO (maxp);
  611. bandwidth = stream->usecs * 8;
  612. bandwidth /= 1 << (interval - 1);
  613. } else {
  614. u32 addr;
  615. int think_time;
  616. int hs_transfers;
  617. addr = dev->ttport << 24;
  618. if (!ehci_is_TDI(ehci)
  619. || (dev->tt->hub !=
  620. ehci_to_hcd(ehci)->self.root_hub))
  621. addr |= dev->tt->hub->devnum << 16;
  622. addr |= epnum << 8;
  623. addr |= dev->devnum;
  624. stream->usecs = HS_USECS_ISO (maxp);
  625. think_time = dev->tt ? dev->tt->think_time : 0;
  626. stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
  627. dev->speed, is_input, 1, maxp));
  628. hs_transfers = max (1u, (maxp + 187) / 188);
  629. if (is_input) {
  630. u32 tmp;
  631. addr |= 1 << 31;
  632. stream->c_usecs = stream->usecs;
  633. stream->usecs = HS_USECS_ISO (1);
  634. stream->raw_mask = 1;
  635. /* c-mask as specified in USB 2.0 11.18.4 3.c */
  636. tmp = (1 << (hs_transfers + 2)) - 1;
  637. stream->raw_mask |= tmp << (8 + 2);
  638. } else
  639. stream->raw_mask = smask_out [hs_transfers - 1];
  640. bandwidth = stream->usecs + stream->c_usecs;
  641. bandwidth /= 1 << (interval + 2);
  642. /* stream->splits gets created from raw_mask later */
  643. stream->address = cpu_to_le32 (addr);
  644. }
  645. stream->bandwidth = bandwidth;
  646. stream->udev = dev;
  647. stream->bEndpointAddress = is_input | epnum;
  648. stream->interval = interval;
  649. stream->maxp = maxp;
  650. }
  651. static void
  652. iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
  653. {
  654. stream->refcount--;
  655. /* free whenever just a dev->ep reference remains.
  656. * not like a QH -- no persistent state (toggle, halt)
  657. */
  658. if (stream->refcount == 1) {
  659. int is_in;
  660. // BUG_ON (!list_empty(&stream->td_list));
  661. while (!list_empty (&stream->free_list)) {
  662. struct list_head *entry;
  663. entry = stream->free_list.next;
  664. list_del (entry);
  665. /* knows about ITD vs SITD */
  666. if (stream->highspeed) {
  667. struct ehci_itd *itd;
  668. itd = list_entry (entry, struct ehci_itd,
  669. itd_list);
  670. dma_pool_free (ehci->itd_pool, itd,
  671. itd->itd_dma);
  672. } else {
  673. struct ehci_sitd *sitd;
  674. sitd = list_entry (entry, struct ehci_sitd,
  675. sitd_list);
  676. dma_pool_free (ehci->sitd_pool, sitd,
  677. sitd->sitd_dma);
  678. }
  679. }
  680. is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
  681. stream->bEndpointAddress &= 0x0f;
  682. stream->ep->hcpriv = NULL;
  683. if (stream->rescheduled) {
  684. ehci_info (ehci, "ep%d%s-iso rescheduled "
  685. "%lu times in %lu seconds\n",
  686. stream->bEndpointAddress, is_in ? "in" : "out",
  687. stream->rescheduled,
  688. ((jiffies - stream->start)/HZ)
  689. );
  690. }
  691. kfree(stream);
  692. }
  693. }
  694. static inline struct ehci_iso_stream *
  695. iso_stream_get (struct ehci_iso_stream *stream)
  696. {
  697. if (likely (stream != NULL))
  698. stream->refcount++;
  699. return stream;
  700. }
  701. static struct ehci_iso_stream *
  702. iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
  703. {
  704. unsigned epnum;
  705. struct ehci_iso_stream *stream;
  706. struct usb_host_endpoint *ep;
  707. unsigned long flags;
  708. epnum = usb_pipeendpoint (urb->pipe);
  709. if (usb_pipein(urb->pipe))
  710. ep = urb->dev->ep_in[epnum];
  711. else
  712. ep = urb->dev->ep_out[epnum];
  713. spin_lock_irqsave (&ehci->lock, flags);
  714. stream = ep->hcpriv;
  715. if (unlikely (stream == NULL)) {
  716. stream = iso_stream_alloc(GFP_ATOMIC);
  717. if (likely (stream != NULL)) {
  718. /* dev->ep owns the initial refcount */
  719. ep->hcpriv = stream;
  720. stream->ep = ep;
  721. iso_stream_init(ehci, stream, urb->dev, urb->pipe,
  722. urb->interval);
  723. }
  724. /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
  725. } else if (unlikely (stream->hw_info1 != 0)) {
  726. ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
  727. urb->dev->devpath, epnum,
  728. usb_pipein(urb->pipe) ? "in" : "out");
  729. stream = NULL;
  730. }
  731. /* caller guarantees an eventual matching iso_stream_put */
  732. stream = iso_stream_get (stream);
  733. spin_unlock_irqrestore (&ehci->lock, flags);
  734. return stream;
  735. }
  736. /*-------------------------------------------------------------------------*/
  737. /* ehci_iso_sched ops can be ITD-only or SITD-only */
  738. static struct ehci_iso_sched *
  739. iso_sched_alloc (unsigned packets, gfp_t mem_flags)
  740. {
  741. struct ehci_iso_sched *iso_sched;
  742. int size = sizeof *iso_sched;
  743. size += packets * sizeof (struct ehci_iso_packet);
  744. iso_sched = kzalloc(size, mem_flags);
  745. if (likely (iso_sched != NULL)) {
  746. INIT_LIST_HEAD (&iso_sched->td_list);
  747. }
  748. return iso_sched;
  749. }
  750. static inline void
  751. itd_sched_init (
  752. struct ehci_iso_sched *iso_sched,
  753. struct ehci_iso_stream *stream,
  754. struct urb *urb
  755. )
  756. {
  757. unsigned i;
  758. dma_addr_t dma = urb->transfer_dma;
  759. /* how many uframes are needed for these transfers */
  760. iso_sched->span = urb->number_of_packets * stream->interval;
  761. /* figure out per-uframe itd fields that we'll need later
  762. * when we fit new itds into the schedule.
  763. */
  764. for (i = 0; i < urb->number_of_packets; i++) {
  765. struct ehci_iso_packet *uframe = &iso_sched->packet [i];
  766. unsigned length;
  767. dma_addr_t buf;
  768. u32 trans;
  769. length = urb->iso_frame_desc [i].length;
  770. buf = dma + urb->iso_frame_desc [i].offset;
  771. trans = EHCI_ISOC_ACTIVE;
  772. trans |= buf & 0x0fff;
  773. if (unlikely (((i + 1) == urb->number_of_packets))
  774. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  775. trans |= EHCI_ITD_IOC;
  776. trans |= length << 16;
  777. uframe->transaction = cpu_to_le32 (trans);
  778. /* might need to cross a buffer page within a uframe */
  779. uframe->bufp = (buf & ~(u64)0x0fff);
  780. buf += length;
  781. if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
  782. uframe->cross = 1;
  783. }
  784. }
  785. static void
  786. iso_sched_free (
  787. struct ehci_iso_stream *stream,
  788. struct ehci_iso_sched *iso_sched
  789. )
  790. {
  791. if (!iso_sched)
  792. return;
  793. // caller must hold ehci->lock!
  794. list_splice (&iso_sched->td_list, &stream->free_list);
  795. kfree (iso_sched);
  796. }
  797. static int
  798. itd_urb_transaction (
  799. struct ehci_iso_stream *stream,
  800. struct ehci_hcd *ehci,
  801. struct urb *urb,
  802. gfp_t mem_flags
  803. )
  804. {
  805. struct ehci_itd *itd;
  806. dma_addr_t itd_dma;
  807. int i;
  808. unsigned num_itds;
  809. struct ehci_iso_sched *sched;
  810. unsigned long flags;
  811. sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  812. if (unlikely (sched == NULL))
  813. return -ENOMEM;
  814. itd_sched_init (sched, stream, urb);
  815. if (urb->interval < 8)
  816. num_itds = 1 + (sched->span + 7) / 8;
  817. else
  818. num_itds = urb->number_of_packets;
  819. /* allocate/init ITDs */
  820. spin_lock_irqsave (&ehci->lock, flags);
  821. for (i = 0; i < num_itds; i++) {
  822. /* free_list.next might be cache-hot ... but maybe
  823. * the HC caches it too. avoid that issue for now.
  824. */
  825. /* prefer previously-allocated itds */
  826. if (likely (!list_empty(&stream->free_list))) {
  827. itd = list_entry (stream->free_list.prev,
  828. struct ehci_itd, itd_list);
  829. list_del (&itd->itd_list);
  830. itd_dma = itd->itd_dma;
  831. } else
  832. itd = NULL;
  833. if (!itd) {
  834. spin_unlock_irqrestore (&ehci->lock, flags);
  835. itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
  836. &itd_dma);
  837. spin_lock_irqsave (&ehci->lock, flags);
  838. }
  839. if (unlikely (NULL == itd)) {
  840. iso_sched_free (stream, sched);
  841. spin_unlock_irqrestore (&ehci->lock, flags);
  842. return -ENOMEM;
  843. }
  844. memset (itd, 0, sizeof *itd);
  845. itd->itd_dma = itd_dma;
  846. list_add (&itd->itd_list, &sched->td_list);
  847. }
  848. spin_unlock_irqrestore (&ehci->lock, flags);
  849. /* temporarily store schedule info in hcpriv */
  850. urb->hcpriv = sched;
  851. urb->error_count = 0;
  852. return 0;
  853. }
  854. /*-------------------------------------------------------------------------*/
  855. static inline int
  856. itd_slot_ok (
  857. struct ehci_hcd *ehci,
  858. u32 mod,
  859. u32 uframe,
  860. u8 usecs,
  861. u32 period
  862. )
  863. {
  864. uframe %= period;
  865. do {
  866. /* can't commit more than 80% periodic == 100 usec */
  867. if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
  868. > (100 - usecs))
  869. return 0;
  870. /* we know urb->interval is 2^N uframes */
  871. uframe += period;
  872. } while (uframe < mod);
  873. return 1;
  874. }
  875. static inline int
  876. sitd_slot_ok (
  877. struct ehci_hcd *ehci,
  878. u32 mod,
  879. struct ehci_iso_stream *stream,
  880. u32 uframe,
  881. struct ehci_iso_sched *sched,
  882. u32 period_uframes
  883. )
  884. {
  885. u32 mask, tmp;
  886. u32 frame, uf;
  887. mask = stream->raw_mask << (uframe & 7);
  888. /* for IN, don't wrap CSPLIT into the next frame */
  889. if (mask & ~0xffff)
  890. return 0;
  891. /* this multi-pass logic is simple, but performance may
  892. * suffer when the schedule data isn't cached.
  893. */
  894. /* check bandwidth */
  895. uframe %= period_uframes;
  896. do {
  897. u32 max_used;
  898. frame = uframe >> 3;
  899. uf = uframe & 7;
  900. /* tt must be idle for start(s), any gap, and csplit.
  901. * assume scheduling slop leaves 10+% for control/bulk.
  902. */
  903. if (!tt_no_collision (ehci, period_uframes << 3,
  904. stream->udev, frame, mask))
  905. return 0;
  906. /* check starts (OUT uses more than one) */
  907. max_used = 100 - stream->usecs;
  908. for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
  909. if (periodic_usecs (ehci, frame, uf) > max_used)
  910. return 0;
  911. }
  912. /* for IN, check CSPLIT */
  913. if (stream->c_usecs) {
  914. uf = uframe & 7;
  915. max_used = 100 - stream->c_usecs;
  916. do {
  917. tmp = 1 << uf;
  918. tmp <<= 8;
  919. if ((stream->raw_mask & tmp) == 0)
  920. continue;
  921. if (periodic_usecs (ehci, frame, uf)
  922. > max_used)
  923. return 0;
  924. } while (++uf < 8);
  925. }
  926. /* we know urb->interval is 2^N uframes */
  927. uframe += period_uframes;
  928. } while (uframe < mod);
  929. stream->splits = cpu_to_le32(stream->raw_mask << (uframe & 7));
  930. return 1;
  931. }
  932. /*
  933. * This scheduler plans almost as far into the future as it has actual
  934. * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
  935. * "as small as possible" to be cache-friendlier.) That limits the size
  936. * transfers you can stream reliably; avoid more than 64 msec per urb.
  937. * Also avoid queue depths of less than ehci's worst irq latency (affected
  938. * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
  939. * and other factors); or more than about 230 msec total (for portability,
  940. * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
  941. */
  942. #define SCHEDULE_SLOP 10 /* frames */
  943. static int
  944. iso_stream_schedule (
  945. struct ehci_hcd *ehci,
  946. struct urb *urb,
  947. struct ehci_iso_stream *stream
  948. )
  949. {
  950. u32 now, start, max, period;
  951. int status;
  952. unsigned mod = ehci->periodic_size << 3;
  953. struct ehci_iso_sched *sched = urb->hcpriv;
  954. if (sched->span > (mod - 8 * SCHEDULE_SLOP)) {
  955. ehci_dbg (ehci, "iso request %p too long\n", urb);
  956. status = -EFBIG;
  957. goto fail;
  958. }
  959. if ((stream->depth + sched->span) > mod) {
  960. ehci_dbg (ehci, "request %p would overflow (%d+%d>%d)\n",
  961. urb, stream->depth, sched->span, mod);
  962. status = -EFBIG;
  963. goto fail;
  964. }
  965. now = readl (&ehci->regs->frame_index) % mod;
  966. /* when's the last uframe this urb could start? */
  967. max = now + mod;
  968. /* typical case: reuse current schedule. stream is still active,
  969. * and no gaps from host falling behind (irq delays etc)
  970. */
  971. if (likely (!list_empty (&stream->td_list))) {
  972. start = stream->next_uframe;
  973. if (start < now)
  974. start += mod;
  975. if (likely ((start + sched->span) < max))
  976. goto ready;
  977. /* else fell behind; someday, try to reschedule */
  978. status = -EL2NSYNC;
  979. goto fail;
  980. }
  981. /* need to schedule; when's the next (u)frame we could start?
  982. * this is bigger than ehci->i_thresh allows; scheduling itself
  983. * isn't free, the slop should handle reasonably slow cpus. it
  984. * can also help high bandwidth if the dma and irq loads don't
  985. * jump until after the queue is primed.
  986. */
  987. start = SCHEDULE_SLOP * 8 + (now & ~0x07);
  988. start %= mod;
  989. stream->next_uframe = start;
  990. /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
  991. period = urb->interval;
  992. if (!stream->highspeed)
  993. period <<= 3;
  994. /* find a uframe slot with enough bandwidth */
  995. for (; start < (stream->next_uframe + period); start++) {
  996. int enough_space;
  997. /* check schedule: enough space? */
  998. if (stream->highspeed)
  999. enough_space = itd_slot_ok (ehci, mod, start,
  1000. stream->usecs, period);
  1001. else {
  1002. if ((start % 8) >= 6)
  1003. continue;
  1004. enough_space = sitd_slot_ok (ehci, mod, stream,
  1005. start, sched, period);
  1006. }
  1007. /* schedule it here if there's enough bandwidth */
  1008. if (enough_space) {
  1009. stream->next_uframe = start % mod;
  1010. goto ready;
  1011. }
  1012. }
  1013. /* no room in the schedule */
  1014. ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
  1015. list_empty (&stream->td_list) ? "" : "re",
  1016. urb, now, max);
  1017. status = -ENOSPC;
  1018. fail:
  1019. iso_sched_free (stream, sched);
  1020. urb->hcpriv = NULL;
  1021. return status;
  1022. ready:
  1023. /* report high speed start in uframes; full speed, in frames */
  1024. urb->start_frame = stream->next_uframe;
  1025. if (!stream->highspeed)
  1026. urb->start_frame >>= 3;
  1027. return 0;
  1028. }
  1029. /*-------------------------------------------------------------------------*/
  1030. static inline void
  1031. itd_init (struct ehci_iso_stream *stream, struct ehci_itd *itd)
  1032. {
  1033. int i;
  1034. /* it's been recently zeroed */
  1035. itd->hw_next = EHCI_LIST_END;
  1036. itd->hw_bufp [0] = stream->buf0;
  1037. itd->hw_bufp [1] = stream->buf1;
  1038. itd->hw_bufp [2] = stream->buf2;
  1039. for (i = 0; i < 8; i++)
  1040. itd->index[i] = -1;
  1041. /* All other fields are filled when scheduling */
  1042. }
  1043. static inline void
  1044. itd_patch (
  1045. struct ehci_itd *itd,
  1046. struct ehci_iso_sched *iso_sched,
  1047. unsigned index,
  1048. u16 uframe
  1049. )
  1050. {
  1051. struct ehci_iso_packet *uf = &iso_sched->packet [index];
  1052. unsigned pg = itd->pg;
  1053. // BUG_ON (pg == 6 && uf->cross);
  1054. uframe &= 0x07;
  1055. itd->index [uframe] = index;
  1056. itd->hw_transaction [uframe] = uf->transaction;
  1057. itd->hw_transaction [uframe] |= cpu_to_le32 (pg << 12);
  1058. itd->hw_bufp [pg] |= cpu_to_le32 (uf->bufp & ~(u32)0);
  1059. itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(uf->bufp >> 32));
  1060. /* iso_frame_desc[].offset must be strictly increasing */
  1061. if (unlikely (uf->cross)) {
  1062. u64 bufp = uf->bufp + 4096;
  1063. itd->pg = ++pg;
  1064. itd->hw_bufp [pg] |= cpu_to_le32 (bufp & ~(u32)0);
  1065. itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(bufp >> 32));
  1066. }
  1067. }
  1068. static inline void
  1069. itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
  1070. {
  1071. /* always prepend ITD/SITD ... only QH tree is order-sensitive */
  1072. itd->itd_next = ehci->pshadow [frame];
  1073. itd->hw_next = ehci->periodic [frame];
  1074. ehci->pshadow [frame].itd = itd;
  1075. itd->frame = frame;
  1076. wmb ();
  1077. ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;
  1078. }
  1079. /* fit urb's itds into the selected schedule slot; activate as needed */
  1080. static int
  1081. itd_link_urb (
  1082. struct ehci_hcd *ehci,
  1083. struct urb *urb,
  1084. unsigned mod,
  1085. struct ehci_iso_stream *stream
  1086. )
  1087. {
  1088. int packet;
  1089. unsigned next_uframe, uframe, frame;
  1090. struct ehci_iso_sched *iso_sched = urb->hcpriv;
  1091. struct ehci_itd *itd;
  1092. next_uframe = stream->next_uframe % mod;
  1093. if (unlikely (list_empty(&stream->td_list))) {
  1094. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1095. += stream->bandwidth;
  1096. ehci_vdbg (ehci,
  1097. "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
  1098. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  1099. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  1100. urb->interval,
  1101. next_uframe >> 3, next_uframe & 0x7);
  1102. stream->start = jiffies;
  1103. }
  1104. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
  1105. /* fill iTDs uframe by uframe */
  1106. for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
  1107. if (itd == NULL) {
  1108. /* ASSERT: we have all necessary itds */
  1109. // BUG_ON (list_empty (&iso_sched->td_list));
  1110. /* ASSERT: no itds for this endpoint in this uframe */
  1111. itd = list_entry (iso_sched->td_list.next,
  1112. struct ehci_itd, itd_list);
  1113. list_move_tail (&itd->itd_list, &stream->td_list);
  1114. itd->stream = iso_stream_get (stream);
  1115. itd->urb = usb_get_urb (urb);
  1116. itd_init (stream, itd);
  1117. }
  1118. uframe = next_uframe & 0x07;
  1119. frame = next_uframe >> 3;
  1120. itd->usecs [uframe] = stream->usecs;
  1121. itd_patch (itd, iso_sched, packet, uframe);
  1122. next_uframe += stream->interval;
  1123. stream->depth += stream->interval;
  1124. next_uframe %= mod;
  1125. packet++;
  1126. /* link completed itds into the schedule */
  1127. if (((next_uframe >> 3) != frame)
  1128. || packet == urb->number_of_packets) {
  1129. itd_link (ehci, frame % ehci->periodic_size, itd);
  1130. itd = NULL;
  1131. }
  1132. }
  1133. stream->next_uframe = next_uframe;
  1134. /* don't need that schedule data any more */
  1135. iso_sched_free (stream, iso_sched);
  1136. urb->hcpriv = NULL;
  1137. timer_action (ehci, TIMER_IO_WATCHDOG);
  1138. if (unlikely (!ehci->periodic_sched++))
  1139. return enable_periodic (ehci);
  1140. return 0;
  1141. }
  1142. #define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
  1143. static unsigned
  1144. itd_complete (
  1145. struct ehci_hcd *ehci,
  1146. struct ehci_itd *itd,
  1147. struct pt_regs *regs
  1148. ) {
  1149. struct urb *urb = itd->urb;
  1150. struct usb_iso_packet_descriptor *desc;
  1151. u32 t;
  1152. unsigned uframe;
  1153. int urb_index = -1;
  1154. struct ehci_iso_stream *stream = itd->stream;
  1155. struct usb_device *dev;
  1156. /* for each uframe with a packet */
  1157. for (uframe = 0; uframe < 8; uframe++) {
  1158. if (likely (itd->index[uframe] == -1))
  1159. continue;
  1160. urb_index = itd->index[uframe];
  1161. desc = &urb->iso_frame_desc [urb_index];
  1162. t = le32_to_cpup (&itd->hw_transaction [uframe]);
  1163. itd->hw_transaction [uframe] = 0;
  1164. stream->depth -= stream->interval;
  1165. /* report transfer status */
  1166. if (unlikely (t & ISO_ERRS)) {
  1167. urb->error_count++;
  1168. if (t & EHCI_ISOC_BUF_ERR)
  1169. desc->status = usb_pipein (urb->pipe)
  1170. ? -ENOSR /* hc couldn't read */
  1171. : -ECOMM; /* hc couldn't write */
  1172. else if (t & EHCI_ISOC_BABBLE)
  1173. desc->status = -EOVERFLOW;
  1174. else /* (t & EHCI_ISOC_XACTERR) */
  1175. desc->status = -EPROTO;
  1176. /* HC need not update length with this error */
  1177. if (!(t & EHCI_ISOC_BABBLE))
  1178. desc->actual_length = EHCI_ITD_LENGTH (t);
  1179. } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
  1180. desc->status = 0;
  1181. desc->actual_length = EHCI_ITD_LENGTH (t);
  1182. }
  1183. }
  1184. usb_put_urb (urb);
  1185. itd->urb = NULL;
  1186. itd->stream = NULL;
  1187. list_move (&itd->itd_list, &stream->free_list);
  1188. iso_stream_put (ehci, stream);
  1189. /* handle completion now? */
  1190. if (likely ((urb_index + 1) != urb->number_of_packets))
  1191. return 0;
  1192. /* ASSERT: it's really the last itd for this urb
  1193. list_for_each_entry (itd, &stream->td_list, itd_list)
  1194. BUG_ON (itd->urb == urb);
  1195. */
  1196. /* give urb back to the driver ... can be out-of-order */
  1197. dev = urb->dev;
  1198. ehci_urb_done (ehci, urb, regs);
  1199. urb = NULL;
  1200. /* defer stopping schedule; completion can submit */
  1201. ehci->periodic_sched--;
  1202. if (unlikely (!ehci->periodic_sched))
  1203. (void) disable_periodic (ehci);
  1204. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1205. if (unlikely (list_empty (&stream->td_list))) {
  1206. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1207. -= stream->bandwidth;
  1208. ehci_vdbg (ehci,
  1209. "deschedule devp %s ep%d%s-iso\n",
  1210. dev->devpath, stream->bEndpointAddress & 0x0f,
  1211. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1212. }
  1213. iso_stream_put (ehci, stream);
  1214. return 1;
  1215. }
  1216. /*-------------------------------------------------------------------------*/
  1217. static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1218. gfp_t mem_flags)
  1219. {
  1220. int status = -EINVAL;
  1221. unsigned long flags;
  1222. struct ehci_iso_stream *stream;
  1223. /* Get iso_stream head */
  1224. stream = iso_stream_find (ehci, urb);
  1225. if (unlikely (stream == NULL)) {
  1226. ehci_dbg (ehci, "can't get iso stream\n");
  1227. return -ENOMEM;
  1228. }
  1229. if (unlikely (urb->interval != stream->interval)) {
  1230. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1231. stream->interval, urb->interval);
  1232. goto done;
  1233. }
  1234. #ifdef EHCI_URB_TRACE
  1235. ehci_dbg (ehci,
  1236. "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
  1237. __FUNCTION__, urb->dev->devpath, urb,
  1238. usb_pipeendpoint (urb->pipe),
  1239. usb_pipein (urb->pipe) ? "in" : "out",
  1240. urb->transfer_buffer_length,
  1241. urb->number_of_packets, urb->interval,
  1242. stream);
  1243. #endif
  1244. /* allocate ITDs w/o locking anything */
  1245. status = itd_urb_transaction (stream, ehci, urb, mem_flags);
  1246. if (unlikely (status < 0)) {
  1247. ehci_dbg (ehci, "can't init itds\n");
  1248. goto done;
  1249. }
  1250. /* schedule ... need to lock */
  1251. spin_lock_irqsave (&ehci->lock, flags);
  1252. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  1253. &ehci_to_hcd(ehci)->flags)))
  1254. status = -ESHUTDOWN;
  1255. else
  1256. status = iso_stream_schedule (ehci, urb, stream);
  1257. if (likely (status == 0))
  1258. itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1259. spin_unlock_irqrestore (&ehci->lock, flags);
  1260. done:
  1261. if (unlikely (status < 0))
  1262. iso_stream_put (ehci, stream);
  1263. return status;
  1264. }
  1265. #ifdef CONFIG_USB_EHCI_SPLIT_ISO
  1266. /*-------------------------------------------------------------------------*/
  1267. /*
  1268. * "Split ISO TDs" ... used for USB 1.1 devices going through the
  1269. * TTs in USB 2.0 hubs. These need microframe scheduling.
  1270. */
  1271. static inline void
  1272. sitd_sched_init (
  1273. struct ehci_iso_sched *iso_sched,
  1274. struct ehci_iso_stream *stream,
  1275. struct urb *urb
  1276. )
  1277. {
  1278. unsigned i;
  1279. dma_addr_t dma = urb->transfer_dma;
  1280. /* how many frames are needed for these transfers */
  1281. iso_sched->span = urb->number_of_packets * stream->interval;
  1282. /* figure out per-frame sitd fields that we'll need later
  1283. * when we fit new sitds into the schedule.
  1284. */
  1285. for (i = 0; i < urb->number_of_packets; i++) {
  1286. struct ehci_iso_packet *packet = &iso_sched->packet [i];
  1287. unsigned length;
  1288. dma_addr_t buf;
  1289. u32 trans;
  1290. length = urb->iso_frame_desc [i].length & 0x03ff;
  1291. buf = dma + urb->iso_frame_desc [i].offset;
  1292. trans = SITD_STS_ACTIVE;
  1293. if (((i + 1) == urb->number_of_packets)
  1294. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  1295. trans |= SITD_IOC;
  1296. trans |= length << 16;
  1297. packet->transaction = cpu_to_le32 (trans);
  1298. /* might need to cross a buffer page within a td */
  1299. packet->bufp = buf;
  1300. packet->buf1 = (buf + length) & ~0x0fff;
  1301. if (packet->buf1 != (buf & ~(u64)0x0fff))
  1302. packet->cross = 1;
  1303. /* OUT uses multiple start-splits */
  1304. if (stream->bEndpointAddress & USB_DIR_IN)
  1305. continue;
  1306. length = (length + 187) / 188;
  1307. if (length > 1) /* BEGIN vs ALL */
  1308. length |= 1 << 3;
  1309. packet->buf1 |= length;
  1310. }
  1311. }
  1312. static int
  1313. sitd_urb_transaction (
  1314. struct ehci_iso_stream *stream,
  1315. struct ehci_hcd *ehci,
  1316. struct urb *urb,
  1317. gfp_t mem_flags
  1318. )
  1319. {
  1320. struct ehci_sitd *sitd;
  1321. dma_addr_t sitd_dma;
  1322. int i;
  1323. struct ehci_iso_sched *iso_sched;
  1324. unsigned long flags;
  1325. iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  1326. if (iso_sched == NULL)
  1327. return -ENOMEM;
  1328. sitd_sched_init (iso_sched, stream, urb);
  1329. /* allocate/init sITDs */
  1330. spin_lock_irqsave (&ehci->lock, flags);
  1331. for (i = 0; i < urb->number_of_packets; i++) {
  1332. /* NOTE: for now, we don't try to handle wraparound cases
  1333. * for IN (using sitd->hw_backpointer, like a FSTN), which
  1334. * means we never need two sitds for full speed packets.
  1335. */
  1336. /* free_list.next might be cache-hot ... but maybe
  1337. * the HC caches it too. avoid that issue for now.
  1338. */
  1339. /* prefer previously-allocated sitds */
  1340. if (!list_empty(&stream->free_list)) {
  1341. sitd = list_entry (stream->free_list.prev,
  1342. struct ehci_sitd, sitd_list);
  1343. list_del (&sitd->sitd_list);
  1344. sitd_dma = sitd->sitd_dma;
  1345. } else
  1346. sitd = NULL;
  1347. if (!sitd) {
  1348. spin_unlock_irqrestore (&ehci->lock, flags);
  1349. sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
  1350. &sitd_dma);
  1351. spin_lock_irqsave (&ehci->lock, flags);
  1352. }
  1353. if (!sitd) {
  1354. iso_sched_free (stream, iso_sched);
  1355. spin_unlock_irqrestore (&ehci->lock, flags);
  1356. return -ENOMEM;
  1357. }
  1358. memset (sitd, 0, sizeof *sitd);
  1359. sitd->sitd_dma = sitd_dma;
  1360. list_add (&sitd->sitd_list, &iso_sched->td_list);
  1361. }
  1362. /* temporarily store schedule info in hcpriv */
  1363. urb->hcpriv = iso_sched;
  1364. urb->error_count = 0;
  1365. spin_unlock_irqrestore (&ehci->lock, flags);
  1366. return 0;
  1367. }
  1368. /*-------------------------------------------------------------------------*/
  1369. static inline void
  1370. sitd_patch (
  1371. struct ehci_iso_stream *stream,
  1372. struct ehci_sitd *sitd,
  1373. struct ehci_iso_sched *iso_sched,
  1374. unsigned index
  1375. )
  1376. {
  1377. struct ehci_iso_packet *uf = &iso_sched->packet [index];
  1378. u64 bufp = uf->bufp;
  1379. sitd->hw_next = EHCI_LIST_END;
  1380. sitd->hw_fullspeed_ep = stream->address;
  1381. sitd->hw_uframe = stream->splits;
  1382. sitd->hw_results = uf->transaction;
  1383. sitd->hw_backpointer = EHCI_LIST_END;
  1384. bufp = uf->bufp;
  1385. sitd->hw_buf [0] = cpu_to_le32 (bufp);
  1386. sitd->hw_buf_hi [0] = cpu_to_le32 (bufp >> 32);
  1387. sitd->hw_buf [1] = cpu_to_le32 (uf->buf1);
  1388. if (uf->cross)
  1389. bufp += 4096;
  1390. sitd->hw_buf_hi [1] = cpu_to_le32 (bufp >> 32);
  1391. sitd->index = index;
  1392. }
  1393. static inline void
  1394. sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
  1395. {
  1396. /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
  1397. sitd->sitd_next = ehci->pshadow [frame];
  1398. sitd->hw_next = ehci->periodic [frame];
  1399. ehci->pshadow [frame].sitd = sitd;
  1400. sitd->frame = frame;
  1401. wmb ();
  1402. ehci->periodic [frame] = cpu_to_le32 (sitd->sitd_dma) | Q_TYPE_SITD;
  1403. }
  1404. /* fit urb's sitds into the selected schedule slot; activate as needed */
  1405. static int
  1406. sitd_link_urb (
  1407. struct ehci_hcd *ehci,
  1408. struct urb *urb,
  1409. unsigned mod,
  1410. struct ehci_iso_stream *stream
  1411. )
  1412. {
  1413. int packet;
  1414. unsigned next_uframe;
  1415. struct ehci_iso_sched *sched = urb->hcpriv;
  1416. struct ehci_sitd *sitd;
  1417. next_uframe = stream->next_uframe;
  1418. if (list_empty(&stream->td_list)) {
  1419. /* usbfs ignores TT bandwidth */
  1420. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1421. += stream->bandwidth;
  1422. ehci_vdbg (ehci,
  1423. "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
  1424. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  1425. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  1426. (next_uframe >> 3) % ehci->periodic_size,
  1427. stream->interval, le32_to_cpu (stream->splits));
  1428. stream->start = jiffies;
  1429. }
  1430. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
  1431. /* fill sITDs frame by frame */
  1432. for (packet = 0, sitd = NULL;
  1433. packet < urb->number_of_packets;
  1434. packet++) {
  1435. /* ASSERT: we have all necessary sitds */
  1436. BUG_ON (list_empty (&sched->td_list));
  1437. /* ASSERT: no itds for this endpoint in this frame */
  1438. sitd = list_entry (sched->td_list.next,
  1439. struct ehci_sitd, sitd_list);
  1440. list_move_tail (&sitd->sitd_list, &stream->td_list);
  1441. sitd->stream = iso_stream_get (stream);
  1442. sitd->urb = usb_get_urb (urb);
  1443. sitd_patch (stream, sitd, sched, packet);
  1444. sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
  1445. sitd);
  1446. next_uframe += stream->interval << 3;
  1447. stream->depth += stream->interval << 3;
  1448. }
  1449. stream->next_uframe = next_uframe % mod;
  1450. /* don't need that schedule data any more */
  1451. iso_sched_free (stream, sched);
  1452. urb->hcpriv = NULL;
  1453. timer_action (ehci, TIMER_IO_WATCHDOG);
  1454. if (!ehci->periodic_sched++)
  1455. return enable_periodic (ehci);
  1456. return 0;
  1457. }
  1458. /*-------------------------------------------------------------------------*/
  1459. #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
  1460. | SITD_STS_XACT | SITD_STS_MMF)
  1461. static unsigned
  1462. sitd_complete (
  1463. struct ehci_hcd *ehci,
  1464. struct ehci_sitd *sitd,
  1465. struct pt_regs *regs
  1466. ) {
  1467. struct urb *urb = sitd->urb;
  1468. struct usb_iso_packet_descriptor *desc;
  1469. u32 t;
  1470. int urb_index = -1;
  1471. struct ehci_iso_stream *stream = sitd->stream;
  1472. struct usb_device *dev;
  1473. urb_index = sitd->index;
  1474. desc = &urb->iso_frame_desc [urb_index];
  1475. t = le32_to_cpup (&sitd->hw_results);
  1476. /* report transfer status */
  1477. if (t & SITD_ERRS) {
  1478. urb->error_count++;
  1479. if (t & SITD_STS_DBE)
  1480. desc->status = usb_pipein (urb->pipe)
  1481. ? -ENOSR /* hc couldn't read */
  1482. : -ECOMM; /* hc couldn't write */
  1483. else if (t & SITD_STS_BABBLE)
  1484. desc->status = -EOVERFLOW;
  1485. else /* XACT, MMF, etc */
  1486. desc->status = -EPROTO;
  1487. } else {
  1488. desc->status = 0;
  1489. desc->actual_length = desc->length - SITD_LENGTH (t);
  1490. }
  1491. usb_put_urb (urb);
  1492. sitd->urb = NULL;
  1493. sitd->stream = NULL;
  1494. list_move (&sitd->sitd_list, &stream->free_list);
  1495. stream->depth -= stream->interval << 3;
  1496. iso_stream_put (ehci, stream);
  1497. /* handle completion now? */
  1498. if ((urb_index + 1) != urb->number_of_packets)
  1499. return 0;
  1500. /* ASSERT: it's really the last sitd for this urb
  1501. list_for_each_entry (sitd, &stream->td_list, sitd_list)
  1502. BUG_ON (sitd->urb == urb);
  1503. */
  1504. /* give urb back to the driver */
  1505. dev = urb->dev;
  1506. ehci_urb_done (ehci, urb, regs);
  1507. urb = NULL;
  1508. /* defer stopping schedule; completion can submit */
  1509. ehci->periodic_sched--;
  1510. if (!ehci->periodic_sched)
  1511. (void) disable_periodic (ehci);
  1512. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1513. if (list_empty (&stream->td_list)) {
  1514. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1515. -= stream->bandwidth;
  1516. ehci_vdbg (ehci,
  1517. "deschedule devp %s ep%d%s-iso\n",
  1518. dev->devpath, stream->bEndpointAddress & 0x0f,
  1519. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1520. }
  1521. iso_stream_put (ehci, stream);
  1522. return 1;
  1523. }
  1524. static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1525. gfp_t mem_flags)
  1526. {
  1527. int status = -EINVAL;
  1528. unsigned long flags;
  1529. struct ehci_iso_stream *stream;
  1530. /* Get iso_stream head */
  1531. stream = iso_stream_find (ehci, urb);
  1532. if (stream == NULL) {
  1533. ehci_dbg (ehci, "can't get iso stream\n");
  1534. return -ENOMEM;
  1535. }
  1536. if (urb->interval != stream->interval) {
  1537. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1538. stream->interval, urb->interval);
  1539. goto done;
  1540. }
  1541. #ifdef EHCI_URB_TRACE
  1542. ehci_dbg (ehci,
  1543. "submit %p dev%s ep%d%s-iso len %d\n",
  1544. urb, urb->dev->devpath,
  1545. usb_pipeendpoint (urb->pipe),
  1546. usb_pipein (urb->pipe) ? "in" : "out",
  1547. urb->transfer_buffer_length);
  1548. #endif
  1549. /* allocate SITDs */
  1550. status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
  1551. if (status < 0) {
  1552. ehci_dbg (ehci, "can't init sitds\n");
  1553. goto done;
  1554. }
  1555. /* schedule ... need to lock */
  1556. spin_lock_irqsave (&ehci->lock, flags);
  1557. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  1558. &ehci_to_hcd(ehci)->flags)))
  1559. status = -ESHUTDOWN;
  1560. else
  1561. status = iso_stream_schedule (ehci, urb, stream);
  1562. if (status == 0)
  1563. sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1564. spin_unlock_irqrestore (&ehci->lock, flags);
  1565. done:
  1566. if (status < 0)
  1567. iso_stream_put (ehci, stream);
  1568. return status;
  1569. }
  1570. #else
  1571. static inline int
  1572. sitd_submit (struct ehci_hcd *ehci, struct urb *urb, gfp_t mem_flags)
  1573. {
  1574. ehci_dbg (ehci, "split iso support is disabled\n");
  1575. return -ENOSYS;
  1576. }
  1577. static inline unsigned
  1578. sitd_complete (
  1579. struct ehci_hcd *ehci,
  1580. struct ehci_sitd *sitd,
  1581. struct pt_regs *regs
  1582. ) {
  1583. ehci_err (ehci, "sitd_complete %p?\n", sitd);
  1584. return 0;
  1585. }
  1586. #endif /* USB_EHCI_SPLIT_ISO */
  1587. /*-------------------------------------------------------------------------*/
  1588. static void
  1589. scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
  1590. {
  1591. unsigned frame, clock, now_uframe, mod;
  1592. unsigned modified;
  1593. mod = ehci->periodic_size << 3;
  1594. /*
  1595. * When running, scan from last scan point up to "now"
  1596. * else clean up by scanning everything that's left.
  1597. * Touches as few pages as possible: cache-friendly.
  1598. */
  1599. now_uframe = ehci->next_uframe;
  1600. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  1601. clock = readl (&ehci->regs->frame_index);
  1602. else
  1603. clock = now_uframe + mod - 1;
  1604. clock %= mod;
  1605. for (;;) {
  1606. union ehci_shadow q, *q_p;
  1607. __le32 type, *hw_p;
  1608. unsigned uframes;
  1609. /* don't scan past the live uframe */
  1610. frame = now_uframe >> 3;
  1611. if (frame == (clock >> 3))
  1612. uframes = now_uframe & 0x07;
  1613. else {
  1614. /* safe to scan the whole frame at once */
  1615. now_uframe |= 0x07;
  1616. uframes = 8;
  1617. }
  1618. restart:
  1619. /* scan each element in frame's queue for completions */
  1620. q_p = &ehci->pshadow [frame];
  1621. hw_p = &ehci->periodic [frame];
  1622. q.ptr = q_p->ptr;
  1623. type = Q_NEXT_TYPE (*hw_p);
  1624. modified = 0;
  1625. while (q.ptr != NULL) {
  1626. unsigned uf;
  1627. union ehci_shadow temp;
  1628. int live;
  1629. live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
  1630. switch (type) {
  1631. case Q_TYPE_QH:
  1632. /* handle any completions */
  1633. temp.qh = qh_get (q.qh);
  1634. type = Q_NEXT_TYPE (q.qh->hw_next);
  1635. q = q.qh->qh_next;
  1636. modified = qh_completions (ehci, temp.qh, regs);
  1637. if (unlikely (list_empty (&temp.qh->qtd_list)))
  1638. intr_deschedule (ehci, temp.qh);
  1639. qh_put (temp.qh);
  1640. break;
  1641. case Q_TYPE_FSTN:
  1642. /* for "save place" FSTNs, look at QH entries
  1643. * in the previous frame for completions.
  1644. */
  1645. if (q.fstn->hw_prev != EHCI_LIST_END) {
  1646. dbg ("ignoring completions from FSTNs");
  1647. }
  1648. type = Q_NEXT_TYPE (q.fstn->hw_next);
  1649. q = q.fstn->fstn_next;
  1650. break;
  1651. case Q_TYPE_ITD:
  1652. /* skip itds for later in the frame */
  1653. rmb ();
  1654. for (uf = live ? uframes : 8; uf < 8; uf++) {
  1655. if (0 == (q.itd->hw_transaction [uf]
  1656. & ITD_ACTIVE))
  1657. continue;
  1658. q_p = &q.itd->itd_next;
  1659. hw_p = &q.itd->hw_next;
  1660. type = Q_NEXT_TYPE (q.itd->hw_next);
  1661. q = *q_p;
  1662. break;
  1663. }
  1664. if (uf != 8)
  1665. break;
  1666. /* this one's ready ... HC won't cache the
  1667. * pointer for much longer, if at all.
  1668. */
  1669. *q_p = q.itd->itd_next;
  1670. *hw_p = q.itd->hw_next;
  1671. type = Q_NEXT_TYPE (q.itd->hw_next);
  1672. wmb();
  1673. modified = itd_complete (ehci, q.itd, regs);
  1674. q = *q_p;
  1675. break;
  1676. case Q_TYPE_SITD:
  1677. if ((q.sitd->hw_results & SITD_ACTIVE)
  1678. && live) {
  1679. q_p = &q.sitd->sitd_next;
  1680. hw_p = &q.sitd->hw_next;
  1681. type = Q_NEXT_TYPE (q.sitd->hw_next);
  1682. q = *q_p;
  1683. break;
  1684. }
  1685. *q_p = q.sitd->sitd_next;
  1686. *hw_p = q.sitd->hw_next;
  1687. type = Q_NEXT_TYPE (q.sitd->hw_next);
  1688. wmb();
  1689. modified = sitd_complete (ehci, q.sitd, regs);
  1690. q = *q_p;
  1691. break;
  1692. default:
  1693. dbg ("corrupt type %d frame %d shadow %p",
  1694. type, frame, q.ptr);
  1695. // BUG ();
  1696. q.ptr = NULL;
  1697. }
  1698. /* assume completion callbacks modify the queue */
  1699. if (unlikely (modified))
  1700. goto restart;
  1701. }
  1702. /* stop when we catch up to the HC */
  1703. // FIXME: this assumes we won't get lapped when
  1704. // latencies climb; that should be rare, but...
  1705. // detect it, and just go all the way around.
  1706. // FLR might help detect this case, so long as latencies
  1707. // don't exceed periodic_size msec (default 1.024 sec).
  1708. // FIXME: likewise assumes HC doesn't halt mid-scan
  1709. if (now_uframe == clock) {
  1710. unsigned now;
  1711. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  1712. break;
  1713. ehci->next_uframe = now_uframe;
  1714. now = readl (&ehci->regs->frame_index) % mod;
  1715. if (now_uframe == now)
  1716. break;
  1717. /* rescan the rest of this frame, then ... */
  1718. clock = now;
  1719. } else {
  1720. now_uframe++;
  1721. now_uframe %= mod;
  1722. }
  1723. }
  1724. }