xhci-ring.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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 "xhci.h"
  67. /*
  68. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  69. * address of the TRB.
  70. */
  71. dma_addr_t trb_virt_to_dma(struct xhci_segment *seg,
  72. union xhci_trb *trb)
  73. {
  74. unsigned int offset;
  75. if (!seg || !trb || (void *) trb < (void *) seg->trbs)
  76. return 0;
  77. /* offset in bytes, since these are byte-addressable */
  78. offset = (unsigned int) trb - (unsigned int) seg->trbs;
  79. /* SEGMENT_SIZE in bytes, trbs are 16-byte aligned */
  80. if (offset > SEGMENT_SIZE || (offset % sizeof(*trb)) != 0)
  81. return 0;
  82. return seg->dma + offset;
  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. /*
  109. * See Cycle bit rules. SW is the consumer for the event ring only.
  110. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  111. */
  112. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  113. {
  114. union xhci_trb *next = ++(ring->dequeue);
  115. ring->deq_updates++;
  116. /* Update the dequeue pointer further if that was a link TRB or we're at
  117. * the end of an event ring segment (which doesn't have link TRBS)
  118. */
  119. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  120. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  121. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  122. if (!in_interrupt())
  123. xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n",
  124. (unsigned int) ring,
  125. (unsigned int) ring->cycle_state);
  126. }
  127. ring->deq_seg = ring->deq_seg->next;
  128. ring->dequeue = ring->deq_seg->trbs;
  129. next = ring->dequeue;
  130. }
  131. }
  132. /*
  133. * See Cycle bit rules. SW is the consumer for the event ring only.
  134. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  135. *
  136. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  137. * chain bit is set), then set the chain bit in all the following link TRBs.
  138. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  139. * have their chain bit cleared (so that each Link TRB is a separate TD).
  140. *
  141. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  142. * set, but other sections talk about dealing with the chain bit set.
  143. * Assume section 6.4.4.1 is wrong, and the chain bit can be set in a Link TRB.
  144. */
  145. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  146. {
  147. u32 chain;
  148. union xhci_trb *next;
  149. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  150. next = ++(ring->enqueue);
  151. ring->enq_updates++;
  152. /* Update the dequeue pointer further if that was a link TRB or we're at
  153. * the end of an event ring segment (which doesn't have link TRBS)
  154. */
  155. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  156. if (!consumer) {
  157. if (ring != xhci->event_ring) {
  158. /* Give this link TRB to the hardware */
  159. if (next->link.control & TRB_CYCLE)
  160. next->link.control &= (u32) ~TRB_CYCLE;
  161. else
  162. next->link.control |= (u32) TRB_CYCLE;
  163. next->link.control &= TRB_CHAIN;
  164. next->link.control |= chain;
  165. }
  166. /* Toggle the cycle bit after the last ring segment. */
  167. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  168. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  169. if (!in_interrupt())
  170. xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n",
  171. (unsigned int) ring,
  172. (unsigned int) ring->cycle_state);
  173. }
  174. }
  175. ring->enq_seg = ring->enq_seg->next;
  176. ring->enqueue = ring->enq_seg->trbs;
  177. next = ring->enqueue;
  178. }
  179. }
  180. /*
  181. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  182. * above.
  183. * FIXME: this would be simpler and faster if we just kept track of the number
  184. * of free TRBs in a ring.
  185. */
  186. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  187. unsigned int num_trbs)
  188. {
  189. int i;
  190. union xhci_trb *enq = ring->enqueue;
  191. struct xhci_segment *enq_seg = ring->enq_seg;
  192. /* Check if ring is empty */
  193. if (enq == ring->dequeue)
  194. return 1;
  195. /* Make sure there's an extra empty TRB available */
  196. for (i = 0; i <= num_trbs; ++i) {
  197. if (enq == ring->dequeue)
  198. return 0;
  199. enq++;
  200. while (last_trb(xhci, ring, enq_seg, enq)) {
  201. enq_seg = enq_seg->next;
  202. enq = enq_seg->trbs;
  203. }
  204. }
  205. return 1;
  206. }
  207. void set_hc_event_deq(struct xhci_hcd *xhci)
  208. {
  209. u32 temp;
  210. dma_addr_t deq;
  211. deq = trb_virt_to_dma(xhci->event_ring->deq_seg,
  212. xhci->event_ring->dequeue);
  213. if (deq == 0 && !in_interrupt())
  214. xhci_warn(xhci, "WARN something wrong with SW event ring "
  215. "dequeue ptr.\n");
  216. /* Update HC event ring dequeue pointer */
  217. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  218. temp &= ERST_PTR_MASK;
  219. if (!in_interrupt())
  220. xhci_dbg(xhci, "// Write event ring dequeue pointer\n");
  221. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  222. xhci_writel(xhci, (deq & ~ERST_PTR_MASK) | temp,
  223. &xhci->ir_set->erst_dequeue[0]);
  224. }
  225. /* Ring the host controller doorbell after placing a command on the ring */
  226. void ring_cmd_db(struct xhci_hcd *xhci)
  227. {
  228. u32 temp;
  229. xhci_dbg(xhci, "// Ding dong!\n");
  230. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  231. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  232. /* Flush PCI posted writes */
  233. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  234. }
  235. static void handle_cmd_completion(struct xhci_hcd *xhci,
  236. struct xhci_event_cmd *event)
  237. {
  238. int slot_id = TRB_TO_SLOT_ID(event->flags);
  239. u64 cmd_dma;
  240. dma_addr_t cmd_dequeue_dma;
  241. cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0];
  242. cmd_dequeue_dma = trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  243. xhci->cmd_ring->dequeue);
  244. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  245. if (cmd_dequeue_dma == 0) {
  246. xhci->error_bitmask |= 1 << 4;
  247. return;
  248. }
  249. /* Does the DMA address match our internal dequeue pointer address? */
  250. if (cmd_dma != (u64) cmd_dequeue_dma) {
  251. xhci->error_bitmask |= 1 << 5;
  252. return;
  253. }
  254. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  255. case TRB_TYPE(TRB_ENABLE_SLOT):
  256. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  257. xhci->slot_id = slot_id;
  258. else
  259. xhci->slot_id = 0;
  260. complete(&xhci->addr_dev);
  261. break;
  262. case TRB_TYPE(TRB_DISABLE_SLOT):
  263. if (xhci->devs[slot_id])
  264. xhci_free_virt_device(xhci, slot_id);
  265. break;
  266. case TRB_TYPE(TRB_CONFIG_EP):
  267. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  268. complete(&xhci->devs[slot_id]->cmd_completion);
  269. break;
  270. case TRB_TYPE(TRB_ADDR_DEV):
  271. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  272. complete(&xhci->addr_dev);
  273. break;
  274. case TRB_TYPE(TRB_CMD_NOOP):
  275. ++xhci->noops_handled;
  276. break;
  277. default:
  278. /* Skip over unknown commands on the event ring */
  279. xhci->error_bitmask |= 1 << 6;
  280. break;
  281. }
  282. inc_deq(xhci, xhci->cmd_ring, false);
  283. }
  284. static void handle_port_status(struct xhci_hcd *xhci,
  285. union xhci_trb *event)
  286. {
  287. u32 port_id;
  288. /* Port status change events always have a successful completion code */
  289. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  290. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  291. xhci->error_bitmask |= 1 << 8;
  292. }
  293. /* FIXME: core doesn't care about all port link state changes yet */
  294. port_id = GET_PORT_ID(event->generic.field[0]);
  295. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  296. /* Update event ring dequeue pointer before dropping the lock */
  297. inc_deq(xhci, xhci->event_ring, true);
  298. set_hc_event_deq(xhci);
  299. spin_unlock(&xhci->lock);
  300. /* Pass this up to the core */
  301. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  302. spin_lock(&xhci->lock);
  303. }
  304. /*
  305. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  306. * at end_trb, which may be in another segment. If the suspect DMA address is a
  307. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  308. * returns 0.
  309. */
  310. static struct xhci_segment *trb_in_td(
  311. struct xhci_segment *start_seg,
  312. union xhci_trb *start_trb,
  313. union xhci_trb *end_trb,
  314. dma_addr_t suspect_dma)
  315. {
  316. dma_addr_t start_dma;
  317. dma_addr_t end_seg_dma;
  318. dma_addr_t end_trb_dma;
  319. struct xhci_segment *cur_seg;
  320. start_dma = trb_virt_to_dma(start_seg, start_trb);
  321. cur_seg = start_seg;
  322. do {
  323. /*
  324. * Last TRB is a link TRB (unless we start inserting links in
  325. * the middle, FIXME if you do)
  326. */
  327. end_seg_dma = trb_virt_to_dma(cur_seg, &start_seg->trbs[TRBS_PER_SEGMENT - 2]);
  328. /* If the end TRB isn't in this segment, this is set to 0 */
  329. end_trb_dma = trb_virt_to_dma(cur_seg, end_trb);
  330. if (end_trb_dma > 0) {
  331. /* The end TRB is in this segment, so suspect should be here */
  332. if (start_dma <= end_trb_dma) {
  333. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  334. return cur_seg;
  335. } else {
  336. /* Case for one segment with
  337. * a TD wrapped around to the top
  338. */
  339. if ((suspect_dma >= start_dma &&
  340. suspect_dma <= end_seg_dma) ||
  341. (suspect_dma >= cur_seg->dma &&
  342. suspect_dma <= end_trb_dma))
  343. return cur_seg;
  344. }
  345. return 0;
  346. } else {
  347. /* Might still be somewhere in this segment */
  348. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  349. return cur_seg;
  350. }
  351. cur_seg = cur_seg->next;
  352. start_dma = trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  353. } while (1);
  354. }
  355. /*
  356. * If this function returns an error condition, it means it got a Transfer
  357. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  358. * At this point, the host controller is probably hosed and should be reset.
  359. */
  360. static int handle_tx_event(struct xhci_hcd *xhci,
  361. struct xhci_transfer_event *event)
  362. {
  363. struct xhci_virt_device *xdev;
  364. struct xhci_ring *ep_ring;
  365. int ep_index;
  366. struct xhci_td *td = 0;
  367. dma_addr_t event_dma;
  368. struct xhci_segment *event_seg;
  369. union xhci_trb *event_trb;
  370. struct urb *urb;
  371. int status = -EINPROGRESS;
  372. xdev = xhci->devs[TRB_TO_SLOT_ID(event->flags)];
  373. if (!xdev) {
  374. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  375. return -ENODEV;
  376. }
  377. /* Endpoint ID is 1 based, our index is zero based */
  378. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  379. ep_ring = xdev->ep_rings[ep_index];
  380. if (!ep_ring || (xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  381. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  382. return -ENODEV;
  383. }
  384. event_dma = event->buffer[0];
  385. if (event->buffer[1] != 0)
  386. xhci_warn(xhci, "WARN ignoring upper 32-bits of 64-bit TRB dma address\n");
  387. /* This TRB should be in the TD at the head of this ring's TD list */
  388. if (list_empty(&ep_ring->td_list)) {
  389. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  390. TRB_TO_SLOT_ID(event->flags), ep_index);
  391. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  392. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  393. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  394. urb = NULL;
  395. goto cleanup;
  396. }
  397. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  398. /* Is this a TRB in the currently executing TD? */
  399. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  400. td->last_trb, event_dma);
  401. if (!event_seg) {
  402. /* HC is busted, give up! */
  403. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  404. return -ESHUTDOWN;
  405. }
  406. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  407. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  408. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  409. xhci_dbg(xhci, "Offset 0x00 (buffer[0]) = 0x%x\n",
  410. (unsigned int) event->buffer[0]);
  411. xhci_dbg(xhci, "Offset 0x04 (buffer[0]) = 0x%x\n",
  412. (unsigned int) event->buffer[1]);
  413. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  414. (unsigned int) event->transfer_len);
  415. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  416. (unsigned int) event->flags);
  417. /* Look for common error cases */
  418. switch (GET_COMP_CODE(event->transfer_len)) {
  419. /* Skip codes that require special handling depending on
  420. * transfer type
  421. */
  422. case COMP_SUCCESS:
  423. case COMP_SHORT_TX:
  424. break;
  425. case COMP_STALL:
  426. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  427. status = -EPIPE;
  428. break;
  429. case COMP_TRB_ERR:
  430. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  431. status = -EILSEQ;
  432. break;
  433. case COMP_TX_ERR:
  434. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  435. status = -EPROTO;
  436. break;
  437. case COMP_DB_ERR:
  438. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  439. status = -ENOSR;
  440. break;
  441. default:
  442. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  443. urb = NULL;
  444. goto cleanup;
  445. }
  446. /* Now update the urb's actual_length and give back to the core */
  447. /* Was this a control transfer? */
  448. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  449. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  450. switch (GET_COMP_CODE(event->transfer_len)) {
  451. case COMP_SUCCESS:
  452. if (event_trb == ep_ring->dequeue) {
  453. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  454. status = -ESHUTDOWN;
  455. } else if (event_trb != td->last_trb) {
  456. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  457. status = -ESHUTDOWN;
  458. } else {
  459. xhci_dbg(xhci, "Successful control transfer!\n");
  460. status = 0;
  461. }
  462. break;
  463. case COMP_SHORT_TX:
  464. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  465. status = -EREMOTEIO;
  466. break;
  467. default:
  468. /* Others already handled above */
  469. break;
  470. }
  471. /*
  472. * Did we transfer any data, despite the errors that might have
  473. * happened? I.e. did we get past the setup stage?
  474. */
  475. if (event_trb != ep_ring->dequeue) {
  476. /* The event was for the status stage */
  477. if (event_trb == td->last_trb) {
  478. td->urb->actual_length = td->urb->transfer_buffer_length;
  479. } else {
  480. /* The event was for the data stage */
  481. td->urb->actual_length = td->urb->transfer_buffer_length -
  482. TRB_LEN(event->transfer_len);
  483. }
  484. }
  485. } else {
  486. switch (GET_COMP_CODE(event->transfer_len)) {
  487. case COMP_SUCCESS:
  488. /* Double check that the HW transferred everything. */
  489. if (event_trb != td->last_trb) {
  490. xhci_warn(xhci, "WARN Successful completion "
  491. "on short TX\n");
  492. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  493. status = -EREMOTEIO;
  494. else
  495. status = 0;
  496. } else {
  497. xhci_dbg(xhci, "Successful bulk transfer!\n");
  498. status = 0;
  499. }
  500. break;
  501. case COMP_SHORT_TX:
  502. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  503. status = -EREMOTEIO;
  504. else
  505. status = 0;
  506. break;
  507. default:
  508. /* Others already handled above */
  509. break;
  510. }
  511. dev_dbg(&td->urb->dev->dev,
  512. "ep %#x - asked for %d bytes, "
  513. "%d bytes untransferred\n",
  514. td->urb->ep->desc.bEndpointAddress,
  515. td->urb->transfer_buffer_length,
  516. TRB_LEN(event->transfer_len));
  517. /* Fast path - was this the last TRB in the TD for this URB? */
  518. if (event_trb == td->last_trb) {
  519. if (TRB_LEN(event->transfer_len) != 0) {
  520. td->urb->actual_length =
  521. td->urb->transfer_buffer_length -
  522. TRB_LEN(event->transfer_len);
  523. if (td->urb->actual_length < 0) {
  524. xhci_warn(xhci, "HC gave bad length "
  525. "of %d bytes left\n",
  526. TRB_LEN(event->transfer_len));
  527. td->urb->actual_length = 0;
  528. }
  529. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  530. status = -EREMOTEIO;
  531. else
  532. status = 0;
  533. } else {
  534. td->urb->actual_length = td->urb->transfer_buffer_length;
  535. /* Ignore a short packet completion if the
  536. * untransferred length was zero.
  537. */
  538. status = 0;
  539. }
  540. } else {
  541. /* Slow path - walk the list, starting from the first
  542. * TRB to get the actual length transferred
  543. */
  544. td->urb->actual_length = 0;
  545. while (ep_ring->dequeue != event_trb) {
  546. td->urb->actual_length += TRB_LEN(ep_ring->dequeue->generic.field[2]);
  547. inc_deq(xhci, ep_ring, false);
  548. }
  549. td->urb->actual_length += TRB_LEN(ep_ring->dequeue->generic.field[2]) -
  550. TRB_LEN(event->transfer_len);
  551. }
  552. }
  553. /* Update ring dequeue pointer */
  554. while (ep_ring->dequeue != td->last_trb)
  555. inc_deq(xhci, ep_ring, false);
  556. inc_deq(xhci, ep_ring, false);
  557. /* Clean up the endpoint's TD list */
  558. urb = td->urb;
  559. list_del(&td->td_list);
  560. kfree(td);
  561. urb->hcpriv = NULL;
  562. cleanup:
  563. inc_deq(xhci, xhci->event_ring, true);
  564. set_hc_event_deq(xhci);
  565. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  566. if (urb) {
  567. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  568. spin_unlock(&xhci->lock);
  569. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  570. spin_lock(&xhci->lock);
  571. }
  572. return 0;
  573. }
  574. /*
  575. * This function handles all OS-owned events on the event ring. It may drop
  576. * xhci->lock between event processing (e.g. to pass up port status changes).
  577. */
  578. void handle_event(struct xhci_hcd *xhci)
  579. {
  580. union xhci_trb *event;
  581. int update_ptrs = 1;
  582. int ret;
  583. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  584. xhci->error_bitmask |= 1 << 1;
  585. return;
  586. }
  587. event = xhci->event_ring->dequeue;
  588. /* Does the HC or OS own the TRB? */
  589. if ((event->event_cmd.flags & TRB_CYCLE) !=
  590. xhci->event_ring->cycle_state) {
  591. xhci->error_bitmask |= 1 << 2;
  592. return;
  593. }
  594. /* FIXME: Handle more event types. */
  595. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  596. case TRB_TYPE(TRB_COMPLETION):
  597. handle_cmd_completion(xhci, &event->event_cmd);
  598. break;
  599. case TRB_TYPE(TRB_PORT_STATUS):
  600. handle_port_status(xhci, event);
  601. update_ptrs = 0;
  602. break;
  603. case TRB_TYPE(TRB_TRANSFER):
  604. ret = handle_tx_event(xhci, &event->trans_event);
  605. if (ret < 0)
  606. xhci->error_bitmask |= 1 << 9;
  607. else
  608. update_ptrs = 0;
  609. break;
  610. default:
  611. xhci->error_bitmask |= 1 << 3;
  612. }
  613. if (update_ptrs) {
  614. /* Update SW and HC event ring dequeue pointer */
  615. inc_deq(xhci, xhci->event_ring, true);
  616. set_hc_event_deq(xhci);
  617. }
  618. /* Are there more items on the event ring? */
  619. handle_event(xhci);
  620. }
  621. /**** Endpoint Ring Operations ****/
  622. /*
  623. * Generic function for queueing a TRB on a ring.
  624. * The caller must have checked to make sure there's room on the ring.
  625. */
  626. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  627. bool consumer,
  628. u32 field1, u32 field2, u32 field3, u32 field4)
  629. {
  630. struct xhci_generic_trb *trb;
  631. trb = &ring->enqueue->generic;
  632. trb->field[0] = field1;
  633. trb->field[1] = field2;
  634. trb->field[2] = field3;
  635. trb->field[3] = field4;
  636. inc_enq(xhci, ring, consumer);
  637. }
  638. /*
  639. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  640. * FIXME allocate segments if the ring is full.
  641. */
  642. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  643. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  644. {
  645. /* Make sure the endpoint has been added to xHC schedule */
  646. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  647. switch (ep_state) {
  648. case EP_STATE_DISABLED:
  649. /*
  650. * USB core changed config/interfaces without notifying us,
  651. * or hardware is reporting the wrong state.
  652. */
  653. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  654. return -ENOENT;
  655. case EP_STATE_HALTED:
  656. case EP_STATE_ERROR:
  657. xhci_warn(xhci, "WARN waiting for halt or error on ep "
  658. "to be cleared\n");
  659. /* FIXME event handling code for error needs to clear it */
  660. /* XXX not sure if this should be -ENOENT or not */
  661. return -EINVAL;
  662. case EP_STATE_STOPPED:
  663. case EP_STATE_RUNNING:
  664. break;
  665. default:
  666. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  667. /*
  668. * FIXME issue Configure Endpoint command to try to get the HC
  669. * back into a known state.
  670. */
  671. return -EINVAL;
  672. }
  673. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  674. /* FIXME allocate more room */
  675. xhci_err(xhci, "ERROR no room on ep ring\n");
  676. return -ENOMEM;
  677. }
  678. return 0;
  679. }
  680. int xhci_prepare_transfer(struct xhci_hcd *xhci,
  681. struct xhci_virt_device *xdev,
  682. unsigned int ep_index,
  683. unsigned int num_trbs,
  684. struct urb *urb,
  685. struct xhci_td **td,
  686. gfp_t mem_flags)
  687. {
  688. int ret;
  689. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  690. xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK,
  691. num_trbs, mem_flags);
  692. if (ret)
  693. return ret;
  694. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  695. if (!*td)
  696. return -ENOMEM;
  697. INIT_LIST_HEAD(&(*td)->td_list);
  698. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  699. if (unlikely(ret)) {
  700. kfree(*td);
  701. return ret;
  702. }
  703. (*td)->urb = urb;
  704. urb->hcpriv = (void *) (*td);
  705. /* Add this TD to the tail of the endpoint ring's TD list */
  706. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  707. return 0;
  708. }
  709. unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  710. {
  711. int num_sgs, num_trbs, running_total, temp, i;
  712. struct scatterlist *sg;
  713. sg = NULL;
  714. num_sgs = urb->num_sgs;
  715. temp = urb->transfer_buffer_length;
  716. xhci_dbg(xhci, "count sg list trbs: \n");
  717. num_trbs = 0;
  718. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  719. unsigned int previous_total_trbs = num_trbs;
  720. unsigned int len = sg_dma_len(sg);
  721. /* Scatter gather list entries may cross 64KB boundaries */
  722. running_total = TRB_MAX_BUFF_SIZE -
  723. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  724. if (running_total != 0)
  725. num_trbs++;
  726. /* How many more 64KB chunks to transfer, how many more TRBs? */
  727. while (running_total < sg_dma_len(sg)) {
  728. num_trbs++;
  729. running_total += TRB_MAX_BUFF_SIZE;
  730. }
  731. xhci_dbg(xhci, " sg #%d: dma = %#x, len = %#x (%d), num_trbs = %d\n",
  732. i, sg_dma_address(sg), len, len,
  733. num_trbs - previous_total_trbs);
  734. len = min_t(int, len, temp);
  735. temp -= len;
  736. if (temp == 0)
  737. break;
  738. }
  739. xhci_dbg(xhci, "\n");
  740. if (!in_interrupt())
  741. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  742. urb->ep->desc.bEndpointAddress,
  743. urb->transfer_buffer_length,
  744. num_trbs);
  745. return num_trbs;
  746. }
  747. void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  748. {
  749. if (num_trbs != 0)
  750. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  751. "TRBs, %d left\n", __func__,
  752. urb->ep->desc.bEndpointAddress, num_trbs);
  753. if (running_total != urb->transfer_buffer_length)
  754. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  755. "queued %#x (%d), asked for %#x (%d)\n",
  756. __func__,
  757. urb->ep->desc.bEndpointAddress,
  758. running_total, running_total,
  759. urb->transfer_buffer_length,
  760. urb->transfer_buffer_length);
  761. }
  762. void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  763. unsigned int ep_index, int start_cycle,
  764. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  765. {
  766. u32 field;
  767. /*
  768. * Pass all the TRBs to the hardware at once and make sure this write
  769. * isn't reordered.
  770. */
  771. wmb();
  772. start_trb->field[3] |= start_cycle;
  773. field = xhci_readl(xhci, &xhci->dba->doorbell[slot_id]) & DB_MASK;
  774. xhci_writel(xhci, field | EPI_TO_DB(ep_index),
  775. &xhci->dba->doorbell[slot_id]);
  776. /* Flush PCI posted writes */
  777. xhci_readl(xhci, &xhci->dba->doorbell[slot_id]);
  778. }
  779. int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  780. struct urb *urb, int slot_id, unsigned int ep_index)
  781. {
  782. struct xhci_ring *ep_ring;
  783. unsigned int num_trbs;
  784. struct xhci_td *td;
  785. struct scatterlist *sg;
  786. int num_sgs;
  787. int trb_buff_len, this_sg_len, running_total;
  788. bool first_trb;
  789. u64 addr;
  790. struct xhci_generic_trb *start_trb;
  791. int start_cycle;
  792. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  793. num_trbs = count_sg_trbs_needed(xhci, urb);
  794. num_sgs = urb->num_sgs;
  795. trb_buff_len = xhci_prepare_transfer(xhci, xhci->devs[slot_id],
  796. ep_index, num_trbs, urb, &td, mem_flags);
  797. if (trb_buff_len < 0)
  798. return trb_buff_len;
  799. /*
  800. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  801. * until we've finished creating all the other TRBs. The ring's cycle
  802. * state may change as we enqueue the other TRBs, so save it too.
  803. */
  804. start_trb = &ep_ring->enqueue->generic;
  805. start_cycle = ep_ring->cycle_state;
  806. running_total = 0;
  807. /*
  808. * How much data is in the first TRB?
  809. *
  810. * There are three forces at work for TRB buffer pointers and lengths:
  811. * 1. We don't want to walk off the end of this sg-list entry buffer.
  812. * 2. The transfer length that the driver requested may be smaller than
  813. * the amount of memory allocated for this scatter-gather list.
  814. * 3. TRBs buffers can't cross 64KB boundaries.
  815. */
  816. sg = urb->sg->sg;
  817. addr = (u64) sg_dma_address(sg);
  818. this_sg_len = sg_dma_len(sg);
  819. trb_buff_len = TRB_MAX_BUFF_SIZE -
  820. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  821. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  822. if (trb_buff_len > urb->transfer_buffer_length)
  823. trb_buff_len = urb->transfer_buffer_length;
  824. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  825. trb_buff_len);
  826. first_trb = true;
  827. /* Queue the first TRB, even if it's zero-length */
  828. do {
  829. u32 field = 0;
  830. /* Don't change the cycle bit of the first TRB until later */
  831. if (first_trb)
  832. first_trb = false;
  833. else
  834. field |= ep_ring->cycle_state;
  835. /* Chain all the TRBs together; clear the chain bit in the last
  836. * TRB to indicate it's the last TRB in the chain.
  837. */
  838. if (num_trbs > 1) {
  839. field |= TRB_CHAIN;
  840. } else {
  841. /* FIXME - add check for ZERO_PACKET flag before this */
  842. td->last_trb = ep_ring->enqueue;
  843. field |= TRB_IOC;
  844. }
  845. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  846. "64KB boundary at %#x, end dma = %#x\n",
  847. (unsigned int) addr, trb_buff_len, trb_buff_len,
  848. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  849. (unsigned int) addr + trb_buff_len);
  850. if (TRB_MAX_BUFF_SIZE -
  851. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  852. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  853. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  854. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  855. (unsigned int) addr + trb_buff_len);
  856. }
  857. queue_trb(xhci, ep_ring, false,
  858. (u32) addr,
  859. (u32) ((u64) addr >> 32),
  860. TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
  861. /* We always want to know if the TRB was short,
  862. * or we won't get an event when it completes.
  863. * (Unless we use event data TRBs, which are a
  864. * waste of space and HC resources.)
  865. */
  866. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  867. --num_trbs;
  868. running_total += trb_buff_len;
  869. /* Calculate length for next transfer --
  870. * Are we done queueing all the TRBs for this sg entry?
  871. */
  872. this_sg_len -= trb_buff_len;
  873. if (this_sg_len == 0) {
  874. --num_sgs;
  875. if (num_sgs == 0)
  876. break;
  877. sg = sg_next(sg);
  878. addr = (u64) sg_dma_address(sg);
  879. this_sg_len = sg_dma_len(sg);
  880. } else {
  881. addr += trb_buff_len;
  882. }
  883. trb_buff_len = TRB_MAX_BUFF_SIZE -
  884. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  885. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  886. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  887. trb_buff_len =
  888. urb->transfer_buffer_length - running_total;
  889. } while (running_total < urb->transfer_buffer_length);
  890. check_trb_math(urb, num_trbs, running_total);
  891. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  892. return 0;
  893. }
  894. /* This is very similar to what ehci-q.c qtd_fill() does */
  895. int queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  896. struct urb *urb, int slot_id, unsigned int ep_index)
  897. {
  898. struct xhci_ring *ep_ring;
  899. struct xhci_td *td;
  900. int num_trbs;
  901. struct xhci_generic_trb *start_trb;
  902. bool first_trb;
  903. int start_cycle;
  904. u32 field;
  905. int running_total, trb_buff_len, ret;
  906. u64 addr;
  907. if (urb->sg)
  908. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  909. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  910. num_trbs = 0;
  911. /* How much data is (potentially) left before the 64KB boundary? */
  912. running_total = TRB_MAX_BUFF_SIZE -
  913. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  914. /* If there's some data on this 64KB chunk, or we have to send a
  915. * zero-length transfer, we need at least one TRB
  916. */
  917. if (running_total != 0 || urb->transfer_buffer_length == 0)
  918. num_trbs++;
  919. /* How many more 64KB chunks to transfer, how many more TRBs? */
  920. while (running_total < urb->transfer_buffer_length) {
  921. num_trbs++;
  922. running_total += TRB_MAX_BUFF_SIZE;
  923. }
  924. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  925. if (!in_interrupt())
  926. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#x, num_trbs = %d\n",
  927. urb->ep->desc.bEndpointAddress,
  928. urb->transfer_buffer_length,
  929. urb->transfer_buffer_length,
  930. urb->transfer_dma,
  931. num_trbs);
  932. ret = xhci_prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  933. num_trbs, urb, &td, mem_flags);
  934. if (ret < 0)
  935. return ret;
  936. /*
  937. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  938. * until we've finished creating all the other TRBs. The ring's cycle
  939. * state may change as we enqueue the other TRBs, so save it too.
  940. */
  941. start_trb = &ep_ring->enqueue->generic;
  942. start_cycle = ep_ring->cycle_state;
  943. running_total = 0;
  944. /* How much data is in the first TRB? */
  945. addr = (u64) urb->transfer_dma;
  946. trb_buff_len = TRB_MAX_BUFF_SIZE -
  947. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  948. if (urb->transfer_buffer_length < trb_buff_len)
  949. trb_buff_len = urb->transfer_buffer_length;
  950. first_trb = true;
  951. /* Queue the first TRB, even if it's zero-length */
  952. do {
  953. field = 0;
  954. /* Don't change the cycle bit of the first TRB until later */
  955. if (first_trb)
  956. first_trb = false;
  957. else
  958. field |= ep_ring->cycle_state;
  959. /* Chain all the TRBs together; clear the chain bit in the last
  960. * TRB to indicate it's the last TRB in the chain.
  961. */
  962. if (num_trbs > 1) {
  963. field |= TRB_CHAIN;
  964. } else {
  965. /* FIXME - add check for ZERO_PACKET flag before this */
  966. td->last_trb = ep_ring->enqueue;
  967. field |= TRB_IOC;
  968. }
  969. queue_trb(xhci, ep_ring, false,
  970. (u32) addr,
  971. (u32) ((u64) addr >> 32),
  972. TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
  973. /* We always want to know if the TRB was short,
  974. * or we won't get an event when it completes.
  975. * (Unless we use event data TRBs, which are a
  976. * waste of space and HC resources.)
  977. */
  978. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  979. --num_trbs;
  980. running_total += trb_buff_len;
  981. /* Calculate length for next transfer */
  982. addr += trb_buff_len;
  983. trb_buff_len = urb->transfer_buffer_length - running_total;
  984. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  985. trb_buff_len = TRB_MAX_BUFF_SIZE;
  986. } while (running_total < urb->transfer_buffer_length);
  987. check_trb_math(urb, num_trbs, running_total);
  988. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  989. return 0;
  990. }
  991. /* Caller must have locked xhci->lock */
  992. int queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  993. struct urb *urb, int slot_id, unsigned int ep_index)
  994. {
  995. struct xhci_ring *ep_ring;
  996. int num_trbs;
  997. int ret;
  998. struct usb_ctrlrequest *setup;
  999. struct xhci_generic_trb *start_trb;
  1000. int start_cycle;
  1001. u32 field;
  1002. struct xhci_td *td;
  1003. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1004. /*
  1005. * Need to copy setup packet into setup TRB, so we can't use the setup
  1006. * DMA address.
  1007. */
  1008. if (!urb->setup_packet)
  1009. return -EINVAL;
  1010. if (!in_interrupt())
  1011. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1012. slot_id, ep_index);
  1013. /* 1 TRB for setup, 1 for status */
  1014. num_trbs = 2;
  1015. /*
  1016. * Don't need to check if we need additional event data and normal TRBs,
  1017. * since data in control transfers will never get bigger than 16MB
  1018. * XXX: can we get a buffer that crosses 64KB boundaries?
  1019. */
  1020. if (urb->transfer_buffer_length > 0)
  1021. num_trbs++;
  1022. ret = xhci_prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1023. urb, &td, mem_flags);
  1024. if (ret < 0)
  1025. return ret;
  1026. /*
  1027. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1028. * until we've finished creating all the other TRBs. The ring's cycle
  1029. * state may change as we enqueue the other TRBs, so save it too.
  1030. */
  1031. start_trb = &ep_ring->enqueue->generic;
  1032. start_cycle = ep_ring->cycle_state;
  1033. /* Queue setup TRB - see section 6.4.1.2.1 */
  1034. /* FIXME better way to translate setup_packet into two u32 fields? */
  1035. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1036. queue_trb(xhci, ep_ring, false,
  1037. /* FIXME endianness is probably going to bite my ass here. */
  1038. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1039. setup->wIndex | setup->wLength << 16,
  1040. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1041. /* Immediate data in pointer */
  1042. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1043. /* If there's data, queue data TRBs */
  1044. field = 0;
  1045. if (urb->transfer_buffer_length > 0) {
  1046. if (setup->bRequestType & USB_DIR_IN)
  1047. field |= TRB_DIR_IN;
  1048. queue_trb(xhci, ep_ring, false,
  1049. lower_32_bits(urb->transfer_dma),
  1050. upper_32_bits(urb->transfer_dma),
  1051. TRB_LEN(urb->transfer_buffer_length) | TRB_INTR_TARGET(0),
  1052. /* Event on short tx */
  1053. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1054. }
  1055. /* Save the DMA address of the last TRB in the TD */
  1056. td->last_trb = ep_ring->enqueue;
  1057. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1058. /* If the device sent data, the status stage is an OUT transfer */
  1059. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1060. field = 0;
  1061. else
  1062. field = TRB_DIR_IN;
  1063. queue_trb(xhci, ep_ring, false,
  1064. 0,
  1065. 0,
  1066. TRB_INTR_TARGET(0),
  1067. /* Event on completion */
  1068. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1069. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1070. return 0;
  1071. }
  1072. /**** Command Ring Operations ****/
  1073. /* Generic function for queueing a command TRB on the command ring */
  1074. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  1075. {
  1076. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  1077. if (!in_interrupt())
  1078. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1079. return -ENOMEM;
  1080. }
  1081. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1082. field4 | xhci->cmd_ring->cycle_state);
  1083. return 0;
  1084. }
  1085. /* Queue a no-op command on the command ring */
  1086. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1087. {
  1088. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  1089. }
  1090. /*
  1091. * Place a no-op command on the command ring to test the command and
  1092. * event ring.
  1093. */
  1094. void *setup_one_noop(struct xhci_hcd *xhci)
  1095. {
  1096. if (queue_cmd_noop(xhci) < 0)
  1097. return NULL;
  1098. xhci->noops_submitted++;
  1099. return ring_cmd_db;
  1100. }
  1101. /* Queue a slot enable or disable request on the command ring */
  1102. int queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  1103. {
  1104. return queue_command(xhci, 0, 0, 0,
  1105. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  1106. }
  1107. /* Queue an address device command TRB */
  1108. int queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  1109. {
  1110. return queue_command(xhci, in_ctx_ptr, 0, 0,
  1111. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  1112. }
  1113. /* Queue a configure endpoint command TRB */
  1114. int queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  1115. {
  1116. return queue_command(xhci, in_ctx_ptr, 0, 0,
  1117. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  1118. }