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