xhci-ring.c 78 KB

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