ehci-sched.c 61 KB

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