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