ehci-sched.c 65 KB

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