blob: db8b8f200250986af254de359f55996eb9641fbd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vivek Gautam4912dcc2013-09-14 14:02:45 +05302/*
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>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053014 */
15
16#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053019#include <asm/byteorder.h>
20#include <usb.h>
21#include <asm/unaligned.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060022#include <linux/bug.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090023#include <linux/errno.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053024
Jean-Jacques Hiblotad4142b2019-09-11 11:33:46 +020025#include <usb/xhci.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053026
Mark Kettenisfac410c2023-01-21 20:27:55 +010027/*
28 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
29 * address of the TRB.
30 */
31dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
32 union xhci_trb *trb)
33{
34 unsigned long segment_offset;
35
36 if (!seg || !trb || trb < seg->trbs)
37 return 0;
38 /* offset in TRBs */
39 segment_offset = trb - seg->trbs;
40 if (segment_offset >= TRBS_PER_SEGMENT)
41 return 0;
42 return seg->dma + (segment_offset * sizeof(*trb));
43}
44
Vivek Gautam4912dcc2013-09-14 14:02:45 +053045/**
46 * Is this TRB a link TRB or was the last TRB the last TRB in this event ring
47 * segment? I.e. would the updated event TRB pointer step off the end of the
48 * event seg ?
49 *
50 * @param ctrl Host controller data structure
51 * @param ring pointer to the ring
52 * @param seg poniter to the segment to which TRB belongs
53 * @param trb poniter to the ring trb
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010054 * Return: 1 if this TRB a link TRB else 0
Vivek Gautam4912dcc2013-09-14 14:02:45 +053055 */
56static int last_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
57 struct xhci_segment *seg, union xhci_trb *trb)
58{
59 if (ring == ctrl->event_ring)
60 return trb == &seg->trbs[TRBS_PER_SEGMENT];
61 else
62 return TRB_TYPE_LINK_LE32(trb->link.control);
63}
64
65/**
66 * Does this link TRB point to the first segment in a ring,
67 * or was the previous TRB the last TRB on the last segment in the ERST?
68 *
69 * @param ctrl Host controller data structure
70 * @param ring pointer to the ring
71 * @param seg poniter to the segment to which TRB belongs
72 * @param trb poniter to the ring trb
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010073 * Return: 1 if this TRB is the last TRB on the last segment else 0
Vivek Gautam4912dcc2013-09-14 14:02:45 +053074 */
75static bool last_trb_on_last_seg(struct xhci_ctrl *ctrl,
76 struct xhci_ring *ring,
77 struct xhci_segment *seg,
78 union xhci_trb *trb)
79{
80 if (ring == ctrl->event_ring)
81 return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
82 (seg->next == ring->first_seg));
83 else
84 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
85}
86
87/**
88 * See Cycle bit rules. SW is the consumer for the event ring only.
89 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
90 *
91 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
92 * chain bit is set), then set the chain bit in all the following link TRBs.
93 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
94 * have their chain bit cleared (so that each Link TRB is a separate TD).
95 *
96 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
97 * set, but other sections talk about dealing with the chain bit set. This was
98 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
99 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
100 *
101 * @param ctrl Host controller data structure
102 * @param ring pointer to the ring
103 * @param more_trbs_coming flag to indicate whether more trbs
104 * are expected or NOT.
105 * Will you enqueue more TRBs before calling
106 * prepare_ring()?
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100107 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530108 */
109static void inc_enq(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
110 bool more_trbs_coming)
111{
112 u32 chain;
113 union xhci_trb *next;
114
115 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
116 next = ++(ring->enqueue);
117
118 /*
119 * Update the dequeue pointer further if that was a link TRB or we're at
120 * the end of an event ring segment (which doesn't have link TRBS)
121 */
122 while (last_trb(ctrl, ring, ring->enq_seg, next)) {
123 if (ring != ctrl->event_ring) {
124 /*
125 * If the caller doesn't plan on enqueueing more
126 * TDs before ringing the doorbell, then we
127 * don't want to give the link TRB to the
128 * hardware just yet. We'll give the link TRB
129 * back in prepare_ring() just before we enqueue
130 * the TD at the top of the ring.
131 */
132 if (!chain && !more_trbs_coming)
133 break;
134
135 /*
136 * If we're not dealing with 0.95 hardware or
137 * isoc rings on AMD 0.96 host,
138 * carry over the chain bit of the previous TRB
139 * (which may mean the chain bit is cleared).
140 */
141 next->link.control &= cpu_to_le32(~TRB_CHAIN);
142 next->link.control |= cpu_to_le32(chain);
143
144 next->link.control ^= cpu_to_le32(TRB_CYCLE);
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300145 xhci_flush_cache((uintptr_t)next,
146 sizeof(union xhci_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530147 }
148 /* Toggle the cycle bit after the last ring segment. */
149 if (last_trb_on_last_seg(ctrl, ring,
150 ring->enq_seg, next))
151 ring->cycle_state = (ring->cycle_state ? 0 : 1);
152
153 ring->enq_seg = ring->enq_seg->next;
154 ring->enqueue = ring->enq_seg->trbs;
155 next = ring->enqueue;
156 }
157}
158
159/**
160 * See Cycle bit rules. SW is the consumer for the event ring only.
161 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
162 *
163 * @param ctrl Host controller data structure
164 * @param ring Ring whose Dequeue TRB pointer needs to be incremented.
165 * return none
166 */
167static void inc_deq(struct xhci_ctrl *ctrl, struct xhci_ring *ring)
168{
169 do {
170 /*
171 * Update the dequeue pointer further if that was a link TRB or
172 * we're at the end of an event ring segment (which doesn't have
173 * link TRBS)
174 */
175 if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) {
176 if (ring == ctrl->event_ring &&
177 last_trb_on_last_seg(ctrl, ring,
178 ring->deq_seg, ring->dequeue)) {
179 ring->cycle_state = (ring->cycle_state ? 0 : 1);
180 }
181 ring->deq_seg = ring->deq_seg->next;
182 ring->dequeue = ring->deq_seg->trbs;
183 } else {
184 ring->dequeue++;
185 }
186 } while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue));
187}
188
189/**
190 * Generic function for queueing a TRB on a ring.
191 * The caller must have checked to make sure there's room on the ring.
192 *
193 * @param more_trbs_coming: Will you enqueue more TRBs before calling
194 * prepare_ring()?
195 * @param ctrl Host controller data structure
196 * @param ring pointer to the ring
197 * @param more_trbs_coming flag to indicate whether more trbs
198 * @param trb_fields pointer to trb field array containing TRB contents
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100199 * Return: pointer to the enqueued trb
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530200 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100201static dma_addr_t queue_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
202 bool more_trbs_coming, unsigned int *trb_fields)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530203{
204 struct xhci_generic_trb *trb;
205 int i;
206
207 trb = &ring->enqueue->generic;
208
209 for (i = 0; i < 4; i++)
210 trb->field[i] = cpu_to_le32(trb_fields[i]);
211
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300212 xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530213
214 inc_enq(ctrl, ring, more_trbs_coming);
215
Mark Kettenisfac410c2023-01-21 20:27:55 +0100216 return xhci_trb_virt_to_dma(ring->enq_seg, (union xhci_trb *)trb);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530217}
218
219/**
220 * Does various checks on the endpoint ring, and makes it ready
221 * to queue num_trbs.
222 *
223 * @param ctrl Host controller data structure
224 * @param ep_ring pointer to the EP Transfer Ring
225 * @param ep_state State of the End Point
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100226 * Return: error code in case of invalid ep_state, 0 on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530227 */
228static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring,
229 u32 ep_state)
230{
231 union xhci_trb *next = ep_ring->enqueue;
232
233 /* Make sure the endpoint has been added to xHC schedule */
234 switch (ep_state) {
235 case EP_STATE_DISABLED:
236 /*
237 * USB core changed config/interfaces without notifying us,
238 * or hardware is reporting the wrong state.
239 */
240 puts("WARN urb submitted to disabled ep\n");
241 return -ENOENT;
242 case EP_STATE_ERROR:
243 puts("WARN waiting for error on ep to be cleared\n");
244 return -EINVAL;
245 case EP_STATE_HALTED:
246 puts("WARN halted endpoint, queueing URB anyway.\n");
247 case EP_STATE_STOPPED:
248 case EP_STATE_RUNNING:
249 debug("EP STATE RUNNING.\n");
250 break;
251 default:
252 puts("ERROR unknown endpoint state for ep\n");
253 return -EINVAL;
254 }
255
256 while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) {
257 /*
258 * If we're not dealing with 0.95 hardware or isoc rings
259 * on AMD 0.96 host, clear the chain bit.
260 */
261 next->link.control &= cpu_to_le32(~TRB_CHAIN);
262
263 next->link.control ^= cpu_to_le32(TRB_CYCLE);
264
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300265 xhci_flush_cache((uintptr_t)next, sizeof(union xhci_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530266
267 /* Toggle the cycle bit after the last ring segment. */
268 if (last_trb_on_last_seg(ctrl, ep_ring,
269 ep_ring->enq_seg, next))
270 ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1);
271 ep_ring->enq_seg = ep_ring->enq_seg->next;
272 ep_ring->enqueue = ep_ring->enq_seg->trbs;
273 next = ep_ring->enqueue;
274 }
275
276 return 0;
277}
278
279/**
280 * Generic function for queueing a command TRB on the command ring.
281 * Check to make sure there's room on the command ring for one command TRB.
282 *
283 * @param ctrl Host controller data structure
284 * @param ptr Pointer address to write in the first two fields (opt.)
285 * @param slot_id Slot ID to encode in the flags field (opt.)
286 * @param ep_index Endpoint index to encode in the flags field (opt.)
287 * @param cmd Command type to enqueue
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100288 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530289 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100290void xhci_queue_command(struct xhci_ctrl *ctrl, dma_addr_t addr, u32 slot_id,
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530291 u32 ep_index, trb_type cmd)
292{
293 u32 fields[4];
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530294
295 BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING));
296
Mark Kettenisfac410c2023-01-21 20:27:55 +0100297 fields[0] = lower_32_bits(addr);
298 fields[1] = upper_32_bits(addr);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530299 fields[2] = 0;
Bin Meng474b2502017-07-19 21:49:54 +0800300 fields[3] = TRB_TYPE(cmd) | SLOT_ID_FOR_TRB(slot_id) |
301 ctrl->cmd_ring->cycle_state;
302
303 /*
304 * Only 'reset endpoint', 'stop endpoint' and 'set TR dequeue pointer'
305 * commands need endpoint id encoded.
306 */
307 if (cmd >= TRB_RESET_EP && cmd <= TRB_SET_DEQ)
308 fields[3] |= EP_ID_FOR_TRB(ep_index);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530309
310 queue_trb(ctrl, ctrl->cmd_ring, false, fields);
311
312 /* Ring the command ring doorbell */
313 xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST);
314}
315
developer570c2a92020-09-08 18:59:56 +0200316/*
317 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
318 * packets remaining in the TD (*not* including this TRB).
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530319 *
developer570c2a92020-09-08 18:59:56 +0200320 * Total TD packet count = total_packet_count =
321 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
322 *
323 * Packets transferred up to and including this TRB = packets_transferred =
324 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
325 *
326 * TD size = total_packet_count - packets_transferred
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530327 *
developer570c2a92020-09-08 18:59:56 +0200328 * For xHCI 0.96 and older, TD size field should be the remaining bytes
329 * including this TRB, right shifted by 10
330 *
331 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31.
332 * This is taken care of in the TRB_TD_SIZE() macro
333 *
334 * The last TRB in a TD must have the TD size set to zero.
335 *
336 * @param ctrl host controller data structure
337 * @param transferred total size sent so far
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530338 * @param trb_buff_len length of the TRB Buffer
developer570c2a92020-09-08 18:59:56 +0200339 * @param td_total_len total packet count
340 * @param maxp max packet size of current pipe
341 * @param more_trbs_coming indicate last trb in TD
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100342 * Return: remainder
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530343 */
developer570c2a92020-09-08 18:59:56 +0200344static u32 xhci_td_remainder(struct xhci_ctrl *ctrl, int transferred,
345 int trb_buff_len, unsigned int td_total_len,
346 int maxp, bool more_trbs_coming)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530347{
developer570c2a92020-09-08 18:59:56 +0200348 u32 total_packet_count;
349
developer80390532020-09-08 18:59:57 +0200350 /* MTK xHCI 0.96 contains some features from 1.0 */
351 if (ctrl->hci_version < 0x100 && !(ctrl->quirks & XHCI_MTK_HOST))
developer570c2a92020-09-08 18:59:56 +0200352 return ((td_total_len - transferred) >> 10);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530353
354 /* One TRB with a zero-length data packet. */
developer570c2a92020-09-08 18:59:56 +0200355 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
356 trb_buff_len == td_total_len)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530357 return 0;
358
developer80390532020-09-08 18:59:57 +0200359 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */
360 if ((ctrl->quirks & XHCI_MTK_HOST) && (ctrl->hci_version < 0x100))
361 trb_buff_len = 0;
362
developer570c2a92020-09-08 18:59:56 +0200363 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530364
developer570c2a92020-09-08 18:59:56 +0200365 /* Queueing functions don't count the current TRB into transferred */
366 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530367}
368
369/**
370 * Ring the doorbell of the End Point
371 *
372 * @param udev pointer to the USB device structure
373 * @param ep_index index of the endpoint
374 * @param start_cycle cycle flag of the first TRB
375 * @param start_trb pionter to the first TRB
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100376 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530377 */
378static void giveback_first_trb(struct usb_device *udev, int ep_index,
379 int start_cycle,
380 struct xhci_generic_trb *start_trb)
381{
Simon Glassa49e27b2015-03-25 12:22:49 -0600382 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530383
384 /*
385 * Pass all the TRBs to the hardware at once and make sure this write
386 * isn't reordered.
387 */
388 if (start_cycle)
389 start_trb->field[3] |= cpu_to_le32(start_cycle);
390 else
391 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
392
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300393 xhci_flush_cache((uintptr_t)start_trb, sizeof(struct xhci_generic_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530394
395 /* Ringing EP doorbell here */
396 xhci_writel(&ctrl->dba->doorbell[udev->slot_id],
397 DB_VALUE(ep_index, 0));
398
399 return;
400}
401
402/**** POLLING mechanism for XHCI ****/
403
404/**
405 * Finalizes a handled event TRB by advancing our dequeue pointer and giving
406 * the TRB back to the hardware for recycling. Must call this exactly once at
407 * the end of each event handler, and not touch the TRB again afterwards.
408 *
409 * @param ctrl Host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100410 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530411 */
412void xhci_acknowledge_event(struct xhci_ctrl *ctrl)
413{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100414 dma_addr_t deq;
415
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530416 /* Advance our dequeue pointer to the next event */
417 inc_deq(ctrl, ctrl->event_ring);
418
419 /* Inform the hardware */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100420 deq = xhci_trb_virt_to_dma(ctrl->event_ring->deq_seg,
421 ctrl->event_ring->dequeue);
422 xhci_writeq(&ctrl->ir_set->erst_dequeue, deq | ERST_EHB);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530423}
424
425/**
426 * Checks if there is a new event to handle on the event ring.
427 *
428 * @param ctrl Host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100429 * Return: 0 if failure else 1 on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530430 */
431static int event_ready(struct xhci_ctrl *ctrl)
432{
433 union xhci_trb *event;
434
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300435 xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue,
436 sizeof(union xhci_trb));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530437
438 event = ctrl->event_ring->dequeue;
439
440 /* Does the HC or OS own the TRB? */
441 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
442 ctrl->event_ring->cycle_state)
443 return 0;
444
445 return 1;
446}
447
448/**
449 * Waits for a specific type of event and returns it. Discards unexpected
450 * events. Caller *must* call xhci_acknowledge_event() after it is finished
451 * processing the event, and must not access the returned pointer afterwards.
452 *
453 * @param ctrl Host controller data structure
454 * @param expected TRB type expected from Event TRB
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100455 * Return: pointer to event trb
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530456 */
457union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
458{
459 trb_type type;
460 unsigned long ts = get_timer(0);
461
462 do {
463 union xhci_trb *event = ctrl->event_ring->dequeue;
464
465 if (!event_ready(ctrl))
466 continue;
467
468 type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
Hector Martin191772f2023-10-29 15:37:39 +0900469 if (type == expected ||
470 (expected == TRB_NONE && type != TRB_PORT_STATUS))
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530471 return event;
472
473 if (type == TRB_PORT_STATUS)
474 /* TODO: remove this once enumeration has been reworked */
475 /*
476 * Port status change events always have a
477 * successful completion code
478 */
479 BUG_ON(GET_COMP_CODE(
480 le32_to_cpu(event->generic.field[2])) !=
481 COMP_SUCCESS);
482 else
483 printf("Unexpected XHCI event TRB, skipping... "
484 "(%08x %08x %08x %08x)\n",
485 le32_to_cpu(event->generic.field[0]),
486 le32_to_cpu(event->generic.field[1]),
487 le32_to_cpu(event->generic.field[2]),
488 le32_to_cpu(event->generic.field[3]));
489
490 xhci_acknowledge_event(ctrl);
491 } while (get_timer(ts) < XHCI_TIMEOUT);
492
493 if (expected == TRB_TRANSFER)
494 return NULL;
495
496 printf("XHCI timeout on event type %d... cannot recover.\n", expected);
497 BUG();
498}
499
500/*
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200501 * Send reset endpoint command for given endpoint. This recovers from a
502 * halted endpoint (e.g. due to a stall error).
503 */
504static void reset_ep(struct usb_device *udev, int ep_index)
505{
506 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
507 struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring;
508 union xhci_trb *event;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100509 u64 addr;
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200510 u32 field;
511
512 printf("Resetting EP %d...\n", ep_index);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100513 xhci_queue_command(ctrl, 0, udev->slot_id, ep_index, TRB_RESET_EP);
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200514 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
Hector Martinc5d7dac2023-10-29 15:37:38 +0900515 if (!event)
516 return;
517
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200518 field = le32_to_cpu(event->trans_event.flags);
519 BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
520 xhci_acknowledge_event(ctrl);
521
Mark Kettenisfac410c2023-01-21 20:27:55 +0100522 addr = xhci_trb_virt_to_dma(ring->enq_seg,
523 (void *)((uintptr_t)ring->enqueue | ring->cycle_state));
524 xhci_queue_command(ctrl, addr, udev->slot_id, ep_index, TRB_SET_DEQ);
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200525 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
Hector Martinc5d7dac2023-10-29 15:37:38 +0900526 if (!event)
527 return;
528
Stefan Agnerec2b73d2021-09-27 14:42:58 +0200529 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
530 != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
531 event->event_cmd.status)) != COMP_SUCCESS);
532 xhci_acknowledge_event(ctrl);
533}
534
535/*
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530536 * Stops transfer processing for an endpoint and throws away all unprocessed
537 * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next
538 * xhci_bulk_tx/xhci_ctrl_tx on this enpoint will add new transfers there and
539 * ring the doorbell, causing this endpoint to start working again.
540 * (Careful: This will BUG() when there was no transfer in progress. Shouldn't
541 * happen in practice for current uses and is too complicated to fix right now.)
542 */
543static void abort_td(struct usb_device *udev, int ep_index)
544{
Simon Glassa49e27b2015-03-25 12:22:49 -0600545 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530546 struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring;
547 union xhci_trb *event;
Hector Martinf28db6b2023-10-29 15:37:40 +0900548 xhci_comp_code comp;
Hector Martin191772f2023-10-29 15:37:39 +0900549 trb_type type;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100550 u64 addr;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530551 u32 field;
552
Mark Kettenisfac410c2023-01-21 20:27:55 +0100553 xhci_queue_command(ctrl, 0, udev->slot_id, ep_index, TRB_STOP_RING);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530554
Hector Martin191772f2023-10-29 15:37:39 +0900555 event = xhci_wait_for_event(ctrl, TRB_NONE);
Hector Martinc5d7dac2023-10-29 15:37:38 +0900556 if (!event)
557 return;
558
Hector Martin191772f2023-10-29 15:37:39 +0900559 type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
560 if (type == TRB_TRANSFER) {
561 field = le32_to_cpu(event->trans_event.flags);
562 BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
563 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
564 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len
565 != COMP_STOP)));
566 xhci_acknowledge_event(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530567
Hector Martin191772f2023-10-29 15:37:39 +0900568 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
569 if (!event)
570 return;
571 type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
Hector Martinc5d7dac2023-10-29 15:37:38 +0900572
Hector Martin191772f2023-10-29 15:37:39 +0900573 } else {
574 printf("abort_td: Expected a TRB_TRANSFER TRB first\n");
575 }
576
Hector Martinf28db6b2023-10-29 15:37:40 +0900577 comp = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
Hector Martin191772f2023-10-29 15:37:39 +0900578 BUG_ON(type != TRB_COMPLETION ||
579 TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
Hector Martinf28db6b2023-10-29 15:37:40 +0900580 != udev->slot_id || (comp != COMP_SUCCESS && comp
581 != COMP_CTX_STATE));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530582 xhci_acknowledge_event(ctrl);
583
Mark Kettenisfac410c2023-01-21 20:27:55 +0100584 addr = xhci_trb_virt_to_dma(ring->enq_seg,
585 (void *)((uintptr_t)ring->enqueue | ring->cycle_state));
586 xhci_queue_command(ctrl, addr, udev->slot_id, ep_index, TRB_SET_DEQ);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530587 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
Hector Martinc5d7dac2023-10-29 15:37:38 +0900588 if (!event)
589 return;
590
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530591 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
592 != udev->slot_id || GET_COMP_CODE(le32_to_cpu(
593 event->event_cmd.status)) != COMP_SUCCESS);
594 xhci_acknowledge_event(ctrl);
595}
596
597static void record_transfer_result(struct usb_device *udev,
598 union xhci_trb *event, int length)
599{
600 udev->act_len = min(length, length -
Masahiro Yamadadb204642014-11-07 03:03:31 +0900601 (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len)));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530602
603 switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) {
604 case COMP_SUCCESS:
605 BUG_ON(udev->act_len != length);
606 /* fallthrough */
607 case COMP_SHORT_TX:
608 udev->status = 0;
609 break;
610 case COMP_STALL:
611 udev->status = USB_ST_STALLED;
612 break;
613 case COMP_DB_ERR:
614 case COMP_TRB_ERR:
615 udev->status = USB_ST_BUF_ERR;
616 break;
617 case COMP_BABBLE:
618 udev->status = USB_ST_BABBLE_DET;
619 break;
620 default:
621 udev->status = 0x80; /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */
622 }
623}
624
625/**** Bulk and Control transfer methods ****/
626/**
627 * Queues up the BULK Request
628 *
629 * @param udev pointer to the USB device structure
630 * @param pipe contains the DIR_IN or OUT , devnum
631 * @param length length of the buffer
632 * @param buffer buffer to be read/written based on the request
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100633 * Return: returns 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530634 */
635int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,
636 int length, void *buffer)
637{
638 int num_trbs = 0;
639 struct xhci_generic_trb *start_trb;
Gustavo A. R. Silva0a1ef7c2018-01-20 02:37:31 -0600640 bool first_trb = false;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530641 int start_cycle;
642 u32 field = 0;
643 u32 length_field = 0;
Simon Glassa49e27b2015-03-25 12:22:49 -0600644 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530645 int slot_id = udev->slot_id;
646 int ep_index;
647 struct xhci_virt_device *virt_dev;
648 struct xhci_ep_ctx *ep_ctx;
649 struct xhci_ring *ring; /* EP transfer ring */
650 union xhci_trb *event;
651
652 int running_total, trb_buff_len;
developer570c2a92020-09-08 18:59:56 +0200653 bool more_trbs_coming = true;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530654 int maxpacketsize;
655 u64 addr;
656 int ret;
657 u32 trb_fields[4];
Mark Kettenisfac410c2023-01-21 20:27:55 +0100658 u64 buf_64 = xhci_dma_map(ctrl, buffer, length);
659 dma_addr_t last_transfer_trb_addr;
Ran Wanga0505832020-11-18 15:49:02 +0800660 int available_length;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530661
662 debug("dev=%p, pipe=%lx, buffer=%p, length=%d\n",
663 udev, pipe, buffer, length);
664
Ran Wanga0505832020-11-18 15:49:02 +0800665 available_length = length;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530666 ep_index = usb_pipe_ep_index(pipe);
667 virt_dev = ctrl->devs[slot_id];
668
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300669 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
670 virt_dev->out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530671
672 ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
673
Hector Martin6d204372023-10-29 15:37:41 +0900674 /*
675 * If the endpoint was halted due to a prior error, resume it before
676 * the next transfer. It is the responsibility of the upper layer to
677 * have dealt with whatever caused the error.
678 */
679 if ((le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) == EP_STATE_HALTED)
680 reset_ep(udev, ep_index);
681
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530682 ring = virt_dev->eps[ep_index].ring;
683 /*
684 * How much data is (potentially) left before the 64KB boundary?
685 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
686 * that the buffer should not span 64KB boundary. if so
687 * we send request in more than 1 TRB by chaining them.
688 */
689 running_total = TRB_MAX_BUFF_SIZE -
Mark Kettenisfac410c2023-01-21 20:27:55 +0100690 (lower_32_bits(buf_64) & (TRB_MAX_BUFF_SIZE - 1));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530691 trb_buff_len = running_total;
692 running_total &= TRB_MAX_BUFF_SIZE - 1;
693
694 /*
695 * If there's some data on this 64KB chunk, or we have to send a
696 * zero-length transfer, we need at least one TRB
697 */
698 if (running_total != 0 || length == 0)
699 num_trbs++;
700
701 /* How many more 64KB chunks to transfer, how many more TRBs? */
702 while (running_total < length) {
703 num_trbs++;
704 running_total += TRB_MAX_BUFF_SIZE;
705 }
706
707 /*
708 * XXX: Calling routine prepare_ring() called in place of
709 * prepare_trasfer() as there in 'Linux' since we are not
710 * maintaining multiple TDs/transfer at the same time.
711 */
712 ret = prepare_ring(ctrl, ring,
713 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
714 if (ret < 0)
715 return ret;
716
717 /*
718 * Don't give the first TRB to the hardware (by toggling the cycle bit)
719 * until we've finished creating all the other TRBs. The ring's cycle
720 * state may change as we enqueue the other TRBs, so save it too.
721 */
722 start_trb = &ring->enqueue->generic;
723 start_cycle = ring->cycle_state;
724
725 running_total = 0;
726 maxpacketsize = usb_maxpacket(udev, pipe);
727
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530728 /* How much data is in the first TRB? */
729 /*
730 * How much data is (potentially) left before the 64KB boundary?
731 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
732 * that the buffer should not span 64KB boundary. if so
733 * we send request in more than 1 TRB by chaining them.
734 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100735 addr = buf_64;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530736
737 if (trb_buff_len > length)
738 trb_buff_len = length;
739
740 first_trb = true;
741
742 /* flush the buffer before use */
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300743 xhci_flush_cache((uintptr_t)buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530744
745 /* Queue the first TRB, even if it's zero-length */
746 do {
747 u32 remainder = 0;
748 field = 0;
749 /* Don't change the cycle bit of the first TRB until later */
750 if (first_trb) {
751 first_trb = false;
752 if (start_cycle == 0)
753 field |= TRB_CYCLE;
754 } else {
755 field |= ring->cycle_state;
756 }
757
758 /*
759 * Chain all the TRBs together; clear the chain bit in the last
760 * TRB to indicate it's the last TRB in the chain.
761 */
developer570c2a92020-09-08 18:59:56 +0200762 if (num_trbs > 1) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530763 field |= TRB_CHAIN;
developer570c2a92020-09-08 18:59:56 +0200764 } else {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530765 field |= TRB_IOC;
developer570c2a92020-09-08 18:59:56 +0200766 more_trbs_coming = false;
767 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530768
769 /* Only set interrupt on short packet for IN endpoints */
770 if (usb_pipein(pipe))
771 field |= TRB_ISP;
772
773 /* Set the TRB length, TD size, and interrupter fields. */
developer570c2a92020-09-08 18:59:56 +0200774 remainder = xhci_td_remainder(ctrl, running_total, trb_buff_len,
775 length, maxpacketsize,
776 more_trbs_coming);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530777
developer6cabb142020-09-08 19:00:00 +0200778 length_field = (TRB_LEN(trb_buff_len) |
developer570c2a92020-09-08 18:59:56 +0200779 TRB_TD_SIZE(remainder) |
developer6cabb142020-09-08 19:00:00 +0200780 TRB_INTR_TARGET(0));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530781
782 trb_fields[0] = lower_32_bits(addr);
783 trb_fields[1] = upper_32_bits(addr);
784 trb_fields[2] = length_field;
developer497dcfa2020-09-08 18:59:59 +0200785 trb_fields[3] = field | TRB_TYPE(TRB_NORMAL);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530786
Ran Wanga0505832020-11-18 15:49:02 +0800787 last_transfer_trb_addr = queue_trb(ctrl, ring, (num_trbs > 1), trb_fields);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530788
789 --num_trbs;
790
791 running_total += trb_buff_len;
792
793 /* Calculate length for next transfer */
794 addr += trb_buff_len;
795 trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE);
796 } while (running_total < length);
797
798 giveback_first_trb(udev, ep_index, start_cycle, start_trb);
799
Ran Wanga0505832020-11-18 15:49:02 +0800800again:
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530801 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
802 if (!event) {
803 debug("XHCI bulk transfer timed out, aborting...\n");
804 abort_td(udev, ep_index);
805 udev->status = USB_ST_NAK_REC; /* closest thing to a timeout */
806 udev->act_len = 0;
807 return -ETIMEDOUT;
808 }
Ran Wanga0505832020-11-18 15:49:02 +0800809
Stefan Roese5e3c1462021-01-15 08:52:56 +0100810 if ((uintptr_t)(le64_to_cpu(event->trans_event.buffer)) !=
Mark Kettenisfac410c2023-01-21 20:27:55 +0100811 (uintptr_t)last_transfer_trb_addr) {
Ran Wanga0505832020-11-18 15:49:02 +0800812 available_length -=
813 (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len));
814 xhci_acknowledge_event(ctrl);
815 goto again;
816 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530817
Ran Wanga0505832020-11-18 15:49:02 +0800818 field = le32_to_cpu(event->trans_event.flags);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530819 BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
820 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530821
Ran Wanga0505832020-11-18 15:49:02 +0800822 record_transfer_result(udev, event, available_length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530823 xhci_acknowledge_event(ctrl);
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300824 xhci_inval_cache((uintptr_t)buffer, length);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100825 xhci_dma_unmap(ctrl, buf_64, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530826
827 return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
828}
829
830/**
831 * Queues up the Control Transfer Request
832 *
833 * @param udev pointer to the USB device structure
834 * @param pipe contains the DIR_IN or OUT , devnum
835 * @param req request type
836 * @param length length of the buffer
837 * @param buffer buffer to be read/written based on the request
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100838 * Return: returns 0 if successful else error code on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530839 */
840int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,
841 struct devrequest *req, int length,
842 void *buffer)
843{
844 int ret;
845 int start_cycle;
846 int num_trbs;
847 u32 field;
848 u32 length_field;
849 u64 buf_64 = 0;
850 struct xhci_generic_trb *start_trb;
Simon Glassa49e27b2015-03-25 12:22:49 -0600851 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530852 int slot_id = udev->slot_id;
853 int ep_index;
854 u32 trb_fields[4];
855 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
856 struct xhci_ring *ep_ring;
857 union xhci_trb *event;
developer570c2a92020-09-08 18:59:56 +0200858 u32 remainder;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530859
860 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
861 req->request, req->request,
862 req->requesttype, req->requesttype,
863 le16_to_cpu(req->value), le16_to_cpu(req->value),
864 le16_to_cpu(req->index));
865
866 ep_index = usb_pipe_ep_index(pipe);
867
868 ep_ring = virt_dev->eps[ep_index].ring;
869
870 /*
871 * Check to see if the max packet size for the default control
872 * endpoint changed during FS device enumeration
873 */
874 if (udev->speed == USB_SPEED_FULL) {
875 ret = xhci_check_maxpacket(udev);
876 if (ret < 0)
877 return ret;
878 }
879
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300880 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
881 virt_dev->out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530882
883 struct xhci_ep_ctx *ep_ctx = NULL;
884 ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
885
886 /* 1 TRB for setup, 1 for status */
887 num_trbs = 2;
888 /*
889 * Don't need to check if we need additional event data and normal TRBs,
890 * since data in control transfers will never get bigger than 16MB
891 * XXX: can we get a buffer that crosses 64KB boundaries?
892 */
893
894 if (length > 0)
895 num_trbs++;
896 /*
897 * XXX: Calling routine prepare_ring() called in place of
898 * prepare_trasfer() as there in 'Linux' since we are not
899 * maintaining multiple TDs/transfer at the same time.
900 */
901 ret = prepare_ring(ctrl, ep_ring,
902 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
903
904 if (ret < 0)
905 return ret;
906
907 /*
908 * Don't give the first TRB to the hardware (by toggling the cycle bit)
909 * until we've finished creating all the other TRBs. The ring's cycle
910 * state may change as we enqueue the other TRBs, so save it too.
911 */
912 start_trb = &ep_ring->enqueue->generic;
913 start_cycle = ep_ring->cycle_state;
914
915 debug("start_trb %p, start_cycle %d\n", start_trb, start_cycle);
916
917 /* Queue setup TRB - see section 6.4.1.2.1 */
918 /* FIXME better way to translate setup_packet into two u32 fields? */
919 field = 0;
developer497dcfa2020-09-08 18:59:59 +0200920 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530921 if (start_cycle == 0)
922 field |= 0x1;
923
924 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
developer80390532020-09-08 18:59:57 +0200925 if (ctrl->hci_version >= 0x100 || ctrl->quirks & XHCI_MTK_HOST) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530926 if (length > 0) {
927 if (req->requesttype & USB_DIR_IN)
developer57c052b2020-09-08 19:00:01 +0200928 field |= TRB_TX_TYPE(TRB_DATA_IN);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530929 else
developer57c052b2020-09-08 19:00:01 +0200930 field |= TRB_TX_TYPE(TRB_DATA_OUT);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530931 }
932 }
933
Stefan Roeseede9de12021-04-06 12:10:18 +0200934 debug("req->requesttype = %d, req->request = %d, req->value = %d, req->index = %d, req->length = %d\n",
935 req->requesttype, req->request, le16_to_cpu(req->value),
936 le16_to_cpu(req->index), le16_to_cpu(req->length));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530937
938 trb_fields[0] = req->requesttype | req->request << 8 |
939 le16_to_cpu(req->value) << 16;
940 trb_fields[1] = le16_to_cpu(req->index) |
941 le16_to_cpu(req->length) << 16;
942 /* TRB_LEN | (TRB_INTR_TARGET) */
developer6cabb142020-09-08 19:00:00 +0200943 trb_fields[2] = (TRB_LEN(8) | TRB_INTR_TARGET(0));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530944 /* Immediate data in pointer */
945 trb_fields[3] = field;
946 queue_trb(ctrl, ep_ring, true, trb_fields);
947
948 /* Re-initializing field to zero */
949 field = 0;
950 /* If there's data, queue data TRBs */
951 /* Only set interrupt on short packet for IN endpoints */
952 if (usb_pipein(pipe))
developer497dcfa2020-09-08 18:59:59 +0200953 field = TRB_ISP | TRB_TYPE(TRB_DATA);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530954 else
developer497dcfa2020-09-08 18:59:59 +0200955 field = TRB_TYPE(TRB_DATA);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530956
developer570c2a92020-09-08 18:59:56 +0200957 remainder = xhci_td_remainder(ctrl, 0, length, length,
958 usb_maxpacket(udev, pipe), true);
developer6cabb142020-09-08 19:00:00 +0200959 length_field = TRB_LEN(length) | TRB_TD_SIZE(remainder) |
960 TRB_INTR_TARGET(0);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530961 debug("length_field = %d, length = %d,"
962 "xhci_td_remainder(length) = %d , TRB_INTR_TARGET(0) = %d\n",
developer6cabb142020-09-08 19:00:00 +0200963 length_field, TRB_LEN(length),
developer570c2a92020-09-08 18:59:56 +0200964 TRB_TD_SIZE(remainder), 0);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530965
966 if (length > 0) {
967 if (req->requesttype & USB_DIR_IN)
968 field |= TRB_DIR_IN;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100969 buf_64 = xhci_dma_map(ctrl, buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530970
971 trb_fields[0] = lower_32_bits(buf_64);
972 trb_fields[1] = upper_32_bits(buf_64);
973 trb_fields[2] = length_field;
974 trb_fields[3] = field | ep_ring->cycle_state;
975
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300976 xhci_flush_cache((uintptr_t)buffer, length);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530977 queue_trb(ctrl, ep_ring, true, trb_fields);
978 }
979
980 /*
981 * Queue status TRB -
982 * see Table 7 and sections 4.11.2.2 and 6.4.1.2.3
983 */
984
985 /* If the device sent data, the status stage is an OUT transfer */
986 field = 0;
987 if (length > 0 && req->requesttype & USB_DIR_IN)
988 field = 0;
989 else
990 field = TRB_DIR_IN;
991
992 trb_fields[0] = 0;
993 trb_fields[1] = 0;
developer6cabb142020-09-08 19:00:00 +0200994 trb_fields[2] = TRB_INTR_TARGET(0);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530995 /* Event on completion */
996 trb_fields[3] = field | TRB_IOC |
developer497dcfa2020-09-08 18:59:59 +0200997 TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530998
999 queue_trb(ctrl, ep_ring, false, trb_fields);
1000
1001 giveback_first_trb(udev, ep_index, start_cycle, start_trb);
1002
1003 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
1004 if (!event)
1005 goto abort;
1006 field = le32_to_cpu(event->trans_event.flags);
1007
1008 BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
1009 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
1010
1011 record_transfer_result(udev, event, length);
1012 xhci_acknowledge_event(ctrl);
Stefan Agnerec2b73d2021-09-27 14:42:58 +02001013 if (udev->status == USB_ST_STALLED) {
1014 reset_ep(udev, ep_index);
1015 return -EPIPE;
1016 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301017
1018 /* Invalidate buffer to make it available to usb-core */
Mark Kettenisfac410c2023-01-21 20:27:55 +01001019 if (length > 0) {
Sergey Temerkhanov38593462015-04-01 17:18:45 +03001020 xhci_inval_cache((uintptr_t)buffer, length);
Mark Kettenisfac410c2023-01-21 20:27:55 +01001021 xhci_dma_unmap(ctrl, buf_64, length);
1022 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301023
1024 if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))
1025 == COMP_SHORT_TX) {
1026 /* Short data stage, clear up additional status stage event */
1027 event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
1028 if (!event)
1029 goto abort;
1030 BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
1031 BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
1032 xhci_acknowledge_event(ctrl);
1033 }
1034
1035 return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
1036
1037abort:
1038 debug("XHCI control transfer timed out, aborting...\n");
1039 abort_td(udev, ep_index);
1040 udev->status = USB_ST_NAK_REC;
1041 udev->act_len = 0;
1042 return -ETIMEDOUT;
1043}