ehci-sched.c 64 KB

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