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