xhci-ring.c 102 KB

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