ehci-sched.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  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. addr = dev->ttport << 24;
  617. if (!ehci_is_TDI(ehci)
  618. || (dev->tt->hub !=
  619. ehci_to_hcd(ehci)->self.root_hub))
  620. addr |= dev->tt->hub->devnum << 16;
  621. addr |= epnum << 8;
  622. addr |= dev->devnum;
  623. stream->usecs = HS_USECS_ISO (maxp);
  624. think_time = dev->tt ? dev->tt->think_time : 0;
  625. stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
  626. dev->speed, is_input, 1, maxp));
  627. if (is_input) {
  628. u32 tmp;
  629. addr |= 1 << 31;
  630. stream->c_usecs = stream->usecs;
  631. stream->usecs = HS_USECS_ISO (1);
  632. stream->raw_mask = 1;
  633. /* pessimistic c-mask */
  634. tmp = usb_calc_bus_time (USB_SPEED_FULL, 1, 0, maxp)
  635. / (125 * 1000);
  636. stream->raw_mask |= 3 << (tmp + 9);
  637. } else
  638. stream->raw_mask = smask_out [maxp / 188];
  639. bandwidth = stream->usecs + stream->c_usecs;
  640. bandwidth /= 1 << (interval + 2);
  641. /* stream->splits gets created from raw_mask later */
  642. stream->address = cpu_to_le32 (addr);
  643. }
  644. stream->bandwidth = bandwidth;
  645. stream->udev = dev;
  646. stream->bEndpointAddress = is_input | epnum;
  647. stream->interval = interval;
  648. stream->maxp = maxp;
  649. }
  650. static void
  651. iso_stream_put(struct ehci_hcd *ehci, struct ehci_iso_stream *stream)
  652. {
  653. stream->refcount--;
  654. /* free whenever just a dev->ep reference remains.
  655. * not like a QH -- no persistent state (toggle, halt)
  656. */
  657. if (stream->refcount == 1) {
  658. int is_in;
  659. // BUG_ON (!list_empty(&stream->td_list));
  660. while (!list_empty (&stream->free_list)) {
  661. struct list_head *entry;
  662. entry = stream->free_list.next;
  663. list_del (entry);
  664. /* knows about ITD vs SITD */
  665. if (stream->highspeed) {
  666. struct ehci_itd *itd;
  667. itd = list_entry (entry, struct ehci_itd,
  668. itd_list);
  669. dma_pool_free (ehci->itd_pool, itd,
  670. itd->itd_dma);
  671. } else {
  672. struct ehci_sitd *sitd;
  673. sitd = list_entry (entry, struct ehci_sitd,
  674. sitd_list);
  675. dma_pool_free (ehci->sitd_pool, sitd,
  676. sitd->sitd_dma);
  677. }
  678. }
  679. is_in = (stream->bEndpointAddress & USB_DIR_IN) ? 0x10 : 0;
  680. stream->bEndpointAddress &= 0x0f;
  681. stream->ep->hcpriv = NULL;
  682. if (stream->rescheduled) {
  683. ehci_info (ehci, "ep%d%s-iso rescheduled "
  684. "%lu times in %lu seconds\n",
  685. stream->bEndpointAddress, is_in ? "in" : "out",
  686. stream->rescheduled,
  687. ((jiffies - stream->start)/HZ)
  688. );
  689. }
  690. kfree(stream);
  691. }
  692. }
  693. static inline struct ehci_iso_stream *
  694. iso_stream_get (struct ehci_iso_stream *stream)
  695. {
  696. if (likely (stream != NULL))
  697. stream->refcount++;
  698. return stream;
  699. }
  700. static struct ehci_iso_stream *
  701. iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
  702. {
  703. unsigned epnum;
  704. struct ehci_iso_stream *stream;
  705. struct usb_host_endpoint *ep;
  706. unsigned long flags;
  707. epnum = usb_pipeendpoint (urb->pipe);
  708. if (usb_pipein(urb->pipe))
  709. ep = urb->dev->ep_in[epnum];
  710. else
  711. ep = urb->dev->ep_out[epnum];
  712. spin_lock_irqsave (&ehci->lock, flags);
  713. stream = ep->hcpriv;
  714. if (unlikely (stream == NULL)) {
  715. stream = iso_stream_alloc(GFP_ATOMIC);
  716. if (likely (stream != NULL)) {
  717. /* dev->ep owns the initial refcount */
  718. ep->hcpriv = stream;
  719. stream->ep = ep;
  720. iso_stream_init(ehci, stream, urb->dev, urb->pipe,
  721. urb->interval);
  722. }
  723. /* if dev->ep [epnum] is a QH, info1.maxpacket is nonzero */
  724. } else if (unlikely (stream->hw_info1 != 0)) {
  725. ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
  726. urb->dev->devpath, epnum,
  727. usb_pipein(urb->pipe) ? "in" : "out");
  728. stream = NULL;
  729. }
  730. /* caller guarantees an eventual matching iso_stream_put */
  731. stream = iso_stream_get (stream);
  732. spin_unlock_irqrestore (&ehci->lock, flags);
  733. return stream;
  734. }
  735. /*-------------------------------------------------------------------------*/
  736. /* ehci_iso_sched ops can be ITD-only or SITD-only */
  737. static struct ehci_iso_sched *
  738. iso_sched_alloc (unsigned packets, gfp_t mem_flags)
  739. {
  740. struct ehci_iso_sched *iso_sched;
  741. int size = sizeof *iso_sched;
  742. size += packets * sizeof (struct ehci_iso_packet);
  743. iso_sched = kmalloc (size, mem_flags);
  744. if (likely (iso_sched != NULL)) {
  745. memset(iso_sched, 0, size);
  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 = usb_get_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. usb_put_dev (dev);
  1215. return 1;
  1216. }
  1217. /*-------------------------------------------------------------------------*/
  1218. static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1219. gfp_t mem_flags)
  1220. {
  1221. int status = -EINVAL;
  1222. unsigned long flags;
  1223. struct ehci_iso_stream *stream;
  1224. /* Get iso_stream head */
  1225. stream = iso_stream_find (ehci, urb);
  1226. if (unlikely (stream == NULL)) {
  1227. ehci_dbg (ehci, "can't get iso stream\n");
  1228. return -ENOMEM;
  1229. }
  1230. if (unlikely (urb->interval != stream->interval)) {
  1231. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1232. stream->interval, urb->interval);
  1233. goto done;
  1234. }
  1235. #ifdef EHCI_URB_TRACE
  1236. ehci_dbg (ehci,
  1237. "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
  1238. __FUNCTION__, urb->dev->devpath, urb,
  1239. usb_pipeendpoint (urb->pipe),
  1240. usb_pipein (urb->pipe) ? "in" : "out",
  1241. urb->transfer_buffer_length,
  1242. urb->number_of_packets, urb->interval,
  1243. stream);
  1244. #endif
  1245. /* allocate ITDs w/o locking anything */
  1246. status = itd_urb_transaction (stream, ehci, urb, mem_flags);
  1247. if (unlikely (status < 0)) {
  1248. ehci_dbg (ehci, "can't init itds\n");
  1249. goto done;
  1250. }
  1251. /* schedule ... need to lock */
  1252. spin_lock_irqsave (&ehci->lock, flags);
  1253. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  1254. &ehci_to_hcd(ehci)->flags)))
  1255. status = -ESHUTDOWN;
  1256. else
  1257. status = iso_stream_schedule (ehci, urb, stream);
  1258. if (likely (status == 0))
  1259. itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1260. spin_unlock_irqrestore (&ehci->lock, flags);
  1261. done:
  1262. if (unlikely (status < 0))
  1263. iso_stream_put (ehci, stream);
  1264. return status;
  1265. }
  1266. #ifdef CONFIG_USB_EHCI_SPLIT_ISO
  1267. /*-------------------------------------------------------------------------*/
  1268. /*
  1269. * "Split ISO TDs" ... used for USB 1.1 devices going through the
  1270. * TTs in USB 2.0 hubs. These need microframe scheduling.
  1271. */
  1272. static inline void
  1273. sitd_sched_init (
  1274. struct ehci_iso_sched *iso_sched,
  1275. struct ehci_iso_stream *stream,
  1276. struct urb *urb
  1277. )
  1278. {
  1279. unsigned i;
  1280. dma_addr_t dma = urb->transfer_dma;
  1281. /* how many frames are needed for these transfers */
  1282. iso_sched->span = urb->number_of_packets * stream->interval;
  1283. /* figure out per-frame sitd fields that we'll need later
  1284. * when we fit new sitds into the schedule.
  1285. */
  1286. for (i = 0; i < urb->number_of_packets; i++) {
  1287. struct ehci_iso_packet *packet = &iso_sched->packet [i];
  1288. unsigned length;
  1289. dma_addr_t buf;
  1290. u32 trans;
  1291. length = urb->iso_frame_desc [i].length & 0x03ff;
  1292. buf = dma + urb->iso_frame_desc [i].offset;
  1293. trans = SITD_STS_ACTIVE;
  1294. if (((i + 1) == urb->number_of_packets)
  1295. && !(urb->transfer_flags & URB_NO_INTERRUPT))
  1296. trans |= SITD_IOC;
  1297. trans |= length << 16;
  1298. packet->transaction = cpu_to_le32 (trans);
  1299. /* might need to cross a buffer page within a td */
  1300. packet->bufp = buf;
  1301. packet->buf1 = (buf + length) & ~0x0fff;
  1302. if (packet->buf1 != (buf & ~(u64)0x0fff))
  1303. packet->cross = 1;
  1304. /* OUT uses multiple start-splits */
  1305. if (stream->bEndpointAddress & USB_DIR_IN)
  1306. continue;
  1307. length = (length + 187) / 188;
  1308. if (length > 1) /* BEGIN vs ALL */
  1309. length |= 1 << 3;
  1310. packet->buf1 |= length;
  1311. }
  1312. }
  1313. static int
  1314. sitd_urb_transaction (
  1315. struct ehci_iso_stream *stream,
  1316. struct ehci_hcd *ehci,
  1317. struct urb *urb,
  1318. gfp_t mem_flags
  1319. )
  1320. {
  1321. struct ehci_sitd *sitd;
  1322. dma_addr_t sitd_dma;
  1323. int i;
  1324. struct ehci_iso_sched *iso_sched;
  1325. unsigned long flags;
  1326. iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
  1327. if (iso_sched == NULL)
  1328. return -ENOMEM;
  1329. sitd_sched_init (iso_sched, stream, urb);
  1330. /* allocate/init sITDs */
  1331. spin_lock_irqsave (&ehci->lock, flags);
  1332. for (i = 0; i < urb->number_of_packets; i++) {
  1333. /* NOTE: for now, we don't try to handle wraparound cases
  1334. * for IN (using sitd->hw_backpointer, like a FSTN), which
  1335. * means we never need two sitds for full speed packets.
  1336. */
  1337. /* free_list.next might be cache-hot ... but maybe
  1338. * the HC caches it too. avoid that issue for now.
  1339. */
  1340. /* prefer previously-allocated sitds */
  1341. if (!list_empty(&stream->free_list)) {
  1342. sitd = list_entry (stream->free_list.prev,
  1343. struct ehci_sitd, sitd_list);
  1344. list_del (&sitd->sitd_list);
  1345. sitd_dma = sitd->sitd_dma;
  1346. } else
  1347. sitd = NULL;
  1348. if (!sitd) {
  1349. spin_unlock_irqrestore (&ehci->lock, flags);
  1350. sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
  1351. &sitd_dma);
  1352. spin_lock_irqsave (&ehci->lock, flags);
  1353. }
  1354. if (!sitd) {
  1355. iso_sched_free (stream, iso_sched);
  1356. spin_unlock_irqrestore (&ehci->lock, flags);
  1357. return -ENOMEM;
  1358. }
  1359. memset (sitd, 0, sizeof *sitd);
  1360. sitd->sitd_dma = sitd_dma;
  1361. list_add (&sitd->sitd_list, &iso_sched->td_list);
  1362. }
  1363. /* temporarily store schedule info in hcpriv */
  1364. urb->hcpriv = iso_sched;
  1365. urb->error_count = 0;
  1366. spin_unlock_irqrestore (&ehci->lock, flags);
  1367. return 0;
  1368. }
  1369. /*-------------------------------------------------------------------------*/
  1370. static inline void
  1371. sitd_patch (
  1372. struct ehci_iso_stream *stream,
  1373. struct ehci_sitd *sitd,
  1374. struct ehci_iso_sched *iso_sched,
  1375. unsigned index
  1376. )
  1377. {
  1378. struct ehci_iso_packet *uf = &iso_sched->packet [index];
  1379. u64 bufp = uf->bufp;
  1380. sitd->hw_next = EHCI_LIST_END;
  1381. sitd->hw_fullspeed_ep = stream->address;
  1382. sitd->hw_uframe = stream->splits;
  1383. sitd->hw_results = uf->transaction;
  1384. sitd->hw_backpointer = EHCI_LIST_END;
  1385. bufp = uf->bufp;
  1386. sitd->hw_buf [0] = cpu_to_le32 (bufp);
  1387. sitd->hw_buf_hi [0] = cpu_to_le32 (bufp >> 32);
  1388. sitd->hw_buf [1] = cpu_to_le32 (uf->buf1);
  1389. if (uf->cross)
  1390. bufp += 4096;
  1391. sitd->hw_buf_hi [1] = cpu_to_le32 (bufp >> 32);
  1392. sitd->index = index;
  1393. }
  1394. static inline void
  1395. sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
  1396. {
  1397. /* note: sitd ordering could matter (CSPLIT then SSPLIT) */
  1398. sitd->sitd_next = ehci->pshadow [frame];
  1399. sitd->hw_next = ehci->periodic [frame];
  1400. ehci->pshadow [frame].sitd = sitd;
  1401. sitd->frame = frame;
  1402. wmb ();
  1403. ehci->periodic [frame] = cpu_to_le32 (sitd->sitd_dma) | Q_TYPE_SITD;
  1404. }
  1405. /* fit urb's sitds into the selected schedule slot; activate as needed */
  1406. static int
  1407. sitd_link_urb (
  1408. struct ehci_hcd *ehci,
  1409. struct urb *urb,
  1410. unsigned mod,
  1411. struct ehci_iso_stream *stream
  1412. )
  1413. {
  1414. int packet;
  1415. unsigned next_uframe;
  1416. struct ehci_iso_sched *sched = urb->hcpriv;
  1417. struct ehci_sitd *sitd;
  1418. next_uframe = stream->next_uframe;
  1419. if (list_empty(&stream->td_list)) {
  1420. /* usbfs ignores TT bandwidth */
  1421. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1422. += stream->bandwidth;
  1423. ehci_vdbg (ehci,
  1424. "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
  1425. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
  1426. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  1427. (next_uframe >> 3) % ehci->periodic_size,
  1428. stream->interval, le32_to_cpu (stream->splits));
  1429. stream->start = jiffies;
  1430. }
  1431. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
  1432. /* fill sITDs frame by frame */
  1433. for (packet = 0, sitd = NULL;
  1434. packet < urb->number_of_packets;
  1435. packet++) {
  1436. /* ASSERT: we have all necessary sitds */
  1437. BUG_ON (list_empty (&sched->td_list));
  1438. /* ASSERT: no itds for this endpoint in this frame */
  1439. sitd = list_entry (sched->td_list.next,
  1440. struct ehci_sitd, sitd_list);
  1441. list_move_tail (&sitd->sitd_list, &stream->td_list);
  1442. sitd->stream = iso_stream_get (stream);
  1443. sitd->urb = usb_get_urb (urb);
  1444. sitd_patch (stream, sitd, sched, packet);
  1445. sitd_link (ehci, (next_uframe >> 3) % ehci->periodic_size,
  1446. sitd);
  1447. next_uframe += stream->interval << 3;
  1448. stream->depth += stream->interval << 3;
  1449. }
  1450. stream->next_uframe = next_uframe % mod;
  1451. /* don't need that schedule data any more */
  1452. iso_sched_free (stream, sched);
  1453. urb->hcpriv = NULL;
  1454. timer_action (ehci, TIMER_IO_WATCHDOG);
  1455. if (!ehci->periodic_sched++)
  1456. return enable_periodic (ehci);
  1457. return 0;
  1458. }
  1459. /*-------------------------------------------------------------------------*/
  1460. #define SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
  1461. | SITD_STS_XACT | SITD_STS_MMF)
  1462. static unsigned
  1463. sitd_complete (
  1464. struct ehci_hcd *ehci,
  1465. struct ehci_sitd *sitd,
  1466. struct pt_regs *regs
  1467. ) {
  1468. struct urb *urb = sitd->urb;
  1469. struct usb_iso_packet_descriptor *desc;
  1470. u32 t;
  1471. int urb_index = -1;
  1472. struct ehci_iso_stream *stream = sitd->stream;
  1473. struct usb_device *dev;
  1474. urb_index = sitd->index;
  1475. desc = &urb->iso_frame_desc [urb_index];
  1476. t = le32_to_cpup (&sitd->hw_results);
  1477. /* report transfer status */
  1478. if (t & SITD_ERRS) {
  1479. urb->error_count++;
  1480. if (t & SITD_STS_DBE)
  1481. desc->status = usb_pipein (urb->pipe)
  1482. ? -ENOSR /* hc couldn't read */
  1483. : -ECOMM; /* hc couldn't write */
  1484. else if (t & SITD_STS_BABBLE)
  1485. desc->status = -EOVERFLOW;
  1486. else /* XACT, MMF, etc */
  1487. desc->status = -EPROTO;
  1488. } else {
  1489. desc->status = 0;
  1490. desc->actual_length = desc->length - SITD_LENGTH (t);
  1491. }
  1492. usb_put_urb (urb);
  1493. sitd->urb = NULL;
  1494. sitd->stream = NULL;
  1495. list_move (&sitd->sitd_list, &stream->free_list);
  1496. stream->depth -= stream->interval << 3;
  1497. iso_stream_put (ehci, stream);
  1498. /* handle completion now? */
  1499. if ((urb_index + 1) != urb->number_of_packets)
  1500. return 0;
  1501. /* ASSERT: it's really the last sitd for this urb
  1502. list_for_each_entry (sitd, &stream->td_list, sitd_list)
  1503. BUG_ON (sitd->urb == urb);
  1504. */
  1505. /* give urb back to the driver */
  1506. dev = usb_get_dev (urb->dev);
  1507. ehci_urb_done (ehci, urb, regs);
  1508. urb = NULL;
  1509. /* defer stopping schedule; completion can submit */
  1510. ehci->periodic_sched--;
  1511. if (!ehci->periodic_sched)
  1512. (void) disable_periodic (ehci);
  1513. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
  1514. if (list_empty (&stream->td_list)) {
  1515. ehci_to_hcd(ehci)->self.bandwidth_allocated
  1516. -= stream->bandwidth;
  1517. ehci_vdbg (ehci,
  1518. "deschedule devp %s ep%d%s-iso\n",
  1519. dev->devpath, stream->bEndpointAddress & 0x0f,
  1520. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
  1521. }
  1522. iso_stream_put (ehci, stream);
  1523. usb_put_dev (dev);
  1524. return 1;
  1525. }
  1526. static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
  1527. gfp_t mem_flags)
  1528. {
  1529. int status = -EINVAL;
  1530. unsigned long flags;
  1531. struct ehci_iso_stream *stream;
  1532. /* Get iso_stream head */
  1533. stream = iso_stream_find (ehci, urb);
  1534. if (stream == NULL) {
  1535. ehci_dbg (ehci, "can't get iso stream\n");
  1536. return -ENOMEM;
  1537. }
  1538. if (urb->interval != stream->interval) {
  1539. ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
  1540. stream->interval, urb->interval);
  1541. goto done;
  1542. }
  1543. #ifdef EHCI_URB_TRACE
  1544. ehci_dbg (ehci,
  1545. "submit %p dev%s ep%d%s-iso len %d\n",
  1546. urb, urb->dev->devpath,
  1547. usb_pipeendpoint (urb->pipe),
  1548. usb_pipein (urb->pipe) ? "in" : "out",
  1549. urb->transfer_buffer_length);
  1550. #endif
  1551. /* allocate SITDs */
  1552. status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
  1553. if (status < 0) {
  1554. ehci_dbg (ehci, "can't init sitds\n");
  1555. goto done;
  1556. }
  1557. /* schedule ... need to lock */
  1558. spin_lock_irqsave (&ehci->lock, flags);
  1559. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  1560. &ehci_to_hcd(ehci)->flags)))
  1561. status = -ESHUTDOWN;
  1562. else
  1563. status = iso_stream_schedule (ehci, urb, stream);
  1564. if (status == 0)
  1565. sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
  1566. spin_unlock_irqrestore (&ehci->lock, flags);
  1567. done:
  1568. if (status < 0)
  1569. iso_stream_put (ehci, stream);
  1570. return status;
  1571. }
  1572. #else
  1573. static inline int
  1574. sitd_submit (struct ehci_hcd *ehci, struct urb *urb, gfp_t mem_flags)
  1575. {
  1576. ehci_dbg (ehci, "split iso support is disabled\n");
  1577. return -ENOSYS;
  1578. }
  1579. static inline unsigned
  1580. sitd_complete (
  1581. struct ehci_hcd *ehci,
  1582. struct ehci_sitd *sitd,
  1583. struct pt_regs *regs
  1584. ) {
  1585. ehci_err (ehci, "sitd_complete %p?\n", sitd);
  1586. return 0;
  1587. }
  1588. #endif /* USB_EHCI_SPLIT_ISO */
  1589. /*-------------------------------------------------------------------------*/
  1590. static void
  1591. scan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs)
  1592. {
  1593. unsigned frame, clock, now_uframe, mod;
  1594. unsigned modified;
  1595. mod = ehci->periodic_size << 3;
  1596. /*
  1597. * When running, scan from last scan point up to "now"
  1598. * else clean up by scanning everything that's left.
  1599. * Touches as few pages as possible: cache-friendly.
  1600. */
  1601. now_uframe = ehci->next_uframe;
  1602. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  1603. clock = readl (&ehci->regs->frame_index);
  1604. else
  1605. clock = now_uframe + mod - 1;
  1606. clock %= mod;
  1607. for (;;) {
  1608. union ehci_shadow q, *q_p;
  1609. __le32 type, *hw_p;
  1610. unsigned uframes;
  1611. /* don't scan past the live uframe */
  1612. frame = now_uframe >> 3;
  1613. if (frame == (clock >> 3))
  1614. uframes = now_uframe & 0x07;
  1615. else {
  1616. /* safe to scan the whole frame at once */
  1617. now_uframe |= 0x07;
  1618. uframes = 8;
  1619. }
  1620. restart:
  1621. /* scan each element in frame's queue for completions */
  1622. q_p = &ehci->pshadow [frame];
  1623. hw_p = &ehci->periodic [frame];
  1624. q.ptr = q_p->ptr;
  1625. type = Q_NEXT_TYPE (*hw_p);
  1626. modified = 0;
  1627. while (q.ptr != NULL) {
  1628. unsigned uf;
  1629. union ehci_shadow temp;
  1630. int live;
  1631. live = HC_IS_RUNNING (ehci_to_hcd(ehci)->state);
  1632. switch (type) {
  1633. case Q_TYPE_QH:
  1634. /* handle any completions */
  1635. temp.qh = qh_get (q.qh);
  1636. type = Q_NEXT_TYPE (q.qh->hw_next);
  1637. q = q.qh->qh_next;
  1638. modified = qh_completions (ehci, temp.qh, regs);
  1639. if (unlikely (list_empty (&temp.qh->qtd_list)))
  1640. intr_deschedule (ehci, temp.qh);
  1641. qh_put (temp.qh);
  1642. break;
  1643. case Q_TYPE_FSTN:
  1644. /* for "save place" FSTNs, look at QH entries
  1645. * in the previous frame for completions.
  1646. */
  1647. if (q.fstn->hw_prev != EHCI_LIST_END) {
  1648. dbg ("ignoring completions from FSTNs");
  1649. }
  1650. type = Q_NEXT_TYPE (q.fstn->hw_next);
  1651. q = q.fstn->fstn_next;
  1652. break;
  1653. case Q_TYPE_ITD:
  1654. /* skip itds for later in the frame */
  1655. rmb ();
  1656. for (uf = live ? uframes : 8; uf < 8; uf++) {
  1657. if (0 == (q.itd->hw_transaction [uf]
  1658. & ITD_ACTIVE))
  1659. continue;
  1660. q_p = &q.itd->itd_next;
  1661. hw_p = &q.itd->hw_next;
  1662. type = Q_NEXT_TYPE (q.itd->hw_next);
  1663. q = *q_p;
  1664. break;
  1665. }
  1666. if (uf != 8)
  1667. break;
  1668. /* this one's ready ... HC won't cache the
  1669. * pointer for much longer, if at all.
  1670. */
  1671. *q_p = q.itd->itd_next;
  1672. *hw_p = q.itd->hw_next;
  1673. type = Q_NEXT_TYPE (q.itd->hw_next);
  1674. wmb();
  1675. modified = itd_complete (ehci, q.itd, regs);
  1676. q = *q_p;
  1677. break;
  1678. case Q_TYPE_SITD:
  1679. if ((q.sitd->hw_results & SITD_ACTIVE)
  1680. && live) {
  1681. q_p = &q.sitd->sitd_next;
  1682. hw_p = &q.sitd->hw_next;
  1683. type = Q_NEXT_TYPE (q.sitd->hw_next);
  1684. q = *q_p;
  1685. break;
  1686. }
  1687. *q_p = q.sitd->sitd_next;
  1688. *hw_p = q.sitd->hw_next;
  1689. type = Q_NEXT_TYPE (q.sitd->hw_next);
  1690. wmb();
  1691. modified = sitd_complete (ehci, q.sitd, regs);
  1692. q = *q_p;
  1693. break;
  1694. default:
  1695. dbg ("corrupt type %d frame %d shadow %p",
  1696. type, frame, q.ptr);
  1697. // BUG ();
  1698. q.ptr = NULL;
  1699. }
  1700. /* assume completion callbacks modify the queue */
  1701. if (unlikely (modified))
  1702. goto restart;
  1703. }
  1704. /* stop when we catch up to the HC */
  1705. // FIXME: this assumes we won't get lapped when
  1706. // latencies climb; that should be rare, but...
  1707. // detect it, and just go all the way around.
  1708. // FLR might help detect this case, so long as latencies
  1709. // don't exceed periodic_size msec (default 1.024 sec).
  1710. // FIXME: likewise assumes HC doesn't halt mid-scan
  1711. if (now_uframe == clock) {
  1712. unsigned now;
  1713. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  1714. break;
  1715. ehci->next_uframe = now_uframe;
  1716. now = readl (&ehci->regs->frame_index) % mod;
  1717. if (now_uframe == now)
  1718. break;
  1719. /* rescan the rest of this frame, then ... */
  1720. clock = now;
  1721. } else {
  1722. now_uframe++;
  1723. now_uframe %= mod;
  1724. }
  1725. }
  1726. }