ehci-sched.c 61 KB

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