xhci-ring.c 77 KB

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