ehci-sched.c 61 KB

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