ehci-sched.c 65 KB

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