ehci-sched.c 62 KB

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