ehci-sched.c 64 KB

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