xhci-ring.c 94 KB

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