ehci-sched.c 61 KB

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