xhci-ring.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*
  23. * Ring initialization rules:
  24. * 1. Each segment is initialized to zero, except for link TRBs.
  25. * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
  26. * Consumer Cycle State (CCS), depending on ring function.
  27. * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
  28. *
  29. * Ring behavior rules:
  30. * 1. A ring is empty if enqueue == dequeue. This means there will always be at
  31. * least one free TRB in the ring. This is useful if you want to turn that
  32. * into a link TRB and expand the ring.
  33. * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
  34. * link TRB, then load the pointer with the address in the link TRB. If the
  35. * link TRB had its toggle bit set, you may need to update the ring cycle
  36. * state (see cycle bit rules). You may have to do this multiple times
  37. * until you reach a non-link TRB.
  38. * 3. A ring is full if enqueue++ (for the definition of increment above)
  39. * equals the dequeue pointer.
  40. *
  41. * Cycle bit rules:
  42. * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
  43. * in a link TRB, it must toggle the ring cycle state.
  44. * 2. When a producer increments an enqueue pointer and encounters a toggle bit
  45. * in a link TRB, it must toggle the ring cycle state.
  46. *
  47. * Producer rules:
  48. * 1. Check if ring is full before you enqueue.
  49. * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
  50. * Update enqueue pointer between each write (which may update the ring
  51. * cycle state).
  52. * 3. Notify consumer. If SW is producer, it rings the doorbell for command
  53. * and endpoint rings. If HC is the producer for the event ring,
  54. * and it generates an interrupt according to interrupt modulation rules.
  55. *
  56. * Consumer rules:
  57. * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
  58. * the TRB is owned by the consumer.
  59. * 2. Update dequeue pointer (which may update the ring cycle state) and
  60. * continue processing TRBs until you reach a TRB which is not owned by you.
  61. * 3. Notify the producer. SW is the consumer for the event ring, and it
  62. * updates event ring dequeue pointer. HC is the consumer for the command and
  63. * endpoint rings; it generates events on the event ring for these.
  64. */
  65. #include <linux/scatterlist.h>
  66. #include "xhci.h"
  67. /*
  68. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  69. * address of the TRB.
  70. */
  71. dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
  72. union xhci_trb *trb)
  73. {
  74. unsigned long segment_offset;
  75. if (!seg || !trb || trb < seg->trbs)
  76. return 0;
  77. /* offset in TRBs */
  78. segment_offset = trb - seg->trbs;
  79. if (segment_offset > TRBS_PER_SEGMENT)
  80. return 0;
  81. return seg->dma + (segment_offset * sizeof(*trb));
  82. }
  83. /* Does this link TRB point to the first segment in a ring,
  84. * or was the previous TRB the last TRB on the last segment in the ERST?
  85. */
  86. static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
  87. struct xhci_segment *seg, union xhci_trb *trb)
  88. {
  89. if (ring == xhci->event_ring)
  90. return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
  91. (seg->next == xhci->event_ring->first_seg);
  92. else
  93. return trb->link.control & LINK_TOGGLE;
  94. }
  95. /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
  96. * segment? I.e. would the updated event TRB pointer step off the end of the
  97. * event seg?
  98. */
  99. static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  100. struct xhci_segment *seg, union xhci_trb *trb)
  101. {
  102. if (ring == xhci->event_ring)
  103. return trb == &seg->trbs[TRBS_PER_SEGMENT];
  104. else
  105. return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
  106. }
  107. /* Updates trb to point to the next TRB in the ring, and updates seg if the next
  108. * TRB is in a new segment. This does not skip over link TRBs, and it does not
  109. * effect the ring dequeue or enqueue pointers.
  110. */
  111. static void next_trb(struct xhci_hcd *xhci,
  112. struct xhci_ring *ring,
  113. struct xhci_segment **seg,
  114. union xhci_trb **trb)
  115. {
  116. if (last_trb(xhci, ring, *seg, *trb)) {
  117. *seg = (*seg)->next;
  118. *trb = ((*seg)->trbs);
  119. } else {
  120. *trb = (*trb)++;
  121. }
  122. }
  123. /*
  124. * See Cycle bit rules. SW is the consumer for the event ring only.
  125. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  126. */
  127. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  128. {
  129. union xhci_trb *next = ++(ring->dequeue);
  130. unsigned long long addr;
  131. ring->deq_updates++;
  132. /* Update the dequeue pointer further if that was a link TRB or we're at
  133. * the end of an event ring segment (which doesn't have link TRBS)
  134. */
  135. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  136. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  137. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  138. if (!in_interrupt())
  139. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  140. ring,
  141. (unsigned int) ring->cycle_state);
  142. }
  143. ring->deq_seg = ring->deq_seg->next;
  144. ring->dequeue = ring->deq_seg->trbs;
  145. next = ring->dequeue;
  146. }
  147. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
  148. if (ring == xhci->event_ring)
  149. xhci_dbg(xhci, "Event ring deq = 0x%llx (DMA)\n", addr);
  150. else if (ring == xhci->cmd_ring)
  151. xhci_dbg(xhci, "Command ring deq = 0x%llx (DMA)\n", addr);
  152. else
  153. xhci_dbg(xhci, "Ring deq = 0x%llx (DMA)\n", addr);
  154. }
  155. /*
  156. * See Cycle bit rules. SW is the consumer for the event ring only.
  157. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  158. *
  159. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  160. * chain bit is set), then set the chain bit in all the following link TRBs.
  161. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  162. * have their chain bit cleared (so that each Link TRB is a separate TD).
  163. *
  164. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  165. * set, but other sections talk about dealing with the chain bit set. This was
  166. * fixed in the 0.96 specification errata, but we have to assume that all 0.95
  167. * xHCI hardware can't handle the chain bit being cleared on a link TRB.
  168. */
  169. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  170. {
  171. u32 chain;
  172. union xhci_trb *next;
  173. unsigned long long addr;
  174. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  175. next = ++(ring->enqueue);
  176. ring->enq_updates++;
  177. /* Update the dequeue pointer further if that was a link TRB or we're at
  178. * the end of an event ring segment (which doesn't have link TRBS)
  179. */
  180. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  181. if (!consumer) {
  182. if (ring != xhci->event_ring) {
  183. /* If we're not dealing with 0.95 hardware,
  184. * carry over the chain bit of the previous TRB
  185. * (which may mean the chain bit is cleared).
  186. */
  187. if (!xhci_link_trb_quirk(xhci)) {
  188. next->link.control &= ~TRB_CHAIN;
  189. next->link.control |= chain;
  190. }
  191. /* Give this link TRB to the hardware */
  192. wmb();
  193. if (next->link.control & TRB_CYCLE)
  194. next->link.control &= (u32) ~TRB_CYCLE;
  195. else
  196. next->link.control |= (u32) TRB_CYCLE;
  197. }
  198. /* Toggle the cycle bit after the last ring segment. */
  199. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  200. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  201. if (!in_interrupt())
  202. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  203. ring,
  204. (unsigned int) ring->cycle_state);
  205. }
  206. }
  207. ring->enq_seg = ring->enq_seg->next;
  208. ring->enqueue = ring->enq_seg->trbs;
  209. next = ring->enqueue;
  210. }
  211. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  212. if (ring == xhci->event_ring)
  213. xhci_dbg(xhci, "Event ring enq = 0x%llx (DMA)\n", addr);
  214. else if (ring == xhci->cmd_ring)
  215. xhci_dbg(xhci, "Command ring enq = 0x%llx (DMA)\n", addr);
  216. else
  217. xhci_dbg(xhci, "Ring enq = 0x%llx (DMA)\n", addr);
  218. }
  219. /*
  220. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  221. * above.
  222. * FIXME: this would be simpler and faster if we just kept track of the number
  223. * of free TRBs in a ring.
  224. */
  225. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  226. unsigned int num_trbs)
  227. {
  228. int i;
  229. union xhci_trb *enq = ring->enqueue;
  230. struct xhci_segment *enq_seg = ring->enq_seg;
  231. /* Check if ring is empty */
  232. if (enq == ring->dequeue)
  233. return 1;
  234. /* Make sure there's an extra empty TRB available */
  235. for (i = 0; i <= num_trbs; ++i) {
  236. if (enq == ring->dequeue)
  237. return 0;
  238. enq++;
  239. while (last_trb(xhci, ring, enq_seg, enq)) {
  240. enq_seg = enq_seg->next;
  241. enq = enq_seg->trbs;
  242. }
  243. }
  244. return 1;
  245. }
  246. void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  247. {
  248. u64 temp;
  249. dma_addr_t deq;
  250. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  251. xhci->event_ring->dequeue);
  252. if (deq == 0 && !in_interrupt())
  253. xhci_warn(xhci, "WARN something wrong with SW event ring "
  254. "dequeue ptr.\n");
  255. /* Update HC event ring dequeue pointer */
  256. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  257. temp &= ERST_PTR_MASK;
  258. /* Don't clear the EHB bit (which is RW1C) because
  259. * there might be more events to service.
  260. */
  261. temp &= ~ERST_EHB;
  262. xhci_dbg(xhci, "// Write event ring dequeue pointer, preserving EHB bit\n");
  263. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  264. &xhci->ir_set->erst_dequeue);
  265. }
  266. /* Ring the host controller doorbell after placing a command on the ring */
  267. void xhci_ring_cmd_db(struct xhci_hcd *xhci)
  268. {
  269. u32 temp;
  270. xhci_dbg(xhci, "// Ding dong!\n");
  271. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  272. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  273. /* Flush PCI posted writes */
  274. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  275. }
  276. static void ring_ep_doorbell(struct xhci_hcd *xhci,
  277. unsigned int slot_id,
  278. unsigned int ep_index)
  279. {
  280. struct xhci_virt_ep *ep;
  281. unsigned int ep_state;
  282. u32 field;
  283. __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  284. ep = &xhci->devs[slot_id]->eps[ep_index];
  285. ep_state = ep->ep_state;
  286. /* Don't ring the doorbell for this endpoint if there are pending
  287. * cancellations because the we don't want to interrupt processing.
  288. */
  289. if (!(ep_state & EP_HALT_PENDING) && !(ep_state & SET_DEQ_PENDING)
  290. && !(ep_state & EP_HALTED)) {
  291. field = xhci_readl(xhci, db_addr) & DB_MASK;
  292. xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
  293. /* Flush PCI posted writes - FIXME Matthew Wilcox says this
  294. * isn't time-critical and we shouldn't make the CPU wait for
  295. * the flush.
  296. */
  297. xhci_readl(xhci, db_addr);
  298. }
  299. }
  300. /*
  301. * Find the segment that trb is in. Start searching in start_seg.
  302. * If we must move past a segment that has a link TRB with a toggle cycle state
  303. * bit set, then we will toggle the value pointed at by cycle_state.
  304. */
  305. static struct xhci_segment *find_trb_seg(
  306. struct xhci_segment *start_seg,
  307. union xhci_trb *trb, int *cycle_state)
  308. {
  309. struct xhci_segment *cur_seg = start_seg;
  310. struct xhci_generic_trb *generic_trb;
  311. while (cur_seg->trbs > trb ||
  312. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  313. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  314. if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
  315. (generic_trb->field[3] & LINK_TOGGLE))
  316. *cycle_state = ~(*cycle_state) & 0x1;
  317. cur_seg = cur_seg->next;
  318. if (cur_seg == start_seg)
  319. /* Looped over the entire list. Oops! */
  320. return 0;
  321. }
  322. return cur_seg;
  323. }
  324. /*
  325. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  326. * Record the new state of the xHC's endpoint ring dequeue segment,
  327. * dequeue pointer, and new consumer cycle state in state.
  328. * Update our internal representation of the ring's dequeue pointer.
  329. *
  330. * We do this in three jumps:
  331. * - First we update our new ring state to be the same as when the xHC stopped.
  332. * - Then we traverse the ring to find the segment that contains
  333. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  334. * any link TRBs with the toggle cycle bit set.
  335. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  336. * if we've moved it past a link TRB with the toggle cycle bit set.
  337. */
  338. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  339. unsigned int slot_id, unsigned int ep_index,
  340. struct xhci_td *cur_td, struct xhci_dequeue_state *state)
  341. {
  342. struct xhci_virt_device *dev = xhci->devs[slot_id];
  343. struct xhci_ring *ep_ring = dev->eps[ep_index].ring;
  344. struct xhci_generic_trb *trb;
  345. struct xhci_ep_ctx *ep_ctx;
  346. dma_addr_t addr;
  347. state->new_cycle_state = 0;
  348. xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
  349. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  350. dev->eps[ep_index].stopped_trb,
  351. &state->new_cycle_state);
  352. if (!state->new_deq_seg)
  353. BUG();
  354. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  355. xhci_dbg(xhci, "Finding endpoint context\n");
  356. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  357. state->new_cycle_state = 0x1 & ep_ctx->deq;
  358. state->new_deq_ptr = cur_td->last_trb;
  359. xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
  360. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  361. state->new_deq_ptr,
  362. &state->new_cycle_state);
  363. if (!state->new_deq_seg)
  364. BUG();
  365. trb = &state->new_deq_ptr->generic;
  366. if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
  367. (trb->field[3] & LINK_TOGGLE))
  368. state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
  369. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  370. /* Don't update the ring cycle state for the producer (us). */
  371. xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
  372. state->new_deq_seg);
  373. addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
  374. xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
  375. (unsigned long long) addr);
  376. xhci_dbg(xhci, "Setting dequeue pointer in internal ring state.\n");
  377. ep_ring->dequeue = state->new_deq_ptr;
  378. ep_ring->deq_seg = state->new_deq_seg;
  379. }
  380. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  381. struct xhci_td *cur_td)
  382. {
  383. struct xhci_segment *cur_seg;
  384. union xhci_trb *cur_trb;
  385. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  386. true;
  387. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  388. if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
  389. TRB_TYPE(TRB_LINK)) {
  390. /* Unchain any chained Link TRBs, but
  391. * leave the pointers intact.
  392. */
  393. cur_trb->generic.field[3] &= ~TRB_CHAIN;
  394. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  395. xhci_dbg(xhci, "Address = %p (0x%llx dma); "
  396. "in seg %p (0x%llx dma)\n",
  397. cur_trb,
  398. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  399. cur_seg,
  400. (unsigned long long)cur_seg->dma);
  401. } else {
  402. cur_trb->generic.field[0] = 0;
  403. cur_trb->generic.field[1] = 0;
  404. cur_trb->generic.field[2] = 0;
  405. /* Preserve only the cycle bit of this TRB */
  406. cur_trb->generic.field[3] &= TRB_CYCLE;
  407. cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
  408. xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
  409. "in seg %p (0x%llx dma)\n",
  410. cur_trb,
  411. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  412. cur_seg,
  413. (unsigned long long)cur_seg->dma);
  414. }
  415. if (cur_trb == cur_td->last_trb)
  416. break;
  417. }
  418. }
  419. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  420. unsigned int ep_index, struct xhci_segment *deq_seg,
  421. union xhci_trb *deq_ptr, u32 cycle_state);
  422. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  423. unsigned int slot_id, unsigned int ep_index,
  424. struct xhci_dequeue_state *deq_state)
  425. {
  426. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  427. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
  428. "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
  429. deq_state->new_deq_seg,
  430. (unsigned long long)deq_state->new_deq_seg->dma,
  431. deq_state->new_deq_ptr,
  432. (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
  433. deq_state->new_cycle_state);
  434. queue_set_tr_deq(xhci, slot_id, ep_index,
  435. deq_state->new_deq_seg,
  436. deq_state->new_deq_ptr,
  437. (u32) deq_state->new_cycle_state);
  438. /* Stop the TD queueing code from ringing the doorbell until
  439. * this command completes. The HC won't set the dequeue pointer
  440. * if the ring is running, and ringing the doorbell starts the
  441. * ring running.
  442. */
  443. ep->ep_state |= SET_DEQ_PENDING;
  444. }
  445. static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
  446. struct xhci_virt_ep *ep)
  447. {
  448. ep->ep_state &= ~EP_HALT_PENDING;
  449. /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
  450. * timer is running on another CPU, we don't decrement stop_cmds_pending
  451. * (since we didn't successfully stop the watchdog timer).
  452. */
  453. if (del_timer(&ep->stop_cmd_timer))
  454. ep->stop_cmds_pending--;
  455. }
  456. /* Must be called with xhci->lock held in interrupt context */
  457. static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
  458. struct xhci_td *cur_td, int status, char *adjective)
  459. {
  460. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  461. cur_td->urb->hcpriv = NULL;
  462. usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb);
  463. xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb);
  464. spin_unlock(&xhci->lock);
  465. usb_hcd_giveback_urb(hcd, cur_td->urb, status);
  466. kfree(cur_td);
  467. spin_lock(&xhci->lock);
  468. xhci_dbg(xhci, "%s URB given back\n", adjective);
  469. }
  470. /*
  471. * When we get a command completion for a Stop Endpoint Command, we need to
  472. * unlink any cancelled TDs from the ring. There are two ways to do that:
  473. *
  474. * 1. If the HW was in the middle of processing the TD that needs to be
  475. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  476. * in the TD with a Set Dequeue Pointer Command.
  477. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  478. * bit cleared) so that the HW will skip over them.
  479. */
  480. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  481. union xhci_trb *trb)
  482. {
  483. unsigned int slot_id;
  484. unsigned int ep_index;
  485. struct xhci_ring *ep_ring;
  486. struct xhci_virt_ep *ep;
  487. struct list_head *entry;
  488. struct xhci_td *cur_td = 0;
  489. struct xhci_td *last_unlinked_td;
  490. struct xhci_dequeue_state deq_state;
  491. memset(&deq_state, 0, sizeof(deq_state));
  492. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  493. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  494. ep = &xhci->devs[slot_id]->eps[ep_index];
  495. ep_ring = ep->ring;
  496. if (list_empty(&ep->cancelled_td_list)) {
  497. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  498. ring_ep_doorbell(xhci, slot_id, ep_index);
  499. return;
  500. }
  501. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  502. * We have the xHCI lock, so nothing can modify this list until we drop
  503. * it. We're also in the event handler, so we can't get re-interrupted
  504. * if another Stop Endpoint command completes
  505. */
  506. list_for_each(entry, &ep->cancelled_td_list) {
  507. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  508. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  509. cur_td->first_trb,
  510. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  511. /*
  512. * If we stopped on the TD we need to cancel, then we have to
  513. * move the xHC endpoint ring dequeue pointer past this TD.
  514. */
  515. if (cur_td == ep->stopped_td)
  516. xhci_find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
  517. &deq_state);
  518. else
  519. td_to_noop(xhci, ep_ring, cur_td);
  520. /*
  521. * The event handler won't see a completion for this TD anymore,
  522. * so remove it from the endpoint ring's TD list. Keep it in
  523. * the cancelled TD list for URB completion later.
  524. */
  525. list_del(&cur_td->td_list);
  526. }
  527. last_unlinked_td = cur_td;
  528. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  529. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  530. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  531. xhci_queue_new_dequeue_state(xhci,
  532. slot_id, ep_index, &deq_state);
  533. xhci_ring_cmd_db(xhci);
  534. } else {
  535. /* Otherwise just ring the doorbell to restart the ring */
  536. ring_ep_doorbell(xhci, slot_id, ep_index);
  537. }
  538. /*
  539. * Drop the lock and complete the URBs in the cancelled TD list.
  540. * New TDs to be cancelled might be added to the end of the list before
  541. * we can complete all the URBs for the TDs we already unlinked.
  542. * So stop when we've completed the URB for the last TD we unlinked.
  543. */
  544. do {
  545. cur_td = list_entry(ep->cancelled_td_list.next,
  546. struct xhci_td, cancelled_td_list);
  547. list_del(&cur_td->cancelled_td_list);
  548. /* Clean up the cancelled URB */
  549. /* Doesn't matter what we pass for status, since the core will
  550. * just overwrite it (because the URB has been unlinked).
  551. */
  552. xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
  553. /* Stop processing the cancelled list if the watchdog timer is
  554. * running.
  555. */
  556. if (xhci->xhc_state & XHCI_STATE_DYING)
  557. return;
  558. } while (cur_td != last_unlinked_td);
  559. /* Return to the event handler with xhci->lock re-acquired */
  560. }
  561. /* Watchdog timer function for when a stop endpoint command fails to complete.
  562. * In this case, we assume the host controller is broken or dying or dead. The
  563. * host may still be completing some other events, so we have to be careful to
  564. * let the event ring handler and the URB dequeueing/enqueueing functions know
  565. * through xhci->state.
  566. *
  567. * The timer may also fire if the host takes a very long time to respond to the
  568. * command, and the stop endpoint command completion handler cannot delete the
  569. * timer before the timer function is called. Another endpoint cancellation may
  570. * sneak in before the timer function can grab the lock, and that may queue
  571. * another stop endpoint command and add the timer back. So we cannot use a
  572. * simple flag to say whether there is a pending stop endpoint command for a
  573. * particular endpoint.
  574. *
  575. * Instead we use a combination of that flag and a counter for the number of
  576. * pending stop endpoint commands. If the timer is the tail end of the last
  577. * stop endpoint command, and the endpoint's command is still pending, we assume
  578. * the host is dying.
  579. */
  580. void xhci_stop_endpoint_command_watchdog(unsigned long arg)
  581. {
  582. struct xhci_hcd *xhci;
  583. struct xhci_virt_ep *ep;
  584. struct xhci_virt_ep *temp_ep;
  585. struct xhci_ring *ring;
  586. struct xhci_td *cur_td;
  587. int ret, i, j;
  588. ep = (struct xhci_virt_ep *) arg;
  589. xhci = ep->xhci;
  590. spin_lock(&xhci->lock);
  591. ep->stop_cmds_pending--;
  592. if (xhci->xhc_state & XHCI_STATE_DYING) {
  593. xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
  594. "xHCI as DYING, exiting.\n");
  595. spin_unlock(&xhci->lock);
  596. return;
  597. }
  598. if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
  599. xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
  600. "exiting.\n");
  601. spin_unlock(&xhci->lock);
  602. return;
  603. }
  604. xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
  605. xhci_warn(xhci, "Assuming host is dying, halting host.\n");
  606. /* Oops, HC is dead or dying or at least not responding to the stop
  607. * endpoint command.
  608. */
  609. xhci->xhc_state |= XHCI_STATE_DYING;
  610. /* Disable interrupts from the host controller and start halting it */
  611. xhci_quiesce(xhci);
  612. spin_unlock(&xhci->lock);
  613. ret = xhci_halt(xhci);
  614. spin_lock(&xhci->lock);
  615. if (ret < 0) {
  616. /* This is bad; the host is not responding to commands and it's
  617. * not allowing itself to be halted. At least interrupts are
  618. * disabled, so we can set HC_STATE_HALT and notify the
  619. * USB core. But if we call usb_hc_died(), it will attempt to
  620. * disconnect all device drivers under this host. Those
  621. * disconnect() methods will wait for all URBs to be unlinked,
  622. * so we must complete them.
  623. */
  624. xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
  625. xhci_warn(xhci, "Completing active URBs anyway.\n");
  626. /* We could turn all TDs on the rings to no-ops. This won't
  627. * help if the host has cached part of the ring, and is slow if
  628. * we want to preserve the cycle bit. Skip it and hope the host
  629. * doesn't touch the memory.
  630. */
  631. }
  632. for (i = 0; i < MAX_HC_SLOTS; i++) {
  633. if (!xhci->devs[i])
  634. continue;
  635. for (j = 0; j < 31; j++) {
  636. temp_ep = &xhci->devs[i]->eps[j];
  637. ring = temp_ep->ring;
  638. if (!ring)
  639. continue;
  640. xhci_dbg(xhci, "Killing URBs for slot ID %u, "
  641. "ep index %u\n", i, j);
  642. while (!list_empty(&ring->td_list)) {
  643. cur_td = list_first_entry(&ring->td_list,
  644. struct xhci_td,
  645. td_list);
  646. list_del(&cur_td->td_list);
  647. if (!list_empty(&cur_td->cancelled_td_list))
  648. list_del(&cur_td->cancelled_td_list);
  649. xhci_giveback_urb_in_irq(xhci, cur_td,
  650. -ESHUTDOWN, "killed");
  651. }
  652. while (!list_empty(&temp_ep->cancelled_td_list)) {
  653. cur_td = list_first_entry(
  654. &temp_ep->cancelled_td_list,
  655. struct xhci_td,
  656. cancelled_td_list);
  657. list_del(&cur_td->cancelled_td_list);
  658. xhci_giveback_urb_in_irq(xhci, cur_td,
  659. -ESHUTDOWN, "killed");
  660. }
  661. }
  662. }
  663. spin_unlock(&xhci->lock);
  664. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  665. xhci_dbg(xhci, "Calling usb_hc_died()\n");
  666. usb_hc_died(xhci_to_hcd(xhci));
  667. xhci_dbg(xhci, "xHCI host controller is dead.\n");
  668. }
  669. /*
  670. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  671. * we need to clear the set deq pending flag in the endpoint ring state, so that
  672. * the TD queueing code can ring the doorbell again. We also need to ring the
  673. * endpoint doorbell to restart the ring, but only if there aren't more
  674. * cancellations pending.
  675. */
  676. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  677. struct xhci_event_cmd *event,
  678. union xhci_trb *trb)
  679. {
  680. unsigned int slot_id;
  681. unsigned int ep_index;
  682. struct xhci_ring *ep_ring;
  683. struct xhci_virt_device *dev;
  684. struct xhci_ep_ctx *ep_ctx;
  685. struct xhci_slot_ctx *slot_ctx;
  686. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  687. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  688. dev = xhci->devs[slot_id];
  689. ep_ring = dev->eps[ep_index].ring;
  690. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  691. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  692. if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
  693. unsigned int ep_state;
  694. unsigned int slot_state;
  695. switch (GET_COMP_CODE(event->status)) {
  696. case COMP_TRB_ERR:
  697. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  698. "of stream ID configuration\n");
  699. break;
  700. case COMP_CTX_STATE:
  701. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  702. "to incorrect slot or ep state.\n");
  703. ep_state = ep_ctx->ep_info;
  704. ep_state &= EP_STATE_MASK;
  705. slot_state = slot_ctx->dev_state;
  706. slot_state = GET_SLOT_STATE(slot_state);
  707. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  708. slot_state, ep_state);
  709. break;
  710. case COMP_EBADSLT:
  711. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  712. "slot %u was not enabled.\n", slot_id);
  713. break;
  714. default:
  715. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  716. "completion code of %u.\n",
  717. GET_COMP_CODE(event->status));
  718. break;
  719. }
  720. /* OK what do we do now? The endpoint state is hosed, and we
  721. * should never get to this point if the synchronization between
  722. * queueing, and endpoint state are correct. This might happen
  723. * if the device gets disconnected after we've finished
  724. * cancelling URBs, which might not be an error...
  725. */
  726. } else {
  727. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
  728. ep_ctx->deq);
  729. }
  730. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  731. ring_ep_doorbell(xhci, slot_id, ep_index);
  732. }
  733. static void handle_reset_ep_completion(struct xhci_hcd *xhci,
  734. struct xhci_event_cmd *event,
  735. union xhci_trb *trb)
  736. {
  737. int slot_id;
  738. unsigned int ep_index;
  739. struct xhci_ring *ep_ring;
  740. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  741. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  742. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  743. /* This command will only fail if the endpoint wasn't halted,
  744. * but we don't care.
  745. */
  746. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  747. (unsigned int) GET_COMP_CODE(event->status));
  748. /* HW with the reset endpoint quirk needs to have a configure endpoint
  749. * command complete before the endpoint can be used. Queue that here
  750. * because the HW can't handle two commands being queued in a row.
  751. */
  752. if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
  753. xhci_dbg(xhci, "Queueing configure endpoint command\n");
  754. xhci_queue_configure_endpoint(xhci,
  755. xhci->devs[slot_id]->in_ctx->dma, slot_id,
  756. false);
  757. xhci_ring_cmd_db(xhci);
  758. } else {
  759. /* Clear our internal halted state and restart the ring */
  760. xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
  761. ring_ep_doorbell(xhci, slot_id, ep_index);
  762. }
  763. }
  764. /* Check to see if a command in the device's command queue matches this one.
  765. * Signal the completion or free the command, and return 1. Return 0 if the
  766. * completed command isn't at the head of the command list.
  767. */
  768. static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
  769. struct xhci_virt_device *virt_dev,
  770. struct xhci_event_cmd *event)
  771. {
  772. struct xhci_command *command;
  773. if (list_empty(&virt_dev->cmd_list))
  774. return 0;
  775. command = list_entry(virt_dev->cmd_list.next,
  776. struct xhci_command, cmd_list);
  777. if (xhci->cmd_ring->dequeue != command->command_trb)
  778. return 0;
  779. command->status =
  780. GET_COMP_CODE(event->status);
  781. list_del(&command->cmd_list);
  782. if (command->completion)
  783. complete(command->completion);
  784. else
  785. xhci_free_command(xhci, command);
  786. return 1;
  787. }
  788. static void handle_cmd_completion(struct xhci_hcd *xhci,
  789. struct xhci_event_cmd *event)
  790. {
  791. int slot_id = TRB_TO_SLOT_ID(event->flags);
  792. u64 cmd_dma;
  793. dma_addr_t cmd_dequeue_dma;
  794. struct xhci_input_control_ctx *ctrl_ctx;
  795. struct xhci_virt_device *virt_dev;
  796. unsigned int ep_index;
  797. struct xhci_ring *ep_ring;
  798. unsigned int ep_state;
  799. cmd_dma = event->cmd_trb;
  800. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  801. xhci->cmd_ring->dequeue);
  802. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  803. if (cmd_dequeue_dma == 0) {
  804. xhci->error_bitmask |= 1 << 4;
  805. return;
  806. }
  807. /* Does the DMA address match our internal dequeue pointer address? */
  808. if (cmd_dma != (u64) cmd_dequeue_dma) {
  809. xhci->error_bitmask |= 1 << 5;
  810. return;
  811. }
  812. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  813. case TRB_TYPE(TRB_ENABLE_SLOT):
  814. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  815. xhci->slot_id = slot_id;
  816. else
  817. xhci->slot_id = 0;
  818. complete(&xhci->addr_dev);
  819. break;
  820. case TRB_TYPE(TRB_DISABLE_SLOT):
  821. if (xhci->devs[slot_id])
  822. xhci_free_virt_device(xhci, slot_id);
  823. break;
  824. case TRB_TYPE(TRB_CONFIG_EP):
  825. virt_dev = xhci->devs[slot_id];
  826. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  827. break;
  828. /*
  829. * Configure endpoint commands can come from the USB core
  830. * configuration or alt setting changes, or because the HW
  831. * needed an extra configure endpoint command after a reset
  832. * endpoint command. In the latter case, the xHCI driver is
  833. * not waiting on the configure endpoint command.
  834. */
  835. ctrl_ctx = xhci_get_input_control_ctx(xhci,
  836. virt_dev->in_ctx);
  837. /* Input ctx add_flags are the endpoint index plus one */
  838. ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
  839. /* A usb_set_interface() call directly after clearing a halted
  840. * condition may race on this quirky hardware.
  841. * Not worth worrying about, since this is prototype hardware.
  842. */
  843. if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
  844. ep_index != (unsigned int) -1 &&
  845. ctrl_ctx->add_flags - SLOT_FLAG ==
  846. ctrl_ctx->drop_flags) {
  847. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  848. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  849. if (!(ep_state & EP_HALTED))
  850. goto bandwidth_change;
  851. xhci_dbg(xhci, "Completed config ep cmd - "
  852. "last ep index = %d, state = %d\n",
  853. ep_index, ep_state);
  854. /* Clear our internal halted state and restart ring */
  855. xhci->devs[slot_id]->eps[ep_index].ep_state &=
  856. ~EP_HALTED;
  857. ring_ep_doorbell(xhci, slot_id, ep_index);
  858. break;
  859. }
  860. bandwidth_change:
  861. xhci_dbg(xhci, "Completed config ep cmd\n");
  862. xhci->devs[slot_id]->cmd_status =
  863. GET_COMP_CODE(event->status);
  864. complete(&xhci->devs[slot_id]->cmd_completion);
  865. break;
  866. case TRB_TYPE(TRB_EVAL_CONTEXT):
  867. virt_dev = xhci->devs[slot_id];
  868. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  869. break;
  870. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  871. complete(&xhci->devs[slot_id]->cmd_completion);
  872. break;
  873. case TRB_TYPE(TRB_ADDR_DEV):
  874. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  875. complete(&xhci->addr_dev);
  876. break;
  877. case TRB_TYPE(TRB_STOP_RING):
  878. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  879. break;
  880. case TRB_TYPE(TRB_SET_DEQ):
  881. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  882. break;
  883. case TRB_TYPE(TRB_CMD_NOOP):
  884. ++xhci->noops_handled;
  885. break;
  886. case TRB_TYPE(TRB_RESET_EP):
  887. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  888. break;
  889. default:
  890. /* Skip over unknown commands on the event ring */
  891. xhci->error_bitmask |= 1 << 6;
  892. break;
  893. }
  894. inc_deq(xhci, xhci->cmd_ring, false);
  895. }
  896. static void handle_port_status(struct xhci_hcd *xhci,
  897. union xhci_trb *event)
  898. {
  899. u32 port_id;
  900. /* Port status change events always have a successful completion code */
  901. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  902. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  903. xhci->error_bitmask |= 1 << 8;
  904. }
  905. /* FIXME: core doesn't care about all port link state changes yet */
  906. port_id = GET_PORT_ID(event->generic.field[0]);
  907. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  908. /* Update event ring dequeue pointer before dropping the lock */
  909. inc_deq(xhci, xhci->event_ring, true);
  910. xhci_set_hc_event_deq(xhci);
  911. spin_unlock(&xhci->lock);
  912. /* Pass this up to the core */
  913. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  914. spin_lock(&xhci->lock);
  915. }
  916. /*
  917. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  918. * at end_trb, which may be in another segment. If the suspect DMA address is a
  919. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  920. * returns 0.
  921. */
  922. struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
  923. union xhci_trb *start_trb,
  924. union xhci_trb *end_trb,
  925. dma_addr_t suspect_dma)
  926. {
  927. dma_addr_t start_dma;
  928. dma_addr_t end_seg_dma;
  929. dma_addr_t end_trb_dma;
  930. struct xhci_segment *cur_seg;
  931. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  932. cur_seg = start_seg;
  933. do {
  934. if (start_dma == 0)
  935. return 0;
  936. /* We may get an event for a Link TRB in the middle of a TD */
  937. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  938. &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
  939. /* If the end TRB isn't in this segment, this is set to 0 */
  940. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  941. if (end_trb_dma > 0) {
  942. /* The end TRB is in this segment, so suspect should be here */
  943. if (start_dma <= end_trb_dma) {
  944. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  945. return cur_seg;
  946. } else {
  947. /* Case for one segment with
  948. * a TD wrapped around to the top
  949. */
  950. if ((suspect_dma >= start_dma &&
  951. suspect_dma <= end_seg_dma) ||
  952. (suspect_dma >= cur_seg->dma &&
  953. suspect_dma <= end_trb_dma))
  954. return cur_seg;
  955. }
  956. return 0;
  957. } else {
  958. /* Might still be somewhere in this segment */
  959. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  960. return cur_seg;
  961. }
  962. cur_seg = cur_seg->next;
  963. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  964. } while (cur_seg != start_seg);
  965. return 0;
  966. }
  967. static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
  968. unsigned int slot_id, unsigned int ep_index,
  969. struct xhci_td *td, union xhci_trb *event_trb)
  970. {
  971. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  972. ep->ep_state |= EP_HALTED;
  973. ep->stopped_td = td;
  974. ep->stopped_trb = event_trb;
  975. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  976. xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
  977. xhci_ring_cmd_db(xhci);
  978. }
  979. /* Check if an error has halted the endpoint ring. The class driver will
  980. * cleanup the halt for a non-default control endpoint if we indicate a stall.
  981. * However, a babble and other errors also halt the endpoint ring, and the class
  982. * driver won't clear the halt in that case, so we need to issue a Set Transfer
  983. * Ring Dequeue Pointer command manually.
  984. */
  985. static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
  986. struct xhci_ep_ctx *ep_ctx,
  987. unsigned int trb_comp_code)
  988. {
  989. /* TRB completion codes that may require a manual halt cleanup */
  990. if (trb_comp_code == COMP_TX_ERR ||
  991. trb_comp_code == COMP_BABBLE ||
  992. trb_comp_code == COMP_SPLIT_ERR)
  993. /* The 0.96 spec says a babbling control endpoint
  994. * is not halted. The 0.96 spec says it is. Some HW
  995. * claims to be 0.95 compliant, but it halts the control
  996. * endpoint anyway. Check if a babble halted the
  997. * endpoint.
  998. */
  999. if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED)
  1000. return 1;
  1001. return 0;
  1002. }
  1003. /*
  1004. * If this function returns an error condition, it means it got a Transfer
  1005. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  1006. * At this point, the host controller is probably hosed and should be reset.
  1007. */
  1008. static int handle_tx_event(struct xhci_hcd *xhci,
  1009. struct xhci_transfer_event *event)
  1010. {
  1011. struct xhci_virt_device *xdev;
  1012. struct xhci_virt_ep *ep;
  1013. struct xhci_ring *ep_ring;
  1014. unsigned int slot_id;
  1015. int ep_index;
  1016. struct xhci_td *td = 0;
  1017. dma_addr_t event_dma;
  1018. struct xhci_segment *event_seg;
  1019. union xhci_trb *event_trb;
  1020. struct urb *urb = 0;
  1021. int status = -EINPROGRESS;
  1022. struct xhci_ep_ctx *ep_ctx;
  1023. u32 trb_comp_code;
  1024. xhci_dbg(xhci, "In %s\n", __func__);
  1025. slot_id = TRB_TO_SLOT_ID(event->flags);
  1026. xdev = xhci->devs[slot_id];
  1027. if (!xdev) {
  1028. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  1029. return -ENODEV;
  1030. }
  1031. /* Endpoint ID is 1 based, our index is zero based */
  1032. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  1033. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  1034. ep = &xdev->eps[ep_index];
  1035. ep_ring = ep->ring;
  1036. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1037. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  1038. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  1039. return -ENODEV;
  1040. }
  1041. event_dma = event->buffer;
  1042. /* This TRB should be in the TD at the head of this ring's TD list */
  1043. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  1044. if (list_empty(&ep_ring->td_list)) {
  1045. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  1046. TRB_TO_SLOT_ID(event->flags), ep_index);
  1047. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1048. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1049. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  1050. urb = NULL;
  1051. goto cleanup;
  1052. }
  1053. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  1054. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  1055. /* Is this a TRB in the currently executing TD? */
  1056. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  1057. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  1058. td->last_trb, event_dma);
  1059. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  1060. if (!event_seg) {
  1061. /* HC is busted, give up! */
  1062. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  1063. return -ESHUTDOWN;
  1064. }
  1065. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  1066. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1067. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1068. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  1069. lower_32_bits(event->buffer));
  1070. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  1071. upper_32_bits(event->buffer));
  1072. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  1073. (unsigned int) event->transfer_len);
  1074. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  1075. (unsigned int) event->flags);
  1076. /* Look for common error cases */
  1077. trb_comp_code = GET_COMP_CODE(event->transfer_len);
  1078. switch (trb_comp_code) {
  1079. /* Skip codes that require special handling depending on
  1080. * transfer type
  1081. */
  1082. case COMP_SUCCESS:
  1083. case COMP_SHORT_TX:
  1084. break;
  1085. case COMP_STOP:
  1086. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  1087. break;
  1088. case COMP_STOP_INVAL:
  1089. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  1090. break;
  1091. case COMP_STALL:
  1092. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  1093. ep->ep_state |= EP_HALTED;
  1094. status = -EPIPE;
  1095. break;
  1096. case COMP_TRB_ERR:
  1097. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  1098. status = -EILSEQ;
  1099. break;
  1100. case COMP_SPLIT_ERR:
  1101. case COMP_TX_ERR:
  1102. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  1103. status = -EPROTO;
  1104. break;
  1105. case COMP_BABBLE:
  1106. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  1107. status = -EOVERFLOW;
  1108. break;
  1109. case COMP_DB_ERR:
  1110. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  1111. status = -ENOSR;
  1112. break;
  1113. default:
  1114. if (trb_comp_code >= 224 && trb_comp_code <= 255) {
  1115. /* Vendor defined "informational" completion code,
  1116. * treat as not-an-error.
  1117. */
  1118. xhci_dbg(xhci, "Vendor defined info completion code %u\n",
  1119. trb_comp_code);
  1120. xhci_dbg(xhci, "Treating code as success.\n");
  1121. status = 0;
  1122. break;
  1123. }
  1124. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  1125. urb = NULL;
  1126. goto cleanup;
  1127. }
  1128. /* Now update the urb's actual_length and give back to the core */
  1129. /* Was this a control transfer? */
  1130. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  1131. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  1132. switch (trb_comp_code) {
  1133. case COMP_SUCCESS:
  1134. if (event_trb == ep_ring->dequeue) {
  1135. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  1136. status = -ESHUTDOWN;
  1137. } else if (event_trb != td->last_trb) {
  1138. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  1139. status = -ESHUTDOWN;
  1140. } else {
  1141. xhci_dbg(xhci, "Successful control transfer!\n");
  1142. status = 0;
  1143. }
  1144. break;
  1145. case COMP_SHORT_TX:
  1146. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  1147. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1148. status = -EREMOTEIO;
  1149. else
  1150. status = 0;
  1151. break;
  1152. default:
  1153. if (!xhci_requires_manual_halt_cleanup(xhci,
  1154. ep_ctx, trb_comp_code))
  1155. break;
  1156. xhci_dbg(xhci, "TRB error code %u, "
  1157. "halted endpoint index = %u\n",
  1158. trb_comp_code, ep_index);
  1159. /* else fall through */
  1160. case COMP_STALL:
  1161. /* Did we transfer part of the data (middle) phase? */
  1162. if (event_trb != ep_ring->dequeue &&
  1163. event_trb != td->last_trb)
  1164. td->urb->actual_length =
  1165. td->urb->transfer_buffer_length
  1166. - TRB_LEN(event->transfer_len);
  1167. else
  1168. td->urb->actual_length = 0;
  1169. xhci_cleanup_halted_endpoint(xhci,
  1170. slot_id, ep_index, td, event_trb);
  1171. goto td_cleanup;
  1172. }
  1173. /*
  1174. * Did we transfer any data, despite the errors that might have
  1175. * happened? I.e. did we get past the setup stage?
  1176. */
  1177. if (event_trb != ep_ring->dequeue) {
  1178. /* The event was for the status stage */
  1179. if (event_trb == td->last_trb) {
  1180. if (td->urb->actual_length != 0) {
  1181. /* Don't overwrite a previously set error code */
  1182. if ((status == -EINPROGRESS ||
  1183. status == 0) &&
  1184. (td->urb->transfer_flags
  1185. & URB_SHORT_NOT_OK))
  1186. /* Did we already see a short data stage? */
  1187. status = -EREMOTEIO;
  1188. } else {
  1189. td->urb->actual_length =
  1190. td->urb->transfer_buffer_length;
  1191. }
  1192. } else {
  1193. /* Maybe the event was for the data stage? */
  1194. if (trb_comp_code != COMP_STOP_INVAL) {
  1195. /* We didn't stop on a link TRB in the middle */
  1196. td->urb->actual_length =
  1197. td->urb->transfer_buffer_length -
  1198. TRB_LEN(event->transfer_len);
  1199. xhci_dbg(xhci, "Waiting for status stage event\n");
  1200. urb = NULL;
  1201. goto cleanup;
  1202. }
  1203. }
  1204. }
  1205. } else {
  1206. switch (trb_comp_code) {
  1207. case COMP_SUCCESS:
  1208. /* Double check that the HW transferred everything. */
  1209. if (event_trb != td->last_trb) {
  1210. xhci_warn(xhci, "WARN Successful completion "
  1211. "on short TX\n");
  1212. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1213. status = -EREMOTEIO;
  1214. else
  1215. status = 0;
  1216. } else {
  1217. if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
  1218. xhci_dbg(xhci, "Successful bulk "
  1219. "transfer!\n");
  1220. else
  1221. xhci_dbg(xhci, "Successful interrupt "
  1222. "transfer!\n");
  1223. status = 0;
  1224. }
  1225. break;
  1226. case COMP_SHORT_TX:
  1227. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1228. status = -EREMOTEIO;
  1229. else
  1230. status = 0;
  1231. break;
  1232. default:
  1233. /* Others already handled above */
  1234. break;
  1235. }
  1236. dev_dbg(&td->urb->dev->dev,
  1237. "ep %#x - asked for %d bytes, "
  1238. "%d bytes untransferred\n",
  1239. td->urb->ep->desc.bEndpointAddress,
  1240. td->urb->transfer_buffer_length,
  1241. TRB_LEN(event->transfer_len));
  1242. /* Fast path - was this the last TRB in the TD for this URB? */
  1243. if (event_trb == td->last_trb) {
  1244. if (TRB_LEN(event->transfer_len) != 0) {
  1245. td->urb->actual_length =
  1246. td->urb->transfer_buffer_length -
  1247. TRB_LEN(event->transfer_len);
  1248. if (td->urb->transfer_buffer_length <
  1249. td->urb->actual_length) {
  1250. xhci_warn(xhci, "HC gave bad length "
  1251. "of %d bytes left\n",
  1252. TRB_LEN(event->transfer_len));
  1253. td->urb->actual_length = 0;
  1254. if (td->urb->transfer_flags &
  1255. URB_SHORT_NOT_OK)
  1256. status = -EREMOTEIO;
  1257. else
  1258. status = 0;
  1259. }
  1260. /* Don't overwrite a previously set error code */
  1261. if (status == -EINPROGRESS) {
  1262. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1263. status = -EREMOTEIO;
  1264. else
  1265. status = 0;
  1266. }
  1267. } else {
  1268. td->urb->actual_length = td->urb->transfer_buffer_length;
  1269. /* Ignore a short packet completion if the
  1270. * untransferred length was zero.
  1271. */
  1272. if (status == -EREMOTEIO)
  1273. status = 0;
  1274. }
  1275. } else {
  1276. /* Slow path - walk the list, starting from the dequeue
  1277. * pointer, to get the actual length transferred.
  1278. */
  1279. union xhci_trb *cur_trb;
  1280. struct xhci_segment *cur_seg;
  1281. td->urb->actual_length = 0;
  1282. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  1283. cur_trb != event_trb;
  1284. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1285. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  1286. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  1287. td->urb->actual_length +=
  1288. TRB_LEN(cur_trb->generic.field[2]);
  1289. }
  1290. /* If the ring didn't stop on a Link or No-op TRB, add
  1291. * in the actual bytes transferred from the Normal TRB
  1292. */
  1293. if (trb_comp_code != COMP_STOP_INVAL)
  1294. td->urb->actual_length +=
  1295. TRB_LEN(cur_trb->generic.field[2]) -
  1296. TRB_LEN(event->transfer_len);
  1297. }
  1298. }
  1299. if (trb_comp_code == COMP_STOP_INVAL ||
  1300. trb_comp_code == COMP_STOP) {
  1301. /* The Endpoint Stop Command completion will take care of any
  1302. * stopped TDs. A stopped TD may be restarted, so don't update
  1303. * the ring dequeue pointer or take this TD off any lists yet.
  1304. */
  1305. ep->stopped_td = td;
  1306. ep->stopped_trb = event_trb;
  1307. } else {
  1308. if (trb_comp_code == COMP_STALL) {
  1309. /* The transfer is completed from the driver's
  1310. * perspective, but we need to issue a set dequeue
  1311. * command for this stalled endpoint to move the dequeue
  1312. * pointer past the TD. We can't do that here because
  1313. * the halt condition must be cleared first. Let the
  1314. * USB class driver clear the stall later.
  1315. */
  1316. ep->stopped_td = td;
  1317. ep->stopped_trb = event_trb;
  1318. } else if (xhci_requires_manual_halt_cleanup(xhci,
  1319. ep_ctx, trb_comp_code)) {
  1320. /* Other types of errors halt the endpoint, but the
  1321. * class driver doesn't call usb_reset_endpoint() unless
  1322. * the error is -EPIPE. Clear the halted status in the
  1323. * xHCI hardware manually.
  1324. */
  1325. xhci_cleanup_halted_endpoint(xhci,
  1326. slot_id, ep_index, td, event_trb);
  1327. } else {
  1328. /* Update ring dequeue pointer */
  1329. while (ep_ring->dequeue != td->last_trb)
  1330. inc_deq(xhci, ep_ring, false);
  1331. inc_deq(xhci, ep_ring, false);
  1332. }
  1333. td_cleanup:
  1334. /* Clean up the endpoint's TD list */
  1335. urb = td->urb;
  1336. /* Do one last check of the actual transfer length.
  1337. * If the host controller said we transferred more data than
  1338. * the buffer length, urb->actual_length will be a very big
  1339. * number (since it's unsigned). Play it safe and say we didn't
  1340. * transfer anything.
  1341. */
  1342. if (urb->actual_length > urb->transfer_buffer_length) {
  1343. xhci_warn(xhci, "URB transfer length is wrong, "
  1344. "xHC issue? req. len = %u, "
  1345. "act. len = %u\n",
  1346. urb->transfer_buffer_length,
  1347. urb->actual_length);
  1348. urb->actual_length = 0;
  1349. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1350. status = -EREMOTEIO;
  1351. else
  1352. status = 0;
  1353. }
  1354. list_del(&td->td_list);
  1355. /* Was this TD slated to be cancelled but completed anyway? */
  1356. if (!list_empty(&td->cancelled_td_list))
  1357. list_del(&td->cancelled_td_list);
  1358. /* Leave the TD around for the reset endpoint function to use
  1359. * (but only if it's not a control endpoint, since we already
  1360. * queued the Set TR dequeue pointer command for stalled
  1361. * control endpoints).
  1362. */
  1363. if (usb_endpoint_xfer_control(&urb->ep->desc) ||
  1364. (trb_comp_code != COMP_STALL &&
  1365. trb_comp_code != COMP_BABBLE)) {
  1366. kfree(td);
  1367. }
  1368. urb->hcpriv = NULL;
  1369. }
  1370. cleanup:
  1371. inc_deq(xhci, xhci->event_ring, true);
  1372. xhci_set_hc_event_deq(xhci);
  1373. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1374. if (urb) {
  1375. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1376. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1377. urb, urb->actual_length, status);
  1378. spin_unlock(&xhci->lock);
  1379. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1380. spin_lock(&xhci->lock);
  1381. }
  1382. return 0;
  1383. }
  1384. /*
  1385. * This function handles all OS-owned events on the event ring. It may drop
  1386. * xhci->lock between event processing (e.g. to pass up port status changes).
  1387. */
  1388. void xhci_handle_event(struct xhci_hcd *xhci)
  1389. {
  1390. union xhci_trb *event;
  1391. int update_ptrs = 1;
  1392. int ret;
  1393. xhci_dbg(xhci, "In %s\n", __func__);
  1394. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1395. xhci->error_bitmask |= 1 << 1;
  1396. return;
  1397. }
  1398. event = xhci->event_ring->dequeue;
  1399. /* Does the HC or OS own the TRB? */
  1400. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1401. xhci->event_ring->cycle_state) {
  1402. xhci->error_bitmask |= 1 << 2;
  1403. return;
  1404. }
  1405. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1406. /* FIXME: Handle more event types. */
  1407. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1408. case TRB_TYPE(TRB_COMPLETION):
  1409. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1410. handle_cmd_completion(xhci, &event->event_cmd);
  1411. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1412. break;
  1413. case TRB_TYPE(TRB_PORT_STATUS):
  1414. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1415. handle_port_status(xhci, event);
  1416. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1417. update_ptrs = 0;
  1418. break;
  1419. case TRB_TYPE(TRB_TRANSFER):
  1420. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1421. ret = handle_tx_event(xhci, &event->trans_event);
  1422. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1423. if (ret < 0)
  1424. xhci->error_bitmask |= 1 << 9;
  1425. else
  1426. update_ptrs = 0;
  1427. break;
  1428. default:
  1429. xhci->error_bitmask |= 1 << 3;
  1430. }
  1431. /* Any of the above functions may drop and re-acquire the lock, so check
  1432. * to make sure a watchdog timer didn't mark the host as non-responsive.
  1433. */
  1434. if (xhci->xhc_state & XHCI_STATE_DYING) {
  1435. xhci_dbg(xhci, "xHCI host dying, returning from "
  1436. "event handler.\n");
  1437. return;
  1438. }
  1439. if (update_ptrs) {
  1440. /* Update SW and HC event ring dequeue pointer */
  1441. inc_deq(xhci, xhci->event_ring, true);
  1442. xhci_set_hc_event_deq(xhci);
  1443. }
  1444. /* Are there more items on the event ring? */
  1445. xhci_handle_event(xhci);
  1446. }
  1447. /**** Endpoint Ring Operations ****/
  1448. /*
  1449. * Generic function for queueing a TRB on a ring.
  1450. * The caller must have checked to make sure there's room on the ring.
  1451. */
  1452. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1453. bool consumer,
  1454. u32 field1, u32 field2, u32 field3, u32 field4)
  1455. {
  1456. struct xhci_generic_trb *trb;
  1457. trb = &ring->enqueue->generic;
  1458. trb->field[0] = field1;
  1459. trb->field[1] = field2;
  1460. trb->field[2] = field3;
  1461. trb->field[3] = field4;
  1462. inc_enq(xhci, ring, consumer);
  1463. }
  1464. /*
  1465. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1466. * FIXME allocate segments if the ring is full.
  1467. */
  1468. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1469. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1470. {
  1471. /* Make sure the endpoint has been added to xHC schedule */
  1472. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1473. switch (ep_state) {
  1474. case EP_STATE_DISABLED:
  1475. /*
  1476. * USB core changed config/interfaces without notifying us,
  1477. * or hardware is reporting the wrong state.
  1478. */
  1479. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1480. return -ENOENT;
  1481. case EP_STATE_ERROR:
  1482. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1483. /* FIXME event handling code for error needs to clear it */
  1484. /* XXX not sure if this should be -ENOENT or not */
  1485. return -EINVAL;
  1486. case EP_STATE_HALTED:
  1487. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1488. case EP_STATE_STOPPED:
  1489. case EP_STATE_RUNNING:
  1490. break;
  1491. default:
  1492. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1493. /*
  1494. * FIXME issue Configure Endpoint command to try to get the HC
  1495. * back into a known state.
  1496. */
  1497. return -EINVAL;
  1498. }
  1499. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1500. /* FIXME allocate more room */
  1501. xhci_err(xhci, "ERROR no room on ep ring\n");
  1502. return -ENOMEM;
  1503. }
  1504. return 0;
  1505. }
  1506. static int prepare_transfer(struct xhci_hcd *xhci,
  1507. struct xhci_virt_device *xdev,
  1508. unsigned int ep_index,
  1509. unsigned int num_trbs,
  1510. struct urb *urb,
  1511. struct xhci_td **td,
  1512. gfp_t mem_flags)
  1513. {
  1514. int ret;
  1515. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1516. ret = prepare_ring(xhci, xdev->eps[ep_index].ring,
  1517. ep_ctx->ep_info & EP_STATE_MASK,
  1518. num_trbs, mem_flags);
  1519. if (ret)
  1520. return ret;
  1521. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1522. if (!*td)
  1523. return -ENOMEM;
  1524. INIT_LIST_HEAD(&(*td)->td_list);
  1525. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1526. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1527. if (unlikely(ret)) {
  1528. kfree(*td);
  1529. return ret;
  1530. }
  1531. (*td)->urb = urb;
  1532. urb->hcpriv = (void *) (*td);
  1533. /* Add this TD to the tail of the endpoint ring's TD list */
  1534. list_add_tail(&(*td)->td_list, &xdev->eps[ep_index].ring->td_list);
  1535. (*td)->start_seg = xdev->eps[ep_index].ring->enq_seg;
  1536. (*td)->first_trb = xdev->eps[ep_index].ring->enqueue;
  1537. return 0;
  1538. }
  1539. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1540. {
  1541. int num_sgs, num_trbs, running_total, temp, i;
  1542. struct scatterlist *sg;
  1543. sg = NULL;
  1544. num_sgs = urb->num_sgs;
  1545. temp = urb->transfer_buffer_length;
  1546. xhci_dbg(xhci, "count sg list trbs: \n");
  1547. num_trbs = 0;
  1548. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1549. unsigned int previous_total_trbs = num_trbs;
  1550. unsigned int len = sg_dma_len(sg);
  1551. /* Scatter gather list entries may cross 64KB boundaries */
  1552. running_total = TRB_MAX_BUFF_SIZE -
  1553. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1554. if (running_total != 0)
  1555. num_trbs++;
  1556. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1557. while (running_total < sg_dma_len(sg)) {
  1558. num_trbs++;
  1559. running_total += TRB_MAX_BUFF_SIZE;
  1560. }
  1561. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1562. i, (unsigned long long)sg_dma_address(sg),
  1563. len, len, num_trbs - previous_total_trbs);
  1564. len = min_t(int, len, temp);
  1565. temp -= len;
  1566. if (temp == 0)
  1567. break;
  1568. }
  1569. xhci_dbg(xhci, "\n");
  1570. if (!in_interrupt())
  1571. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1572. urb->ep->desc.bEndpointAddress,
  1573. urb->transfer_buffer_length,
  1574. num_trbs);
  1575. return num_trbs;
  1576. }
  1577. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1578. {
  1579. if (num_trbs != 0)
  1580. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1581. "TRBs, %d left\n", __func__,
  1582. urb->ep->desc.bEndpointAddress, num_trbs);
  1583. if (running_total != urb->transfer_buffer_length)
  1584. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1585. "queued %#x (%d), asked for %#x (%d)\n",
  1586. __func__,
  1587. urb->ep->desc.bEndpointAddress,
  1588. running_total, running_total,
  1589. urb->transfer_buffer_length,
  1590. urb->transfer_buffer_length);
  1591. }
  1592. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1593. unsigned int ep_index, int start_cycle,
  1594. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1595. {
  1596. /*
  1597. * Pass all the TRBs to the hardware at once and make sure this write
  1598. * isn't reordered.
  1599. */
  1600. wmb();
  1601. start_trb->field[3] |= start_cycle;
  1602. ring_ep_doorbell(xhci, slot_id, ep_index);
  1603. }
  1604. /*
  1605. * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
  1606. * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
  1607. * (comprised of sg list entries) can take several service intervals to
  1608. * transmit.
  1609. */
  1610. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1611. struct urb *urb, int slot_id, unsigned int ep_index)
  1612. {
  1613. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
  1614. xhci->devs[slot_id]->out_ctx, ep_index);
  1615. int xhci_interval;
  1616. int ep_interval;
  1617. xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
  1618. ep_interval = urb->interval;
  1619. /* Convert to microframes */
  1620. if (urb->dev->speed == USB_SPEED_LOW ||
  1621. urb->dev->speed == USB_SPEED_FULL)
  1622. ep_interval *= 8;
  1623. /* FIXME change this to a warning and a suggestion to use the new API
  1624. * to set the polling interval (once the API is added).
  1625. */
  1626. if (xhci_interval != ep_interval) {
  1627. if (!printk_ratelimit())
  1628. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  1629. " (%d microframe%s) than xHCI "
  1630. "(%d microframe%s)\n",
  1631. ep_interval,
  1632. ep_interval == 1 ? "" : "s",
  1633. xhci_interval,
  1634. xhci_interval == 1 ? "" : "s");
  1635. urb->interval = xhci_interval;
  1636. /* Convert back to frames for LS/FS devices */
  1637. if (urb->dev->speed == USB_SPEED_LOW ||
  1638. urb->dev->speed == USB_SPEED_FULL)
  1639. urb->interval /= 8;
  1640. }
  1641. return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
  1642. }
  1643. /*
  1644. * The TD size is the number of bytes remaining in the TD (including this TRB),
  1645. * right shifted by 10.
  1646. * It must fit in bits 21:17, so it can't be bigger than 31.
  1647. */
  1648. static u32 xhci_td_remainder(unsigned int remainder)
  1649. {
  1650. u32 max = (1 << (21 - 17 + 1)) - 1;
  1651. if ((remainder >> 10) >= max)
  1652. return max << 17;
  1653. else
  1654. return (remainder >> 10) << 17;
  1655. }
  1656. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1657. struct urb *urb, int slot_id, unsigned int ep_index)
  1658. {
  1659. struct xhci_ring *ep_ring;
  1660. unsigned int num_trbs;
  1661. struct xhci_td *td;
  1662. struct scatterlist *sg;
  1663. int num_sgs;
  1664. int trb_buff_len, this_sg_len, running_total;
  1665. bool first_trb;
  1666. u64 addr;
  1667. struct xhci_generic_trb *start_trb;
  1668. int start_cycle;
  1669. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1670. num_trbs = count_sg_trbs_needed(xhci, urb);
  1671. num_sgs = urb->num_sgs;
  1672. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1673. ep_index, num_trbs, urb, &td, mem_flags);
  1674. if (trb_buff_len < 0)
  1675. return trb_buff_len;
  1676. /*
  1677. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1678. * until we've finished creating all the other TRBs. The ring's cycle
  1679. * state may change as we enqueue the other TRBs, so save it too.
  1680. */
  1681. start_trb = &ep_ring->enqueue->generic;
  1682. start_cycle = ep_ring->cycle_state;
  1683. running_total = 0;
  1684. /*
  1685. * How much data is in the first TRB?
  1686. *
  1687. * There are three forces at work for TRB buffer pointers and lengths:
  1688. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1689. * 2. The transfer length that the driver requested may be smaller than
  1690. * the amount of memory allocated for this scatter-gather list.
  1691. * 3. TRBs buffers can't cross 64KB boundaries.
  1692. */
  1693. sg = urb->sg->sg;
  1694. addr = (u64) sg_dma_address(sg);
  1695. this_sg_len = sg_dma_len(sg);
  1696. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1697. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1698. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1699. if (trb_buff_len > urb->transfer_buffer_length)
  1700. trb_buff_len = urb->transfer_buffer_length;
  1701. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1702. trb_buff_len);
  1703. first_trb = true;
  1704. /* Queue the first TRB, even if it's zero-length */
  1705. do {
  1706. u32 field = 0;
  1707. u32 length_field = 0;
  1708. u32 remainder = 0;
  1709. /* Don't change the cycle bit of the first TRB until later */
  1710. if (first_trb)
  1711. first_trb = false;
  1712. else
  1713. field |= ep_ring->cycle_state;
  1714. /* Chain all the TRBs together; clear the chain bit in the last
  1715. * TRB to indicate it's the last TRB in the chain.
  1716. */
  1717. if (num_trbs > 1) {
  1718. field |= TRB_CHAIN;
  1719. } else {
  1720. /* FIXME - add check for ZERO_PACKET flag before this */
  1721. td->last_trb = ep_ring->enqueue;
  1722. field |= TRB_IOC;
  1723. }
  1724. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1725. "64KB boundary at %#x, end dma = %#x\n",
  1726. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1727. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1728. (unsigned int) addr + trb_buff_len);
  1729. if (TRB_MAX_BUFF_SIZE -
  1730. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1731. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1732. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1733. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1734. (unsigned int) addr + trb_buff_len);
  1735. }
  1736. remainder = xhci_td_remainder(urb->transfer_buffer_length -
  1737. running_total) ;
  1738. length_field = TRB_LEN(trb_buff_len) |
  1739. remainder |
  1740. TRB_INTR_TARGET(0);
  1741. queue_trb(xhci, ep_ring, false,
  1742. lower_32_bits(addr),
  1743. upper_32_bits(addr),
  1744. length_field,
  1745. /* We always want to know if the TRB was short,
  1746. * or we won't get an event when it completes.
  1747. * (Unless we use event data TRBs, which are a
  1748. * waste of space and HC resources.)
  1749. */
  1750. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1751. --num_trbs;
  1752. running_total += trb_buff_len;
  1753. /* Calculate length for next transfer --
  1754. * Are we done queueing all the TRBs for this sg entry?
  1755. */
  1756. this_sg_len -= trb_buff_len;
  1757. if (this_sg_len == 0) {
  1758. --num_sgs;
  1759. if (num_sgs == 0)
  1760. break;
  1761. sg = sg_next(sg);
  1762. addr = (u64) sg_dma_address(sg);
  1763. this_sg_len = sg_dma_len(sg);
  1764. } else {
  1765. addr += trb_buff_len;
  1766. }
  1767. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1768. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1769. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1770. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1771. trb_buff_len =
  1772. urb->transfer_buffer_length - running_total;
  1773. } while (running_total < urb->transfer_buffer_length);
  1774. check_trb_math(urb, num_trbs, running_total);
  1775. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1776. return 0;
  1777. }
  1778. /* This is very similar to what ehci-q.c qtd_fill() does */
  1779. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1780. struct urb *urb, int slot_id, unsigned int ep_index)
  1781. {
  1782. struct xhci_ring *ep_ring;
  1783. struct xhci_td *td;
  1784. int num_trbs;
  1785. struct xhci_generic_trb *start_trb;
  1786. bool first_trb;
  1787. int start_cycle;
  1788. u32 field, length_field;
  1789. int running_total, trb_buff_len, ret;
  1790. u64 addr;
  1791. if (urb->sg)
  1792. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1793. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1794. num_trbs = 0;
  1795. /* How much data is (potentially) left before the 64KB boundary? */
  1796. running_total = TRB_MAX_BUFF_SIZE -
  1797. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1798. /* If there's some data on this 64KB chunk, or we have to send a
  1799. * zero-length transfer, we need at least one TRB
  1800. */
  1801. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1802. num_trbs++;
  1803. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1804. while (running_total < urb->transfer_buffer_length) {
  1805. num_trbs++;
  1806. running_total += TRB_MAX_BUFF_SIZE;
  1807. }
  1808. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1809. if (!in_interrupt())
  1810. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1811. urb->ep->desc.bEndpointAddress,
  1812. urb->transfer_buffer_length,
  1813. urb->transfer_buffer_length,
  1814. (unsigned long long)urb->transfer_dma,
  1815. num_trbs);
  1816. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1817. num_trbs, urb, &td, mem_flags);
  1818. if (ret < 0)
  1819. return ret;
  1820. /*
  1821. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1822. * until we've finished creating all the other TRBs. The ring's cycle
  1823. * state may change as we enqueue the other TRBs, so save it too.
  1824. */
  1825. start_trb = &ep_ring->enqueue->generic;
  1826. start_cycle = ep_ring->cycle_state;
  1827. running_total = 0;
  1828. /* How much data is in the first TRB? */
  1829. addr = (u64) urb->transfer_dma;
  1830. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1831. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1832. if (urb->transfer_buffer_length < trb_buff_len)
  1833. trb_buff_len = urb->transfer_buffer_length;
  1834. first_trb = true;
  1835. /* Queue the first TRB, even if it's zero-length */
  1836. do {
  1837. u32 remainder = 0;
  1838. field = 0;
  1839. /* Don't change the cycle bit of the first TRB until later */
  1840. if (first_trb)
  1841. first_trb = false;
  1842. else
  1843. field |= ep_ring->cycle_state;
  1844. /* Chain all the TRBs together; clear the chain bit in the last
  1845. * TRB to indicate it's the last TRB in the chain.
  1846. */
  1847. if (num_trbs > 1) {
  1848. field |= TRB_CHAIN;
  1849. } else {
  1850. /* FIXME - add check for ZERO_PACKET flag before this */
  1851. td->last_trb = ep_ring->enqueue;
  1852. field |= TRB_IOC;
  1853. }
  1854. remainder = xhci_td_remainder(urb->transfer_buffer_length -
  1855. running_total);
  1856. length_field = TRB_LEN(trb_buff_len) |
  1857. remainder |
  1858. TRB_INTR_TARGET(0);
  1859. queue_trb(xhci, ep_ring, false,
  1860. lower_32_bits(addr),
  1861. upper_32_bits(addr),
  1862. length_field,
  1863. /* We always want to know if the TRB was short,
  1864. * or we won't get an event when it completes.
  1865. * (Unless we use event data TRBs, which are a
  1866. * waste of space and HC resources.)
  1867. */
  1868. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1869. --num_trbs;
  1870. running_total += trb_buff_len;
  1871. /* Calculate length for next transfer */
  1872. addr += trb_buff_len;
  1873. trb_buff_len = urb->transfer_buffer_length - running_total;
  1874. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1875. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1876. } while (running_total < urb->transfer_buffer_length);
  1877. check_trb_math(urb, num_trbs, running_total);
  1878. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1879. return 0;
  1880. }
  1881. /* Caller must have locked xhci->lock */
  1882. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1883. struct urb *urb, int slot_id, unsigned int ep_index)
  1884. {
  1885. struct xhci_ring *ep_ring;
  1886. int num_trbs;
  1887. int ret;
  1888. struct usb_ctrlrequest *setup;
  1889. struct xhci_generic_trb *start_trb;
  1890. int start_cycle;
  1891. u32 field, length_field;
  1892. struct xhci_td *td;
  1893. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1894. /*
  1895. * Need to copy setup packet into setup TRB, so we can't use the setup
  1896. * DMA address.
  1897. */
  1898. if (!urb->setup_packet)
  1899. return -EINVAL;
  1900. if (!in_interrupt())
  1901. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1902. slot_id, ep_index);
  1903. /* 1 TRB for setup, 1 for status */
  1904. num_trbs = 2;
  1905. /*
  1906. * Don't need to check if we need additional event data and normal TRBs,
  1907. * since data in control transfers will never get bigger than 16MB
  1908. * XXX: can we get a buffer that crosses 64KB boundaries?
  1909. */
  1910. if (urb->transfer_buffer_length > 0)
  1911. num_trbs++;
  1912. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1913. urb, &td, mem_flags);
  1914. if (ret < 0)
  1915. return ret;
  1916. /*
  1917. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1918. * until we've finished creating all the other TRBs. The ring's cycle
  1919. * state may change as we enqueue the other TRBs, so save it too.
  1920. */
  1921. start_trb = &ep_ring->enqueue->generic;
  1922. start_cycle = ep_ring->cycle_state;
  1923. /* Queue setup TRB - see section 6.4.1.2.1 */
  1924. /* FIXME better way to translate setup_packet into two u32 fields? */
  1925. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1926. queue_trb(xhci, ep_ring, false,
  1927. /* FIXME endianness is probably going to bite my ass here. */
  1928. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1929. setup->wIndex | setup->wLength << 16,
  1930. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1931. /* Immediate data in pointer */
  1932. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1933. /* If there's data, queue data TRBs */
  1934. field = 0;
  1935. length_field = TRB_LEN(urb->transfer_buffer_length) |
  1936. xhci_td_remainder(urb->transfer_buffer_length) |
  1937. TRB_INTR_TARGET(0);
  1938. if (urb->transfer_buffer_length > 0) {
  1939. if (setup->bRequestType & USB_DIR_IN)
  1940. field |= TRB_DIR_IN;
  1941. queue_trb(xhci, ep_ring, false,
  1942. lower_32_bits(urb->transfer_dma),
  1943. upper_32_bits(urb->transfer_dma),
  1944. length_field,
  1945. /* Event on short tx */
  1946. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1947. }
  1948. /* Save the DMA address of the last TRB in the TD */
  1949. td->last_trb = ep_ring->enqueue;
  1950. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1951. /* If the device sent data, the status stage is an OUT transfer */
  1952. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1953. field = 0;
  1954. else
  1955. field = TRB_DIR_IN;
  1956. queue_trb(xhci, ep_ring, false,
  1957. 0,
  1958. 0,
  1959. TRB_INTR_TARGET(0),
  1960. /* Event on completion */
  1961. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1962. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1963. return 0;
  1964. }
  1965. /**** Command Ring Operations ****/
  1966. /* Generic function for queueing a command TRB on the command ring.
  1967. * Check to make sure there's room on the command ring for one command TRB.
  1968. * Also check that there's room reserved for commands that must not fail.
  1969. * If this is a command that must not fail, meaning command_must_succeed = TRUE,
  1970. * then only check for the number of reserved spots.
  1971. * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
  1972. * because the command event handler may want to resubmit a failed command.
  1973. */
  1974. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
  1975. u32 field3, u32 field4, bool command_must_succeed)
  1976. {
  1977. int reserved_trbs = xhci->cmd_ring_reserved_trbs;
  1978. if (!command_must_succeed)
  1979. reserved_trbs++;
  1980. if (!room_on_ring(xhci, xhci->cmd_ring, reserved_trbs)) {
  1981. if (!in_interrupt())
  1982. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1983. if (command_must_succeed)
  1984. xhci_err(xhci, "ERR: Reserved TRB counting for "
  1985. "unfailable commands failed.\n");
  1986. return -ENOMEM;
  1987. }
  1988. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1989. field4 | xhci->cmd_ring->cycle_state);
  1990. return 0;
  1991. }
  1992. /* Queue a no-op command on the command ring */
  1993. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1994. {
  1995. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP), false);
  1996. }
  1997. /*
  1998. * Place a no-op command on the command ring to test the command and
  1999. * event ring.
  2000. */
  2001. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  2002. {
  2003. if (queue_cmd_noop(xhci) < 0)
  2004. return NULL;
  2005. xhci->noops_submitted++;
  2006. return xhci_ring_cmd_db;
  2007. }
  2008. /* Queue a slot enable or disable request on the command ring */
  2009. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  2010. {
  2011. return queue_command(xhci, 0, 0, 0,
  2012. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
  2013. }
  2014. /* Queue an address device command TRB */
  2015. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2016. u32 slot_id)
  2017. {
  2018. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2019. upper_32_bits(in_ctx_ptr), 0,
  2020. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
  2021. false);
  2022. }
  2023. /* Queue a configure endpoint command TRB */
  2024. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2025. u32 slot_id, bool command_must_succeed)
  2026. {
  2027. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2028. upper_32_bits(in_ctx_ptr), 0,
  2029. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
  2030. command_must_succeed);
  2031. }
  2032. /* Queue an evaluate context command TRB */
  2033. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2034. u32 slot_id)
  2035. {
  2036. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2037. upper_32_bits(in_ctx_ptr), 0,
  2038. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
  2039. false);
  2040. }
  2041. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  2042. unsigned int ep_index)
  2043. {
  2044. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2045. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2046. u32 type = TRB_TYPE(TRB_STOP_RING);
  2047. return queue_command(xhci, 0, 0, 0,
  2048. trb_slot_id | trb_ep_index | type, false);
  2049. }
  2050. /* Set Transfer Ring Dequeue Pointer command.
  2051. * This should not be used for endpoints that have streams enabled.
  2052. */
  2053. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  2054. unsigned int ep_index, struct xhci_segment *deq_seg,
  2055. union xhci_trb *deq_ptr, u32 cycle_state)
  2056. {
  2057. dma_addr_t addr;
  2058. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2059. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2060. u32 type = TRB_TYPE(TRB_SET_DEQ);
  2061. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  2062. if (addr == 0) {
  2063. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  2064. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  2065. deq_seg, deq_ptr);
  2066. return 0;
  2067. }
  2068. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  2069. upper_32_bits(addr), 0,
  2070. trb_slot_id | trb_ep_index | type, false);
  2071. }
  2072. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  2073. unsigned int ep_index)
  2074. {
  2075. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2076. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2077. u32 type = TRB_TYPE(TRB_RESET_EP);
  2078. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
  2079. false);
  2080. }