ehci-sched.c 60 KB

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