xhci-ring.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * USB HOST XHCI Controller stack
  3. *
  4. * Based on xHCI host controller driver in linux-kernel
  5. * by Sarah Sharp.
  6. *
  7. * Copyright (C) 2008 Intel Corp.
  8. * Author: Sarah Sharp
  9. *
  10. * Copyright (C) 2013 Samsung Electronics Co.Ltd
  11. * Authors: Vivek Gautam <gautam.vivek@samsung.com>
  12. * Vikas Sajjan <vikas.sajjan@samsung.com>
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include <common.h>
  17. #include <asm/byteorder.h>
  18. #include <usb.h>
  19. #include <asm/unaligned.h>
  20. #include <linux/errno.h>
  21. #include "xhci.h"
  22. /**
  23. * Is this TRB a link TRB or was the last TRB the last TRB in this event ring
  24. * segment? I.e. would the updated event TRB pointer step off the end of the
  25. * event seg ?
  26. *
  27. * @param ctrl Host controller data structure
  28. * @param ring pointer to the ring
  29. * @param seg poniter to the segment to which TRB belongs
  30. * @param trb poniter to the ring trb
  31. * @return 1 if this TRB a link TRB else 0
  32. */
  33. static int last_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
  34. struct xhci_segment *seg, union xhci_trb *trb)
  35. {
  36. if (ring == ctrl->event_ring)
  37. return trb == &seg->trbs[TRBS_PER_SEGMENT];
  38. else
  39. return TRB_TYPE_LINK_LE32(trb->link.control);
  40. }
  41. /**
  42. * Does this link TRB point to the first segment in a ring,
  43. * or was the previous TRB the last TRB on the last segment in the ERST?
  44. *
  45. * @param ctrl Host controller data structure
  46. * @param ring pointer to the ring
  47. * @param seg poniter to the segment to which TRB belongs
  48. * @param trb poniter to the ring trb
  49. * @return 1 if this TRB is the last TRB on the last segment else 0
  50. */
  51. static bool last_trb_on_last_seg(struct xhci_ctrl *ctrl,
  52. struct xhci_ring *ring,
  53. struct xhci_segment *seg,
  54. union xhci_trb *trb)
  55. {
  56. if (ring == ctrl->event_ring)
  57. return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
  58. (seg->next == ring->first_seg));
  59. else
  60. return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
  61. }
  62. /**
  63. * See Cycle bit rules. SW is the consumer for the event ring only.
  64. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  65. *
  66. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  67. * chain bit is set), then set the chain bit in all the following link TRBs.
  68. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  69. * have their chain bit cleared (so that each Link TRB is a separate TD).
  70. *
  71. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  72. * set, but other sections talk about dealing with the chain bit set. This was
  73. * fixed in the 0.96 specification errata, but we have to assume that all 0.95
  74. * xHCI hardware can't handle the chain bit being cleared on a link TRB.
  75. *
  76. * @param ctrl Host controller data structure
  77. * @param ring pointer to the ring
  78. * @param more_trbs_coming flag to indicate whether more trbs
  79. * are expected or NOT.
  80. * Will you enqueue more TRBs before calling
  81. * prepare_ring()?
  82. * @return none
  83. */
  84. static void inc_enq(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
  85. bool more_trbs_coming)
  86. {
  87. u32 chain;
  88. union xhci_trb *next;
  89. chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
  90. next = ++(ring->enqueue);
  91. /*
  92. * Update the dequeue pointer further if that was a link TRB or we're at
  93. * the end of an event ring segment (which doesn't have link TRBS)
  94. */
  95. while (last_trb(ctrl, ring, ring->enq_seg, next)) {
  96. if (ring != ctrl->event_ring) {
  97. /*
  98. * If the caller doesn't plan on enqueueing more
  99. * TDs before ringing the doorbell, then we
  100. * don't want to give the link TRB to the
  101. * hardware just yet. We'll give the link TRB
  102. * back in prepare_ring() just before we enqueue
  103. * the TD at the top of the ring.
  104. */
  105. if (!chain && !more_trbs_coming)
  106. break;
  107. /*
  108. * If we're not dealing with 0.95 hardware or
  109. * isoc rings on AMD 0.96 host,
  110. * carry over the chain bit of the previous TRB
  111. * (which may mean the chain bit is cleared).
  112. */
  113. next->link.control &= cpu_to_le32(~TRB_CHAIN);
  114. next->link.control |= cpu_to_le32(chain);
  115. next->link.control ^= cpu_to_le32(TRB_CYCLE);
  116. xhci_flush_cache((uintptr_t)next,
  117. sizeof(union xhci_trb));
  118. }
  119. /* Toggle the cycle bit after the last ring segment. */
  120. if (last_trb_on_last_seg(ctrl, ring,
  121. ring->enq_seg, next))
  122. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  123. ring->enq_seg = ring->enq_seg->next;
  124. ring->enqueue = ring->enq_seg->trbs;
  125. next = ring->enqueue;
  126. }
  127. }
  128. /**
  129. * See Cycle bit rules. SW is the consumer for the event ring only.
  130. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  131. *
  132. * @param ctrl Host controller data structure
  133. * @param ring Ring whose Dequeue TRB pointer needs to be incremented.
  134. * return none
  135. */
  136. static void inc_deq(struct xhci_ctrl *ctrl, struct xhci_ring *ring)
  137. {
  138. do {
  139. /*
  140. * Update the dequeue pointer further if that was a link TRB or
  141. * we're at the end of an event ring segment (which doesn't have
  142. * link TRBS)
  143. */
  144. if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) {
  145. if (ring == ctrl->event_ring &&
  146. last_trb_on_last_seg(ctrl, ring,
  147. ring->deq_seg, ring->dequeue)) {
  148. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  149. }
  150. ring->deq_seg = ring->deq_seg->next;
  151. ring->dequeue = ring->deq_seg->trbs;
  152. } else {
  153. ring->dequeue++;
  154. }
  155. } while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue));
  156. }
  157. /**
  158. * Generic function for queueing a TRB on a ring.
  159. * The caller must have checked to make sure there's room on the ring.
  160. *
  161. * @param more_trbs_coming: Will you enqueue more TRBs before calling
  162. * prepare_ring()?
  163. * @param ctrl Host controller data structure
  164. * @param ring pointer to the ring
  165. * @param more_trbs_coming flag to indicate whether more trbs
  166. * @param trb_fields pointer to trb field array containing TRB contents
  167. * @return pointer to the enqueued trb
  168. */
  169. static struct xhci_generic_trb *queue_trb(struct xhci_ctrl *ctrl,
  170. struct xhci_ring *ring,
  171. bool more_trbs_coming,
  172. unsigned int *trb_fields)
  173. {
  174. struct xhci_generic_trb *trb;
  175. int i;
  176. trb = &ring->enqueue->generic;
  177. for (i = 0; i < 4; i++)
  178. trb->field[i] = cpu_to_le32(trb_fields[i]);
  179. xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb));
  180. inc_enq(ctrl, ring, more_trbs_coming);
  181. return trb;
  182. }
  183. /**
  184. * Does various checks on the endpoint ring, and makes it ready
  185. * to queue num_trbs.
  186. *
  187. * @param ctrl Host controller data structure
  188. * @param ep_ring pointer to the EP Transfer Ring
  189. * @param ep_state State of the End Point
  190. * @return error code in case of invalid ep_state, 0 on success
  191. */
  192. static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring,
  193. u32 ep_state)
  194. {
  195. union xhci_trb *next = ep_ring->enqueue;
  196. /* Make sure the endpoint has been added to xHC schedule */
  197. switch (ep_state) {
  198. case EP_STATE_DISABLED:
  199. /*
  200. * USB core changed config/interfaces without notifying us,
  201. * or hardware is reporting the wrong state.
  202. */
  203. puts("WARN urb submitted to disabled ep\n");
  204. return -ENOENT;
  205. case EP_STATE_ERROR:
  206. puts("WARN waiting for error on ep to be cleared\n");
  207. return -EINVAL;
  208. case EP_STATE_HALTED:
  209. puts("WARN halted endpoint, queueing URB anyway.\n");
  210. case EP_STATE_STOPPED:
  211. case EP_STATE_RUNNING:
  212. debug("EP STATE RUNNING.\n");
  213. break;
  214. default:
  215. puts("ERROR unknown endpoint state for ep\n");
  216. return -EINVAL;
  217. }
  218. while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) {
  219. /*
  220. * If we're not dealing with 0.95 hardware or isoc rings
  221. * on AMD 0.96 host, clear the chain bit.
  222. */
  223. next->link.control &= cpu_to_le32(~TRB_CHAIN);
  224. next->link.control ^= cpu_to_le32(TRB_CYCLE);
  225. xhci_flush_cache((uintptr_t)next, sizeof(union xhci_trb));
  226. /* Toggle the cycle bit after the last ring segment. */
  227. if (last_trb_on_last_seg(ctrl, ep_ring,
  228. ep_ring->enq_seg, next))
  229. ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1);
  230. ep_ring->enq_seg = ep_ring->enq_seg->next;
  231. ep_ring->enqueue = ep_ring->enq_seg->trbs;
  232. next = ep_ring->enqueue;
  233. }
  234. return 0;
  235. }
  236. /**
  237. * Generic function for queueing a command TRB on the command ring.
  238. * Check to make sure there's room on the command ring for one command TRB.
  239. *
  240. * @param ctrl Host controller data structure
  241. * @param ptr Pointer address to write in the first two fields (opt.)
  242. * @param slot_id Slot ID to encode in the flags field (opt.)
  243. * @param ep_index Endpoint index to encode in the flags field (opt.)
  244. * @param cmd Command type to enqueue
  245. * @return none
  246. */
  247. void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id,
  248. u32 ep_index, trb_type cmd)
  249. {
  250. u32 fields[4];
  251. u64 val_64 = (uintptr_t)ptr;
  252. BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING));
  253. fields[0] = lower_32_bits(val_64);
  254. fields[1] = upper_32_bits(val_64);
  255. fields[2] = 0;
  256. fields[3] = TRB_TYPE(cmd) | EP_ID_FOR_TRB(ep_index) |
  257. SLOT_ID_FOR_TRB(slot_id) | ctrl->cmd_ring->cycle_state;
  258. queue_trb(ctrl, ctrl->cmd_ring, false, fields);
  259. /* Ring the command ring doorbell */
  260. xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST);
  261. }
  262. /**
  263. * The TD size is the number of bytes remaining in the TD (including this TRB),
  264. * right shifted by 10.
  265. * It must fit in bits 21:17, so it can't be bigger than 31.
  266. *
  267. * @param remainder remaining packets to be sent
  268. * @return remainder if remainder is less than max else max
  269. */
  270. static u32 xhci_td_remainder(unsigned int remainder)
  271. {
  272. u32 max = (1 << (21 - 17 + 1)) - 1;
  273. if ((remainder >> 10) >= max)
  274. return max << 17;
  275. else
  276. return (remainder >> 10) << 17;
  277. }
  278. /**
  279. * Finds out the remanining packets to be sent
  280. *
  281. * @param running_total total size sent so far
  282. * @param trb_buff_len length of the TRB Buffer
  283. * @param total_packet_count total packet count
  284. * @param maxpacketsize max packet size of current pipe
  285. * @param num_trbs_left number of TRBs left to be processed
  286. * @return 0 if running_total or trb_buff_len is 0, else remainder
  287. */
  288. static u32 xhci_v1_0_td_remainder(int running_total,
  289. int trb_buff_len,
  290. unsigned int total_packet_count,
  291. int maxpacketsize,
  292. unsigned int num_trbs_left)
  293. {
  294. int packets_transferred;
  295. /* One TRB with a zero-length data packet. */
  296. if (num_trbs_left == 0 || (running_total == 0 && trb_buff_len == 0))
  297. return 0;
  298. /*
  299. * All the TRB queueing functions don't count the current TRB in
  300. * running_total.
  301. */
  302. packets_transferred = (running_total + trb_buff_len) / maxpacketsize;
  303. if ((total_packet_count - packets_transferred) > 31)
  304. return 31 << 17;
  305. return (total_packet_count - packets_transferred) << 17;
  306. }
  307. /**
  308. * Ring the doorbell of the End Point
  309. *
  310. * @param udev pointer to the USB device structure
  311. * @param ep_index index of the endpoint
  312. * @param start_cycle cycle flag of the first TRB
  313. * @param start_trb pionter to the first TRB
  314. * @return none
  315. */
  316. static void giveback_first_trb(struct usb_device *udev, int ep_index,
  317. int start_cycle,
  318. struct xhci_generic_trb *start_trb)
  319. {
  320. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  321. /*
  322. * Pass all the TRBs to the hardware at once and make sure this write
  323. * isn't reordered.
  324. */
  325. if (start_cycle)
  326. start_trb->field[3] |= cpu_to_le32(start_cycle);
  327. else
  328. start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  329. xhci_flush_cache((uintptr_t)start_trb, sizeof(struct xhci_generic_trb));
  330. /* Ringing EP doorbell here */
  331. xhci_writel(&ctrl->dba->doorbell[udev->slot_id],
  332. DB_VALUE(ep_index, 0));
  333. return;
  334. }
  335. /**** POLLING mechanism for XHCI ****/
  336. /**
  337. * Finalizes a handled event TRB by advancing our dequeue pointer and giving
  338. * the TRB back to the hardware for recycling. Must call this exactly once at
  339. * the end of each event handler, and not touch the TRB again afterwards.
  340. *
  341. * @param ctrl Host controller data structure
  342. * @return none
  343. */
  344. void xhci_acknowledge_event(struct xhci_ctrl *ctrl)
  345. {
  346. /* Advance our dequeue pointer to the next event */
  347. inc_deq(ctrl, ctrl->event_ring);
  348. /* Inform the hardware */
  349. xhci_writeq(&ctrl->ir_set->erst_dequeue,
  350. (uintptr_t)ctrl->event_ring->dequeue | ERST_EHB);
  351. }
  352. /**
  353. * Checks if there is a new event to handle on the event ring.
  354. *
  355. * @param ctrl Host controller data structure
  356. * @return 0 if failure else 1 on success
  357. */
  358. static int event_ready(struct xhci_ctrl *ctrl)
  359. {
  360. union xhci_trb *event;
  361. xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue,
  362. sizeof(union xhci_trb));
  363. event = ctrl->event_ring->dequeue;
  364. /* Does the HC or OS own the TRB? */
  365. if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
  366. ctrl->event_ring->cycle_state)
  367. return 0;
  368. return 1;
  369. }
  370. /**
  371. * Waits for a specific type of event and returns it. Discards unexpected
  372. * events. Caller *must* call xhci_acknowledge_event() after it is finished
  373. * processing the event, and must not access the returned pointer afterwards.
  374. *
  375. * @param ctrl Host controller data structure
  376. * @param expected TRB type expected from Event TRB
  377. * @return pointer to event trb
  378. */
  379. union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
  380. {
  381. trb_type type;
  382. unsigned long ts = get_timer(0);
  383. do {
  384. union xhci_trb *event = ctrl->event_ring->dequeue;
  385. if (!event_ready(ctrl))
  386. continue;
  387. type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
  388. if (type == expected)
  389. return event;
  390. if (type == TRB_PORT_STATUS)
  391. /* TODO: remove this once enumeration has been reworked */
  392. /*
  393. * Port status change events always have a
  394. * successful completion code
  395. */
  396. BUG_ON(GET_COMP_CODE(
  397. le32_to_cpu(event->generic.field[2])) !=
  398. COMP_SUCCESS);
  399. else
  400. printf("Unexpected XHCI event TRB, skipping... "
  401. "(%08x %08x %08x %08x)\n",
  402. le32_to_cpu(event->generic.field[0]),
  403. le32_to_cpu(event->generic.field[1]),
  404. le32_to_cpu(event->generic.field[2]),
  405. le32_to_cpu(event->generic.field[3]));
  406. xhci_acknowledge_event(ctrl);
  407. } while (get_timer(ts) < XHCI_TIMEOUT);
  408. if (expected == TRB_TRANSFER)
  409. return NULL;
  410. printf("XHCI timeout on event type %d... cannot recover.\n", expected);
  411. BUG();
  412. }
  413. /*
  414. * Stops transfer processing for an endpoint and throws away all unprocessed
  415. * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next
  416. * xhci_bulk_tx/xhci_ctrl_tx on this enpoint will add new transfers there and
  417. * ring the doorbell, causing this endpoint to start working again.
  418. * (Careful: This will BUG() when there was no transfer in progress. Shouldn't
  419. * happen in practice for current uses and is too complicated to fix right now.)
  420. */
  421. static void abort_td(struct usb_device *udev, int ep_index)
  422. {
  423. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  424. struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring;
  425. union xhci_trb *event;
  426. u32 field;
  427. xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_STOP_RING);
  428. event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
  429. field = le32_to_cpu(event->trans_event.flags);
  430. BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
  431. BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
  432. BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len
  433. != COMP_STOP)));
  434. xhci_acknowledge_event(ctrl);
  435. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  436. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
  437. != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
  438. event->event_cmd.status)) != COMP_SUCCESS);
  439. xhci_acknowledge_event(ctrl);
  440. xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue |
  441. ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ);
  442. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  443. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
  444. != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
  445. event->event_cmd.status)) != COMP_SUCCESS);
  446. xhci_acknowledge_event(ctrl);
  447. }
  448. static void record_transfer_result(struct usb_device *udev,
  449. union xhci_trb *event, int length)
  450. {
  451. udev->act_len = min(length, length -
  452. (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len)));
  453. switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) {
  454. case COMP_SUCCESS:
  455. BUG_ON(udev->act_len != length);
  456. /* fallthrough */
  457. case COMP_SHORT_TX:
  458. udev->status = 0;
  459. break;
  460. case COMP_STALL:
  461. udev->status = USB_ST_STALLED;
  462. break;
  463. case COMP_DB_ERR:
  464. case COMP_TRB_ERR:
  465. udev->status = USB_ST_BUF_ERR;
  466. break;
  467. case COMP_BABBLE:
  468. udev->status = USB_ST_BABBLE_DET;
  469. break;
  470. default:
  471. udev->status = 0x80; /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */
  472. }
  473. }
  474. /**** Bulk and Control transfer methods ****/
  475. /**
  476. * Queues up the BULK Request
  477. *
  478. * @param udev pointer to the USB device structure
  479. * @param pipe contains the DIR_IN or OUT , devnum
  480. * @param length length of the buffer
  481. * @param buffer buffer to be read/written based on the request
  482. * @return returns 0 if successful else -1 on failure
  483. */
  484. int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,
  485. int length, void *buffer)
  486. {
  487. int num_trbs = 0;
  488. struct xhci_generic_trb *start_trb;
  489. bool first_trb = 0;
  490. int start_cycle;
  491. u32 field = 0;
  492. u32 length_field = 0;
  493. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  494. int slot_id = udev->slot_id;
  495. int ep_index;
  496. struct xhci_virt_device *virt_dev;
  497. struct xhci_ep_ctx *ep_ctx;
  498. struct xhci_ring *ring; /* EP transfer ring */
  499. union xhci_trb *event;
  500. int running_total, trb_buff_len;
  501. unsigned int total_packet_count;
  502. int maxpacketsize;
  503. u64 addr;
  504. int ret;
  505. u32 trb_fields[4];
  506. u64 val_64 = (uintptr_t)buffer;
  507. debug("dev=%p, pipe=%lx, buffer=%p, length=%d\n",
  508. udev, pipe, buffer, length);
  509. ep_index = usb_pipe_ep_index(pipe);
  510. virt_dev = ctrl->devs[slot_id];
  511. xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
  512. virt_dev->out_ctx->size);
  513. ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
  514. ring = virt_dev->eps[ep_index].ring;
  515. /*
  516. * How much data is (potentially) left before the 64KB boundary?
  517. * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
  518. * that the buffer should not span 64KB boundary. if so
  519. * we send request in more than 1 TRB by chaining them.
  520. */
  521. running_total = TRB_MAX_BUFF_SIZE -
  522. (lower_32_bits(val_64) & (TRB_MAX_BUFF_SIZE - 1));
  523. trb_buff_len = running_total;
  524. running_total &= TRB_MAX_BUFF_SIZE - 1;
  525. /*
  526. * If there's some data on this 64KB chunk, or we have to send a
  527. * zero-length transfer, we need at least one TRB
  528. */
  529. if (running_total != 0 || length == 0)
  530. num_trbs++;
  531. /* How many more 64KB chunks to transfer, how many more TRBs? */
  532. while (running_total < length) {
  533. num_trbs++;
  534. running_total += TRB_MAX_BUFF_SIZE;
  535. }
  536. /*
  537. * XXX: Calling routine prepare_ring() called in place of
  538. * prepare_trasfer() as there in 'Linux' since we are not
  539. * maintaining multiple TDs/transfer at the same time.
  540. */
  541. ret = prepare_ring(ctrl, ring,
  542. le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
  543. if (ret < 0)
  544. return ret;
  545. /*
  546. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  547. * until we've finished creating all the other TRBs. The ring's cycle
  548. * state may change as we enqueue the other TRBs, so save it too.
  549. */
  550. start_trb = &ring->enqueue->generic;
  551. start_cycle = ring->cycle_state;
  552. running_total = 0;
  553. maxpacketsize = usb_maxpacket(udev, pipe);
  554. total_packet_count = DIV_ROUND_UP(length, maxpacketsize);
  555. /* How much data is in the first TRB? */
  556. /*
  557. * How much data is (potentially) left before the 64KB boundary?
  558. * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
  559. * that the buffer should not span 64KB boundary. if so
  560. * we send request in more than 1 TRB by chaining them.
  561. */
  562. addr = val_64;
  563. if (trb_buff_len > length)
  564. trb_buff_len = length;
  565. first_trb = true;
  566. /* flush the buffer before use */
  567. xhci_flush_cache((uintptr_t)buffer, length);
  568. /* Queue the first TRB, even if it's zero-length */
  569. do {
  570. u32 remainder = 0;
  571. field = 0;
  572. /* Don't change the cycle bit of the first TRB until later */
  573. if (first_trb) {
  574. first_trb = false;
  575. if (start_cycle == 0)
  576. field |= TRB_CYCLE;
  577. } else {
  578. field |= ring->cycle_state;
  579. }
  580. /*
  581. * Chain all the TRBs together; clear the chain bit in the last
  582. * TRB to indicate it's the last TRB in the chain.
  583. */
  584. if (num_trbs > 1)
  585. field |= TRB_CHAIN;
  586. else
  587. field |= TRB_IOC;
  588. /* Only set interrupt on short packet for IN endpoints */
  589. if (usb_pipein(pipe))
  590. field |= TRB_ISP;
  591. /* Set the TRB length, TD size, and interrupter fields. */
  592. if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) < 0x100)
  593. remainder = xhci_td_remainder(length - running_total);
  594. else
  595. remainder = xhci_v1_0_td_remainder(running_total,
  596. trb_buff_len,
  597. total_packet_count,
  598. maxpacketsize,
  599. num_trbs - 1);
  600. length_field = ((trb_buff_len & TRB_LEN_MASK) |
  601. remainder |
  602. ((0 & TRB_INTR_TARGET_MASK) <<
  603. TRB_INTR_TARGET_SHIFT));
  604. trb_fields[0] = lower_32_bits(addr);
  605. trb_fields[1] = upper_32_bits(addr);
  606. trb_fields[2] = length_field;
  607. trb_fields[3] = field | (TRB_NORMAL << TRB_TYPE_SHIFT);
  608. queue_trb(ctrl, ring, (num_trbs > 1), trb_fields);
  609. --num_trbs;
  610. running_total += trb_buff_len;
  611. /* Calculate length for next transfer */
  612. addr += trb_buff_len;
  613. trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE);
  614. } while (running_total < length);
  615. giveback_first_trb(udev, ep_index, start_cycle, start_trb);
  616. event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
  617. if (!event) {
  618. debug("XHCI bulk transfer timed out, aborting...\n");
  619. abort_td(udev, ep_index);
  620. udev->status = USB_ST_NAK_REC; /* closest thing to a timeout */
  621. udev->act_len = 0;
  622. return -ETIMEDOUT;
  623. }
  624. field = le32_to_cpu(event->trans_event.flags);
  625. BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
  626. BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
  627. BUG_ON(*(void **)(uintptr_t)le64_to_cpu(event->trans_event.buffer) -
  628. buffer > (size_t)length);
  629. record_transfer_result(udev, event, length);
  630. xhci_acknowledge_event(ctrl);
  631. xhci_inval_cache((uintptr_t)buffer, length);
  632. return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
  633. }
  634. /**
  635. * Queues up the Control Transfer Request
  636. *
  637. * @param udev pointer to the USB device structure
  638. * @param pipe contains the DIR_IN or OUT , devnum
  639. * @param req request type
  640. * @param length length of the buffer
  641. * @param buffer buffer to be read/written based on the request
  642. * @return returns 0 if successful else error code on failure
  643. */
  644. int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,
  645. struct devrequest *req, int length,
  646. void *buffer)
  647. {
  648. int ret;
  649. int start_cycle;
  650. int num_trbs;
  651. u32 field;
  652. u32 length_field;
  653. u64 buf_64 = 0;
  654. struct xhci_generic_trb *start_trb;
  655. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  656. int slot_id = udev->slot_id;
  657. int ep_index;
  658. u32 trb_fields[4];
  659. struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
  660. struct xhci_ring *ep_ring;
  661. union xhci_trb *event;
  662. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  663. req->request, req->request,
  664. req->requesttype, req->requesttype,
  665. le16_to_cpu(req->value), le16_to_cpu(req->value),
  666. le16_to_cpu(req->index));
  667. ep_index = usb_pipe_ep_index(pipe);
  668. ep_ring = virt_dev->eps[ep_index].ring;
  669. /*
  670. * Check to see if the max packet size for the default control
  671. * endpoint changed during FS device enumeration
  672. */
  673. if (udev->speed == USB_SPEED_FULL) {
  674. ret = xhci_check_maxpacket(udev);
  675. if (ret < 0)
  676. return ret;
  677. }
  678. xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
  679. virt_dev->out_ctx->size);
  680. struct xhci_ep_ctx *ep_ctx = NULL;
  681. ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
  682. /* 1 TRB for setup, 1 for status */
  683. num_trbs = 2;
  684. /*
  685. * Don't need to check if we need additional event data and normal TRBs,
  686. * since data in control transfers will never get bigger than 16MB
  687. * XXX: can we get a buffer that crosses 64KB boundaries?
  688. */
  689. if (length > 0)
  690. num_trbs++;
  691. /*
  692. * XXX: Calling routine prepare_ring() called in place of
  693. * prepare_trasfer() as there in 'Linux' since we are not
  694. * maintaining multiple TDs/transfer at the same time.
  695. */
  696. ret = prepare_ring(ctrl, ep_ring,
  697. le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
  698. if (ret < 0)
  699. return ret;
  700. /*
  701. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  702. * until we've finished creating all the other TRBs. The ring's cycle
  703. * state may change as we enqueue the other TRBs, so save it too.
  704. */
  705. start_trb = &ep_ring->enqueue->generic;
  706. start_cycle = ep_ring->cycle_state;
  707. debug("start_trb %p, start_cycle %d\n", start_trb, start_cycle);
  708. /* Queue setup TRB - see section 6.4.1.2.1 */
  709. /* FIXME better way to translate setup_packet into two u32 fields? */
  710. field = 0;
  711. field |= TRB_IDT | (TRB_SETUP << TRB_TYPE_SHIFT);
  712. if (start_cycle == 0)
  713. field |= 0x1;
  714. /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
  715. if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) == 0x100) {
  716. if (length > 0) {
  717. if (req->requesttype & USB_DIR_IN)
  718. field |= (TRB_DATA_IN << TRB_TX_TYPE_SHIFT);
  719. else
  720. field |= (TRB_DATA_OUT << TRB_TX_TYPE_SHIFT);
  721. }
  722. }
  723. debug("req->requesttype = %d, req->request = %d,"
  724. "le16_to_cpu(req->value) = %d,"
  725. "le16_to_cpu(req->index) = %d,"
  726. "le16_to_cpu(req->length) = %d\n",
  727. req->requesttype, req->request, le16_to_cpu(req->value),
  728. le16_to_cpu(req->index), le16_to_cpu(req->length));
  729. trb_fields[0] = req->requesttype | req->request << 8 |
  730. le16_to_cpu(req->value) << 16;
  731. trb_fields[1] = le16_to_cpu(req->index) |
  732. le16_to_cpu(req->length) << 16;
  733. /* TRB_LEN | (TRB_INTR_TARGET) */
  734. trb_fields[2] = (8 | ((0 & TRB_INTR_TARGET_MASK) <<
  735. TRB_INTR_TARGET_SHIFT));
  736. /* Immediate data in pointer */
  737. trb_fields[3] = field;
  738. queue_trb(ctrl, ep_ring, true, trb_fields);
  739. /* Re-initializing field to zero */
  740. field = 0;
  741. /* If there's data, queue data TRBs */
  742. /* Only set interrupt on short packet for IN endpoints */
  743. if (usb_pipein(pipe))
  744. field = TRB_ISP | (TRB_DATA << TRB_TYPE_SHIFT);
  745. else
  746. field = (TRB_DATA << TRB_TYPE_SHIFT);
  747. length_field = (length & TRB_LEN_MASK) | xhci_td_remainder(length) |
  748. ((0 & TRB_INTR_TARGET_MASK) << TRB_INTR_TARGET_SHIFT);
  749. debug("length_field = %d, length = %d,"
  750. "xhci_td_remainder(length) = %d , TRB_INTR_TARGET(0) = %d\n",
  751. length_field, (length & TRB_LEN_MASK),
  752. xhci_td_remainder(length), 0);
  753. if (length > 0) {
  754. if (req->requesttype & USB_DIR_IN)
  755. field |= TRB_DIR_IN;
  756. buf_64 = (uintptr_t)buffer;
  757. trb_fields[0] = lower_32_bits(buf_64);
  758. trb_fields[1] = upper_32_bits(buf_64);
  759. trb_fields[2] = length_field;
  760. trb_fields[3] = field | ep_ring->cycle_state;
  761. xhci_flush_cache((uintptr_t)buffer, length);
  762. queue_trb(ctrl, ep_ring, true, trb_fields);
  763. }
  764. /*
  765. * Queue status TRB -
  766. * see Table 7 and sections 4.11.2.2 and 6.4.1.2.3
  767. */
  768. /* If the device sent data, the status stage is an OUT transfer */
  769. field = 0;
  770. if (length > 0 && req->requesttype & USB_DIR_IN)
  771. field = 0;
  772. else
  773. field = TRB_DIR_IN;
  774. trb_fields[0] = 0;
  775. trb_fields[1] = 0;
  776. trb_fields[2] = ((0 & TRB_INTR_TARGET_MASK) << TRB_INTR_TARGET_SHIFT);
  777. /* Event on completion */
  778. trb_fields[3] = field | TRB_IOC |
  779. (TRB_STATUS << TRB_TYPE_SHIFT) |
  780. ep_ring->cycle_state;
  781. queue_trb(ctrl, ep_ring, false, trb_fields);
  782. giveback_first_trb(udev, ep_index, start_cycle, start_trb);
  783. event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
  784. if (!event)
  785. goto abort;
  786. field = le32_to_cpu(event->trans_event.flags);
  787. BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
  788. BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
  789. record_transfer_result(udev, event, length);
  790. xhci_acknowledge_event(ctrl);
  791. /* Invalidate buffer to make it available to usb-core */
  792. if (length > 0)
  793. xhci_inval_cache((uintptr_t)buffer, length);
  794. if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))
  795. == COMP_SHORT_TX) {
  796. /* Short data stage, clear up additional status stage event */
  797. event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
  798. if (!event)
  799. goto abort;
  800. BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
  801. BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
  802. xhci_acknowledge_event(ctrl);
  803. }
  804. return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
  805. abort:
  806. debug("XHCI control transfer timed out, aborting...\n");
  807. abort_td(udev, ep_index);
  808. udev->status = USB_ST_NAK_REC;
  809. udev->act_len = 0;
  810. return -ETIMEDOUT;
  811. }