ehci-sched.c 64 KB

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