1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * xHCI host controller driver
4 *
5 * Copyright (C) 2008 Intel Corp.
6 *
7 * Author: Sarah Sharp
8 * Some code borrowed from the Linux EHCI driver.
9 */
10
11 /*
12 * Ring initialization rules:
13 * 1. Each segment is initialized to zero, except for link TRBs.
14 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
15 * Consumer Cycle State (CCS), depending on ring function.
16 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
17 *
18 * Ring behavior rules:
19 * 1. A ring is empty if enqueue == dequeue. This means there will always be at
20 * least one free TRB in the ring. This is useful if you want to turn that
21 * into a link TRB and expand the ring.
22 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
23 * link TRB, then load the pointer with the address in the link TRB. If the
24 * link TRB had its toggle bit set, you may need to update the ring cycle
25 * state (see cycle bit rules). You may have to do this multiple times
26 * until you reach a non-link TRB.
27 * 3. A ring is full if enqueue++ (for the definition of increment above)
28 * equals the dequeue pointer.
29 *
30 * Cycle bit rules:
31 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
32 * in a link TRB, it must toggle the ring cycle state.
33 * 2. When a producer increments an enqueue pointer and encounters a toggle bit
34 * in a link TRB, it must toggle the ring cycle state.
35 *
36 * Producer rules:
37 * 1. Check if ring is full before you enqueue.
38 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
39 * Update enqueue pointer between each write (which may update the ring
40 * cycle state).
41 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
42 * and endpoint rings. If HC is the producer for the event ring,
43 * and it generates an interrupt according to interrupt modulation rules.
44 *
45 * Consumer rules:
46 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
47 * the TRB is owned by the consumer.
48 * 2. Update dequeue pointer (which may update the ring cycle state) and
49 * continue processing TRBs until you reach a TRB which is not owned by you.
50 * 3. Notify the producer. SW is the consumer for the event ring, and it
51 * updates event ring dequeue pointer. HC is the consumer for the command and
52 * endpoint rings; it generates events on the event ring for these.
53 */
54
55 #include <linux/jiffies.h>
56 #include <linux/scatterlist.h>
57 #include <linux/slab.h>
58 #include <linux/string_choices.h>
59 #include <linux/dma-mapping.h>
60 #include "xhci.h"
61 #include "xhci-trace.h"
62
63 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
64 u32 field1, u32 field2,
65 u32 field3, u32 field4, bool command_must_succeed);
66
67 /*
68 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
69 * address of the TRB.
70 */
xhci_trb_virt_to_dma(struct xhci_segment * seg,union xhci_trb * trb)71 dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
72 union xhci_trb *trb)
73 {
74 unsigned long segment_offset;
75
76 if (!seg || !trb || trb < seg->trbs)
77 return 0;
78 /* offset in TRBs */
79 segment_offset = trb - seg->trbs;
80 if (segment_offset >= TRBS_PER_SEGMENT)
81 return 0;
82 return seg->dma + (segment_offset * sizeof(*trb));
83 }
84
trb_is_noop(union xhci_trb * trb)85 static bool trb_is_noop(union xhci_trb *trb)
86 {
87 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]);
88 }
89
trb_is_link(union xhci_trb * trb)90 static bool trb_is_link(union xhci_trb *trb)
91 {
92 return TRB_TYPE_LINK_LE32(trb->link.control);
93 }
94
last_trb_on_seg(struct xhci_segment * seg,union xhci_trb * trb)95 static bool last_trb_on_seg(struct xhci_segment *seg, union xhci_trb *trb)
96 {
97 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1];
98 }
99
last_trb_on_ring(struct xhci_ring * ring,struct xhci_segment * seg,union xhci_trb * trb)100 static bool last_trb_on_ring(struct xhci_ring *ring,
101 struct xhci_segment *seg, union xhci_trb *trb)
102 {
103 return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg);
104 }
105
link_trb_toggles_cycle(union xhci_trb * trb)106 static bool link_trb_toggles_cycle(union xhci_trb *trb)
107 {
108 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
109 }
110
last_td_in_urb(struct xhci_td * td)111 static bool last_td_in_urb(struct xhci_td *td)
112 {
113 struct urb_priv *urb_priv = td->urb->hcpriv;
114
115 return urb_priv->num_tds_done == urb_priv->num_tds;
116 }
117
unhandled_event_trb(struct xhci_ring * ring)118 static bool unhandled_event_trb(struct xhci_ring *ring)
119 {
120 return ((le32_to_cpu(ring->dequeue->event_cmd.flags) & TRB_CYCLE) ==
121 ring->cycle_state);
122 }
123
inc_td_cnt(struct urb * urb)124 static void inc_td_cnt(struct urb *urb)
125 {
126 struct urb_priv *urb_priv = urb->hcpriv;
127
128 urb_priv->num_tds_done++;
129 }
130
trb_to_noop(union xhci_trb * trb,u32 noop_type)131 static void trb_to_noop(union xhci_trb *trb, u32 noop_type)
132 {
133 if (trb_is_link(trb)) {
134 /* unchain chained link TRBs */
135 trb->link.control &= cpu_to_le32(~TRB_CHAIN);
136 } else {
137 trb->generic.field[0] = 0;
138 trb->generic.field[1] = 0;
139 trb->generic.field[2] = 0;
140 /* Preserve only the cycle bit of this TRB */
141 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
142 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type));
143 }
144 }
145
146 /* Updates trb to point to the next TRB in the ring, and updates seg if the next
147 * TRB is in a new segment. This does not skip over link TRBs, and it does not
148 * effect the ring dequeue or enqueue pointers.
149 */
next_trb(struct xhci_segment ** seg,union xhci_trb ** trb)150 static void next_trb(struct xhci_segment **seg,
151 union xhci_trb **trb)
152 {
153 if (trb_is_link(*trb) || last_trb_on_seg(*seg, *trb)) {
154 *seg = (*seg)->next;
155 *trb = ((*seg)->trbs);
156 } else {
157 (*trb)++;
158 }
159 }
160
161 /*
162 * See Cycle bit rules. SW is the consumer for the event ring only.
163 */
inc_deq(struct xhci_hcd * xhci,struct xhci_ring * ring)164 void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
165 {
166 unsigned int link_trb_count = 0;
167
168 /* event ring doesn't have link trbs, check for last trb */
169 if (ring->type == TYPE_EVENT) {
170 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
171 ring->dequeue++;
172 return;
173 }
174 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue))
175 ring->cycle_state ^= 1;
176 ring->deq_seg = ring->deq_seg->next;
177 ring->dequeue = ring->deq_seg->trbs;
178
179 trace_xhci_inc_deq(ring);
180
181 return;
182 }
183
184 /* All other rings have link trbs */
185 if (!trb_is_link(ring->dequeue)) {
186 if (last_trb_on_seg(ring->deq_seg, ring->dequeue))
187 xhci_warn(xhci, "Missing link TRB at end of segment\n");
188 else
189 ring->dequeue++;
190 }
191
192 while (trb_is_link(ring->dequeue)) {
193 ring->deq_seg = ring->deq_seg->next;
194 ring->dequeue = ring->deq_seg->trbs;
195
196 trace_xhci_inc_deq(ring);
197
198 if (link_trb_count++ > ring->num_segs) {
199 xhci_warn(xhci, "Ring is an endless link TRB loop\n");
200 break;
201 }
202 }
203 return;
204 }
205
206 /*
207 * If enqueue points at a link TRB, follow links until an ordinary TRB is reached.
208 * Toggle the cycle bit of passed link TRBs and optionally chain them.
209 */
inc_enq_past_link(struct xhci_hcd * xhci,struct xhci_ring * ring,u32 chain)210 static void inc_enq_past_link(struct xhci_hcd *xhci, struct xhci_ring *ring, u32 chain)
211 {
212 unsigned int link_trb_count = 0;
213
214 while (trb_is_link(ring->enqueue)) {
215
216 /*
217 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
218 * set, but other sections talk about dealing with the chain bit set. This was
219 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
220 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
221 *
222 * On 0.95 and some 0.96 HCs the chain bit is set once at segment initalization
223 * and never changed here. On all others, modify it as requested by the caller.
224 */
225 if (!xhci_link_chain_quirk(xhci, ring->type)) {
226 ring->enqueue->link.control &= cpu_to_le32(~TRB_CHAIN);
227 ring->enqueue->link.control |= cpu_to_le32(chain);
228 }
229
230 /* Give this link TRB to the hardware */
231 wmb();
232 ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE);
233
234 /* Toggle the cycle bit after the last ring segment. */
235 if (link_trb_toggles_cycle(ring->enqueue))
236 ring->cycle_state ^= 1;
237
238 ring->enq_seg = ring->enq_seg->next;
239 ring->enqueue = ring->enq_seg->trbs;
240
241 trace_xhci_inc_enq(ring);
242
243 if (link_trb_count++ > ring->num_segs) {
244 xhci_warn(xhci, "Link TRB loop at enqueue\n");
245 break;
246 }
247 }
248 }
249
250 /*
251 * See Cycle bit rules. SW is the consumer for the event ring only.
252 *
253 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
254 * chain bit is set), then set the chain bit in all the following link TRBs.
255 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
256 * have their chain bit cleared (so that each Link TRB is a separate TD).
257 *
258 * @more_trbs_coming: Will you enqueue more TRBs before calling
259 * prepare_transfer()?
260 */
inc_enq(struct xhci_hcd * xhci,struct xhci_ring * ring,bool more_trbs_coming)261 static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
262 bool more_trbs_coming)
263 {
264 u32 chain;
265
266 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
267
268 if (last_trb_on_seg(ring->enq_seg, ring->enqueue)) {
269 xhci_err(xhci, "Tried to move enqueue past ring segment\n");
270 return;
271 }
272
273 ring->enqueue++;
274
275 /*
276 * If we are in the middle of a TD or the caller plans to enqueue more
277 * TDs as one transfer (eg. control), traverse any link TRBs right now.
278 * Otherwise, enqueue can stay on a link until the next prepare_ring().
279 * This avoids enqueue entering deq_seg and simplifies ring expansion.
280 */
281 if (trb_is_link(ring->enqueue) && (chain || more_trbs_coming))
282 inc_enq_past_link(xhci, ring, chain);
283 }
284
285 /*
286 * If the suspect DMA address is a TRB in this TD, this function returns that
287 * TRB's segment. Otherwise it returns 0.
288 */
trb_in_td(struct xhci_td * td,dma_addr_t suspect_dma)289 static struct xhci_segment *trb_in_td(struct xhci_td *td, dma_addr_t suspect_dma)
290 {
291 dma_addr_t start_dma;
292 dma_addr_t end_seg_dma;
293 dma_addr_t end_trb_dma;
294 struct xhci_segment *cur_seg;
295
296 start_dma = xhci_trb_virt_to_dma(td->start_seg, td->start_trb);
297 cur_seg = td->start_seg;
298
299 do {
300 if (start_dma == 0)
301 return NULL;
302 /* We may get an event for a Link TRB in the middle of a TD */
303 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
304 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
305 /* If the end TRB isn't in this segment, this is set to 0 */
306 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, td->end_trb);
307
308 if (end_trb_dma > 0) {
309 /* The end TRB is in this segment, so suspect should be here */
310 if (start_dma <= end_trb_dma) {
311 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
312 return cur_seg;
313 } else {
314 /* Case for one segment with
315 * a TD wrapped around to the top
316 */
317 if ((suspect_dma >= start_dma &&
318 suspect_dma <= end_seg_dma) ||
319 (suspect_dma >= cur_seg->dma &&
320 suspect_dma <= end_trb_dma))
321 return cur_seg;
322 }
323 return NULL;
324 }
325 /* Might still be somewhere in this segment */
326 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
327 return cur_seg;
328
329 cur_seg = cur_seg->next;
330 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
331 } while (cur_seg != td->start_seg);
332
333 return NULL;
334 }
335
336 /*
337 * Return number of free normal TRBs from enqueue to dequeue pointer on ring.
338 * Not counting an assumed link TRB at end of each TRBS_PER_SEGMENT sized segment.
339 * Only for transfer and command rings where driver is the producer, not for
340 * event rings.
341 */
xhci_num_trbs_free(struct xhci_ring * ring)342 static unsigned int xhci_num_trbs_free(struct xhci_ring *ring)
343 {
344 struct xhci_segment *enq_seg = ring->enq_seg;
345 union xhci_trb *enq = ring->enqueue;
346 union xhci_trb *last_on_seg;
347 unsigned int free = 0;
348 int i = 0;
349
350 /* Ring might be empty even if enq != deq if enq is left on a link trb */
351 if (trb_is_link(enq)) {
352 enq_seg = enq_seg->next;
353 enq = enq_seg->trbs;
354 }
355
356 /* Empty ring, common case, don't walk the segments */
357 if (enq == ring->dequeue)
358 return ring->num_segs * (TRBS_PER_SEGMENT - 1);
359
360 do {
361 if (ring->deq_seg == enq_seg && ring->dequeue >= enq)
362 return free + (ring->dequeue - enq);
363 last_on_seg = &enq_seg->trbs[TRBS_PER_SEGMENT - 1];
364 free += last_on_seg - enq;
365 enq_seg = enq_seg->next;
366 enq = enq_seg->trbs;
367 } while (i++ < ring->num_segs);
368
369 return free;
370 }
371
372 /*
373 * Check to see if there's room to enqueue num_trbs on the ring and make sure
374 * enqueue pointer will not advance into dequeue segment. See rules above.
375 * return number of new segments needed to ensure this.
376 */
377
xhci_ring_expansion_needed(struct xhci_hcd * xhci,struct xhci_ring * ring,unsigned int num_trbs)378 static unsigned int xhci_ring_expansion_needed(struct xhci_hcd *xhci, struct xhci_ring *ring,
379 unsigned int num_trbs)
380 {
381 struct xhci_segment *seg;
382 int trbs_past_seg;
383 int enq_used;
384 int new_segs;
385
386 enq_used = ring->enqueue - ring->enq_seg->trbs;
387
388 /* how many trbs will be queued past the enqueue segment? */
389 trbs_past_seg = enq_used + num_trbs - (TRBS_PER_SEGMENT - 1);
390
391 /*
392 * Consider expanding the ring already if num_trbs fills the current
393 * segment (i.e. trbs_past_seg == 0), not only when num_trbs goes into
394 * the next segment. Avoids confusing full ring with special empty ring
395 * case below
396 */
397 if (trbs_past_seg < 0)
398 return 0;
399
400 /* Empty ring special case, enqueue stuck on link trb while dequeue advanced */
401 if (trb_is_link(ring->enqueue) && ring->enq_seg->next->trbs == ring->dequeue)
402 return 0;
403
404 new_segs = 1 + (trbs_past_seg / (TRBS_PER_SEGMENT - 1));
405 seg = ring->enq_seg;
406
407 while (new_segs > 0) {
408 seg = seg->next;
409 if (seg == ring->deq_seg) {
410 xhci_dbg(xhci, "Adding %d trbs requires expanding ring by %d segments\n",
411 num_trbs, new_segs);
412 return new_segs;
413 }
414 new_segs--;
415 }
416
417 return 0;
418 }
419
420 /* Ring the host controller doorbell after placing a command on the ring */
xhci_ring_cmd_db(struct xhci_hcd * xhci)421 void xhci_ring_cmd_db(struct xhci_hcd *xhci)
422 {
423 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING))
424 return;
425
426 xhci_dbg(xhci, "// Ding dong!\n");
427
428 trace_xhci_ring_host_doorbell(0, DB_VALUE_HOST);
429
430 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]);
431 /* Flush PCI posted writes */
432 readl(&xhci->dba->doorbell[0]);
433 }
434
xhci_mod_cmd_timer(struct xhci_hcd * xhci)435 static bool xhci_mod_cmd_timer(struct xhci_hcd *xhci)
436 {
437 return mod_delayed_work(system_wq, &xhci->cmd_timer,
438 msecs_to_jiffies(xhci->current_cmd->timeout_ms));
439 }
440
xhci_next_queued_cmd(struct xhci_hcd * xhci)441 static struct xhci_command *xhci_next_queued_cmd(struct xhci_hcd *xhci)
442 {
443 return list_first_entry_or_null(&xhci->cmd_list, struct xhci_command,
444 cmd_list);
445 }
446
447 /*
448 * Turn all commands on command ring with status set to "aborted" to no-op trbs.
449 * If there are other commands waiting then restart the ring and kick the timer.
450 * This must be called with command ring stopped and xhci->lock held.
451 */
xhci_handle_stopped_cmd_ring(struct xhci_hcd * xhci,struct xhci_command * cur_cmd)452 static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci,
453 struct xhci_command *cur_cmd)
454 {
455 struct xhci_command *i_cmd;
456
457 /* Turn all aborted commands in list to no-ops, then restart */
458 list_for_each_entry(i_cmd, &xhci->cmd_list, cmd_list) {
459
460 if (i_cmd->status != COMP_COMMAND_ABORTED)
461 continue;
462
463 i_cmd->status = COMP_COMMAND_RING_STOPPED;
464
465 xhci_dbg(xhci, "Turn aborted command %p to no-op\n",
466 i_cmd->command_trb);
467
468 trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP);
469
470 /*
471 * caller waiting for completion is called when command
472 * completion event is received for these no-op commands
473 */
474 }
475
476 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
477
478 /* ring command ring doorbell to restart the command ring */
479 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) &&
480 !(xhci->xhc_state & XHCI_STATE_DYING)) {
481 xhci->current_cmd = cur_cmd;
482 if (cur_cmd)
483 xhci_mod_cmd_timer(xhci);
484 xhci_ring_cmd_db(xhci);
485 }
486 }
487
488 /* Must be called with xhci->lock held, releases and acquires lock back */
xhci_abort_cmd_ring(struct xhci_hcd * xhci,unsigned long flags)489 static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags)
490 {
491 struct xhci_segment *new_seg = xhci->cmd_ring->deq_seg;
492 union xhci_trb *new_deq = xhci->cmd_ring->dequeue;
493 u64 crcr;
494 int ret;
495
496 xhci_dbg(xhci, "Abort command ring\n");
497
498 reinit_completion(&xhci->cmd_ring_stop_completion);
499
500 /*
501 * The control bits like command stop, abort are located in lower
502 * dword of the command ring control register.
503 * Some controllers require all 64 bits to be written to abort the ring.
504 * Make sure the upper dword is valid, pointing to the next command,
505 * avoiding corrupting the command ring pointer in case the command ring
506 * is stopped by the time the upper dword is written.
507 */
508 next_trb(&new_seg, &new_deq);
509 if (trb_is_link(new_deq))
510 next_trb(&new_seg, &new_deq);
511
512 crcr = xhci_trb_virt_to_dma(new_seg, new_deq);
513 xhci_write_64(xhci, crcr | CMD_RING_ABORT, &xhci->op_regs->cmd_ring);
514
515 /* Section 4.6.1.2 of xHCI 1.0 spec says software should also time the
516 * completion of the Command Abort operation. If CRR is not negated in 5
517 * seconds then driver handles it as if host died (-ENODEV).
518 * In the future we should distinguish between -ENODEV and -ETIMEDOUT
519 * and try to recover a -ETIMEDOUT with a host controller reset.
520 */
521 ret = xhci_handshake_check_state(xhci, &xhci->op_regs->cmd_ring,
522 CMD_RING_RUNNING, 0, 5 * 1000 * 1000,
523 XHCI_STATE_REMOVING);
524 if (ret < 0) {
525 xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret);
526 xhci_halt(xhci);
527 xhci_hc_died(xhci);
528 return ret;
529 }
530 /*
531 * Writing the CMD_RING_ABORT bit should cause a cmd completion event,
532 * however on some host hw the CMD_RING_RUNNING bit is correctly cleared
533 * but the completion event in never sent. Wait 2 secs (arbitrary
534 * number) to handle those cases after negation of CMD_RING_RUNNING.
535 */
536 spin_unlock_irqrestore(&xhci->lock, flags);
537 ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion,
538 msecs_to_jiffies(2000));
539 spin_lock_irqsave(&xhci->lock, flags);
540 if (!ret) {
541 xhci_dbg(xhci, "No stop event for abort, ring start fail?\n");
542 xhci_cleanup_command_queue(xhci);
543 } else {
544 xhci_handle_stopped_cmd_ring(xhci, xhci_next_queued_cmd(xhci));
545 }
546 return 0;
547 }
548
xhci_ring_ep_doorbell(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index,unsigned int stream_id)549 void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
550 unsigned int slot_id,
551 unsigned int ep_index,
552 unsigned int stream_id)
553 {
554 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
555 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
556 unsigned int ep_state = ep->ep_state;
557
558 /* Don't ring the doorbell for this endpoint if there are pending
559 * cancellations because we don't want to interrupt processing.
560 * We don't want to restart any stream rings if there's a set dequeue
561 * pointer command pending because the device can choose to start any
562 * stream once the endpoint is on the HW schedule.
563 */
564 if ((ep_state & EP_STOP_CMD_PENDING) || (ep_state & SET_DEQ_PENDING) ||
565 (ep_state & EP_HALTED) || (ep_state & EP_CLEARING_TT))
566 return;
567
568 trace_xhci_ring_ep_doorbell(slot_id, DB_VALUE(ep_index, stream_id));
569
570 writel(DB_VALUE(ep_index, stream_id), db_addr);
571 /* flush the write */
572 readl(db_addr);
573 }
574
575 /* Ring the doorbell for any rings with pending URBs */
ring_doorbell_for_active_rings(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index)576 static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
577 unsigned int slot_id,
578 unsigned int ep_index)
579 {
580 unsigned int stream_id;
581 struct xhci_virt_ep *ep;
582
583 ep = &xhci->devs[slot_id]->eps[ep_index];
584
585 /* A ring has pending URBs if its TD list is not empty */
586 if (!(ep->ep_state & EP_HAS_STREAMS)) {
587 if (ep->ring && !(list_empty(&ep->ring->td_list)))
588 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
589 return;
590 }
591
592 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
593 stream_id++) {
594 struct xhci_stream_info *stream_info = ep->stream_info;
595 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
596 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
597 stream_id);
598 }
599 }
600
xhci_ring_doorbell_for_active_rings(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index)601 void xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
602 unsigned int slot_id,
603 unsigned int ep_index)
604 {
605 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
606 }
607
xhci_get_virt_ep(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index)608 static struct xhci_virt_ep *xhci_get_virt_ep(struct xhci_hcd *xhci,
609 unsigned int slot_id,
610 unsigned int ep_index)
611 {
612 if (slot_id == 0 || slot_id >= MAX_HC_SLOTS) {
613 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id);
614 return NULL;
615 }
616 if (ep_index >= EP_CTX_PER_DEV) {
617 xhci_warn(xhci, "Invalid endpoint index %u\n", ep_index);
618 return NULL;
619 }
620 if (!xhci->devs[slot_id]) {
621 xhci_warn(xhci, "No xhci virt device for slot_id %u\n", slot_id);
622 return NULL;
623 }
624
625 return &xhci->devs[slot_id]->eps[ep_index];
626 }
627
xhci_virt_ep_to_ring(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,unsigned int stream_id)628 static struct xhci_ring *xhci_virt_ep_to_ring(struct xhci_hcd *xhci,
629 struct xhci_virt_ep *ep,
630 unsigned int stream_id)
631 {
632 /* common case, no streams */
633 if (!(ep->ep_state & EP_HAS_STREAMS))
634 return ep->ring;
635
636 if (!ep->stream_info)
637 return NULL;
638
639 if (stream_id == 0 || stream_id >= ep->stream_info->num_streams) {
640 xhci_warn(xhci, "Invalid stream_id %u request for slot_id %u ep_index %u\n",
641 stream_id, ep->vdev->slot_id, ep->ep_index);
642 return NULL;
643 }
644
645 return ep->stream_info->stream_rings[stream_id];
646 }
647
648 /* Get the right ring for the given slot_id, ep_index and stream_id.
649 * If the endpoint supports streams, boundary check the URB's stream ID.
650 * If the endpoint doesn't support streams, return the singular endpoint ring.
651 */
xhci_triad_to_transfer_ring(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index,unsigned int stream_id)652 struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
653 unsigned int slot_id, unsigned int ep_index,
654 unsigned int stream_id)
655 {
656 struct xhci_virt_ep *ep;
657
658 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
659 if (!ep)
660 return NULL;
661
662 return xhci_virt_ep_to_ring(xhci, ep, stream_id);
663 }
664
665
666 /*
667 * Get the hw dequeue pointer xHC stopped on, either directly from the
668 * endpoint context, or if streams are in use from the stream context.
669 * The returned hw_dequeue contains the lowest four bits with cycle state
670 * and possbile stream context type.
671 */
xhci_get_hw_deq(struct xhci_hcd * xhci,struct xhci_virt_device * vdev,unsigned int ep_index,unsigned int stream_id)672 static u64 xhci_get_hw_deq(struct xhci_hcd *xhci, struct xhci_virt_device *vdev,
673 unsigned int ep_index, unsigned int stream_id)
674 {
675 struct xhci_ep_ctx *ep_ctx;
676 struct xhci_stream_ctx *st_ctx;
677 struct xhci_virt_ep *ep;
678
679 ep = &vdev->eps[ep_index];
680
681 if (ep->ep_state & EP_HAS_STREAMS) {
682 st_ctx = &ep->stream_info->stream_ctx_array[stream_id];
683 return le64_to_cpu(st_ctx->stream_ring);
684 }
685 ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index);
686 return le64_to_cpu(ep_ctx->deq);
687 }
688
xhci_move_dequeue_past_td(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index,unsigned int stream_id,struct xhci_td * td)689 static int xhci_move_dequeue_past_td(struct xhci_hcd *xhci,
690 unsigned int slot_id, unsigned int ep_index,
691 unsigned int stream_id, struct xhci_td *td)
692 {
693 struct xhci_virt_device *dev = xhci->devs[slot_id];
694 struct xhci_virt_ep *ep = &dev->eps[ep_index];
695 struct xhci_ring *ep_ring;
696 struct xhci_command *cmd;
697 struct xhci_segment *new_seg;
698 union xhci_trb *new_deq;
699 int new_cycle;
700 dma_addr_t addr;
701 u64 hw_dequeue;
702 bool hw_dequeue_found = false;
703 bool td_last_trb_found = false;
704 u32 trb_sct = 0;
705 int ret;
706
707 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
708 ep_index, stream_id);
709 if (!ep_ring) {
710 xhci_warn(xhci, "WARN can't find new dequeue, invalid stream ID %u\n",
711 stream_id);
712 return -ENODEV;
713 }
714
715 hw_dequeue = xhci_get_hw_deq(xhci, dev, ep_index, stream_id);
716 new_seg = ep_ring->deq_seg;
717 new_deq = ep_ring->dequeue;
718 new_cycle = le32_to_cpu(td->end_trb->generic.field[3]) & TRB_CYCLE;
719
720 /*
721 * Walk the ring until both the next TRB and hw_dequeue are found (don't
722 * move hw_dequeue back if it went forward due to a HW bug). Cycle state
723 * is loaded from a known good TRB, track later toggles to maintain it.
724 */
725 do {
726 if (!hw_dequeue_found && xhci_trb_virt_to_dma(new_seg, new_deq)
727 == (dma_addr_t)(hw_dequeue & ~0xf)) {
728 hw_dequeue_found = true;
729 if (td_last_trb_found)
730 break;
731 }
732 if (new_deq == td->end_trb)
733 td_last_trb_found = true;
734
735 if (td_last_trb_found && trb_is_link(new_deq) &&
736 link_trb_toggles_cycle(new_deq))
737 new_cycle ^= 0x1;
738
739 next_trb(&new_seg, &new_deq);
740
741 /* Search wrapped around, bail out */
742 if (new_deq == ep->ring->dequeue) {
743 xhci_err(xhci, "Error: Failed finding new dequeue state\n");
744 return -EINVAL;
745 }
746
747 } while (!hw_dequeue_found || !td_last_trb_found);
748
749 /* Don't update the ring cycle state for the producer (us). */
750 addr = xhci_trb_virt_to_dma(new_seg, new_deq);
751 if (addr == 0) {
752 xhci_warn(xhci, "Can't find dma of new dequeue ptr\n");
753 xhci_warn(xhci, "deq seg = %p, deq ptr = %p\n", new_seg, new_deq);
754 return -EINVAL;
755 }
756
757 if ((ep->ep_state & SET_DEQ_PENDING)) {
758 xhci_warn(xhci, "Set TR Deq already pending, don't submit for 0x%pad\n",
759 &addr);
760 return -EBUSY;
761 }
762
763 /* This function gets called from contexts where it cannot sleep */
764 cmd = xhci_alloc_command(xhci, false, GFP_ATOMIC);
765 if (!cmd) {
766 xhci_warn(xhci, "Can't alloc Set TR Deq cmd 0x%pad\n", &addr);
767 return -ENOMEM;
768 }
769
770 if (stream_id)
771 trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
772 ret = queue_command(xhci, cmd,
773 lower_32_bits(addr) | trb_sct | new_cycle,
774 upper_32_bits(addr),
775 STREAM_ID_FOR_TRB(stream_id), SLOT_ID_FOR_TRB(slot_id) |
776 EP_INDEX_FOR_TRB(ep_index) | TRB_TYPE(TRB_SET_DEQ), false);
777 if (ret < 0) {
778 xhci_free_command(xhci, cmd);
779 return ret;
780 }
781 ep->queued_deq_seg = new_seg;
782 ep->queued_deq_ptr = new_deq;
783
784 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
785 "Set TR Deq ptr 0x%llx, cycle %u\n", addr, new_cycle);
786
787 /* Stop the TD queueing code from ringing the doorbell until
788 * this command completes. The HC won't set the dequeue pointer
789 * if the ring is running, and ringing the doorbell starts the
790 * ring running.
791 */
792 ep->ep_state |= SET_DEQ_PENDING;
793 xhci_ring_cmd_db(xhci);
794 return 0;
795 }
796
797 /* flip_cycle means flip the cycle bit of all but the first and last TRB.
798 * (The last TRB actually points to the ring enqueue pointer, which is not part
799 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
800 */
td_to_noop(struct xhci_td * td,bool flip_cycle)801 static void td_to_noop(struct xhci_td *td, bool flip_cycle)
802 {
803 struct xhci_segment *seg = td->start_seg;
804 union xhci_trb *trb = td->start_trb;
805
806 while (1) {
807 trb_to_noop(trb, TRB_TR_NOOP);
808
809 /* flip cycle if asked to */
810 if (flip_cycle && trb != td->start_trb && trb != td->end_trb)
811 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE);
812
813 if (trb == td->end_trb)
814 break;
815
816 next_trb(&seg, &trb);
817 }
818 }
819
xhci_giveback_urb_in_irq(struct xhci_hcd * xhci,struct xhci_td * cur_td,int status)820 static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
821 struct xhci_td *cur_td, int status)
822 {
823 struct urb *urb = cur_td->urb;
824 struct urb_priv *urb_priv = urb->hcpriv;
825 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
826
827 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
828 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
829 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
830 if (xhci->quirks & XHCI_AMD_PLL_FIX)
831 usb_amd_quirk_pll_enable();
832 }
833 }
834 xhci_urb_free_priv(urb_priv);
835 usb_hcd_unlink_urb_from_ep(hcd, urb);
836 trace_xhci_urb_giveback(urb);
837 usb_hcd_giveback_urb(hcd, urb, status);
838 }
839
xhci_unmap_td_bounce_buffer(struct xhci_hcd * xhci,struct xhci_ring * ring,struct xhci_td * td)840 static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci,
841 struct xhci_ring *ring, struct xhci_td *td)
842 {
843 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
844 struct xhci_segment *seg = td->bounce_seg;
845 struct urb *urb = td->urb;
846 size_t len;
847
848 if (!ring || !seg || !urb)
849 return;
850
851 if (usb_urb_dir_out(urb)) {
852 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
853 DMA_TO_DEVICE);
854 return;
855 }
856
857 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
858 DMA_FROM_DEVICE);
859 /* for in transfers we need to copy the data from bounce to sg */
860 if (urb->num_sgs) {
861 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf,
862 seg->bounce_len, seg->bounce_offs);
863 if (len != seg->bounce_len)
864 xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n",
865 len, seg->bounce_len);
866 } else {
867 memcpy(urb->transfer_buffer + seg->bounce_offs, seg->bounce_buf,
868 seg->bounce_len);
869 }
870 seg->bounce_len = 0;
871 seg->bounce_offs = 0;
872 }
873
xhci_td_cleanup(struct xhci_hcd * xhci,struct xhci_td * td,struct xhci_ring * ep_ring,int status)874 static void xhci_td_cleanup(struct xhci_hcd *xhci, struct xhci_td *td,
875 struct xhci_ring *ep_ring, int status)
876 {
877 struct urb *urb = NULL;
878
879 /* Clean up the endpoint's TD list */
880 urb = td->urb;
881
882 /* if a bounce buffer was used to align this td then unmap it */
883 xhci_unmap_td_bounce_buffer(xhci, ep_ring, td);
884
885 /* Do one last check of the actual transfer length.
886 * If the host controller said we transferred more data than the buffer
887 * length, urb->actual_length will be a very big number (since it's
888 * unsigned). Play it safe and say we didn't transfer anything.
889 */
890 if (urb->actual_length > urb->transfer_buffer_length) {
891 xhci_warn(xhci, "URB req %u and actual %u transfer length mismatch\n",
892 urb->transfer_buffer_length, urb->actual_length);
893 urb->actual_length = 0;
894 status = 0;
895 }
896 /* TD might be removed from td_list if we are giving back a cancelled URB */
897 if (!list_empty(&td->td_list))
898 list_del_init(&td->td_list);
899 /* Giving back a cancelled URB, or if a slated TD completed anyway */
900 if (!list_empty(&td->cancelled_td_list))
901 list_del_init(&td->cancelled_td_list);
902
903 inc_td_cnt(urb);
904 /* Giveback the urb when all the tds are completed */
905 if (last_td_in_urb(td)) {
906 if ((urb->actual_length != urb->transfer_buffer_length &&
907 (urb->transfer_flags & URB_SHORT_NOT_OK)) ||
908 (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc)))
909 xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n",
910 urb, urb->actual_length,
911 urb->transfer_buffer_length, status);
912
913 /* set isoc urb status to 0 just as EHCI, UHCI, and OHCI */
914 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
915 status = 0;
916 xhci_giveback_urb_in_irq(xhci, td, status);
917 }
918 }
919
920 /* Give back previous TD and move on to the next TD. */
xhci_dequeue_td(struct xhci_hcd * xhci,struct xhci_td * td,struct xhci_ring * ring,u32 status)921 static void xhci_dequeue_td(struct xhci_hcd *xhci, struct xhci_td *td, struct xhci_ring *ring,
922 u32 status)
923 {
924 ring->dequeue = td->end_trb;
925 ring->deq_seg = td->end_seg;
926 inc_deq(xhci, ring);
927
928 xhci_td_cleanup(xhci, td, ring, status);
929 }
930
931 /* Complete the cancelled URBs we unlinked from td_list. */
xhci_giveback_invalidated_tds(struct xhci_virt_ep * ep)932 static void xhci_giveback_invalidated_tds(struct xhci_virt_ep *ep)
933 {
934 struct xhci_ring *ring;
935 struct xhci_td *td, *tmp_td;
936
937 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list,
938 cancelled_td_list) {
939
940 ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb);
941
942 if (td->cancel_status == TD_CLEARED) {
943 xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n",
944 __func__, td->urb);
945 xhci_td_cleanup(ep->xhci, td, ring, td->status);
946 } else {
947 xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n",
948 __func__, td->urb, td->cancel_status);
949 }
950 if (ep->xhci->xhc_state & XHCI_STATE_DYING)
951 return;
952 }
953 }
954
xhci_reset_halted_ep(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index,enum xhci_ep_reset_type reset_type)955 static int xhci_reset_halted_ep(struct xhci_hcd *xhci, unsigned int slot_id,
956 unsigned int ep_index, enum xhci_ep_reset_type reset_type)
957 {
958 struct xhci_command *command;
959 int ret = 0;
960
961 command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
962 if (!command) {
963 ret = -ENOMEM;
964 goto done;
965 }
966
967 xhci_dbg(xhci, "%s-reset ep %u, slot %u\n",
968 (reset_type == EP_HARD_RESET) ? "Hard" : "Soft",
969 ep_index, slot_id);
970
971 ret = xhci_queue_reset_ep(xhci, command, slot_id, ep_index, reset_type);
972 done:
973 if (ret)
974 xhci_err(xhci, "ERROR queuing reset endpoint for slot %d ep_index %d, %d\n",
975 slot_id, ep_index, ret);
976 return ret;
977 }
978
xhci_handle_halted_endpoint(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,struct xhci_td * td,enum xhci_ep_reset_type reset_type)979 static int xhci_handle_halted_endpoint(struct xhci_hcd *xhci,
980 struct xhci_virt_ep *ep,
981 struct xhci_td *td,
982 enum xhci_ep_reset_type reset_type)
983 {
984 unsigned int slot_id = ep->vdev->slot_id;
985 int err;
986
987 /*
988 * Avoid resetting endpoint if link is inactive. Can cause host hang.
989 * Device will be reset soon to recover the link so don't do anything
990 */
991 if (ep->vdev->flags & VDEV_PORT_ERROR)
992 return -ENODEV;
993
994 /* add td to cancelled list and let reset ep handler take care of it */
995 if (reset_type == EP_HARD_RESET) {
996 ep->ep_state |= EP_HARD_CLEAR_TOGGLE;
997 if (td && list_empty(&td->cancelled_td_list)) {
998 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
999 td->cancel_status = TD_HALTED;
1000 }
1001 }
1002
1003 if (ep->ep_state & EP_HALTED) {
1004 xhci_dbg(xhci, "Reset ep command for ep_index %d already pending\n",
1005 ep->ep_index);
1006 return 0;
1007 }
1008
1009 err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type);
1010 if (err)
1011 return err;
1012
1013 ep->ep_state |= EP_HALTED;
1014
1015 xhci_ring_cmd_db(xhci);
1016
1017 return 0;
1018 }
1019
1020 /*
1021 * Fix up the ep ring first, so HW stops executing cancelled TDs.
1022 * We have the xHCI lock, so nothing can modify this list until we drop it.
1023 * We're also in the event handler, so we can't get re-interrupted if another
1024 * Stop Endpoint command completes.
1025 *
1026 * only call this when ring is not in a running state
1027 */
1028
xhci_invalidate_cancelled_tds(struct xhci_virt_ep * ep)1029 static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep)
1030 {
1031 struct xhci_hcd *xhci;
1032 struct xhci_td *td = NULL;
1033 struct xhci_td *tmp_td = NULL;
1034 struct xhci_td *cached_td = NULL;
1035 struct xhci_ring *ring;
1036 u64 hw_deq;
1037 unsigned int slot_id = ep->vdev->slot_id;
1038 int err;
1039
1040 /*
1041 * This is not going to work if the hardware is changing its dequeue
1042 * pointers as we look at them. Completion handler will call us later.
1043 */
1044 if (ep->ep_state & SET_DEQ_PENDING)
1045 return 0;
1046
1047 xhci = ep->xhci;
1048
1049 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) {
1050 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1051 "Removing canceled TD starting at 0x%llx (dma) in stream %u URB %p",
1052 (unsigned long long)xhci_trb_virt_to_dma(
1053 td->start_seg, td->start_trb),
1054 td->urb->stream_id, td->urb);
1055 list_del_init(&td->td_list);
1056 ring = xhci_urb_to_transfer_ring(xhci, td->urb);
1057 if (!ring) {
1058 xhci_warn(xhci, "WARN Cancelled URB %p has invalid stream ID %u.\n",
1059 td->urb, td->urb->stream_id);
1060 continue;
1061 }
1062 /*
1063 * If a ring stopped on the TD we need to cancel then we have to
1064 * move the xHC endpoint ring dequeue pointer past this TD.
1065 * Rings halted due to STALL may show hw_deq is past the stalled
1066 * TD, but still require a set TR Deq command to flush xHC cache.
1067 */
1068 hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index,
1069 td->urb->stream_id);
1070 hw_deq &= ~0xf;
1071
1072 if (td->cancel_status == TD_HALTED || trb_in_td(td, hw_deq)) {
1073 switch (td->cancel_status) {
1074 case TD_CLEARED: /* TD is already no-op */
1075 case TD_CLEARING_CACHE: /* set TR deq command already queued */
1076 break;
1077 case TD_DIRTY: /* TD is cached, clear it */
1078 case TD_HALTED:
1079 case TD_CLEARING_CACHE_DEFERRED:
1080 if (cached_td) {
1081 if (cached_td->urb->stream_id != td->urb->stream_id) {
1082 /* Multiple streams case, defer move dq */
1083 xhci_dbg(xhci,
1084 "Move dq deferred: stream %u URB %p\n",
1085 td->urb->stream_id, td->urb);
1086 td->cancel_status = TD_CLEARING_CACHE_DEFERRED;
1087 break;
1088 }
1089
1090 /* Should never happen, but clear the TD if it does */
1091 xhci_warn(xhci,
1092 "Found multiple active URBs %p and %p in stream %u?\n",
1093 td->urb, cached_td->urb,
1094 td->urb->stream_id);
1095 td_to_noop(cached_td, false);
1096 cached_td->cancel_status = TD_CLEARED;
1097 }
1098 td_to_noop(td, false);
1099 td->cancel_status = TD_CLEARING_CACHE;
1100 cached_td = td;
1101 break;
1102 }
1103 } else {
1104 td_to_noop(td, false);
1105 td->cancel_status = TD_CLEARED;
1106 }
1107 }
1108
1109 /* If there's no need to move the dequeue pointer then we're done */
1110 if (!cached_td)
1111 return 0;
1112
1113 err = xhci_move_dequeue_past_td(xhci, slot_id, ep->ep_index,
1114 cached_td->urb->stream_id,
1115 cached_td);
1116 if (err) {
1117 /* Failed to move past cached td, just set cached TDs to no-op */
1118 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) {
1119 /*
1120 * Deferred TDs need to have the deq pointer set after the above command
1121 * completes, so if that failed we just give up on all of them (and
1122 * complain loudly since this could cause issues due to caching).
1123 */
1124 if (td->cancel_status != TD_CLEARING_CACHE &&
1125 td->cancel_status != TD_CLEARING_CACHE_DEFERRED)
1126 continue;
1127 xhci_warn(xhci, "Failed to clear cancelled cached URB %p, mark clear anyway\n",
1128 td->urb);
1129 td_to_noop(td, false);
1130 td->cancel_status = TD_CLEARED;
1131 }
1132 }
1133 return 0;
1134 }
1135
1136 /*
1137 * Erase queued TDs from transfer ring(s) and give back those the xHC didn't
1138 * stop on. If necessary, queue commands to move the xHC off cancelled TDs it
1139 * stopped on. Those will be given back later when the commands complete.
1140 *
1141 * Call under xhci->lock on a stopped endpoint.
1142 */
xhci_process_cancelled_tds(struct xhci_virt_ep * ep)1143 void xhci_process_cancelled_tds(struct xhci_virt_ep *ep)
1144 {
1145 xhci_invalidate_cancelled_tds(ep);
1146 xhci_giveback_invalidated_tds(ep);
1147 }
1148
1149 /*
1150 * Returns the TD the endpoint ring halted on.
1151 * Only call for non-running rings without streams.
1152 */
find_halted_td(struct xhci_virt_ep * ep)1153 static struct xhci_td *find_halted_td(struct xhci_virt_ep *ep)
1154 {
1155 struct xhci_td *td;
1156 u64 hw_deq;
1157
1158 if (!list_empty(&ep->ring->td_list)) { /* Not streams compatible */
1159 hw_deq = xhci_get_hw_deq(ep->xhci, ep->vdev, ep->ep_index, 0);
1160 hw_deq &= ~0xf;
1161 td = list_first_entry(&ep->ring->td_list, struct xhci_td, td_list);
1162 if (trb_in_td(td, hw_deq))
1163 return td;
1164 }
1165 return NULL;
1166 }
1167
1168 /*
1169 * When we get a command completion for a Stop Endpoint Command, we need to
1170 * unlink any cancelled TDs from the ring. There are two ways to do that:
1171 *
1172 * 1. If the HW was in the middle of processing the TD that needs to be
1173 * cancelled, then we must move the ring's dequeue pointer past the last TRB
1174 * in the TD with a Set Dequeue Pointer Command.
1175 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
1176 * bit cleared) so that the HW will skip over them.
1177 */
xhci_handle_cmd_stop_ep(struct xhci_hcd * xhci,int slot_id,union xhci_trb * trb,u32 comp_code)1178 static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id,
1179 union xhci_trb *trb, u32 comp_code)
1180 {
1181 unsigned int ep_index;
1182 struct xhci_virt_ep *ep;
1183 struct xhci_ep_ctx *ep_ctx;
1184 struct xhci_td *td = NULL;
1185 enum xhci_ep_reset_type reset_type;
1186 struct xhci_command *command;
1187 int err;
1188
1189 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) {
1190 if (!xhci->devs[slot_id])
1191 xhci_warn(xhci, "Stop endpoint command completion for disabled slot %u\n",
1192 slot_id);
1193 return;
1194 }
1195
1196 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
1197 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
1198 if (!ep)
1199 return;
1200
1201 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
1202
1203 trace_xhci_handle_cmd_stop_ep(ep_ctx);
1204
1205 if (comp_code == COMP_CONTEXT_STATE_ERROR) {
1206 /*
1207 * If stop endpoint command raced with a halting endpoint we need to
1208 * reset the host side endpoint first.
1209 * If the TD we halted on isn't cancelled the TD should be given back
1210 * with a proper error code, and the ring dequeue moved past the TD.
1211 * If streams case we can't find hw_deq, or the TD we halted on so do a
1212 * soft reset.
1213 *
1214 * Proper error code is unknown here, it would be -EPIPE if device side
1215 * of enadpoit halted (aka STALL), and -EPROTO if not (transaction error)
1216 * We use -EPROTO, if device is stalled it should return a stall error on
1217 * next transfer, which then will return -EPIPE, and device side stall is
1218 * noted and cleared by class driver.
1219 */
1220 switch (GET_EP_CTX_STATE(ep_ctx)) {
1221 case EP_STATE_HALTED:
1222 xhci_dbg(xhci, "Stop ep completion raced with stall\n");
1223 /*
1224 * If the halt happened before Stop Endpoint failed, its transfer event
1225 * should have already been handled and Reset Endpoint should be pending.
1226 */
1227 if (ep->ep_state & EP_HALTED)
1228 goto reset_done;
1229
1230 if (ep->ep_state & EP_HAS_STREAMS) {
1231 reset_type = EP_SOFT_RESET;
1232 } else {
1233 reset_type = EP_HARD_RESET;
1234 td = find_halted_td(ep);
1235 if (td)
1236 td->status = -EPROTO;
1237 }
1238 /* reset ep, reset handler cleans up cancelled tds */
1239 err = xhci_handle_halted_endpoint(xhci, ep, td, reset_type);
1240 xhci_dbg(xhci, "Stop ep completion resetting ep, status %d\n", err);
1241 if (err)
1242 break;
1243 reset_done:
1244 /* Reset EP handler will clean up cancelled TDs */
1245 ep->ep_state &= ~EP_STOP_CMD_PENDING;
1246 return;
1247 case EP_STATE_STOPPED:
1248 /*
1249 * Per xHCI 4.6.9, Stop Endpoint command on a Stopped
1250 * EP is a Context State Error, and EP stays Stopped.
1251 *
1252 * But maybe it failed on Halted, and somebody ran Reset
1253 * Endpoint later. EP state is now Stopped and EP_HALTED
1254 * still set because Reset EP handler will run after us.
1255 */
1256 if (ep->ep_state & EP_HALTED)
1257 break;
1258 /*
1259 * On some HCs EP state remains Stopped for some tens of
1260 * us to a few ms or more after a doorbell ring, and any
1261 * new Stop Endpoint fails without aborting the restart.
1262 * This handler may run quickly enough to still see this
1263 * Stopped state, but it will soon change to Running.
1264 *
1265 * Assume this bug on unexpected Stop Endpoint failures.
1266 * Keep retrying until the EP starts and stops again.
1267 */
1268 fallthrough;
1269 case EP_STATE_RUNNING:
1270 /* Race, HW handled stop ep cmd before ep was running */
1271 xhci_dbg(xhci, "Stop ep completion ctx error, ctx_state %d\n",
1272 GET_EP_CTX_STATE(ep_ctx));
1273 /*
1274 * Don't retry forever if we guessed wrong or a defective HC never starts
1275 * the EP or says 'Running' but fails the command. We must give back TDs.
1276 */
1277 if (time_is_before_jiffies(ep->stop_time + msecs_to_jiffies(100)))
1278 break;
1279
1280 command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
1281 if (!command) {
1282 ep->ep_state &= ~EP_STOP_CMD_PENDING;
1283 return;
1284 }
1285 xhci_queue_stop_endpoint(xhci, command, slot_id, ep_index, 0);
1286 xhci_ring_cmd_db(xhci);
1287
1288 return;
1289 default:
1290 break;
1291 }
1292 }
1293
1294 /* will queue a set TR deq if stopped on a cancelled, uncleared TD */
1295 xhci_invalidate_cancelled_tds(ep);
1296 ep->ep_state &= ~EP_STOP_CMD_PENDING;
1297
1298 /* Otherwise ring the doorbell(s) to restart queued transfers */
1299 xhci_giveback_invalidated_tds(ep);
1300 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1301 }
1302
xhci_kill_ring_urbs(struct xhci_hcd * xhci,struct xhci_ring * ring)1303 static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring)
1304 {
1305 struct xhci_td *cur_td;
1306 struct xhci_td *tmp;
1307
1308 list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) {
1309 list_del_init(&cur_td->td_list);
1310
1311 if (!list_empty(&cur_td->cancelled_td_list))
1312 list_del_init(&cur_td->cancelled_td_list);
1313
1314 xhci_unmap_td_bounce_buffer(xhci, ring, cur_td);
1315
1316 inc_td_cnt(cur_td->urb);
1317 if (last_td_in_urb(cur_td))
1318 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
1319 }
1320 }
1321
xhci_kill_endpoint_urbs(struct xhci_hcd * xhci,int slot_id,int ep_index)1322 static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci,
1323 int slot_id, int ep_index)
1324 {
1325 struct xhci_td *cur_td;
1326 struct xhci_td *tmp;
1327 struct xhci_virt_ep *ep;
1328 struct xhci_ring *ring;
1329
1330 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
1331 if (!ep)
1332 return;
1333
1334 if ((ep->ep_state & EP_HAS_STREAMS) ||
1335 (ep->ep_state & EP_GETTING_NO_STREAMS)) {
1336 int stream_id;
1337
1338 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
1339 stream_id++) {
1340 ring = ep->stream_info->stream_rings[stream_id];
1341 if (!ring)
1342 continue;
1343
1344 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1345 "Killing URBs for slot ID %u, ep index %u, stream %u",
1346 slot_id, ep_index, stream_id);
1347 xhci_kill_ring_urbs(xhci, ring);
1348 }
1349 } else {
1350 ring = ep->ring;
1351 if (!ring)
1352 return;
1353 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1354 "Killing URBs for slot ID %u, ep index %u",
1355 slot_id, ep_index);
1356 xhci_kill_ring_urbs(xhci, ring);
1357 }
1358
1359 list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list,
1360 cancelled_td_list) {
1361 list_del_init(&cur_td->cancelled_td_list);
1362 inc_td_cnt(cur_td->urb);
1363
1364 if (last_td_in_urb(cur_td))
1365 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
1366 }
1367 }
1368
1369 /*
1370 * host controller died, register read returns 0xffffffff
1371 * Complete pending commands, mark them ABORTED.
1372 * URBs need to be given back as usb core might be waiting with device locks
1373 * held for the URBs to finish during device disconnect, blocking host remove.
1374 *
1375 * Call with xhci->lock held.
1376 * lock is relased and re-acquired while giving back urb.
1377 */
xhci_hc_died(struct xhci_hcd * xhci)1378 void xhci_hc_died(struct xhci_hcd *xhci)
1379 {
1380 int i, j;
1381
1382 if (xhci->xhc_state & XHCI_STATE_DYING)
1383 return;
1384
1385 xhci_err(xhci, "xHCI host controller not responding, assume dead\n");
1386 xhci->xhc_state |= XHCI_STATE_DYING;
1387
1388 xhci_cleanup_command_queue(xhci);
1389
1390 /* return any pending urbs, remove may be waiting for them */
1391 for (i = 0; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
1392 if (!xhci->devs[i])
1393 continue;
1394 for (j = 0; j < 31; j++)
1395 xhci_kill_endpoint_urbs(xhci, i, j);
1396 }
1397
1398 /* inform usb core hc died if PCI remove isn't already handling it */
1399 if (!(xhci->xhc_state & XHCI_STATE_REMOVING))
1400 usb_hc_died(xhci_to_hcd(xhci));
1401 }
1402
1403 /*
1404 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
1405 * we need to clear the set deq pending flag in the endpoint ring state, so that
1406 * the TD queueing code can ring the doorbell again. We also need to ring the
1407 * endpoint doorbell to restart the ring, but only if there aren't more
1408 * cancellations pending.
1409 */
xhci_handle_cmd_set_deq(struct xhci_hcd * xhci,int slot_id,union xhci_trb * trb,u32 cmd_comp_code)1410 static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
1411 union xhci_trb *trb, u32 cmd_comp_code)
1412 {
1413 unsigned int ep_index;
1414 unsigned int stream_id;
1415 struct xhci_ring *ep_ring;
1416 struct xhci_virt_ep *ep;
1417 struct xhci_ep_ctx *ep_ctx;
1418 struct xhci_slot_ctx *slot_ctx;
1419 struct xhci_stream_ctx *stream_ctx;
1420 struct xhci_td *td, *tmp_td;
1421
1422 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
1423 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
1424 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
1425 if (!ep)
1426 return;
1427
1428 ep_ring = xhci_virt_ep_to_ring(xhci, ep, stream_id);
1429 if (!ep_ring) {
1430 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n",
1431 stream_id);
1432 /* XXX: Harmless??? */
1433 goto cleanup;
1434 }
1435
1436 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
1437 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx);
1438 trace_xhci_handle_cmd_set_deq(slot_ctx);
1439 trace_xhci_handle_cmd_set_deq_ep(ep_ctx);
1440
1441 if (ep->ep_state & EP_HAS_STREAMS) {
1442 stream_ctx = &ep->stream_info->stream_ctx_array[stream_id];
1443 trace_xhci_handle_cmd_set_deq_stream(ep->stream_info, stream_id);
1444 }
1445
1446 if (cmd_comp_code != COMP_SUCCESS) {
1447 unsigned int ep_state;
1448 unsigned int slot_state;
1449
1450 switch (cmd_comp_code) {
1451 case COMP_TRB_ERROR:
1452 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n");
1453 break;
1454 case COMP_CONTEXT_STATE_ERROR:
1455 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n");
1456 ep_state = GET_EP_CTX_STATE(ep_ctx);
1457 slot_state = le32_to_cpu(slot_ctx->dev_state);
1458 slot_state = GET_SLOT_STATE(slot_state);
1459 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1460 "Slot state = %u, EP state = %u",
1461 slot_state, ep_state);
1462 break;
1463 case COMP_SLOT_NOT_ENABLED_ERROR:
1464 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n",
1465 slot_id);
1466 break;
1467 default:
1468 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n",
1469 cmd_comp_code);
1470 break;
1471 }
1472 /* OK what do we do now? The endpoint state is hosed, and we
1473 * should never get to this point if the synchronization between
1474 * queueing, and endpoint state are correct. This might happen
1475 * if the device gets disconnected after we've finished
1476 * cancelling URBs, which might not be an error...
1477 */
1478 } else {
1479 u64 deq;
1480 /* 4.6.10 deq ptr is written to the stream ctx for streams */
1481 if (ep->ep_state & EP_HAS_STREAMS) {
1482 deq = le64_to_cpu(stream_ctx->stream_ring) & SCTX_DEQ_MASK;
1483
1484 /*
1485 * Cadence xHCI controllers store some endpoint state
1486 * information within Rsvd0 fields of Stream Endpoint
1487 * context. This field is not cleared during Set TR
1488 * Dequeue Pointer command which causes XDMA to skip
1489 * over transfer ring and leads to data loss on stream
1490 * pipe.
1491 * To fix this issue driver must clear Rsvd0 field.
1492 */
1493 if (xhci->quirks & XHCI_CDNS_SCTX_QUIRK) {
1494 stream_ctx->reserved[0] = 0;
1495 stream_ctx->reserved[1] = 0;
1496 }
1497 } else {
1498 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK;
1499 }
1500 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1501 "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
1502 if (xhci_trb_virt_to_dma(ep->queued_deq_seg,
1503 ep->queued_deq_ptr) == deq) {
1504 /* Update the ring's dequeue segment and dequeue pointer
1505 * to reflect the new position.
1506 */
1507 ep_ring->deq_seg = ep->queued_deq_seg;
1508 ep_ring->dequeue = ep->queued_deq_ptr;
1509 } else {
1510 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n");
1511 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
1512 ep->queued_deq_seg, ep->queued_deq_ptr);
1513 }
1514 }
1515 /* HW cached TDs cleared from cache, give them back */
1516 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list,
1517 cancelled_td_list) {
1518 ep_ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb);
1519 if (td->cancel_status == TD_CLEARING_CACHE) {
1520 td->cancel_status = TD_CLEARED;
1521 xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n",
1522 __func__, td->urb);
1523 xhci_td_cleanup(ep->xhci, td, ep_ring, td->status);
1524 } else {
1525 xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n",
1526 __func__, td->urb, td->cancel_status);
1527 }
1528 }
1529 cleanup:
1530 ep->ep_state &= ~SET_DEQ_PENDING;
1531 ep->queued_deq_seg = NULL;
1532 ep->queued_deq_ptr = NULL;
1533
1534 /* Check for deferred or newly cancelled TDs */
1535 if (!list_empty(&ep->cancelled_td_list)) {
1536 xhci_dbg(ep->xhci, "%s: Pending TDs to clear, continuing with invalidation\n",
1537 __func__);
1538 xhci_invalidate_cancelled_tds(ep);
1539 /* Try to restart the endpoint if all is done */
1540 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1541 /* Start giving back any TDs invalidated above */
1542 xhci_giveback_invalidated_tds(ep);
1543 } else {
1544 /* Restart any rings with pending URBs */
1545 xhci_dbg(ep->xhci, "%s: All TDs cleared, ring doorbell\n", __func__);
1546 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1547 }
1548 }
1549
xhci_handle_cmd_reset_ep(struct xhci_hcd * xhci,int slot_id,union xhci_trb * trb,u32 cmd_comp_code)1550 static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id,
1551 union xhci_trb *trb, u32 cmd_comp_code)
1552 {
1553 struct xhci_virt_ep *ep;
1554 struct xhci_ep_ctx *ep_ctx;
1555 unsigned int ep_index;
1556
1557 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
1558 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
1559 if (!ep)
1560 return;
1561
1562 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
1563 trace_xhci_handle_cmd_reset_ep(ep_ctx);
1564
1565 /* This command will only fail if the endpoint wasn't halted,
1566 * but we don't care.
1567 */
1568 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
1569 "Ignoring reset ep completion code of %u", cmd_comp_code);
1570
1571 /* Cleanup cancelled TDs as ep is stopped. May queue a Set TR Deq cmd */
1572 xhci_invalidate_cancelled_tds(ep);
1573
1574 /* Clear our internal halted state */
1575 ep->ep_state &= ~EP_HALTED;
1576
1577 xhci_giveback_invalidated_tds(ep);
1578
1579 /* if this was a soft reset, then restart */
1580 if ((le32_to_cpu(trb->generic.field[3])) & TRB_TSP)
1581 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1582 }
1583
xhci_handle_cmd_enable_slot(int slot_id,struct xhci_command * command,u32 cmd_comp_code)1584 static void xhci_handle_cmd_enable_slot(int slot_id, struct xhci_command *command,
1585 u32 cmd_comp_code)
1586 {
1587 if (cmd_comp_code == COMP_SUCCESS)
1588 command->slot_id = slot_id;
1589 else
1590 command->slot_id = 0;
1591 }
1592
xhci_handle_cmd_disable_slot(struct xhci_hcd * xhci,int slot_id)1593 static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id)
1594 {
1595 struct xhci_virt_device *virt_dev;
1596 struct xhci_slot_ctx *slot_ctx;
1597
1598 virt_dev = xhci->devs[slot_id];
1599 if (!virt_dev)
1600 return;
1601
1602 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
1603 trace_xhci_handle_cmd_disable_slot(slot_ctx);
1604
1605 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1606 /* Delete default control endpoint resources */
1607 xhci_free_device_endpoint_resources(xhci, virt_dev, true);
1608 }
1609
xhci_handle_cmd_config_ep(struct xhci_hcd * xhci,int slot_id)1610 static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id)
1611 {
1612 struct xhci_virt_device *virt_dev;
1613 struct xhci_input_control_ctx *ctrl_ctx;
1614 struct xhci_ep_ctx *ep_ctx;
1615 unsigned int ep_index;
1616 u32 add_flags;
1617
1618 /*
1619 * Configure endpoint commands can come from the USB core configuration
1620 * or alt setting changes, or when streams were being configured.
1621 */
1622
1623 virt_dev = xhci->devs[slot_id];
1624 if (!virt_dev)
1625 return;
1626 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
1627 if (!ctrl_ctx) {
1628 xhci_warn(xhci, "Could not get input context, bad type.\n");
1629 return;
1630 }
1631
1632 add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1633
1634 /* Input ctx add_flags are the endpoint index plus one */
1635 ep_index = xhci_last_valid_endpoint(add_flags) - 1;
1636
1637 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index);
1638 trace_xhci_handle_cmd_config_ep(ep_ctx);
1639
1640 return;
1641 }
1642
xhci_handle_cmd_addr_dev(struct xhci_hcd * xhci,int slot_id)1643 static void xhci_handle_cmd_addr_dev(struct xhci_hcd *xhci, int slot_id)
1644 {
1645 struct xhci_virt_device *vdev;
1646 struct xhci_slot_ctx *slot_ctx;
1647
1648 vdev = xhci->devs[slot_id];
1649 if (!vdev)
1650 return;
1651 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
1652 trace_xhci_handle_cmd_addr_dev(slot_ctx);
1653 }
1654
xhci_handle_cmd_reset_dev(struct xhci_hcd * xhci,int slot_id)1655 static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id)
1656 {
1657 struct xhci_virt_device *vdev;
1658 struct xhci_slot_ctx *slot_ctx;
1659
1660 vdev = xhci->devs[slot_id];
1661 if (!vdev) {
1662 xhci_warn(xhci, "Reset device command completion for disabled slot %u\n",
1663 slot_id);
1664 return;
1665 }
1666 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
1667 trace_xhci_handle_cmd_reset_dev(slot_ctx);
1668
1669 xhci_dbg(xhci, "Completed reset device command.\n");
1670 }
1671
xhci_handle_cmd_nec_get_fw(struct xhci_hcd * xhci,struct xhci_event_cmd * event)1672 static void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci,
1673 struct xhci_event_cmd *event)
1674 {
1675 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1676 xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n");
1677 return;
1678 }
1679 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1680 "NEC firmware version %2x.%02x",
1681 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1682 NEC_FW_MINOR(le32_to_cpu(event->status)));
1683 }
1684
xhci_complete_del_and_free_cmd(struct xhci_command * cmd,u32 comp_code,u32 comp_param)1685 static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 comp_code, u32 comp_param)
1686 {
1687 list_del(&cmd->cmd_list);
1688
1689 if (cmd->completion) {
1690 cmd->status = comp_code;
1691 cmd->comp_param = comp_param;
1692 complete(cmd->completion);
1693 } else {
1694 kfree(cmd);
1695 }
1696 }
1697
xhci_cleanup_command_queue(struct xhci_hcd * xhci)1698 void xhci_cleanup_command_queue(struct xhci_hcd *xhci)
1699 {
1700 struct xhci_command *cur_cmd, *tmp_cmd;
1701 xhci->current_cmd = NULL;
1702 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list)
1703 xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED, 0);
1704 }
1705
xhci_handle_command_timeout(struct work_struct * work)1706 void xhci_handle_command_timeout(struct work_struct *work)
1707 {
1708 struct xhci_hcd *xhci;
1709 unsigned long flags;
1710 char str[XHCI_MSG_MAX];
1711 u64 hw_ring_state;
1712 u32 cmd_field3;
1713 u32 usbsts;
1714
1715 xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer);
1716
1717 spin_lock_irqsave(&xhci->lock, flags);
1718
1719 /*
1720 * If timeout work is pending, or current_cmd is NULL, it means we
1721 * raced with command completion. Command is handled so just return.
1722 */
1723 if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) {
1724 spin_unlock_irqrestore(&xhci->lock, flags);
1725 return;
1726 }
1727
1728 cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]);
1729 usbsts = readl(&xhci->op_regs->status);
1730 xhci_dbg(xhci, "Command timeout, USBSTS:%s\n", xhci_decode_usbsts(str, usbsts));
1731
1732 /* Bail out and tear down xhci if a stop endpoint command failed */
1733 if (TRB_FIELD_TO_TYPE(cmd_field3) == TRB_STOP_RING) {
1734 struct xhci_virt_ep *ep;
1735
1736 xhci_warn(xhci, "xHCI host not responding to stop endpoint command\n");
1737
1738 ep = xhci_get_virt_ep(xhci, TRB_TO_SLOT_ID(cmd_field3),
1739 TRB_TO_EP_INDEX(cmd_field3));
1740 if (ep)
1741 ep->ep_state &= ~EP_STOP_CMD_PENDING;
1742
1743 xhci_halt(xhci);
1744 xhci_hc_died(xhci);
1745 goto time_out_completed;
1746 }
1747
1748 /* mark this command to be cancelled */
1749 xhci->current_cmd->status = COMP_COMMAND_ABORTED;
1750
1751 /* Make sure command ring is running before aborting it */
1752 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
1753 if (hw_ring_state == ~(u64)0) {
1754 xhci_hc_died(xhci);
1755 goto time_out_completed;
1756 }
1757
1758 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
1759 (hw_ring_state & CMD_RING_RUNNING)) {
1760 /* Prevent new doorbell, and start command abort */
1761 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED;
1762 xhci_dbg(xhci, "Command timeout\n");
1763 xhci_abort_cmd_ring(xhci, flags);
1764 goto time_out_completed;
1765 }
1766
1767 /* host removed. Bail out */
1768 if (xhci->xhc_state & XHCI_STATE_REMOVING) {
1769 xhci_dbg(xhci, "host removed, ring start fail?\n");
1770 xhci_cleanup_command_queue(xhci);
1771
1772 goto time_out_completed;
1773 }
1774
1775 /* command timeout on stopped ring, ring can't be aborted */
1776 xhci_dbg(xhci, "Command timeout on stopped ring\n");
1777 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd);
1778
1779 time_out_completed:
1780 spin_unlock_irqrestore(&xhci->lock, flags);
1781 return;
1782 }
1783
handle_cmd_completion(struct xhci_hcd * xhci,struct xhci_event_cmd * event)1784 static void handle_cmd_completion(struct xhci_hcd *xhci,
1785 struct xhci_event_cmd *event)
1786 {
1787 unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
1788 u32 status = le32_to_cpu(event->status);
1789 u64 cmd_dma;
1790 dma_addr_t cmd_dequeue_dma;
1791 u32 cmd_comp_code;
1792 union xhci_trb *cmd_trb;
1793 struct xhci_command *cmd;
1794 u32 cmd_type;
1795
1796 if (slot_id >= MAX_HC_SLOTS) {
1797 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id);
1798 return;
1799 }
1800
1801 cmd_dma = le64_to_cpu(event->cmd_trb);
1802 cmd_trb = xhci->cmd_ring->dequeue;
1803
1804 trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic, cmd_dma);
1805
1806 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status));
1807
1808 /* If CMD ring stopped we own the trbs between enqueue and dequeue */
1809 if (cmd_comp_code == COMP_COMMAND_RING_STOPPED) {
1810 complete_all(&xhci->cmd_ring_stop_completion);
1811 return;
1812 }
1813
1814 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
1815 cmd_trb);
1816 /*
1817 * Check whether the completion event is for our internal kept
1818 * command.
1819 */
1820 if (!cmd_dequeue_dma || cmd_dma != (u64)cmd_dequeue_dma) {
1821 xhci_warn(xhci,
1822 "ERROR mismatched command completion event\n");
1823 return;
1824 }
1825
1826 cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list);
1827
1828 cancel_delayed_work(&xhci->cmd_timer);
1829
1830 if (cmd->command_trb != xhci->cmd_ring->dequeue) {
1831 xhci_err(xhci,
1832 "Command completion event does not match command\n");
1833 return;
1834 }
1835
1836 /*
1837 * Host aborted the command ring, check if the current command was
1838 * supposed to be aborted, otherwise continue normally.
1839 * The command ring is stopped now, but the xHC will issue a Command
1840 * Ring Stopped event which will cause us to restart it.
1841 */
1842 if (cmd_comp_code == COMP_COMMAND_ABORTED) {
1843 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
1844 if (cmd->status == COMP_COMMAND_ABORTED) {
1845 if (xhci->current_cmd == cmd)
1846 xhci->current_cmd = NULL;
1847 goto event_handled;
1848 }
1849 }
1850
1851 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3]));
1852 switch (cmd_type) {
1853 case TRB_ENABLE_SLOT:
1854 xhci_handle_cmd_enable_slot(slot_id, cmd, cmd_comp_code);
1855 break;
1856 case TRB_DISABLE_SLOT:
1857 xhci_handle_cmd_disable_slot(xhci, slot_id);
1858 break;
1859 case TRB_CONFIG_EP:
1860 if (!cmd->completion)
1861 xhci_handle_cmd_config_ep(xhci, slot_id);
1862 break;
1863 case TRB_EVAL_CONTEXT:
1864 break;
1865 case TRB_ADDR_DEV:
1866 xhci_handle_cmd_addr_dev(xhci, slot_id);
1867 break;
1868 case TRB_STOP_RING:
1869 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1870 le32_to_cpu(cmd_trb->generic.field[3])));
1871 if (!cmd->completion)
1872 xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb,
1873 cmd_comp_code);
1874 break;
1875 case TRB_SET_DEQ:
1876 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1877 le32_to_cpu(cmd_trb->generic.field[3])));
1878 xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code);
1879 break;
1880 case TRB_CMD_NOOP:
1881 /* Is this an aborted command turned to NO-OP? */
1882 if (cmd->status == COMP_COMMAND_RING_STOPPED)
1883 cmd_comp_code = COMP_COMMAND_RING_STOPPED;
1884 break;
1885 case TRB_RESET_EP:
1886 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1887 le32_to_cpu(cmd_trb->generic.field[3])));
1888 xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code);
1889 break;
1890 case TRB_RESET_DEV:
1891 /* SLOT_ID field in reset device cmd completion event TRB is 0.
1892 * Use the SLOT_ID from the command TRB instead (xhci 4.6.11)
1893 */
1894 slot_id = TRB_TO_SLOT_ID(
1895 le32_to_cpu(cmd_trb->generic.field[3]));
1896 xhci_handle_cmd_reset_dev(xhci, slot_id);
1897 break;
1898 case TRB_NEC_GET_FW:
1899 xhci_handle_cmd_nec_get_fw(xhci, event);
1900 break;
1901 case TRB_GET_BW:
1902 break;
1903 default:
1904 /* Skip over unknown commands on the event ring */
1905 xhci_info(xhci, "INFO unknown command type %d\n", cmd_type);
1906 break;
1907 }
1908
1909 /* restart timer if this wasn't the last command */
1910 if (!list_is_singular(&xhci->cmd_list)) {
1911 xhci->current_cmd = list_first_entry(&cmd->cmd_list,
1912 struct xhci_command, cmd_list);
1913 xhci_mod_cmd_timer(xhci);
1914 } else if (xhci->current_cmd == cmd) {
1915 xhci->current_cmd = NULL;
1916 }
1917
1918 event_handled:
1919 xhci_complete_del_and_free_cmd(cmd, cmd_comp_code, COMP_PARAM(status));
1920
1921 inc_deq(xhci, xhci->cmd_ring);
1922 }
1923
handle_vendor_event(struct xhci_hcd * xhci,union xhci_trb * event,u32 trb_type)1924 static void handle_vendor_event(struct xhci_hcd *xhci,
1925 union xhci_trb *event, u32 trb_type)
1926 {
1927 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1928 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1929 handle_cmd_completion(xhci, &event->event_cmd);
1930 }
1931
handle_device_notification(struct xhci_hcd * xhci,union xhci_trb * event)1932 static void handle_device_notification(struct xhci_hcd *xhci,
1933 union xhci_trb *event)
1934 {
1935 u32 slot_id;
1936 struct usb_device *udev;
1937
1938 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3]));
1939 if (!xhci->devs[slot_id]) {
1940 xhci_warn(xhci, "Device Notification event for "
1941 "unused slot %u\n", slot_id);
1942 return;
1943 }
1944
1945 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
1946 slot_id);
1947 udev = xhci->devs[slot_id]->udev;
1948 if (udev && udev->parent)
1949 usb_wakeup_notification(udev->parent, udev->portnum);
1950 }
1951
1952 /*
1953 * Quirk hanlder for errata seen on Cavium ThunderX2 processor XHCI
1954 * Controller.
1955 * As per ThunderX2errata-129 USB 2 device may come up as USB 1
1956 * If a connection to a USB 1 device is followed by another connection
1957 * to a USB 2 device.
1958 *
1959 * Reset the PHY after the USB device is disconnected if device speed
1960 * is less than HCD_USB3.
1961 * Retry the reset sequence max of 4 times checking the PLL lock status.
1962 *
1963 */
xhci_cavium_reset_phy_quirk(struct xhci_hcd * xhci)1964 static void xhci_cavium_reset_phy_quirk(struct xhci_hcd *xhci)
1965 {
1966 struct usb_hcd *hcd = xhci_to_hcd(xhci);
1967 u32 pll_lock_check;
1968 u32 retry_count = 4;
1969
1970 do {
1971 /* Assert PHY reset */
1972 writel(0x6F, hcd->regs + 0x1048);
1973 udelay(10);
1974 /* De-assert the PHY reset */
1975 writel(0x7F, hcd->regs + 0x1048);
1976 udelay(200);
1977 pll_lock_check = readl(hcd->regs + 0x1070);
1978 } while (!(pll_lock_check & 0x1) && --retry_count);
1979 }
1980
handle_port_status(struct xhci_hcd * xhci,union xhci_trb * event)1981 static void handle_port_status(struct xhci_hcd *xhci, union xhci_trb *event)
1982 {
1983 struct usb_hcd *hcd;
1984 u32 port_id;
1985 u32 portsc, cmd_reg;
1986 int max_ports;
1987 unsigned int hcd_portnum;
1988 struct xhci_bus_state *bus_state;
1989 bool bogus_port_status = false;
1990 struct xhci_port *port;
1991
1992 /* Port status change events always have a successful completion code */
1993 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS)
1994 xhci_warn(xhci,
1995 "WARN: xHC returned failed port status event\n");
1996
1997 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
1998 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1999
2000 if ((port_id <= 0) || (port_id > max_ports)) {
2001 xhci_warn(xhci, "Port change event with invalid port ID %d\n",
2002 port_id);
2003 return;
2004 }
2005
2006 port = &xhci->hw_ports[port_id - 1];
2007 if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) {
2008 xhci_warn(xhci, "Port change event, no port for port ID %u\n",
2009 port_id);
2010 bogus_port_status = true;
2011 goto cleanup;
2012 }
2013
2014 /* We might get interrupts after shared_hcd is removed */
2015 if (port->rhub == &xhci->usb3_rhub && xhci->shared_hcd == NULL) {
2016 xhci_dbg(xhci, "ignore port event for removed USB3 hcd\n");
2017 bogus_port_status = true;
2018 goto cleanup;
2019 }
2020
2021 hcd = port->rhub->hcd;
2022 bus_state = &port->rhub->bus_state;
2023 hcd_portnum = port->hcd_portnum;
2024 portsc = readl(port->addr);
2025
2026 xhci_dbg(xhci, "Port change event, %d-%d, id %d, portsc: 0x%x\n",
2027 hcd->self.busnum, hcd_portnum + 1, port_id, portsc);
2028
2029 trace_xhci_handle_port_status(port, portsc);
2030
2031 if (hcd->state == HC_STATE_SUSPENDED) {
2032 xhci_dbg(xhci, "resume root hub\n");
2033 usb_hcd_resume_root_hub(hcd);
2034 }
2035
2036 if (hcd->speed >= HCD_USB3 &&
2037 (portsc & PORT_PLS_MASK) == XDEV_INACTIVE) {
2038 if (port->slot_id && xhci->devs[port->slot_id])
2039 xhci->devs[port->slot_id]->flags |= VDEV_PORT_ERROR;
2040 }
2041
2042 if ((portsc & PORT_PLC) && (portsc & PORT_PLS_MASK) == XDEV_RESUME) {
2043 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
2044
2045 cmd_reg = readl(&xhci->op_regs->command);
2046 if (!(cmd_reg & CMD_RUN)) {
2047 xhci_warn(xhci, "xHC is not running.\n");
2048 goto cleanup;
2049 }
2050
2051 if (DEV_SUPERSPEED_ANY(portsc)) {
2052 xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
2053 /* Set a flag to say the port signaled remote wakeup,
2054 * so we can tell the difference between the end of
2055 * device and host initiated resume.
2056 */
2057 bus_state->port_remote_wakeup |= 1 << hcd_portnum;
2058 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
2059 usb_hcd_start_port_resume(&hcd->self, hcd_portnum);
2060 xhci_set_link_state(xhci, port, XDEV_U0);
2061 /* Need to wait until the next link state change
2062 * indicates the device is actually in U0.
2063 */
2064 bogus_port_status = true;
2065 goto cleanup;
2066 } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) {
2067 xhci_dbg(xhci, "resume HS port %d\n", port_id);
2068 port->resume_timestamp = jiffies +
2069 msecs_to_jiffies(USB_RESUME_TIMEOUT);
2070 set_bit(hcd_portnum, &bus_state->resuming_ports);
2071 /* Do the rest in GetPortStatus after resume time delay.
2072 * Avoid polling roothub status before that so that a
2073 * usb device auto-resume latency around ~40ms.
2074 */
2075 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2076 mod_timer(&hcd->rh_timer,
2077 port->resume_timestamp);
2078 usb_hcd_start_port_resume(&hcd->self, hcd_portnum);
2079 bogus_port_status = true;
2080 }
2081 }
2082
2083 if ((portsc & PORT_PLC) &&
2084 DEV_SUPERSPEED_ANY(portsc) &&
2085 ((portsc & PORT_PLS_MASK) == XDEV_U0 ||
2086 (portsc & PORT_PLS_MASK) == XDEV_U1 ||
2087 (portsc & PORT_PLS_MASK) == XDEV_U2)) {
2088 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
2089 complete(&port->u3exit_done);
2090 /* We've just brought the device into U0/1/2 through either the
2091 * Resume state after a device remote wakeup, or through the
2092 * U3Exit state after a host-initiated resume. If it's a device
2093 * initiated remote wake, don't pass up the link state change,
2094 * so the roothub behavior is consistent with external
2095 * USB 3.0 hub behavior.
2096 */
2097 if (port->slot_id && xhci->devs[port->slot_id])
2098 xhci_ring_device(xhci, port->slot_id);
2099 if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) {
2100 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
2101 usb_wakeup_notification(hcd->self.root_hub,
2102 hcd_portnum + 1);
2103 bogus_port_status = true;
2104 goto cleanup;
2105 }
2106 }
2107
2108 /*
2109 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or
2110 * RExit to a disconnect state). If so, let the driver know it's
2111 * out of the RExit state.
2112 */
2113 if (hcd->speed < HCD_USB3 && port->rexit_active) {
2114 complete(&port->rexit_done);
2115 port->rexit_active = false;
2116 bogus_port_status = true;
2117 goto cleanup;
2118 }
2119
2120 if (hcd->speed < HCD_USB3) {
2121 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
2122 if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) &&
2123 (portsc & PORT_CSC) && !(portsc & PORT_CONNECT))
2124 xhci_cavium_reset_phy_quirk(xhci);
2125 }
2126
2127 cleanup:
2128
2129 /* Don't make the USB core poll the roothub if we got a bad port status
2130 * change event. Besides, at that point we can't tell which roothub
2131 * (USB 2.0 or USB 3.0) to kick.
2132 */
2133 if (bogus_port_status)
2134 return;
2135
2136 /*
2137 * xHCI port-status-change events occur when the "or" of all the
2138 * status-change bits in the portsc register changes from 0 to 1.
2139 * New status changes won't cause an event if any other change
2140 * bits are still set. When an event occurs, switch over to
2141 * polling to avoid losing status changes.
2142 */
2143 xhci_dbg(xhci, "%s: starting usb%d port polling.\n",
2144 __func__, hcd->self.busnum);
2145 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2146 spin_unlock(&xhci->lock);
2147 /* Pass this up to the core */
2148 usb_hcd_poll_rh_status(hcd);
2149 spin_lock(&xhci->lock);
2150 }
2151
xhci_clear_hub_tt_buffer(struct xhci_hcd * xhci,struct xhci_td * td,struct xhci_virt_ep * ep)2152 static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci, struct xhci_td *td,
2153 struct xhci_virt_ep *ep)
2154 {
2155 /*
2156 * As part of low/full-speed endpoint-halt processing
2157 * we must clear the TT buffer (USB 2.0 specification 11.17.5).
2158 */
2159 if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) &&
2160 (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) &&
2161 !(ep->ep_state & EP_CLEARING_TT)) {
2162 ep->ep_state |= EP_CLEARING_TT;
2163 td->urb->ep->hcpriv = td->urb->dev;
2164 if (usb_hub_clear_tt_buffer(td->urb))
2165 ep->ep_state &= ~EP_CLEARING_TT;
2166 }
2167 }
2168
2169 /*
2170 * Check if xhci internal endpoint state has gone to a "halt" state due to an
2171 * error or stall, including default control pipe protocol stall.
2172 * The internal halt needs to be cleared with a reset endpoint command.
2173 *
2174 * External device side is also halted in functional stall cases. Class driver
2175 * will clear the device halt with a CLEAR_FEATURE(ENDPOINT_HALT) request later.
2176 */
xhci_halted_host_endpoint(struct xhci_ep_ctx * ep_ctx,unsigned int comp_code)2177 static bool xhci_halted_host_endpoint(struct xhci_ep_ctx *ep_ctx, unsigned int comp_code)
2178 {
2179 /* Stall halts both internal and device side endpoint */
2180 if (comp_code == COMP_STALL_ERROR)
2181 return true;
2182
2183 /* TRB completion codes that may require internal halt cleanup */
2184 if (comp_code == COMP_USB_TRANSACTION_ERROR ||
2185 comp_code == COMP_BABBLE_DETECTED_ERROR ||
2186 comp_code == COMP_SPLIT_TRANSACTION_ERROR)
2187 /*
2188 * The 0.95 spec says a babbling control endpoint is not halted.
2189 * The 0.96 spec says it is. Some HW claims to be 0.95
2190 * compliant, but it halts the control endpoint anyway.
2191 * Check endpoint context if endpoint is halted.
2192 */
2193 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_HALTED)
2194 return true;
2195
2196 return false;
2197 }
2198
xhci_is_vendor_info_code(struct xhci_hcd * xhci,unsigned int trb_comp_code)2199 int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
2200 {
2201 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
2202 /* Vendor defined "informational" completion code,
2203 * treat as not-an-error.
2204 */
2205 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
2206 trb_comp_code);
2207 xhci_dbg(xhci, "Treating code as success.\n");
2208 return 1;
2209 }
2210 return 0;
2211 }
2212
finish_td(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,struct xhci_ring * ep_ring,struct xhci_td * td,u32 trb_comp_code)2213 static void finish_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
2214 struct xhci_ring *ep_ring, struct xhci_td *td,
2215 u32 trb_comp_code)
2216 {
2217 struct xhci_ep_ctx *ep_ctx;
2218
2219 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index);
2220
2221 switch (trb_comp_code) {
2222 case COMP_STOPPED_LENGTH_INVALID:
2223 case COMP_STOPPED_SHORT_PACKET:
2224 case COMP_STOPPED:
2225 /*
2226 * The "Stop Endpoint" completion will take care of any
2227 * stopped TDs. A stopped TD may be restarted, so don't update
2228 * the ring dequeue pointer or take this TD off any lists yet.
2229 */
2230 return;
2231 case COMP_USB_TRANSACTION_ERROR:
2232 case COMP_BABBLE_DETECTED_ERROR:
2233 case COMP_SPLIT_TRANSACTION_ERROR:
2234 /*
2235 * If endpoint context state is not halted we might be
2236 * racing with a reset endpoint command issued by a unsuccessful
2237 * stop endpoint completion (context error). In that case the
2238 * td should be on the cancelled list, and EP_HALTED flag set.
2239 *
2240 * Or then it's not halted due to the 0.95 spec stating that a
2241 * babbling control endpoint should not halt. The 0.96 spec
2242 * again says it should. Some HW claims to be 0.95 compliant,
2243 * but it halts the control endpoint anyway.
2244 */
2245 if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_HALTED) {
2246 /*
2247 * If EP_HALTED is set and TD is on the cancelled list
2248 * the TD and dequeue pointer will be handled by reset
2249 * ep command completion
2250 */
2251 if ((ep->ep_state & EP_HALTED) &&
2252 !list_empty(&td->cancelled_td_list)) {
2253 xhci_dbg(xhci, "Already resolving halted ep for 0x%llx\n",
2254 (unsigned long long)xhci_trb_virt_to_dma(
2255 td->start_seg, td->start_trb));
2256 return;
2257 }
2258 /* endpoint not halted, don't reset it */
2259 break;
2260 }
2261 /* Almost same procedure as for STALL_ERROR below */
2262 xhci_clear_hub_tt_buffer(xhci, td, ep);
2263 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET);
2264 return;
2265 case COMP_STALL_ERROR:
2266 /*
2267 * xhci internal endpoint state will go to a "halt" state for
2268 * any stall, including default control pipe protocol stall.
2269 * To clear the host side halt we need to issue a reset endpoint
2270 * command, followed by a set dequeue command to move past the
2271 * TD.
2272 * Class drivers clear the device side halt from a functional
2273 * stall later. Hub TT buffer should only be cleared for FS/LS
2274 * devices behind HS hubs for functional stalls.
2275 */
2276 if (ep->ep_index != 0)
2277 xhci_clear_hub_tt_buffer(xhci, td, ep);
2278
2279 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET);
2280
2281 return; /* xhci_handle_halted_endpoint marked td cancelled */
2282 default:
2283 break;
2284 }
2285
2286 xhci_dequeue_td(xhci, td, ep_ring, td->status);
2287 }
2288
2289 /* sum trb lengths from the first trb up to stop_trb, _excluding_ stop_trb */
sum_trb_lengths(struct xhci_td * td,union xhci_trb * stop_trb)2290 static u32 sum_trb_lengths(struct xhci_td *td, union xhci_trb *stop_trb)
2291 {
2292 u32 sum;
2293 union xhci_trb *trb = td->start_trb;
2294 struct xhci_segment *seg = td->start_seg;
2295
2296 for (sum = 0; trb != stop_trb; next_trb(&seg, &trb)) {
2297 if (!trb_is_noop(trb) && !trb_is_link(trb))
2298 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2]));
2299 }
2300 return sum;
2301 }
2302
2303 /*
2304 * Process control tds, update urb status and actual_length.
2305 */
process_ctrl_td(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,struct xhci_ring * ep_ring,struct xhci_td * td,union xhci_trb * ep_trb,struct xhci_transfer_event * event)2306 static void process_ctrl_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
2307 struct xhci_ring *ep_ring, struct xhci_td *td,
2308 union xhci_trb *ep_trb, struct xhci_transfer_event *event)
2309 {
2310 struct xhci_ep_ctx *ep_ctx;
2311 u32 trb_comp_code;
2312 u32 remaining, requested;
2313 u32 trb_type;
2314
2315 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3]));
2316 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index);
2317 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
2318 requested = td->urb->transfer_buffer_length;
2319 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2320
2321 switch (trb_comp_code) {
2322 case COMP_SUCCESS:
2323 if (trb_type != TRB_STATUS) {
2324 xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n",
2325 (trb_type == TRB_DATA) ? "data" : "setup");
2326 td->status = -ESHUTDOWN;
2327 break;
2328 }
2329 td->status = 0;
2330 break;
2331 case COMP_SHORT_PACKET:
2332 td->status = 0;
2333 break;
2334 case COMP_STOPPED_SHORT_PACKET:
2335 if (trb_type == TRB_DATA || trb_type == TRB_NORMAL)
2336 td->urb->actual_length = remaining;
2337 else
2338 xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n");
2339 goto finish_td;
2340 case COMP_STOPPED:
2341 switch (trb_type) {
2342 case TRB_SETUP:
2343 td->urb->actual_length = 0;
2344 goto finish_td;
2345 case TRB_DATA:
2346 case TRB_NORMAL:
2347 td->urb->actual_length = requested - remaining;
2348 goto finish_td;
2349 case TRB_STATUS:
2350 td->urb->actual_length = requested;
2351 goto finish_td;
2352 default:
2353 xhci_warn(xhci, "WARN: unexpected TRB Type %d\n",
2354 trb_type);
2355 goto finish_td;
2356 }
2357 case COMP_STOPPED_LENGTH_INVALID:
2358 goto finish_td;
2359 default:
2360 if (!xhci_halted_host_endpoint(ep_ctx, trb_comp_code))
2361 break;
2362 xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n",
2363 trb_comp_code, ep->ep_index);
2364 fallthrough;
2365 case COMP_STALL_ERROR:
2366 /* Did we transfer part of the data (middle) phase? */
2367 if (trb_type == TRB_DATA || trb_type == TRB_NORMAL)
2368 td->urb->actual_length = requested - remaining;
2369 else if (!td->urb_length_set)
2370 td->urb->actual_length = 0;
2371 goto finish_td;
2372 }
2373
2374 /* stopped at setup stage, no data transferred */
2375 if (trb_type == TRB_SETUP)
2376 goto finish_td;
2377
2378 /*
2379 * if on data stage then update the actual_length of the URB and flag it
2380 * as set, so it won't be overwritten in the event for the last TRB.
2381 */
2382 if (trb_type == TRB_DATA ||
2383 trb_type == TRB_NORMAL) {
2384 td->urb_length_set = true;
2385 td->urb->actual_length = requested - remaining;
2386 xhci_dbg(xhci, "Waiting for status stage event\n");
2387 return;
2388 }
2389
2390 /* at status stage */
2391 if (!td->urb_length_set)
2392 td->urb->actual_length = requested;
2393
2394 finish_td:
2395 finish_td(xhci, ep, ep_ring, td, trb_comp_code);
2396 }
2397
2398 /*
2399 * Process isochronous tds, update urb packet status and actual_length.
2400 */
process_isoc_td(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,struct xhci_ring * ep_ring,struct xhci_td * td,union xhci_trb * ep_trb,struct xhci_transfer_event * event)2401 static void process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
2402 struct xhci_ring *ep_ring, struct xhci_td *td,
2403 union xhci_trb *ep_trb, struct xhci_transfer_event *event)
2404 {
2405 struct urb_priv *urb_priv;
2406 int idx;
2407 struct usb_iso_packet_descriptor *frame;
2408 u32 trb_comp_code;
2409 bool sum_trbs_for_length = false;
2410 u32 remaining, requested, ep_trb_len;
2411 int short_framestatus;
2412
2413 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
2414 urb_priv = td->urb->hcpriv;
2415 idx = urb_priv->num_tds_done;
2416 frame = &td->urb->iso_frame_desc[idx];
2417 requested = frame->length;
2418 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2419 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
2420 short_framestatus = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
2421 -EREMOTEIO : 0;
2422
2423 /* handle completion code */
2424 switch (trb_comp_code) {
2425 case COMP_SUCCESS:
2426 /* Don't overwrite status if TD had an error, see xHCI 4.9.1 */
2427 if (td->error_mid_td)
2428 break;
2429 if (remaining) {
2430 frame->status = short_framestatus;
2431 sum_trbs_for_length = true;
2432 break;
2433 }
2434 frame->status = 0;
2435 break;
2436 case COMP_SHORT_PACKET:
2437 frame->status = short_framestatus;
2438 sum_trbs_for_length = true;
2439 break;
2440 case COMP_BANDWIDTH_OVERRUN_ERROR:
2441 frame->status = -ECOMM;
2442 break;
2443 case COMP_BABBLE_DETECTED_ERROR:
2444 sum_trbs_for_length = true;
2445 fallthrough;
2446 case COMP_ISOCH_BUFFER_OVERRUN:
2447 frame->status = -EOVERFLOW;
2448 if (ep_trb != td->end_trb)
2449 td->error_mid_td = true;
2450 break;
2451 case COMP_MISSED_SERVICE_ERROR:
2452 frame->status = -EXDEV;
2453 sum_trbs_for_length = true;
2454 if (ep_trb != td->end_trb)
2455 td->error_mid_td = true;
2456 break;
2457 case COMP_INCOMPATIBLE_DEVICE_ERROR:
2458 case COMP_STALL_ERROR:
2459 frame->status = -EPROTO;
2460 break;
2461 case COMP_USB_TRANSACTION_ERROR:
2462 frame->status = -EPROTO;
2463 sum_trbs_for_length = true;
2464 if (ep_trb != td->end_trb)
2465 td->error_mid_td = true;
2466 break;
2467 case COMP_STOPPED:
2468 sum_trbs_for_length = true;
2469 break;
2470 case COMP_STOPPED_SHORT_PACKET:
2471 /* field normally containing residue now contains transferred */
2472 frame->status = short_framestatus;
2473 requested = remaining;
2474 break;
2475 case COMP_STOPPED_LENGTH_INVALID:
2476 /* exclude stopped trb with invalid length from length sum */
2477 sum_trbs_for_length = true;
2478 ep_trb_len = 0;
2479 remaining = 0;
2480 break;
2481 default:
2482 sum_trbs_for_length = true;
2483 frame->status = -1;
2484 break;
2485 }
2486
2487 if (td->urb_length_set)
2488 goto finish_td;
2489
2490 if (sum_trbs_for_length)
2491 frame->actual_length = sum_trb_lengths(td, ep_trb) +
2492 ep_trb_len - remaining;
2493 else
2494 frame->actual_length = requested;
2495
2496 td->urb->actual_length += frame->actual_length;
2497
2498 finish_td:
2499 /* Don't give back TD yet if we encountered an error mid TD */
2500 if (td->error_mid_td && ep_trb != td->end_trb) {
2501 xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n");
2502 td->urb_length_set = true;
2503 return;
2504 }
2505 finish_td(xhci, ep, ep_ring, td, trb_comp_code);
2506 }
2507
skip_isoc_td(struct xhci_hcd * xhci,struct xhci_td * td,struct xhci_virt_ep * ep,int status)2508 static void skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
2509 struct xhci_virt_ep *ep, int status)
2510 {
2511 struct urb_priv *urb_priv;
2512 struct usb_iso_packet_descriptor *frame;
2513 int idx;
2514
2515 urb_priv = td->urb->hcpriv;
2516 idx = urb_priv->num_tds_done;
2517 frame = &td->urb->iso_frame_desc[idx];
2518
2519 /* The transfer is partly done. */
2520 frame->status = -EXDEV;
2521
2522 /* calc actual length */
2523 frame->actual_length = 0;
2524
2525 xhci_dequeue_td(xhci, td, ep->ring, status);
2526 }
2527
2528 /*
2529 * Process bulk and interrupt tds, update urb status and actual_length.
2530 */
process_bulk_intr_td(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,struct xhci_ring * ep_ring,struct xhci_td * td,union xhci_trb * ep_trb,struct xhci_transfer_event * event)2531 static void process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
2532 struct xhci_ring *ep_ring, struct xhci_td *td,
2533 union xhci_trb *ep_trb, struct xhci_transfer_event *event)
2534 {
2535 struct xhci_slot_ctx *slot_ctx;
2536 u32 trb_comp_code;
2537 u32 remaining, requested, ep_trb_len;
2538
2539 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx);
2540 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
2541 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2542 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
2543 requested = td->urb->transfer_buffer_length;
2544
2545 switch (trb_comp_code) {
2546 case COMP_SUCCESS:
2547 ep->err_count = 0;
2548 /* handle success with untransferred data as short packet */
2549 if (ep_trb != td->end_trb || remaining) {
2550 xhci_warn(xhci, "WARN Successful completion on short TX\n");
2551 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n",
2552 td->urb->ep->desc.bEndpointAddress,
2553 requested, remaining);
2554 }
2555 td->status = 0;
2556 break;
2557 case COMP_SHORT_PACKET:
2558 td->status = 0;
2559 break;
2560 case COMP_STOPPED_SHORT_PACKET:
2561 td->urb->actual_length = remaining;
2562 goto finish_td;
2563 case COMP_STOPPED_LENGTH_INVALID:
2564 /* stopped on ep trb with invalid length, exclude it */
2565 td->urb->actual_length = sum_trb_lengths(td, ep_trb);
2566 goto finish_td;
2567 case COMP_USB_TRANSACTION_ERROR:
2568 if (xhci->quirks & XHCI_NO_SOFT_RETRY ||
2569 (ep->err_count++ > MAX_SOFT_RETRY) ||
2570 le32_to_cpu(slot_ctx->tt_info) & TT_SLOT)
2571 break;
2572
2573 td->status = 0;
2574
2575 xhci_handle_halted_endpoint(xhci, ep, td, EP_SOFT_RESET);
2576 return;
2577 default:
2578 /* do nothing */
2579 break;
2580 }
2581
2582 if (ep_trb == td->end_trb)
2583 td->urb->actual_length = requested - remaining;
2584 else
2585 td->urb->actual_length =
2586 sum_trb_lengths(td, ep_trb) +
2587 ep_trb_len - remaining;
2588 finish_td:
2589 if (remaining > requested) {
2590 xhci_warn(xhci, "bad transfer trb length %d in event trb\n",
2591 remaining);
2592 td->urb->actual_length = 0;
2593 }
2594
2595 finish_td(xhci, ep, ep_ring, td, trb_comp_code);
2596 }
2597
2598 /* Transfer events which don't point to a transfer TRB, see xhci 4.17.4 */
handle_transferless_tx_event(struct xhci_hcd * xhci,struct xhci_virt_ep * ep,u32 trb_comp_code)2599 static int handle_transferless_tx_event(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
2600 u32 trb_comp_code)
2601 {
2602 switch (trb_comp_code) {
2603 case COMP_STALL_ERROR:
2604 case COMP_USB_TRANSACTION_ERROR:
2605 case COMP_INVALID_STREAM_TYPE_ERROR:
2606 case COMP_INVALID_STREAM_ID_ERROR:
2607 xhci_dbg(xhci, "Stream transaction error ep %u no id\n", ep->ep_index);
2608 if (ep->err_count++ > MAX_SOFT_RETRY)
2609 xhci_handle_halted_endpoint(xhci, ep, NULL, EP_HARD_RESET);
2610 else
2611 xhci_handle_halted_endpoint(xhci, ep, NULL, EP_SOFT_RESET);
2612 break;
2613 case COMP_RING_UNDERRUN:
2614 case COMP_RING_OVERRUN:
2615 case COMP_STOPPED_LENGTH_INVALID:
2616 break;
2617 default:
2618 xhci_err(xhci, "Transfer event %u for unknown stream ring slot %u ep %u\n",
2619 trb_comp_code, ep->vdev->slot_id, ep->ep_index);
2620 return -ENODEV;
2621 }
2622 return 0;
2623 }
2624
xhci_spurious_success_tx_event(struct xhci_hcd * xhci,struct xhci_ring * ring)2625 static bool xhci_spurious_success_tx_event(struct xhci_hcd *xhci,
2626 struct xhci_ring *ring)
2627 {
2628 switch (ring->old_trb_comp_code) {
2629 case COMP_SHORT_PACKET:
2630 return xhci->quirks & XHCI_SPURIOUS_SUCCESS;
2631 case COMP_USB_TRANSACTION_ERROR:
2632 case COMP_BABBLE_DETECTED_ERROR:
2633 case COMP_ISOCH_BUFFER_OVERRUN:
2634 return xhci->quirks & XHCI_ETRON_HOST &&
2635 ring->type == TYPE_ISOC;
2636 default:
2637 return false;
2638 }
2639 }
2640
2641 /*
2642 * If this function returns an error condition, it means it got a Transfer
2643 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
2644 * At this point, the host controller is probably hosed and should be reset.
2645 */
handle_tx_event(struct xhci_hcd * xhci,struct xhci_interrupter * ir,struct xhci_transfer_event * event)2646 static int handle_tx_event(struct xhci_hcd *xhci,
2647 struct xhci_interrupter *ir,
2648 struct xhci_transfer_event *event)
2649 {
2650 struct xhci_virt_ep *ep;
2651 struct xhci_ring *ep_ring;
2652 unsigned int slot_id;
2653 int ep_index;
2654 struct xhci_td *td = NULL;
2655 dma_addr_t ep_trb_dma;
2656 struct xhci_segment *ep_seg;
2657 union xhci_trb *ep_trb;
2658 int status = -EINPROGRESS;
2659 struct xhci_ep_ctx *ep_ctx;
2660 u32 trb_comp_code;
2661 bool ring_xrun_event = false;
2662
2663 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
2664 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
2665 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
2666 ep_trb_dma = le64_to_cpu(event->buffer);
2667
2668 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
2669 if (!ep) {
2670 xhci_err(xhci, "ERROR Invalid Transfer event\n");
2671 goto err_out;
2672 }
2673
2674 ep_ring = xhci_dma_to_transfer_ring(ep, ep_trb_dma);
2675 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
2676
2677 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) {
2678 xhci_err(xhci,
2679 "ERROR Transfer event for disabled endpoint slot %u ep %u\n",
2680 slot_id, ep_index);
2681 goto err_out;
2682 }
2683
2684 if (!ep_ring)
2685 return handle_transferless_tx_event(xhci, ep, trb_comp_code);
2686
2687 /* Look for common error cases */
2688 switch (trb_comp_code) {
2689 /* Skip codes that require special handling depending on
2690 * transfer type
2691 */
2692 case COMP_SUCCESS:
2693 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
2694 trb_comp_code = COMP_SHORT_PACKET;
2695 xhci_dbg(xhci, "Successful completion on short TX for slot %u ep %u with last td comp code %d\n",
2696 slot_id, ep_index, ep_ring->old_trb_comp_code);
2697 }
2698 break;
2699 case COMP_SHORT_PACKET:
2700 break;
2701 /* Completion codes for endpoint stopped state */
2702 case COMP_STOPPED:
2703 xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n",
2704 slot_id, ep_index);
2705 break;
2706 case COMP_STOPPED_LENGTH_INVALID:
2707 xhci_dbg(xhci,
2708 "Stopped on No-op or Link TRB for slot %u ep %u\n",
2709 slot_id, ep_index);
2710 break;
2711 case COMP_STOPPED_SHORT_PACKET:
2712 xhci_dbg(xhci,
2713 "Stopped with short packet transfer detected for slot %u ep %u\n",
2714 slot_id, ep_index);
2715 break;
2716 /* Completion codes for endpoint halted state */
2717 case COMP_STALL_ERROR:
2718 xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id,
2719 ep_index);
2720 status = -EPIPE;
2721 break;
2722 case COMP_SPLIT_TRANSACTION_ERROR:
2723 xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n",
2724 slot_id, ep_index);
2725 status = -EPROTO;
2726 break;
2727 case COMP_USB_TRANSACTION_ERROR:
2728 xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n",
2729 slot_id, ep_index);
2730 status = -EPROTO;
2731 break;
2732 case COMP_BABBLE_DETECTED_ERROR:
2733 xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n",
2734 slot_id, ep_index);
2735 status = -EOVERFLOW;
2736 break;
2737 /* Completion codes for endpoint error state */
2738 case COMP_TRB_ERROR:
2739 xhci_warn(xhci,
2740 "WARN: TRB error for slot %u ep %u on endpoint\n",
2741 slot_id, ep_index);
2742 status = -EILSEQ;
2743 break;
2744 /* completion codes not indicating endpoint state change */
2745 case COMP_DATA_BUFFER_ERROR:
2746 xhci_warn(xhci,
2747 "WARN: HC couldn't access mem fast enough for slot %u ep %u\n",
2748 slot_id, ep_index);
2749 status = -ENOSR;
2750 break;
2751 case COMP_BANDWIDTH_OVERRUN_ERROR:
2752 xhci_warn(xhci,
2753 "WARN: bandwidth overrun event for slot %u ep %u on endpoint\n",
2754 slot_id, ep_index);
2755 break;
2756 case COMP_ISOCH_BUFFER_OVERRUN:
2757 xhci_warn(xhci,
2758 "WARN: buffer overrun event for slot %u ep %u on endpoint",
2759 slot_id, ep_index);
2760 break;
2761 case COMP_RING_UNDERRUN:
2762 /*
2763 * When the Isoch ring is empty, the xHC will generate
2764 * a Ring Overrun Event for IN Isoch endpoint or Ring
2765 * Underrun Event for OUT Isoch endpoint.
2766 */
2767 xhci_dbg(xhci, "Underrun event on slot %u ep %u\n", slot_id, ep_index);
2768 ring_xrun_event = true;
2769 break;
2770 case COMP_RING_OVERRUN:
2771 xhci_dbg(xhci, "Overrun event on slot %u ep %u\n", slot_id, ep_index);
2772 ring_xrun_event = true;
2773 break;
2774 case COMP_MISSED_SERVICE_ERROR:
2775 /*
2776 * When encounter missed service error, one or more isoc tds
2777 * may be missed by xHC.
2778 * Set skip flag of the ep_ring; Complete the missed tds as
2779 * short transfer when process the ep_ring next time.
2780 */
2781 ep->skip = true;
2782 xhci_dbg(xhci,
2783 "Miss service interval error for slot %u ep %u, set skip flag%s\n",
2784 slot_id, ep_index, ep_trb_dma ? ", skip now" : "");
2785 break;
2786 case COMP_NO_PING_RESPONSE_ERROR:
2787 ep->skip = true;
2788 xhci_dbg(xhci,
2789 "No Ping response error for slot %u ep %u, Skip one Isoc TD\n",
2790 slot_id, ep_index);
2791 return 0;
2792
2793 case COMP_INCOMPATIBLE_DEVICE_ERROR:
2794 /* needs disable slot command to recover */
2795 xhci_warn(xhci,
2796 "WARN: detect an incompatible device for slot %u ep %u",
2797 slot_id, ep_index);
2798 status = -EPROTO;
2799 break;
2800 default:
2801 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
2802 status = 0;
2803 break;
2804 }
2805 xhci_warn(xhci,
2806 "ERROR Unknown event condition %u for slot %u ep %u , HC probably busted\n",
2807 trb_comp_code, slot_id, ep_index);
2808 if (ep->skip)
2809 break;
2810 return 0;
2811 }
2812
2813 /*
2814 * xhci 4.10.2 states isoc endpoints should continue
2815 * processing the next TD if there was an error mid TD.
2816 * So host like NEC don't generate an event for the last
2817 * isoc TRB even if the IOC flag is set.
2818 * xhci 4.9.1 states that if there are errors in mult-TRB
2819 * TDs xHC should generate an error for that TRB, and if xHC
2820 * proceeds to the next TD it should genete an event for
2821 * any TRB with IOC flag on the way. Other host follow this.
2822 *
2823 * We wait for the final IOC event, but if we get an event
2824 * anywhere outside this TD, just give it back already.
2825 */
2826 td = list_first_entry_or_null(&ep_ring->td_list, struct xhci_td, td_list);
2827
2828 if (td && td->error_mid_td && !trb_in_td(td, ep_trb_dma)) {
2829 xhci_dbg(xhci, "Missing TD completion event after mid TD error\n");
2830 xhci_dequeue_td(xhci, td, ep_ring, td->status);
2831 }
2832
2833 /* If the TRB pointer is NULL, missed TDs will be skipped on the next event */
2834 if (trb_comp_code == COMP_MISSED_SERVICE_ERROR && !ep_trb_dma)
2835 return 0;
2836
2837 if (list_empty(&ep_ring->td_list)) {
2838 /*
2839 * Don't print wanings if ring is empty due to a stopped endpoint generating an
2840 * extra completion event if the device was suspended. Or, a event for the last TRB
2841 * of a short TD we already got a short event for. The short TD is already removed
2842 * from the TD list.
2843 */
2844 if (trb_comp_code != COMP_STOPPED &&
2845 trb_comp_code != COMP_STOPPED_LENGTH_INVALID &&
2846 !ring_xrun_event &&
2847 !xhci_spurious_success_tx_event(xhci, ep_ring)) {
2848 xhci_warn(xhci, "Event TRB for slot %u ep %u with no TDs queued\n",
2849 slot_id, ep_index);
2850 }
2851
2852 ep->skip = false;
2853 goto check_endpoint_halted;
2854 }
2855
2856 do {
2857 td = list_first_entry(&ep_ring->td_list, struct xhci_td,
2858 td_list);
2859
2860 /* Is this a TRB in the currently executing TD? */
2861 ep_seg = trb_in_td(td, ep_trb_dma);
2862
2863 if (!ep_seg) {
2864
2865 if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
2866 /* this event is unlikely to match any TD, don't skip them all */
2867 if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID)
2868 return 0;
2869
2870 skip_isoc_td(xhci, td, ep, status);
2871
2872 if (!list_empty(&ep_ring->td_list)) {
2873 if (ring_xrun_event) {
2874 /*
2875 * If we are here, we are on xHCI 1.0 host with no
2876 * idea how many TDs were missed or where the xrun
2877 * occurred. New TDs may have been added after the
2878 * xrun, so skip only one TD to be safe.
2879 */
2880 xhci_dbg(xhci, "Skipped one TD for slot %u ep %u",
2881 slot_id, ep_index);
2882 return 0;
2883 }
2884 continue;
2885 }
2886
2887 xhci_dbg(xhci, "All TDs skipped for slot %u ep %u. Clear skip flag.\n",
2888 slot_id, ep_index);
2889 ep->skip = false;
2890 td = NULL;
2891 goto check_endpoint_halted;
2892 }
2893
2894 /* TD was queued after xrun, maybe xrun was on a link, don't panic yet */
2895 if (ring_xrun_event)
2896 return 0;
2897
2898 /*
2899 * Skip the Force Stopped Event. The 'ep_trb' of FSE is not in the current
2900 * TD pointed by 'ep_ring->dequeue' because that the hardware dequeue
2901 * pointer still at the previous TRB of the current TD. The previous TRB
2902 * maybe a Link TD or the last TRB of the previous TD. The command
2903 * completion handle will take care the rest.
2904 */
2905 if (trb_comp_code == COMP_STOPPED ||
2906 trb_comp_code == COMP_STOPPED_LENGTH_INVALID) {
2907 return 0;
2908 }
2909
2910 /*
2911 * Some hosts give a spurious success event after a short
2912 * transfer or error on last TRB. Ignore it.
2913 */
2914 if (xhci_spurious_success_tx_event(xhci, ep_ring)) {
2915 xhci_dbg(xhci, "Spurious event dma %pad, comp_code %u after %u\n",
2916 &ep_trb_dma, trb_comp_code, ep_ring->old_trb_comp_code);
2917 ep_ring->old_trb_comp_code = 0;
2918 return 0;
2919 }
2920
2921 /* HC is busted, give up! */
2922 goto debug_finding_td;
2923 }
2924
2925 if (ep->skip) {
2926 xhci_dbg(xhci,
2927 "Found td. Clear skip flag for slot %u ep %u.\n",
2928 slot_id, ep_index);
2929 ep->skip = false;
2930 }
2931
2932 /*
2933 * If ep->skip is set, it means there are missed tds on the
2934 * endpoint ring need to take care of.
2935 * Process them as short transfer until reach the td pointed by
2936 * the event.
2937 */
2938 } while (ep->skip);
2939
2940 ep_ring->old_trb_comp_code = trb_comp_code;
2941
2942 /* Get out if a TD was queued at enqueue after the xrun occurred */
2943 if (ring_xrun_event)
2944 return 0;
2945
2946 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / sizeof(*ep_trb)];
2947 trace_xhci_handle_transfer(ep_ring, (struct xhci_generic_trb *) ep_trb, ep_trb_dma);
2948
2949 /*
2950 * No-op TRB could trigger interrupts in a case where a URB was killed
2951 * and a STALL_ERROR happens right after the endpoint ring stopped.
2952 * Reset the halted endpoint. Otherwise, the endpoint remains stalled
2953 * indefinitely.
2954 */
2955
2956 if (trb_is_noop(ep_trb))
2957 goto check_endpoint_halted;
2958
2959 td->status = status;
2960
2961 /* update the urb's actual_length and give back to the core */
2962 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2963 process_ctrl_td(xhci, ep, ep_ring, td, ep_trb, event);
2964 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2965 process_isoc_td(xhci, ep, ep_ring, td, ep_trb, event);
2966 else
2967 process_bulk_intr_td(xhci, ep, ep_ring, td, ep_trb, event);
2968 return 0;
2969
2970 check_endpoint_halted:
2971 if (xhci_halted_host_endpoint(ep_ctx, trb_comp_code))
2972 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET);
2973
2974 return 0;
2975
2976 debug_finding_td:
2977 xhci_err(xhci, "Event dma %pad for ep %d status %d not part of TD at %016llx - %016llx\n",
2978 &ep_trb_dma, ep_index, trb_comp_code,
2979 (unsigned long long)xhci_trb_virt_to_dma(td->start_seg, td->start_trb),
2980 (unsigned long long)xhci_trb_virt_to_dma(td->end_seg, td->end_trb));
2981
2982 return -ESHUTDOWN;
2983
2984 err_out:
2985 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
2986 (unsigned long long) xhci_trb_virt_to_dma(
2987 ir->event_ring->deq_seg,
2988 ir->event_ring->dequeue),
2989 lower_32_bits(le64_to_cpu(event->buffer)),
2990 upper_32_bits(le64_to_cpu(event->buffer)),
2991 le32_to_cpu(event->transfer_len),
2992 le32_to_cpu(event->flags));
2993 return -ENODEV;
2994 }
2995
2996 /*
2997 * This function handles one OS-owned event on the event ring. It may drop
2998 * xhci->lock between event processing (e.g. to pass up port status changes).
2999 */
xhci_handle_event_trb(struct xhci_hcd * xhci,struct xhci_interrupter * ir,union xhci_trb * event)3000 static int xhci_handle_event_trb(struct xhci_hcd *xhci, struct xhci_interrupter *ir,
3001 union xhci_trb *event)
3002 {
3003 u32 trb_type;
3004
3005 trace_xhci_handle_event(ir->event_ring, &event->generic,
3006 xhci_trb_virt_to_dma(ir->event_ring->deq_seg,
3007 ir->event_ring->dequeue));
3008
3009 /*
3010 * Barrier between reading the TRB_CYCLE (valid) flag before, and any
3011 * speculative reads of the event's flags/data below.
3012 */
3013 rmb();
3014 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
3015 /* FIXME: Handle more event types. */
3016
3017 switch (trb_type) {
3018 case TRB_COMPLETION:
3019 handle_cmd_completion(xhci, &event->event_cmd);
3020 break;
3021 case TRB_PORT_STATUS:
3022 handle_port_status(xhci, event);
3023 break;
3024 case TRB_TRANSFER:
3025 handle_tx_event(xhci, ir, &event->trans_event);
3026 break;
3027 case TRB_DEV_NOTE:
3028 handle_device_notification(xhci, event);
3029 break;
3030 default:
3031 if (trb_type >= TRB_VENDOR_DEFINED_LOW)
3032 handle_vendor_event(xhci, event, trb_type);
3033 else
3034 xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type);
3035 }
3036 /* Any of the above functions may drop and re-acquire the lock, so check
3037 * to make sure a watchdog timer didn't mark the host as non-responsive.
3038 */
3039 if (xhci->xhc_state & XHCI_STATE_DYING) {
3040 xhci_dbg(xhci, "xHCI host dying, returning from event handler.\n");
3041 return -ENODEV;
3042 }
3043
3044 return 0;
3045 }
3046
3047 /*
3048 * Update Event Ring Dequeue Pointer:
3049 * - When all events have finished
3050 * - To avoid "Event Ring Full Error" condition
3051 */
xhci_update_erst_dequeue(struct xhci_hcd * xhci,struct xhci_interrupter * ir,bool clear_ehb)3052 void xhci_update_erst_dequeue(struct xhci_hcd *xhci,
3053 struct xhci_interrupter *ir,
3054 bool clear_ehb)
3055 {
3056 u64 temp_64;
3057 dma_addr_t deq;
3058
3059 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
3060 deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg,
3061 ir->event_ring->dequeue);
3062 if (deq == 0)
3063 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr\n");
3064 /*
3065 * Per 4.9.4, Software writes to the ERDP register shall always advance
3066 * the Event Ring Dequeue Pointer value.
3067 */
3068 if ((temp_64 & ERST_PTR_MASK) == (deq & ERST_PTR_MASK) && !clear_ehb)
3069 return;
3070
3071 /* Update HC event ring dequeue pointer */
3072 temp_64 = ir->event_ring->deq_seg->num & ERST_DESI_MASK;
3073 temp_64 |= deq & ERST_PTR_MASK;
3074
3075 /* Clear the event handler busy flag (RW1C) */
3076 if (clear_ehb)
3077 temp_64 |= ERST_EHB;
3078 xhci_write_64(xhci, temp_64, &ir->ir_set->erst_dequeue);
3079 }
3080
3081 /* Clear the interrupt pending bit for a specific interrupter. */
xhci_clear_interrupt_pending(struct xhci_interrupter * ir)3082 static void xhci_clear_interrupt_pending(struct xhci_interrupter *ir)
3083 {
3084 if (!ir->ip_autoclear) {
3085 u32 iman;
3086
3087 iman = readl(&ir->ir_set->iman);
3088 iman |= IMAN_IP;
3089 writel(iman, &ir->ir_set->iman);
3090
3091 /* Read operation to guarantee the write has been flushed from posted buffers */
3092 readl(&ir->ir_set->iman);
3093 }
3094 }
3095
3096 /*
3097 * Handle all OS-owned events on an interrupter event ring. It may drop
3098 * and reaquire xhci->lock between event processing.
3099 */
xhci_handle_events(struct xhci_hcd * xhci,struct xhci_interrupter * ir,bool skip_events)3100 static int xhci_handle_events(struct xhci_hcd *xhci, struct xhci_interrupter *ir,
3101 bool skip_events)
3102 {
3103 int event_loop = 0;
3104 int err = 0;
3105 u64 temp;
3106
3107 xhci_clear_interrupt_pending(ir);
3108
3109 /* Event ring hasn't been allocated yet. */
3110 if (!ir->event_ring || !ir->event_ring->dequeue) {
3111 xhci_err(xhci, "ERROR interrupter event ring not ready\n");
3112 return -ENOMEM;
3113 }
3114
3115 if (xhci->xhc_state & XHCI_STATE_DYING ||
3116 xhci->xhc_state & XHCI_STATE_HALTED) {
3117 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. Shouldn't IRQs be disabled?\n");
3118
3119 /* Clear the event handler busy flag (RW1C) */
3120 temp = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
3121 xhci_write_64(xhci, temp | ERST_EHB, &ir->ir_set->erst_dequeue);
3122 return -ENODEV;
3123 }
3124
3125 /* Process all OS owned event TRBs on this event ring */
3126 while (unhandled_event_trb(ir->event_ring)) {
3127 if (!skip_events)
3128 err = xhci_handle_event_trb(xhci, ir, ir->event_ring->dequeue);
3129
3130 /*
3131 * If half a segment of events have been handled in one go then
3132 * update ERDP, and force isoc trbs to interrupt more often
3133 */
3134 if (event_loop++ > TRBS_PER_SEGMENT / 2) {
3135 xhci_update_erst_dequeue(xhci, ir, false);
3136
3137 if (ir->isoc_bei_interval > AVOID_BEI_INTERVAL_MIN)
3138 ir->isoc_bei_interval = ir->isoc_bei_interval / 2;
3139
3140 event_loop = 0;
3141 }
3142
3143 /* Update SW event ring dequeue pointer */
3144 inc_deq(xhci, ir->event_ring);
3145
3146 if (err)
3147 break;
3148 }
3149
3150 xhci_update_erst_dequeue(xhci, ir, true);
3151
3152 return 0;
3153 }
3154
3155 /*
3156 * Move the event ring dequeue pointer to skip events kept in the secondary
3157 * event ring. This is used to ensure that pending events in the ring are
3158 * acknowledged, so the xHCI HCD can properly enter suspend/resume. The
3159 * secondary ring is typically maintained by an external component.
3160 */
xhci_skip_sec_intr_events(struct xhci_hcd * xhci,struct xhci_ring * ring,struct xhci_interrupter * ir)3161 void xhci_skip_sec_intr_events(struct xhci_hcd *xhci,
3162 struct xhci_ring *ring, struct xhci_interrupter *ir)
3163 {
3164 union xhci_trb *current_trb;
3165 u64 erdp_reg;
3166 dma_addr_t deq;
3167
3168 /* disable irq, ack pending interrupt and ack all pending events */
3169 xhci_disable_interrupter(xhci, ir);
3170
3171 /* last acked event trb is in erdp reg */
3172 erdp_reg = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
3173 deq = (dma_addr_t)(erdp_reg & ERST_PTR_MASK);
3174 if (!deq) {
3175 xhci_err(xhci, "event ring handling not required\n");
3176 return;
3177 }
3178
3179 current_trb = ir->event_ring->dequeue;
3180 /* read cycle state of the last acked trb to find out CCS */
3181 ring->cycle_state = le32_to_cpu(current_trb->event_cmd.flags) & TRB_CYCLE;
3182
3183 xhci_handle_events(xhci, ir, true);
3184 }
3185
3186 /*
3187 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
3188 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
3189 * indicators of an event TRB error, but we check the status *first* to be safe.
3190 */
xhci_irq(struct usb_hcd * hcd)3191 irqreturn_t xhci_irq(struct usb_hcd *hcd)
3192 {
3193 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3194 irqreturn_t ret = IRQ_HANDLED;
3195 u32 status;
3196
3197 spin_lock(&xhci->lock);
3198 /* Check if the xHC generated the interrupt, or the irq is shared */
3199 status = readl(&xhci->op_regs->status);
3200 if (status == ~(u32)0) {
3201 xhci_hc_died(xhci);
3202 goto out;
3203 }
3204
3205 if (!(status & STS_EINT)) {
3206 ret = IRQ_NONE;
3207 goto out;
3208 }
3209
3210 if (status & STS_HCE) {
3211 xhci_warn(xhci, "WARNING: Host Controller Error\n");
3212 goto out;
3213 }
3214
3215 if (status & STS_FATAL) {
3216 xhci_warn(xhci, "WARNING: Host System Error\n");
3217 xhci_halt(xhci);
3218 goto out;
3219 }
3220
3221 /*
3222 * Clear the op reg interrupt status first,
3223 * so we can receive interrupts from other MSI-X interrupters.
3224 * Write 1 to clear the interrupt status.
3225 */
3226 status |= STS_EINT;
3227 writel(status, &xhci->op_regs->status);
3228
3229 /* This is the handler of the primary interrupter */
3230 xhci_handle_events(xhci, xhci->interrupters[0], false);
3231 out:
3232 spin_unlock(&xhci->lock);
3233
3234 return ret;
3235 }
3236
xhci_msi_irq(int irq,void * hcd)3237 irqreturn_t xhci_msi_irq(int irq, void *hcd)
3238 {
3239 return xhci_irq(hcd);
3240 }
3241 EXPORT_SYMBOL_GPL(xhci_msi_irq);
3242
3243 /**** Endpoint Ring Operations ****/
3244
3245 /*
3246 * Generic function for queueing a TRB on a ring.
3247 * The caller must have checked to make sure there's room on the ring.
3248 *
3249 * @more_trbs_coming: Will you enqueue more TRBs before calling
3250 * prepare_transfer()?
3251 */
queue_trb(struct xhci_hcd * xhci,struct xhci_ring * ring,bool more_trbs_coming,u32 field1,u32 field2,u32 field3,u32 field4)3252 static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
3253 bool more_trbs_coming,
3254 u32 field1, u32 field2, u32 field3, u32 field4)
3255 {
3256 struct xhci_generic_trb *trb;
3257
3258 trb = &ring->enqueue->generic;
3259 trb->field[0] = cpu_to_le32(field1);
3260 trb->field[1] = cpu_to_le32(field2);
3261 trb->field[2] = cpu_to_le32(field3);
3262 /* make sure TRB is fully written before giving it to the controller */
3263 wmb();
3264 trb->field[3] = cpu_to_le32(field4);
3265
3266 trace_xhci_queue_trb(ring, trb,
3267 xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue));
3268
3269 inc_enq(xhci, ring, more_trbs_coming);
3270 }
3271
3272 /*
3273 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
3274 * expand ring if it start to be full.
3275 */
prepare_ring(struct xhci_hcd * xhci,struct xhci_ring * ep_ring,u32 ep_state,unsigned int num_trbs,gfp_t mem_flags)3276 static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
3277 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
3278 {
3279 unsigned int new_segs = 0;
3280
3281 /* Make sure the endpoint has been added to xHC schedule */
3282 switch (ep_state) {
3283 case EP_STATE_DISABLED:
3284 /*
3285 * USB core changed config/interfaces without notifying us,
3286 * or hardware is reporting the wrong state.
3287 */
3288 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
3289 return -ENOENT;
3290 case EP_STATE_ERROR:
3291 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
3292 /* FIXME event handling code for error needs to clear it */
3293 /* XXX not sure if this should be -ENOENT or not */
3294 return -EINVAL;
3295 case EP_STATE_HALTED:
3296 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
3297 break;
3298 case EP_STATE_STOPPED:
3299 case EP_STATE_RUNNING:
3300 break;
3301 default:
3302 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
3303 /*
3304 * FIXME issue Configure Endpoint command to try to get the HC
3305 * back into a known state.
3306 */
3307 return -EINVAL;
3308 }
3309
3310 if (ep_ring != xhci->cmd_ring) {
3311 new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs);
3312 } else if (xhci_num_trbs_free(ep_ring) <= num_trbs) {
3313 xhci_err(xhci, "Do not support expand command ring\n");
3314 return -ENOMEM;
3315 }
3316
3317 if (new_segs) {
3318 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
3319 "ERROR no room on ep ring, try ring expansion");
3320 if (xhci_ring_expansion(xhci, ep_ring, new_segs, mem_flags)) {
3321 xhci_err(xhci, "Ring expansion failed\n");
3322 return -ENOMEM;
3323 }
3324 }
3325
3326 /* Ensure that new TRBs won't overwrite a link */
3327 if (trb_is_link(ep_ring->enqueue))
3328 inc_enq_past_link(xhci, ep_ring, 0);
3329
3330 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) {
3331 xhci_warn(xhci, "Missing link TRB at end of ring segment\n");
3332 return -EINVAL;
3333 }
3334
3335 return 0;
3336 }
3337
prepare_transfer(struct xhci_hcd * xhci,struct xhci_virt_device * xdev,unsigned int ep_index,unsigned int stream_id,unsigned int num_trbs,struct urb * urb,unsigned int td_index,gfp_t mem_flags)3338 static int prepare_transfer(struct xhci_hcd *xhci,
3339 struct xhci_virt_device *xdev,
3340 unsigned int ep_index,
3341 unsigned int stream_id,
3342 unsigned int num_trbs,
3343 struct urb *urb,
3344 unsigned int td_index,
3345 gfp_t mem_flags)
3346 {
3347 int ret;
3348 struct urb_priv *urb_priv;
3349 struct xhci_td *td;
3350 struct xhci_ring *ep_ring;
3351 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3352
3353 ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index,
3354 stream_id);
3355 if (!ep_ring) {
3356 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
3357 stream_id);
3358 return -EINVAL;
3359 }
3360
3361 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx),
3362 num_trbs, mem_flags);
3363 if (ret)
3364 return ret;
3365
3366 urb_priv = urb->hcpriv;
3367 td = &urb_priv->td[td_index];
3368
3369 INIT_LIST_HEAD(&td->td_list);
3370 INIT_LIST_HEAD(&td->cancelled_td_list);
3371
3372 if (td_index == 0) {
3373 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
3374 if (unlikely(ret))
3375 return ret;
3376 }
3377
3378 td->urb = urb;
3379 /* Add this TD to the tail of the endpoint ring's TD list */
3380 list_add_tail(&td->td_list, &ep_ring->td_list);
3381 td->start_seg = ep_ring->enq_seg;
3382 td->start_trb = ep_ring->enqueue;
3383
3384 return 0;
3385 }
3386
count_trbs(u64 addr,u64 len)3387 unsigned int count_trbs(u64 addr, u64 len)
3388 {
3389 unsigned int num_trbs;
3390
3391 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
3392 TRB_MAX_BUFF_SIZE);
3393 if (num_trbs == 0)
3394 num_trbs++;
3395
3396 return num_trbs;
3397 }
3398
count_trbs_needed(struct urb * urb)3399 static inline unsigned int count_trbs_needed(struct urb *urb)
3400 {
3401 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length);
3402 }
3403
count_sg_trbs_needed(struct urb * urb)3404 static unsigned int count_sg_trbs_needed(struct urb *urb)
3405 {
3406 struct scatterlist *sg;
3407 unsigned int i, len, full_len, num_trbs = 0;
3408
3409 full_len = urb->transfer_buffer_length;
3410
3411 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
3412 len = sg_dma_len(sg);
3413 num_trbs += count_trbs(sg_dma_address(sg), len);
3414 len = min_t(unsigned int, len, full_len);
3415 full_len -= len;
3416 if (full_len == 0)
3417 break;
3418 }
3419
3420 return num_trbs;
3421 }
3422
count_isoc_trbs_needed(struct urb * urb,int i)3423 static unsigned int count_isoc_trbs_needed(struct urb *urb, int i)
3424 {
3425 u64 addr, len;
3426
3427 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
3428 len = urb->iso_frame_desc[i].length;
3429
3430 return count_trbs(addr, len);
3431 }
3432
check_trb_math(struct urb * urb,int running_total)3433 static void check_trb_math(struct urb *urb, int running_total)
3434 {
3435 if (unlikely(running_total != urb->transfer_buffer_length))
3436 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
3437 "queued %#x (%d), asked for %#x (%d)\n",
3438 __func__,
3439 urb->ep->desc.bEndpointAddress,
3440 running_total, running_total,
3441 urb->transfer_buffer_length,
3442 urb->transfer_buffer_length);
3443 }
3444
giveback_first_trb(struct xhci_hcd * xhci,int slot_id,unsigned int ep_index,unsigned int stream_id,int start_cycle,struct xhci_generic_trb * start_trb)3445 static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
3446 unsigned int ep_index, unsigned int stream_id, int start_cycle,
3447 struct xhci_generic_trb *start_trb)
3448 {
3449 /*
3450 * Pass all the TRBs to the hardware at once and make sure this write
3451 * isn't reordered.
3452 */
3453 wmb();
3454 if (start_cycle)
3455 start_trb->field[3] |= cpu_to_le32(start_cycle);
3456 else
3457 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
3458 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
3459 }
3460
check_interval(struct urb * urb,struct xhci_ep_ctx * ep_ctx)3461 static void check_interval(struct urb *urb, struct xhci_ep_ctx *ep_ctx)
3462 {
3463 int xhci_interval;
3464 int ep_interval;
3465
3466 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
3467 ep_interval = urb->interval;
3468
3469 /* Convert to microframes */
3470 if (urb->dev->speed == USB_SPEED_LOW ||
3471 urb->dev->speed == USB_SPEED_FULL)
3472 ep_interval *= 8;
3473
3474 /* FIXME change this to a warning and a suggestion to use the new API
3475 * to set the polling interval (once the API is added).
3476 */
3477 if (xhci_interval != ep_interval) {
3478 dev_dbg_ratelimited(&urb->dev->dev,
3479 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n",
3480 ep_interval, str_plural(ep_interval),
3481 xhci_interval, str_plural(xhci_interval));
3482 urb->interval = xhci_interval;
3483 /* Convert back to frames for LS/FS devices */
3484 if (urb->dev->speed == USB_SPEED_LOW ||
3485 urb->dev->speed == USB_SPEED_FULL)
3486 urb->interval /= 8;
3487 }
3488 }
3489
3490 /*
3491 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
3492 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
3493 * (comprised of sg list entries) can take several service intervals to
3494 * transmit.
3495 */
xhci_queue_intr_tx(struct xhci_hcd * xhci,gfp_t mem_flags,struct urb * urb,int slot_id,unsigned int ep_index)3496 int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3497 struct urb *urb, int slot_id, unsigned int ep_index)
3498 {
3499 struct xhci_ep_ctx *ep_ctx;
3500
3501 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index);
3502 check_interval(urb, ep_ctx);
3503
3504 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
3505 }
3506
3507 /*
3508 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
3509 * packets remaining in the TD (*not* including this TRB).
3510 *
3511 * Total TD packet count = total_packet_count =
3512 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
3513 *
3514 * Packets transferred up to and including this TRB = packets_transferred =
3515 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
3516 *
3517 * TD size = total_packet_count - packets_transferred
3518 *
3519 * For xHCI 0.96 and older, TD size field should be the remaining bytes
3520 * including this TRB, right shifted by 10
3521 *
3522 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31.
3523 * This is taken care of in the TRB_TD_SIZE() macro
3524 *
3525 * The last TRB in a TD must have the TD size set to zero.
3526 */
xhci_td_remainder(struct xhci_hcd * xhci,int transferred,int trb_buff_len,unsigned int td_total_len,struct urb * urb,bool more_trbs_coming)3527 static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred,
3528 int trb_buff_len, unsigned int td_total_len,
3529 struct urb *urb, bool more_trbs_coming)
3530 {
3531 u32 maxp, total_packet_count;
3532
3533 /* MTK xHCI 0.96 contains some features from 1.0 */
3534 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST))
3535 return ((td_total_len - transferred) >> 10);
3536
3537 /* One TRB with a zero-length data packet. */
3538 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
3539 trb_buff_len == td_total_len)
3540 return 0;
3541
3542 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */
3543 if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100))
3544 trb_buff_len = 0;
3545
3546 maxp = usb_endpoint_maxp(&urb->ep->desc);
3547 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
3548
3549 /* Queueing functions don't count the current TRB into transferred */
3550 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
3551 }
3552
3553
xhci_align_td(struct xhci_hcd * xhci,struct urb * urb,u32 enqd_len,u32 * trb_buff_len,struct xhci_segment * seg)3554 static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len,
3555 u32 *trb_buff_len, struct xhci_segment *seg)
3556 {
3557 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
3558 unsigned int unalign;
3559 unsigned int max_pkt;
3560 u32 new_buff_len;
3561 size_t len;
3562
3563 max_pkt = usb_endpoint_maxp(&urb->ep->desc);
3564 unalign = (enqd_len + *trb_buff_len) % max_pkt;
3565
3566 /* we got lucky, last normal TRB data on segment is packet aligned */
3567 if (unalign == 0)
3568 return 0;
3569
3570 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n",
3571 unalign, *trb_buff_len);
3572
3573 /* is the last nornal TRB alignable by splitting it */
3574 if (*trb_buff_len > unalign) {
3575 *trb_buff_len -= unalign;
3576 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len);
3577 return 0;
3578 }
3579
3580 /*
3581 * We want enqd_len + trb_buff_len to sum up to a number aligned to
3582 * number which is divisible by the endpoint's wMaxPacketSize. IOW:
3583 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0.
3584 */
3585 new_buff_len = max_pkt - (enqd_len % max_pkt);
3586
3587 if (new_buff_len > (urb->transfer_buffer_length - enqd_len))
3588 new_buff_len = (urb->transfer_buffer_length - enqd_len);
3589
3590 /* create a max max_pkt sized bounce buffer pointed to by last trb */
3591 if (usb_urb_dir_out(urb)) {
3592 if (urb->num_sgs) {
3593 len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
3594 seg->bounce_buf, new_buff_len, enqd_len);
3595 if (len != new_buff_len)
3596 xhci_warn(xhci, "WARN Wrong bounce buffer write length: %zu != %d\n",
3597 len, new_buff_len);
3598 } else {
3599 memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len);
3600 }
3601
3602 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3603 max_pkt, DMA_TO_DEVICE);
3604 } else {
3605 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3606 max_pkt, DMA_FROM_DEVICE);
3607 }
3608
3609 if (dma_mapping_error(dev, seg->bounce_dma)) {
3610 /* try without aligning. Some host controllers survive */
3611 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n");
3612 return 0;
3613 }
3614 *trb_buff_len = new_buff_len;
3615 seg->bounce_len = new_buff_len;
3616 seg->bounce_offs = enqd_len;
3617
3618 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len);
3619
3620 return 1;
3621 }
3622
3623 /* This is very similar to what ehci-q.c qtd_fill() does */
xhci_queue_bulk_tx(struct xhci_hcd * xhci,gfp_t mem_flags,struct urb * urb,int slot_id,unsigned int ep_index)3624 int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3625 struct urb *urb, int slot_id, unsigned int ep_index)
3626 {
3627 struct xhci_ring *ring;
3628 struct urb_priv *urb_priv;
3629 struct xhci_td *td;
3630 struct xhci_generic_trb *start_trb;
3631 struct scatterlist *sg = NULL;
3632 bool more_trbs_coming = true;
3633 bool need_zero_pkt = false;
3634 bool first_trb = true;
3635 unsigned int num_trbs;
3636 unsigned int start_cycle, num_sgs = 0;
3637 unsigned int enqd_len, block_len, trb_buff_len, full_len;
3638 int sent_len, ret;
3639 u32 field, length_field, remainder;
3640 u64 addr, send_addr;
3641
3642 ring = xhci_urb_to_transfer_ring(xhci, urb);
3643 if (!ring)
3644 return -EINVAL;
3645
3646 full_len = urb->transfer_buffer_length;
3647 /* If we have scatter/gather list, we use it. */
3648 if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) {
3649 num_sgs = urb->num_mapped_sgs;
3650 sg = urb->sg;
3651 addr = (u64) sg_dma_address(sg);
3652 block_len = sg_dma_len(sg);
3653 num_trbs = count_sg_trbs_needed(urb);
3654 } else {
3655 num_trbs = count_trbs_needed(urb);
3656 addr = (u64) urb->transfer_dma;
3657 block_len = full_len;
3658 }
3659 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3660 ep_index, urb->stream_id,
3661 num_trbs, urb, 0, mem_flags);
3662 if (unlikely(ret < 0))
3663 return ret;
3664
3665 urb_priv = urb->hcpriv;
3666
3667 /* Deal with URB_ZERO_PACKET - need one more td/trb */
3668 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1)
3669 need_zero_pkt = true;
3670
3671 td = &urb_priv->td[0];
3672
3673 /*
3674 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3675 * until we've finished creating all the other TRBs. The ring's cycle
3676 * state may change as we enqueue the other TRBs, so save it too.
3677 */
3678 start_trb = &ring->enqueue->generic;
3679 start_cycle = ring->cycle_state;
3680 send_addr = addr;
3681
3682 /* Queue the TRBs, even if they are zero-length */
3683 for (enqd_len = 0; first_trb || enqd_len < full_len;
3684 enqd_len += trb_buff_len) {
3685 field = TRB_TYPE(TRB_NORMAL);
3686
3687 /* TRB buffer should not cross 64KB boundaries */
3688 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
3689 trb_buff_len = min_t(unsigned int, trb_buff_len, block_len);
3690
3691 if (enqd_len + trb_buff_len > full_len)
3692 trb_buff_len = full_len - enqd_len;
3693
3694 /* Don't change the cycle bit of the first TRB until later */
3695 if (first_trb) {
3696 first_trb = false;
3697 if (start_cycle == 0)
3698 field |= TRB_CYCLE;
3699 } else
3700 field |= ring->cycle_state;
3701
3702 /* Chain all the TRBs together; clear the chain bit in the last
3703 * TRB to indicate it's the last TRB in the chain.
3704 */
3705 if (enqd_len + trb_buff_len < full_len) {
3706 field |= TRB_CHAIN;
3707 if (trb_is_link(ring->enqueue + 1)) {
3708 if (xhci_align_td(xhci, urb, enqd_len,
3709 &trb_buff_len,
3710 ring->enq_seg)) {
3711 send_addr = ring->enq_seg->bounce_dma;
3712 /* assuming TD won't span 2 segs */
3713 td->bounce_seg = ring->enq_seg;
3714 }
3715 }
3716 }
3717 if (enqd_len + trb_buff_len >= full_len) {
3718 field &= ~TRB_CHAIN;
3719 field |= TRB_IOC;
3720 more_trbs_coming = false;
3721 td->end_trb = ring->enqueue;
3722 td->end_seg = ring->enq_seg;
3723 if (xhci_urb_suitable_for_idt(urb)) {
3724 memcpy(&send_addr, urb->transfer_buffer,
3725 trb_buff_len);
3726 le64_to_cpus(&send_addr);
3727 field |= TRB_IDT;
3728 }
3729 }
3730
3731 /* Only set interrupt on short packet for IN endpoints */
3732 if (usb_urb_dir_in(urb))
3733 field |= TRB_ISP;
3734
3735 /* Set the TRB length, TD size, and interrupter fields. */
3736 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len,
3737 full_len, urb, more_trbs_coming);
3738
3739 length_field = TRB_LEN(trb_buff_len) |
3740 TRB_TD_SIZE(remainder) |
3741 TRB_INTR_TARGET(0);
3742
3743 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt,
3744 lower_32_bits(send_addr),
3745 upper_32_bits(send_addr),
3746 length_field,
3747 field);
3748 addr += trb_buff_len;
3749 sent_len = trb_buff_len;
3750
3751 while (sg && sent_len >= block_len) {
3752 /* New sg entry */
3753 --num_sgs;
3754 sent_len -= block_len;
3755 sg = sg_next(sg);
3756 if (num_sgs != 0 && sg) {
3757 block_len = sg_dma_len(sg);
3758 addr = (u64) sg_dma_address(sg);
3759 addr += sent_len;
3760 }
3761 }
3762 block_len -= sent_len;
3763 send_addr = addr;
3764 }
3765
3766 if (need_zero_pkt) {
3767 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3768 ep_index, urb->stream_id,
3769 1, urb, 1, mem_flags);
3770 urb_priv->td[1].end_trb = ring->enqueue;
3771 urb_priv->td[1].end_seg = ring->enq_seg;
3772 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC;
3773 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field);
3774 }
3775
3776 check_trb_math(urb, enqd_len);
3777 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3778 start_cycle, start_trb);
3779 return 0;
3780 }
3781
3782 /* Caller must have locked xhci->lock */
xhci_queue_ctrl_tx(struct xhci_hcd * xhci,gfp_t mem_flags,struct urb * urb,int slot_id,unsigned int ep_index)3783 int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3784 struct urb *urb, int slot_id, unsigned int ep_index)
3785 {
3786 struct xhci_ring *ep_ring;
3787 int num_trbs;
3788 int ret;
3789 struct usb_ctrlrequest *setup;
3790 struct xhci_generic_trb *start_trb;
3791 int start_cycle;
3792 u32 field;
3793 struct urb_priv *urb_priv;
3794 struct xhci_td *td;
3795
3796 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
3797 if (!ep_ring)
3798 return -EINVAL;
3799
3800 /*
3801 * Need to copy setup packet into setup TRB, so we can't use the setup
3802 * DMA address.
3803 */
3804 if (!urb->setup_packet)
3805 return -EINVAL;
3806
3807 if ((xhci->quirks & XHCI_ETRON_HOST) &&
3808 urb->dev->speed >= USB_SPEED_SUPER) {
3809 /*
3810 * If next available TRB is the Link TRB in the ring segment then
3811 * enqueue a No Op TRB, this can prevent the Setup and Data Stage
3812 * TRB to be breaked by the Link TRB.
3813 */
3814 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue + 1)) {
3815 field = TRB_TYPE(TRB_TR_NOOP) | ep_ring->cycle_state;
3816 queue_trb(xhci, ep_ring, false, 0, 0,
3817 TRB_INTR_TARGET(0), field);
3818 }
3819 }
3820
3821 /* 1 TRB for setup, 1 for status */
3822 num_trbs = 2;
3823 /*
3824 * Don't need to check if we need additional event data and normal TRBs,
3825 * since data in control transfers will never get bigger than 16MB
3826 * XXX: can we get a buffer that crosses 64KB boundaries?
3827 */
3828 if (urb->transfer_buffer_length > 0)
3829 num_trbs++;
3830 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3831 ep_index, urb->stream_id,
3832 num_trbs, urb, 0, mem_flags);
3833 if (ret < 0)
3834 return ret;
3835
3836 urb_priv = urb->hcpriv;
3837 td = &urb_priv->td[0];
3838
3839 /*
3840 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3841 * until we've finished creating all the other TRBs. The ring's cycle
3842 * state may change as we enqueue the other TRBs, so save it too.
3843 */
3844 start_trb = &ep_ring->enqueue->generic;
3845 start_cycle = ep_ring->cycle_state;
3846
3847 /* Queue setup TRB - see section 6.4.1.2.1 */
3848 /* FIXME better way to translate setup_packet into two u32 fields? */
3849 setup = (struct usb_ctrlrequest *) urb->setup_packet;
3850 field = 0;
3851 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3852 if (start_cycle == 0)
3853 field |= 0x1;
3854
3855 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */
3856 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) {
3857 if (urb->transfer_buffer_length > 0) {
3858 if (setup->bRequestType & USB_DIR_IN)
3859 field |= TRB_TX_TYPE(TRB_DATA_IN);
3860 else
3861 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3862 }
3863 }
3864
3865 queue_trb(xhci, ep_ring, true,
3866 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3867 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3868 TRB_LEN(8) | TRB_INTR_TARGET(0),
3869 /* Immediate data in pointer */
3870 field);
3871
3872 /* If there's data, queue data TRBs */
3873 /* Only set interrupt on short packet for IN endpoints */
3874 if (usb_urb_dir_in(urb))
3875 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3876 else
3877 field = TRB_TYPE(TRB_DATA);
3878
3879 if (urb->transfer_buffer_length > 0) {
3880 u32 length_field, remainder;
3881 u64 addr;
3882
3883 if (xhci_urb_suitable_for_idt(urb)) {
3884 memcpy(&addr, urb->transfer_buffer,
3885 urb->transfer_buffer_length);
3886 le64_to_cpus(&addr);
3887 field |= TRB_IDT;
3888 } else {
3889 addr = (u64) urb->transfer_dma;
3890 }
3891
3892 remainder = xhci_td_remainder(xhci, 0,
3893 urb->transfer_buffer_length,
3894 urb->transfer_buffer_length,
3895 urb, 1);
3896 length_field = TRB_LEN(urb->transfer_buffer_length) |
3897 TRB_TD_SIZE(remainder) |
3898 TRB_INTR_TARGET(0);
3899 if (setup->bRequestType & USB_DIR_IN)
3900 field |= TRB_DIR_IN;
3901 queue_trb(xhci, ep_ring, true,
3902 lower_32_bits(addr),
3903 upper_32_bits(addr),
3904 length_field,
3905 field | ep_ring->cycle_state);
3906 }
3907
3908 /* Save the DMA address of the last TRB in the TD */
3909 td->end_trb = ep_ring->enqueue;
3910 td->end_seg = ep_ring->enq_seg;
3911
3912 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3913 /* If the device sent data, the status stage is an OUT transfer */
3914 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3915 field = 0;
3916 else
3917 field = TRB_DIR_IN;
3918 queue_trb(xhci, ep_ring, false,
3919 0,
3920 0,
3921 TRB_INTR_TARGET(0),
3922 /* Event on completion */
3923 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3924
3925 giveback_first_trb(xhci, slot_id, ep_index, 0,
3926 start_cycle, start_trb);
3927 return 0;
3928 }
3929
3930 /*
3931 * The transfer burst count field of the isochronous TRB defines the number of
3932 * bursts that are required to move all packets in this TD. Only SuperSpeed
3933 * devices can burst up to bMaxBurst number of packets per service interval.
3934 * This field is zero based, meaning a value of zero in the field means one
3935 * burst. Basically, for everything but SuperSpeed devices, this field will be
3936 * zero. Only xHCI 1.0 host controllers support this field.
3937 */
xhci_get_burst_count(struct xhci_hcd * xhci,struct urb * urb,unsigned int total_packet_count)3938 static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
3939 struct urb *urb, unsigned int total_packet_count)
3940 {
3941 unsigned int max_burst;
3942
3943 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER)
3944 return 0;
3945
3946 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3947 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
3948 }
3949
3950 /*
3951 * Returns the number of packets in the last "burst" of packets. This field is
3952 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3953 * the last burst packet count is equal to the total number of packets in the
3954 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3955 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3956 * contain 1 to (bMaxBurst + 1) packets.
3957 */
xhci_get_last_burst_packet_count(struct xhci_hcd * xhci,struct urb * urb,unsigned int total_packet_count)3958 static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
3959 struct urb *urb, unsigned int total_packet_count)
3960 {
3961 unsigned int max_burst;
3962 unsigned int residue;
3963
3964 if (xhci->hci_version < 0x100)
3965 return 0;
3966
3967 if (urb->dev->speed >= USB_SPEED_SUPER) {
3968 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3969 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3970 residue = total_packet_count % (max_burst + 1);
3971 /* If residue is zero, the last burst contains (max_burst + 1)
3972 * number of packets, but the TLBPC field is zero-based.
3973 */
3974 if (residue == 0)
3975 return max_burst;
3976 return residue - 1;
3977 }
3978 if (total_packet_count == 0)
3979 return 0;
3980 return total_packet_count - 1;
3981 }
3982
3983 /*
3984 * Calculates Frame ID field of the isochronous TRB identifies the
3985 * target frame that the Interval associated with this Isochronous
3986 * Transfer Descriptor will start on. Refer to 4.11.2.5 in 1.1 spec.
3987 *
3988 * Returns actual frame id on success, negative value on error.
3989 */
xhci_get_isoc_frame_id(struct xhci_hcd * xhci,struct urb * urb,int index)3990 static int xhci_get_isoc_frame_id(struct xhci_hcd *xhci,
3991 struct urb *urb, int index)
3992 {
3993 int start_frame, ist, ret = 0;
3994 int start_frame_id, end_frame_id, current_frame_id;
3995
3996 if (urb->dev->speed == USB_SPEED_LOW ||
3997 urb->dev->speed == USB_SPEED_FULL)
3998 start_frame = urb->start_frame + index * urb->interval;
3999 else
4000 start_frame = (urb->start_frame + index * urb->interval) >> 3;
4001
4002 /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2):
4003 *
4004 * If bit [3] of IST is cleared to '0', software can add a TRB no
4005 * later than IST[2:0] Microframes before that TRB is scheduled to
4006 * be executed.
4007 * If bit [3] of IST is set to '1', software can add a TRB no later
4008 * than IST[2:0] Frames before that TRB is scheduled to be executed.
4009 */
4010 ist = HCS_IST(xhci->hcs_params2) & 0x7;
4011 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
4012 ist <<= 3;
4013
4014 /* Software shall not schedule an Isoch TD with a Frame ID value that
4015 * is less than the Start Frame ID or greater than the End Frame ID,
4016 * where:
4017 *
4018 * End Frame ID = (Current MFINDEX register value + 895 ms.) MOD 2048
4019 * Start Frame ID = (Current MFINDEX register value + IST + 1) MOD 2048
4020 *
4021 * Both the End Frame ID and Start Frame ID values are calculated
4022 * in microframes. When software determines the valid Frame ID value;
4023 * The End Frame ID value should be rounded down to the nearest Frame
4024 * boundary, and the Start Frame ID value should be rounded up to the
4025 * nearest Frame boundary.
4026 */
4027 current_frame_id = readl(&xhci->run_regs->microframe_index);
4028 start_frame_id = roundup(current_frame_id + ist + 1, 8);
4029 end_frame_id = rounddown(current_frame_id + 895 * 8, 8);
4030
4031 start_frame &= 0x7ff;
4032 start_frame_id = (start_frame_id >> 3) & 0x7ff;
4033 end_frame_id = (end_frame_id >> 3) & 0x7ff;
4034
4035 if (start_frame_id < end_frame_id) {
4036 if (start_frame > end_frame_id ||
4037 start_frame < start_frame_id)
4038 ret = -EINVAL;
4039 } else if (start_frame_id > end_frame_id) {
4040 if ((start_frame > end_frame_id &&
4041 start_frame < start_frame_id))
4042 ret = -EINVAL;
4043 } else {
4044 ret = -EINVAL;
4045 }
4046
4047 if (index == 0) {
4048 if (ret == -EINVAL || start_frame == start_frame_id) {
4049 start_frame = start_frame_id + 1;
4050 if (urb->dev->speed == USB_SPEED_LOW ||
4051 urb->dev->speed == USB_SPEED_FULL)
4052 urb->start_frame = start_frame;
4053 else
4054 urb->start_frame = start_frame << 3;
4055 ret = 0;
4056 }
4057 }
4058
4059 if (ret) {
4060 xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n",
4061 start_frame, current_frame_id, index,
4062 start_frame_id, end_frame_id);
4063 xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n");
4064 return ret;
4065 }
4066
4067 return start_frame;
4068 }
4069
4070 /* Check if we should generate event interrupt for a TD in an isoc URB */
trb_block_event_intr(struct xhci_hcd * xhci,int num_tds,int i,struct xhci_interrupter * ir)4071 static bool trb_block_event_intr(struct xhci_hcd *xhci, int num_tds, int i,
4072 struct xhci_interrupter *ir)
4073 {
4074 if (xhci->hci_version < 0x100)
4075 return false;
4076 /* always generate an event interrupt for the last TD */
4077 if (i == num_tds - 1)
4078 return false;
4079 /*
4080 * If AVOID_BEI is set the host handles full event rings poorly,
4081 * generate an event at least every 8th TD to clear the event ring
4082 */
4083 if (i && ir->isoc_bei_interval && xhci->quirks & XHCI_AVOID_BEI)
4084 return !!(i % ir->isoc_bei_interval);
4085
4086 return true;
4087 }
4088
4089 /* This is for isoc transfer */
xhci_queue_isoc_tx(struct xhci_hcd * xhci,gfp_t mem_flags,struct urb * urb,int slot_id,unsigned int ep_index)4090 static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
4091 struct urb *urb, int slot_id, unsigned int ep_index)
4092 {
4093 struct xhci_interrupter *ir;
4094 struct xhci_ring *ep_ring;
4095 struct urb_priv *urb_priv;
4096 struct xhci_td *td;
4097 int num_tds, trbs_per_td;
4098 struct xhci_generic_trb *start_trb;
4099 bool first_trb;
4100 int start_cycle;
4101 u32 field, length_field;
4102 int running_total, trb_buff_len, td_len, td_remain_len, ret;
4103 u64 start_addr, addr;
4104 int i, j;
4105 bool more_trbs_coming;
4106 struct xhci_virt_ep *xep;
4107 int frame_id;
4108
4109 xep = &xhci->devs[slot_id]->eps[ep_index];
4110 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
4111 ir = xhci->interrupters[0];
4112
4113 num_tds = urb->number_of_packets;
4114 if (num_tds < 1) {
4115 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
4116 return -EINVAL;
4117 }
4118 start_addr = (u64) urb->transfer_dma;
4119 start_trb = &ep_ring->enqueue->generic;
4120 start_cycle = ep_ring->cycle_state;
4121
4122 urb_priv = urb->hcpriv;
4123 /* Queue the TRBs for each TD, even if they are zero-length */
4124 for (i = 0; i < num_tds; i++) {
4125 unsigned int total_pkt_count, max_pkt;
4126 unsigned int burst_count, last_burst_pkt_count;
4127 u32 sia_frame_id;
4128
4129 first_trb = true;
4130 running_total = 0;
4131 addr = start_addr + urb->iso_frame_desc[i].offset;
4132 td_len = urb->iso_frame_desc[i].length;
4133 td_remain_len = td_len;
4134 max_pkt = usb_endpoint_maxp(&urb->ep->desc);
4135 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt);
4136
4137 /* A zero-length transfer still involves at least one packet. */
4138 if (total_pkt_count == 0)
4139 total_pkt_count++;
4140 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count);
4141 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci,
4142 urb, total_pkt_count);
4143
4144 trbs_per_td = count_isoc_trbs_needed(urb, i);
4145
4146 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
4147 urb->stream_id, trbs_per_td, urb, i, mem_flags);
4148 if (ret < 0) {
4149 if (i == 0)
4150 return ret;
4151 goto cleanup;
4152 }
4153 td = &urb_priv->td[i];
4154 /* use SIA as default, if frame id is used overwrite it */
4155 sia_frame_id = TRB_SIA;
4156 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
4157 HCC_CFC(xhci->hcc_params)) {
4158 frame_id = xhci_get_isoc_frame_id(xhci, urb, i);
4159 if (frame_id >= 0)
4160 sia_frame_id = TRB_FRAME_ID(frame_id);
4161 }
4162 /*
4163 * Set isoc specific data for the first TRB in a TD.
4164 * Prevent HW from getting the TRBs by keeping the cycle state
4165 * inverted in the first TDs isoc TRB.
4166 */
4167 field = TRB_TYPE(TRB_ISOC) |
4168 TRB_TLBPC(last_burst_pkt_count) |
4169 sia_frame_id |
4170 (i ? ep_ring->cycle_state : !start_cycle);
4171
4172 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */
4173 if (!xep->use_extended_tbc)
4174 field |= TRB_TBC(burst_count);
4175
4176 /* fill the rest of the TRB fields, and remaining normal TRBs */
4177 for (j = 0; j < trbs_per_td; j++) {
4178 u32 remainder = 0;
4179
4180 /* only first TRB is isoc, overwrite otherwise */
4181 if (!first_trb)
4182 field = TRB_TYPE(TRB_NORMAL) |
4183 ep_ring->cycle_state;
4184
4185 /* Only set interrupt on short packet for IN EPs */
4186 if (usb_urb_dir_in(urb))
4187 field |= TRB_ISP;
4188
4189 /* Set the chain bit for all except the last TRB */
4190 if (j < trbs_per_td - 1) {
4191 more_trbs_coming = true;
4192 field |= TRB_CHAIN;
4193 } else {
4194 more_trbs_coming = false;
4195 td->end_trb = ep_ring->enqueue;
4196 td->end_seg = ep_ring->enq_seg;
4197 field |= TRB_IOC;
4198 if (trb_block_event_intr(xhci, num_tds, i, ir))
4199 field |= TRB_BEI;
4200 }
4201 /* Calculate TRB length */
4202 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
4203 if (trb_buff_len > td_remain_len)
4204 trb_buff_len = td_remain_len;
4205
4206 /* Set the TRB length, TD size, & interrupter fields. */
4207 remainder = xhci_td_remainder(xhci, running_total,
4208 trb_buff_len, td_len,
4209 urb, more_trbs_coming);
4210
4211 length_field = TRB_LEN(trb_buff_len) |
4212 TRB_INTR_TARGET(0);
4213
4214 /* xhci 1.1 with ETE uses TD Size field for TBC */
4215 if (first_trb && xep->use_extended_tbc)
4216 length_field |= TRB_TD_SIZE_TBC(burst_count);
4217 else
4218 length_field |= TRB_TD_SIZE(remainder);
4219 first_trb = false;
4220
4221 queue_trb(xhci, ep_ring, more_trbs_coming,
4222 lower_32_bits(addr),
4223 upper_32_bits(addr),
4224 length_field,
4225 field);
4226 running_total += trb_buff_len;
4227
4228 addr += trb_buff_len;
4229 td_remain_len -= trb_buff_len;
4230 }
4231
4232 /* Check TD length */
4233 if (running_total != td_len) {
4234 xhci_err(xhci, "ISOC TD length unmatch\n");
4235 ret = -EINVAL;
4236 goto cleanup;
4237 }
4238 }
4239
4240 /* store the next frame id */
4241 if (HCC_CFC(xhci->hcc_params))
4242 xep->next_frame_id = urb->start_frame + num_tds * urb->interval;
4243
4244 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
4245 if (xhci->quirks & XHCI_AMD_PLL_FIX)
4246 usb_amd_quirk_pll_disable();
4247 }
4248 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
4249
4250 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
4251 start_cycle, start_trb);
4252 return 0;
4253 cleanup:
4254 /* Clean up a partially enqueued isoc transfer. */
4255
4256 for (i--; i >= 0; i--)
4257 list_del_init(&urb_priv->td[i].td_list);
4258
4259 /* Use the first TD as a temporary variable to turn the TDs we've queued
4260 * into No-ops with a software-owned cycle bit. That way the hardware
4261 * won't accidentally start executing bogus TDs when we partially
4262 * overwrite them. td->start_trb and td->start_seg are already set.
4263 */
4264 urb_priv->td[0].end_trb = ep_ring->enqueue;
4265 /* Every TRB except the first & last will have its cycle bit flipped. */
4266 td_to_noop(&urb_priv->td[0], true);
4267
4268 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
4269 ep_ring->enqueue = urb_priv->td[0].start_trb;
4270 ep_ring->enq_seg = urb_priv->td[0].start_seg;
4271 ep_ring->cycle_state = start_cycle;
4272 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
4273 return ret;
4274 }
4275
4276 /*
4277 * Check transfer ring to guarantee there is enough room for the urb.
4278 * Update ISO URB start_frame and interval.
4279 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to
4280 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or
4281 * Contiguous Frame ID is not supported by HC.
4282 */
xhci_queue_isoc_tx_prepare(struct xhci_hcd * xhci,gfp_t mem_flags,struct urb * urb,int slot_id,unsigned int ep_index)4283 int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
4284 struct urb *urb, int slot_id, unsigned int ep_index)
4285 {
4286 struct xhci_virt_device *xdev;
4287 struct xhci_ring *ep_ring;
4288 struct xhci_ep_ctx *ep_ctx;
4289 int start_frame;
4290 int num_tds, num_trbs, i;
4291 int ret;
4292 struct xhci_virt_ep *xep;
4293 int ist;
4294
4295 xdev = xhci->devs[slot_id];
4296 xep = &xhci->devs[slot_id]->eps[ep_index];
4297 ep_ring = xdev->eps[ep_index].ring;
4298 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
4299
4300 num_trbs = 0;
4301 num_tds = urb->number_of_packets;
4302 for (i = 0; i < num_tds; i++)
4303 num_trbs += count_isoc_trbs_needed(urb, i);
4304
4305 /* Check the ring to guarantee there is enough room for the whole urb.
4306 * Do not insert any td of the urb to the ring if the check failed.
4307 */
4308 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx),
4309 num_trbs, mem_flags);
4310 if (ret)
4311 return ret;
4312
4313 /*
4314 * Check interval value. This should be done before we start to
4315 * calculate the start frame value.
4316 */
4317 check_interval(urb, ep_ctx);
4318
4319 /* Calculate the start frame and put it in urb->start_frame. */
4320 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) {
4321 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) {
4322 urb->start_frame = xep->next_frame_id;
4323 goto skip_start_over;
4324 }
4325 }
4326
4327 start_frame = readl(&xhci->run_regs->microframe_index);
4328 start_frame &= 0x3fff;
4329 /*
4330 * Round up to the next frame and consider the time before trb really
4331 * gets scheduled by hardare.
4332 */
4333 ist = HCS_IST(xhci->hcs_params2) & 0x7;
4334 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
4335 ist <<= 3;
4336 start_frame += ist + XHCI_CFC_DELAY;
4337 start_frame = roundup(start_frame, 8);
4338
4339 /*
4340 * Round up to the next ESIT (Endpoint Service Interval Time) if ESIT
4341 * is greate than 8 microframes.
4342 */
4343 if (urb->dev->speed == USB_SPEED_LOW ||
4344 urb->dev->speed == USB_SPEED_FULL) {
4345 start_frame = roundup(start_frame, urb->interval << 3);
4346 urb->start_frame = start_frame >> 3;
4347 } else {
4348 start_frame = roundup(start_frame, urb->interval);
4349 urb->start_frame = start_frame;
4350 }
4351
4352 skip_start_over:
4353
4354 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
4355 }
4356
4357 /**** Command Ring Operations ****/
4358
4359 /* Generic function for queueing a command TRB on the command ring.
4360 * Check to make sure there's room on the command ring for one command TRB.
4361 * Also check that there's room reserved for commands that must not fail.
4362 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
4363 * then only check for the number of reserved spots.
4364 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
4365 * because the command event handler may want to resubmit a failed command.
4366 */
queue_command(struct xhci_hcd * xhci,struct xhci_command * cmd,u32 field1,u32 field2,u32 field3,u32 field4,bool command_must_succeed)4367 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
4368 u32 field1, u32 field2,
4369 u32 field3, u32 field4, bool command_must_succeed)
4370 {
4371 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
4372 int ret;
4373
4374 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
4375 (xhci->xhc_state & XHCI_STATE_HALTED)) {
4376 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n");
4377 return -ESHUTDOWN;
4378 }
4379
4380 if (!command_must_succeed)
4381 reserved_trbs++;
4382
4383 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
4384 reserved_trbs, GFP_ATOMIC);
4385 if (ret < 0) {
4386 xhci_err(xhci, "ERR: No room for command on command ring\n");
4387 if (command_must_succeed)
4388 xhci_err(xhci, "ERR: Reserved TRB counting for "
4389 "unfailable commands failed.\n");
4390 return ret;
4391 }
4392
4393 cmd->command_trb = xhci->cmd_ring->enqueue;
4394
4395 /* if there are no other commands queued we start the timeout timer */
4396 if (list_empty(&xhci->cmd_list)) {
4397 xhci->current_cmd = cmd;
4398 xhci_mod_cmd_timer(xhci);
4399 }
4400
4401 list_add_tail(&cmd->cmd_list, &xhci->cmd_list);
4402
4403 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
4404 field4 | xhci->cmd_ring->cycle_state);
4405 return 0;
4406 }
4407
4408 /* Queue a slot enable or disable request on the command ring */
xhci_queue_slot_control(struct xhci_hcd * xhci,struct xhci_command * cmd,u32 trb_type,u32 slot_id)4409 int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
4410 u32 trb_type, u32 slot_id)
4411 {
4412 return queue_command(xhci, cmd, 0, 0, 0,
4413 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
4414 }
4415
4416 /* Queue an address device command TRB */
xhci_queue_address_device(struct xhci_hcd * xhci,struct xhci_command * cmd,dma_addr_t in_ctx_ptr,u32 slot_id,enum xhci_setup_dev setup)4417 int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
4418 dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup)
4419 {
4420 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
4421 upper_32_bits(in_ctx_ptr), 0,
4422 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id)
4423 | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false);
4424 }
4425
xhci_queue_vendor_command(struct xhci_hcd * xhci,struct xhci_command * cmd,u32 field1,u32 field2,u32 field3,u32 field4)4426 int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
4427 u32 field1, u32 field2, u32 field3, u32 field4)
4428 {
4429 return queue_command(xhci, cmd, field1, field2, field3, field4, false);
4430 }
4431
4432 /* Queue a reset device command TRB */
xhci_queue_reset_device(struct xhci_hcd * xhci,struct xhci_command * cmd,u32 slot_id)4433 int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
4434 u32 slot_id)
4435 {
4436 return queue_command(xhci, cmd, 0, 0, 0,
4437 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
4438 false);
4439 }
4440
4441 /* Queue a configure endpoint command TRB */
xhci_queue_configure_endpoint(struct xhci_hcd * xhci,struct xhci_command * cmd,dma_addr_t in_ctx_ptr,u32 slot_id,bool command_must_succeed)4442 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
4443 struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
4444 u32 slot_id, bool command_must_succeed)
4445 {
4446 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
4447 upper_32_bits(in_ctx_ptr), 0,
4448 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
4449 command_must_succeed);
4450 }
4451
4452 /* Queue a get root hub port bandwidth command TRB */
xhci_queue_get_port_bw(struct xhci_hcd * xhci,struct xhci_command * cmd,dma_addr_t in_ctx_ptr,u8 dev_speed,bool command_must_succeed)4453 int xhci_queue_get_port_bw(struct xhci_hcd *xhci,
4454 struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
4455 u8 dev_speed, bool command_must_succeed)
4456 {
4457 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
4458 upper_32_bits(in_ctx_ptr), 0,
4459 TRB_TYPE(TRB_GET_BW) | DEV_SPEED_FOR_TRB(dev_speed),
4460 command_must_succeed);
4461 }
4462
4463 /* Queue an evaluate context command TRB */
xhci_queue_evaluate_context(struct xhci_hcd * xhci,struct xhci_command * cmd,dma_addr_t in_ctx_ptr,u32 slot_id,bool command_must_succeed)4464 int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
4465 dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed)
4466 {
4467 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
4468 upper_32_bits(in_ctx_ptr), 0,
4469 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
4470 command_must_succeed);
4471 }
4472
4473 /*
4474 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
4475 * activity on an endpoint that is about to be suspended.
4476 */
xhci_queue_stop_endpoint(struct xhci_hcd * xhci,struct xhci_command * cmd,int slot_id,unsigned int ep_index,int suspend)4477 int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
4478 int slot_id, unsigned int ep_index, int suspend)
4479 {
4480 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4481 u32 trb_ep_index = EP_INDEX_FOR_TRB(ep_index);
4482 u32 type = TRB_TYPE(TRB_STOP_RING);
4483 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
4484
4485 return queue_command(xhci, cmd, 0, 0, 0,
4486 trb_slot_id | trb_ep_index | type | trb_suspend, false);
4487 }
4488
xhci_queue_reset_ep(struct xhci_hcd * xhci,struct xhci_command * cmd,int slot_id,unsigned int ep_index,enum xhci_ep_reset_type reset_type)4489 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
4490 int slot_id, unsigned int ep_index,
4491 enum xhci_ep_reset_type reset_type)
4492 {
4493 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4494 u32 trb_ep_index = EP_INDEX_FOR_TRB(ep_index);
4495 u32 type = TRB_TYPE(TRB_RESET_EP);
4496
4497 if (reset_type == EP_SOFT_RESET)
4498 type |= TRB_TSP;
4499
4500 return queue_command(xhci, cmd, 0, 0, 0,
4501 trb_slot_id | trb_ep_index | type, false);
4502 }
4503