xhci-ring.c 28 KB

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