ehci-sched.c 63 KB

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