]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/host/xhci-ring.c
xhci: refactor handle_tx_event() urb giveback
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / host / xhci-ring.c
CommitLineData
7f84eef0
SS
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Ring initialization rules:
25 * 1. Each segment is initialized to zero, except for link TRBs.
26 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
27 * Consumer Cycle State (CCS), depending on ring function.
28 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
29 *
30 * Ring behavior rules:
31 * 1. A ring is empty if enqueue == dequeue. This means there will always be at
32 * least one free TRB in the ring. This is useful if you want to turn that
33 * into a link TRB and expand the ring.
34 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
35 * link TRB, then load the pointer with the address in the link TRB. If the
36 * link TRB had its toggle bit set, you may need to update the ring cycle
37 * state (see cycle bit rules). You may have to do this multiple times
38 * until you reach a non-link TRB.
39 * 3. A ring is full if enqueue++ (for the definition of increment above)
40 * equals the dequeue pointer.
41 *
42 * Cycle bit rules:
43 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
44 * in a link TRB, it must toggle the ring cycle state.
45 * 2. When a producer increments an enqueue pointer and encounters a toggle bit
46 * in a link TRB, it must toggle the ring cycle state.
47 *
48 * Producer rules:
49 * 1. Check if ring is full before you enqueue.
50 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
51 * Update enqueue pointer between each write (which may update the ring
52 * cycle state).
53 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
54 * and endpoint rings. If HC is the producer for the event ring,
55 * and it generates an interrupt according to interrupt modulation rules.
56 *
57 * Consumer rules:
58 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
59 * the TRB is owned by the consumer.
60 * 2. Update dequeue pointer (which may update the ring cycle state) and
61 * continue processing TRBs until you reach a TRB which is not owned by you.
62 * 3. Notify the producer. SW is the consumer for the event ring, and it
63 * updates event ring dequeue pointer. HC is the consumer for the command and
64 * endpoint rings; it generates events on the event ring for these.
65 */
66
8a96c052 67#include <linux/scatterlist.h>
5a0e3ad6 68#include <linux/slab.h>
f9c589e1 69#include <linux/dma-mapping.h>
7f84eef0 70#include "xhci.h"
3a7fa5be 71#include "xhci-trace.h"
0cbd4b34 72#include "xhci-mtk.h"
7f84eef0
SS
73
74/*
75 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
76 * address of the TRB.
77 */
23e3be11 78dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
7f84eef0
SS
79 union xhci_trb *trb)
80{
6071d836 81 unsigned long segment_offset;
7f84eef0 82
6071d836 83 if (!seg || !trb || trb < seg->trbs)
7f84eef0 84 return 0;
6071d836
SS
85 /* offset in TRBs */
86 segment_offset = trb - seg->trbs;
7895086a 87 if (segment_offset >= TRBS_PER_SEGMENT)
7f84eef0 88 return 0;
6071d836 89 return seg->dma + (segment_offset * sizeof(*trb));
7f84eef0
SS
90}
91
0ce57499
MN
92static bool trb_is_noop(union xhci_trb *trb)
93{
94 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]);
95}
96
2d98ef40
MN
97static bool trb_is_link(union xhci_trb *trb)
98{
99 return TRB_TYPE_LINK_LE32(trb->link.control);
100}
101
bd5e67f5
MN
102static bool last_trb_on_seg(struct xhci_segment *seg, union xhci_trb *trb)
103{
104 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1];
105}
106
107static bool last_trb_on_ring(struct xhci_ring *ring,
108 struct xhci_segment *seg, union xhci_trb *trb)
109{
110 return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg);
111}
112
d0c77d84
MN
113static bool link_trb_toggles_cycle(union xhci_trb *trb)
114{
115 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
116}
117
ae636747
SS
118/* Updates trb to point to the next TRB in the ring, and updates seg if the next
119 * TRB is in a new segment. This does not skip over link TRBs, and it does not
120 * effect the ring dequeue or enqueue pointers.
121 */
122static void next_trb(struct xhci_hcd *xhci,
123 struct xhci_ring *ring,
124 struct xhci_segment **seg,
125 union xhci_trb **trb)
126{
2d98ef40 127 if (trb_is_link(*trb)) {
ae636747
SS
128 *seg = (*seg)->next;
129 *trb = ((*seg)->trbs);
130 } else {
a1669b2c 131 (*trb)++;
ae636747
SS
132 }
133}
134
7f84eef0
SS
135/*
136 * See Cycle bit rules. SW is the consumer for the event ring only.
137 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
138 */
3b72fca0 139static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
7f84eef0 140{
7f84eef0 141 ring->deq_updates++;
b008df60 142
bd5e67f5
MN
143 /* event ring doesn't have link trbs, check for last trb */
144 if (ring->type == TYPE_EVENT) {
145 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
50d0206f 146 ring->dequeue++;
bd5e67f5 147 return;
7f84eef0 148 }
bd5e67f5
MN
149 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue))
150 ring->cycle_state ^= 1;
151 ring->deq_seg = ring->deq_seg->next;
152 ring->dequeue = ring->deq_seg->trbs;
153 return;
154 }
155
156 /* All other rings have link trbs */
157 if (!trb_is_link(ring->dequeue)) {
158 ring->dequeue++;
159 ring->num_trbs_free++;
160 }
161 while (trb_is_link(ring->dequeue)) {
162 ring->deq_seg = ring->deq_seg->next;
163 ring->dequeue = ring->deq_seg->trbs;
164 }
165 return;
7f84eef0
SS
166}
167
168/*
169 * See Cycle bit rules. SW is the consumer for the event ring only.
170 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
171 *
172 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
173 * chain bit is set), then set the chain bit in all the following link TRBs.
174 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
175 * have their chain bit cleared (so that each Link TRB is a separate TD).
176 *
177 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
b0567b3f
SS
178 * set, but other sections talk about dealing with the chain bit set. This was
179 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
180 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
6cc30d85
SS
181 *
182 * @more_trbs_coming: Will you enqueue more TRBs before calling
183 * prepare_transfer()?
7f84eef0 184 */
6cc30d85 185static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
3b72fca0 186 bool more_trbs_coming)
7f84eef0
SS
187{
188 u32 chain;
189 union xhci_trb *next;
190
28ccd296 191 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
b008df60 192 /* If this is not event ring, there is one less usable TRB */
2d98ef40 193 if (!trb_is_link(ring->enqueue))
b008df60 194 ring->num_trbs_free--;
7f84eef0
SS
195 next = ++(ring->enqueue);
196
197 ring->enq_updates++;
2251198b 198 /* Update the dequeue pointer further if that was a link TRB */
2d98ef40 199 while (trb_is_link(next)) {
6cc30d85 200
2251198b
MN
201 /*
202 * If the caller doesn't plan on enqueueing more TDs before
203 * ringing the doorbell, then we don't want to give the link TRB
204 * to the hardware just yet. We'll give the link TRB back in
205 * prepare_ring() just before we enqueue the TD at the top of
206 * the ring.
207 */
208 if (!chain && !more_trbs_coming)
209 break;
3b72fca0 210
2251198b
MN
211 /* If we're not dealing with 0.95 hardware or isoc rings on
212 * AMD 0.96 host, carry over the chain bit of the previous TRB
213 * (which may mean the chain bit is cleared).
214 */
215 if (!(ring->type == TYPE_ISOC &&
216 (xhci->quirks & XHCI_AMD_0x96_HOST)) &&
217 !xhci_link_trb_quirk(xhci)) {
218 next->link.control &= cpu_to_le32(~TRB_CHAIN);
219 next->link.control |= cpu_to_le32(chain);
7f84eef0 220 }
2251198b
MN
221 /* Give this link TRB to the hardware */
222 wmb();
223 next->link.control ^= cpu_to_le32(TRB_CYCLE);
224
225 /* Toggle the cycle bit after the last ring segment. */
d0c77d84 226 if (link_trb_toggles_cycle(next))
2251198b
MN
227 ring->cycle_state ^= 1;
228
7f84eef0
SS
229 ring->enq_seg = ring->enq_seg->next;
230 ring->enqueue = ring->enq_seg->trbs;
231 next = ring->enqueue;
232 }
233}
234
235/*
085deb16
AX
236 * Check to see if there's room to enqueue num_trbs on the ring and make sure
237 * enqueue pointer will not advance into dequeue segment. See rules above.
7f84eef0 238 */
b008df60 239static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
7f84eef0
SS
240 unsigned int num_trbs)
241{
085deb16 242 int num_trbs_in_deq_seg;
b008df60 243
085deb16
AX
244 if (ring->num_trbs_free < num_trbs)
245 return 0;
246
247 if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
248 num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
249 if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
250 return 0;
251 }
252
253 return 1;
7f84eef0
SS
254}
255
7f84eef0 256/* Ring the host controller doorbell after placing a command on the ring */
23e3be11 257void xhci_ring_cmd_db(struct xhci_hcd *xhci)
7f84eef0 258{
c181bc5b
EF
259 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING))
260 return;
261
7f84eef0 262 xhci_dbg(xhci, "// Ding dong!\n");
204b7793 263 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]);
7f84eef0 264 /* Flush PCI posted writes */
b0ba9720 265 readl(&xhci->dba->doorbell[0]);
7f84eef0
SS
266}
267
b92cc66c
EF
268static int xhci_abort_cmd_ring(struct xhci_hcd *xhci)
269{
270 u64 temp_64;
271 int ret;
272
273 xhci_dbg(xhci, "Abort command ring\n");
274
f7b2e403 275 temp_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
b92cc66c 276 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED;
3425aa03
MN
277
278 /*
279 * Writing the CMD_RING_ABORT bit should cause a cmd completion event,
280 * however on some host hw the CMD_RING_RUNNING bit is correctly cleared
281 * but the completion event in never sent. Use the cmd timeout timer to
282 * handle those cases. Use twice the time to cover the bit polling retry
283 */
284 mod_timer(&xhci->cmd_timer, jiffies + (2 * XHCI_CMD_DEFAULT_TIMEOUT));
477632df
SS
285 xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
286 &xhci->op_regs->cmd_ring);
b92cc66c
EF
287
288 /* Section 4.6.1.2 of xHCI 1.0 spec says software should
289 * time the completion od all xHCI commands, including
290 * the Command Abort operation. If software doesn't see
291 * CRR negated in a timely manner (e.g. longer than 5
292 * seconds), then it should assume that the there are
293 * larger problems with the xHC and assert HCRST.
294 */
dc0b177c 295 ret = xhci_handshake(&xhci->op_regs->cmd_ring,
b92cc66c
EF
296 CMD_RING_RUNNING, 0, 5 * 1000 * 1000);
297 if (ret < 0) {
a6809ffd
MN
298 /* we are about to kill xhci, give it one more chance */
299 xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
300 &xhci->op_regs->cmd_ring);
301 udelay(1000);
302 ret = xhci_handshake(&xhci->op_regs->cmd_ring,
303 CMD_RING_RUNNING, 0, 3 * 1000 * 1000);
304 if (ret == 0)
305 return 0;
306
b92cc66c
EF
307 xhci_err(xhci, "Stopped the command ring failed, "
308 "maybe the host is dead\n");
3425aa03 309 del_timer(&xhci->cmd_timer);
b92cc66c 310 xhci->xhc_state |= XHCI_STATE_DYING;
b92cc66c
EF
311 xhci_halt(xhci);
312 return -ESHUTDOWN;
313 }
314
315 return 0;
316}
317
be88fe4f 318void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
ae636747 319 unsigned int slot_id,
e9df17eb
SS
320 unsigned int ep_index,
321 unsigned int stream_id)
ae636747 322{
28ccd296 323 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
50d64676
MW
324 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
325 unsigned int ep_state = ep->ep_state;
ae636747 326
ae636747 327 /* Don't ring the doorbell for this endpoint if there are pending
50d64676 328 * cancellations because we don't want to interrupt processing.
8df75f42
SS
329 * We don't want to restart any stream rings if there's a set dequeue
330 * pointer command pending because the device can choose to start any
331 * stream once the endpoint is on the HW schedule.
ae636747 332 */
50d64676
MW
333 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
334 (ep_state & EP_HALTED))
335 return;
204b7793 336 writel(DB_VALUE(ep_index, stream_id), db_addr);
50d64676
MW
337 /* The CPU has better things to do at this point than wait for a
338 * write-posting flush. It'll get there soon enough.
339 */
ae636747
SS
340}
341
e9df17eb
SS
342/* Ring the doorbell for any rings with pending URBs */
343static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
344 unsigned int slot_id,
345 unsigned int ep_index)
346{
347 unsigned int stream_id;
348 struct xhci_virt_ep *ep;
349
350 ep = &xhci->devs[slot_id]->eps[ep_index];
351
352 /* A ring has pending URBs if its TD list is not empty */
353 if (!(ep->ep_state & EP_HAS_STREAMS)) {
d66eaf9f 354 if (ep->ring && !(list_empty(&ep->ring->td_list)))
be88fe4f 355 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
e9df17eb
SS
356 return;
357 }
358
359 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
360 stream_id++) {
361 struct xhci_stream_info *stream_info = ep->stream_info;
362 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
be88fe4f
AX
363 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
364 stream_id);
e9df17eb
SS
365 }
366}
367
75b040ec
AI
368/* Get the right ring for the given slot_id, ep_index and stream_id.
369 * If the endpoint supports streams, boundary check the URB's stream ID.
370 * If the endpoint doesn't support streams, return the singular endpoint ring.
371 */
372struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
021bff91
SS
373 unsigned int slot_id, unsigned int ep_index,
374 unsigned int stream_id)
375{
376 struct xhci_virt_ep *ep;
377
378 ep = &xhci->devs[slot_id]->eps[ep_index];
379 /* Common case: no streams */
380 if (!(ep->ep_state & EP_HAS_STREAMS))
381 return ep->ring;
382
383 if (stream_id == 0) {
384 xhci_warn(xhci,
385 "WARN: Slot ID %u, ep index %u has streams, "
386 "but URB has no stream ID.\n",
387 slot_id, ep_index);
388 return NULL;
389 }
390
391 if (stream_id < ep->stream_info->num_streams)
392 return ep->stream_info->stream_rings[stream_id];
393
394 xhci_warn(xhci,
395 "WARN: Slot ID %u, ep index %u has "
396 "stream IDs 1 to %u allocated, "
397 "but stream ID %u is requested.\n",
398 slot_id, ep_index,
399 ep->stream_info->num_streams - 1,
400 stream_id);
401 return NULL;
402}
403
ae636747
SS
404/*
405 * Move the xHC's endpoint ring dequeue pointer past cur_td.
406 * Record the new state of the xHC's endpoint ring dequeue segment,
407 * dequeue pointer, and new consumer cycle state in state.
408 * Update our internal representation of the ring's dequeue pointer.
409 *
410 * We do this in three jumps:
411 * - First we update our new ring state to be the same as when the xHC stopped.
412 * - Then we traverse the ring to find the segment that contains
413 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
414 * any link TRBs with the toggle cycle bit set.
415 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
416 * if we've moved it past a link TRB with the toggle cycle bit set.
28ccd296
ME
417 *
418 * Some of the uses of xhci_generic_trb are grotty, but if they're done
419 * with correct __le32 accesses they should work fine. Only users of this are
420 * in here.
ae636747 421 */
c92bcfa7 422void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
ae636747 423 unsigned int slot_id, unsigned int ep_index,
e9df17eb
SS
424 unsigned int stream_id, struct xhci_td *cur_td,
425 struct xhci_dequeue_state *state)
ae636747
SS
426{
427 struct xhci_virt_device *dev = xhci->devs[slot_id];
c4bedb77 428 struct xhci_virt_ep *ep = &dev->eps[ep_index];
e9df17eb 429 struct xhci_ring *ep_ring;
365038d8
MN
430 struct xhci_segment *new_seg;
431 union xhci_trb *new_deq;
c92bcfa7 432 dma_addr_t addr;
1f81b6d2 433 u64 hw_dequeue;
365038d8
MN
434 bool cycle_found = false;
435 bool td_last_trb_found = false;
ae636747 436
e9df17eb
SS
437 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
438 ep_index, stream_id);
439 if (!ep_ring) {
440 xhci_warn(xhci, "WARN can't find new dequeue state "
441 "for invalid stream ID %u.\n",
442 stream_id);
443 return;
444 }
68e41c5d 445
ae636747 446 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
aa50b290
XR
447 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
448 "Finding endpoint context");
c4bedb77
HG
449 /* 4.6.9 the css flag is written to the stream context for streams */
450 if (ep->ep_state & EP_HAS_STREAMS) {
451 struct xhci_stream_ctx *ctx =
452 &ep->stream_info->stream_ctx_array[stream_id];
1f81b6d2 453 hw_dequeue = le64_to_cpu(ctx->stream_ring);
c4bedb77
HG
454 } else {
455 struct xhci_ep_ctx *ep_ctx
456 = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
1f81b6d2 457 hw_dequeue = le64_to_cpu(ep_ctx->deq);
c4bedb77 458 }
ae636747 459
365038d8
MN
460 new_seg = ep_ring->deq_seg;
461 new_deq = ep_ring->dequeue;
462 state->new_cycle_state = hw_dequeue & 0x1;
463
1f81b6d2 464 /*
365038d8
MN
465 * We want to find the pointer, segment and cycle state of the new trb
466 * (the one after current TD's last_trb). We know the cycle state at
467 * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are
468 * found.
1f81b6d2 469 */
365038d8
MN
470 do {
471 if (!cycle_found && xhci_trb_virt_to_dma(new_seg, new_deq)
472 == (dma_addr_t)(hw_dequeue & ~0xf)) {
473 cycle_found = true;
474 if (td_last_trb_found)
475 break;
476 }
477 if (new_deq == cur_td->last_trb)
478 td_last_trb_found = true;
1f81b6d2 479
3495e451
MN
480 if (cycle_found && trb_is_link(new_deq) &&
481 link_trb_toggles_cycle(new_deq))
365038d8
MN
482 state->new_cycle_state ^= 0x1;
483
484 next_trb(xhci, ep_ring, &new_seg, &new_deq);
485
486 /* Search wrapped around, bail out */
487 if (new_deq == ep->ring->dequeue) {
488 xhci_err(xhci, "Error: Failed finding new dequeue state\n");
489 state->new_deq_seg = NULL;
490 state->new_deq_ptr = NULL;
491 return;
492 }
493
494 } while (!cycle_found || !td_last_trb_found);
ae636747 495
365038d8
MN
496 state->new_deq_seg = new_seg;
497 state->new_deq_ptr = new_deq;
ae636747 498
1f81b6d2 499 /* Don't update the ring cycle state for the producer (us). */
aa50b290
XR
500 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
501 "Cycle state = 0x%x", state->new_cycle_state);
01a1fdb9 502
aa50b290
XR
503 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
504 "New dequeue segment = %p (virtual)",
c92bcfa7
SS
505 state->new_deq_seg);
506 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
aa50b290
XR
507 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
508 "New dequeue pointer = 0x%llx (DMA)",
c92bcfa7 509 (unsigned long long) addr);
ae636747
SS
510}
511
522989a2
SS
512/* flip_cycle means flip the cycle bit of all but the first and last TRB.
513 * (The last TRB actually points to the ring enqueue pointer, which is not part
514 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
515 */
23e3be11 516static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
0d58a1a0 517 struct xhci_td *td, bool flip_cycle)
ae636747 518{
0d58a1a0
MN
519 struct xhci_segment *seg = td->start_seg;
520 union xhci_trb *trb = td->first_trb;
521
522 while (1) {
523 if (trb_is_link(trb)) {
524 /* unchain chained link TRBs */
525 trb->link.control &= cpu_to_le32(~TRB_CHAIN);
ae636747 526 } else {
0d58a1a0
MN
527 trb->generic.field[0] = 0;
528 trb->generic.field[1] = 0;
529 trb->generic.field[2] = 0;
ae636747 530 /* Preserve only the cycle bit of this TRB */
0d58a1a0
MN
531 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
532 trb->generic.field[3] |= cpu_to_le32(
28ccd296 533 TRB_TYPE(TRB_TR_NOOP));
ae636747 534 }
0d58a1a0
MN
535 /* flip cycle if asked to */
536 if (flip_cycle && trb != td->first_trb && trb != td->last_trb)
537 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE);
538
539 if (trb == td->last_trb)
ae636747 540 break;
0d58a1a0
MN
541
542 next_trb(xhci, ep_ring, &seg, &trb);
ae636747
SS
543 }
544}
545
575688e1 546static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
6f5165cf
SS
547 struct xhci_virt_ep *ep)
548{
549 ep->ep_state &= ~EP_HALT_PENDING;
550 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
551 * timer is running on another CPU, we don't decrement stop_cmds_pending
552 * (since we didn't successfully stop the watchdog timer).
553 */
554 if (del_timer(&ep->stop_cmd_timer))
555 ep->stop_cmds_pending--;
556}
557
446b3141 558
6f5165cf
SS
559/* Must be called with xhci->lock held in interrupt context */
560static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
07a37e9e 561 struct xhci_td *cur_td, int status)
6f5165cf 562{
214f76f7 563 struct usb_hcd *hcd;
8e51adcc
AX
564 struct urb *urb;
565 struct urb_priv *urb_priv;
6f5165cf 566
8e51adcc
AX
567 urb = cur_td->urb;
568 urb_priv = urb->hcpriv;
569 urb_priv->td_cnt++;
214f76f7 570 hcd = bus_to_hcd(urb->dev->bus);
6f5165cf 571
8e51adcc
AX
572 /* Only giveback urb when this is the last td in urb */
573 if (urb_priv->td_cnt == urb_priv->length) {
c41136b0
AX
574 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
575 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
576 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
577 if (xhci->quirks & XHCI_AMD_PLL_FIX)
578 usb_amd_quirk_pll_enable();
579 }
580 }
8e51adcc 581 usb_hcd_unlink_urb_from_ep(hcd, urb);
8e51adcc
AX
582
583 spin_unlock(&xhci->lock);
584 usb_hcd_giveback_urb(hcd, urb, status);
4daf9df5 585 xhci_urb_free_priv(urb_priv);
8e51adcc 586 spin_lock(&xhci->lock);
8e51adcc 587 }
6f5165cf
SS
588}
589
446b3141
MN
590/*
591 * giveback urb, must be called with xhci->lock held.
592 * releases and re-aquires xhci->lock
593 */
594static void xhci_giveback_urb_locked(struct xhci_hcd *xhci, struct xhci_td *td,
595 int status)
596{
597 struct urb *urb = td->urb;
598 struct urb_priv *urb_priv = urb->hcpriv;
599
600 xhci_urb_free_priv(urb_priv);
601
602 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
603 if ((urb->actual_length != urb->transfer_buffer_length &&
604 (urb->transfer_flags & URB_SHORT_NOT_OK)) ||
605 (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc)))
606 xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n",
607 urb, urb->actual_length,
608 urb->transfer_buffer_length, status);
609 spin_unlock(&xhci->lock);
610 /* EHCI, UHCI, and OHCI always unconditionally set the
611 * urb->status of an isochronous endpoint to 0.
612 */
613 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
614 status = 0;
615 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
616 spin_lock(&xhci->lock);
617}
618
2d6d5769
WY
619static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci,
620 struct xhci_ring *ring, struct xhci_td *td)
f9c589e1
MN
621{
622 struct device *dev = xhci_to_hcd(xhci)->self.controller;
623 struct xhci_segment *seg = td->bounce_seg;
624 struct urb *urb = td->urb;
625
626 if (!seg || !urb)
627 return;
628
629 if (usb_urb_dir_out(urb)) {
630 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
631 DMA_TO_DEVICE);
632 return;
633 }
634
635 /* for in tranfers we need to copy the data from bounce to sg */
636 sg_pcopy_from_buffer(urb->sg, urb->num_mapped_sgs, seg->bounce_buf,
637 seg->bounce_len, seg->bounce_offs);
638 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
639 DMA_FROM_DEVICE);
640 seg->bounce_len = 0;
641 seg->bounce_offs = 0;
642}
643
ae636747
SS
644/*
645 * When we get a command completion for a Stop Endpoint Command, we need to
646 * unlink any cancelled TDs from the ring. There are two ways to do that:
647 *
648 * 1. If the HW was in the middle of processing the TD that needs to be
649 * cancelled, then we must move the ring's dequeue pointer past the last TRB
650 * in the TD with a Set Dequeue Pointer Command.
651 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
652 * bit cleared) so that the HW will skip over them.
653 */
b8200c94 654static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id,
be88fe4f 655 union xhci_trb *trb, struct xhci_event_cmd *event)
ae636747 656{
ae636747
SS
657 unsigned int ep_index;
658 struct xhci_ring *ep_ring;
63a0d9ab 659 struct xhci_virt_ep *ep;
ae636747 660 struct list_head *entry;
326b4810 661 struct xhci_td *cur_td = NULL;
ae636747
SS
662 struct xhci_td *last_unlinked_td;
663
c92bcfa7 664 struct xhci_dequeue_state deq_state;
ae636747 665
bc752bde 666 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) {
9ea1833e 667 if (!xhci->devs[slot_id])
be88fe4f
AX
668 xhci_warn(xhci, "Stop endpoint command "
669 "completion for disabled slot %u\n",
670 slot_id);
671 return;
672 }
673
ae636747 674 memset(&deq_state, 0, sizeof(deq_state));
28ccd296 675 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
63a0d9ab 676 ep = &xhci->devs[slot_id]->eps[ep_index];
ae636747 677
678539cf 678 if (list_empty(&ep->cancelled_td_list)) {
6f5165cf 679 xhci_stop_watchdog_timer_in_irq(xhci, ep);
0714a57c 680 ep->stopped_td = NULL;
e9df17eb 681 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ae636747 682 return;
678539cf 683 }
ae636747
SS
684
685 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
686 * We have the xHCI lock, so nothing can modify this list until we drop
687 * it. We're also in the event handler, so we can't get re-interrupted
688 * if another Stop Endpoint command completes
689 */
63a0d9ab 690 list_for_each(entry, &ep->cancelled_td_list) {
ae636747 691 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
aa50b290
XR
692 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
693 "Removing canceled TD starting at 0x%llx (dma).",
79688acf
SS
694 (unsigned long long)xhci_trb_virt_to_dma(
695 cur_td->start_seg, cur_td->first_trb));
e9df17eb
SS
696 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
697 if (!ep_ring) {
698 /* This shouldn't happen unless a driver is mucking
699 * with the stream ID after submission. This will
700 * leave the TD on the hardware ring, and the hardware
701 * will try to execute it, and may access a buffer
702 * that has already been freed. In the best case, the
703 * hardware will execute it, and the event handler will
704 * ignore the completion event for that TD, since it was
705 * removed from the td_list for that endpoint. In
706 * short, don't muck with the stream ID after
707 * submission.
708 */
709 xhci_warn(xhci, "WARN Cancelled URB %p "
710 "has invalid stream ID %u.\n",
711 cur_td->urb,
712 cur_td->urb->stream_id);
713 goto remove_finished_td;
714 }
ae636747
SS
715 /*
716 * If we stopped on the TD we need to cancel, then we have to
717 * move the xHC endpoint ring dequeue pointer past this TD.
718 */
63a0d9ab 719 if (cur_td == ep->stopped_td)
e9df17eb
SS
720 xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
721 cur_td->urb->stream_id,
722 cur_td, &deq_state);
ae636747 723 else
522989a2 724 td_to_noop(xhci, ep_ring, cur_td, false);
e9df17eb 725remove_finished_td:
ae636747
SS
726 /*
727 * The event handler won't see a completion for this TD anymore,
728 * so remove it from the endpoint ring's TD list. Keep it in
729 * the cancelled TD list for URB completion later.
730 */
585df1d9 731 list_del_init(&cur_td->td_list);
ae636747
SS
732 }
733 last_unlinked_td = cur_td;
6f5165cf 734 xhci_stop_watchdog_timer_in_irq(xhci, ep);
ae636747
SS
735
736 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
737 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
1e3452e3
HG
738 xhci_queue_new_dequeue_state(xhci, slot_id, ep_index,
739 ep->stopped_td->urb->stream_id, &deq_state);
ac9d8fe7 740 xhci_ring_cmd_db(xhci);
ae636747 741 } else {
e9df17eb
SS
742 /* Otherwise ring the doorbell(s) to restart queued transfers */
743 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ae636747 744 }
526867c3 745
d97b4f8d 746 ep->stopped_td = NULL;
ae636747
SS
747
748 /*
749 * Drop the lock and complete the URBs in the cancelled TD list.
750 * New TDs to be cancelled might be added to the end of the list before
751 * we can complete all the URBs for the TDs we already unlinked.
752 * So stop when we've completed the URB for the last TD we unlinked.
753 */
754 do {
63a0d9ab 755 cur_td = list_entry(ep->cancelled_td_list.next,
ae636747 756 struct xhci_td, cancelled_td_list);
585df1d9 757 list_del_init(&cur_td->cancelled_td_list);
ae636747
SS
758
759 /* Clean up the cancelled URB */
ae636747
SS
760 /* Doesn't matter what we pass for status, since the core will
761 * just overwrite it (because the URB has been unlinked).
762 */
f76a28a6 763 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
f9c589e1
MN
764 if (ep_ring && cur_td->bounce_seg)
765 xhci_unmap_td_bounce_buffer(xhci, ep_ring, cur_td);
07a37e9e 766 xhci_giveback_urb_in_irq(xhci, cur_td, 0);
ae636747 767
6f5165cf
SS
768 /* Stop processing the cancelled list if the watchdog timer is
769 * running.
770 */
771 if (xhci->xhc_state & XHCI_STATE_DYING)
772 return;
ae636747
SS
773 } while (cur_td != last_unlinked_td);
774
775 /* Return to the event handler with xhci->lock re-acquired */
776}
777
50e8725e
SS
778static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring)
779{
780 struct xhci_td *cur_td;
781
782 while (!list_empty(&ring->td_list)) {
783 cur_td = list_first_entry(&ring->td_list,
784 struct xhci_td, td_list);
785 list_del_init(&cur_td->td_list);
786 if (!list_empty(&cur_td->cancelled_td_list))
787 list_del_init(&cur_td->cancelled_td_list);
f9c589e1
MN
788
789 if (cur_td->bounce_seg)
790 xhci_unmap_td_bounce_buffer(xhci, ring, cur_td);
50e8725e
SS
791 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
792 }
793}
794
795static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci,
796 int slot_id, int ep_index)
797{
798 struct xhci_td *cur_td;
799 struct xhci_virt_ep *ep;
800 struct xhci_ring *ring;
801
802 ep = &xhci->devs[slot_id]->eps[ep_index];
21d0e51b
SS
803 if ((ep->ep_state & EP_HAS_STREAMS) ||
804 (ep->ep_state & EP_GETTING_NO_STREAMS)) {
805 int stream_id;
806
807 for (stream_id = 0; stream_id < ep->stream_info->num_streams;
808 stream_id++) {
809 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
810 "Killing URBs for slot ID %u, ep index %u, stream %u",
811 slot_id, ep_index, stream_id + 1);
812 xhci_kill_ring_urbs(xhci,
813 ep->stream_info->stream_rings[stream_id]);
814 }
815 } else {
816 ring = ep->ring;
817 if (!ring)
818 return;
819 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
820 "Killing URBs for slot ID %u, ep index %u",
821 slot_id, ep_index);
822 xhci_kill_ring_urbs(xhci, ring);
823 }
50e8725e
SS
824 while (!list_empty(&ep->cancelled_td_list)) {
825 cur_td = list_first_entry(&ep->cancelled_td_list,
826 struct xhci_td, cancelled_td_list);
827 list_del_init(&cur_td->cancelled_td_list);
828 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
829 }
830}
831
6f5165cf
SS
832/* Watchdog timer function for when a stop endpoint command fails to complete.
833 * In this case, we assume the host controller is broken or dying or dead. The
834 * host may still be completing some other events, so we have to be careful to
835 * let the event ring handler and the URB dequeueing/enqueueing functions know
836 * through xhci->state.
837 *
838 * The timer may also fire if the host takes a very long time to respond to the
839 * command, and the stop endpoint command completion handler cannot delete the
840 * timer before the timer function is called. Another endpoint cancellation may
841 * sneak in before the timer function can grab the lock, and that may queue
842 * another stop endpoint command and add the timer back. So we cannot use a
843 * simple flag to say whether there is a pending stop endpoint command for a
844 * particular endpoint.
845 *
846 * Instead we use a combination of that flag and a counter for the number of
847 * pending stop endpoint commands. If the timer is the tail end of the last
848 * stop endpoint command, and the endpoint's command is still pending, we assume
849 * the host is dying.
850 */
851void xhci_stop_endpoint_command_watchdog(unsigned long arg)
852{
853 struct xhci_hcd *xhci;
854 struct xhci_virt_ep *ep;
6f5165cf 855 int ret, i, j;
f43d6231 856 unsigned long flags;
6f5165cf
SS
857
858 ep = (struct xhci_virt_ep *) arg;
859 xhci = ep->xhci;
860
f43d6231 861 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
862
863 ep->stop_cmds_pending--;
bcf42aa6
MN
864 if (xhci->xhc_state & XHCI_STATE_REMOVING) {
865 spin_unlock_irqrestore(&xhci->lock, flags);
866 return;
867 }
6f5165cf 868 if (xhci->xhc_state & XHCI_STATE_DYING) {
aa50b290
XR
869 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
870 "Stop EP timer ran, but another timer marked "
871 "xHCI as DYING, exiting.");
f43d6231 872 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf
SS
873 return;
874 }
875 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
aa50b290
XR
876 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
877 "Stop EP timer ran, but no command pending, "
878 "exiting.");
f43d6231 879 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf
SS
880 return;
881 }
882
883 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
884 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
885 /* Oops, HC is dead or dying or at least not responding to the stop
886 * endpoint command.
887 */
888 xhci->xhc_state |= XHCI_STATE_DYING;
889 /* Disable interrupts from the host controller and start halting it */
890 xhci_quiesce(xhci);
f43d6231 891 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf
SS
892
893 ret = xhci_halt(xhci);
894
f43d6231 895 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
896 if (ret < 0) {
897 /* This is bad; the host is not responding to commands and it's
898 * not allowing itself to be halted. At least interrupts are
ac04e6ff 899 * disabled. If we call usb_hc_died(), it will attempt to
6f5165cf
SS
900 * disconnect all device drivers under this host. Those
901 * disconnect() methods will wait for all URBs to be unlinked,
902 * so we must complete them.
903 */
904 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
905 xhci_warn(xhci, "Completing active URBs anyway.\n");
906 /* We could turn all TDs on the rings to no-ops. This won't
907 * help if the host has cached part of the ring, and is slow if
908 * we want to preserve the cycle bit. Skip it and hope the host
909 * doesn't touch the memory.
910 */
911 }
912 for (i = 0; i < MAX_HC_SLOTS; i++) {
913 if (!xhci->devs[i])
914 continue;
50e8725e
SS
915 for (j = 0; j < 31; j++)
916 xhci_kill_endpoint_urbs(xhci, i, j);
6f5165cf 917 }
f43d6231 918 spin_unlock_irqrestore(&xhci->lock, flags);
aa50b290
XR
919 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
920 "Calling usb_hc_died()");
bcf42aa6 921 usb_hc_died(xhci_to_hcd(xhci));
aa50b290
XR
922 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
923 "xHCI host controller is dead.");
6f5165cf
SS
924}
925
b008df60
AX
926
927static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci,
928 struct xhci_virt_device *dev,
929 struct xhci_ring *ep_ring,
930 unsigned int ep_index)
931{
932 union xhci_trb *dequeue_temp;
933 int num_trbs_free_temp;
934 bool revert = false;
935
936 num_trbs_free_temp = ep_ring->num_trbs_free;
937 dequeue_temp = ep_ring->dequeue;
938
0d9f78a9
SS
939 /* If we get two back-to-back stalls, and the first stalled transfer
940 * ends just before a link TRB, the dequeue pointer will be left on
941 * the link TRB by the code in the while loop. So we have to update
942 * the dequeue pointer one segment further, or we'll jump off
943 * the segment into la-la-land.
944 */
2d98ef40 945 if (trb_is_link(ep_ring->dequeue)) {
0d9f78a9
SS
946 ep_ring->deq_seg = ep_ring->deq_seg->next;
947 ep_ring->dequeue = ep_ring->deq_seg->trbs;
948 }
949
b008df60
AX
950 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) {
951 /* We have more usable TRBs */
952 ep_ring->num_trbs_free++;
953 ep_ring->dequeue++;
2d98ef40 954 if (trb_is_link(ep_ring->dequeue)) {
b008df60
AX
955 if (ep_ring->dequeue ==
956 dev->eps[ep_index].queued_deq_ptr)
957 break;
958 ep_ring->deq_seg = ep_ring->deq_seg->next;
959 ep_ring->dequeue = ep_ring->deq_seg->trbs;
960 }
961 if (ep_ring->dequeue == dequeue_temp) {
962 revert = true;
963 break;
964 }
965 }
966
967 if (revert) {
968 xhci_dbg(xhci, "Unable to find new dequeue pointer\n");
969 ep_ring->num_trbs_free = num_trbs_free_temp;
970 }
971}
972
ae636747
SS
973/*
974 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
975 * we need to clear the set deq pending flag in the endpoint ring state, so that
976 * the TD queueing code can ring the doorbell again. We also need to ring the
977 * endpoint doorbell to restart the ring, but only if there aren't more
978 * cancellations pending.
979 */
b8200c94 980static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
c69a0597 981 union xhci_trb *trb, u32 cmd_comp_code)
ae636747 982{
ae636747 983 unsigned int ep_index;
e9df17eb 984 unsigned int stream_id;
ae636747
SS
985 struct xhci_ring *ep_ring;
986 struct xhci_virt_device *dev;
9aad95e2 987 struct xhci_virt_ep *ep;
d115b048
JY
988 struct xhci_ep_ctx *ep_ctx;
989 struct xhci_slot_ctx *slot_ctx;
ae636747 990
28ccd296
ME
991 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
992 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
ae636747 993 dev = xhci->devs[slot_id];
9aad95e2 994 ep = &dev->eps[ep_index];
e9df17eb
SS
995
996 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
997 if (!ep_ring) {
e587b8b2 998 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n",
e9df17eb
SS
999 stream_id);
1000 /* XXX: Harmless??? */
0d4976ec 1001 goto cleanup;
e9df17eb
SS
1002 }
1003
d115b048
JY
1004 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
1005 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
ae636747 1006
c69a0597 1007 if (cmd_comp_code != COMP_SUCCESS) {
ae636747
SS
1008 unsigned int ep_state;
1009 unsigned int slot_state;
1010
c69a0597 1011 switch (cmd_comp_code) {
ae636747 1012 case COMP_TRB_ERR:
e587b8b2 1013 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n");
ae636747
SS
1014 break;
1015 case COMP_CTX_STATE:
e587b8b2 1016 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n");
28ccd296 1017 ep_state = le32_to_cpu(ep_ctx->ep_info);
ae636747 1018 ep_state &= EP_STATE_MASK;
28ccd296 1019 slot_state = le32_to_cpu(slot_ctx->dev_state);
ae636747 1020 slot_state = GET_SLOT_STATE(slot_state);
aa50b290
XR
1021 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1022 "Slot state = %u, EP state = %u",
ae636747
SS
1023 slot_state, ep_state);
1024 break;
1025 case COMP_EBADSLT:
e587b8b2
ON
1026 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n",
1027 slot_id);
ae636747
SS
1028 break;
1029 default:
e587b8b2
ON
1030 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n",
1031 cmd_comp_code);
ae636747
SS
1032 break;
1033 }
1034 /* OK what do we do now? The endpoint state is hosed, and we
1035 * should never get to this point if the synchronization between
1036 * queueing, and endpoint state are correct. This might happen
1037 * if the device gets disconnected after we've finished
1038 * cancelling URBs, which might not be an error...
1039 */
1040 } else {
9aad95e2
HG
1041 u64 deq;
1042 /* 4.6.10 deq ptr is written to the stream ctx for streams */
1043 if (ep->ep_state & EP_HAS_STREAMS) {
1044 struct xhci_stream_ctx *ctx =
1045 &ep->stream_info->stream_ctx_array[stream_id];
1046 deq = le64_to_cpu(ctx->stream_ring) & SCTX_DEQ_MASK;
1047 } else {
1048 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK;
1049 }
aa50b290 1050 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
9aad95e2
HG
1051 "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
1052 if (xhci_trb_virt_to_dma(ep->queued_deq_seg,
1053 ep->queued_deq_ptr) == deq) {
bf161e85
SS
1054 /* Update the ring's dequeue segment and dequeue pointer
1055 * to reflect the new position.
1056 */
b008df60
AX
1057 update_ring_for_set_deq_completion(xhci, dev,
1058 ep_ring, ep_index);
bf161e85 1059 } else {
e587b8b2 1060 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n");
bf161e85 1061 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
9aad95e2 1062 ep->queued_deq_seg, ep->queued_deq_ptr);
bf161e85 1063 }
ae636747
SS
1064 }
1065
0d4976ec 1066cleanup:
63a0d9ab 1067 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
bf161e85
SS
1068 dev->eps[ep_index].queued_deq_seg = NULL;
1069 dev->eps[ep_index].queued_deq_ptr = NULL;
e9df17eb
SS
1070 /* Restart any rings with pending URBs */
1071 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ae636747
SS
1072}
1073
b8200c94 1074static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id,
c69a0597 1075 union xhci_trb *trb, u32 cmd_comp_code)
a1587d97 1076{
a1587d97
SS
1077 unsigned int ep_index;
1078
28ccd296 1079 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
a1587d97
SS
1080 /* This command will only fail if the endpoint wasn't halted,
1081 * but we don't care.
1082 */
a0254324 1083 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
c69a0597 1084 "Ignoring reset ep completion code of %u", cmd_comp_code);
a1587d97 1085
ac9d8fe7
SS
1086 /* HW with the reset endpoint quirk needs to have a configure endpoint
1087 * command complete before the endpoint can be used. Queue that here
1088 * because the HW can't handle two commands being queued in a row.
1089 */
1090 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
ddba5cd0
MN
1091 struct xhci_command *command;
1092 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
a0ee619f
HG
1093 if (!command) {
1094 xhci_warn(xhci, "WARN Cannot submit cfg ep: ENOMEM\n");
1095 return;
1096 }
4bdfe4c3
XR
1097 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1098 "Queueing configure endpoint command");
ddba5cd0 1099 xhci_queue_configure_endpoint(xhci, command,
913a8a34
SS
1100 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1101 false);
ac9d8fe7
SS
1102 xhci_ring_cmd_db(xhci);
1103 } else {
c3492dbf 1104 /* Clear our internal halted state */
63a0d9ab 1105 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
ac9d8fe7 1106 }
a1587d97 1107}
ae636747 1108
b244b431
XR
1109static void xhci_handle_cmd_enable_slot(struct xhci_hcd *xhci, int slot_id,
1110 u32 cmd_comp_code)
1111{
1112 if (cmd_comp_code == COMP_SUCCESS)
1113 xhci->slot_id = slot_id;
1114 else
1115 xhci->slot_id = 0;
b244b431
XR
1116}
1117
6c02dd14
XR
1118static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id)
1119{
1120 struct xhci_virt_device *virt_dev;
1121
1122 virt_dev = xhci->devs[slot_id];
1123 if (!virt_dev)
1124 return;
1125 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1126 /* Delete default control endpoint resources */
1127 xhci_free_device_endpoint_resources(xhci, virt_dev, true);
1128 xhci_free_virt_device(xhci, slot_id);
1129}
1130
6ed46d33
XR
1131static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id,
1132 struct xhci_event_cmd *event, u32 cmd_comp_code)
1133{
1134 struct xhci_virt_device *virt_dev;
1135 struct xhci_input_control_ctx *ctrl_ctx;
1136 unsigned int ep_index;
1137 unsigned int ep_state;
1138 u32 add_flags, drop_flags;
1139
6ed46d33
XR
1140 /*
1141 * Configure endpoint commands can come from the USB core
1142 * configuration or alt setting changes, or because the HW
1143 * needed an extra configure endpoint command after a reset
1144 * endpoint command or streams were being configured.
1145 * If the command was for a halted endpoint, the xHCI driver
1146 * is not waiting on the configure endpoint command.
1147 */
9ea1833e 1148 virt_dev = xhci->devs[slot_id];
4daf9df5 1149 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
6ed46d33
XR
1150 if (!ctrl_ctx) {
1151 xhci_warn(xhci, "Could not get input context, bad type.\n");
1152 return;
1153 }
1154
1155 add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1156 drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1157 /* Input ctx add_flags are the endpoint index plus one */
1158 ep_index = xhci_last_valid_endpoint(add_flags) - 1;
1159
1160 /* A usb_set_interface() call directly after clearing a halted
1161 * condition may race on this quirky hardware. Not worth
1162 * worrying about, since this is prototype hardware. Not sure
1163 * if this will work for streams, but streams support was
1164 * untested on this prototype.
1165 */
1166 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
1167 ep_index != (unsigned int) -1 &&
1168 add_flags - SLOT_FLAG == drop_flags) {
1169 ep_state = virt_dev->eps[ep_index].ep_state;
1170 if (!(ep_state & EP_HALTED))
ddba5cd0 1171 return;
6ed46d33
XR
1172 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1173 "Completed config ep cmd - "
1174 "last ep index = %d, state = %d",
1175 ep_index, ep_state);
1176 /* Clear internal halted state and restart ring(s) */
1177 virt_dev->eps[ep_index].ep_state &= ~EP_HALTED;
1178 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1179 return;
1180 }
6ed46d33
XR
1181 return;
1182}
1183
f681321b
XR
1184static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id,
1185 struct xhci_event_cmd *event)
1186{
f681321b 1187 xhci_dbg(xhci, "Completed reset device command.\n");
9ea1833e 1188 if (!xhci->devs[slot_id])
f681321b
XR
1189 xhci_warn(xhci, "Reset device command completion "
1190 "for disabled slot %u\n", slot_id);
1191}
1192
2c070821
XR
1193static void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci,
1194 struct xhci_event_cmd *event)
1195{
1196 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1197 xhci->error_bitmask |= 1 << 6;
1198 return;
1199 }
1200 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1201 "NEC firmware version %2x.%02x",
1202 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1203 NEC_FW_MINOR(le32_to_cpu(event->status)));
1204}
1205
9ea1833e 1206static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 status)
c9aa1a2d
MN
1207{
1208 list_del(&cmd->cmd_list);
9ea1833e
MN
1209
1210 if (cmd->completion) {
1211 cmd->status = status;
1212 complete(cmd->completion);
1213 } else {
c9aa1a2d 1214 kfree(cmd);
9ea1833e 1215 }
c9aa1a2d
MN
1216}
1217
1218void xhci_cleanup_command_queue(struct xhci_hcd *xhci)
1219{
1220 struct xhci_command *cur_cmd, *tmp_cmd;
1221 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list)
9ea1833e 1222 xhci_complete_del_and_free_cmd(cur_cmd, COMP_CMD_ABORT);
c9aa1a2d
MN
1223}
1224
c311e391
MN
1225/*
1226 * Turn all commands on command ring with status set to "aborted" to no-op trbs.
1227 * If there are other commands waiting then restart the ring and kick the timer.
1228 * This must be called with command ring stopped and xhci->lock held.
1229 */
1230static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci,
1231 struct xhci_command *cur_cmd)
1232{
1233 struct xhci_command *i_cmd, *tmp_cmd;
1234 u32 cycle_state;
1235
1236 /* Turn all aborted commands in list to no-ops, then restart */
1237 list_for_each_entry_safe(i_cmd, tmp_cmd, &xhci->cmd_list,
1238 cmd_list) {
1239
1240 if (i_cmd->status != COMP_CMD_ABORT)
1241 continue;
1242
1243 i_cmd->status = COMP_CMD_STOP;
1244
1245 xhci_dbg(xhci, "Turn aborted command %p to no-op\n",
1246 i_cmd->command_trb);
1247 /* get cycle state from the original cmd trb */
1248 cycle_state = le32_to_cpu(
1249 i_cmd->command_trb->generic.field[3]) & TRB_CYCLE;
1250 /* modify the command trb to no-op command */
1251 i_cmd->command_trb->generic.field[0] = 0;
1252 i_cmd->command_trb->generic.field[1] = 0;
1253 i_cmd->command_trb->generic.field[2] = 0;
1254 i_cmd->command_trb->generic.field[3] = cpu_to_le32(
1255 TRB_TYPE(TRB_CMD_NOOP) | cycle_state);
1256
1257 /*
1258 * caller waiting for completion is called when command
1259 * completion event is received for these no-op commands
1260 */
1261 }
1262
1263 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
1264
1265 /* ring command ring doorbell to restart the command ring */
1266 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) &&
1267 !(xhci->xhc_state & XHCI_STATE_DYING)) {
1268 xhci->current_cmd = cur_cmd;
1269 mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
1270 xhci_ring_cmd_db(xhci);
1271 }
1272 return;
1273}
1274
1275
1276void xhci_handle_command_timeout(unsigned long data)
1277{
1278 struct xhci_hcd *xhci;
1279 int ret;
1280 unsigned long flags;
1281 u64 hw_ring_state;
3425aa03 1282 bool second_timeout = false;
c311e391
MN
1283 xhci = (struct xhci_hcd *) data;
1284
1285 /* mark this command to be cancelled */
1286 spin_lock_irqsave(&xhci->lock, flags);
1287 if (xhci->current_cmd) {
3425aa03
MN
1288 if (xhci->current_cmd->status == COMP_CMD_ABORT)
1289 second_timeout = true;
1290 xhci->current_cmd->status = COMP_CMD_ABORT;
c311e391
MN
1291 }
1292
c311e391
MN
1293 /* Make sure command ring is running before aborting it */
1294 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
1295 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
1296 (hw_ring_state & CMD_RING_RUNNING)) {
c311e391
MN
1297 spin_unlock_irqrestore(&xhci->lock, flags);
1298 xhci_dbg(xhci, "Command timeout\n");
1299 ret = xhci_abort_cmd_ring(xhci);
1300 if (unlikely(ret == -ESHUTDOWN)) {
1301 xhci_err(xhci, "Abort command ring failed\n");
1302 xhci_cleanup_command_queue(xhci);
1303 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
1304 xhci_dbg(xhci, "xHCI host controller is dead.\n");
1305 }
1306 return;
1307 }
3425aa03
MN
1308
1309 /* command ring failed to restart, or host removed. Bail out */
1310 if (second_timeout || xhci->xhc_state & XHCI_STATE_REMOVING) {
1311 spin_unlock_irqrestore(&xhci->lock, flags);
1312 xhci_dbg(xhci, "command timed out twice, ring start fail?\n");
1313 xhci_cleanup_command_queue(xhci);
1314 return;
1315 }
1316
c311e391
MN
1317 /* command timeout on stopped ring, ring can't be aborted */
1318 xhci_dbg(xhci, "Command timeout on stopped ring\n");
1319 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd);
1320 spin_unlock_irqrestore(&xhci->lock, flags);
1321 return;
1322}
1323
7f84eef0
SS
1324static void handle_cmd_completion(struct xhci_hcd *xhci,
1325 struct xhci_event_cmd *event)
1326{
28ccd296 1327 int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
7f84eef0
SS
1328 u64 cmd_dma;
1329 dma_addr_t cmd_dequeue_dma;
e7a79a1d 1330 u32 cmd_comp_code;
9124b121 1331 union xhci_trb *cmd_trb;
c9aa1a2d 1332 struct xhci_command *cmd;
b54fc46d 1333 u32 cmd_type;
7f84eef0 1334
28ccd296 1335 cmd_dma = le64_to_cpu(event->cmd_trb);
9124b121 1336 cmd_trb = xhci->cmd_ring->dequeue;
23e3be11 1337 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
9124b121 1338 cmd_trb);
7f84eef0
SS
1339 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1340 if (cmd_dequeue_dma == 0) {
1341 xhci->error_bitmask |= 1 << 4;
1342 return;
1343 }
1344 /* Does the DMA address match our internal dequeue pointer address? */
1345 if (cmd_dma != (u64) cmd_dequeue_dma) {
1346 xhci->error_bitmask |= 1 << 5;
1347 return;
1348 }
b63f4053 1349
c9aa1a2d
MN
1350 cmd = list_entry(xhci->cmd_list.next, struct xhci_command, cmd_list);
1351
c311e391
MN
1352 del_timer(&xhci->cmd_timer);
1353
9124b121 1354 trace_xhci_cmd_completion(cmd_trb, (struct xhci_generic_trb *) event);
63a23b9a 1355
e7a79a1d 1356 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status));
c311e391
MN
1357
1358 /* If CMD ring stopped we own the trbs between enqueue and dequeue */
1359 if (cmd_comp_code == COMP_CMD_STOP) {
1360 xhci_handle_stopped_cmd_ring(xhci, cmd);
1361 return;
1362 }
33be1265
MN
1363
1364 if (cmd->command_trb != xhci->cmd_ring->dequeue) {
1365 xhci_err(xhci,
1366 "Command completion event does not match command\n");
1367 return;
1368 }
1369
c311e391
MN
1370 /*
1371 * Host aborted the command ring, check if the current command was
1372 * supposed to be aborted, otherwise continue normally.
1373 * The command ring is stopped now, but the xHC will issue a Command
1374 * Ring Stopped event which will cause us to restart it.
1375 */
1376 if (cmd_comp_code == COMP_CMD_ABORT) {
1377 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
1378 if (cmd->status == COMP_CMD_ABORT)
1379 goto event_handled;
b63f4053
EF
1380 }
1381
b54fc46d
XR
1382 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3]));
1383 switch (cmd_type) {
1384 case TRB_ENABLE_SLOT:
e7a79a1d 1385 xhci_handle_cmd_enable_slot(xhci, slot_id, cmd_comp_code);
3ffbba95 1386 break;
b54fc46d 1387 case TRB_DISABLE_SLOT:
6c02dd14 1388 xhci_handle_cmd_disable_slot(xhci, slot_id);
3ffbba95 1389 break;
b54fc46d 1390 case TRB_CONFIG_EP:
9ea1833e
MN
1391 if (!cmd->completion)
1392 xhci_handle_cmd_config_ep(xhci, slot_id, event,
1393 cmd_comp_code);
f94e0186 1394 break;
b54fc46d 1395 case TRB_EVAL_CONTEXT:
2d3f1fac 1396 break;
b54fc46d 1397 case TRB_ADDR_DEV:
3ffbba95 1398 break;
b54fc46d 1399 case TRB_STOP_RING:
b8200c94
XR
1400 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1401 le32_to_cpu(cmd_trb->generic.field[3])));
1402 xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb, event);
ae636747 1403 break;
b54fc46d 1404 case TRB_SET_DEQ:
b8200c94
XR
1405 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1406 le32_to_cpu(cmd_trb->generic.field[3])));
c69a0597 1407 xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code);
ae636747 1408 break;
b54fc46d 1409 case TRB_CMD_NOOP:
c311e391
MN
1410 /* Is this an aborted command turned to NO-OP? */
1411 if (cmd->status == COMP_CMD_STOP)
1412 cmd_comp_code = COMP_CMD_STOP;
7f84eef0 1413 break;
b54fc46d 1414 case TRB_RESET_EP:
b8200c94
XR
1415 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1416 le32_to_cpu(cmd_trb->generic.field[3])));
c69a0597 1417 xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code);
a1587d97 1418 break;
b54fc46d 1419 case TRB_RESET_DEV:
6fcfb0d6
MN
1420 /* SLOT_ID field in reset device cmd completion event TRB is 0.
1421 * Use the SLOT_ID from the command TRB instead (xhci 4.6.11)
1422 */
1423 slot_id = TRB_TO_SLOT_ID(
1424 le32_to_cpu(cmd_trb->generic.field[3]));
f681321b 1425 xhci_handle_cmd_reset_dev(xhci, slot_id, event);
2a8f82c4 1426 break;
b54fc46d 1427 case TRB_NEC_GET_FW:
2c070821 1428 xhci_handle_cmd_nec_get_fw(xhci, event);
0238634d 1429 break;
7f84eef0
SS
1430 default:
1431 /* Skip over unknown commands on the event ring */
1432 xhci->error_bitmask |= 1 << 6;
1433 break;
1434 }
c9aa1a2d 1435
c311e391
MN
1436 /* restart timer if this wasn't the last command */
1437 if (cmd->cmd_list.next != &xhci->cmd_list) {
1438 xhci->current_cmd = list_entry(cmd->cmd_list.next,
1439 struct xhci_command, cmd_list);
1440 mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
1441 }
1442
1443event_handled:
9ea1833e 1444 xhci_complete_del_and_free_cmd(cmd, cmd_comp_code);
c9aa1a2d 1445
3b72fca0 1446 inc_deq(xhci, xhci->cmd_ring);
7f84eef0
SS
1447}
1448
0238634d
SS
1449static void handle_vendor_event(struct xhci_hcd *xhci,
1450 union xhci_trb *event)
1451{
1452 u32 trb_type;
1453
28ccd296 1454 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
0238634d
SS
1455 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1456 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1457 handle_cmd_completion(xhci, &event->event_cmd);
1458}
1459
f6ff0ac8
SS
1460/* @port_id: the one-based port ID from the hardware (indexed from array of all
1461 * port registers -- USB 3.0 and USB 2.0).
1462 *
1463 * Returns a zero-based port number, which is suitable for indexing into each of
1464 * the split roothubs' port arrays and bus state arrays.
d0cd5d48 1465 * Add one to it in order to call xhci_find_slot_id_by_port.
f6ff0ac8
SS
1466 */
1467static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1468 struct xhci_hcd *xhci, u32 port_id)
1469{
1470 unsigned int i;
1471 unsigned int num_similar_speed_ports = 0;
1472
1473 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1474 * and usb2_ports are 0-based indexes. Count the number of similar
1475 * speed ports, up to 1 port before this port.
1476 */
1477 for (i = 0; i < (port_id - 1); i++) {
1478 u8 port_speed = xhci->port_array[i];
1479
1480 /*
1481 * Skip ports that don't have known speeds, or have duplicate
1482 * Extended Capabilities port speed entries.
1483 */
22e04870 1484 if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
f6ff0ac8
SS
1485 continue;
1486
1487 /*
1488 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1489 * 1.1 ports are under the USB 2.0 hub. If the port speed
1490 * matches the device speed, it's a similar speed port.
1491 */
b50107bb 1492 if ((port_speed == 0x03) == (hcd->speed >= HCD_USB3))
f6ff0ac8
SS
1493 num_similar_speed_ports++;
1494 }
1495 return num_similar_speed_ports;
1496}
1497
623bef9e
SS
1498static void handle_device_notification(struct xhci_hcd *xhci,
1499 union xhci_trb *event)
1500{
1501 u32 slot_id;
4ee823b8 1502 struct usb_device *udev;
623bef9e 1503
7e76ad43 1504 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3]));
4ee823b8 1505 if (!xhci->devs[slot_id]) {
623bef9e
SS
1506 xhci_warn(xhci, "Device Notification event for "
1507 "unused slot %u\n", slot_id);
4ee823b8
SS
1508 return;
1509 }
1510
1511 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
1512 slot_id);
1513 udev = xhci->devs[slot_id]->udev;
1514 if (udev && udev->parent)
1515 usb_wakeup_notification(udev->parent, udev->portnum);
623bef9e
SS
1516}
1517
0f2a7930
SS
1518static void handle_port_status(struct xhci_hcd *xhci,
1519 union xhci_trb *event)
1520{
f6ff0ac8 1521 struct usb_hcd *hcd;
0f2a7930 1522 u32 port_id;
56192531 1523 u32 temp, temp1;
518e848e 1524 int max_ports;
56192531 1525 int slot_id;
5308a91b 1526 unsigned int faked_port_index;
f6ff0ac8 1527 u8 major_revision;
20b67cf5 1528 struct xhci_bus_state *bus_state;
28ccd296 1529 __le32 __iomem **port_array;
386139d7 1530 bool bogus_port_status = false;
0f2a7930
SS
1531
1532 /* Port status change events always have a successful completion code */
28ccd296 1533 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
0f2a7930
SS
1534 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1535 xhci->error_bitmask |= 1 << 8;
1536 }
28ccd296 1537 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
0f2a7930
SS
1538 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1539
518e848e
SS
1540 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1541 if ((port_id <= 0) || (port_id > max_ports)) {
56192531 1542 xhci_warn(xhci, "Invalid port id %d\n", port_id);
09ce0c0c
PC
1543 inc_deq(xhci, xhci->event_ring);
1544 return;
56192531
AX
1545 }
1546
f6ff0ac8
SS
1547 /* Figure out which usb_hcd this port is attached to:
1548 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1549 */
1550 major_revision = xhci->port_array[port_id - 1];
09ce0c0c
PC
1551
1552 /* Find the right roothub. */
1553 hcd = xhci_to_hcd(xhci);
b50107bb 1554 if ((major_revision == 0x03) != (hcd->speed >= HCD_USB3))
09ce0c0c
PC
1555 hcd = xhci->shared_hcd;
1556
f6ff0ac8
SS
1557 if (major_revision == 0) {
1558 xhci_warn(xhci, "Event for port %u not in "
1559 "Extended Capabilities, ignoring.\n",
1560 port_id);
386139d7 1561 bogus_port_status = true;
f6ff0ac8 1562 goto cleanup;
5308a91b 1563 }
22e04870 1564 if (major_revision == DUPLICATE_ENTRY) {
f6ff0ac8
SS
1565 xhci_warn(xhci, "Event for port %u duplicated in"
1566 "Extended Capabilities, ignoring.\n",
1567 port_id);
386139d7 1568 bogus_port_status = true;
f6ff0ac8
SS
1569 goto cleanup;
1570 }
1571
1572 /*
1573 * Hardware port IDs reported by a Port Status Change Event include USB
1574 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1575 * resume event, but we first need to translate the hardware port ID
1576 * into the index into the ports on the correct split roothub, and the
1577 * correct bus_state structure.
1578 */
f6ff0ac8 1579 bus_state = &xhci->bus_state[hcd_index(hcd)];
b50107bb 1580 if (hcd->speed >= HCD_USB3)
f6ff0ac8
SS
1581 port_array = xhci->usb3_ports;
1582 else
1583 port_array = xhci->usb2_ports;
1584 /* Find the faked port hub number */
1585 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1586 port_id);
5308a91b 1587
b0ba9720 1588 temp = readl(port_array[faked_port_index]);
7111ebc9 1589 if (hcd->state == HC_STATE_SUSPENDED) {
56192531
AX
1590 xhci_dbg(xhci, "resume root hub\n");
1591 usb_hcd_resume_root_hub(hcd);
1592 }
1593
b50107bb 1594 if (hcd->speed >= HCD_USB3 && (temp & PORT_PLS_MASK) == XDEV_INACTIVE)
fac4271d
ZJC
1595 bus_state->port_remote_wakeup &= ~(1 << faked_port_index);
1596
56192531
AX
1597 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1598 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1599
b0ba9720 1600 temp1 = readl(&xhci->op_regs->command);
56192531
AX
1601 if (!(temp1 & CMD_RUN)) {
1602 xhci_warn(xhci, "xHC is not running.\n");
1603 goto cleanup;
1604 }
1605
2338b9e4 1606 if (DEV_SUPERSPEED_ANY(temp)) {
d93814cf 1607 xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
4ee823b8
SS
1608 /* Set a flag to say the port signaled remote wakeup,
1609 * so we can tell the difference between the end of
1610 * device and host initiated resume.
1611 */
1612 bus_state->port_remote_wakeup |= 1 << faked_port_index;
d93814cf
SS
1613 xhci_test_and_clear_bit(xhci, port_array,
1614 faked_port_index, PORT_PLC);
c9682dff
AX
1615 xhci_set_link_state(xhci, port_array, faked_port_index,
1616 XDEV_U0);
d93814cf
SS
1617 /* Need to wait until the next link state change
1618 * indicates the device is actually in U0.
1619 */
1620 bogus_port_status = true;
1621 goto cleanup;
f69115fd
MN
1622 } else if (!test_bit(faked_port_index,
1623 &bus_state->resuming_ports)) {
56192531 1624 xhci_dbg(xhci, "resume HS port %d\n", port_id);
f6ff0ac8 1625 bus_state->resume_done[faked_port_index] = jiffies +
b9e45188 1626 msecs_to_jiffies(USB_RESUME_TIMEOUT);
f370b996 1627 set_bit(faked_port_index, &bus_state->resuming_ports);
56192531 1628 mod_timer(&hcd->rh_timer,
f6ff0ac8 1629 bus_state->resume_done[faked_port_index]);
56192531
AX
1630 /* Do the rest in GetPortStatus */
1631 }
1632 }
d93814cf
SS
1633
1634 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_U0 &&
2338b9e4 1635 DEV_SUPERSPEED_ANY(temp)) {
d93814cf 1636 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
4ee823b8
SS
1637 /* We've just brought the device into U0 through either the
1638 * Resume state after a device remote wakeup, or through the
1639 * U3Exit state after a host-initiated resume. If it's a device
1640 * initiated remote wake, don't pass up the link state change,
1641 * so the roothub behavior is consistent with external
1642 * USB 3.0 hub behavior.
1643 */
d93814cf
SS
1644 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1645 faked_port_index + 1);
1646 if (slot_id && xhci->devs[slot_id])
1647 xhci_ring_device(xhci, slot_id);
ba7b5c22 1648 if (bus_state->port_remote_wakeup & (1 << faked_port_index)) {
4ee823b8
SS
1649 bus_state->port_remote_wakeup &=
1650 ~(1 << faked_port_index);
1651 xhci_test_and_clear_bit(xhci, port_array,
1652 faked_port_index, PORT_PLC);
1653 usb_wakeup_notification(hcd->self.root_hub,
1654 faked_port_index + 1);
1655 bogus_port_status = true;
1656 goto cleanup;
1657 }
d93814cf 1658 }
56192531 1659
8b3d4570
SS
1660 /*
1661 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or
1662 * RExit to a disconnect state). If so, let the the driver know it's
1663 * out of the RExit state.
1664 */
2338b9e4 1665 if (!DEV_SUPERSPEED_ANY(temp) &&
8b3d4570
SS
1666 test_and_clear_bit(faked_port_index,
1667 &bus_state->rexit_ports)) {
1668 complete(&bus_state->rexit_done[faked_port_index]);
1669 bogus_port_status = true;
1670 goto cleanup;
1671 }
1672
b50107bb 1673 if (hcd->speed < HCD_USB3)
6fd45621
AX
1674 xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
1675 PORT_PLC);
1676
56192531 1677cleanup:
0f2a7930 1678 /* Update event ring dequeue pointer before dropping the lock */
3b72fca0 1679 inc_deq(xhci, xhci->event_ring);
0f2a7930 1680
386139d7
SS
1681 /* Don't make the USB core poll the roothub if we got a bad port status
1682 * change event. Besides, at that point we can't tell which roothub
1683 * (USB 2.0 or USB 3.0) to kick.
1684 */
1685 if (bogus_port_status)
1686 return;
1687
c52804a4
SS
1688 /*
1689 * xHCI port-status-change events occur when the "or" of all the
1690 * status-change bits in the portsc register changes from 0 to 1.
1691 * New status changes won't cause an event if any other change
1692 * bits are still set. When an event occurs, switch over to
1693 * polling to avoid losing status changes.
1694 */
1695 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1696 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
0f2a7930
SS
1697 spin_unlock(&xhci->lock);
1698 /* Pass this up to the core */
f6ff0ac8 1699 usb_hcd_poll_rh_status(hcd);
0f2a7930
SS
1700 spin_lock(&xhci->lock);
1701}
1702
d0e96f5a
SS
1703/*
1704 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1705 * at end_trb, which may be in another segment. If the suspect DMA address is a
1706 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1707 * returns 0.
1708 */
cffb9be8
HG
1709struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
1710 struct xhci_segment *start_seg,
d0e96f5a
SS
1711 union xhci_trb *start_trb,
1712 union xhci_trb *end_trb,
cffb9be8
HG
1713 dma_addr_t suspect_dma,
1714 bool debug)
d0e96f5a
SS
1715{
1716 dma_addr_t start_dma;
1717 dma_addr_t end_seg_dma;
1718 dma_addr_t end_trb_dma;
1719 struct xhci_segment *cur_seg;
1720
23e3be11 1721 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
d0e96f5a
SS
1722 cur_seg = start_seg;
1723
1724 do {
2fa88daa 1725 if (start_dma == 0)
326b4810 1726 return NULL;
ae636747 1727 /* We may get an event for a Link TRB in the middle of a TD */
23e3be11 1728 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
2fa88daa 1729 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
d0e96f5a 1730 /* If the end TRB isn't in this segment, this is set to 0 */
23e3be11 1731 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
d0e96f5a 1732
cffb9be8
HG
1733 if (debug)
1734 xhci_warn(xhci,
1735 "Looking for event-dma %016llx trb-start %016llx trb-end %016llx seg-start %016llx seg-end %016llx\n",
1736 (unsigned long long)suspect_dma,
1737 (unsigned long long)start_dma,
1738 (unsigned long long)end_trb_dma,
1739 (unsigned long long)cur_seg->dma,
1740 (unsigned long long)end_seg_dma);
1741
d0e96f5a
SS
1742 if (end_trb_dma > 0) {
1743 /* The end TRB is in this segment, so suspect should be here */
1744 if (start_dma <= end_trb_dma) {
1745 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1746 return cur_seg;
1747 } else {
1748 /* Case for one segment with
1749 * a TD wrapped around to the top
1750 */
1751 if ((suspect_dma >= start_dma &&
1752 suspect_dma <= end_seg_dma) ||
1753 (suspect_dma >= cur_seg->dma &&
1754 suspect_dma <= end_trb_dma))
1755 return cur_seg;
1756 }
326b4810 1757 return NULL;
d0e96f5a
SS
1758 } else {
1759 /* Might still be somewhere in this segment */
1760 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1761 return cur_seg;
1762 }
1763 cur_seg = cur_seg->next;
23e3be11 1764 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
2fa88daa 1765 } while (cur_seg != start_seg);
d0e96f5a 1766
326b4810 1767 return NULL;
d0e96f5a
SS
1768}
1769
bcef3fd5
SS
1770static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1771 unsigned int slot_id, unsigned int ep_index,
e9df17eb 1772 unsigned int stream_id,
f97c08ae 1773 struct xhci_td *td, union xhci_trb *ep_trb)
bcef3fd5
SS
1774{
1775 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
ddba5cd0
MN
1776 struct xhci_command *command;
1777 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
1778 if (!command)
1779 return;
1780
d0167ad2 1781 ep->ep_state |= EP_HALTED;
e9df17eb 1782 ep->stopped_stream = stream_id;
1624ae1c 1783
ddba5cd0 1784 xhci_queue_reset_ep(xhci, command, slot_id, ep_index);
d97b4f8d 1785 xhci_cleanup_stalled_ring(xhci, ep_index, td);
1624ae1c 1786
5e5cf6fc 1787 ep->stopped_stream = 0;
1624ae1c 1788
bcef3fd5
SS
1789 xhci_ring_cmd_db(xhci);
1790}
1791
1792/* Check if an error has halted the endpoint ring. The class driver will
1793 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1794 * However, a babble and other errors also halt the endpoint ring, and the class
1795 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1796 * Ring Dequeue Pointer command manually.
1797 */
1798static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1799 struct xhci_ep_ctx *ep_ctx,
1800 unsigned int trb_comp_code)
1801{
1802 /* TRB completion codes that may require a manual halt cleanup */
1803 if (trb_comp_code == COMP_TX_ERR ||
1804 trb_comp_code == COMP_BABBLE ||
1805 trb_comp_code == COMP_SPLIT_ERR)
d4fc8bf5 1806 /* The 0.95 spec says a babbling control endpoint
bcef3fd5
SS
1807 * is not halted. The 0.96 spec says it is. Some HW
1808 * claims to be 0.95 compliant, but it halts the control
1809 * endpoint anyway. Check if a babble halted the
1810 * endpoint.
1811 */
f5960b69
ME
1812 if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1813 cpu_to_le32(EP_STATE_HALTED))
bcef3fd5
SS
1814 return 1;
1815
1816 return 0;
1817}
1818
b45b5069
SS
1819int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1820{
1821 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1822 /* Vendor defined "informational" completion code,
1823 * treat as not-an-error.
1824 */
1825 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1826 trb_comp_code);
1827 xhci_dbg(xhci, "Treating code as success.\n");
1828 return 1;
1829 }
1830 return 0;
1831}
1832
4422da61
AX
1833/*
1834 * Finish the td processing, remove the td from td list;
1835 * Return 1 if the urb can be given back.
1836 */
1837static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
f97c08ae 1838 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
4422da61
AX
1839 struct xhci_virt_ep *ep, int *status, bool skip)
1840{
1841 struct xhci_virt_device *xdev;
1842 struct xhci_ring *ep_ring;
1843 unsigned int slot_id;
1844 int ep_index;
1845 struct urb *urb = NULL;
1846 struct xhci_ep_ctx *ep_ctx;
1847 int ret = 0;
8e51adcc 1848 struct urb_priv *urb_priv;
4422da61
AX
1849 u32 trb_comp_code;
1850
28ccd296 1851 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
4422da61 1852 xdev = xhci->devs[slot_id];
28ccd296
ME
1853 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1854 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
4422da61 1855 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
28ccd296 1856 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
4422da61
AX
1857
1858 if (skip)
1859 goto td_cleanup;
1860
40a3b775
LB
1861 if (trb_comp_code == COMP_STOP_INVAL ||
1862 trb_comp_code == COMP_STOP ||
1863 trb_comp_code == COMP_STOP_SHORT) {
4422da61
AX
1864 /* The Endpoint Stop Command completion will take care of any
1865 * stopped TDs. A stopped TD may be restarted, so don't update
1866 * the ring dequeue pointer or take this TD off any lists yet.
1867 */
1868 ep->stopped_td = td;
4422da61 1869 return 0;
69defe04
MN
1870 }
1871 if (trb_comp_code == COMP_STALL ||
1872 xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
1873 trb_comp_code)) {
1874 /* Issue a reset endpoint command to clear the host side
1875 * halt, followed by a set dequeue command to move the
1876 * dequeue pointer past the TD.
1877 * The class driver clears the device side halt later.
1878 */
1879 xhci_cleanup_halted_endpoint(xhci, slot_id, ep_index,
f97c08ae 1880 ep_ring->stream_id, td, ep_trb);
4422da61 1881 } else {
69defe04
MN
1882 /* Update ring dequeue pointer */
1883 while (ep_ring->dequeue != td->last_trb)
3b72fca0 1884 inc_deq(xhci, ep_ring);
69defe04
MN
1885 inc_deq(xhci, ep_ring);
1886 }
4422da61
AX
1887
1888td_cleanup:
69defe04
MN
1889 /* Clean up the endpoint's TD list */
1890 urb = td->urb;
1891 urb_priv = urb->hcpriv;
1892
f9c589e1
MN
1893 /* if a bounce buffer was used to align this td then unmap it */
1894 if (td->bounce_seg)
1895 xhci_unmap_td_bounce_buffer(xhci, ep_ring, td);
1896
69defe04
MN
1897 /* Do one last check of the actual transfer length.
1898 * If the host controller said we transferred more data than the buffer
1899 * length, urb->actual_length will be a very big number (since it's
1900 * unsigned). Play it safe and say we didn't transfer anything.
1901 */
1902 if (urb->actual_length > urb->transfer_buffer_length) {
1903 xhci_warn(xhci, "URB transfer length is wrong, xHC issue? req. len = %u, act. len = %u\n",
1904 urb->transfer_buffer_length,
1905 urb->actual_length);
1906 urb->actual_length = 0;
69defe04
MN
1907 *status = 0;
1908 }
1909 list_del_init(&td->td_list);
1910 /* Was this TD slated to be cancelled but completed anyway? */
1911 if (!list_empty(&td->cancelled_td_list))
1912 list_del_init(&td->cancelled_td_list);
1913
1914 urb_priv->td_cnt++;
1915 /* Giveback the urb when all the tds are completed */
1916 if (urb_priv->td_cnt == urb_priv->length) {
1917 ret = 1;
1918 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1919 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
1920 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
1921 if (xhci->quirks & XHCI_AMD_PLL_FIX)
1922 usb_amd_quirk_pll_enable();
c41136b0
AX
1923 }
1924 }
4422da61
AX
1925 }
1926
1927 return ret;
1928}
1929
30a65b45
MN
1930/* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */
1931static int sum_trb_lengths(struct xhci_hcd *xhci, struct xhci_ring *ring,
1932 union xhci_trb *stop_trb)
1933{
1934 u32 sum;
1935 union xhci_trb *trb = ring->dequeue;
1936 struct xhci_segment *seg = ring->deq_seg;
1937
1938 for (sum = 0; trb != stop_trb; next_trb(xhci, ring, &seg, &trb)) {
1939 if (!trb_is_noop(trb) && !trb_is_link(trb))
1940 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2]));
1941 }
1942 return sum;
1943}
1944
8af56be1
AX
1945/*
1946 * Process control tds, update urb status and actual_length.
1947 */
1948static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
f97c08ae 1949 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
8af56be1
AX
1950 struct xhci_virt_ep *ep, int *status)
1951{
1952 struct xhci_virt_device *xdev;
1953 struct xhci_ring *ep_ring;
1954 unsigned int slot_id;
1955 int ep_index;
1956 struct xhci_ep_ctx *ep_ctx;
1957 u32 trb_comp_code;
0b6c324c
MN
1958 u32 remaining, requested;
1959 bool on_data_stage;
8af56be1 1960
28ccd296 1961 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
8af56be1 1962 xdev = xhci->devs[slot_id];
28ccd296
ME
1963 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1964 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
8af56be1 1965 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
28ccd296 1966 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
0b6c324c
MN
1967 requested = td->urb->transfer_buffer_length;
1968 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
1969
1970 /* not setup (dequeue), or status stage means we are at data stage */
f97c08ae 1971 on_data_stage = (ep_trb != ep_ring->dequeue && ep_trb != td->last_trb);
8af56be1 1972
8af56be1
AX
1973 switch (trb_comp_code) {
1974 case COMP_SUCCESS:
f97c08ae 1975 if (ep_trb != td->last_trb) {
0b6c324c
MN
1976 xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n",
1977 on_data_stage ? "data" : "setup");
8af56be1 1978 *status = -ESHUTDOWN;
0b6c324c 1979 break;
8af56be1 1980 }
0b6c324c 1981 *status = 0;
8af56be1
AX
1982 break;
1983 case COMP_SHORT_TX:
0b6c324c 1984 *status = 0;
8af56be1 1985 break;
40a3b775 1986 case COMP_STOP_SHORT:
0b6c324c
MN
1987 if (on_data_stage)
1988 td->urb->actual_length = remaining;
40a3b775 1989 else
0b6c324c
MN
1990 xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n");
1991 goto finish_td;
3abeca99 1992 case COMP_STOP:
0b6c324c
MN
1993 if (on_data_stage)
1994 td->urb->actual_length = requested - remaining;
1995 goto finish_td;
40a3b775 1996 case COMP_STOP_INVAL:
0b6c324c 1997 goto finish_td;
8af56be1
AX
1998 default:
1999 if (!xhci_requires_manual_halt_cleanup(xhci,
0b6c324c 2000 ep_ctx, trb_comp_code))
8af56be1 2001 break;
0b6c324c
MN
2002 xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n",
2003 trb_comp_code, ep_index);
8af56be1
AX
2004 /* else fall through */
2005 case COMP_STALL:
2006 /* Did we transfer part of the data (middle) phase? */
0b6c324c
MN
2007 if (on_data_stage)
2008 td->urb->actual_length = requested - remaining;
22ae47e6 2009 else if (!td->urb_length_set)
8af56be1 2010 td->urb->actual_length = 0;
0b6c324c 2011 goto finish_td;
8af56be1 2012 }
0b6c324c
MN
2013
2014 /* stopped at setup stage, no data transferred */
f97c08ae 2015 if (ep_trb == ep_ring->dequeue)
0b6c324c
MN
2016 goto finish_td;
2017
8af56be1 2018 /*
0b6c324c
MN
2019 * if on data stage then update the actual_length of the URB and flag it
2020 * as set, so it won't be overwritten in the event for the last TRB.
8af56be1 2021 */
0b6c324c
MN
2022 if (on_data_stage) {
2023 td->urb_length_set = true;
2024 td->urb->actual_length = requested - remaining;
2025 xhci_dbg(xhci, "Waiting for status stage event\n");
2026 return 0;
8af56be1
AX
2027 }
2028
0b6c324c
MN
2029 /* at status stage */
2030 if (!td->urb_length_set)
2031 td->urb->actual_length = requested;
2032
2033finish_td:
f97c08ae 2034 return finish_td(xhci, td, ep_trb, event, ep, status, false);
8af56be1
AX
2035}
2036
04e51901
AX
2037/*
2038 * Process isochronous tds, update urb packet status and actual_length.
2039 */
2040static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
f97c08ae 2041 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
04e51901
AX
2042 struct xhci_virt_ep *ep, int *status)
2043{
2044 struct xhci_ring *ep_ring;
2045 struct urb_priv *urb_priv;
2046 int idx;
926008c9 2047 struct usb_iso_packet_descriptor *frame;
04e51901 2048 u32 trb_comp_code;
36da3a1d
MN
2049 bool sum_trbs_for_length = false;
2050 u32 remaining, requested, ep_trb_len;
2051 int short_framestatus;
04e51901 2052
28ccd296
ME
2053 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
2054 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
04e51901
AX
2055 urb_priv = td->urb->hcpriv;
2056 idx = urb_priv->td_cnt;
926008c9 2057 frame = &td->urb->iso_frame_desc[idx];
36da3a1d
MN
2058 requested = frame->length;
2059 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2060 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
2061 short_framestatus = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
2062 -EREMOTEIO : 0;
04e51901 2063
926008c9
DT
2064 /* handle completion code */
2065 switch (trb_comp_code) {
2066 case COMP_SUCCESS:
36da3a1d
MN
2067 if (remaining) {
2068 frame->status = short_framestatus;
2069 if (xhci->quirks & XHCI_TRUST_TX_LENGTH)
2070 sum_trbs_for_length = true;
1530bbc6
SS
2071 break;
2072 }
36da3a1d
MN
2073 frame->status = 0;
2074 break;
926008c9 2075 case COMP_SHORT_TX:
36da3a1d
MN
2076 frame->status = short_framestatus;
2077 sum_trbs_for_length = true;
926008c9
DT
2078 break;
2079 case COMP_BW_OVER:
2080 frame->status = -ECOMM;
926008c9
DT
2081 break;
2082 case COMP_BUFF_OVER:
2083 case COMP_BABBLE:
2084 frame->status = -EOVERFLOW;
926008c9 2085 break;
f6ba6fe2 2086 case COMP_DEV_ERR:
926008c9 2087 case COMP_STALL:
d104d015 2088 frame->status = -EPROTO;
d104d015 2089 break;
9c745995 2090 case COMP_TX_ERR:
926008c9 2091 frame->status = -EPROTO;
f97c08ae 2092 if (ep_trb != td->last_trb)
d104d015 2093 return 0;
926008c9
DT
2094 break;
2095 case COMP_STOP:
36da3a1d
MN
2096 sum_trbs_for_length = true;
2097 break;
2098 case COMP_STOP_SHORT:
2099 /* field normally containing residue now contains tranferred */
2100 frame->status = short_framestatus;
2101 requested = remaining;
2102 break;
926008c9 2103 case COMP_STOP_INVAL:
36da3a1d
MN
2104 requested = 0;
2105 remaining = 0;
926008c9
DT
2106 break;
2107 default:
36da3a1d 2108 sum_trbs_for_length = true;
926008c9
DT
2109 frame->status = -1;
2110 break;
04e51901
AX
2111 }
2112
36da3a1d
MN
2113 if (sum_trbs_for_length)
2114 frame->actual_length = sum_trb_lengths(xhci, ep_ring, ep_trb) +
2115 ep_trb_len - remaining;
2116 else
2117 frame->actual_length = requested;
04e51901 2118
36da3a1d 2119 td->urb->actual_length += frame->actual_length;
04e51901 2120
f97c08ae 2121 return finish_td(xhci, td, ep_trb, event, ep, status, false);
04e51901
AX
2122}
2123
926008c9
DT
2124static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
2125 struct xhci_transfer_event *event,
2126 struct xhci_virt_ep *ep, int *status)
2127{
2128 struct xhci_ring *ep_ring;
2129 struct urb_priv *urb_priv;
2130 struct usb_iso_packet_descriptor *frame;
2131 int idx;
2132
f6975314 2133 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
926008c9
DT
2134 urb_priv = td->urb->hcpriv;
2135 idx = urb_priv->td_cnt;
2136 frame = &td->urb->iso_frame_desc[idx];
2137
b3df3f9c 2138 /* The transfer is partly done. */
926008c9
DT
2139 frame->status = -EXDEV;
2140
2141 /* calc actual length */
2142 frame->actual_length = 0;
2143
2144 /* Update ring dequeue pointer */
2145 while (ep_ring->dequeue != td->last_trb)
3b72fca0
AX
2146 inc_deq(xhci, ep_ring);
2147 inc_deq(xhci, ep_ring);
926008c9
DT
2148
2149 return finish_td(xhci, td, NULL, event, ep, status, true);
2150}
2151
22405ed2
AX
2152/*
2153 * Process bulk and interrupt tds, update urb status and actual_length.
2154 */
2155static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
f97c08ae 2156 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
22405ed2
AX
2157 struct xhci_virt_ep *ep, int *status)
2158{
2159 struct xhci_ring *ep_ring;
22405ed2 2160 u32 trb_comp_code;
f97c08ae 2161 u32 remaining, requested, ep_trb_len;
22405ed2 2162
28ccd296
ME
2163 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
2164 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
30a65b45 2165 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
f97c08ae 2166 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
30a65b45 2167 requested = td->urb->transfer_buffer_length;
22405ed2
AX
2168
2169 switch (trb_comp_code) {
2170 case COMP_SUCCESS:
30a65b45 2171 /* handle success with untransferred data as short packet */
f97c08ae 2172 if (ep_trb != td->last_trb || remaining) {
52ab8685 2173 xhci_warn(xhci, "WARN Successful completion on short TX\n");
30a65b45
MN
2174 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n",
2175 td->urb->ep->desc.bEndpointAddress,
2176 requested, remaining);
22405ed2 2177 }
52ab8685 2178 *status = 0;
22405ed2
AX
2179 break;
2180 case COMP_SHORT_TX:
30a65b45
MN
2181 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n",
2182 td->urb->ep->desc.bEndpointAddress,
2183 requested, remaining);
52ab8685 2184 *status = 0;
22405ed2 2185 break;
30a65b45
MN
2186 case COMP_STOP_SHORT:
2187 td->urb->actual_length = remaining;
2188 goto finish_td;
2189 case COMP_STOP_INVAL:
2190 /* stopped on ep trb with invalid length, exclude it */
f97c08ae 2191 ep_trb_len = 0;
30a65b45
MN
2192 remaining = 0;
2193 break;
22405ed2 2194 default:
30a65b45 2195 /* do nothing */
22405ed2
AX
2196 break;
2197 }
40a3b775 2198
f97c08ae 2199 if (ep_trb == td->last_trb)
30a65b45
MN
2200 td->urb->actual_length = requested - remaining;
2201 else
2202 td->urb->actual_length =
f97c08ae
MN
2203 sum_trb_lengths(xhci, ep_ring, ep_trb) +
2204 ep_trb_len - remaining;
30a65b45
MN
2205finish_td:
2206 if (remaining > requested) {
2207 xhci_warn(xhci, "bad transfer trb length %d in event trb\n",
2208 remaining);
22405ed2 2209 td->urb->actual_length = 0;
22405ed2 2210 }
f97c08ae 2211 return finish_td(xhci, td, ep_trb, event, ep, status, false);
22405ed2
AX
2212}
2213
d0e96f5a
SS
2214/*
2215 * If this function returns an error condition, it means it got a Transfer
2216 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
2217 * At this point, the host controller is probably hosed and should be reset.
2218 */
2219static int handle_tx_event(struct xhci_hcd *xhci,
2220 struct xhci_transfer_event *event)
ed384bd3
FB
2221 __releases(&xhci->lock)
2222 __acquires(&xhci->lock)
d0e96f5a
SS
2223{
2224 struct xhci_virt_device *xdev;
63a0d9ab 2225 struct xhci_virt_ep *ep;
d0e96f5a 2226 struct xhci_ring *ep_ring;
82d1009f 2227 unsigned int slot_id;
d0e96f5a 2228 int ep_index;
326b4810 2229 struct xhci_td *td = NULL;
f97c08ae
MN
2230 dma_addr_t ep_trb_dma;
2231 struct xhci_segment *ep_seg;
2232 union xhci_trb *ep_trb;
d0e96f5a 2233 int status = -EINPROGRESS;
d115b048 2234 struct xhci_ep_ctx *ep_ctx;
c2d7b49f 2235 struct list_head *tmp;
66d1eebc 2236 u32 trb_comp_code;
4422da61 2237 int ret = 0;
c2d7b49f 2238 int td_num = 0;
3b4739b8 2239 bool handling_skipped_tds = false;
d0e96f5a 2240
28ccd296 2241 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
82d1009f 2242 xdev = xhci->devs[slot_id];
d0e96f5a
SS
2243 if (!xdev) {
2244 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
9258c0b2 2245 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
e910b440
SS
2246 (unsigned long long) xhci_trb_virt_to_dma(
2247 xhci->event_ring->deq_seg,
9258c0b2
SS
2248 xhci->event_ring->dequeue),
2249 lower_32_bits(le64_to_cpu(event->buffer)),
2250 upper_32_bits(le64_to_cpu(event->buffer)),
2251 le32_to_cpu(event->transfer_len),
2252 le32_to_cpu(event->flags));
2253 xhci_dbg(xhci, "Event ring:\n");
2254 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
d0e96f5a
SS
2255 return -ENODEV;
2256 }
2257
2258 /* Endpoint ID is 1 based, our index is zero based */
28ccd296 2259 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
63a0d9ab 2260 ep = &xdev->eps[ep_index];
28ccd296 2261 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
d115b048 2262 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
986a92d4 2263 if (!ep_ring ||
28ccd296
ME
2264 (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
2265 EP_STATE_DISABLED) {
e9df17eb
SS
2266 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
2267 "or incorrect stream ring\n");
9258c0b2 2268 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
e910b440
SS
2269 (unsigned long long) xhci_trb_virt_to_dma(
2270 xhci->event_ring->deq_seg,
9258c0b2
SS
2271 xhci->event_ring->dequeue),
2272 lower_32_bits(le64_to_cpu(event->buffer)),
2273 upper_32_bits(le64_to_cpu(event->buffer)),
2274 le32_to_cpu(event->transfer_len),
2275 le32_to_cpu(event->flags));
2276 xhci_dbg(xhci, "Event ring:\n");
2277 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
d0e96f5a
SS
2278 return -ENODEV;
2279 }
2280
c2d7b49f
AX
2281 /* Count current td numbers if ep->skip is set */
2282 if (ep->skip) {
2283 list_for_each(tmp, &ep_ring->td_list)
2284 td_num++;
2285 }
2286
f97c08ae 2287 ep_trb_dma = le64_to_cpu(event->buffer);
28ccd296 2288 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
986a92d4 2289 /* Look for common error cases */
66d1eebc 2290 switch (trb_comp_code) {
b10de142
SS
2291 /* Skip codes that require special handling depending on
2292 * transfer type
2293 */
2294 case COMP_SUCCESS:
1c11a172 2295 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0)
1530bbc6
SS
2296 break;
2297 if (xhci->quirks & XHCI_TRUST_TX_LENGTH)
2298 trb_comp_code = COMP_SHORT_TX;
2299 else
8202ce2e
SS
2300 xhci_warn_ratelimited(xhci,
2301 "WARN Successful completion on short TX: needs XHCI_TRUST_TX_LENGTH quirk?\n");
b10de142
SS
2302 case COMP_SHORT_TX:
2303 break;
ae636747
SS
2304 case COMP_STOP:
2305 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
2306 break;
2307 case COMP_STOP_INVAL:
2308 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
2309 break;
40a3b775
LB
2310 case COMP_STOP_SHORT:
2311 xhci_dbg(xhci, "Stopped with short packet transfer detected\n");
2312 break;
b10de142 2313 case COMP_STALL:
2a9227a5 2314 xhci_dbg(xhci, "Stalled endpoint\n");
63a0d9ab 2315 ep->ep_state |= EP_HALTED;
b10de142
SS
2316 status = -EPIPE;
2317 break;
2318 case COMP_TRB_ERR:
2319 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
2320 status = -EILSEQ;
2321 break;
ec74e403 2322 case COMP_SPLIT_ERR:
b10de142 2323 case COMP_TX_ERR:
2a9227a5 2324 xhci_dbg(xhci, "Transfer error on endpoint\n");
b10de142
SS
2325 status = -EPROTO;
2326 break;
4a73143c 2327 case COMP_BABBLE:
2a9227a5 2328 xhci_dbg(xhci, "Babble error on endpoint\n");
4a73143c
SS
2329 status = -EOVERFLOW;
2330 break;
b10de142
SS
2331 case COMP_DB_ERR:
2332 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
2333 status = -ENOSR;
2334 break;
986a92d4
AX
2335 case COMP_BW_OVER:
2336 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
2337 break;
2338 case COMP_BUFF_OVER:
2339 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
2340 break;
2341 case COMP_UNDERRUN:
2342 /*
2343 * When the Isoch ring is empty, the xHC will generate
2344 * a Ring Overrun Event for IN Isoch endpoint or Ring
2345 * Underrun Event for OUT Isoch endpoint.
2346 */
2347 xhci_dbg(xhci, "underrun event on endpoint\n");
2348 if (!list_empty(&ep_ring->td_list))
2349 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2350 "still with TDs queued?\n",
28ccd296
ME
2351 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2352 ep_index);
986a92d4
AX
2353 goto cleanup;
2354 case COMP_OVERRUN:
2355 xhci_dbg(xhci, "overrun event on endpoint\n");
2356 if (!list_empty(&ep_ring->td_list))
2357 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2358 "still with TDs queued?\n",
28ccd296
ME
2359 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2360 ep_index);
986a92d4 2361 goto cleanup;
f6ba6fe2
AH
2362 case COMP_DEV_ERR:
2363 xhci_warn(xhci, "WARN: detect an incompatible device");
2364 status = -EPROTO;
2365 break;
d18240db
AX
2366 case COMP_MISSED_INT:
2367 /*
2368 * When encounter missed service error, one or more isoc tds
2369 * may be missed by xHC.
2370 * Set skip flag of the ep_ring; Complete the missed tds as
2371 * short transfer when process the ep_ring next time.
2372 */
2373 ep->skip = true;
2374 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
2375 goto cleanup;
3b4739b8
MN
2376 case COMP_PING_ERR:
2377 ep->skip = true;
2378 xhci_dbg(xhci, "No Ping response error, Skip one Isoc TD\n");
2379 goto cleanup;
b10de142 2380 default:
b45b5069 2381 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
5ad6a529
SS
2382 status = 0;
2383 break;
2384 }
86cd740a
MN
2385 xhci_warn(xhci, "ERROR Unknown event condition %u, HC probably busted\n",
2386 trb_comp_code);
986a92d4
AX
2387 goto cleanup;
2388 }
2389
d18240db
AX
2390 do {
2391 /* This TRB should be in the TD at the head of this ring's
2392 * TD list.
2393 */
2394 if (list_empty(&ep_ring->td_list)) {
a83d6755
SS
2395 /*
2396 * A stopped endpoint may generate an extra completion
2397 * event if the device was suspended. Don't print
2398 * warnings.
2399 */
2400 if (!(trb_comp_code == COMP_STOP ||
2401 trb_comp_code == COMP_STOP_INVAL)) {
2402 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
2403 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2404 ep_index);
2405 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
2406 (le32_to_cpu(event->flags) &
2407 TRB_TYPE_BITMASK)>>10);
2408 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2409 }
d18240db
AX
2410 if (ep->skip) {
2411 ep->skip = false;
2412 xhci_dbg(xhci, "td_list is empty while skip "
2413 "flag set. Clear skip flag.\n");
2414 }
2415 ret = 0;
2416 goto cleanup;
2417 }
986a92d4 2418
c2d7b49f
AX
2419 /* We've skipped all the TDs on the ep ring when ep->skip set */
2420 if (ep->skip && td_num == 0) {
2421 ep->skip = false;
2422 xhci_dbg(xhci, "All tds on the ep_ring skipped. "
2423 "Clear skip flag.\n");
2424 ret = 0;
2425 goto cleanup;
2426 }
2427
d18240db 2428 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
c2d7b49f
AX
2429 if (ep->skip)
2430 td_num--;
926008c9 2431
d18240db 2432 /* Is this a TRB in the currently executing TD? */
f97c08ae
MN
2433 ep_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue,
2434 td->last_trb, ep_trb_dma, false);
e1cf486d
AH
2435
2436 /*
2437 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
2438 * is not in the current TD pointed by ep_ring->dequeue because
2439 * that the hardware dequeue pointer still at the previous TRB
2440 * of the current TD. The previous TRB maybe a Link TD or the
2441 * last TRB of the previous TD. The command completion handle
2442 * will take care the rest.
2443 */
f97c08ae 2444 if (!ep_seg && (trb_comp_code == COMP_STOP ||
9a548863 2445 trb_comp_code == COMP_STOP_INVAL)) {
e1cf486d
AH
2446 ret = 0;
2447 goto cleanup;
2448 }
2449
f97c08ae 2450 if (!ep_seg) {
926008c9
DT
2451 if (!ep->skip ||
2452 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
ad808333
SS
2453 /* Some host controllers give a spurious
2454 * successful event after a short transfer.
2455 * Ignore it.
2456 */
ddba5cd0 2457 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
ad808333
SS
2458 ep_ring->last_td_was_short) {
2459 ep_ring->last_td_was_short = false;
2460 ret = 0;
2461 goto cleanup;
2462 }
926008c9
DT
2463 /* HC is busted, give up! */
2464 xhci_err(xhci,
2465 "ERROR Transfer event TRB DMA ptr not "
cffb9be8
HG
2466 "part of current TD ep_index %d "
2467 "comp_code %u\n", ep_index,
2468 trb_comp_code);
2469 trb_in_td(xhci, ep_ring->deq_seg,
2470 ep_ring->dequeue, td->last_trb,
f97c08ae 2471 ep_trb_dma, true);
926008c9
DT
2472 return -ESHUTDOWN;
2473 }
2474
2475 ret = skip_isoc_td(xhci, td, event, ep, &status);
2476 goto cleanup;
2477 }
ad808333
SS
2478 if (trb_comp_code == COMP_SHORT_TX)
2479 ep_ring->last_td_was_short = true;
2480 else
2481 ep_ring->last_td_was_short = false;
926008c9
DT
2482
2483 if (ep->skip) {
d18240db
AX
2484 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2485 ep->skip = false;
2486 }
678539cf 2487
f97c08ae
MN
2488 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) /
2489 sizeof(*ep_trb)];
926008c9
DT
2490 /*
2491 * No-op TRB should not trigger interrupts.
f97c08ae 2492 * If ep_trb is a no-op TRB, it means the
926008c9
DT
2493 * corresponding TD has been cancelled. Just ignore
2494 * the TD.
2495 */
f97c08ae
MN
2496 if (trb_is_noop(ep_trb)) {
2497 xhci_dbg(xhci, "ep_trb is a no-op TRB. Skip it\n");
926008c9 2498 goto cleanup;
d18240db 2499 }
4422da61 2500
d18240db
AX
2501 /* Now update the urb's actual_length and give back to
2502 * the core
82d1009f 2503 */
d18240db 2504 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
f97c08ae 2505 ret = process_ctrl_td(xhci, td, ep_trb, event, ep,
d18240db 2506 &status);
04e51901 2507 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
f97c08ae 2508 ret = process_isoc_td(xhci, td, ep_trb, event, ep,
04e51901 2509 &status);
d18240db 2510 else
f97c08ae 2511 ret = process_bulk_intr_td(xhci, td, ep_trb, event,
d18240db
AX
2512 ep, &status);
2513
2514cleanup:
3b4739b8
MN
2515
2516
2517 handling_skipped_tds = ep->skip &&
2518 trb_comp_code != COMP_MISSED_INT &&
2519 trb_comp_code != COMP_PING_ERR;
2520
d18240db 2521 /*
3b4739b8
MN
2522 * Do not update event ring dequeue pointer if we're in a loop
2523 * processing missed tds.
d18240db 2524 */
3b4739b8 2525 if (!handling_skipped_tds)
3b72fca0 2526 inc_deq(xhci, xhci->event_ring);
d18240db 2527
446b3141
MN
2528 if (ret)
2529 xhci_giveback_urb_locked(xhci, td, status);
d18240db
AX
2530 /*
2531 * If ep->skip is set, it means there are missed tds on the
2532 * endpoint ring need to take care of.
2533 * Process them as short transfer until reach the td pointed by
2534 * the event.
2535 */
3b4739b8 2536 } while (handling_skipped_tds);
d18240db 2537
d0e96f5a
SS
2538 return 0;
2539}
2540
0f2a7930
SS
2541/*
2542 * This function handles all OS-owned events on the event ring. It may drop
2543 * xhci->lock between event processing (e.g. to pass up port status changes).
9dee9a21
ME
2544 * Returns >0 for "possibly more events to process" (caller should call again),
2545 * otherwise 0 if done. In future, <0 returns should indicate error code.
0f2a7930 2546 */
9dee9a21 2547static int xhci_handle_event(struct xhci_hcd *xhci)
7f84eef0
SS
2548{
2549 union xhci_trb *event;
0f2a7930 2550 int update_ptrs = 1;
d0e96f5a 2551 int ret;
7f84eef0
SS
2552
2553 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2554 xhci->error_bitmask |= 1 << 1;
9dee9a21 2555 return 0;
7f84eef0
SS
2556 }
2557
2558 event = xhci->event_ring->dequeue;
2559 /* Does the HC or OS own the TRB? */
28ccd296
ME
2560 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
2561 xhci->event_ring->cycle_state) {
7f84eef0 2562 xhci->error_bitmask |= 1 << 2;
9dee9a21 2563 return 0;
7f84eef0
SS
2564 }
2565
92a3da41
ME
2566 /*
2567 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2568 * speculative reads of the event's flags/data below.
2569 */
2570 rmb();
0f2a7930 2571 /* FIXME: Handle more event types. */
28ccd296 2572 switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
7f84eef0
SS
2573 case TRB_TYPE(TRB_COMPLETION):
2574 handle_cmd_completion(xhci, &event->event_cmd);
2575 break;
0f2a7930
SS
2576 case TRB_TYPE(TRB_PORT_STATUS):
2577 handle_port_status(xhci, event);
2578 update_ptrs = 0;
2579 break;
d0e96f5a
SS
2580 case TRB_TYPE(TRB_TRANSFER):
2581 ret = handle_tx_event(xhci, &event->trans_event);
2582 if (ret < 0)
2583 xhci->error_bitmask |= 1 << 9;
2584 else
2585 update_ptrs = 0;
2586 break;
623bef9e
SS
2587 case TRB_TYPE(TRB_DEV_NOTE):
2588 handle_device_notification(xhci, event);
2589 break;
7f84eef0 2590 default:
28ccd296
ME
2591 if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
2592 TRB_TYPE(48))
0238634d
SS
2593 handle_vendor_event(xhci, event);
2594 else
2595 xhci->error_bitmask |= 1 << 3;
7f84eef0 2596 }
6f5165cf
SS
2597 /* Any of the above functions may drop and re-acquire the lock, so check
2598 * to make sure a watchdog timer didn't mark the host as non-responsive.
2599 */
2600 if (xhci->xhc_state & XHCI_STATE_DYING) {
2601 xhci_dbg(xhci, "xHCI host dying, returning from "
2602 "event handler.\n");
9dee9a21 2603 return 0;
6f5165cf 2604 }
7f84eef0 2605
c06d68b8
SS
2606 if (update_ptrs)
2607 /* Update SW event ring dequeue pointer */
3b72fca0 2608 inc_deq(xhci, xhci->event_ring);
c06d68b8 2609
9dee9a21
ME
2610 /* Are there more items on the event ring? Caller will call us again to
2611 * check.
2612 */
2613 return 1;
7f84eef0 2614}
9032cd52
SS
2615
2616/*
2617 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2618 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2619 * indicators of an event TRB error, but we check the status *first* to be safe.
2620 */
2621irqreturn_t xhci_irq(struct usb_hcd *hcd)
2622{
2623 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
c21599a3 2624 u32 status;
bda53145 2625 u64 temp_64;
c06d68b8
SS
2626 union xhci_trb *event_ring_deq;
2627 dma_addr_t deq;
9032cd52
SS
2628
2629 spin_lock(&xhci->lock);
9032cd52 2630 /* Check if the xHC generated the interrupt, or the irq is shared */
b0ba9720 2631 status = readl(&xhci->op_regs->status);
c21599a3 2632 if (status == 0xffffffff)
9032cd52
SS
2633 goto hw_died;
2634
c21599a3 2635 if (!(status & STS_EINT)) {
9032cd52 2636 spin_unlock(&xhci->lock);
9032cd52
SS
2637 return IRQ_NONE;
2638 }
27e0dd4d 2639 if (status & STS_FATAL) {
9032cd52
SS
2640 xhci_warn(xhci, "WARNING: Host System Error\n");
2641 xhci_halt(xhci);
2642hw_died:
9032cd52 2643 spin_unlock(&xhci->lock);
948fa135 2644 return IRQ_HANDLED;
9032cd52
SS
2645 }
2646
bda53145
SS
2647 /*
2648 * Clear the op reg interrupt status first,
2649 * so we can receive interrupts from other MSI-X interrupters.
2650 * Write 1 to clear the interrupt status.
2651 */
27e0dd4d 2652 status |= STS_EINT;
204b7793 2653 writel(status, &xhci->op_regs->status);
bda53145
SS
2654 /* FIXME when MSI-X is supported and there are multiple vectors */
2655 /* Clear the MSI-X event interrupt status */
2656
cd70469d 2657 if (hcd->irq) {
c21599a3
SS
2658 u32 irq_pending;
2659 /* Acknowledge the PCI interrupt */
b0ba9720 2660 irq_pending = readl(&xhci->ir_set->irq_pending);
4e833c0b 2661 irq_pending |= IMAN_IP;
204b7793 2662 writel(irq_pending, &xhci->ir_set->irq_pending);
c21599a3 2663 }
bda53145 2664
27a41a83
GKB
2665 if (xhci->xhc_state & XHCI_STATE_DYING ||
2666 xhci->xhc_state & XHCI_STATE_HALTED) {
bda53145
SS
2667 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2668 "Shouldn't IRQs be disabled?\n");
c06d68b8
SS
2669 /* Clear the event handler busy flag (RW1C);
2670 * the event ring should be empty.
bda53145 2671 */
f7b2e403 2672 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
477632df
SS
2673 xhci_write_64(xhci, temp_64 | ERST_EHB,
2674 &xhci->ir_set->erst_dequeue);
c06d68b8
SS
2675 spin_unlock(&xhci->lock);
2676
2677 return IRQ_HANDLED;
2678 }
2679
2680 event_ring_deq = xhci->event_ring->dequeue;
2681 /* FIXME this should be a delayed service routine
2682 * that clears the EHB.
2683 */
9dee9a21 2684 while (xhci_handle_event(xhci) > 0) {}
bda53145 2685
f7b2e403 2686 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
c06d68b8
SS
2687 /* If necessary, update the HW's version of the event ring deq ptr. */
2688 if (event_ring_deq != xhci->event_ring->dequeue) {
2689 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2690 xhci->event_ring->dequeue);
2691 if (deq == 0)
2692 xhci_warn(xhci, "WARN something wrong with SW event "
2693 "ring dequeue ptr.\n");
2694 /* Update HC event ring dequeue pointer */
2695 temp_64 &= ERST_PTR_MASK;
2696 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2697 }
2698
2699 /* Clear the event handler busy flag (RW1C); event ring is empty. */
2700 temp_64 |= ERST_EHB;
477632df 2701 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
c06d68b8 2702
9032cd52
SS
2703 spin_unlock(&xhci->lock);
2704
2705 return IRQ_HANDLED;
2706}
2707
851ec164 2708irqreturn_t xhci_msi_irq(int irq, void *hcd)
9032cd52 2709{
968b822c 2710 return xhci_irq(hcd);
9032cd52 2711}
7f84eef0 2712
d0e96f5a
SS
2713/**** Endpoint Ring Operations ****/
2714
7f84eef0
SS
2715/*
2716 * Generic function for queueing a TRB on a ring.
2717 * The caller must have checked to make sure there's room on the ring.
6cc30d85
SS
2718 *
2719 * @more_trbs_coming: Will you enqueue more TRBs before calling
2720 * prepare_transfer()?
7f84eef0
SS
2721 */
2722static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
3b72fca0 2723 bool more_trbs_coming,
7f84eef0
SS
2724 u32 field1, u32 field2, u32 field3, u32 field4)
2725{
2726 struct xhci_generic_trb *trb;
2727
2728 trb = &ring->enqueue->generic;
28ccd296
ME
2729 trb->field[0] = cpu_to_le32(field1);
2730 trb->field[1] = cpu_to_le32(field2);
2731 trb->field[2] = cpu_to_le32(field3);
2732 trb->field[3] = cpu_to_le32(field4);
3b72fca0 2733 inc_enq(xhci, ring, more_trbs_coming);
7f84eef0
SS
2734}
2735
d0e96f5a
SS
2736/*
2737 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2738 * FIXME allocate segments if the ring is full.
2739 */
2740static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
3b72fca0 2741 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
d0e96f5a 2742{
8dfec614
AX
2743 unsigned int num_trbs_needed;
2744
d0e96f5a 2745 /* Make sure the endpoint has been added to xHC schedule */
d0e96f5a
SS
2746 switch (ep_state) {
2747 case EP_STATE_DISABLED:
2748 /*
2749 * USB core changed config/interfaces without notifying us,
2750 * or hardware is reporting the wrong state.
2751 */
2752 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2753 return -ENOENT;
d0e96f5a 2754 case EP_STATE_ERROR:
c92bcfa7 2755 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
d0e96f5a
SS
2756 /* FIXME event handling code for error needs to clear it */
2757 /* XXX not sure if this should be -ENOENT or not */
2758 return -EINVAL;
c92bcfa7
SS
2759 case EP_STATE_HALTED:
2760 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
d0e96f5a
SS
2761 case EP_STATE_STOPPED:
2762 case EP_STATE_RUNNING:
2763 break;
2764 default:
2765 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2766 /*
2767 * FIXME issue Configure Endpoint command to try to get the HC
2768 * back into a known state.
2769 */
2770 return -EINVAL;
2771 }
8dfec614
AX
2772
2773 while (1) {
3d4b81ed
SS
2774 if (room_on_ring(xhci, ep_ring, num_trbs))
2775 break;
8dfec614
AX
2776
2777 if (ep_ring == xhci->cmd_ring) {
2778 xhci_err(xhci, "Do not support expand command ring\n");
2779 return -ENOMEM;
2780 }
2781
68ffb011
XR
2782 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
2783 "ERROR no room on ep ring, try ring expansion");
8dfec614
AX
2784 num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
2785 if (xhci_ring_expansion(xhci, ep_ring, num_trbs_needed,
2786 mem_flags)) {
2787 xhci_err(xhci, "Ring expansion failed\n");
2788 return -ENOMEM;
2789 }
261fa12b 2790 }
6c12db90 2791
d0c77d84
MN
2792 while (trb_is_link(ep_ring->enqueue)) {
2793 /* If we're not dealing with 0.95 hardware or isoc rings
2794 * on AMD 0.96 host, clear the chain bit.
2795 */
2796 if (!xhci_link_trb_quirk(xhci) &&
2797 !(ep_ring->type == TYPE_ISOC &&
2798 (xhci->quirks & XHCI_AMD_0x96_HOST)))
2799 ep_ring->enqueue->link.control &=
2800 cpu_to_le32(~TRB_CHAIN);
2801 else
2802 ep_ring->enqueue->link.control |=
2803 cpu_to_le32(TRB_CHAIN);
6c12db90 2804
d0c77d84
MN
2805 wmb();
2806 ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE);
6c12db90 2807
d0c77d84
MN
2808 /* Toggle the cycle bit after the last ring segment. */
2809 if (link_trb_toggles_cycle(ep_ring->enqueue))
2810 ep_ring->cycle_state ^= 1;
6c12db90 2811
d0c77d84
MN
2812 ep_ring->enq_seg = ep_ring->enq_seg->next;
2813 ep_ring->enqueue = ep_ring->enq_seg->trbs;
6c12db90 2814 }
d0e96f5a
SS
2815 return 0;
2816}
2817
23e3be11 2818static int prepare_transfer(struct xhci_hcd *xhci,
d0e96f5a
SS
2819 struct xhci_virt_device *xdev,
2820 unsigned int ep_index,
e9df17eb 2821 unsigned int stream_id,
d0e96f5a
SS
2822 unsigned int num_trbs,
2823 struct urb *urb,
8e51adcc 2824 unsigned int td_index,
d0e96f5a
SS
2825 gfp_t mem_flags)
2826{
2827 int ret;
8e51adcc
AX
2828 struct urb_priv *urb_priv;
2829 struct xhci_td *td;
e9df17eb 2830 struct xhci_ring *ep_ring;
d115b048 2831 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
e9df17eb
SS
2832
2833 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2834 if (!ep_ring) {
2835 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2836 stream_id);
2837 return -EINVAL;
2838 }
2839
2840 ret = prepare_ring(xhci, ep_ring,
28ccd296 2841 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
3b72fca0 2842 num_trbs, mem_flags);
d0e96f5a
SS
2843 if (ret)
2844 return ret;
d0e96f5a 2845
8e51adcc
AX
2846 urb_priv = urb->hcpriv;
2847 td = urb_priv->td[td_index];
2848
2849 INIT_LIST_HEAD(&td->td_list);
2850 INIT_LIST_HEAD(&td->cancelled_td_list);
2851
2852 if (td_index == 0) {
214f76f7 2853 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
d13565c1 2854 if (unlikely(ret))
8e51adcc 2855 return ret;
d0e96f5a
SS
2856 }
2857
8e51adcc 2858 td->urb = urb;
d0e96f5a 2859 /* Add this TD to the tail of the endpoint ring's TD list */
8e51adcc
AX
2860 list_add_tail(&td->td_list, &ep_ring->td_list);
2861 td->start_seg = ep_ring->enq_seg;
2862 td->first_trb = ep_ring->enqueue;
2863
2864 urb_priv->td[td_index] = td;
d0e96f5a
SS
2865
2866 return 0;
2867}
2868
d2510342
AI
2869static unsigned int count_trbs(u64 addr, u64 len)
2870{
2871 unsigned int num_trbs;
2872
2873 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
2874 TRB_MAX_BUFF_SIZE);
2875 if (num_trbs == 0)
2876 num_trbs++;
2877
2878 return num_trbs;
2879}
2880
2881static inline unsigned int count_trbs_needed(struct urb *urb)
2882{
2883 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length);
2884}
2885
2886static unsigned int count_sg_trbs_needed(struct urb *urb)
8a96c052 2887{
8a96c052 2888 struct scatterlist *sg;
d2510342 2889 unsigned int i, len, full_len, num_trbs = 0;
8a96c052 2890
d2510342 2891 full_len = urb->transfer_buffer_length;
8a96c052 2892
d2510342
AI
2893 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
2894 len = sg_dma_len(sg);
2895 num_trbs += count_trbs(sg_dma_address(sg), len);
2896 len = min_t(unsigned int, len, full_len);
2897 full_len -= len;
2898 if (full_len == 0)
8a96c052
SS
2899 break;
2900 }
d2510342 2901
8a96c052
SS
2902 return num_trbs;
2903}
2904
d2510342
AI
2905static unsigned int count_isoc_trbs_needed(struct urb *urb, int i)
2906{
2907 u64 addr, len;
2908
2909 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
2910 len = urb->iso_frame_desc[i].length;
2911
2912 return count_trbs(addr, len);
2913}
2914
2915static void check_trb_math(struct urb *urb, int running_total)
8a96c052 2916{
d2510342 2917 if (unlikely(running_total != urb->transfer_buffer_length))
a2490187 2918 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
8a96c052
SS
2919 "queued %#x (%d), asked for %#x (%d)\n",
2920 __func__,
2921 urb->ep->desc.bEndpointAddress,
2922 running_total, running_total,
2923 urb->transfer_buffer_length,
2924 urb->transfer_buffer_length);
2925}
2926
23e3be11 2927static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
e9df17eb 2928 unsigned int ep_index, unsigned int stream_id, int start_cycle,
e1eab2e0 2929 struct xhci_generic_trb *start_trb)
8a96c052 2930{
8a96c052
SS
2931 /*
2932 * Pass all the TRBs to the hardware at once and make sure this write
2933 * isn't reordered.
2934 */
2935 wmb();
50f7b52a 2936 if (start_cycle)
28ccd296 2937 start_trb->field[3] |= cpu_to_le32(start_cycle);
50f7b52a 2938 else
28ccd296 2939 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
be88fe4f 2940 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
8a96c052
SS
2941}
2942
78140156
AI
2943static void check_interval(struct xhci_hcd *xhci, struct urb *urb,
2944 struct xhci_ep_ctx *ep_ctx)
624defa1 2945{
624defa1
SS
2946 int xhci_interval;
2947 int ep_interval;
2948
28ccd296 2949 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
624defa1 2950 ep_interval = urb->interval;
78140156 2951
624defa1
SS
2952 /* Convert to microframes */
2953 if (urb->dev->speed == USB_SPEED_LOW ||
2954 urb->dev->speed == USB_SPEED_FULL)
2955 ep_interval *= 8;
78140156 2956
624defa1
SS
2957 /* FIXME change this to a warning and a suggestion to use the new API
2958 * to set the polling interval (once the API is added).
2959 */
2960 if (xhci_interval != ep_interval) {
0730d52a
DK
2961 dev_dbg_ratelimited(&urb->dev->dev,
2962 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n",
2963 ep_interval, ep_interval == 1 ? "" : "s",
2964 xhci_interval, xhci_interval == 1 ? "" : "s");
624defa1
SS
2965 urb->interval = xhci_interval;
2966 /* Convert back to frames for LS/FS devices */
2967 if (urb->dev->speed == USB_SPEED_LOW ||
2968 urb->dev->speed == USB_SPEED_FULL)
2969 urb->interval /= 8;
2970 }
78140156
AI
2971}
2972
2973/*
2974 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
2975 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
2976 * (comprised of sg list entries) can take several service intervals to
2977 * transmit.
2978 */
2979int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2980 struct urb *urb, int slot_id, unsigned int ep_index)
2981{
2982 struct xhci_ep_ctx *ep_ctx;
2983
2984 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index);
2985 check_interval(xhci, urb, ep_ctx);
2986
3fc8206d 2987 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
624defa1
SS
2988}
2989
4da6e6f2 2990/*
4525c0a1
SS
2991 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
2992 * packets remaining in the TD (*not* including this TRB).
4da6e6f2
SS
2993 *
2994 * Total TD packet count = total_packet_count =
4525c0a1 2995 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
4da6e6f2
SS
2996 *
2997 * Packets transferred up to and including this TRB = packets_transferred =
2998 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
2999 *
3000 * TD size = total_packet_count - packets_transferred
3001 *
c840d6ce
MN
3002 * For xHCI 0.96 and older, TD size field should be the remaining bytes
3003 * including this TRB, right shifted by 10
3004 *
3005 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31.
3006 * This is taken care of in the TRB_TD_SIZE() macro
3007 *
4525c0a1 3008 * The last TRB in a TD must have the TD size set to zero.
4da6e6f2 3009 */
c840d6ce
MN
3010static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred,
3011 int trb_buff_len, unsigned int td_total_len,
124c3937 3012 struct urb *urb, bool more_trbs_coming)
4da6e6f2 3013{
c840d6ce
MN
3014 u32 maxp, total_packet_count;
3015
0cbd4b34
CY
3016 /* MTK xHCI is mostly 0.97 but contains some features from 1.0 */
3017 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST))
c840d6ce
MN
3018 return ((td_total_len - transferred) >> 10);
3019
48df4a6f 3020 /* One TRB with a zero-length data packet. */
124c3937 3021 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
c840d6ce 3022 trb_buff_len == td_total_len)
48df4a6f
SS
3023 return 0;
3024
0cbd4b34
CY
3025 /* for MTK xHCI, TD size doesn't include this TRB */
3026 if (xhci->quirks & XHCI_MTK_HOST)
3027 trb_buff_len = 0;
3028
3029 maxp = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
3030 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
3031
c840d6ce
MN
3032 /* Queueing functions don't count the current TRB into transferred */
3033 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
4da6e6f2
SS
3034}
3035
f9c589e1 3036
474ed23a 3037static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len,
f9c589e1 3038 u32 *trb_buff_len, struct xhci_segment *seg)
474ed23a 3039{
f9c589e1 3040 struct device *dev = xhci_to_hcd(xhci)->self.controller;
474ed23a
MN
3041 unsigned int unalign;
3042 unsigned int max_pkt;
f9c589e1 3043 u32 new_buff_len;
474ed23a
MN
3044
3045 max_pkt = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
3046 unalign = (enqd_len + *trb_buff_len) % max_pkt;
3047
3048 /* we got lucky, last normal TRB data on segment is packet aligned */
3049 if (unalign == 0)
3050 return 0;
3051
f9c589e1
MN
3052 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n",
3053 unalign, *trb_buff_len);
3054
474ed23a
MN
3055 /* is the last nornal TRB alignable by splitting it */
3056 if (*trb_buff_len > unalign) {
3057 *trb_buff_len -= unalign;
f9c589e1 3058 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len);
474ed23a
MN
3059 return 0;
3060 }
f9c589e1
MN
3061
3062 /*
3063 * We want enqd_len + trb_buff_len to sum up to a number aligned to
3064 * number which is divisible by the endpoint's wMaxPacketSize. IOW:
3065 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0.
3066 */
3067 new_buff_len = max_pkt - (enqd_len % max_pkt);
3068
3069 if (new_buff_len > (urb->transfer_buffer_length - enqd_len))
3070 new_buff_len = (urb->transfer_buffer_length - enqd_len);
3071
3072 /* create a max max_pkt sized bounce buffer pointed to by last trb */
3073 if (usb_urb_dir_out(urb)) {
3074 sg_pcopy_to_buffer(urb->sg, urb->num_mapped_sgs,
3075 seg->bounce_buf, new_buff_len, enqd_len);
3076 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3077 max_pkt, DMA_TO_DEVICE);
3078 } else {
3079 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3080 max_pkt, DMA_FROM_DEVICE);
3081 }
3082
3083 if (dma_mapping_error(dev, seg->bounce_dma)) {
3084 /* try without aligning. Some host controllers survive */
3085 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n");
3086 return 0;
3087 }
3088 *trb_buff_len = new_buff_len;
3089 seg->bounce_len = new_buff_len;
3090 seg->bounce_offs = enqd_len;
3091
3092 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len);
3093
474ed23a
MN
3094 return 1;
3095}
3096
d2510342
AI
3097/* This is very similar to what ehci-q.c qtd_fill() does */
3098int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
8a96c052
SS
3099 struct urb *urb, int slot_id, unsigned int ep_index)
3100{
5a5a0b1a 3101 struct xhci_ring *ring;
8e51adcc 3102 struct urb_priv *urb_priv;
8a96c052 3103 struct xhci_td *td;
d2510342
AI
3104 struct xhci_generic_trb *start_trb;
3105 struct scatterlist *sg = NULL;
5a83f04a
MN
3106 bool more_trbs_coming = true;
3107 bool need_zero_pkt = false;
86065c27
MN
3108 bool first_trb = true;
3109 unsigned int num_trbs;
d2510342 3110 unsigned int start_cycle, num_sgs = 0;
86065c27 3111 unsigned int enqd_len, block_len, trb_buff_len, full_len;
f9c589e1 3112 int sent_len, ret;
d2510342 3113 u32 field, length_field, remainder;
f9c589e1 3114 u64 addr, send_addr;
8a96c052 3115
5a5a0b1a
MN
3116 ring = xhci_urb_to_transfer_ring(xhci, urb);
3117 if (!ring)
e9df17eb
SS
3118 return -EINVAL;
3119
86065c27 3120 full_len = urb->transfer_buffer_length;
d2510342
AI
3121 /* If we have scatter/gather list, we use it. */
3122 if (urb->num_sgs) {
3123 num_sgs = urb->num_mapped_sgs;
3124 sg = urb->sg;
86065c27
MN
3125 addr = (u64) sg_dma_address(sg);
3126 block_len = sg_dma_len(sg);
d2510342 3127 num_trbs = count_sg_trbs_needed(urb);
86065c27 3128 } else {
d2510342 3129 num_trbs = count_trbs_needed(urb);
86065c27
MN
3130 addr = (u64) urb->transfer_dma;
3131 block_len = full_len;
3132 }
4758dcd1 3133 ret = prepare_transfer(xhci, xhci->devs[slot_id],
e9df17eb 3134 ep_index, urb->stream_id,
3b72fca0 3135 num_trbs, urb, 0, mem_flags);
d2510342 3136 if (unlikely(ret < 0))
4758dcd1 3137 return ret;
8e51adcc
AX
3138
3139 urb_priv = urb->hcpriv;
4758dcd1
RA
3140
3141 /* Deal with URB_ZERO_PACKET - need one more td/trb */
5a83f04a
MN
3142 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->length > 1)
3143 need_zero_pkt = true;
4758dcd1 3144
8e51adcc
AX
3145 td = urb_priv->td[0];
3146
8a96c052
SS
3147 /*
3148 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3149 * until we've finished creating all the other TRBs. The ring's cycle
3150 * state may change as we enqueue the other TRBs, so save it too.
3151 */
5a5a0b1a
MN
3152 start_trb = &ring->enqueue->generic;
3153 start_cycle = ring->cycle_state;
f9c589e1 3154 send_addr = addr;
8a96c052 3155
d2510342 3156 /* Queue the TRBs, even if they are zero-length */
0d2daade
AB
3157 for (enqd_len = 0; first_trb || enqd_len < full_len;
3158 enqd_len += trb_buff_len) {
d2510342 3159 field = TRB_TYPE(TRB_NORMAL);
af8b9e63 3160
86065c27
MN
3161 /* TRB buffer should not cross 64KB boundaries */
3162 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
3163 trb_buff_len = min_t(unsigned int, trb_buff_len, block_len);
8a96c052 3164
86065c27
MN
3165 if (enqd_len + trb_buff_len > full_len)
3166 trb_buff_len = full_len - enqd_len;
b10de142
SS
3167
3168 /* Don't change the cycle bit of the first TRB until later */
86065c27
MN
3169 if (first_trb) {
3170 first_trb = false;
50f7b52a 3171 if (start_cycle == 0)
d2510342 3172 field |= TRB_CYCLE;
50f7b52a 3173 } else
5a5a0b1a 3174 field |= ring->cycle_state;
b10de142
SS
3175
3176 /* Chain all the TRBs together; clear the chain bit in the last
3177 * TRB to indicate it's the last TRB in the chain.
3178 */
86065c27 3179 if (enqd_len + trb_buff_len < full_len) {
b10de142 3180 field |= TRB_CHAIN;
2d98ef40 3181 if (trb_is_link(ring->enqueue + 1)) {
474ed23a 3182 if (xhci_align_td(xhci, urb, enqd_len,
f9c589e1
MN
3183 &trb_buff_len,
3184 ring->enq_seg)) {
3185 send_addr = ring->enq_seg->bounce_dma;
3186 /* assuming TD won't span 2 segs */
3187 td->bounce_seg = ring->enq_seg;
3188 }
474ed23a 3189 }
f9c589e1
MN
3190 }
3191 if (enqd_len + trb_buff_len >= full_len) {
3192 field &= ~TRB_CHAIN;
4758dcd1 3193 field |= TRB_IOC;
124c3937 3194 more_trbs_coming = false;
5a83f04a 3195 td->last_trb = ring->enqueue;
b10de142 3196 }
af8b9e63
SS
3197
3198 /* Only set interrupt on short packet for IN endpoints */
3199 if (usb_urb_dir_in(urb))
3200 field |= TRB_ISP;
3201
4da6e6f2 3202 /* Set the TRB length, TD size, and interrupter fields. */
86065c27
MN
3203 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len,
3204 full_len, urb, more_trbs_coming);
3205
f9dc68fe 3206 length_field = TRB_LEN(trb_buff_len) |
c840d6ce 3207 TRB_TD_SIZE(remainder) |
f9dc68fe 3208 TRB_INTR_TARGET(0);
4da6e6f2 3209
124c3937 3210 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt,
f9c589e1
MN
3211 lower_32_bits(send_addr),
3212 upper_32_bits(send_addr),
f9dc68fe 3213 length_field,
d2510342 3214 field);
b10de142 3215
b10de142 3216 addr += trb_buff_len;
f9c589e1 3217 sent_len = trb_buff_len;
d2510342 3218
f9c589e1 3219 while (sg && sent_len >= block_len) {
86065c27
MN
3220 /* New sg entry */
3221 --num_sgs;
f9c589e1 3222 sent_len -= block_len;
86065c27 3223 if (num_sgs != 0) {
d2510342 3224 sg = sg_next(sg);
86065c27
MN
3225 block_len = sg_dma_len(sg);
3226 addr = (u64) sg_dma_address(sg);
f9c589e1 3227 addr += sent_len;
d2510342
AI
3228 }
3229 }
f9c589e1
MN
3230 block_len -= sent_len;
3231 send_addr = addr;
d2510342 3232 }
b10de142 3233
5a83f04a
MN
3234 if (need_zero_pkt) {
3235 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3236 ep_index, urb->stream_id,
3237 1, urb, 1, mem_flags);
3238 urb_priv->td[1]->last_trb = ring->enqueue;
3239 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC;
3240 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field);
3241 }
3242
86065c27 3243 check_trb_math(urb, enqd_len);
e9df17eb 3244 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
e1eab2e0 3245 start_cycle, start_trb);
b10de142
SS
3246 return 0;
3247}
3248
d0e96f5a 3249/* Caller must have locked xhci->lock */
23e3be11 3250int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
d0e96f5a
SS
3251 struct urb *urb, int slot_id, unsigned int ep_index)
3252{
3253 struct xhci_ring *ep_ring;
3254 int num_trbs;
3255 int ret;
3256 struct usb_ctrlrequest *setup;
3257 struct xhci_generic_trb *start_trb;
3258 int start_cycle;
c840d6ce 3259 u32 field, length_field, remainder;
8e51adcc 3260 struct urb_priv *urb_priv;
d0e96f5a
SS
3261 struct xhci_td *td;
3262
e9df17eb
SS
3263 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
3264 if (!ep_ring)
3265 return -EINVAL;
d0e96f5a
SS
3266
3267 /*
3268 * Need to copy setup packet into setup TRB, so we can't use the setup
3269 * DMA address.
3270 */
3271 if (!urb->setup_packet)
3272 return -EINVAL;
3273
d0e96f5a
SS
3274 /* 1 TRB for setup, 1 for status */
3275 num_trbs = 2;
3276 /*
3277 * Don't need to check if we need additional event data and normal TRBs,
3278 * since data in control transfers will never get bigger than 16MB
3279 * XXX: can we get a buffer that crosses 64KB boundaries?
3280 */
3281 if (urb->transfer_buffer_length > 0)
3282 num_trbs++;
e9df17eb
SS
3283 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3284 ep_index, urb->stream_id,
3b72fca0 3285 num_trbs, urb, 0, mem_flags);
d0e96f5a
SS
3286 if (ret < 0)
3287 return ret;
3288
8e51adcc
AX
3289 urb_priv = urb->hcpriv;
3290 td = urb_priv->td[0];
3291
d0e96f5a
SS
3292 /*
3293 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3294 * until we've finished creating all the other TRBs. The ring's cycle
3295 * state may change as we enqueue the other TRBs, so save it too.
3296 */
3297 start_trb = &ep_ring->enqueue->generic;
3298 start_cycle = ep_ring->cycle_state;
3299
3300 /* Queue setup TRB - see section 6.4.1.2.1 */
3301 /* FIXME better way to translate setup_packet into two u32 fields? */
3302 setup = (struct usb_ctrlrequest *) urb->setup_packet;
50f7b52a
AX
3303 field = 0;
3304 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3305 if (start_cycle == 0)
3306 field |= 0x1;
b83cdc8f 3307
dca77945 3308 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */
0cbd4b34 3309 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) {
b83cdc8f
AX
3310 if (urb->transfer_buffer_length > 0) {
3311 if (setup->bRequestType & USB_DIR_IN)
3312 field |= TRB_TX_TYPE(TRB_DATA_IN);
3313 else
3314 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3315 }
3316 }
3317
3b72fca0 3318 queue_trb(xhci, ep_ring, true,
28ccd296
ME
3319 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3320 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3321 TRB_LEN(8) | TRB_INTR_TARGET(0),
3322 /* Immediate data in pointer */
3323 field);
d0e96f5a
SS
3324
3325 /* If there's data, queue data TRBs */
af8b9e63
SS
3326 /* Only set interrupt on short packet for IN endpoints */
3327 if (usb_urb_dir_in(urb))
3328 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3329 else
3330 field = TRB_TYPE(TRB_DATA);
3331
c840d6ce
MN
3332 remainder = xhci_td_remainder(xhci, 0,
3333 urb->transfer_buffer_length,
3334 urb->transfer_buffer_length,
3335 urb, 1);
3336
f9dc68fe 3337 length_field = TRB_LEN(urb->transfer_buffer_length) |
c840d6ce 3338 TRB_TD_SIZE(remainder) |
f9dc68fe 3339 TRB_INTR_TARGET(0);
c840d6ce 3340
d0e96f5a
SS
3341 if (urb->transfer_buffer_length > 0) {
3342 if (setup->bRequestType & USB_DIR_IN)
3343 field |= TRB_DIR_IN;
3b72fca0 3344 queue_trb(xhci, ep_ring, true,
d0e96f5a
SS
3345 lower_32_bits(urb->transfer_dma),
3346 upper_32_bits(urb->transfer_dma),
f9dc68fe 3347 length_field,
af8b9e63 3348 field | ep_ring->cycle_state);
d0e96f5a
SS
3349 }
3350
3351 /* Save the DMA address of the last TRB in the TD */
3352 td->last_trb = ep_ring->enqueue;
3353
3354 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3355 /* If the device sent data, the status stage is an OUT transfer */
3356 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3357 field = 0;
3358 else
3359 field = TRB_DIR_IN;
3b72fca0 3360 queue_trb(xhci, ep_ring, false,
d0e96f5a
SS
3361 0,
3362 0,
3363 TRB_INTR_TARGET(0),
3364 /* Event on completion */
3365 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3366
e9df17eb 3367 giveback_first_trb(xhci, slot_id, ep_index, 0,
e1eab2e0 3368 start_cycle, start_trb);
d0e96f5a
SS
3369 return 0;
3370}
3371
5cd43e33
SS
3372/*
3373 * The transfer burst count field of the isochronous TRB defines the number of
3374 * bursts that are required to move all packets in this TD. Only SuperSpeed
3375 * devices can burst up to bMaxBurst number of packets per service interval.
3376 * This field is zero based, meaning a value of zero in the field means one
3377 * burst. Basically, for everything but SuperSpeed devices, this field will be
3378 * zero. Only xHCI 1.0 host controllers support this field.
3379 */
3380static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
5cd43e33
SS
3381 struct urb *urb, unsigned int total_packet_count)
3382{
3383 unsigned int max_burst;
3384
09c352ed 3385 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER)
5cd43e33
SS
3386 return 0;
3387
3388 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3213b151 3389 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
5cd43e33
SS
3390}
3391
b61d378f
SS
3392/*
3393 * Returns the number of packets in the last "burst" of packets. This field is
3394 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3395 * the last burst packet count is equal to the total number of packets in the
3396 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3397 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3398 * contain 1 to (bMaxBurst + 1) packets.
3399 */
3400static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
b61d378f
SS
3401 struct urb *urb, unsigned int total_packet_count)
3402{
3403 unsigned int max_burst;
3404 unsigned int residue;
3405
3406 if (xhci->hci_version < 0x100)
3407 return 0;
3408
09c352ed 3409 if (urb->dev->speed >= USB_SPEED_SUPER) {
b61d378f
SS
3410 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3411 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3412 residue = total_packet_count % (max_burst + 1);
3413 /* If residue is zero, the last burst contains (max_burst + 1)
3414 * number of packets, but the TLBPC field is zero-based.
3415 */
3416 if (residue == 0)
3417 return max_burst;
3418 return residue - 1;
b61d378f 3419 }
09c352ed
MN
3420 if (total_packet_count == 0)
3421 return 0;
3422 return total_packet_count - 1;
b61d378f
SS
3423}
3424
79b8094f
LB
3425/*
3426 * Calculates Frame ID field of the isochronous TRB identifies the
3427 * target frame that the Interval associated with this Isochronous
3428 * Transfer Descriptor will start on. Refer to 4.11.2.5 in 1.1 spec.
3429 *
3430 * Returns actual frame id on success, negative value on error.
3431 */
3432static int xhci_get_isoc_frame_id(struct xhci_hcd *xhci,
3433 struct urb *urb, int index)
3434{
3435 int start_frame, ist, ret = 0;
3436 int start_frame_id, end_frame_id, current_frame_id;
3437
3438 if (urb->dev->speed == USB_SPEED_LOW ||
3439 urb->dev->speed == USB_SPEED_FULL)
3440 start_frame = urb->start_frame + index * urb->interval;
3441 else
3442 start_frame = (urb->start_frame + index * urb->interval) >> 3;
3443
3444 /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2):
3445 *
3446 * If bit [3] of IST is cleared to '0', software can add a TRB no
3447 * later than IST[2:0] Microframes before that TRB is scheduled to
3448 * be executed.
3449 * If bit [3] of IST is set to '1', software can add a TRB no later
3450 * than IST[2:0] Frames before that TRB is scheduled to be executed.
3451 */
3452 ist = HCS_IST(xhci->hcs_params2) & 0x7;
3453 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
3454 ist <<= 3;
3455
3456 /* Software shall not schedule an Isoch TD with a Frame ID value that
3457 * is less than the Start Frame ID or greater than the End Frame ID,
3458 * where:
3459 *
3460 * End Frame ID = (Current MFINDEX register value + 895 ms.) MOD 2048
3461 * Start Frame ID = (Current MFINDEX register value + IST + 1) MOD 2048
3462 *
3463 * Both the End Frame ID and Start Frame ID values are calculated
3464 * in microframes. When software determines the valid Frame ID value;
3465 * The End Frame ID value should be rounded down to the nearest Frame
3466 * boundary, and the Start Frame ID value should be rounded up to the
3467 * nearest Frame boundary.
3468 */
3469 current_frame_id = readl(&xhci->run_regs->microframe_index);
3470 start_frame_id = roundup(current_frame_id + ist + 1, 8);
3471 end_frame_id = rounddown(current_frame_id + 895 * 8, 8);
3472
3473 start_frame &= 0x7ff;
3474 start_frame_id = (start_frame_id >> 3) & 0x7ff;
3475 end_frame_id = (end_frame_id >> 3) & 0x7ff;
3476
3477 xhci_dbg(xhci, "%s: index %d, reg 0x%x start_frame_id 0x%x, end_frame_id 0x%x, start_frame 0x%x\n",
3478 __func__, index, readl(&xhci->run_regs->microframe_index),
3479 start_frame_id, end_frame_id, start_frame);
3480
3481 if (start_frame_id < end_frame_id) {
3482 if (start_frame > end_frame_id ||
3483 start_frame < start_frame_id)
3484 ret = -EINVAL;
3485 } else if (start_frame_id > end_frame_id) {
3486 if ((start_frame > end_frame_id &&
3487 start_frame < start_frame_id))
3488 ret = -EINVAL;
3489 } else {
3490 ret = -EINVAL;
3491 }
3492
3493 if (index == 0) {
3494 if (ret == -EINVAL || start_frame == start_frame_id) {
3495 start_frame = start_frame_id + 1;
3496 if (urb->dev->speed == USB_SPEED_LOW ||
3497 urb->dev->speed == USB_SPEED_FULL)
3498 urb->start_frame = start_frame;
3499 else
3500 urb->start_frame = start_frame << 3;
3501 ret = 0;
3502 }
3503 }
3504
3505 if (ret) {
3506 xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n",
3507 start_frame, current_frame_id, index,
3508 start_frame_id, end_frame_id);
3509 xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n");
3510 return ret;
3511 }
3512
3513 return start_frame;
3514}
3515
04e51901
AX
3516/* This is for isoc transfer */
3517static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3518 struct urb *urb, int slot_id, unsigned int ep_index)
3519{
3520 struct xhci_ring *ep_ring;
3521 struct urb_priv *urb_priv;
3522 struct xhci_td *td;
3523 int num_tds, trbs_per_td;
3524 struct xhci_generic_trb *start_trb;
3525 bool first_trb;
3526 int start_cycle;
3527 u32 field, length_field;
3528 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3529 u64 start_addr, addr;
3530 int i, j;
47cbf692 3531 bool more_trbs_coming;
79b8094f 3532 struct xhci_virt_ep *xep;
09c352ed 3533 int frame_id;
04e51901 3534
79b8094f 3535 xep = &xhci->devs[slot_id]->eps[ep_index];
04e51901
AX
3536 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3537
3538 num_tds = urb->number_of_packets;
3539 if (num_tds < 1) {
3540 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3541 return -EINVAL;
3542 }
04e51901
AX
3543 start_addr = (u64) urb->transfer_dma;
3544 start_trb = &ep_ring->enqueue->generic;
3545 start_cycle = ep_ring->cycle_state;
3546
522989a2 3547 urb_priv = urb->hcpriv;
09c352ed 3548 /* Queue the TRBs for each TD, even if they are zero-length */
04e51901 3549 for (i = 0; i < num_tds; i++) {
09c352ed
MN
3550 unsigned int total_pkt_count, max_pkt;
3551 unsigned int burst_count, last_burst_pkt_count;
3552 u32 sia_frame_id;
04e51901 3553
4da6e6f2 3554 first_trb = true;
04e51901
AX
3555 running_total = 0;
3556 addr = start_addr + urb->iso_frame_desc[i].offset;
3557 td_len = urb->iso_frame_desc[i].length;
3558 td_remain_len = td_len;
09c352ed
MN
3559 max_pkt = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
3560 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt);
3561
48df4a6f 3562 /* A zero-length transfer still involves at least one packet. */
09c352ed
MN
3563 if (total_pkt_count == 0)
3564 total_pkt_count++;
3565 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count);
3566 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci,
3567 urb, total_pkt_count);
04e51901 3568
d2510342 3569 trbs_per_td = count_isoc_trbs_needed(urb, i);
04e51901
AX
3570
3571 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
3b72fca0 3572 urb->stream_id, trbs_per_td, urb, i, mem_flags);
522989a2
SS
3573 if (ret < 0) {
3574 if (i == 0)
3575 return ret;
3576 goto cleanup;
3577 }
04e51901 3578 td = urb_priv->td[i];
09c352ed
MN
3579
3580 /* use SIA as default, if frame id is used overwrite it */
3581 sia_frame_id = TRB_SIA;
3582 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3583 HCC_CFC(xhci->hcc_params)) {
3584 frame_id = xhci_get_isoc_frame_id(xhci, urb, i);
3585 if (frame_id >= 0)
3586 sia_frame_id = TRB_FRAME_ID(frame_id);
3587 }
3588 /*
3589 * Set isoc specific data for the first TRB in a TD.
3590 * Prevent HW from getting the TRBs by keeping the cycle state
3591 * inverted in the first TDs isoc TRB.
3592 */
2f6d3b65 3593 field = TRB_TYPE(TRB_ISOC) |
09c352ed
MN
3594 TRB_TLBPC(last_burst_pkt_count) |
3595 sia_frame_id |
3596 (i ? ep_ring->cycle_state : !start_cycle);
3597
2f6d3b65
MN
3598 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */
3599 if (!xep->use_extended_tbc)
3600 field |= TRB_TBC(burst_count);
3601
09c352ed 3602 /* fill the rest of the TRB fields, and remaining normal TRBs */
04e51901
AX
3603 for (j = 0; j < trbs_per_td; j++) {
3604 u32 remainder = 0;
09c352ed
MN
3605
3606 /* only first TRB is isoc, overwrite otherwise */
3607 if (!first_trb)
3608 field = TRB_TYPE(TRB_NORMAL) |
3609 ep_ring->cycle_state;
04e51901 3610
af8b9e63
SS
3611 /* Only set interrupt on short packet for IN EPs */
3612 if (usb_urb_dir_in(urb))
3613 field |= TRB_ISP;
3614
09c352ed 3615 /* Set the chain bit for all except the last TRB */
04e51901 3616 if (j < trbs_per_td - 1) {
47cbf692 3617 more_trbs_coming = true;
09c352ed 3618 field |= TRB_CHAIN;
04e51901 3619 } else {
09c352ed 3620 more_trbs_coming = false;
04e51901
AX
3621 td->last_trb = ep_ring->enqueue;
3622 field |= TRB_IOC;
09c352ed
MN
3623 /* set BEI, except for the last TD */
3624 if (xhci->hci_version >= 0x100 &&
3625 !(xhci->quirks & XHCI_AVOID_BEI) &&
3626 i < num_tds - 1)
3627 field |= TRB_BEI;
04e51901 3628 }
04e51901 3629 /* Calculate TRB length */
d2510342 3630 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
04e51901
AX
3631 if (trb_buff_len > td_remain_len)
3632 trb_buff_len = td_remain_len;
3633
4da6e6f2 3634 /* Set the TRB length, TD size, & interrupter fields. */
c840d6ce
MN
3635 remainder = xhci_td_remainder(xhci, running_total,
3636 trb_buff_len, td_len,
124c3937 3637 urb, more_trbs_coming);
c840d6ce 3638
04e51901 3639 length_field = TRB_LEN(trb_buff_len) |
04e51901 3640 TRB_INTR_TARGET(0);
4da6e6f2 3641
2f6d3b65
MN
3642 /* xhci 1.1 with ETE uses TD Size field for TBC */
3643 if (first_trb && xep->use_extended_tbc)
3644 length_field |= TRB_TD_SIZE_TBC(burst_count);
3645 else
3646 length_field |= TRB_TD_SIZE(remainder);
3647 first_trb = false;
3648
3b72fca0 3649 queue_trb(xhci, ep_ring, more_trbs_coming,
04e51901
AX
3650 lower_32_bits(addr),
3651 upper_32_bits(addr),
3652 length_field,
af8b9e63 3653 field);
04e51901
AX
3654 running_total += trb_buff_len;
3655
3656 addr += trb_buff_len;
3657 td_remain_len -= trb_buff_len;
3658 }
3659
3660 /* Check TD length */
3661 if (running_total != td_len) {
3662 xhci_err(xhci, "ISOC TD length unmatch\n");
cf840551
AX
3663 ret = -EINVAL;
3664 goto cleanup;
04e51901
AX
3665 }
3666 }
3667
79b8094f
LB
3668 /* store the next frame id */
3669 if (HCC_CFC(xhci->hcc_params))
3670 xep->next_frame_id = urb->start_frame + num_tds * urb->interval;
3671
c41136b0
AX
3672 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
3673 if (xhci->quirks & XHCI_AMD_PLL_FIX)
3674 usb_amd_quirk_pll_disable();
3675 }
3676 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
3677
e1eab2e0
AX
3678 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3679 start_cycle, start_trb);
04e51901 3680 return 0;
522989a2
SS
3681cleanup:
3682 /* Clean up a partially enqueued isoc transfer. */
3683
3684 for (i--; i >= 0; i--)
585df1d9 3685 list_del_init(&urb_priv->td[i]->td_list);
522989a2
SS
3686
3687 /* Use the first TD as a temporary variable to turn the TDs we've queued
3688 * into No-ops with a software-owned cycle bit. That way the hardware
3689 * won't accidentally start executing bogus TDs when we partially
3690 * overwrite them. td->first_trb and td->start_seg are already set.
3691 */
3692 urb_priv->td[0]->last_trb = ep_ring->enqueue;
3693 /* Every TRB except the first & last will have its cycle bit flipped. */
3694 td_to_noop(xhci, ep_ring, urb_priv->td[0], true);
3695
3696 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
3697 ep_ring->enqueue = urb_priv->td[0]->first_trb;
3698 ep_ring->enq_seg = urb_priv->td[0]->start_seg;
3699 ep_ring->cycle_state = start_cycle;
b008df60 3700 ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp;
522989a2
SS
3701 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
3702 return ret;
04e51901
AX
3703}
3704
3705/*
3706 * Check transfer ring to guarantee there is enough room for the urb.
3707 * Update ISO URB start_frame and interval.
79b8094f
LB
3708 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to
3709 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or
3710 * Contiguous Frame ID is not supported by HC.
04e51901
AX
3711 */
3712int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3713 struct urb *urb, int slot_id, unsigned int ep_index)
3714{
3715 struct xhci_virt_device *xdev;
3716 struct xhci_ring *ep_ring;
3717 struct xhci_ep_ctx *ep_ctx;
3718 int start_frame;
04e51901
AX
3719 int num_tds, num_trbs, i;
3720 int ret;
79b8094f
LB
3721 struct xhci_virt_ep *xep;
3722 int ist;
04e51901
AX
3723
3724 xdev = xhci->devs[slot_id];
79b8094f 3725 xep = &xhci->devs[slot_id]->eps[ep_index];
04e51901
AX
3726 ep_ring = xdev->eps[ep_index].ring;
3727 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3728
3729 num_trbs = 0;
3730 num_tds = urb->number_of_packets;
3731 for (i = 0; i < num_tds; i++)
d2510342 3732 num_trbs += count_isoc_trbs_needed(urb, i);
04e51901
AX
3733
3734 /* Check the ring to guarantee there is enough room for the whole urb.
3735 * Do not insert any td of the urb to the ring if the check failed.
3736 */
28ccd296 3737 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
3b72fca0 3738 num_trbs, mem_flags);
04e51901
AX
3739 if (ret)
3740 return ret;
3741
79b8094f
LB
3742 /*
3743 * Check interval value. This should be done before we start to
3744 * calculate the start frame value.
3745 */
78140156 3746 check_interval(xhci, urb, ep_ctx);
79b8094f
LB
3747
3748 /* Calculate the start frame and put it in urb->start_frame. */
42df7215
LB
3749 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) {
3750 if ((le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
3751 EP_STATE_RUNNING) {
3752 urb->start_frame = xep->next_frame_id;
3753 goto skip_start_over;
3754 }
79b8094f
LB
3755 }
3756
3757 start_frame = readl(&xhci->run_regs->microframe_index);
3758 start_frame &= 0x3fff;
3759 /*
3760 * Round up to the next frame and consider the time before trb really
3761 * gets scheduled by hardare.
3762 */
3763 ist = HCS_IST(xhci->hcs_params2) & 0x7;
3764 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
3765 ist <<= 3;
3766 start_frame += ist + XHCI_CFC_DELAY;
3767 start_frame = roundup(start_frame, 8);
3768
3769 /*
3770 * Round up to the next ESIT (Endpoint Service Interval Time) if ESIT
3771 * is greate than 8 microframes.
3772 */
3773 if (urb->dev->speed == USB_SPEED_LOW ||
3774 urb->dev->speed == USB_SPEED_FULL) {
3775 start_frame = roundup(start_frame, urb->interval << 3);
3776 urb->start_frame = start_frame >> 3;
3777 } else {
3778 start_frame = roundup(start_frame, urb->interval);
3779 urb->start_frame = start_frame;
3780 }
3781
3782skip_start_over:
b008df60
AX
3783 ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free;
3784
3fc8206d 3785 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
04e51901
AX
3786}
3787
d0e96f5a
SS
3788/**** Command Ring Operations ****/
3789
913a8a34
SS
3790/* Generic function for queueing a command TRB on the command ring.
3791 * Check to make sure there's room on the command ring for one command TRB.
3792 * Also check that there's room reserved for commands that must not fail.
3793 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3794 * then only check for the number of reserved spots.
3795 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3796 * because the command event handler may want to resubmit a failed command.
3797 */
ddba5cd0
MN
3798static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
3799 u32 field1, u32 field2,
3800 u32 field3, u32 field4, bool command_must_succeed)
7f84eef0 3801{
913a8a34 3802 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
d1dc908a 3803 int ret;
ad6b1d91 3804
98d74f9c
MN
3805 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
3806 (xhci->xhc_state & XHCI_STATE_HALTED)) {
ad6b1d91 3807 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n");
c9aa1a2d 3808 return -ESHUTDOWN;
ad6b1d91 3809 }
d1dc908a 3810
913a8a34
SS
3811 if (!command_must_succeed)
3812 reserved_trbs++;
3813
d1dc908a 3814 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
3b72fca0 3815 reserved_trbs, GFP_ATOMIC);
d1dc908a
SS
3816 if (ret < 0) {
3817 xhci_err(xhci, "ERR: No room for command on command ring\n");
913a8a34
SS
3818 if (command_must_succeed)
3819 xhci_err(xhci, "ERR: Reserved TRB counting for "
3820 "unfailable commands failed.\n");
d1dc908a 3821 return ret;
7f84eef0 3822 }
c9aa1a2d
MN
3823
3824 cmd->command_trb = xhci->cmd_ring->enqueue;
3825 list_add_tail(&cmd->cmd_list, &xhci->cmd_list);
ddba5cd0 3826
c311e391
MN
3827 /* if there are no other commands queued we start the timeout timer */
3828 if (xhci->cmd_list.next == &cmd->cmd_list &&
3829 !timer_pending(&xhci->cmd_timer)) {
3830 xhci->current_cmd = cmd;
3831 mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
3832 }
3833
3b72fca0
AX
3834 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
3835 field4 | xhci->cmd_ring->cycle_state);
7f84eef0
SS
3836 return 0;
3837}
3838
3ffbba95 3839/* Queue a slot enable or disable request on the command ring */
ddba5cd0
MN
3840int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
3841 u32 trb_type, u32 slot_id)
3ffbba95 3842{
ddba5cd0 3843 return queue_command(xhci, cmd, 0, 0, 0,
913a8a34 3844 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
3ffbba95
SS
3845}
3846
3847/* Queue an address device command TRB */
ddba5cd0
MN
3848int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
3849 dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup)
3ffbba95 3850{
ddba5cd0 3851 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
8e595a5d 3852 upper_32_bits(in_ctx_ptr), 0,
48fc7dbd
DW
3853 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id)
3854 | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false);
2a8f82c4
SS
3855}
3856
ddba5cd0 3857int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
0238634d
SS
3858 u32 field1, u32 field2, u32 field3, u32 field4)
3859{
ddba5cd0 3860 return queue_command(xhci, cmd, field1, field2, field3, field4, false);
0238634d
SS
3861}
3862
2a8f82c4 3863/* Queue a reset device command TRB */
ddba5cd0
MN
3864int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
3865 u32 slot_id)
2a8f82c4 3866{
ddba5cd0 3867 return queue_command(xhci, cmd, 0, 0, 0,
2a8f82c4 3868 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
913a8a34 3869 false);
3ffbba95 3870}
f94e0186
SS
3871
3872/* Queue a configure endpoint command TRB */
ddba5cd0
MN
3873int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
3874 struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
913a8a34 3875 u32 slot_id, bool command_must_succeed)
f94e0186 3876{
ddba5cd0 3877 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
8e595a5d 3878 upper_32_bits(in_ctx_ptr), 0,
913a8a34
SS
3879 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3880 command_must_succeed);
f94e0186 3881}
ae636747 3882
f2217e8e 3883/* Queue an evaluate context command TRB */
ddba5cd0
MN
3884int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
3885 dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed)
f2217e8e 3886{
ddba5cd0 3887 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
f2217e8e 3888 upper_32_bits(in_ctx_ptr), 0,
913a8a34 3889 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
4b266541 3890 command_must_succeed);
f2217e8e
SS
3891}
3892
be88fe4f
AX
3893/*
3894 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3895 * activity on an endpoint that is about to be suspended.
3896 */
ddba5cd0
MN
3897int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
3898 int slot_id, unsigned int ep_index, int suspend)
ae636747
SS
3899{
3900 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3901 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3902 u32 type = TRB_TYPE(TRB_STOP_RING);
be88fe4f 3903 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
ae636747 3904
ddba5cd0 3905 return queue_command(xhci, cmd, 0, 0, 0,
be88fe4f 3906 trb_slot_id | trb_ep_index | type | trb_suspend, false);
ae636747
SS
3907}
3908
d3a43e66
HG
3909/* Set Transfer Ring Dequeue Pointer command */
3910void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
3911 unsigned int slot_id, unsigned int ep_index,
3912 unsigned int stream_id,
3913 struct xhci_dequeue_state *deq_state)
ae636747
SS
3914{
3915 dma_addr_t addr;
3916 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3917 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
e9df17eb 3918 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
95241dbd 3919 u32 trb_sct = 0;
ae636747 3920 u32 type = TRB_TYPE(TRB_SET_DEQ);
bf161e85 3921 struct xhci_virt_ep *ep;
1e3452e3
HG
3922 struct xhci_command *cmd;
3923 int ret;
ae636747 3924
d3a43e66
HG
3925 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
3926 "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), new deq ptr = %p (0x%llx dma), new cycle = %u",
3927 deq_state->new_deq_seg,
3928 (unsigned long long)deq_state->new_deq_seg->dma,
3929 deq_state->new_deq_ptr,
3930 (unsigned long long)xhci_trb_virt_to_dma(
3931 deq_state->new_deq_seg, deq_state->new_deq_ptr),
3932 deq_state->new_cycle_state);
3933
3934 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
3935 deq_state->new_deq_ptr);
c92bcfa7 3936 if (addr == 0) {
ae636747 3937 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
700e2052 3938 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
d3a43e66
HG
3939 deq_state->new_deq_seg, deq_state->new_deq_ptr);
3940 return;
c92bcfa7 3941 }
bf161e85
SS
3942 ep = &xhci->devs[slot_id]->eps[ep_index];
3943 if ((ep->ep_state & SET_DEQ_PENDING)) {
3944 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
3945 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
d3a43e66 3946 return;
bf161e85 3947 }
1e3452e3
HG
3948
3949 /* This function gets called from contexts where it cannot sleep */
3950 cmd = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
3951 if (!cmd) {
3952 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr: ENOMEM\n");
d3a43e66 3953 return;
1e3452e3
HG
3954 }
3955
d3a43e66
HG
3956 ep->queued_deq_seg = deq_state->new_deq_seg;
3957 ep->queued_deq_ptr = deq_state->new_deq_ptr;
95241dbd
HG
3958 if (stream_id)
3959 trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
1e3452e3 3960 ret = queue_command(xhci, cmd,
d3a43e66
HG
3961 lower_32_bits(addr) | trb_sct | deq_state->new_cycle_state,
3962 upper_32_bits(addr), trb_stream_id,
3963 trb_slot_id | trb_ep_index | type, false);
1e3452e3
HG
3964 if (ret < 0) {
3965 xhci_free_command(xhci, cmd);
d3a43e66 3966 return;
1e3452e3
HG
3967 }
3968
d3a43e66
HG
3969 /* Stop the TD queueing code from ringing the doorbell until
3970 * this command completes. The HC won't set the dequeue pointer
3971 * if the ring is running, and ringing the doorbell starts the
3972 * ring running.
3973 */
3974 ep->ep_state |= SET_DEQ_PENDING;
ae636747 3975}
a1587d97 3976
ddba5cd0
MN
3977int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
3978 int slot_id, unsigned int ep_index)
a1587d97
SS
3979{
3980 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3981 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3982 u32 type = TRB_TYPE(TRB_RESET_EP);
3983
ddba5cd0
MN
3984 return queue_command(xhci, cmd, 0, 0, 0,
3985 trb_slot_id | trb_ep_index | type, false);
a1587d97 3986}