ehci-sched.c 63 KB

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