xhci-ring.c 103 KB

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