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