xhci-ring.c 110 KB

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