ehci-sched.c 59 KB

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