xhci-ring.c 110 KB

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