ehci-sched.c 61 KB

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