xhci-ring.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*
  23. * Ring initialization rules:
  24. * 1. Each segment is initialized to zero, except for link TRBs.
  25. * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
  26. * Consumer Cycle State (CCS), depending on ring function.
  27. * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
  28. *
  29. * Ring behavior rules:
  30. * 1. A ring is empty if enqueue == dequeue. This means there will always be at
  31. * least one free TRB in the ring. This is useful if you want to turn that
  32. * into a link TRB and expand the ring.
  33. * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
  34. * link TRB, then load the pointer with the address in the link TRB. If the
  35. * link TRB had its toggle bit set, you may need to update the ring cycle
  36. * state (see cycle bit rules). You may have to do this multiple times
  37. * until you reach a non-link TRB.
  38. * 3. A ring is full if enqueue++ (for the definition of increment above)
  39. * equals the dequeue pointer.
  40. *
  41. * Cycle bit rules:
  42. * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
  43. * in a link TRB, it must toggle the ring cycle state.
  44. * 2. When a producer increments an enqueue pointer and encounters a toggle bit
  45. * in a link TRB, it must toggle the ring cycle state.
  46. *
  47. * Producer rules:
  48. * 1. Check if ring is full before you enqueue.
  49. * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
  50. * Update enqueue pointer between each write (which may update the ring
  51. * cycle state).
  52. * 3. Notify consumer. If SW is producer, it rings the doorbell for command
  53. * and endpoint rings. If HC is the producer for the event ring,
  54. * and it generates an interrupt according to interrupt modulation rules.
  55. *
  56. * Consumer rules:
  57. * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
  58. * the TRB is owned by the consumer.
  59. * 2. Update dequeue pointer (which may update the ring cycle state) and
  60. * continue processing TRBs until you reach a TRB which is not owned by you.
  61. * 3. Notify the producer. SW is the consumer for the event ring, and it
  62. * updates event ring dequeue pointer. HC is the consumer for the command and
  63. * endpoint rings; it generates events on the event ring for these.
  64. */
  65. #include <linux/scatterlist.h>
  66. #include <linux/slab.h>
  67. #include "xhci.h"
  68. /*
  69. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  70. * address of the TRB.
  71. */
  72. dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
  73. union xhci_trb *trb)
  74. {
  75. unsigned long segment_offset;
  76. if (!seg || !trb || trb < seg->trbs)
  77. return 0;
  78. /* offset in TRBs */
  79. segment_offset = trb - seg->trbs;
  80. if (segment_offset > TRBS_PER_SEGMENT)
  81. return 0;
  82. return seg->dma + (segment_offset * sizeof(*trb));
  83. }
  84. /* Does this link TRB point to the first segment in a ring,
  85. * or was the previous TRB the last TRB on the last segment in the ERST?
  86. */
  87. static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
  88. struct xhci_segment *seg, union xhci_trb *trb)
  89. {
  90. if (ring == xhci->event_ring)
  91. return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
  92. (seg->next == xhci->event_ring->first_seg);
  93. else
  94. return trb->link.control & LINK_TOGGLE;
  95. }
  96. /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
  97. * segment? I.e. would the updated event TRB pointer step off the end of the
  98. * event seg?
  99. */
  100. static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  101. struct xhci_segment *seg, union xhci_trb *trb)
  102. {
  103. if (ring == xhci->event_ring)
  104. return trb == &seg->trbs[TRBS_PER_SEGMENT];
  105. else
  106. return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
  107. }
  108. static inline int enqueue_is_link_trb(struct xhci_ring *ring)
  109. {
  110. struct xhci_link_trb *link = &ring->enqueue->link;
  111. return ((link->control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK));
  112. }
  113. /* Updates trb to point to the next TRB in the ring, and updates seg if the next
  114. * TRB is in a new segment. This does not skip over link TRBs, and it does not
  115. * effect the ring dequeue or enqueue pointers.
  116. */
  117. static void next_trb(struct xhci_hcd *xhci,
  118. struct xhci_ring *ring,
  119. struct xhci_segment **seg,
  120. union xhci_trb **trb)
  121. {
  122. if (last_trb(xhci, ring, *seg, *trb)) {
  123. *seg = (*seg)->next;
  124. *trb = ((*seg)->trbs);
  125. } else {
  126. *trb = (*trb)++;
  127. }
  128. }
  129. /*
  130. * See Cycle bit rules. SW is the consumer for the event ring only.
  131. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  132. */
  133. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  134. {
  135. union xhci_trb *next = ++(ring->dequeue);
  136. unsigned long long addr;
  137. ring->deq_updates++;
  138. /* Update the dequeue pointer further if that was a link TRB or we're at
  139. * the end of an event ring segment (which doesn't have link TRBS)
  140. */
  141. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  142. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  143. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  144. if (!in_interrupt())
  145. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  146. ring,
  147. (unsigned int) ring->cycle_state);
  148. }
  149. ring->deq_seg = ring->deq_seg->next;
  150. ring->dequeue = ring->deq_seg->trbs;
  151. next = ring->dequeue;
  152. }
  153. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
  154. if (ring == xhci->event_ring)
  155. xhci_dbg(xhci, "Event ring deq = 0x%llx (DMA)\n", addr);
  156. else if (ring == xhci->cmd_ring)
  157. xhci_dbg(xhci, "Command ring deq = 0x%llx (DMA)\n", addr);
  158. else
  159. xhci_dbg(xhci, "Ring deq = 0x%llx (DMA)\n", addr);
  160. }
  161. /*
  162. * See Cycle bit rules. SW is the consumer for the event ring only.
  163. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  164. *
  165. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  166. * chain bit is set), then set the chain bit in all the following link TRBs.
  167. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  168. * have their chain bit cleared (so that each Link TRB is a separate TD).
  169. *
  170. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  171. * set, but other sections talk about dealing with the chain bit set. This was
  172. * fixed in the 0.96 specification errata, but we have to assume that all 0.95
  173. * xHCI hardware can't handle the chain bit being cleared on a link TRB.
  174. */
  175. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  176. {
  177. u32 chain;
  178. union xhci_trb *next;
  179. unsigned long long addr;
  180. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  181. next = ++(ring->enqueue);
  182. ring->enq_updates++;
  183. /* Update the dequeue pointer further if that was a link TRB or we're at
  184. * the end of an event ring segment (which doesn't have link TRBS)
  185. */
  186. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  187. if (!consumer) {
  188. if (ring != xhci->event_ring) {
  189. if (chain) {
  190. next->link.control |= TRB_CHAIN;
  191. /* Give this link TRB to the hardware */
  192. wmb();
  193. next->link.control ^= TRB_CYCLE;
  194. } else {
  195. break;
  196. }
  197. }
  198. /* Toggle the cycle bit after the last ring segment. */
  199. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  200. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  201. if (!in_interrupt())
  202. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  203. ring,
  204. (unsigned int) ring->cycle_state);
  205. }
  206. }
  207. ring->enq_seg = ring->enq_seg->next;
  208. ring->enqueue = ring->enq_seg->trbs;
  209. next = ring->enqueue;
  210. }
  211. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  212. if (ring == xhci->event_ring)
  213. xhci_dbg(xhci, "Event ring enq = 0x%llx (DMA)\n", addr);
  214. else if (ring == xhci->cmd_ring)
  215. xhci_dbg(xhci, "Command ring enq = 0x%llx (DMA)\n", addr);
  216. else
  217. xhci_dbg(xhci, "Ring enq = 0x%llx (DMA)\n", addr);
  218. }
  219. /*
  220. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  221. * above.
  222. * FIXME: this would be simpler and faster if we just kept track of the number
  223. * of free TRBs in a ring.
  224. */
  225. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  226. unsigned int num_trbs)
  227. {
  228. int i;
  229. union xhci_trb *enq = ring->enqueue;
  230. struct xhci_segment *enq_seg = ring->enq_seg;
  231. struct xhci_segment *cur_seg;
  232. unsigned int left_on_ring;
  233. /* If we are currently pointing to a link TRB, advance the
  234. * enqueue pointer before checking for space */
  235. while (last_trb(xhci, ring, enq_seg, enq)) {
  236. enq_seg = enq_seg->next;
  237. enq = enq_seg->trbs;
  238. }
  239. /* Check if ring is empty */
  240. if (enq == ring->dequeue) {
  241. /* Can't use link trbs */
  242. left_on_ring = TRBS_PER_SEGMENT - 1;
  243. for (cur_seg = enq_seg->next; cur_seg != enq_seg;
  244. cur_seg = cur_seg->next)
  245. left_on_ring += TRBS_PER_SEGMENT - 1;
  246. /* Always need one TRB free in the ring. */
  247. left_on_ring -= 1;
  248. if (num_trbs > left_on_ring) {
  249. xhci_warn(xhci, "Not enough room on ring; "
  250. "need %u TRBs, %u TRBs left\n",
  251. num_trbs, left_on_ring);
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. /* Make sure there's an extra empty TRB available */
  257. for (i = 0; i <= num_trbs; ++i) {
  258. if (enq == ring->dequeue)
  259. return 0;
  260. enq++;
  261. while (last_trb(xhci, ring, enq_seg, enq)) {
  262. enq_seg = enq_seg->next;
  263. enq = enq_seg->trbs;
  264. }
  265. }
  266. return 1;
  267. }
  268. void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  269. {
  270. u64 temp;
  271. dma_addr_t deq;
  272. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  273. xhci->event_ring->dequeue);
  274. if (deq == 0 && !in_interrupt())
  275. xhci_warn(xhci, "WARN something wrong with SW event ring "
  276. "dequeue ptr.\n");
  277. /* Update HC event ring dequeue pointer */
  278. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  279. temp &= ERST_PTR_MASK;
  280. /* Don't clear the EHB bit (which is RW1C) because
  281. * there might be more events to service.
  282. */
  283. temp &= ~ERST_EHB;
  284. xhci_dbg(xhci, "// Write event ring dequeue pointer, preserving EHB bit\n");
  285. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  286. &xhci->ir_set->erst_dequeue);
  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. u32 temp;
  292. xhci_dbg(xhci, "// Ding dong!\n");
  293. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  294. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  295. /* Flush PCI posted writes */
  296. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  297. }
  298. static void ring_ep_doorbell(struct xhci_hcd *xhci,
  299. unsigned int slot_id,
  300. unsigned int ep_index,
  301. unsigned int stream_id)
  302. {
  303. struct xhci_virt_ep *ep;
  304. unsigned int ep_state;
  305. u32 field;
  306. __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  307. ep = &xhci->devs[slot_id]->eps[ep_index];
  308. ep_state = ep->ep_state;
  309. /* Don't ring the doorbell for this endpoint if there are pending
  310. * cancellations because the we don't want to interrupt processing.
  311. * We don't want to restart any stream rings if there's a set dequeue
  312. * pointer command pending because the device can choose to start any
  313. * stream once the endpoint is on the HW schedule.
  314. * FIXME - check all the stream rings for pending cancellations.
  315. */
  316. if (!(ep_state & EP_HALT_PENDING) && !(ep_state & SET_DEQ_PENDING)
  317. && !(ep_state & EP_HALTED)) {
  318. field = xhci_readl(xhci, db_addr) & DB_MASK;
  319. field |= EPI_TO_DB(ep_index) | STREAM_ID_TO_DB(stream_id);
  320. xhci_writel(xhci, field, db_addr);
  321. /* Flush PCI posted writes - FIXME Matthew Wilcox says this
  322. * isn't time-critical and we shouldn't make the CPU wait for
  323. * the flush.
  324. */
  325. xhci_readl(xhci, db_addr);
  326. }
  327. }
  328. /* Ring the doorbell for any rings with pending URBs */
  329. static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
  330. unsigned int slot_id,
  331. unsigned int ep_index)
  332. {
  333. unsigned int stream_id;
  334. struct xhci_virt_ep *ep;
  335. ep = &xhci->devs[slot_id]->eps[ep_index];
  336. /* A ring has pending URBs if its TD list is not empty */
  337. if (!(ep->ep_state & EP_HAS_STREAMS)) {
  338. if (!(list_empty(&ep->ring->td_list)))
  339. ring_ep_doorbell(xhci, slot_id, ep_index, 0);
  340. return;
  341. }
  342. for (stream_id = 1; stream_id < ep->stream_info->num_streams;
  343. stream_id++) {
  344. struct xhci_stream_info *stream_info = ep->stream_info;
  345. if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
  346. ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
  347. }
  348. }
  349. /*
  350. * Find the segment that trb is in. Start searching in start_seg.
  351. * If we must move past a segment that has a link TRB with a toggle cycle state
  352. * bit set, then we will toggle the value pointed at by cycle_state.
  353. */
  354. static struct xhci_segment *find_trb_seg(
  355. struct xhci_segment *start_seg,
  356. union xhci_trb *trb, int *cycle_state)
  357. {
  358. struct xhci_segment *cur_seg = start_seg;
  359. struct xhci_generic_trb *generic_trb;
  360. while (cur_seg->trbs > trb ||
  361. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  362. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  363. if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
  364. (generic_trb->field[3] & LINK_TOGGLE))
  365. *cycle_state = ~(*cycle_state) & 0x1;
  366. cur_seg = cur_seg->next;
  367. if (cur_seg == start_seg)
  368. /* Looped over the entire list. Oops! */
  369. return NULL;
  370. }
  371. return cur_seg;
  372. }
  373. /*
  374. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  375. * Record the new state of the xHC's endpoint ring dequeue segment,
  376. * dequeue pointer, and new consumer cycle state in state.
  377. * Update our internal representation of the ring's dequeue pointer.
  378. *
  379. * We do this in three jumps:
  380. * - First we update our new ring state to be the same as when the xHC stopped.
  381. * - Then we traverse the ring to find the segment that contains
  382. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  383. * any link TRBs with the toggle cycle bit set.
  384. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  385. * if we've moved it past a link TRB with the toggle cycle bit set.
  386. */
  387. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  388. unsigned int slot_id, unsigned int ep_index,
  389. unsigned int stream_id, struct xhci_td *cur_td,
  390. struct xhci_dequeue_state *state)
  391. {
  392. struct xhci_virt_device *dev = xhci->devs[slot_id];
  393. struct xhci_ring *ep_ring;
  394. struct xhci_generic_trb *trb;
  395. struct xhci_ep_ctx *ep_ctx;
  396. dma_addr_t addr;
  397. ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
  398. ep_index, stream_id);
  399. if (!ep_ring) {
  400. xhci_warn(xhci, "WARN can't find new dequeue state "
  401. "for invalid stream ID %u.\n",
  402. stream_id);
  403. return;
  404. }
  405. state->new_cycle_state = 0;
  406. xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
  407. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  408. dev->eps[ep_index].stopped_trb,
  409. &state->new_cycle_state);
  410. if (!state->new_deq_seg)
  411. BUG();
  412. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  413. xhci_dbg(xhci, "Finding endpoint context\n");
  414. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  415. state->new_cycle_state = 0x1 & ep_ctx->deq;
  416. state->new_deq_ptr = cur_td->last_trb;
  417. xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
  418. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  419. state->new_deq_ptr,
  420. &state->new_cycle_state);
  421. if (!state->new_deq_seg)
  422. BUG();
  423. trb = &state->new_deq_ptr->generic;
  424. if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
  425. (trb->field[3] & LINK_TOGGLE))
  426. state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
  427. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  428. /* Don't update the ring cycle state for the producer (us). */
  429. xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
  430. state->new_deq_seg);
  431. addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
  432. xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
  433. (unsigned long long) addr);
  434. xhci_dbg(xhci, "Setting dequeue pointer in internal ring state.\n");
  435. ep_ring->dequeue = state->new_deq_ptr;
  436. ep_ring->deq_seg = state->new_deq_seg;
  437. }
  438. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  439. struct xhci_td *cur_td)
  440. {
  441. struct xhci_segment *cur_seg;
  442. union xhci_trb *cur_trb;
  443. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  444. true;
  445. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  446. if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
  447. TRB_TYPE(TRB_LINK)) {
  448. /* Unchain any chained Link TRBs, but
  449. * leave the pointers intact.
  450. */
  451. cur_trb->generic.field[3] &= ~TRB_CHAIN;
  452. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  453. xhci_dbg(xhci, "Address = %p (0x%llx dma); "
  454. "in seg %p (0x%llx dma)\n",
  455. cur_trb,
  456. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  457. cur_seg,
  458. (unsigned long long)cur_seg->dma);
  459. } else {
  460. cur_trb->generic.field[0] = 0;
  461. cur_trb->generic.field[1] = 0;
  462. cur_trb->generic.field[2] = 0;
  463. /* Preserve only the cycle bit of this TRB */
  464. cur_trb->generic.field[3] &= TRB_CYCLE;
  465. cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
  466. xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
  467. "in seg %p (0x%llx dma)\n",
  468. cur_trb,
  469. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  470. cur_seg,
  471. (unsigned long long)cur_seg->dma);
  472. }
  473. if (cur_trb == cur_td->last_trb)
  474. break;
  475. }
  476. }
  477. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  478. unsigned int ep_index, unsigned int stream_id,
  479. struct xhci_segment *deq_seg,
  480. union xhci_trb *deq_ptr, u32 cycle_state);
  481. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  482. unsigned int slot_id, unsigned int ep_index,
  483. unsigned int stream_id,
  484. struct xhci_dequeue_state *deq_state)
  485. {
  486. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  487. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
  488. "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
  489. deq_state->new_deq_seg,
  490. (unsigned long long)deq_state->new_deq_seg->dma,
  491. deq_state->new_deq_ptr,
  492. (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
  493. deq_state->new_cycle_state);
  494. queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
  495. deq_state->new_deq_seg,
  496. deq_state->new_deq_ptr,
  497. (u32) deq_state->new_cycle_state);
  498. /* Stop the TD queueing code from ringing the doorbell until
  499. * this command completes. The HC won't set the dequeue pointer
  500. * if the ring is running, and ringing the doorbell starts the
  501. * ring running.
  502. */
  503. ep->ep_state |= SET_DEQ_PENDING;
  504. }
  505. static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
  506. struct xhci_virt_ep *ep)
  507. {
  508. ep->ep_state &= ~EP_HALT_PENDING;
  509. /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
  510. * timer is running on another CPU, we don't decrement stop_cmds_pending
  511. * (since we didn't successfully stop the watchdog timer).
  512. */
  513. if (del_timer(&ep->stop_cmd_timer))
  514. ep->stop_cmds_pending--;
  515. }
  516. /* Must be called with xhci->lock held in interrupt context */
  517. static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
  518. struct xhci_td *cur_td, int status, char *adjective)
  519. {
  520. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  521. cur_td->urb->hcpriv = NULL;
  522. usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb);
  523. xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb);
  524. spin_unlock(&xhci->lock);
  525. usb_hcd_giveback_urb(hcd, cur_td->urb, status);
  526. kfree(cur_td);
  527. spin_lock(&xhci->lock);
  528. xhci_dbg(xhci, "%s URB given back\n", adjective);
  529. }
  530. /*
  531. * When we get a command completion for a Stop Endpoint Command, we need to
  532. * unlink any cancelled TDs from the ring. There are two ways to do that:
  533. *
  534. * 1. If the HW was in the middle of processing the TD that needs to be
  535. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  536. * in the TD with a Set Dequeue Pointer Command.
  537. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  538. * bit cleared) so that the HW will skip over them.
  539. */
  540. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  541. union xhci_trb *trb)
  542. {
  543. unsigned int slot_id;
  544. unsigned int ep_index;
  545. struct xhci_ring *ep_ring;
  546. struct xhci_virt_ep *ep;
  547. struct list_head *entry;
  548. struct xhci_td *cur_td = NULL;
  549. struct xhci_td *last_unlinked_td;
  550. struct xhci_dequeue_state deq_state;
  551. memset(&deq_state, 0, sizeof(deq_state));
  552. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  553. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  554. ep = &xhci->devs[slot_id]->eps[ep_index];
  555. if (list_empty(&ep->cancelled_td_list)) {
  556. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  557. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  558. return;
  559. }
  560. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  561. * We have the xHCI lock, so nothing can modify this list until we drop
  562. * it. We're also in the event handler, so we can't get re-interrupted
  563. * if another Stop Endpoint command completes
  564. */
  565. list_for_each(entry, &ep->cancelled_td_list) {
  566. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  567. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  568. cur_td->first_trb,
  569. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  570. ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
  571. if (!ep_ring) {
  572. /* This shouldn't happen unless a driver is mucking
  573. * with the stream ID after submission. This will
  574. * leave the TD on the hardware ring, and the hardware
  575. * will try to execute it, and may access a buffer
  576. * that has already been freed. In the best case, the
  577. * hardware will execute it, and the event handler will
  578. * ignore the completion event for that TD, since it was
  579. * removed from the td_list for that endpoint. In
  580. * short, don't muck with the stream ID after
  581. * submission.
  582. */
  583. xhci_warn(xhci, "WARN Cancelled URB %p "
  584. "has invalid stream ID %u.\n",
  585. cur_td->urb,
  586. cur_td->urb->stream_id);
  587. goto remove_finished_td;
  588. }
  589. /*
  590. * If we stopped on the TD we need to cancel, then we have to
  591. * move the xHC endpoint ring dequeue pointer past this TD.
  592. */
  593. if (cur_td == ep->stopped_td)
  594. xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
  595. cur_td->urb->stream_id,
  596. cur_td, &deq_state);
  597. else
  598. td_to_noop(xhci, ep_ring, cur_td);
  599. remove_finished_td:
  600. /*
  601. * The event handler won't see a completion for this TD anymore,
  602. * so remove it from the endpoint ring's TD list. Keep it in
  603. * the cancelled TD list for URB completion later.
  604. */
  605. list_del(&cur_td->td_list);
  606. }
  607. last_unlinked_td = cur_td;
  608. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  609. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  610. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  611. xhci_queue_new_dequeue_state(xhci,
  612. slot_id, ep_index,
  613. ep->stopped_td->urb->stream_id,
  614. &deq_state);
  615. xhci_ring_cmd_db(xhci);
  616. } else {
  617. /* Otherwise ring the doorbell(s) to restart queued transfers */
  618. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  619. }
  620. ep->stopped_td = NULL;
  621. ep->stopped_trb = NULL;
  622. /*
  623. * Drop the lock and complete the URBs in the cancelled TD list.
  624. * New TDs to be cancelled might be added to the end of the list before
  625. * we can complete all the URBs for the TDs we already unlinked.
  626. * So stop when we've completed the URB for the last TD we unlinked.
  627. */
  628. do {
  629. cur_td = list_entry(ep->cancelled_td_list.next,
  630. struct xhci_td, cancelled_td_list);
  631. list_del(&cur_td->cancelled_td_list);
  632. /* Clean up the cancelled URB */
  633. /* Doesn't matter what we pass for status, since the core will
  634. * just overwrite it (because the URB has been unlinked).
  635. */
  636. xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
  637. /* Stop processing the cancelled list if the watchdog timer is
  638. * running.
  639. */
  640. if (xhci->xhc_state & XHCI_STATE_DYING)
  641. return;
  642. } while (cur_td != last_unlinked_td);
  643. /* Return to the event handler with xhci->lock re-acquired */
  644. }
  645. /* Watchdog timer function for when a stop endpoint command fails to complete.
  646. * In this case, we assume the host controller is broken or dying or dead. The
  647. * host may still be completing some other events, so we have to be careful to
  648. * let the event ring handler and the URB dequeueing/enqueueing functions know
  649. * through xhci->state.
  650. *
  651. * The timer may also fire if the host takes a very long time to respond to the
  652. * command, and the stop endpoint command completion handler cannot delete the
  653. * timer before the timer function is called. Another endpoint cancellation may
  654. * sneak in before the timer function can grab the lock, and that may queue
  655. * another stop endpoint command and add the timer back. So we cannot use a
  656. * simple flag to say whether there is a pending stop endpoint command for a
  657. * particular endpoint.
  658. *
  659. * Instead we use a combination of that flag and a counter for the number of
  660. * pending stop endpoint commands. If the timer is the tail end of the last
  661. * stop endpoint command, and the endpoint's command is still pending, we assume
  662. * the host is dying.
  663. */
  664. void xhci_stop_endpoint_command_watchdog(unsigned long arg)
  665. {
  666. struct xhci_hcd *xhci;
  667. struct xhci_virt_ep *ep;
  668. struct xhci_virt_ep *temp_ep;
  669. struct xhci_ring *ring;
  670. struct xhci_td *cur_td;
  671. int ret, i, j;
  672. ep = (struct xhci_virt_ep *) arg;
  673. xhci = ep->xhci;
  674. spin_lock(&xhci->lock);
  675. ep->stop_cmds_pending--;
  676. if (xhci->xhc_state & XHCI_STATE_DYING) {
  677. xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
  678. "xHCI as DYING, exiting.\n");
  679. spin_unlock(&xhci->lock);
  680. return;
  681. }
  682. if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
  683. xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
  684. "exiting.\n");
  685. spin_unlock(&xhci->lock);
  686. return;
  687. }
  688. xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
  689. xhci_warn(xhci, "Assuming host is dying, halting host.\n");
  690. /* Oops, HC is dead or dying or at least not responding to the stop
  691. * endpoint command.
  692. */
  693. xhci->xhc_state |= XHCI_STATE_DYING;
  694. /* Disable interrupts from the host controller and start halting it */
  695. xhci_quiesce(xhci);
  696. spin_unlock(&xhci->lock);
  697. ret = xhci_halt(xhci);
  698. spin_lock(&xhci->lock);
  699. if (ret < 0) {
  700. /* This is bad; the host is not responding to commands and it's
  701. * not allowing itself to be halted. At least interrupts are
  702. * disabled, so we can set HC_STATE_HALT and notify the
  703. * USB core. But if we call usb_hc_died(), it will attempt to
  704. * disconnect all device drivers under this host. Those
  705. * disconnect() methods will wait for all URBs to be unlinked,
  706. * so we must complete them.
  707. */
  708. xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
  709. xhci_warn(xhci, "Completing active URBs anyway.\n");
  710. /* We could turn all TDs on the rings to no-ops. This won't
  711. * help if the host has cached part of the ring, and is slow if
  712. * we want to preserve the cycle bit. Skip it and hope the host
  713. * doesn't touch the memory.
  714. */
  715. }
  716. for (i = 0; i < MAX_HC_SLOTS; i++) {
  717. if (!xhci->devs[i])
  718. continue;
  719. for (j = 0; j < 31; j++) {
  720. temp_ep = &xhci->devs[i]->eps[j];
  721. ring = temp_ep->ring;
  722. if (!ring)
  723. continue;
  724. xhci_dbg(xhci, "Killing URBs for slot ID %u, "
  725. "ep index %u\n", i, j);
  726. while (!list_empty(&ring->td_list)) {
  727. cur_td = list_first_entry(&ring->td_list,
  728. struct xhci_td,
  729. td_list);
  730. list_del(&cur_td->td_list);
  731. if (!list_empty(&cur_td->cancelled_td_list))
  732. list_del(&cur_td->cancelled_td_list);
  733. xhci_giveback_urb_in_irq(xhci, cur_td,
  734. -ESHUTDOWN, "killed");
  735. }
  736. while (!list_empty(&temp_ep->cancelled_td_list)) {
  737. cur_td = list_first_entry(
  738. &temp_ep->cancelled_td_list,
  739. struct xhci_td,
  740. cancelled_td_list);
  741. list_del(&cur_td->cancelled_td_list);
  742. xhci_giveback_urb_in_irq(xhci, cur_td,
  743. -ESHUTDOWN, "killed");
  744. }
  745. }
  746. }
  747. spin_unlock(&xhci->lock);
  748. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  749. xhci_dbg(xhci, "Calling usb_hc_died()\n");
  750. usb_hc_died(xhci_to_hcd(xhci));
  751. xhci_dbg(xhci, "xHCI host controller is dead.\n");
  752. }
  753. /*
  754. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  755. * we need to clear the set deq pending flag in the endpoint ring state, so that
  756. * the TD queueing code can ring the doorbell again. We also need to ring the
  757. * endpoint doorbell to restart the ring, but only if there aren't more
  758. * cancellations pending.
  759. */
  760. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  761. struct xhci_event_cmd *event,
  762. union xhci_trb *trb)
  763. {
  764. unsigned int slot_id;
  765. unsigned int ep_index;
  766. unsigned int stream_id;
  767. struct xhci_ring *ep_ring;
  768. struct xhci_virt_device *dev;
  769. struct xhci_ep_ctx *ep_ctx;
  770. struct xhci_slot_ctx *slot_ctx;
  771. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  772. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  773. stream_id = TRB_TO_STREAM_ID(trb->generic.field[2]);
  774. dev = xhci->devs[slot_id];
  775. ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
  776. if (!ep_ring) {
  777. xhci_warn(xhci, "WARN Set TR deq ptr command for "
  778. "freed stream ID %u\n",
  779. stream_id);
  780. /* XXX: Harmless??? */
  781. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  782. return;
  783. }
  784. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  785. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  786. if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
  787. unsigned int ep_state;
  788. unsigned int slot_state;
  789. switch (GET_COMP_CODE(event->status)) {
  790. case COMP_TRB_ERR:
  791. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  792. "of stream ID configuration\n");
  793. break;
  794. case COMP_CTX_STATE:
  795. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  796. "to incorrect slot or ep state.\n");
  797. ep_state = ep_ctx->ep_info;
  798. ep_state &= EP_STATE_MASK;
  799. slot_state = slot_ctx->dev_state;
  800. slot_state = GET_SLOT_STATE(slot_state);
  801. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  802. slot_state, ep_state);
  803. break;
  804. case COMP_EBADSLT:
  805. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  806. "slot %u was not enabled.\n", slot_id);
  807. break;
  808. default:
  809. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  810. "completion code of %u.\n",
  811. GET_COMP_CODE(event->status));
  812. break;
  813. }
  814. /* OK what do we do now? The endpoint state is hosed, and we
  815. * should never get to this point if the synchronization between
  816. * queueing, and endpoint state are correct. This might happen
  817. * if the device gets disconnected after we've finished
  818. * cancelling URBs, which might not be an error...
  819. */
  820. } else {
  821. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
  822. ep_ctx->deq);
  823. }
  824. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  825. /* Restart any rings with pending URBs */
  826. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  827. }
  828. static void handle_reset_ep_completion(struct xhci_hcd *xhci,
  829. struct xhci_event_cmd *event,
  830. union xhci_trb *trb)
  831. {
  832. int slot_id;
  833. unsigned int ep_index;
  834. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  835. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  836. /* This command will only fail if the endpoint wasn't halted,
  837. * but we don't care.
  838. */
  839. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  840. (unsigned int) GET_COMP_CODE(event->status));
  841. /* HW with the reset endpoint quirk needs to have a configure endpoint
  842. * command complete before the endpoint can be used. Queue that here
  843. * because the HW can't handle two commands being queued in a row.
  844. */
  845. if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
  846. xhci_dbg(xhci, "Queueing configure endpoint command\n");
  847. xhci_queue_configure_endpoint(xhci,
  848. xhci->devs[slot_id]->in_ctx->dma, slot_id,
  849. false);
  850. xhci_ring_cmd_db(xhci);
  851. } else {
  852. /* Clear our internal halted state and restart the ring(s) */
  853. xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
  854. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  855. }
  856. }
  857. /* Check to see if a command in the device's command queue matches this one.
  858. * Signal the completion or free the command, and return 1. Return 0 if the
  859. * completed command isn't at the head of the command list.
  860. */
  861. static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
  862. struct xhci_virt_device *virt_dev,
  863. struct xhci_event_cmd *event)
  864. {
  865. struct xhci_command *command;
  866. if (list_empty(&virt_dev->cmd_list))
  867. return 0;
  868. command = list_entry(virt_dev->cmd_list.next,
  869. struct xhci_command, cmd_list);
  870. if (xhci->cmd_ring->dequeue != command->command_trb)
  871. return 0;
  872. command->status =
  873. GET_COMP_CODE(event->status);
  874. list_del(&command->cmd_list);
  875. if (command->completion)
  876. complete(command->completion);
  877. else
  878. xhci_free_command(xhci, command);
  879. return 1;
  880. }
  881. static void handle_cmd_completion(struct xhci_hcd *xhci,
  882. struct xhci_event_cmd *event)
  883. {
  884. int slot_id = TRB_TO_SLOT_ID(event->flags);
  885. u64 cmd_dma;
  886. dma_addr_t cmd_dequeue_dma;
  887. struct xhci_input_control_ctx *ctrl_ctx;
  888. struct xhci_virt_device *virt_dev;
  889. unsigned int ep_index;
  890. struct xhci_ring *ep_ring;
  891. unsigned int ep_state;
  892. cmd_dma = event->cmd_trb;
  893. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  894. xhci->cmd_ring->dequeue);
  895. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  896. if (cmd_dequeue_dma == 0) {
  897. xhci->error_bitmask |= 1 << 4;
  898. return;
  899. }
  900. /* Does the DMA address match our internal dequeue pointer address? */
  901. if (cmd_dma != (u64) cmd_dequeue_dma) {
  902. xhci->error_bitmask |= 1 << 5;
  903. return;
  904. }
  905. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  906. case TRB_TYPE(TRB_ENABLE_SLOT):
  907. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  908. xhci->slot_id = slot_id;
  909. else
  910. xhci->slot_id = 0;
  911. complete(&xhci->addr_dev);
  912. break;
  913. case TRB_TYPE(TRB_DISABLE_SLOT):
  914. if (xhci->devs[slot_id])
  915. xhci_free_virt_device(xhci, slot_id);
  916. break;
  917. case TRB_TYPE(TRB_CONFIG_EP):
  918. virt_dev = xhci->devs[slot_id];
  919. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  920. break;
  921. /*
  922. * Configure endpoint commands can come from the USB core
  923. * configuration or alt setting changes, or because the HW
  924. * needed an extra configure endpoint command after a reset
  925. * endpoint command or streams were being configured.
  926. * If the command was for a halted endpoint, the xHCI driver
  927. * is not waiting on the configure endpoint command.
  928. */
  929. ctrl_ctx = xhci_get_input_control_ctx(xhci,
  930. virt_dev->in_ctx);
  931. /* Input ctx add_flags are the endpoint index plus one */
  932. ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
  933. /* A usb_set_interface() call directly after clearing a halted
  934. * condition may race on this quirky hardware. Not worth
  935. * worrying about, since this is prototype hardware. Not sure
  936. * if this will work for streams, but streams support was
  937. * untested on this prototype.
  938. */
  939. if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
  940. ep_index != (unsigned int) -1 &&
  941. ctrl_ctx->add_flags - SLOT_FLAG ==
  942. ctrl_ctx->drop_flags) {
  943. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  944. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  945. if (!(ep_state & EP_HALTED))
  946. goto bandwidth_change;
  947. xhci_dbg(xhci, "Completed config ep cmd - "
  948. "last ep index = %d, state = %d\n",
  949. ep_index, ep_state);
  950. /* Clear internal halted state and restart ring(s) */
  951. xhci->devs[slot_id]->eps[ep_index].ep_state &=
  952. ~EP_HALTED;
  953. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  954. break;
  955. }
  956. bandwidth_change:
  957. xhci_dbg(xhci, "Completed config ep cmd\n");
  958. xhci->devs[slot_id]->cmd_status =
  959. GET_COMP_CODE(event->status);
  960. complete(&xhci->devs[slot_id]->cmd_completion);
  961. break;
  962. case TRB_TYPE(TRB_EVAL_CONTEXT):
  963. virt_dev = xhci->devs[slot_id];
  964. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  965. break;
  966. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  967. complete(&xhci->devs[slot_id]->cmd_completion);
  968. break;
  969. case TRB_TYPE(TRB_ADDR_DEV):
  970. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  971. complete(&xhci->addr_dev);
  972. break;
  973. case TRB_TYPE(TRB_STOP_RING):
  974. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  975. break;
  976. case TRB_TYPE(TRB_SET_DEQ):
  977. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  978. break;
  979. case TRB_TYPE(TRB_CMD_NOOP):
  980. ++xhci->noops_handled;
  981. break;
  982. case TRB_TYPE(TRB_RESET_EP):
  983. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  984. break;
  985. case TRB_TYPE(TRB_RESET_DEV):
  986. xhci_dbg(xhci, "Completed reset device command.\n");
  987. slot_id = TRB_TO_SLOT_ID(
  988. xhci->cmd_ring->dequeue->generic.field[3]);
  989. virt_dev = xhci->devs[slot_id];
  990. if (virt_dev)
  991. handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
  992. else
  993. xhci_warn(xhci, "Reset device command completion "
  994. "for disabled slot %u\n", slot_id);
  995. break;
  996. default:
  997. /* Skip over unknown commands on the event ring */
  998. xhci->error_bitmask |= 1 << 6;
  999. break;
  1000. }
  1001. inc_deq(xhci, xhci->cmd_ring, false);
  1002. }
  1003. static void handle_port_status(struct xhci_hcd *xhci,
  1004. union xhci_trb *event)
  1005. {
  1006. u32 port_id;
  1007. /* Port status change events always have a successful completion code */
  1008. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  1009. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  1010. xhci->error_bitmask |= 1 << 8;
  1011. }
  1012. /* FIXME: core doesn't care about all port link state changes yet */
  1013. port_id = GET_PORT_ID(event->generic.field[0]);
  1014. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  1015. /* Update event ring dequeue pointer before dropping the lock */
  1016. inc_deq(xhci, xhci->event_ring, true);
  1017. xhci_set_hc_event_deq(xhci);
  1018. spin_unlock(&xhci->lock);
  1019. /* Pass this up to the core */
  1020. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  1021. spin_lock(&xhci->lock);
  1022. }
  1023. /*
  1024. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  1025. * at end_trb, which may be in another segment. If the suspect DMA address is a
  1026. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  1027. * returns 0.
  1028. */
  1029. struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
  1030. union xhci_trb *start_trb,
  1031. union xhci_trb *end_trb,
  1032. dma_addr_t suspect_dma)
  1033. {
  1034. dma_addr_t start_dma;
  1035. dma_addr_t end_seg_dma;
  1036. dma_addr_t end_trb_dma;
  1037. struct xhci_segment *cur_seg;
  1038. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  1039. cur_seg = start_seg;
  1040. do {
  1041. if (start_dma == 0)
  1042. return NULL;
  1043. /* We may get an event for a Link TRB in the middle of a TD */
  1044. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  1045. &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
  1046. /* If the end TRB isn't in this segment, this is set to 0 */
  1047. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  1048. if (end_trb_dma > 0) {
  1049. /* The end TRB is in this segment, so suspect should be here */
  1050. if (start_dma <= end_trb_dma) {
  1051. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  1052. return cur_seg;
  1053. } else {
  1054. /* Case for one segment with
  1055. * a TD wrapped around to the top
  1056. */
  1057. if ((suspect_dma >= start_dma &&
  1058. suspect_dma <= end_seg_dma) ||
  1059. (suspect_dma >= cur_seg->dma &&
  1060. suspect_dma <= end_trb_dma))
  1061. return cur_seg;
  1062. }
  1063. return NULL;
  1064. } else {
  1065. /* Might still be somewhere in this segment */
  1066. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  1067. return cur_seg;
  1068. }
  1069. cur_seg = cur_seg->next;
  1070. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  1071. } while (cur_seg != start_seg);
  1072. return NULL;
  1073. }
  1074. static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
  1075. unsigned int slot_id, unsigned int ep_index,
  1076. unsigned int stream_id,
  1077. struct xhci_td *td, union xhci_trb *event_trb)
  1078. {
  1079. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  1080. ep->ep_state |= EP_HALTED;
  1081. ep->stopped_td = td;
  1082. ep->stopped_trb = event_trb;
  1083. ep->stopped_stream = stream_id;
  1084. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  1085. xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
  1086. ep->stopped_td = NULL;
  1087. ep->stopped_trb = NULL;
  1088. ep->stopped_stream = 0;
  1089. xhci_ring_cmd_db(xhci);
  1090. }
  1091. /* Check if an error has halted the endpoint ring. The class driver will
  1092. * cleanup the halt for a non-default control endpoint if we indicate a stall.
  1093. * However, a babble and other errors also halt the endpoint ring, and the class
  1094. * driver won't clear the halt in that case, so we need to issue a Set Transfer
  1095. * Ring Dequeue Pointer command manually.
  1096. */
  1097. static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
  1098. struct xhci_ep_ctx *ep_ctx,
  1099. unsigned int trb_comp_code)
  1100. {
  1101. /* TRB completion codes that may require a manual halt cleanup */
  1102. if (trb_comp_code == COMP_TX_ERR ||
  1103. trb_comp_code == COMP_BABBLE ||
  1104. trb_comp_code == COMP_SPLIT_ERR)
  1105. /* The 0.96 spec says a babbling control endpoint
  1106. * is not halted. The 0.96 spec says it is. Some HW
  1107. * claims to be 0.95 compliant, but it halts the control
  1108. * endpoint anyway. Check if a babble halted the
  1109. * endpoint.
  1110. */
  1111. if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED)
  1112. return 1;
  1113. return 0;
  1114. }
  1115. int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
  1116. {
  1117. if (trb_comp_code >= 224 && trb_comp_code <= 255) {
  1118. /* Vendor defined "informational" completion code,
  1119. * treat as not-an-error.
  1120. */
  1121. xhci_dbg(xhci, "Vendor defined info completion code %u\n",
  1122. trb_comp_code);
  1123. xhci_dbg(xhci, "Treating code as success.\n");
  1124. return 1;
  1125. }
  1126. return 0;
  1127. }
  1128. /*
  1129. * If this function returns an error condition, it means it got a Transfer
  1130. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  1131. * At this point, the host controller is probably hosed and should be reset.
  1132. */
  1133. static int handle_tx_event(struct xhci_hcd *xhci,
  1134. struct xhci_transfer_event *event)
  1135. {
  1136. struct xhci_virt_device *xdev;
  1137. struct xhci_virt_ep *ep;
  1138. struct xhci_ring *ep_ring;
  1139. unsigned int slot_id;
  1140. int ep_index;
  1141. struct xhci_td *td = NULL;
  1142. dma_addr_t event_dma;
  1143. struct xhci_segment *event_seg;
  1144. union xhci_trb *event_trb;
  1145. struct urb *urb = NULL;
  1146. int status = -EINPROGRESS;
  1147. struct xhci_ep_ctx *ep_ctx;
  1148. u32 trb_comp_code;
  1149. xhci_dbg(xhci, "In %s\n", __func__);
  1150. slot_id = TRB_TO_SLOT_ID(event->flags);
  1151. xdev = xhci->devs[slot_id];
  1152. if (!xdev) {
  1153. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  1154. return -ENODEV;
  1155. }
  1156. /* Endpoint ID is 1 based, our index is zero based */
  1157. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  1158. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  1159. ep = &xdev->eps[ep_index];
  1160. ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
  1161. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1162. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  1163. xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
  1164. "or incorrect stream ring\n");
  1165. return -ENODEV;
  1166. }
  1167. event_dma = event->buffer;
  1168. /* This TRB should be in the TD at the head of this ring's TD list */
  1169. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  1170. if (list_empty(&ep_ring->td_list)) {
  1171. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  1172. TRB_TO_SLOT_ID(event->flags), ep_index);
  1173. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1174. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1175. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  1176. urb = NULL;
  1177. goto cleanup;
  1178. }
  1179. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  1180. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  1181. /* Is this a TRB in the currently executing TD? */
  1182. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  1183. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  1184. td->last_trb, event_dma);
  1185. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  1186. if (!event_seg) {
  1187. /* HC is busted, give up! */
  1188. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  1189. return -ESHUTDOWN;
  1190. }
  1191. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  1192. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1193. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1194. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  1195. lower_32_bits(event->buffer));
  1196. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  1197. upper_32_bits(event->buffer));
  1198. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  1199. (unsigned int) event->transfer_len);
  1200. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  1201. (unsigned int) event->flags);
  1202. /* Look for common error cases */
  1203. trb_comp_code = GET_COMP_CODE(event->transfer_len);
  1204. switch (trb_comp_code) {
  1205. /* Skip codes that require special handling depending on
  1206. * transfer type
  1207. */
  1208. case COMP_SUCCESS:
  1209. case COMP_SHORT_TX:
  1210. break;
  1211. case COMP_STOP:
  1212. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  1213. break;
  1214. case COMP_STOP_INVAL:
  1215. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  1216. break;
  1217. case COMP_STALL:
  1218. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  1219. ep->ep_state |= EP_HALTED;
  1220. status = -EPIPE;
  1221. break;
  1222. case COMP_TRB_ERR:
  1223. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  1224. status = -EILSEQ;
  1225. break;
  1226. case COMP_SPLIT_ERR:
  1227. case COMP_TX_ERR:
  1228. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  1229. status = -EPROTO;
  1230. break;
  1231. case COMP_BABBLE:
  1232. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  1233. status = -EOVERFLOW;
  1234. break;
  1235. case COMP_DB_ERR:
  1236. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  1237. status = -ENOSR;
  1238. break;
  1239. default:
  1240. if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
  1241. status = 0;
  1242. break;
  1243. }
  1244. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  1245. urb = NULL;
  1246. goto cleanup;
  1247. }
  1248. /* Now update the urb's actual_length and give back to the core */
  1249. /* Was this a control transfer? */
  1250. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  1251. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  1252. switch (trb_comp_code) {
  1253. case COMP_SUCCESS:
  1254. if (event_trb == ep_ring->dequeue) {
  1255. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  1256. status = -ESHUTDOWN;
  1257. } else if (event_trb != td->last_trb) {
  1258. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  1259. status = -ESHUTDOWN;
  1260. } else {
  1261. xhci_dbg(xhci, "Successful control transfer!\n");
  1262. status = 0;
  1263. }
  1264. break;
  1265. case COMP_SHORT_TX:
  1266. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  1267. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1268. status = -EREMOTEIO;
  1269. else
  1270. status = 0;
  1271. break;
  1272. default:
  1273. if (!xhci_requires_manual_halt_cleanup(xhci,
  1274. ep_ctx, trb_comp_code))
  1275. break;
  1276. xhci_dbg(xhci, "TRB error code %u, "
  1277. "halted endpoint index = %u\n",
  1278. trb_comp_code, ep_index);
  1279. /* else fall through */
  1280. case COMP_STALL:
  1281. /* Did we transfer part of the data (middle) phase? */
  1282. if (event_trb != ep_ring->dequeue &&
  1283. event_trb != td->last_trb)
  1284. td->urb->actual_length =
  1285. td->urb->transfer_buffer_length
  1286. - TRB_LEN(event->transfer_len);
  1287. else
  1288. td->urb->actual_length = 0;
  1289. xhci_cleanup_halted_endpoint(xhci,
  1290. slot_id, ep_index, 0, td, event_trb);
  1291. goto td_cleanup;
  1292. }
  1293. /*
  1294. * Did we transfer any data, despite the errors that might have
  1295. * happened? I.e. did we get past the setup stage?
  1296. */
  1297. if (event_trb != ep_ring->dequeue) {
  1298. /* The event was for the status stage */
  1299. if (event_trb == td->last_trb) {
  1300. if (td->urb->actual_length != 0) {
  1301. /* Don't overwrite a previously set error code */
  1302. if ((status == -EINPROGRESS ||
  1303. status == 0) &&
  1304. (td->urb->transfer_flags
  1305. & URB_SHORT_NOT_OK))
  1306. /* Did we already see a short data stage? */
  1307. status = -EREMOTEIO;
  1308. } else {
  1309. td->urb->actual_length =
  1310. td->urb->transfer_buffer_length;
  1311. }
  1312. } else {
  1313. /* Maybe the event was for the data stage? */
  1314. if (trb_comp_code != COMP_STOP_INVAL) {
  1315. /* We didn't stop on a link TRB in the middle */
  1316. td->urb->actual_length =
  1317. td->urb->transfer_buffer_length -
  1318. TRB_LEN(event->transfer_len);
  1319. xhci_dbg(xhci, "Waiting for status stage event\n");
  1320. urb = NULL;
  1321. goto cleanup;
  1322. }
  1323. }
  1324. }
  1325. } else {
  1326. switch (trb_comp_code) {
  1327. case COMP_SUCCESS:
  1328. /* Double check that the HW transferred everything. */
  1329. if (event_trb != td->last_trb) {
  1330. xhci_warn(xhci, "WARN Successful completion "
  1331. "on short TX\n");
  1332. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1333. status = -EREMOTEIO;
  1334. else
  1335. status = 0;
  1336. } else {
  1337. if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
  1338. xhci_dbg(xhci, "Successful bulk "
  1339. "transfer!\n");
  1340. else
  1341. xhci_dbg(xhci, "Successful interrupt "
  1342. "transfer!\n");
  1343. status = 0;
  1344. }
  1345. break;
  1346. case COMP_SHORT_TX:
  1347. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1348. status = -EREMOTEIO;
  1349. else
  1350. status = 0;
  1351. break;
  1352. default:
  1353. /* Others already handled above */
  1354. break;
  1355. }
  1356. dev_dbg(&td->urb->dev->dev,
  1357. "ep %#x - asked for %d bytes, "
  1358. "%d bytes untransferred\n",
  1359. td->urb->ep->desc.bEndpointAddress,
  1360. td->urb->transfer_buffer_length,
  1361. TRB_LEN(event->transfer_len));
  1362. /* Fast path - was this the last TRB in the TD for this URB? */
  1363. if (event_trb == td->last_trb) {
  1364. if (TRB_LEN(event->transfer_len) != 0) {
  1365. td->urb->actual_length =
  1366. td->urb->transfer_buffer_length -
  1367. TRB_LEN(event->transfer_len);
  1368. if (td->urb->transfer_buffer_length <
  1369. td->urb->actual_length) {
  1370. xhci_warn(xhci, "HC gave bad length "
  1371. "of %d bytes left\n",
  1372. TRB_LEN(event->transfer_len));
  1373. td->urb->actual_length = 0;
  1374. if (td->urb->transfer_flags &
  1375. URB_SHORT_NOT_OK)
  1376. status = -EREMOTEIO;
  1377. else
  1378. status = 0;
  1379. }
  1380. /* Don't overwrite a previously set error code */
  1381. if (status == -EINPROGRESS) {
  1382. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1383. status = -EREMOTEIO;
  1384. else
  1385. status = 0;
  1386. }
  1387. } else {
  1388. td->urb->actual_length = td->urb->transfer_buffer_length;
  1389. /* Ignore a short packet completion if the
  1390. * untransferred length was zero.
  1391. */
  1392. if (status == -EREMOTEIO)
  1393. status = 0;
  1394. }
  1395. } else {
  1396. /* Slow path - walk the list, starting from the dequeue
  1397. * pointer, to get the actual length transferred.
  1398. */
  1399. union xhci_trb *cur_trb;
  1400. struct xhci_segment *cur_seg;
  1401. td->urb->actual_length = 0;
  1402. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  1403. cur_trb != event_trb;
  1404. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1405. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  1406. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  1407. td->urb->actual_length +=
  1408. TRB_LEN(cur_trb->generic.field[2]);
  1409. }
  1410. /* If the ring didn't stop on a Link or No-op TRB, add
  1411. * in the actual bytes transferred from the Normal TRB
  1412. */
  1413. if (trb_comp_code != COMP_STOP_INVAL)
  1414. td->urb->actual_length +=
  1415. TRB_LEN(cur_trb->generic.field[2]) -
  1416. TRB_LEN(event->transfer_len);
  1417. }
  1418. }
  1419. if (trb_comp_code == COMP_STOP_INVAL ||
  1420. trb_comp_code == COMP_STOP) {
  1421. /* The Endpoint Stop Command completion will take care of any
  1422. * stopped TDs. A stopped TD may be restarted, so don't update
  1423. * the ring dequeue pointer or take this TD off any lists yet.
  1424. */
  1425. ep->stopped_td = td;
  1426. ep->stopped_trb = event_trb;
  1427. } else {
  1428. if (trb_comp_code == COMP_STALL) {
  1429. /* The transfer is completed from the driver's
  1430. * perspective, but we need to issue a set dequeue
  1431. * command for this stalled endpoint to move the dequeue
  1432. * pointer past the TD. We can't do that here because
  1433. * the halt condition must be cleared first. Let the
  1434. * USB class driver clear the stall later.
  1435. */
  1436. ep->stopped_td = td;
  1437. ep->stopped_trb = event_trb;
  1438. ep->stopped_stream = ep_ring->stream_id;
  1439. } else if (xhci_requires_manual_halt_cleanup(xhci,
  1440. ep_ctx, trb_comp_code)) {
  1441. /* Other types of errors halt the endpoint, but the
  1442. * class driver doesn't call usb_reset_endpoint() unless
  1443. * the error is -EPIPE. Clear the halted status in the
  1444. * xHCI hardware manually.
  1445. */
  1446. xhci_cleanup_halted_endpoint(xhci,
  1447. slot_id, ep_index, ep_ring->stream_id, td, event_trb);
  1448. } else {
  1449. /* Update ring dequeue pointer */
  1450. while (ep_ring->dequeue != td->last_trb)
  1451. inc_deq(xhci, ep_ring, false);
  1452. inc_deq(xhci, ep_ring, false);
  1453. }
  1454. td_cleanup:
  1455. /* Clean up the endpoint's TD list */
  1456. urb = td->urb;
  1457. /* Do one last check of the actual transfer length.
  1458. * If the host controller said we transferred more data than
  1459. * the buffer length, urb->actual_length will be a very big
  1460. * number (since it's unsigned). Play it safe and say we didn't
  1461. * transfer anything.
  1462. */
  1463. if (urb->actual_length > urb->transfer_buffer_length) {
  1464. xhci_warn(xhci, "URB transfer length is wrong, "
  1465. "xHC issue? req. len = %u, "
  1466. "act. len = %u\n",
  1467. urb->transfer_buffer_length,
  1468. urb->actual_length);
  1469. urb->actual_length = 0;
  1470. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1471. status = -EREMOTEIO;
  1472. else
  1473. status = 0;
  1474. }
  1475. list_del(&td->td_list);
  1476. /* Was this TD slated to be cancelled but completed anyway? */
  1477. if (!list_empty(&td->cancelled_td_list))
  1478. list_del(&td->cancelled_td_list);
  1479. /* Leave the TD around for the reset endpoint function to use
  1480. * (but only if it's not a control endpoint, since we already
  1481. * queued the Set TR dequeue pointer command for stalled
  1482. * control endpoints).
  1483. */
  1484. if (usb_endpoint_xfer_control(&urb->ep->desc) ||
  1485. (trb_comp_code != COMP_STALL &&
  1486. trb_comp_code != COMP_BABBLE)) {
  1487. kfree(td);
  1488. }
  1489. urb->hcpriv = NULL;
  1490. }
  1491. cleanup:
  1492. inc_deq(xhci, xhci->event_ring, true);
  1493. xhci_set_hc_event_deq(xhci);
  1494. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1495. if (urb) {
  1496. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1497. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1498. urb, urb->actual_length, status);
  1499. spin_unlock(&xhci->lock);
  1500. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1501. spin_lock(&xhci->lock);
  1502. }
  1503. return 0;
  1504. }
  1505. /*
  1506. * This function handles all OS-owned events on the event ring. It may drop
  1507. * xhci->lock between event processing (e.g. to pass up port status changes).
  1508. */
  1509. void xhci_handle_event(struct xhci_hcd *xhci)
  1510. {
  1511. union xhci_trb *event;
  1512. int update_ptrs = 1;
  1513. int ret;
  1514. xhci_dbg(xhci, "In %s\n", __func__);
  1515. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1516. xhci->error_bitmask |= 1 << 1;
  1517. return;
  1518. }
  1519. event = xhci->event_ring->dequeue;
  1520. /* Does the HC or OS own the TRB? */
  1521. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1522. xhci->event_ring->cycle_state) {
  1523. xhci->error_bitmask |= 1 << 2;
  1524. return;
  1525. }
  1526. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1527. /* FIXME: Handle more event types. */
  1528. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1529. case TRB_TYPE(TRB_COMPLETION):
  1530. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1531. handle_cmd_completion(xhci, &event->event_cmd);
  1532. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1533. break;
  1534. case TRB_TYPE(TRB_PORT_STATUS):
  1535. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1536. handle_port_status(xhci, event);
  1537. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1538. update_ptrs = 0;
  1539. break;
  1540. case TRB_TYPE(TRB_TRANSFER):
  1541. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1542. ret = handle_tx_event(xhci, &event->trans_event);
  1543. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1544. if (ret < 0)
  1545. xhci->error_bitmask |= 1 << 9;
  1546. else
  1547. update_ptrs = 0;
  1548. break;
  1549. default:
  1550. xhci->error_bitmask |= 1 << 3;
  1551. }
  1552. /* Any of the above functions may drop and re-acquire the lock, so check
  1553. * to make sure a watchdog timer didn't mark the host as non-responsive.
  1554. */
  1555. if (xhci->xhc_state & XHCI_STATE_DYING) {
  1556. xhci_dbg(xhci, "xHCI host dying, returning from "
  1557. "event handler.\n");
  1558. return;
  1559. }
  1560. if (update_ptrs) {
  1561. /* Update SW and HC event ring dequeue pointer */
  1562. inc_deq(xhci, xhci->event_ring, true);
  1563. xhci_set_hc_event_deq(xhci);
  1564. }
  1565. /* Are there more items on the event ring? */
  1566. xhci_handle_event(xhci);
  1567. }
  1568. /**** Endpoint Ring Operations ****/
  1569. /*
  1570. * Generic function for queueing a TRB on a ring.
  1571. * The caller must have checked to make sure there's room on the ring.
  1572. */
  1573. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1574. bool consumer,
  1575. u32 field1, u32 field2, u32 field3, u32 field4)
  1576. {
  1577. struct xhci_generic_trb *trb;
  1578. trb = &ring->enqueue->generic;
  1579. trb->field[0] = field1;
  1580. trb->field[1] = field2;
  1581. trb->field[2] = field3;
  1582. trb->field[3] = field4;
  1583. inc_enq(xhci, ring, consumer);
  1584. }
  1585. /*
  1586. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1587. * FIXME allocate segments if the ring is full.
  1588. */
  1589. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1590. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1591. {
  1592. /* Make sure the endpoint has been added to xHC schedule */
  1593. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1594. switch (ep_state) {
  1595. case EP_STATE_DISABLED:
  1596. /*
  1597. * USB core changed config/interfaces without notifying us,
  1598. * or hardware is reporting the wrong state.
  1599. */
  1600. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1601. return -ENOENT;
  1602. case EP_STATE_ERROR:
  1603. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1604. /* FIXME event handling code for error needs to clear it */
  1605. /* XXX not sure if this should be -ENOENT or not */
  1606. return -EINVAL;
  1607. case EP_STATE_HALTED:
  1608. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1609. case EP_STATE_STOPPED:
  1610. case EP_STATE_RUNNING:
  1611. break;
  1612. default:
  1613. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1614. /*
  1615. * FIXME issue Configure Endpoint command to try to get the HC
  1616. * back into a known state.
  1617. */
  1618. return -EINVAL;
  1619. }
  1620. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1621. /* FIXME allocate more room */
  1622. xhci_err(xhci, "ERROR no room on ep ring\n");
  1623. return -ENOMEM;
  1624. }
  1625. if (enqueue_is_link_trb(ep_ring)) {
  1626. struct xhci_ring *ring = ep_ring;
  1627. union xhci_trb *next;
  1628. unsigned long long addr;
  1629. xhci_dbg(xhci, "prepare_ring: pointing to link trb\n");
  1630. next = ring->enqueue;
  1631. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  1632. /* If we're not dealing with 0.95 hardware,
  1633. * clear the chain bit.
  1634. */
  1635. if (!xhci_link_trb_quirk(xhci))
  1636. next->link.control &= ~TRB_CHAIN;
  1637. else
  1638. next->link.control |= TRB_CHAIN;
  1639. wmb();
  1640. next->link.control ^= (u32) TRB_CYCLE;
  1641. /* Toggle the cycle bit after the last ring segment. */
  1642. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  1643. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  1644. if (!in_interrupt()) {
  1645. xhci_dbg(xhci, "queue_trb: Toggle cycle "
  1646. "state for ring %p = %i\n",
  1647. ring, (unsigned int)ring->cycle_state);
  1648. }
  1649. }
  1650. ring->enq_seg = ring->enq_seg->next;
  1651. ring->enqueue = ring->enq_seg->trbs;
  1652. next = ring->enqueue;
  1653. }
  1654. }
  1655. return 0;
  1656. }
  1657. static int prepare_transfer(struct xhci_hcd *xhci,
  1658. struct xhci_virt_device *xdev,
  1659. unsigned int ep_index,
  1660. unsigned int stream_id,
  1661. unsigned int num_trbs,
  1662. struct urb *urb,
  1663. struct xhci_td **td,
  1664. gfp_t mem_flags)
  1665. {
  1666. int ret;
  1667. struct xhci_ring *ep_ring;
  1668. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1669. ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
  1670. if (!ep_ring) {
  1671. xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
  1672. stream_id);
  1673. return -EINVAL;
  1674. }
  1675. ret = prepare_ring(xhci, ep_ring,
  1676. ep_ctx->ep_info & EP_STATE_MASK,
  1677. num_trbs, mem_flags);
  1678. if (ret)
  1679. return ret;
  1680. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1681. if (!*td)
  1682. return -ENOMEM;
  1683. INIT_LIST_HEAD(&(*td)->td_list);
  1684. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1685. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1686. if (unlikely(ret)) {
  1687. kfree(*td);
  1688. return ret;
  1689. }
  1690. (*td)->urb = urb;
  1691. urb->hcpriv = (void *) (*td);
  1692. /* Add this TD to the tail of the endpoint ring's TD list */
  1693. list_add_tail(&(*td)->td_list, &ep_ring->td_list);
  1694. (*td)->start_seg = ep_ring->enq_seg;
  1695. (*td)->first_trb = ep_ring->enqueue;
  1696. return 0;
  1697. }
  1698. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1699. {
  1700. int num_sgs, num_trbs, running_total, temp, i;
  1701. struct scatterlist *sg;
  1702. sg = NULL;
  1703. num_sgs = urb->num_sgs;
  1704. temp = urb->transfer_buffer_length;
  1705. xhci_dbg(xhci, "count sg list trbs: \n");
  1706. num_trbs = 0;
  1707. for_each_sg(urb->sg, sg, num_sgs, i) {
  1708. unsigned int previous_total_trbs = num_trbs;
  1709. unsigned int len = sg_dma_len(sg);
  1710. /* Scatter gather list entries may cross 64KB boundaries */
  1711. running_total = TRB_MAX_BUFF_SIZE -
  1712. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1713. if (running_total != 0)
  1714. num_trbs++;
  1715. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1716. while (running_total < sg_dma_len(sg)) {
  1717. num_trbs++;
  1718. running_total += TRB_MAX_BUFF_SIZE;
  1719. }
  1720. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1721. i, (unsigned long long)sg_dma_address(sg),
  1722. len, len, num_trbs - previous_total_trbs);
  1723. len = min_t(int, len, temp);
  1724. temp -= len;
  1725. if (temp == 0)
  1726. break;
  1727. }
  1728. xhci_dbg(xhci, "\n");
  1729. if (!in_interrupt())
  1730. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1731. urb->ep->desc.bEndpointAddress,
  1732. urb->transfer_buffer_length,
  1733. num_trbs);
  1734. return num_trbs;
  1735. }
  1736. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1737. {
  1738. if (num_trbs != 0)
  1739. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1740. "TRBs, %d left\n", __func__,
  1741. urb->ep->desc.bEndpointAddress, num_trbs);
  1742. if (running_total != urb->transfer_buffer_length)
  1743. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1744. "queued %#x (%d), asked for %#x (%d)\n",
  1745. __func__,
  1746. urb->ep->desc.bEndpointAddress,
  1747. running_total, running_total,
  1748. urb->transfer_buffer_length,
  1749. urb->transfer_buffer_length);
  1750. }
  1751. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1752. unsigned int ep_index, unsigned int stream_id, int start_cycle,
  1753. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1754. {
  1755. /*
  1756. * Pass all the TRBs to the hardware at once and make sure this write
  1757. * isn't reordered.
  1758. */
  1759. wmb();
  1760. start_trb->field[3] |= start_cycle;
  1761. ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
  1762. }
  1763. /*
  1764. * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
  1765. * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
  1766. * (comprised of sg list entries) can take several service intervals to
  1767. * transmit.
  1768. */
  1769. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1770. struct urb *urb, int slot_id, unsigned int ep_index)
  1771. {
  1772. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
  1773. xhci->devs[slot_id]->out_ctx, ep_index);
  1774. int xhci_interval;
  1775. int ep_interval;
  1776. xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
  1777. ep_interval = urb->interval;
  1778. /* Convert to microframes */
  1779. if (urb->dev->speed == USB_SPEED_LOW ||
  1780. urb->dev->speed == USB_SPEED_FULL)
  1781. ep_interval *= 8;
  1782. /* FIXME change this to a warning and a suggestion to use the new API
  1783. * to set the polling interval (once the API is added).
  1784. */
  1785. if (xhci_interval != ep_interval) {
  1786. if (!printk_ratelimit())
  1787. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  1788. " (%d microframe%s) than xHCI "
  1789. "(%d microframe%s)\n",
  1790. ep_interval,
  1791. ep_interval == 1 ? "" : "s",
  1792. xhci_interval,
  1793. xhci_interval == 1 ? "" : "s");
  1794. urb->interval = xhci_interval;
  1795. /* Convert back to frames for LS/FS devices */
  1796. if (urb->dev->speed == USB_SPEED_LOW ||
  1797. urb->dev->speed == USB_SPEED_FULL)
  1798. urb->interval /= 8;
  1799. }
  1800. return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
  1801. }
  1802. /*
  1803. * The TD size is the number of bytes remaining in the TD (including this TRB),
  1804. * right shifted by 10.
  1805. * It must fit in bits 21:17, so it can't be bigger than 31.
  1806. */
  1807. static u32 xhci_td_remainder(unsigned int remainder)
  1808. {
  1809. u32 max = (1 << (21 - 17 + 1)) - 1;
  1810. if ((remainder >> 10) >= max)
  1811. return max << 17;
  1812. else
  1813. return (remainder >> 10) << 17;
  1814. }
  1815. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1816. struct urb *urb, int slot_id, unsigned int ep_index)
  1817. {
  1818. struct xhci_ring *ep_ring;
  1819. unsigned int num_trbs;
  1820. struct xhci_td *td;
  1821. struct scatterlist *sg;
  1822. int num_sgs;
  1823. int trb_buff_len, this_sg_len, running_total;
  1824. bool first_trb;
  1825. u64 addr;
  1826. struct xhci_generic_trb *start_trb;
  1827. int start_cycle;
  1828. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1829. if (!ep_ring)
  1830. return -EINVAL;
  1831. num_trbs = count_sg_trbs_needed(xhci, urb);
  1832. num_sgs = urb->num_sgs;
  1833. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1834. ep_index, urb->stream_id,
  1835. num_trbs, urb, &td, mem_flags);
  1836. if (trb_buff_len < 0)
  1837. return trb_buff_len;
  1838. /*
  1839. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1840. * until we've finished creating all the other TRBs. The ring's cycle
  1841. * state may change as we enqueue the other TRBs, so save it too.
  1842. */
  1843. start_trb = &ep_ring->enqueue->generic;
  1844. start_cycle = ep_ring->cycle_state;
  1845. running_total = 0;
  1846. /*
  1847. * How much data is in the first TRB?
  1848. *
  1849. * There are three forces at work for TRB buffer pointers and lengths:
  1850. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1851. * 2. The transfer length that the driver requested may be smaller than
  1852. * the amount of memory allocated for this scatter-gather list.
  1853. * 3. TRBs buffers can't cross 64KB boundaries.
  1854. */
  1855. sg = urb->sg;
  1856. addr = (u64) sg_dma_address(sg);
  1857. this_sg_len = sg_dma_len(sg);
  1858. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1859. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1860. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1861. if (trb_buff_len > urb->transfer_buffer_length)
  1862. trb_buff_len = urb->transfer_buffer_length;
  1863. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1864. trb_buff_len);
  1865. first_trb = true;
  1866. /* Queue the first TRB, even if it's zero-length */
  1867. do {
  1868. u32 field = 0;
  1869. u32 length_field = 0;
  1870. u32 remainder = 0;
  1871. /* Don't change the cycle bit of the first TRB until later */
  1872. if (first_trb)
  1873. first_trb = false;
  1874. else
  1875. field |= ep_ring->cycle_state;
  1876. /* Chain all the TRBs together; clear the chain bit in the last
  1877. * TRB to indicate it's the last TRB in the chain.
  1878. */
  1879. if (num_trbs > 1) {
  1880. field |= TRB_CHAIN;
  1881. } else {
  1882. /* FIXME - add check for ZERO_PACKET flag before this */
  1883. td->last_trb = ep_ring->enqueue;
  1884. field |= TRB_IOC;
  1885. }
  1886. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1887. "64KB boundary at %#x, end dma = %#x\n",
  1888. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1889. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1890. (unsigned int) addr + trb_buff_len);
  1891. if (TRB_MAX_BUFF_SIZE -
  1892. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1893. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1894. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1895. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1896. (unsigned int) addr + trb_buff_len);
  1897. }
  1898. remainder = xhci_td_remainder(urb->transfer_buffer_length -
  1899. running_total) ;
  1900. length_field = TRB_LEN(trb_buff_len) |
  1901. remainder |
  1902. TRB_INTR_TARGET(0);
  1903. queue_trb(xhci, ep_ring, false,
  1904. lower_32_bits(addr),
  1905. upper_32_bits(addr),
  1906. length_field,
  1907. /* We always want to know if the TRB was short,
  1908. * or we won't get an event when it completes.
  1909. * (Unless we use event data TRBs, which are a
  1910. * waste of space and HC resources.)
  1911. */
  1912. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1913. --num_trbs;
  1914. running_total += trb_buff_len;
  1915. /* Calculate length for next transfer --
  1916. * Are we done queueing all the TRBs for this sg entry?
  1917. */
  1918. this_sg_len -= trb_buff_len;
  1919. if (this_sg_len == 0) {
  1920. --num_sgs;
  1921. if (num_sgs == 0)
  1922. break;
  1923. sg = sg_next(sg);
  1924. addr = (u64) sg_dma_address(sg);
  1925. this_sg_len = sg_dma_len(sg);
  1926. } else {
  1927. addr += trb_buff_len;
  1928. }
  1929. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1930. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1931. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1932. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1933. trb_buff_len =
  1934. urb->transfer_buffer_length - running_total;
  1935. } while (running_total < urb->transfer_buffer_length);
  1936. check_trb_math(urb, num_trbs, running_total);
  1937. giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
  1938. start_cycle, start_trb, td);
  1939. return 0;
  1940. }
  1941. /* This is very similar to what ehci-q.c qtd_fill() does */
  1942. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1943. struct urb *urb, int slot_id, unsigned int ep_index)
  1944. {
  1945. struct xhci_ring *ep_ring;
  1946. struct xhci_td *td;
  1947. int num_trbs;
  1948. struct xhci_generic_trb *start_trb;
  1949. bool first_trb;
  1950. int start_cycle;
  1951. u32 field, length_field;
  1952. int running_total, trb_buff_len, ret;
  1953. u64 addr;
  1954. if (urb->num_sgs)
  1955. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1956. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1957. if (!ep_ring)
  1958. return -EINVAL;
  1959. num_trbs = 0;
  1960. /* How much data is (potentially) left before the 64KB boundary? */
  1961. running_total = TRB_MAX_BUFF_SIZE -
  1962. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1963. /* If there's some data on this 64KB chunk, or we have to send a
  1964. * zero-length transfer, we need at least one TRB
  1965. */
  1966. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1967. num_trbs++;
  1968. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1969. while (running_total < urb->transfer_buffer_length) {
  1970. num_trbs++;
  1971. running_total += TRB_MAX_BUFF_SIZE;
  1972. }
  1973. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1974. if (!in_interrupt())
  1975. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1976. urb->ep->desc.bEndpointAddress,
  1977. urb->transfer_buffer_length,
  1978. urb->transfer_buffer_length,
  1979. (unsigned long long)urb->transfer_dma,
  1980. num_trbs);
  1981. ret = prepare_transfer(xhci, xhci->devs[slot_id],
  1982. ep_index, urb->stream_id,
  1983. num_trbs, urb, &td, mem_flags);
  1984. if (ret < 0)
  1985. return ret;
  1986. /*
  1987. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1988. * until we've finished creating all the other TRBs. The ring's cycle
  1989. * state may change as we enqueue the other TRBs, so save it too.
  1990. */
  1991. start_trb = &ep_ring->enqueue->generic;
  1992. start_cycle = ep_ring->cycle_state;
  1993. running_total = 0;
  1994. /* How much data is in the first TRB? */
  1995. addr = (u64) urb->transfer_dma;
  1996. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1997. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1998. if (urb->transfer_buffer_length < trb_buff_len)
  1999. trb_buff_len = urb->transfer_buffer_length;
  2000. first_trb = true;
  2001. /* Queue the first TRB, even if it's zero-length */
  2002. do {
  2003. u32 remainder = 0;
  2004. field = 0;
  2005. /* Don't change the cycle bit of the first TRB until later */
  2006. if (first_trb)
  2007. first_trb = false;
  2008. else
  2009. field |= ep_ring->cycle_state;
  2010. /* Chain all the TRBs together; clear the chain bit in the last
  2011. * TRB to indicate it's the last TRB in the chain.
  2012. */
  2013. if (num_trbs > 1) {
  2014. field |= TRB_CHAIN;
  2015. } else {
  2016. /* FIXME - add check for ZERO_PACKET flag before this */
  2017. td->last_trb = ep_ring->enqueue;
  2018. field |= TRB_IOC;
  2019. }
  2020. remainder = xhci_td_remainder(urb->transfer_buffer_length -
  2021. running_total);
  2022. length_field = TRB_LEN(trb_buff_len) |
  2023. remainder |
  2024. TRB_INTR_TARGET(0);
  2025. queue_trb(xhci, ep_ring, false,
  2026. lower_32_bits(addr),
  2027. upper_32_bits(addr),
  2028. length_field,
  2029. /* We always want to know if the TRB was short,
  2030. * or we won't get an event when it completes.
  2031. * (Unless we use event data TRBs, which are a
  2032. * waste of space and HC resources.)
  2033. */
  2034. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  2035. --num_trbs;
  2036. running_total += trb_buff_len;
  2037. /* Calculate length for next transfer */
  2038. addr += trb_buff_len;
  2039. trb_buff_len = urb->transfer_buffer_length - running_total;
  2040. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  2041. trb_buff_len = TRB_MAX_BUFF_SIZE;
  2042. } while (running_total < urb->transfer_buffer_length);
  2043. check_trb_math(urb, num_trbs, running_total);
  2044. giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
  2045. start_cycle, start_trb, td);
  2046. return 0;
  2047. }
  2048. /* Caller must have locked xhci->lock */
  2049. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  2050. struct urb *urb, int slot_id, unsigned int ep_index)
  2051. {
  2052. struct xhci_ring *ep_ring;
  2053. int num_trbs;
  2054. int ret;
  2055. struct usb_ctrlrequest *setup;
  2056. struct xhci_generic_trb *start_trb;
  2057. int start_cycle;
  2058. u32 field, length_field;
  2059. struct xhci_td *td;
  2060. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  2061. if (!ep_ring)
  2062. return -EINVAL;
  2063. /*
  2064. * Need to copy setup packet into setup TRB, so we can't use the setup
  2065. * DMA address.
  2066. */
  2067. if (!urb->setup_packet)
  2068. return -EINVAL;
  2069. if (!in_interrupt())
  2070. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  2071. slot_id, ep_index);
  2072. /* 1 TRB for setup, 1 for status */
  2073. num_trbs = 2;
  2074. /*
  2075. * Don't need to check if we need additional event data and normal TRBs,
  2076. * since data in control transfers will never get bigger than 16MB
  2077. * XXX: can we get a buffer that crosses 64KB boundaries?
  2078. */
  2079. if (urb->transfer_buffer_length > 0)
  2080. num_trbs++;
  2081. ret = prepare_transfer(xhci, xhci->devs[slot_id],
  2082. ep_index, urb->stream_id,
  2083. num_trbs, urb, &td, mem_flags);
  2084. if (ret < 0)
  2085. return ret;
  2086. /*
  2087. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  2088. * until we've finished creating all the other TRBs. The ring's cycle
  2089. * state may change as we enqueue the other TRBs, so save it too.
  2090. */
  2091. start_trb = &ep_ring->enqueue->generic;
  2092. start_cycle = ep_ring->cycle_state;
  2093. /* Queue setup TRB - see section 6.4.1.2.1 */
  2094. /* FIXME better way to translate setup_packet into two u32 fields? */
  2095. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  2096. queue_trb(xhci, ep_ring, false,
  2097. /* FIXME endianness is probably going to bite my ass here. */
  2098. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  2099. setup->wIndex | setup->wLength << 16,
  2100. TRB_LEN(8) | TRB_INTR_TARGET(0),
  2101. /* Immediate data in pointer */
  2102. TRB_IDT | TRB_TYPE(TRB_SETUP));
  2103. /* If there's data, queue data TRBs */
  2104. field = 0;
  2105. length_field = TRB_LEN(urb->transfer_buffer_length) |
  2106. xhci_td_remainder(urb->transfer_buffer_length) |
  2107. TRB_INTR_TARGET(0);
  2108. if (urb->transfer_buffer_length > 0) {
  2109. if (setup->bRequestType & USB_DIR_IN)
  2110. field |= TRB_DIR_IN;
  2111. queue_trb(xhci, ep_ring, false,
  2112. lower_32_bits(urb->transfer_dma),
  2113. upper_32_bits(urb->transfer_dma),
  2114. length_field,
  2115. /* Event on short tx */
  2116. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  2117. }
  2118. /* Save the DMA address of the last TRB in the TD */
  2119. td->last_trb = ep_ring->enqueue;
  2120. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  2121. /* If the device sent data, the status stage is an OUT transfer */
  2122. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  2123. field = 0;
  2124. else
  2125. field = TRB_DIR_IN;
  2126. queue_trb(xhci, ep_ring, false,
  2127. 0,
  2128. 0,
  2129. TRB_INTR_TARGET(0),
  2130. /* Event on completion */
  2131. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  2132. giveback_first_trb(xhci, slot_id, ep_index, 0,
  2133. start_cycle, start_trb, td);
  2134. return 0;
  2135. }
  2136. /**** Command Ring Operations ****/
  2137. /* Generic function for queueing a command TRB on the command ring.
  2138. * Check to make sure there's room on the command ring for one command TRB.
  2139. * Also check that there's room reserved for commands that must not fail.
  2140. * If this is a command that must not fail, meaning command_must_succeed = TRUE,
  2141. * then only check for the number of reserved spots.
  2142. * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
  2143. * because the command event handler may want to resubmit a failed command.
  2144. */
  2145. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
  2146. u32 field3, u32 field4, bool command_must_succeed)
  2147. {
  2148. int reserved_trbs = xhci->cmd_ring_reserved_trbs;
  2149. if (!command_must_succeed)
  2150. reserved_trbs++;
  2151. if (!room_on_ring(xhci, xhci->cmd_ring, reserved_trbs)) {
  2152. if (!in_interrupt())
  2153. xhci_err(xhci, "ERR: No room for command on command ring\n");
  2154. if (command_must_succeed)
  2155. xhci_err(xhci, "ERR: Reserved TRB counting for "
  2156. "unfailable commands failed.\n");
  2157. return -ENOMEM;
  2158. }
  2159. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  2160. field4 | xhci->cmd_ring->cycle_state);
  2161. return 0;
  2162. }
  2163. /* Queue a no-op command on the command ring */
  2164. static int queue_cmd_noop(struct xhci_hcd *xhci)
  2165. {
  2166. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP), false);
  2167. }
  2168. /*
  2169. * Place a no-op command on the command ring to test the command and
  2170. * event ring.
  2171. */
  2172. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  2173. {
  2174. if (queue_cmd_noop(xhci) < 0)
  2175. return NULL;
  2176. xhci->noops_submitted++;
  2177. return xhci_ring_cmd_db;
  2178. }
  2179. /* Queue a slot enable or disable request on the command ring */
  2180. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  2181. {
  2182. return queue_command(xhci, 0, 0, 0,
  2183. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
  2184. }
  2185. /* Queue an address device command TRB */
  2186. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2187. u32 slot_id)
  2188. {
  2189. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2190. upper_32_bits(in_ctx_ptr), 0,
  2191. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
  2192. false);
  2193. }
  2194. /* Queue a reset device command TRB */
  2195. int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
  2196. {
  2197. return queue_command(xhci, 0, 0, 0,
  2198. TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
  2199. false);
  2200. }
  2201. /* Queue a configure endpoint command TRB */
  2202. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2203. u32 slot_id, bool command_must_succeed)
  2204. {
  2205. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2206. upper_32_bits(in_ctx_ptr), 0,
  2207. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
  2208. command_must_succeed);
  2209. }
  2210. /* Queue an evaluate context command TRB */
  2211. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2212. u32 slot_id)
  2213. {
  2214. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2215. upper_32_bits(in_ctx_ptr), 0,
  2216. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
  2217. false);
  2218. }
  2219. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  2220. unsigned int ep_index)
  2221. {
  2222. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2223. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2224. u32 type = TRB_TYPE(TRB_STOP_RING);
  2225. return queue_command(xhci, 0, 0, 0,
  2226. trb_slot_id | trb_ep_index | type, false);
  2227. }
  2228. /* Set Transfer Ring Dequeue Pointer command.
  2229. * This should not be used for endpoints that have streams enabled.
  2230. */
  2231. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  2232. unsigned int ep_index, unsigned int stream_id,
  2233. struct xhci_segment *deq_seg,
  2234. union xhci_trb *deq_ptr, u32 cycle_state)
  2235. {
  2236. dma_addr_t addr;
  2237. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2238. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2239. u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
  2240. u32 type = TRB_TYPE(TRB_SET_DEQ);
  2241. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  2242. if (addr == 0) {
  2243. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  2244. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  2245. deq_seg, deq_ptr);
  2246. return 0;
  2247. }
  2248. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  2249. upper_32_bits(addr), trb_stream_id,
  2250. trb_slot_id | trb_ep_index | type, false);
  2251. }
  2252. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  2253. unsigned int ep_index)
  2254. {
  2255. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2256. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2257. u32 type = TRB_TYPE(TRB_RESET_EP);
  2258. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
  2259. false);
  2260. }