ehci-sched.c 60 KB

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