blob: 3ba91580e222e29ad28fa09ca02dbd0fc27a3f6f [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002/*
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.
Sarah Sharp7f84eef2009-04-27 19:53:56 -07009 */
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
Sarah Sharp8a96c052009-04-27 19:59:19 -070055#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Mathias Nymanf9c589e2016-06-21 10:58:02 +030057#include <linux/dma-mapping.h>
Sarah Sharp7f84eef2009-04-27 19:53:56 -070058#include "xhci.h"
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +030059#include "xhci-trace.h"
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020060#include "xhci-mtk.h"
Sarah Sharp7f84eef2009-04-27 19:53:56 -070061
62/*
63 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
64 * address of the TRB.
65 */
Sarah Sharp23e3be12009-04-29 19:05:20 -070066dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -070067 union xhci_trb *trb)
68{
Sarah Sharp6071d832009-05-14 11:44:14 -070069 unsigned long segment_offset;
Sarah Sharp7f84eef2009-04-27 19:53:56 -070070
Sarah Sharp6071d832009-05-14 11:44:14 -070071 if (!seg || !trb || trb < seg->trbs)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070072 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070073 /* offset in TRBs */
74 segment_offset = trb - seg->trbs;
Mathias Nyman78950862015-08-03 16:07:48 +030075 if (segment_offset >= TRBS_PER_SEGMENT)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070076 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070077 return seg->dma + (segment_offset * sizeof(*trb));
Sarah Sharp7f84eef2009-04-27 19:53:56 -070078}
79
Mathias Nyman0ce57492016-11-11 15:13:14 +020080static bool trb_is_noop(union xhci_trb *trb)
81{
82 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]);
83}
84
Mathias Nyman2d98ef42016-06-21 10:58:04 +030085static bool trb_is_link(union xhci_trb *trb)
86{
87 return TRB_TYPE_LINK_LE32(trb->link.control);
88}
89
Mathias Nymanbd5e67f2016-06-21 10:58:05 +030090static 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
95static 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
Mathias Nymand0c77d82016-06-21 10:58:07 +0300101static bool link_trb_toggles_cycle(union xhci_trb *trb)
102{
103 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
104}
105
Mathias Nyman2a721262016-11-11 15:13:24 +0200106static bool last_td_in_urb(struct xhci_td *td)
107{
108 struct urb_priv *urb_priv = td->urb->hcpriv;
109
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +0200110 return urb_priv->num_tds_done == urb_priv->num_tds;
Mathias Nyman2a721262016-11-11 15:13:24 +0200111}
112
113static void inc_td_cnt(struct urb *urb)
114{
115 struct urb_priv *urb_priv = urb->hcpriv;
116
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +0200117 urb_priv->num_tds_done++;
Mathias Nyman2a721262016-11-11 15:13:24 +0200118}
119
Mathias Nymanae1e3f02017-01-23 14:20:15 +0200120static 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
Sarah Sharpae636742009-04-29 19:02:31 -0700135/* 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 */
139static void next_trb(struct xhci_hcd *xhci,
140 struct xhci_ring *ring,
141 struct xhci_segment **seg,
142 union xhci_trb **trb)
143{
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300144 if (trb_is_link(*trb)) {
Sarah Sharpae636742009-04-29 19:02:31 -0700145 *seg = (*seg)->next;
146 *trb = ((*seg)->trbs);
147 } else {
John Youna1669b22010-08-09 13:56:11 -0700148 (*trb)++;
Sarah Sharpae636742009-04-29 19:02:31 -0700149 }
150}
151
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700152/*
153 * See Cycle bit rules. SW is the consumer for the event ring only.
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700154 */
Lu Baolu67d2ea92017-12-08 17:59:09 +0200155void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700156{
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200157 unsigned int link_trb_count = 0;
158
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300159 /* event ring doesn't have link trbs, check for last trb */
160 if (ring->type == TYPE_EVENT) {
161 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
Sarah Sharp50d02062012-07-26 12:03:59 -0700162 ring->dequeue++;
Adam Wallis49d5b052017-10-05 11:21:47 +0300163 goto out;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700164 }
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300165 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue))
166 ring->cycle_state ^= 1;
167 ring->deq_seg = ring->deq_seg->next;
168 ring->dequeue = ring->deq_seg->trbs;
Adam Wallis49d5b052017-10-05 11:21:47 +0300169 goto out;
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300170 }
171
172 /* All other rings have link trbs */
173 if (!trb_is_link(ring->dequeue)) {
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200174 if (last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
175 xhci_warn(xhci, "Missing link TRB at end of segment\n");
176 } else {
177 ring->dequeue++;
178 ring->num_trbs_free++;
179 }
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300180 }
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200181
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300182 while (trb_is_link(ring->dequeue)) {
183 ring->deq_seg = ring->deq_seg->next;
184 ring->dequeue = ring->deq_seg->trbs;
Lu Baolub2d6edb2017-04-07 17:57:02 +0300185
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200186 if (link_trb_count++ > ring->num_segs) {
187 xhci_warn(xhci, "Ring is an endless link TRB loop\n");
188 break;
189 }
190 }
Adam Wallis49d5b052017-10-05 11:21:47 +0300191out:
Lu Baolub2d6edb2017-04-07 17:57:02 +0300192 trace_xhci_inc_deq(ring);
193
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300194 return;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700195}
196
197/*
198 * See Cycle bit rules. SW is the consumer for the event ring only.
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700199 *
200 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
201 * chain bit is set), then set the chain bit in all the following link TRBs.
202 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
203 * have their chain bit cleared (so that each Link TRB is a separate TD).
204 *
205 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
Sarah Sharpb0567b32009-08-07 14:04:36 -0700206 * set, but other sections talk about dealing with the chain bit set. This was
207 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
208 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700209 *
210 * @more_trbs_coming: Will you enqueue more TRBs before calling
211 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700212 */
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700213static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +0800214 bool more_trbs_coming)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700215{
216 u32 chain;
217 union xhci_trb *next;
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200218 unsigned int link_trb_count = 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700219
Matt Evans28ccd292011-03-29 13:40:46 +1100220 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
Andiry Xub008df62012-03-05 17:49:34 +0800221 /* If this is not event ring, there is one less usable TRB */
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300222 if (!trb_is_link(ring->enqueue))
Andiry Xub008df62012-03-05 17:49:34 +0800223 ring->num_trbs_free--;
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200224
225 if (last_trb_on_seg(ring->enq_seg, ring->enqueue)) {
226 xhci_err(xhci, "Tried to move enqueue past ring segment\n");
227 return;
228 }
229
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700230 next = ++(ring->enqueue);
231
Mathias Nyman22511982016-06-21 10:58:03 +0300232 /* Update the dequeue pointer further if that was a link TRB */
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300233 while (trb_is_link(next)) {
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700234
Mathias Nyman22511982016-06-21 10:58:03 +0300235 /*
236 * If the caller doesn't plan on enqueueing more TDs before
237 * ringing the doorbell, then we don't want to give the link TRB
238 * to the hardware just yet. We'll give the link TRB back in
239 * prepare_ring() just before we enqueue the TD at the top of
240 * the ring.
241 */
242 if (!chain && !more_trbs_coming)
243 break;
Andiry Xu3b72fca2012-03-05 17:49:32 +0800244
Mathias Nyman22511982016-06-21 10:58:03 +0300245 /* If we're not dealing with 0.95 hardware or isoc rings on
246 * AMD 0.96 host, carry over the chain bit of the previous TRB
247 * (which may mean the chain bit is cleared).
248 */
249 if (!(ring->type == TYPE_ISOC &&
250 (xhci->quirks & XHCI_AMD_0x96_HOST)) &&
251 !xhci_link_trb_quirk(xhci)) {
252 next->link.control &= cpu_to_le32(~TRB_CHAIN);
253 next->link.control |= cpu_to_le32(chain);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700254 }
Mathias Nyman22511982016-06-21 10:58:03 +0300255 /* Give this link TRB to the hardware */
256 wmb();
257 next->link.control ^= cpu_to_le32(TRB_CYCLE);
258
259 /* Toggle the cycle bit after the last ring segment. */
Mathias Nymand0c77d82016-06-21 10:58:07 +0300260 if (link_trb_toggles_cycle(next))
Mathias Nyman22511982016-06-21 10:58:03 +0300261 ring->cycle_state ^= 1;
262
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700263 ring->enq_seg = ring->enq_seg->next;
264 ring->enqueue = ring->enq_seg->trbs;
265 next = ring->enqueue;
Mathias Nymanc716e8a2021-01-29 15:00:30 +0200266
267 if (link_trb_count++ > ring->num_segs) {
268 xhci_warn(xhci, "%s: Ring link TRB loop\n", __func__);
269 break;
270 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700271 }
Lu Baolub2d6edb2017-04-07 17:57:02 +0300272
273 trace_xhci_inc_enq(ring);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700274}
275
276/*
Andiry Xu085deb12012-03-05 17:49:40 +0800277 * Check to see if there's room to enqueue num_trbs on the ring and make sure
278 * enqueue pointer will not advance into dequeue segment. See rules above.
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700279 */
Andiry Xub008df62012-03-05 17:49:34 +0800280static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700281 unsigned int num_trbs)
282{
Andiry Xu085deb12012-03-05 17:49:40 +0800283 int num_trbs_in_deq_seg;
Andiry Xub008df62012-03-05 17:49:34 +0800284
Andiry Xu085deb12012-03-05 17:49:40 +0800285 if (ring->num_trbs_free < num_trbs)
286 return 0;
287
288 if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
289 num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
290 if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
291 return 0;
292 }
293
294 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700295}
296
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700297/* Ring the host controller doorbell after placing a command on the ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700298void xhci_ring_cmd_db(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700299{
Elric Fuc181bc52012-06-27 16:30:57 +0800300 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING))
301 return;
302
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700303 xhci_dbg(xhci, "// Ding dong!\n");
Mathias Nyman58b9d712019-11-15 18:50:01 +0200304
305 trace_xhci_ring_host_doorbell(0, DB_VALUE_HOST);
306
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200307 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700308 /* Flush PCI posted writes */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200309 readl(&xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700310}
311
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +0200312static bool xhci_mod_cmd_timer(struct xhci_hcd *xhci, unsigned long delay)
313{
314 return mod_delayed_work(system_wq, &xhci->cmd_timer, delay);
315}
316
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200317static struct xhci_command *xhci_next_queued_cmd(struct xhci_hcd *xhci)
318{
319 return list_first_entry_or_null(&xhci->cmd_list, struct xhci_command,
320 cmd_list);
321}
322
323/*
324 * Turn all commands on command ring with status set to "aborted" to no-op trbs.
325 * If there are other commands waiting then restart the ring and kick the timer.
326 * This must be called with command ring stopped and xhci->lock held.
327 */
328static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci,
329 struct xhci_command *cur_cmd)
330{
331 struct xhci_command *i_cmd;
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200332
333 /* Turn all aborted commands in list to no-ops, then restart */
334 list_for_each_entry(i_cmd, &xhci->cmd_list, cmd_list) {
335
Felipe Balbi0b7c1052017-01-23 14:20:06 +0200336 if (i_cmd->status != COMP_COMMAND_ABORTED)
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200337 continue;
338
Mathias Nyman604d02a2017-05-17 18:32:05 +0300339 i_cmd->status = COMP_COMMAND_RING_STOPPED;
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200340
341 xhci_dbg(xhci, "Turn aborted command %p to no-op\n",
342 i_cmd->command_trb);
Mathias Nyman52782042017-01-23 14:20:16 +0200343
344 trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP);
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200345
346 /*
347 * caller waiting for completion is called when command
348 * completion event is received for these no-op commands
349 */
350 }
351
352 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
353
354 /* ring command ring doorbell to restart the command ring */
355 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) &&
356 !(xhci->xhc_state & XHCI_STATE_DYING)) {
357 xhci->current_cmd = cur_cmd;
358 xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT);
359 xhci_ring_cmd_db(xhci);
360 }
361}
362
363/* Must be called with xhci->lock held, releases and aquires lock back */
364static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags)
Elric Fub92cc662012-06-27 16:31:12 +0800365{
366 u64 temp_64;
367 int ret;
368
369 xhci_dbg(xhci, "Abort command ring\n");
370
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200371 reinit_completion(&xhci->cmd_ring_stop_completion);
Mathias Nyman3425aa02016-06-01 18:09:08 +0300372
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200373 temp_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Sarah Sharp477632d2014-01-29 14:02:00 -0800374 xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
375 &xhci->op_regs->cmd_ring);
Elric Fub92cc662012-06-27 16:31:12 +0800376
Mathias Nymand9f11ba2017-04-07 17:57:01 +0300377 /* Section 4.6.1.2 of xHCI 1.0 spec says software should also time the
378 * completion of the Command Abort operation. If CRR is not negated in 5
379 * seconds then driver handles it as if host died (-ENODEV).
380 * In the future we should distinguish between -ENODEV and -ETIMEDOUT
381 * and try to recover a -ETIMEDOUT with a host controller reset.
Elric Fub92cc662012-06-27 16:31:12 +0800382 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200383 ret = xhci_handshake(&xhci->op_regs->cmd_ring,
Elric Fub92cc662012-06-27 16:31:12 +0800384 CMD_RING_RUNNING, 0, 5 * 1000 * 1000);
385 if (ret < 0) {
Mathias Nymand9f11ba2017-04-07 17:57:01 +0300386 xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret);
Lu Baolu1cc6d862017-01-23 14:19:55 +0200387 xhci_halt(xhci);
Mathias Nymand9f11ba2017-04-07 17:57:01 +0300388 xhci_hc_died(xhci);
389 return ret;
Elric Fub92cc662012-06-27 16:31:12 +0800390 }
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +0200391 /*
392 * Writing the CMD_RING_ABORT bit should cause a cmd completion event,
393 * however on some host hw the CMD_RING_RUNNING bit is correctly cleared
394 * but the completion event in never sent. Wait 2 secs (arbitrary
395 * number) to handle those cases after negation of CMD_RING_RUNNING.
396 */
397 spin_unlock_irqrestore(&xhci->lock, flags);
398 ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion,
399 msecs_to_jiffies(2000));
400 spin_lock_irqsave(&xhci->lock, flags);
401 if (!ret) {
402 xhci_dbg(xhci, "No stop event for abort, ring start fail?\n");
403 xhci_cleanup_command_queue(xhci);
404 } else {
405 xhci_handle_stopped_cmd_ring(xhci, xhci_next_queued_cmd(xhci));
406 }
Elric Fub92cc662012-06-27 16:31:12 +0800407 return 0;
408}
409
Andiry Xube88fe42010-10-14 07:22:57 -0700410void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700411 unsigned int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700412 unsigned int ep_index,
413 unsigned int stream_id)
Sarah Sharpae636742009-04-29 19:02:31 -0700414{
Matt Evans28ccd292011-03-29 13:40:46 +1100415 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
Matthew Wilcox50d646762010-12-15 14:18:11 -0500416 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
417 unsigned int ep_state = ep->ep_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700418
Sarah Sharpae636742009-04-29 19:02:31 -0700419 /* Don't ring the doorbell for this endpoint if there are pending
Matthew Wilcox50d646762010-12-15 14:18:11 -0500420 * cancellations because we don't want to interrupt processing.
Sarah Sharp8df75f42010-04-02 15:34:16 -0700421 * We don't want to restart any stream rings if there's a set dequeue
422 * pointer command pending because the device can choose to start any
423 * stream once the endpoint is on the HW schedule.
Sarah Sharpae636742009-04-29 19:02:31 -0700424 */
Mathias Nyman9983a5f2017-01-23 14:19:52 +0200425 if ((ep_state & EP_STOP_CMD_PENDING) || (ep_state & SET_DEQ_PENDING) ||
Jim Linef513be2019-06-03 18:53:44 +0800426 (ep_state & EP_HALTED) || (ep_state & EP_CLEARING_TT))
Matthew Wilcox50d646762010-12-15 14:18:11 -0500427 return;
Mathias Nyman58b9d712019-11-15 18:50:01 +0200428
429 trace_xhci_ring_ep_doorbell(slot_id, DB_VALUE(ep_index, stream_id));
430
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200431 writel(DB_VALUE(ep_index, stream_id), db_addr);
Mathias Nymanb05dadb2021-01-29 15:00:31 +0200432 /* flush the write */
433 readl(db_addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700434}
435
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700436/* Ring the doorbell for any rings with pending URBs */
437static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
438 unsigned int slot_id,
439 unsigned int ep_index)
440{
441 unsigned int stream_id;
442 struct xhci_virt_ep *ep;
443
444 ep = &xhci->devs[slot_id]->eps[ep_index];
445
446 /* A ring has pending URBs if its TD list is not empty */
447 if (!(ep->ep_state & EP_HAS_STREAMS)) {
Oleksij Rempeld66eaf92013-07-21 15:36:19 +0200448 if (ep->ring && !(list_empty(&ep->ring->td_list)))
Andiry Xube88fe42010-10-14 07:22:57 -0700449 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700450 return;
451 }
452
453 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
454 stream_id++) {
455 struct xhci_stream_info *stream_info = ep->stream_info;
456 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
Andiry Xube88fe42010-10-14 07:22:57 -0700457 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
458 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700459 }
460}
461
Jim Linef513be2019-06-03 18:53:44 +0800462void xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
463 unsigned int slot_id,
464 unsigned int ep_index)
465{
466 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
467}
468
Mathias Nymanb1adc422021-01-29 15:00:22 +0200469static struct xhci_virt_ep *xhci_get_virt_ep(struct xhci_hcd *xhci,
470 unsigned int slot_id,
471 unsigned int ep_index)
472{
473 if (slot_id == 0 || slot_id >= MAX_HC_SLOTS) {
474 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id);
475 return NULL;
476 }
477 if (ep_index >= EP_CTX_PER_DEV) {
478 xhci_warn(xhci, "Invalid endpoint index %u\n", ep_index);
479 return NULL;
480 }
481 if (!xhci->devs[slot_id]) {
482 xhci_warn(xhci, "No xhci virt device for slot_id %u\n", slot_id);
483 return NULL;
484 }
485
486 return &xhci->devs[slot_id]->eps[ep_index];
487}
488
Mathias Nyman42f28902021-01-29 15:00:24 +0200489static struct xhci_ring *xhci_virt_ep_to_ring(struct xhci_hcd *xhci,
490 struct xhci_virt_ep *ep,
491 unsigned int stream_id)
492{
493 /* common case, no streams */
494 if (!(ep->ep_state & EP_HAS_STREAMS))
495 return ep->ring;
496
497 if (!ep->stream_info)
498 return NULL;
499
500 if (stream_id == 0 || stream_id >= ep->stream_info->num_streams) {
501 xhci_warn(xhci, "Invalid stream_id %u request for slot_id %u ep_index %u\n",
502 stream_id, ep->vdev->slot_id, ep->ep_index);
503 return NULL;
504 }
505
506 return ep->stream_info->stream_rings[stream_id];
507}
508
Alexandr Ivanov75b040e2016-04-22 13:17:10 +0300509/* Get the right ring for the given slot_id, ep_index and stream_id.
510 * If the endpoint supports streams, boundary check the URB's stream ID.
511 * If the endpoint doesn't support streams, return the singular endpoint ring.
512 */
513struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
Sarah Sharp021bff92010-07-29 22:12:20 -0700514 unsigned int slot_id, unsigned int ep_index,
515 unsigned int stream_id)
516{
517 struct xhci_virt_ep *ep;
518
Mathias Nymanb1adc422021-01-29 15:00:22 +0200519 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
520 if (!ep)
521 return NULL;
522
Mathias Nyman42f28902021-01-29 15:00:24 +0200523 return xhci_virt_ep_to_ring(xhci, ep, stream_id);
Sarah Sharp021bff92010-07-29 22:12:20 -0700524}
525
Mathias Nymane6b20122017-06-02 16:36:22 +0300526
527/*
528 * Get the hw dequeue pointer xHC stopped on, either directly from the
529 * endpoint context, or if streams are in use from the stream context.
530 * The returned hw_dequeue contains the lowest four bits with cycle state
531 * and possbile stream context type.
532 */
533static u64 xhci_get_hw_deq(struct xhci_hcd *xhci, struct xhci_virt_device *vdev,
534 unsigned int ep_index, unsigned int stream_id)
535{
536 struct xhci_ep_ctx *ep_ctx;
537 struct xhci_stream_ctx *st_ctx;
538 struct xhci_virt_ep *ep;
539
540 ep = &vdev->eps[ep_index];
541
542 if (ep->ep_state & EP_HAS_STREAMS) {
543 st_ctx = &ep->stream_info->stream_ctx_array[stream_id];
544 return le64_to_cpu(st_ctx->stream_ring);
545 }
546 ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index);
547 return le64_to_cpu(ep_ctx->deq);
548}
549
Sarah Sharpae636742009-04-29 19:02:31 -0700550/*
551 * Move the xHC's endpoint ring dequeue pointer past cur_td.
552 * Record the new state of the xHC's endpoint ring dequeue segment,
Mathias Nyman87907362017-06-02 16:36:23 +0300553 * dequeue pointer, stream id, and new consumer cycle state in state.
Sarah Sharpae636742009-04-29 19:02:31 -0700554 * Update our internal representation of the ring's dequeue pointer.
555 *
556 * We do this in three jumps:
557 * - First we update our new ring state to be the same as when the xHC stopped.
558 * - Then we traverse the ring to find the segment that contains
559 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
560 * any link TRBs with the toggle cycle bit set.
561 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
562 * if we've moved it past a link TRB with the toggle cycle bit set.
Matt Evans28ccd292011-03-29 13:40:46 +1100563 *
564 * Some of the uses of xhci_generic_trb are grotty, but if they're done
565 * with correct __le32 accesses they should work fine. Only users of this are
566 * in here.
Sarah Sharpae636742009-04-29 19:02:31 -0700567 */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700568void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700569 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700570 unsigned int stream_id, struct xhci_td *cur_td,
571 struct xhci_dequeue_state *state)
Sarah Sharpae636742009-04-29 19:02:31 -0700572{
573 struct xhci_virt_device *dev = xhci->devs[slot_id];
Hans de Goedec4bedb72013-10-04 00:29:47 +0200574 struct xhci_virt_ep *ep = &dev->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700575 struct xhci_ring *ep_ring;
Mathias Nyman365038d2014-08-19 15:17:58 +0300576 struct xhci_segment *new_seg;
577 union xhci_trb *new_deq;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700578 dma_addr_t addr;
Julius Werner1f81b6d2014-04-25 19:20:13 +0300579 u64 hw_dequeue;
Mathias Nyman365038d2014-08-19 15:17:58 +0300580 bool cycle_found = false;
581 bool td_last_trb_found = false;
Sarah Sharpae636742009-04-29 19:02:31 -0700582
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700583 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
584 ep_index, stream_id);
585 if (!ep_ring) {
586 xhci_warn(xhci, "WARN can't find new dequeue state "
587 "for invalid stream ID %u.\n",
588 stream_id);
589 return;
590 }
Mathias Nyman93ceaa82020-04-21 17:08:20 +0300591 /*
592 * A cancelled TD can complete with a stall if HW cached the trb.
593 * In this case driver can't find cur_td, but if the ring is empty we
594 * can move the dequeue pointer to the current enqueue position.
595 */
596 if (!cur_td) {
597 if (list_empty(&ep_ring->td_list)) {
598 state->new_deq_seg = ep_ring->enq_seg;
599 state->new_deq_ptr = ep_ring->enqueue;
600 state->new_cycle_state = ep_ring->cycle_state;
601 goto done;
602 } else {
603 xhci_warn(xhci, "Can't find new dequeue state, missing cur_td\n");
604 return;
605 }
606 }
607
Sarah Sharpae636742009-04-29 19:02:31 -0700608 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300609 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
610 "Finding endpoint context");
Sarah Sharpae636742009-04-29 19:02:31 -0700611
Mathias Nymane6b20122017-06-02 16:36:22 +0300612 hw_dequeue = xhci_get_hw_deq(xhci, dev, ep_index, stream_id);
Mathias Nyman365038d2014-08-19 15:17:58 +0300613 new_seg = ep_ring->deq_seg;
614 new_deq = ep_ring->dequeue;
615 state->new_cycle_state = hw_dequeue & 0x1;
Mathias Nyman87907362017-06-02 16:36:23 +0300616 state->stream_id = stream_id;
Mathias Nyman365038d2014-08-19 15:17:58 +0300617
618 /*
619 * We want to find the pointer, segment and cycle state of the new trb
620 * (the one after current TD's last_trb). We know the cycle state at
621 * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are
622 * found.
623 */
624 do {
625 if (!cycle_found && xhci_trb_virt_to_dma(new_seg, new_deq)
626 == (dma_addr_t)(hw_dequeue & ~0xf)) {
627 cycle_found = true;
628 if (td_last_trb_found)
629 break;
630 }
631 if (new_deq == cur_td->last_trb)
632 td_last_trb_found = true;
633
Mathias Nyman3495e452016-11-11 15:13:13 +0200634 if (cycle_found && trb_is_link(new_deq) &&
635 link_trb_toggles_cycle(new_deq))
Mathias Nyman365038d2014-08-19 15:17:58 +0300636 state->new_cycle_state ^= 0x1;
637
638 next_trb(xhci, ep_ring, &new_seg, &new_deq);
639
640 /* Search wrapped around, bail out */
641 if (new_deq == ep->ring->dequeue) {
642 xhci_err(xhci, "Error: Failed finding new dequeue state\n");
643 state->new_deq_seg = NULL;
644 state->new_deq_ptr = NULL;
Julius Werner1f81b6d2014-04-25 19:20:13 +0300645 return;
646 }
Julius Werner1f81b6d2014-04-25 19:20:13 +0300647
Mathias Nyman365038d2014-08-19 15:17:58 +0300648 } while (!cycle_found || !td_last_trb_found);
Sarah Sharpae636742009-04-29 19:02:31 -0700649
Mathias Nyman365038d2014-08-19 15:17:58 +0300650 state->new_deq_seg = new_seg;
651 state->new_deq_ptr = new_deq;
Sarah Sharpae636742009-04-29 19:02:31 -0700652
Mathias Nyman93ceaa82020-04-21 17:08:20 +0300653done:
Julius Werner1f81b6d2014-04-25 19:20:13 +0300654 /* Don't update the ring cycle state for the producer (us). */
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300655 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
656 "Cycle state = 0x%x", state->new_cycle_state);
Sarah Sharp01a1fdb2011-02-23 18:12:29 -0800657
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300658 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
659 "New dequeue segment = %p (virtual)",
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700660 state->new_deq_seg);
661 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300662 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
663 "New dequeue pointer = 0x%llx (DMA)",
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700664 (unsigned long long) addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700665}
666
Sarah Sharp522989a2011-07-29 12:44:32 -0700667/* flip_cycle means flip the cycle bit of all but the first and last TRB.
668 * (The last TRB actually points to the ring enqueue pointer, which is not part
669 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
670 */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700671static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Mathias Nyman0d58a1a2016-11-11 15:13:20 +0200672 struct xhci_td *td, bool flip_cycle)
Sarah Sharpae636742009-04-29 19:02:31 -0700673{
Mathias Nyman0d58a1a2016-11-11 15:13:20 +0200674 struct xhci_segment *seg = td->start_seg;
675 union xhci_trb *trb = td->first_trb;
Sarah Sharpae636742009-04-29 19:02:31 -0700676
Mathias Nyman0d58a1a2016-11-11 15:13:20 +0200677 while (1) {
Mathias Nymanae1e3f02017-01-23 14:20:15 +0200678 trb_to_noop(trb, TRB_TR_NOOP);
679
Mathias Nyman0d58a1a2016-11-11 15:13:20 +0200680 /* flip cycle if asked to */
681 if (flip_cycle && trb != td->first_trb && trb != td->last_trb)
682 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE);
683
684 if (trb == td->last_trb)
Sarah Sharpae636742009-04-29 19:02:31 -0700685 break;
Mathias Nyman0d58a1a2016-11-11 15:13:20 +0200686
687 next_trb(xhci, ep_ring, &seg, &trb);
Sarah Sharpae636742009-04-29 19:02:31 -0700688 }
689}
690
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700691static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700692 struct xhci_virt_ep *ep)
693{
Mathias Nyman9983a5f2017-01-23 14:19:52 +0200694 ep->ep_state &= ~EP_STOP_CMD_PENDING;
Mathias Nymanf9926592017-01-23 14:19:53 +0200695 /* Can't del_timer_sync in interrupt */
696 del_timer(&ep->stop_cmd_timer);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700697}
698
Mathias Nyman446b3142016-11-11 15:13:22 +0200699/*
Mathias Nyman2a721262016-11-11 15:13:24 +0200700 * Must be called with xhci->lock held in interrupt context,
701 * releases and re-acquires xhci->lock
Mathias Nyman446b3142016-11-11 15:13:22 +0200702 */
Mathias Nyman2a721262016-11-11 15:13:24 +0200703static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
704 struct xhci_td *cur_td, int status)
Mathias Nyman446b3142016-11-11 15:13:22 +0200705{
Mathias Nyman2a721262016-11-11 15:13:24 +0200706 struct urb *urb = cur_td->urb;
707 struct urb_priv *urb_priv = urb->hcpriv;
708 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
Mathias Nyman446b3142016-11-11 15:13:22 +0200709
Mathias Nyman2a721262016-11-11 15:13:24 +0200710 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
711 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
712 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
713 if (xhci->quirks & XHCI_AMD_PLL_FIX)
714 usb_amd_quirk_pll_enable();
715 }
716 }
Mathias Nyman446b3142016-11-11 15:13:22 +0200717 xhci_urb_free_priv(urb_priv);
Mathias Nyman2a721262016-11-11 15:13:24 +0200718 usb_hcd_unlink_urb_from_ep(hcd, urb);
Felipe Balbi5abdc2e2017-01-23 14:20:20 +0200719 trace_xhci_urb_giveback(urb);
Mathias Nyman7bc5d5a2017-05-17 18:31:59 +0300720 usb_hcd_giveback_urb(hcd, urb, status);
Mathias Nyman446b3142016-11-11 15:13:22 +0200721}
722
Wei Yongjun2d6d5762016-11-11 15:13:21 +0200723static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci,
724 struct xhci_ring *ring, struct xhci_td *td)
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300725{
726 struct device *dev = xhci_to_hcd(xhci)->self.controller;
727 struct xhci_segment *seg = td->bounce_seg;
728 struct urb *urb = td->urb;
Henry Lin597c56e2019-05-22 14:33:57 +0300729 size_t len;
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300730
Felipe Balbif45e2a02017-01-23 14:20:13 +0200731 if (!ring || !seg || !urb)
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300732 return;
733
734 if (usb_urb_dir_out(urb)) {
735 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
736 DMA_TO_DEVICE);
737 return;
738 }
739
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300740 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
741 DMA_FROM_DEVICE);
Henry Lin597c56e2019-05-22 14:33:57 +0300742 /* for in tranfers we need to copy the data from bounce to sg */
743 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf,
744 seg->bounce_len, seg->bounce_offs);
745 if (len != seg->bounce_len)
Fabio Estevamc1a145a2019-05-22 10:35:29 -0300746 xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n",
Henry Lin597c56e2019-05-22 14:33:57 +0300747 len, seg->bounce_len);
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300748 seg->bounce_len = 0;
749 seg->bounce_offs = 0;
750}
751
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200752static int xhci_td_cleanup(struct xhci_hcd *xhci, struct xhci_td *td,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +0200753 struct xhci_ring *ep_ring, int status)
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200754{
755 struct urb *urb = NULL;
756
757 /* Clean up the endpoint's TD list */
758 urb = td->urb;
759
760 /* if a bounce buffer was used to align this td then unmap it */
761 xhci_unmap_td_bounce_buffer(xhci, ep_ring, td);
762
763 /* Do one last check of the actual transfer length.
764 * If the host controller said we transferred more data than the buffer
765 * length, urb->actual_length will be a very big number (since it's
766 * unsigned). Play it safe and say we didn't transfer anything.
767 */
768 if (urb->actual_length > urb->transfer_buffer_length) {
769 xhci_warn(xhci, "URB req %u and actual %u transfer length mismatch\n",
770 urb->transfer_buffer_length, urb->actual_length);
771 urb->actual_length = 0;
Mathias Nymana6ccd1f2021-01-29 15:00:35 +0200772 status = 0;
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200773 }
Mathias Nymane1a29832021-01-29 15:00:34 +0200774 /* TD might be removed from td_list if we are giving back a cancelled URB */
775 if (!list_empty(&td->td_list))
776 list_del_init(&td->td_list);
777 /* Giving back a cancelled URB, or if a slated TD completed anyway */
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200778 if (!list_empty(&td->cancelled_td_list))
779 list_del_init(&td->cancelled_td_list);
780
781 inc_td_cnt(urb);
782 /* Giveback the urb when all the tds are completed */
783 if (last_td_in_urb(td)) {
784 if ((urb->actual_length != urb->transfer_buffer_length &&
785 (urb->transfer_flags & URB_SHORT_NOT_OK)) ||
Mathias Nymana6ccd1f2021-01-29 15:00:35 +0200786 (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc)))
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200787 xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n",
788 urb, urb->actual_length,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +0200789 urb->transfer_buffer_length, status);
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200790
791 /* set isoc urb status to 0 just as EHCI, UHCI, and OHCI */
792 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
Mathias Nymana6ccd1f2021-01-29 15:00:35 +0200793 status = 0;
794 xhci_giveback_urb_in_irq(xhci, td, status);
Mathias Nyman69eaf9e72021-01-29 15:00:33 +0200795 }
796
797 return 0;
798}
799
Mathias Nyman674f8432021-01-29 15:00:38 +0200800
801/* Complete the cancelled URBs we unlinked from td_list. */
802static void xhci_giveback_invalidated_tds(struct xhci_virt_ep *ep)
803{
804 struct xhci_ring *ring;
805 struct xhci_td *td, *tmp_td;
806
807 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list,
808 cancelled_td_list) {
809
810 /*
811 * Doesn't matter what we pass for status, since the core will
812 * just overwrite it (because the URB has been unlinked).
813 */
814 ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb);
815
816 if (td->cancel_status == TD_CLEARED)
817 xhci_td_cleanup(ep->xhci, td, ring, 0);
818
819 if (ep->xhci->xhc_state & XHCI_STATE_DYING)
820 return;
821 }
822}
823
Mathias Nymand8ac9502021-01-29 15:00:32 +0200824static int xhci_reset_halted_ep(struct xhci_hcd *xhci, unsigned int slot_id,
825 unsigned int ep_index, enum xhci_ep_reset_type reset_type)
826{
827 struct xhci_command *command;
828 int ret = 0;
829
830 command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
831 if (!command) {
832 ret = -ENOMEM;
833 goto done;
834 }
835
836 ret = xhci_queue_reset_ep(xhci, command, slot_id, ep_index, reset_type);
837done:
838 if (ret)
839 xhci_err(xhci, "ERROR queuing reset endpoint for slot %d ep_index %d, %d\n",
840 slot_id, ep_index, ret);
841 return ret;
842}
843
Mathias Nyman7c6c3342021-01-29 15:00:37 +0200844static void xhci_handle_halted_endpoint(struct xhci_hcd *xhci,
845 struct xhci_virt_ep *ep, unsigned int stream_id,
846 struct xhci_td *td,
847 enum xhci_ep_reset_type reset_type)
848{
849 unsigned int slot_id = ep->vdev->slot_id;
850 int err;
851
852 /*
853 * Avoid resetting endpoint if link is inactive. Can cause host hang.
854 * Device will be reset soon to recover the link so don't do anything
855 */
856 if (ep->vdev->flags & VDEV_PORT_ERROR)
857 return;
858
859 ep->ep_state |= EP_HALTED;
860
Mathias Nyman674f8432021-01-29 15:00:38 +0200861 /* add td to cancelled list and let reset ep handler take care of it */
862 if (reset_type == EP_HARD_RESET) {
863 ep->ep_state |= EP_HARD_CLEAR_TOGGLE;
864 if (td && list_empty(&td->cancelled_td_list)) {
865 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
866 td->cancel_status = TD_HALTED;
867 }
868 }
869
Mathias Nyman7c6c3342021-01-29 15:00:37 +0200870 err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type);
871 if (err)
872 return;
873
Mathias Nyman7c6c3342021-01-29 15:00:37 +0200874 xhci_ring_cmd_db(xhci);
875}
876
Sarah Sharpae636742009-04-29 19:02:31 -0700877/*
Mathias Nyman4db356922021-01-29 15:00:36 +0200878 * Fix up the ep ring first, so HW stops executing cancelled TDs.
879 * We have the xHCI lock, so nothing can modify this list until we drop it.
880 * We're also in the event handler, so we can't get re-interrupted if another
881 * Stop Endpoint command completes.
Mathias Nyman674f8432021-01-29 15:00:38 +0200882 *
883 * only call this when ring is not in a running state
Mathias Nyman4db356922021-01-29 15:00:36 +0200884 */
885
Mathias Nyman674f8432021-01-29 15:00:38 +0200886static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep)
Mathias Nyman4db356922021-01-29 15:00:36 +0200887{
888 struct xhci_hcd *xhci;
889 struct xhci_td *td = NULL;
890 struct xhci_td *tmp_td = NULL;
Mathias Nyman674f8432021-01-29 15:00:38 +0200891 struct xhci_td *cached_td = NULL;
Mathias Nyman4db356922021-01-29 15:00:36 +0200892 struct xhci_ring *ring;
Mathias Nyman674f8432021-01-29 15:00:38 +0200893 struct xhci_dequeue_state deq_state;
Mathias Nyman4db356922021-01-29 15:00:36 +0200894 u64 hw_deq;
Mathias Nyman674f8432021-01-29 15:00:38 +0200895 unsigned int slot_id = ep->vdev->slot_id;
Mathias Nyman4db356922021-01-29 15:00:36 +0200896
897 xhci = ep->xhci;
898
899 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) {
900 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
901 "Removing canceled TD starting at 0x%llx (dma).",
902 (unsigned long long)xhci_trb_virt_to_dma(
903 td->start_seg, td->first_trb));
904 list_del_init(&td->td_list);
905 ring = xhci_urb_to_transfer_ring(xhci, td->urb);
906 if (!ring) {
907 xhci_warn(xhci, "WARN Cancelled URB %p has invalid stream ID %u.\n",
908 td->urb, td->urb->stream_id);
909 continue;
910 }
911 /*
912 * If ring stopped on the TD we need to cancel, then we have to
913 * move the xHC endpoint ring dequeue pointer past this TD.
914 */
915 hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index,
916 td->urb->stream_id);
917 hw_deq &= ~0xf;
918
919 if (trb_in_td(xhci, td->start_seg, td->first_trb,
920 td->last_trb, hw_deq, false)) {
Mathias Nyman674f8432021-01-29 15:00:38 +0200921 switch (td->cancel_status) {
922 case TD_CLEARED: /* TD is already no-op */
923 case TD_CLEARING_CACHE: /* set TR deq command already queued */
924 break;
925 case TD_DIRTY: /* TD is cached, clear it */
926 case TD_HALTED:
927 /* FIXME stream case, several stopped rings */
928 cached_td = td;
929 break;
930 }
Mathias Nyman4db356922021-01-29 15:00:36 +0200931 } else {
932 td_to_noop(xhci, ring, td, false);
Mathias Nyman674f8432021-01-29 15:00:38 +0200933 td->cancel_status = TD_CLEARED;
Mathias Nyman4db356922021-01-29 15:00:36 +0200934 }
Mathias Nyman674f8432021-01-29 15:00:38 +0200935 }
936 if (cached_td) {
937 cached_td->cancel_status = TD_CLEARING_CACHE;
938 xhci_find_new_dequeue_state(xhci, slot_id, ep->ep_index,
939 cached_td->urb->stream_id,
940 cached_td, &deq_state);
941 xhci_queue_new_dequeue_state(xhci, slot_id, ep->ep_index,
942 &deq_state);
Mathias Nyman4db356922021-01-29 15:00:36 +0200943 }
944 return 0;
945}
946
Mathias Nyman9ebf3002021-01-29 15:00:39 +0200947
948/*
949 * Returns the TD the endpoint ring halted on.
950 * Only call for non-running rings without streams.
951 */
952static struct xhci_td *find_halted_td(struct xhci_virt_ep *ep)
953{
954 struct xhci_td *td;
955 u64 hw_deq;
956
957 if (!list_empty(&ep->ring->td_list)) { /* Not streams compatible */
958 hw_deq = xhci_get_hw_deq(ep->xhci, ep->vdev, ep->ep_index, 0);
959 hw_deq &= ~0xf;
960 td = list_first_entry(&ep->ring->td_list, struct xhci_td, td_list);
961 if (trb_in_td(ep->xhci, td->start_seg, td->first_trb,
962 td->last_trb, hw_deq, false))
963 return td;
964 }
965 return NULL;
966}
967
Mathias Nyman4db356922021-01-29 15:00:36 +0200968/*
Sarah Sharpae636742009-04-29 19:02:31 -0700969 * When we get a command completion for a Stop Endpoint Command, we need to
970 * unlink any cancelled TDs from the ring. There are two ways to do that:
971 *
972 * 1. If the HW was in the middle of processing the TD that needs to be
973 * cancelled, then we must move the ring's dequeue pointer past the last TRB
974 * in the TD with a Set Dequeue Pointer Command.
975 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
976 * bit cleared) so that the HW will skip over them.
977 */
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +0300978static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id,
Mathias Nyman9ebf3002021-01-29 15:00:39 +0200979 union xhci_trb *trb, u32 comp_code)
Sarah Sharpae636742009-04-29 19:02:31 -0700980{
Sarah Sharpae636742009-04-29 19:02:31 -0700981 unsigned int ep_index;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700982 struct xhci_virt_ep *ep;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +0300983 struct xhci_ep_ctx *ep_ctx;
Mathias Nyman9ebf3002021-01-29 15:00:39 +0200984 struct xhci_td *td = NULL;
985 enum xhci_ep_reset_type reset_type;
Sarah Sharpae636742009-04-29 19:02:31 -0700986
Xenia Ragiadakoubc752bd2013-09-09 13:29:59 +0300987 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) {
Mathias Nyman9ea18332014-05-08 19:26:02 +0300988 if (!xhci->devs[slot_id])
Mathias Nyman674f8432021-01-29 15:00:38 +0200989 xhci_warn(xhci, "Stop endpoint command completion for disabled slot %u\n",
990 slot_id);
Andiry Xube88fe42010-10-14 07:22:57 -0700991 return;
992 }
993
Matt Evans28ccd292011-03-29 13:40:46 +1100994 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Mathias Nymanb1adc422021-01-29 15:00:22 +0200995 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
996 if (!ep)
997 return;
998
Mathias Nyman674f8432021-01-29 15:00:38 +0200999 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
1000
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001001 trace_xhci_handle_cmd_stop_ep(ep_ctx);
1002
Mathias Nyman9ebf3002021-01-29 15:00:39 +02001003 if (comp_code == COMP_CONTEXT_STATE_ERROR) {
1004 /*
1005 * If stop endpoint command raced with a halting endpoint we need to
1006 * reset the host side endpoint first.
1007 * If the TD we halted on isn't cancelled the TD should be given back
1008 * with a proper error code, and the ring dequeue moved past the TD.
1009 * If streams case we can't find hw_deq, or the TD we halted on so do a
1010 * soft reset.
1011 *
1012 * Proper error code is unknown here, it would be -EPIPE if device side
1013 * of enadpoit halted (aka STALL), and -EPROTO if not (transaction error)
1014 * We use -EPROTO, if device is stalled it should return a stall error on
1015 * next transfer, which then will return -EPIPE, and device side stall is
1016 * noted and cleared by class driver.
1017 */
1018 switch (GET_EP_CTX_STATE(ep_ctx)) {
1019 case EP_STATE_HALTED:
1020 xhci_dbg(xhci, "Stop ep completion raced with stall, reset ep\n");
1021 if (ep->ep_state & EP_HAS_STREAMS) {
1022 reset_type = EP_SOFT_RESET;
1023 } else {
1024 reset_type = EP_HARD_RESET;
1025 td = find_halted_td(ep);
1026 if (td)
1027 td->status = -EPROTO;
1028 }
1029 /* reset ep, reset handler cleans up cancelled tds */
1030 xhci_handle_halted_endpoint(xhci, ep, 0, td, reset_type);
1031 xhci_stop_watchdog_timer_in_irq(xhci, ep);
1032 return;
1033 default:
1034 break;
1035 }
1036 }
Mathias Nyman674f8432021-01-29 15:00:38 +02001037 /* will queue a set TR deq if stopped on a cancelled, uncleared TD */
1038 xhci_invalidate_cancelled_tds(ep);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001039 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharpae636742009-04-29 19:02:31 -07001040
Mathias Nyman674f8432021-01-29 15:00:38 +02001041 /* Otherwise ring the doorbell(s) to restart queued transfers */
1042 xhci_giveback_invalidated_tds(ep);
1043 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07001044}
1045
Sarah Sharp50e87252014-02-21 09:27:30 -08001046static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring)
1047{
1048 struct xhci_td *cur_td;
Felipe Balbia54cfae2017-01-23 14:20:17 +02001049 struct xhci_td *tmp;
Sarah Sharp50e87252014-02-21 09:27:30 -08001050
Felipe Balbia54cfae2017-01-23 14:20:17 +02001051 list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) {
Sarah Sharp50e87252014-02-21 09:27:30 -08001052 list_del_init(&cur_td->td_list);
Felipe Balbia54cfae2017-01-23 14:20:17 +02001053
Sarah Sharp50e87252014-02-21 09:27:30 -08001054 if (!list_empty(&cur_td->cancelled_td_list))
1055 list_del_init(&cur_td->cancelled_td_list);
Mathias Nymanf9c589e2016-06-21 10:58:02 +03001056
Felipe Balbia60f2f22017-01-23 14:20:14 +02001057 xhci_unmap_td_bounce_buffer(xhci, ring, cur_td);
Mathias Nyman2a721262016-11-11 15:13:24 +02001058
1059 inc_td_cnt(cur_td->urb);
1060 if (last_td_in_urb(cur_td))
1061 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
Sarah Sharp50e87252014-02-21 09:27:30 -08001062 }
1063}
1064
1065static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci,
1066 int slot_id, int ep_index)
1067{
1068 struct xhci_td *cur_td;
Felipe Balbia54cfae2017-01-23 14:20:17 +02001069 struct xhci_td *tmp;
Sarah Sharp50e87252014-02-21 09:27:30 -08001070 struct xhci_virt_ep *ep;
1071 struct xhci_ring *ring;
1072
1073 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharp21d0e512014-02-21 14:29:02 -08001074 if ((ep->ep_state & EP_HAS_STREAMS) ||
1075 (ep->ep_state & EP_GETTING_NO_STREAMS)) {
1076 int stream_id;
1077
Mathias Nyman4b895862017-07-20 14:48:26 +03001078 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
Sarah Sharp21d0e512014-02-21 14:29:02 -08001079 stream_id++) {
Mathias Nyman4b895862017-07-20 14:48:26 +03001080 ring = ep->stream_info->stream_rings[stream_id];
1081 if (!ring)
1082 continue;
1083
Sarah Sharp21d0e512014-02-21 14:29:02 -08001084 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1085 "Killing URBs for slot ID %u, ep index %u, stream %u",
Mathias Nyman4b895862017-07-20 14:48:26 +03001086 slot_id, ep_index, stream_id);
1087 xhci_kill_ring_urbs(xhci, ring);
Sarah Sharp21d0e512014-02-21 14:29:02 -08001088 }
1089 } else {
1090 ring = ep->ring;
1091 if (!ring)
1092 return;
1093 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1094 "Killing URBs for slot ID %u, ep index %u",
1095 slot_id, ep_index);
1096 xhci_kill_ring_urbs(xhci, ring);
1097 }
Mathias Nyman2a721262016-11-11 15:13:24 +02001098
Felipe Balbia54cfae2017-01-23 14:20:17 +02001099 list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list,
1100 cancelled_td_list) {
1101 list_del_init(&cur_td->cancelled_td_list);
Mathias Nyman2a721262016-11-11 15:13:24 +02001102 inc_td_cnt(cur_td->urb);
Felipe Balbia54cfae2017-01-23 14:20:17 +02001103
Mathias Nyman2a721262016-11-11 15:13:24 +02001104 if (last_td_in_urb(cur_td))
1105 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
Sarah Sharp50e87252014-02-21 09:27:30 -08001106 }
1107}
1108
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001109/*
1110 * host controller died, register read returns 0xffffffff
1111 * Complete pending commands, mark them ABORTED.
1112 * URBs need to be given back as usb core might be waiting with device locks
1113 * held for the URBs to finish during device disconnect, blocking host remove.
1114 *
1115 * Call with xhci->lock held.
1116 * lock is relased and re-acquired while giving back urb.
1117 */
1118void xhci_hc_died(struct xhci_hcd *xhci)
1119{
1120 int i, j;
1121
1122 if (xhci->xhc_state & XHCI_STATE_DYING)
1123 return;
1124
1125 xhci_err(xhci, "xHCI host controller not responding, assume dead\n");
1126 xhci->xhc_state |= XHCI_STATE_DYING;
1127
1128 xhci_cleanup_command_queue(xhci);
1129
1130 /* return any pending urbs, remove may be waiting for them */
1131 for (i = 0; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
1132 if (!xhci->devs[i])
1133 continue;
1134 for (j = 0; j < 31; j++)
1135 xhci_kill_endpoint_urbs(xhci, i, j);
1136 }
1137
1138 /* inform usb core hc died if PCI remove isn't already handling it */
1139 if (!(xhci->xhc_state & XHCI_STATE_REMOVING))
1140 usb_hc_died(xhci_to_hcd(xhci));
1141}
1142
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001143/* Watchdog timer function for when a stop endpoint command fails to complete.
1144 * In this case, we assume the host controller is broken or dying or dead. The
1145 * host may still be completing some other events, so we have to be careful to
1146 * let the event ring handler and the URB dequeueing/enqueueing functions know
1147 * through xhci->state.
1148 *
1149 * The timer may also fire if the host takes a very long time to respond to the
1150 * command, and the stop endpoint command completion handler cannot delete the
1151 * timer before the timer function is called. Another endpoint cancellation may
1152 * sneak in before the timer function can grab the lock, and that may queue
1153 * another stop endpoint command and add the timer back. So we cannot use a
1154 * simple flag to say whether there is a pending stop endpoint command for a
1155 * particular endpoint.
1156 *
Mathias Nymanf9926592017-01-23 14:19:53 +02001157 * Instead we use a combination of that flag and checking if a new timer is
1158 * pending.
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001159 */
Kees Cook66a45502017-10-16 16:16:58 -07001160void xhci_stop_endpoint_command_watchdog(struct timer_list *t)
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001161{
Kees Cook66a45502017-10-16 16:16:58 -07001162 struct xhci_virt_ep *ep = from_timer(ep, t, stop_cmd_timer);
1163 struct xhci_hcd *xhci = ep->xhci;
Don Zickusf43d6232011-10-20 23:52:14 -04001164 unsigned long flags;
Mathias Nyman9c1aa362020-03-12 16:45:11 +02001165 u32 usbsts;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001166
Don Zickusf43d6232011-10-20 23:52:14 -04001167 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001168
Mathias Nymanf9926592017-01-23 14:19:53 +02001169 /* bail out if cmd completed but raced with stop ep watchdog timer.*/
1170 if (!(ep->ep_state & EP_STOP_CMD_PENDING) ||
1171 timer_pending(&ep->stop_cmd_timer)) {
Don Zickusf43d6232011-10-20 23:52:14 -04001172 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanf9926592017-01-23 14:19:53 +02001173 xhci_dbg(xhci, "Stop EP timer raced with cmd completion, exit");
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001174 return;
1175 }
Mathias Nyman9c1aa362020-03-12 16:45:11 +02001176 usbsts = readl(&xhci->op_regs->status);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001177
1178 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
Mathias Nyman9c1aa362020-03-12 16:45:11 +02001179 xhci_warn(xhci, "USBSTS:%s\n", xhci_decode_usbsts(usbsts));
1180
Mathias Nymanf9926592017-01-23 14:19:53 +02001181 ep->ep_state &= ~EP_STOP_CMD_PENDING;
1182
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001183 xhci_halt(xhci);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001184
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001185 /*
1186 * handle a stop endpoint cmd timeout as if host died (-ENODEV).
1187 * In the future we could distinguish between -ENODEV and -ETIMEDOUT
1188 * and try to recover a -ETIMEDOUT with a host controller reset
1189 */
1190 xhci_hc_died(xhci);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001191
Don Zickusf43d6232011-10-20 23:52:14 -04001192 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001193 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001194 "xHCI host controller is dead.");
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001195}
1196
Andiry Xub008df62012-03-05 17:49:34 +08001197static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci,
1198 struct xhci_virt_device *dev,
1199 struct xhci_ring *ep_ring,
1200 unsigned int ep_index)
1201{
1202 union xhci_trb *dequeue_temp;
1203 int num_trbs_free_temp;
1204 bool revert = false;
1205
1206 num_trbs_free_temp = ep_ring->num_trbs_free;
1207 dequeue_temp = ep_ring->dequeue;
1208
Sarah Sharp0d9f78a2012-06-21 16:28:30 -07001209 /* If we get two back-to-back stalls, and the first stalled transfer
1210 * ends just before a link TRB, the dequeue pointer will be left on
1211 * the link TRB by the code in the while loop. So we have to update
1212 * the dequeue pointer one segment further, or we'll jump off
1213 * the segment into la-la-land.
1214 */
Mathias Nyman2d98ef42016-06-21 10:58:04 +03001215 if (trb_is_link(ep_ring->dequeue)) {
Sarah Sharp0d9f78a2012-06-21 16:28:30 -07001216 ep_ring->deq_seg = ep_ring->deq_seg->next;
1217 ep_ring->dequeue = ep_ring->deq_seg->trbs;
1218 }
1219
Andiry Xub008df62012-03-05 17:49:34 +08001220 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) {
1221 /* We have more usable TRBs */
1222 ep_ring->num_trbs_free++;
1223 ep_ring->dequeue++;
Mathias Nyman2d98ef42016-06-21 10:58:04 +03001224 if (trb_is_link(ep_ring->dequeue)) {
Andiry Xub008df62012-03-05 17:49:34 +08001225 if (ep_ring->dequeue ==
1226 dev->eps[ep_index].queued_deq_ptr)
1227 break;
1228 ep_ring->deq_seg = ep_ring->deq_seg->next;
1229 ep_ring->dequeue = ep_ring->deq_seg->trbs;
1230 }
1231 if (ep_ring->dequeue == dequeue_temp) {
1232 revert = true;
1233 break;
1234 }
1235 }
1236
1237 if (revert) {
1238 xhci_dbg(xhci, "Unable to find new dequeue pointer\n");
1239 ep_ring->num_trbs_free = num_trbs_free_temp;
1240 }
1241}
1242
Sarah Sharpae636742009-04-29 19:02:31 -07001243/*
1244 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
1245 * we need to clear the set deq pending flag in the endpoint ring state, so that
1246 * the TD queueing code can ring the doorbell again. We also need to ring the
1247 * endpoint doorbell to restart the ring, but only if there aren't more
1248 * cancellations pending.
1249 */
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001250static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001251 union xhci_trb *trb, u32 cmd_comp_code)
Sarah Sharpae636742009-04-29 19:02:31 -07001252{
Sarah Sharpae636742009-04-29 19:02:31 -07001253 unsigned int ep_index;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001254 unsigned int stream_id;
Sarah Sharpae636742009-04-29 19:02:31 -07001255 struct xhci_ring *ep_ring;
Hans de Goede9aad95e2013-10-04 00:29:49 +02001256 struct xhci_virt_ep *ep;
John Yound115b042009-07-27 12:05:15 -07001257 struct xhci_ep_ctx *ep_ctx;
1258 struct xhci_slot_ctx *slot_ctx;
Mathias Nyman674f8432021-01-29 15:00:38 +02001259 struct xhci_td *td, *tmp_td;
Sarah Sharpae636742009-04-29 19:02:31 -07001260
Matt Evans28ccd292011-03-29 13:40:46 +11001261 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
1262 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
Mathias Nymanb1adc422021-01-29 15:00:22 +02001263 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
1264 if (!ep)
1265 return;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001266
Mathias Nyman42f28902021-01-29 15:00:24 +02001267 ep_ring = xhci_virt_ep_to_ring(xhci, ep, stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001268 if (!ep_ring) {
Oliver Neukume587b8b2014-01-08 17:13:11 +01001269 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n",
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001270 stream_id);
1271 /* XXX: Harmless??? */
Hans de Goede0d4976e2014-08-20 16:41:55 +03001272 goto cleanup;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001273 }
1274
Mathias Nymanb1adc422021-01-29 15:00:22 +02001275 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
1276 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx);
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001277 trace_xhci_handle_cmd_set_deq(slot_ctx);
1278 trace_xhci_handle_cmd_set_deq_ep(ep_ctx);
Sarah Sharpae636742009-04-29 19:02:31 -07001279
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001280 if (cmd_comp_code != COMP_SUCCESS) {
Sarah Sharpae636742009-04-29 19:02:31 -07001281 unsigned int ep_state;
1282 unsigned int slot_state;
1283
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001284 switch (cmd_comp_code) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001285 case COMP_TRB_ERROR:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001286 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n");
Sarah Sharpae636742009-04-29 19:02:31 -07001287 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001288 case COMP_CONTEXT_STATE_ERROR:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001289 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n");
Mathias Nyman5071e6b2016-11-11 15:13:28 +02001290 ep_state = GET_EP_CTX_STATE(ep_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001291 slot_state = le32_to_cpu(slot_ctx->dev_state);
Sarah Sharpae636742009-04-29 19:02:31 -07001292 slot_state = GET_SLOT_STATE(slot_state);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001293 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1294 "Slot state = %u, EP state = %u",
Sarah Sharpae636742009-04-29 19:02:31 -07001295 slot_state, ep_state);
1296 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001297 case COMP_SLOT_NOT_ENABLED_ERROR:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001298 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n",
1299 slot_id);
Sarah Sharpae636742009-04-29 19:02:31 -07001300 break;
1301 default:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001302 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n",
1303 cmd_comp_code);
Sarah Sharpae636742009-04-29 19:02:31 -07001304 break;
1305 }
1306 /* OK what do we do now? The endpoint state is hosed, and we
1307 * should never get to this point if the synchronization between
1308 * queueing, and endpoint state are correct. This might happen
1309 * if the device gets disconnected after we've finished
1310 * cancelling URBs, which might not be an error...
1311 */
1312 } else {
Hans de Goede9aad95e2013-10-04 00:29:49 +02001313 u64 deq;
1314 /* 4.6.10 deq ptr is written to the stream ctx for streams */
1315 if (ep->ep_state & EP_HAS_STREAMS) {
1316 struct xhci_stream_ctx *ctx =
1317 &ep->stream_info->stream_ctx_array[stream_id];
1318 deq = le64_to_cpu(ctx->stream_ring) & SCTX_DEQ_MASK;
1319 } else {
1320 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK;
1321 }
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001322 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
Hans de Goede9aad95e2013-10-04 00:29:49 +02001323 "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
1324 if (xhci_trb_virt_to_dma(ep->queued_deq_seg,
1325 ep->queued_deq_ptr) == deq) {
Sarah Sharpbf161e82011-02-23 15:46:42 -08001326 /* Update the ring's dequeue segment and dequeue pointer
1327 * to reflect the new position.
1328 */
Mathias Nymanb1adc422021-01-29 15:00:22 +02001329 update_ring_for_set_deq_completion(xhci, ep->vdev,
Andiry Xub008df62012-03-05 17:49:34 +08001330 ep_ring, ep_index);
Sarah Sharpbf161e82011-02-23 15:46:42 -08001331 } else {
Oliver Neukume587b8b2014-01-08 17:13:11 +01001332 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n");
Sarah Sharpbf161e82011-02-23 15:46:42 -08001333 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
Hans de Goede9aad95e2013-10-04 00:29:49 +02001334 ep->queued_deq_seg, ep->queued_deq_ptr);
Sarah Sharpbf161e82011-02-23 15:46:42 -08001335 }
Sarah Sharpae636742009-04-29 19:02:31 -07001336 }
Mathias Nyman674f8432021-01-29 15:00:38 +02001337 /* HW cached TDs cleared from cache, give them back */
1338 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list,
1339 cancelled_td_list) {
1340 ep_ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb);
1341 if (td->cancel_status == TD_CLEARING_CACHE) {
1342 td->cancel_status = TD_CLEARED;
1343 xhci_td_cleanup(ep->xhci, td, ep_ring, td->status);
1344 }
1345 }
Hans de Goede0d4976e2014-08-20 16:41:55 +03001346cleanup:
Mathias Nymanb1adc422021-01-29 15:00:22 +02001347 ep->ep_state &= ~SET_DEQ_PENDING;
1348 ep->queued_deq_seg = NULL;
1349 ep->queued_deq_ptr = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001350 /* Restart any rings with pending URBs */
1351 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07001352}
1353
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001354static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id,
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001355 union xhci_trb *trb, u32 cmd_comp_code)
Sarah Sharpa1587d92009-07-27 12:03:15 -07001356{
Mathias Nymanb1adc422021-01-29 15:00:22 +02001357 struct xhci_virt_ep *ep;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001358 struct xhci_ep_ctx *ep_ctx;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001359 unsigned int ep_index;
1360
Matt Evans28ccd292011-03-29 13:40:46 +11001361 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Mathias Nymanb1adc422021-01-29 15:00:22 +02001362 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
1363 if (!ep)
1364 return;
1365
1366 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001367 trace_xhci_handle_cmd_reset_ep(ep_ctx);
1368
Sarah Sharpa1587d92009-07-27 12:03:15 -07001369 /* This command will only fail if the endpoint wasn't halted,
1370 * but we don't care.
1371 */
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03001372 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001373 "Ignoring reset ep completion code of %u", cmd_comp_code);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001374
Mathias Nyman674f8432021-01-29 15:00:38 +02001375 /* Cleanup cancelled TDs as ep is stopped. May queue a Set TR Deq cmd */
1376 xhci_invalidate_cancelled_tds(ep);
Lu Baolu74e0b562017-04-07 17:57:05 +03001377
Mathias Nyman674f8432021-01-29 15:00:38 +02001378 if (xhci->quirks & XHCI_RESET_EP_QUIRK)
1379 xhci_dbg(xhci, "Note: Removed workaround to queue config ep for this hw");
1380 /* Clear our internal halted state */
1381 ep->ep_state &= ~EP_HALTED;
Lu Baolu74e0b562017-04-07 17:57:05 +03001382
Mathias Nyman674f8432021-01-29 15:00:38 +02001383 xhci_giveback_invalidated_tds(ep);
Mathias Nymanf8f80be2018-09-20 19:13:37 +03001384
1385 /* if this was a soft reset, then restart */
1386 if ((le32_to_cpu(trb->generic.field[3])) & TRB_TSP)
1387 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001388}
Sarah Sharpae636742009-04-29 19:02:31 -07001389
Xenia Ragiadakoub244b432013-09-09 13:29:47 +03001390static void xhci_handle_cmd_enable_slot(struct xhci_hcd *xhci, int slot_id,
Lu Baoluc2d3d492016-11-11 15:13:31 +02001391 struct xhci_command *command, u32 cmd_comp_code)
Xenia Ragiadakoub244b432013-09-09 13:29:47 +03001392{
1393 if (cmd_comp_code == COMP_SUCCESS)
Lu Baoluc2d3d492016-11-11 15:13:31 +02001394 command->slot_id = slot_id;
Xenia Ragiadakoub244b432013-09-09 13:29:47 +03001395 else
Lu Baoluc2d3d492016-11-11 15:13:31 +02001396 command->slot_id = 0;
Xenia Ragiadakoub244b432013-09-09 13:29:47 +03001397}
1398
Xenia Ragiadakou6c02dd12013-09-09 13:29:48 +03001399static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id)
1400{
1401 struct xhci_virt_device *virt_dev;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001402 struct xhci_slot_ctx *slot_ctx;
Xenia Ragiadakou6c02dd12013-09-09 13:29:48 +03001403
1404 virt_dev = xhci->devs[slot_id];
1405 if (!virt_dev)
1406 return;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001407
1408 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
1409 trace_xhci_handle_cmd_disable_slot(slot_ctx);
1410
Xenia Ragiadakou6c02dd12013-09-09 13:29:48 +03001411 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1412 /* Delete default control endpoint resources */
1413 xhci_free_device_endpoint_resources(xhci, virt_dev, true);
1414 xhci_free_virt_device(xhci, slot_id);
1415}
1416
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001417static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id,
Mathias Nymana18103072021-01-29 15:00:21 +02001418 u32 cmd_comp_code)
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001419{
1420 struct xhci_virt_device *virt_dev;
1421 struct xhci_input_control_ctx *ctrl_ctx;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001422 struct xhci_ep_ctx *ep_ctx;
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001423 unsigned int ep_index;
1424 unsigned int ep_state;
1425 u32 add_flags, drop_flags;
1426
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001427 /*
1428 * Configure endpoint commands can come from the USB core
1429 * configuration or alt setting changes, or because the HW
1430 * needed an extra configure endpoint command after a reset
1431 * endpoint command or streams were being configured.
1432 * If the command was for a halted endpoint, the xHCI driver
1433 * is not waiting on the configure endpoint command.
1434 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03001435 virt_dev = xhci->devs[slot_id];
Mathias Nyman03ed5792021-01-29 15:00:23 +02001436 if (!virt_dev)
1437 return;
Lin Wang4daf9df2015-01-09 16:06:31 +02001438 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001439 if (!ctrl_ctx) {
1440 xhci_warn(xhci, "Could not get input context, bad type.\n");
1441 return;
1442 }
1443
1444 add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1445 drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1446 /* Input ctx add_flags are the endpoint index plus one */
1447 ep_index = xhci_last_valid_endpoint(add_flags) - 1;
1448
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001449 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index);
1450 trace_xhci_handle_cmd_config_ep(ep_ctx);
1451
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001452 /* A usb_set_interface() call directly after clearing a halted
1453 * condition may race on this quirky hardware. Not worth
1454 * worrying about, since this is prototype hardware. Not sure
1455 * if this will work for streams, but streams support was
1456 * untested on this prototype.
1457 */
1458 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
1459 ep_index != (unsigned int) -1 &&
1460 add_flags - SLOT_FLAG == drop_flags) {
1461 ep_state = virt_dev->eps[ep_index].ep_state;
1462 if (!(ep_state & EP_HALTED))
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001463 return;
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001464 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1465 "Completed config ep cmd - "
1466 "last ep index = %d, state = %d",
1467 ep_index, ep_state);
1468 /* Clear internal halted state and restart ring(s) */
1469 virt_dev->eps[ep_index].ep_state &= ~EP_HALTED;
1470 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1471 return;
1472 }
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001473 return;
1474}
1475
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001476static void xhci_handle_cmd_addr_dev(struct xhci_hcd *xhci, int slot_id)
1477{
1478 struct xhci_virt_device *vdev;
1479 struct xhci_slot_ctx *slot_ctx;
1480
1481 vdev = xhci->devs[slot_id];
Mathias Nyman03ed5792021-01-29 15:00:23 +02001482 if (!vdev)
1483 return;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001484 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
1485 trace_xhci_handle_cmd_addr_dev(slot_ctx);
1486}
1487
Mathias Nymana18103072021-01-29 15:00:21 +02001488static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id)
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001489{
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001490 struct xhci_virt_device *vdev;
1491 struct xhci_slot_ctx *slot_ctx;
1492
1493 vdev = xhci->devs[slot_id];
Mathias Nyman03ed5792021-01-29 15:00:23 +02001494 if (!vdev) {
1495 xhci_warn(xhci, "Reset device command completion for disabled slot %u\n",
1496 slot_id);
1497 return;
1498 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001499 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
1500 trace_xhci_handle_cmd_reset_dev(slot_ctx);
1501
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001502 xhci_dbg(xhci, "Completed reset device command.\n");
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001503}
1504
Xenia Ragiadakou2c070822013-09-09 13:29:52 +03001505static void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci,
1506 struct xhci_event_cmd *event)
1507{
1508 if (!(xhci->quirks & XHCI_NEC_HOST)) {
Lu Baoluf4c8f032016-11-11 15:13:25 +02001509 xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n");
Xenia Ragiadakou2c070822013-09-09 13:29:52 +03001510 return;
1511 }
1512 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1513 "NEC firmware version %2x.%02x",
1514 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1515 NEC_FW_MINOR(le32_to_cpu(event->status)));
1516}
1517
Mathias Nyman9ea18332014-05-08 19:26:02 +03001518static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 status)
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001519{
1520 list_del(&cmd->cmd_list);
Mathias Nyman9ea18332014-05-08 19:26:02 +03001521
1522 if (cmd->completion) {
1523 cmd->status = status;
1524 complete(cmd->completion);
1525 } else {
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001526 kfree(cmd);
Mathias Nyman9ea18332014-05-08 19:26:02 +03001527 }
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001528}
1529
1530void xhci_cleanup_command_queue(struct xhci_hcd *xhci)
1531{
1532 struct xhci_command *cur_cmd, *tmp_cmd;
Jeffy Chend1aad522017-10-06 17:45:28 +03001533 xhci->current_cmd = NULL;
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001534 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list)
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001535 xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED);
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001536}
1537
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +02001538void xhci_handle_command_timeout(struct work_struct *work)
Mathias Nymanc311e392014-05-08 19:26:03 +03001539{
1540 struct xhci_hcd *xhci;
Mathias Nymanc311e392014-05-08 19:26:03 +03001541 unsigned long flags;
1542 u64 hw_ring_state;
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +02001543
1544 xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer);
Mathias Nymanc311e392014-05-08 19:26:03 +03001545
Mathias Nymanc311e392014-05-08 19:26:03 +03001546 spin_lock_irqsave(&xhci->lock, flags);
Lu Baolu2b985462017-01-03 18:28:46 +02001547
Mathias Nymana5a1b952017-01-03 18:28:48 +02001548 /*
1549 * If timeout work is pending, or current_cmd is NULL, it means we
1550 * raced with command completion. Command is handled so just return.
1551 */
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +02001552 if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) {
Lu Baolu2b985462017-01-03 18:28:46 +02001553 spin_unlock_irqrestore(&xhci->lock, flags);
1554 return;
Mathias Nymanc311e392014-05-08 19:26:03 +03001555 }
Lu Baolu2b985462017-01-03 18:28:46 +02001556 /* mark this command to be cancelled */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001557 xhci->current_cmd->status = COMP_COMMAND_ABORTED;
Lu Baolu2b985462017-01-03 18:28:46 +02001558
Mathias Nymanc311e392014-05-08 19:26:03 +03001559 /* Make sure command ring is running before aborting it */
1560 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001561 if (hw_ring_state == ~(u64)0) {
1562 xhci_hc_died(xhci);
1563 goto time_out_completed;
1564 }
1565
Mathias Nymanc311e392014-05-08 19:26:03 +03001566 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
1567 (hw_ring_state & CMD_RING_RUNNING)) {
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +02001568 /* Prevent new doorbell, and start command abort */
1569 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED;
Mathias Nymanc311e392014-05-08 19:26:03 +03001570 xhci_dbg(xhci, "Command timeout\n");
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001571 xhci_abort_cmd_ring(xhci, flags);
Lu Baolu4dea7072017-01-03 18:28:49 +02001572 goto time_out_completed;
Mathias Nymanc311e392014-05-08 19:26:03 +03001573 }
Mathias Nyman3425aa02016-06-01 18:09:08 +03001574
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +02001575 /* host removed. Bail out */
1576 if (xhci->xhc_state & XHCI_STATE_REMOVING) {
1577 xhci_dbg(xhci, "host removed, ring start fail?\n");
Mathias Nyman3425aa02016-06-01 18:09:08 +03001578 xhci_cleanup_command_queue(xhci);
Lu Baolu4dea7072017-01-03 18:28:49 +02001579
1580 goto time_out_completed;
Mathias Nyman3425aa02016-06-01 18:09:08 +03001581 }
1582
Mathias Nymanc311e392014-05-08 19:26:03 +03001583 /* command timeout on stopped ring, ring can't be aborted */
1584 xhci_dbg(xhci, "Command timeout on stopped ring\n");
1585 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd);
Lu Baolu4dea7072017-01-03 18:28:49 +02001586
1587time_out_completed:
Mathias Nymanc311e392014-05-08 19:26:03 +03001588 spin_unlock_irqrestore(&xhci->lock, flags);
1589 return;
1590}
1591
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001592static void handle_cmd_completion(struct xhci_hcd *xhci,
1593 struct xhci_event_cmd *event)
1594{
Lalithambika Krishna Kumar296fcda2021-01-29 15:00:27 +02001595 unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001596 u64 cmd_dma;
1597 dma_addr_t cmd_dequeue_dma;
Xenia Ragiadakoue7a79a12013-09-09 13:29:56 +03001598 u32 cmd_comp_code;
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001599 union xhci_trb *cmd_trb;
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001600 struct xhci_command *cmd;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001601 u32 cmd_type;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001602
Lalithambika Krishna Kumar296fcda2021-01-29 15:00:27 +02001603 if (slot_id >= MAX_HC_SLOTS) {
1604 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id);
1605 return;
1606 }
1607
Matt Evans28ccd292011-03-29 13:40:46 +11001608 cmd_dma = le64_to_cpu(event->cmd_trb);
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001609 cmd_trb = xhci->cmd_ring->dequeue;
Felipe Balbia37c3f72017-01-23 14:20:19 +02001610
1611 trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic);
1612
Sarah Sharp23e3be12009-04-29 19:05:20 -07001613 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001614 cmd_trb);
Lu Baoluf4c8f032016-11-11 15:13:25 +02001615 /*
1616 * Check whether the completion event is for our internal kept
1617 * command.
1618 */
1619 if (!cmd_dequeue_dma || cmd_dma != (u64)cmd_dequeue_dma) {
1620 xhci_warn(xhci,
1621 "ERROR mismatched command completion event\n");
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001622 return;
1623 }
Elric Fub63f4052012-06-27 16:55:43 +08001624
Felipe Balbi04861f82017-01-23 14:20:09 +02001625 cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list);
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001626
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +02001627 cancel_delayed_work(&xhci->cmd_timer);
Mathias Nymanc311e392014-05-08 19:26:03 +03001628
Xenia Ragiadakoue7a79a12013-09-09 13:29:56 +03001629 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status));
Mathias Nymanc311e392014-05-08 19:26:03 +03001630
1631 /* If CMD ring stopped we own the trbs between enqueue and dequeue */
Mathias Nyman604d02a2017-05-17 18:32:05 +03001632 if (cmd_comp_code == COMP_COMMAND_RING_STOPPED) {
OGAWA Hirofumi1c111b62017-01-03 18:28:51 +02001633 complete_all(&xhci->cmd_ring_stop_completion);
Mathias Nymanc311e392014-05-08 19:26:03 +03001634 return;
1635 }
Mathias Nyman33be1262016-08-16 10:18:03 +03001636
1637 if (cmd->command_trb != xhci->cmd_ring->dequeue) {
1638 xhci_err(xhci,
1639 "Command completion event does not match command\n");
1640 return;
1641 }
1642
Mathias Nymanc311e392014-05-08 19:26:03 +03001643 /*
1644 * Host aborted the command ring, check if the current command was
1645 * supposed to be aborted, otherwise continue normally.
1646 * The command ring is stopped now, but the xHC will issue a Command
1647 * Ring Stopped event which will cause us to restart it.
1648 */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001649 if (cmd_comp_code == COMP_COMMAND_ABORTED) {
Mathias Nymanc311e392014-05-08 19:26:03 +03001650 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001651 if (cmd->status == COMP_COMMAND_ABORTED) {
Baolin Wang2a7cfdf2017-01-03 18:28:47 +02001652 if (xhci->current_cmd == cmd)
1653 xhci->current_cmd = NULL;
Mathias Nymanc311e392014-05-08 19:26:03 +03001654 goto event_handled;
Baolin Wang2a7cfdf2017-01-03 18:28:47 +02001655 }
Elric Fub63f4052012-06-27 16:55:43 +08001656 }
1657
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001658 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3]));
1659 switch (cmd_type) {
1660 case TRB_ENABLE_SLOT:
Lu Baoluc2d3d492016-11-11 15:13:31 +02001661 xhci_handle_cmd_enable_slot(xhci, slot_id, cmd, cmd_comp_code);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001662 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001663 case TRB_DISABLE_SLOT:
Xenia Ragiadakou6c02dd12013-09-09 13:29:48 +03001664 xhci_handle_cmd_disable_slot(xhci, slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001665 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001666 case TRB_CONFIG_EP:
Mathias Nyman9ea18332014-05-08 19:26:02 +03001667 if (!cmd->completion)
Mathias Nymana18103072021-01-29 15:00:21 +02001668 xhci_handle_cmd_config_ep(xhci, slot_id, cmd_comp_code);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001669 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001670 case TRB_EVAL_CONTEXT:
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001671 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001672 case TRB_ADDR_DEV:
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03001673 xhci_handle_cmd_addr_dev(xhci, slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001674 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001675 case TRB_STOP_RING:
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001676 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1677 le32_to_cpu(cmd_trb->generic.field[3])));
Mathias Nymana38fe332018-03-16 16:33:02 +02001678 if (!cmd->completion)
Mathias Nyman9ebf3002021-01-29 15:00:39 +02001679 xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb,
1680 cmd_comp_code);
Sarah Sharpae636742009-04-29 19:02:31 -07001681 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001682 case TRB_SET_DEQ:
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001683 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1684 le32_to_cpu(cmd_trb->generic.field[3])));
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001685 xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code);
Sarah Sharpae636742009-04-29 19:02:31 -07001686 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001687 case TRB_CMD_NOOP:
Mathias Nymanc311e392014-05-08 19:26:03 +03001688 /* Is this an aborted command turned to NO-OP? */
Mathias Nyman604d02a2017-05-17 18:32:05 +03001689 if (cmd->status == COMP_COMMAND_RING_STOPPED)
1690 cmd_comp_code = COMP_COMMAND_RING_STOPPED;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001691 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001692 case TRB_RESET_EP:
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001693 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1694 le32_to_cpu(cmd_trb->generic.field[3])));
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001695 xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001696 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001697 case TRB_RESET_DEV:
Mathias Nyman6fcfb0d2014-06-24 17:14:40 +03001698 /* SLOT_ID field in reset device cmd completion event TRB is 0.
1699 * Use the SLOT_ID from the command TRB instead (xhci 4.6.11)
1700 */
1701 slot_id = TRB_TO_SLOT_ID(
1702 le32_to_cpu(cmd_trb->generic.field[3]));
Mathias Nymana18103072021-01-29 15:00:21 +02001703 xhci_handle_cmd_reset_dev(xhci, slot_id);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001704 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001705 case TRB_NEC_GET_FW:
Xenia Ragiadakou2c070822013-09-09 13:29:52 +03001706 xhci_handle_cmd_nec_get_fw(xhci, event);
Sarah Sharp02386342010-05-24 13:25:28 -07001707 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001708 default:
1709 /* Skip over unknown commands on the event ring */
Lu Baoluf4c8f032016-11-11 15:13:25 +02001710 xhci_info(xhci, "INFO unknown command type %d\n", cmd_type);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001711 break;
1712 }
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001713
Mathias Nymanc311e392014-05-08 19:26:03 +03001714 /* restart timer if this wasn't the last command */
Lu Baoludaa47f22017-01-23 14:20:02 +02001715 if (!list_is_singular(&xhci->cmd_list)) {
Felipe Balbi04861f82017-01-23 14:20:09 +02001716 xhci->current_cmd = list_first_entry(&cmd->cmd_list,
1717 struct xhci_command, cmd_list);
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +02001718 xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT);
Lu Baolu2b985462017-01-03 18:28:46 +02001719 } else if (xhci->current_cmd == cmd) {
1720 xhci->current_cmd = NULL;
Mathias Nymanc311e392014-05-08 19:26:03 +03001721 }
1722
1723event_handled:
Mathias Nyman9ea18332014-05-08 19:26:02 +03001724 xhci_complete_del_and_free_cmd(cmd, cmd_comp_code);
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001725
Andiry Xu3b72fca2012-03-05 17:49:32 +08001726 inc_deq(xhci, xhci->cmd_ring);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001727}
1728
Sarah Sharp02386342010-05-24 13:25:28 -07001729static void handle_vendor_event(struct xhci_hcd *xhci,
Mathias Nyman03538102021-01-29 15:00:29 +02001730 union xhci_trb *event, u32 trb_type)
Sarah Sharp02386342010-05-24 13:25:28 -07001731{
Sarah Sharp02386342010-05-24 13:25:28 -07001732 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1733 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1734 handle_cmd_completion(xhci, &event->event_cmd);
1735}
1736
Sarah Sharp623bef92011-11-11 14:57:33 -08001737static void handle_device_notification(struct xhci_hcd *xhci,
1738 union xhci_trb *event)
1739{
1740 u32 slot_id;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001741 struct usb_device *udev;
Sarah Sharp623bef92011-11-11 14:57:33 -08001742
Xenia Ragiadakou7e76ad42013-09-09 21:03:10 +03001743 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3]));
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001744 if (!xhci->devs[slot_id]) {
Sarah Sharp623bef92011-11-11 14:57:33 -08001745 xhci_warn(xhci, "Device Notification event for "
1746 "unused slot %u\n", slot_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001747 return;
1748 }
1749
1750 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
1751 slot_id);
1752 udev = xhci->devs[slot_id]->udev;
1753 if (udev && udev->parent)
1754 usb_wakeup_notification(udev->parent, udev->portnum);
Sarah Sharp623bef92011-11-11 14:57:33 -08001755}
1756
Cherian, George11644a72018-11-09 17:21:22 +02001757/*
1758 * Quirk hanlder for errata seen on Cavium ThunderX2 processor XHCI
1759 * Controller.
1760 * As per ThunderX2errata-129 USB 2 device may come up as USB 1
1761 * If a connection to a USB 1 device is followed by another connection
1762 * to a USB 2 device.
1763 *
1764 * Reset the PHY after the USB device is disconnected if device speed
1765 * is less than HCD_USB3.
1766 * Retry the reset sequence max of 4 times checking the PLL lock status.
1767 *
1768 */
1769static void xhci_cavium_reset_phy_quirk(struct xhci_hcd *xhci)
1770{
1771 struct usb_hcd *hcd = xhci_to_hcd(xhci);
1772 u32 pll_lock_check;
1773 u32 retry_count = 4;
1774
1775 do {
1776 /* Assert PHY reset */
1777 writel(0x6F, hcd->regs + 0x1048);
1778 udelay(10);
1779 /* De-assert the PHY reset */
1780 writel(0x7F, hcd->regs + 0x1048);
1781 udelay(200);
1782 pll_lock_check = readl(hcd->regs + 0x1070);
1783 } while (!(pll_lock_check & 0x1) && --retry_count);
1784}
1785
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001786static void handle_port_status(struct xhci_hcd *xhci,
1787 union xhci_trb *event)
1788{
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001789 struct usb_hcd *hcd;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001790 u32 port_id;
Mathias Nyman76a0f322017-08-16 14:23:23 +03001791 u32 portsc, cmd_reg;
Sarah Sharp518e8482010-12-15 11:56:29 -08001792 int max_ports;
Andiry Xu56192532010-10-14 07:23:00 -07001793 int slot_id;
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001794 unsigned int hcd_portnum;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001795 struct xhci_bus_state *bus_state;
Sarah Sharp386139d2011-03-24 08:02:58 -07001796 bool bogus_port_status = false;
Mathias Nyman52c77552018-05-21 16:39:57 +03001797 struct xhci_port *port;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001798
1799 /* Port status change events always have a successful completion code */
Lu Baoluf4c8f032016-11-11 15:13:25 +02001800 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS)
1801 xhci_warn(xhci,
1802 "WARN: xHC returned failed port status event\n");
1803
Matt Evans28ccd292011-03-29 13:40:46 +11001804 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
Sarah Sharp518e8482010-12-15 11:56:29 -08001805 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
Mathias Nymand70d5a82019-04-26 16:23:30 +03001806
Sarah Sharp518e8482010-12-15 11:56:29 -08001807 if ((port_id <= 0) || (port_id > max_ports)) {
Mathias Nymand70d5a82019-04-26 16:23:30 +03001808 xhci_warn(xhci, "Port change event with invalid port ID %d\n",
1809 port_id);
Peter Chen09ce0c02013-03-20 09:30:00 +08001810 inc_deq(xhci, xhci->event_ring);
1811 return;
Andiry Xu56192532010-10-14 07:23:00 -07001812 }
1813
Mathias Nyman52c77552018-05-21 16:39:57 +03001814 port = &xhci->hw_ports[port_id - 1];
1815 if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) {
Mathias Nymand70d5a82019-04-26 16:23:30 +03001816 xhci_warn(xhci, "Port change event, no port for port ID %u\n",
1817 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001818 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001819 goto cleanup;
Sarah Sharp5308a912010-12-01 11:34:59 -08001820 }
1821
Mathias Nyman12453742018-11-09 17:21:18 +02001822 /* We might get interrupts after shared_hcd is removed */
1823 if (port->rhub == &xhci->usb3_rhub && xhci->shared_hcd == NULL) {
1824 xhci_dbg(xhci, "ignore port event for removed USB3 hcd\n");
1825 bogus_port_status = true;
1826 goto cleanup;
1827 }
1828
Mathias Nyman52c77552018-05-21 16:39:57 +03001829 hcd = port->rhub->hcd;
Mathias Nymanf6187f42018-12-07 16:19:30 +02001830 bus_state = &port->rhub->bus_state;
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001831 hcd_portnum = port->hcd_portnum;
Mathias Nyman52c77552018-05-21 16:39:57 +03001832 portsc = readl(port->addr);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001833
Mathias Nymand70d5a82019-04-26 16:23:30 +03001834 xhci_dbg(xhci, "Port change event, %d-%d, id %d, portsc: 0x%x\n",
1835 hcd->self.busnum, hcd_portnum + 1, port_id, portsc);
1836
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001837 trace_xhci_handle_port_status(hcd_portnum, portsc);
Mathias Nyman8ca13582017-08-16 14:23:24 +03001838
Sarah Sharp7111ebc2010-12-14 13:24:55 -08001839 if (hcd->state == HC_STATE_SUSPENDED) {
Andiry Xu56192532010-10-14 07:23:00 -07001840 xhci_dbg(xhci, "resume root hub\n");
1841 usb_hcd_resume_root_hub(hcd);
1842 }
1843
Mathias Nymanb8c3b712019-06-18 17:27:47 +03001844 if (hcd->speed >= HCD_USB3 &&
1845 (portsc & PORT_PLS_MASK) == XDEV_INACTIVE) {
1846 slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1);
1847 if (slot_id && xhci->devs[slot_id])
1848 xhci->devs[slot_id]->flags |= VDEV_PORT_ERROR;
Mathias Nymanb8c3b712019-06-18 17:27:47 +03001849 }
Zhuang Jin Canfac42712015-07-21 17:20:30 +03001850
Mathias Nyman76a0f322017-08-16 14:23:23 +03001851 if ((portsc & PORT_PLC) && (portsc & PORT_PLS_MASK) == XDEV_RESUME) {
Andiry Xu56192532010-10-14 07:23:00 -07001852 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1853
Mathias Nyman76a0f322017-08-16 14:23:23 +03001854 cmd_reg = readl(&xhci->op_regs->command);
1855 if (!(cmd_reg & CMD_RUN)) {
Andiry Xu56192532010-10-14 07:23:00 -07001856 xhci_warn(xhci, "xHC is not running.\n");
1857 goto cleanup;
1858 }
1859
Mathias Nyman76a0f322017-08-16 14:23:23 +03001860 if (DEV_SUPERSPEED_ANY(portsc)) {
Sarah Sharpd93814c2012-01-24 16:39:02 -08001861 xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001862 /* Set a flag to say the port signaled remote wakeup,
1863 * so we can tell the difference between the end of
1864 * device and host initiated resume.
1865 */
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001866 bus_state->port_remote_wakeup |= 1 << hcd_portnum;
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001867 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
Mathias Nyman057d4762019-12-11 16:20:03 +02001868 usb_hcd_start_port_resume(&hcd->self, hcd_portnum);
Mathias Nyman6b7f40f2018-05-21 16:39:59 +03001869 xhci_set_link_state(xhci, port, XDEV_U0);
Sarah Sharpd93814c2012-01-24 16:39:02 -08001870 /* Need to wait until the next link state change
1871 * indicates the device is actually in U0.
1872 */
1873 bogus_port_status = true;
1874 goto cleanup;
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001875 } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001876 xhci_dbg(xhci, "resume HS port %d\n", port_id);
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001877 bus_state->resume_done[hcd_portnum] = jiffies +
Felipe Balbib9e45182015-02-13 14:39:13 -06001878 msecs_to_jiffies(USB_RESUME_TIMEOUT);
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001879 set_bit(hcd_portnum, &bus_state->resuming_ports);
Anshuman Gupta0914ea62017-10-05 11:21:46 +03001880 /* Do the rest in GetPortStatus after resume time delay.
1881 * Avoid polling roothub status before that so that a
1882 * usb device auto-resume latency around ~40ms.
1883 */
1884 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Andiry Xu56192532010-10-14 07:23:00 -07001885 mod_timer(&hcd->rh_timer,
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001886 bus_state->resume_done[hcd_portnum]);
Anshuman Gupta330e2d62018-09-20 19:13:40 +03001887 usb_hcd_start_port_resume(&hcd->self, hcd_portnum);
Anshuman Gupta0914ea62017-10-05 11:21:46 +03001888 bogus_port_status = true;
Andiry Xu56192532010-10-14 07:23:00 -07001889 }
1890 }
1891
Mathias Nyman6cbcf592019-03-22 17:50:15 +02001892 if ((portsc & PORT_PLC) &&
1893 DEV_SUPERSPEED_ANY(portsc) &&
1894 ((portsc & PORT_PLS_MASK) == XDEV_U0 ||
1895 (portsc & PORT_PLS_MASK) == XDEV_U1 ||
1896 (portsc & PORT_PLS_MASK) == XDEV_U2)) {
Sarah Sharpd93814c2012-01-24 16:39:02 -08001897 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
Kai-Heng Feng0200b9f72020-03-12 16:45:15 +02001898 complete(&bus_state->u3exit_done[hcd_portnum]);
Mathias Nyman6cbcf592019-03-22 17:50:15 +02001899 /* We've just brought the device into U0/1/2 through either the
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001900 * Resume state after a device remote wakeup, or through the
1901 * U3Exit state after a host-initiated resume. If it's a device
1902 * initiated remote wake, don't pass up the link state change,
1903 * so the roothub behavior is consistent with external
1904 * USB 3.0 hub behavior.
1905 */
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001906 slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1);
Sarah Sharpd93814c2012-01-24 16:39:02 -08001907 if (slot_id && xhci->devs[slot_id])
1908 xhci_ring_device(xhci, slot_id);
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001909 if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) {
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001910 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001911 usb_wakeup_notification(hcd->self.root_hub,
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001912 hcd_portnum + 1);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001913 bogus_port_status = true;
1914 goto cleanup;
1915 }
Sarah Sharpd93814c2012-01-24 16:39:02 -08001916 }
1917
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001918 /*
1919 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or
1920 * RExit to a disconnect state). If so, let the the driver know it's
1921 * out of the RExit state.
1922 */
Aaron Ma958c0bd2018-11-09 17:21:20 +02001923 if (!DEV_SUPERSPEED_ANY(portsc) && hcd->speed < HCD_USB3 &&
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001924 test_and_clear_bit(hcd_portnum,
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001925 &bus_state->rexit_ports)) {
Mathias Nyman74e6ad52018-05-21 16:39:58 +03001926 complete(&bus_state->rexit_done[hcd_portnum]);
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001927 bogus_port_status = true;
1928 goto cleanup;
1929 }
1930
Cherian, George11644a72018-11-09 17:21:22 +02001931 if (hcd->speed < HCD_USB3) {
Mathias Nymaneaefcf22018-05-21 16:40:00 +03001932 xhci_test_and_clear_bit(xhci, port, PORT_PLC);
Cherian, George11644a72018-11-09 17:21:22 +02001933 if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) &&
1934 (portsc & PORT_CSC) && !(portsc & PORT_CONNECT))
1935 xhci_cavium_reset_phy_quirk(xhci);
1936 }
Andiry Xu6fd45622011-09-23 14:19:50 -07001937
Andiry Xu56192532010-10-14 07:23:00 -07001938cleanup:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001939 /* Update event ring dequeue pointer before dropping the lock */
Andiry Xu3b72fca2012-03-05 17:49:32 +08001940 inc_deq(xhci, xhci->event_ring);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001941
Sarah Sharp386139d2011-03-24 08:02:58 -07001942 /* Don't make the USB core poll the roothub if we got a bad port status
1943 * change event. Besides, at that point we can't tell which roothub
1944 * (USB 2.0 or USB 3.0) to kick.
1945 */
1946 if (bogus_port_status)
1947 return;
1948
Sarah Sharpc52804a2012-11-27 12:30:23 -08001949 /*
1950 * xHCI port-status-change events occur when the "or" of all the
1951 * status-change bits in the portsc register changes from 0 to 1.
1952 * New status changes won't cause an event if any other change
1953 * bits are still set. When an event occurs, switch over to
1954 * polling to avoid losing status changes.
1955 */
1956 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1957 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001958 spin_unlock(&xhci->lock);
1959 /* Pass this up to the core */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001960 usb_hcd_poll_rh_status(hcd);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001961 spin_lock(&xhci->lock);
1962}
1963
1964/*
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001965 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1966 * at end_trb, which may be in another segment. If the suspect DMA address is a
1967 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1968 * returns 0.
1969 */
Hans de Goedecffb9be2014-08-20 16:41:51 +03001970struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
1971 struct xhci_segment *start_seg,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001972 union xhci_trb *start_trb,
1973 union xhci_trb *end_trb,
Hans de Goedecffb9be2014-08-20 16:41:51 +03001974 dma_addr_t suspect_dma,
1975 bool debug)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001976{
1977 dma_addr_t start_dma;
1978 dma_addr_t end_seg_dma;
1979 dma_addr_t end_trb_dma;
1980 struct xhci_segment *cur_seg;
1981
Sarah Sharp23e3be12009-04-29 19:05:20 -07001982 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001983 cur_seg = start_seg;
1984
1985 do {
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001986 if (start_dma == 0)
Randy Dunlap326b4812010-04-19 08:53:50 -07001987 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -07001988 /* We may get an event for a Link TRB in the middle of a TD */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001989 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001990 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001991 /* If the end TRB isn't in this segment, this is set to 0 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001992 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001993
Hans de Goedecffb9be2014-08-20 16:41:51 +03001994 if (debug)
1995 xhci_warn(xhci,
1996 "Looking for event-dma %016llx trb-start %016llx trb-end %016llx seg-start %016llx seg-end %016llx\n",
1997 (unsigned long long)suspect_dma,
1998 (unsigned long long)start_dma,
1999 (unsigned long long)end_trb_dma,
2000 (unsigned long long)cur_seg->dma,
2001 (unsigned long long)end_seg_dma);
2002
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002003 if (end_trb_dma > 0) {
2004 /* The end TRB is in this segment, so suspect should be here */
2005 if (start_dma <= end_trb_dma) {
2006 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
2007 return cur_seg;
2008 } else {
2009 /* Case for one segment with
2010 * a TD wrapped around to the top
2011 */
2012 if ((suspect_dma >= start_dma &&
2013 suspect_dma <= end_seg_dma) ||
2014 (suspect_dma >= cur_seg->dma &&
2015 suspect_dma <= end_trb_dma))
2016 return cur_seg;
2017 }
Randy Dunlap326b4812010-04-19 08:53:50 -07002018 return NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002019 } else {
2020 /* Might still be somewhere in this segment */
2021 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
2022 return cur_seg;
2023 }
2024 cur_seg = cur_seg->next;
Sarah Sharp23e3be12009-04-29 19:05:20 -07002025 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
Sarah Sharp2fa88da2009-11-03 22:02:24 -08002026 } while (cur_seg != start_seg);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002027
Randy Dunlap326b4812010-04-19 08:53:50 -07002028 return NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002029}
2030
Jim Linef513be2019-06-03 18:53:44 +08002031static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci, struct xhci_td *td,
2032 struct xhci_virt_ep *ep)
2033{
2034 /*
2035 * As part of low/full-speed endpoint-halt processing
2036 * we must clear the TT buffer (USB 2.0 specification 11.17.5).
2037 */
2038 if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) &&
2039 (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) &&
2040 !(ep->ep_state & EP_CLEARING_TT)) {
2041 ep->ep_state |= EP_CLEARING_TT;
2042 td->urb->ep->hcpriv = td->urb->dev;
2043 if (usb_hub_clear_tt_buffer(td->urb))
2044 ep->ep_state &= ~EP_CLEARING_TT;
2045 }
2046}
2047
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08002048/* Check if an error has halted the endpoint ring. The class driver will
2049 * cleanup the halt for a non-default control endpoint if we indicate a stall.
2050 * However, a babble and other errors also halt the endpoint ring, and the class
2051 * driver won't clear the halt in that case, so we need to issue a Set Transfer
2052 * Ring Dequeue Pointer command manually.
2053 */
2054static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
2055 struct xhci_ep_ctx *ep_ctx,
2056 unsigned int trb_comp_code)
2057{
2058 /* TRB completion codes that may require a manual halt cleanup */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002059 if (trb_comp_code == COMP_USB_TRANSACTION_ERROR ||
2060 trb_comp_code == COMP_BABBLE_DETECTED_ERROR ||
2061 trb_comp_code == COMP_SPLIT_TRANSACTION_ERROR)
Rajesh Bhagatd4fc8bf2016-03-11 10:27:49 +05302062 /* The 0.95 spec says a babbling control endpoint
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08002063 * is not halted. The 0.96 spec says it is. Some HW
2064 * claims to be 0.95 compliant, but it halts the control
2065 * endpoint anyway. Check if a babble halted the
2066 * endpoint.
2067 */
Mathias Nyman5071e6b2016-11-11 15:13:28 +02002068 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_HALTED)
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08002069 return 1;
2070
2071 return 0;
2072}
2073
Sarah Sharpb45b5062009-12-09 15:59:06 -08002074int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
2075{
2076 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
2077 /* Vendor defined "informational" completion code,
2078 * treat as not-an-error.
2079 */
2080 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
2081 trb_comp_code);
2082 xhci_dbg(xhci, "Treating code as success.\n");
2083 return 1;
2084 }
2085 return 0;
2086}
2087
Andiry Xu4422da62010-07-22 15:22:55 -07002088static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002089 struct xhci_transfer_event *event, struct xhci_virt_ep *ep)
Andiry Xu4422da62010-07-22 15:22:55 -07002090{
Andiry Xu4422da62010-07-22 15:22:55 -07002091 struct xhci_ep_ctx *ep_ctx;
Felipe Balbibe0f50c2017-01-23 14:20:10 +02002092 struct xhci_ring *ep_ring;
Andiry Xu4422da62010-07-22 15:22:55 -07002093 u32 trb_comp_code;
2094
Matt Evans28ccd292011-03-29 13:40:46 +11002095 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Mathias Nymanab58f3b2021-01-29 15:00:18 +02002096 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11002097 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu4422da62010-07-22 15:22:55 -07002098
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002099 if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID ||
2100 trb_comp_code == COMP_STOPPED ||
2101 trb_comp_code == COMP_STOPPED_SHORT_PACKET) {
Andiry Xu4422da62010-07-22 15:22:55 -07002102 /* The Endpoint Stop Command completion will take care of any
2103 * stopped TDs. A stopped TD may be restarted, so don't update
2104 * the ring dequeue pointer or take this TD off any lists yet.
2105 */
Andiry Xu4422da62010-07-22 15:22:55 -07002106 return 0;
Mathias Nyman69defe02014-11-27 18:19:14 +02002107 }
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002108 if (trb_comp_code == COMP_STALL_ERROR ||
Mathias Nyman69defe02014-11-27 18:19:14 +02002109 xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
2110 trb_comp_code)) {
Mathias Nyman8f972502020-04-21 17:08:22 +03002111 /*
2112 * xhci internal endpoint state will go to a "halt" state for
2113 * any stall, including default control pipe protocol stall.
2114 * To clear the host side halt we need to issue a reset endpoint
2115 * command, followed by a set dequeue command to move past the
2116 * TD.
2117 * Class drivers clear the device side halt from a functional
2118 * stall later. Hub TT buffer should only be cleared for FS/LS
2119 * devices behind HS hubs for functional stalls.
Mathias Nyman69defe02014-11-27 18:19:14 +02002120 */
Mathias Nymanab58f3b2021-01-29 15:00:18 +02002121 if ((ep->ep_index != 0) || (trb_comp_code != COMP_STALL_ERROR))
Mathias Nyman8f972502020-04-21 17:08:22 +03002122 xhci_clear_hub_tt_buffer(xhci, td, ep);
Mathias Nyman7c6c3342021-01-29 15:00:37 +02002123
2124 xhci_handle_halted_endpoint(xhci, ep, ep_ring->stream_id, td,
Mathias Nyman674f8432021-01-29 15:00:38 +02002125 EP_HARD_RESET);
2126
2127 return 0; /* xhci_handle_halted_endpoint marked td cancelled */
Andiry Xu4422da62010-07-22 15:22:55 -07002128 } else {
Mathias Nyman69defe02014-11-27 18:19:14 +02002129 /* Update ring dequeue pointer */
Mathias Nyman55f61532021-01-29 15:00:28 +02002130 ep_ring->dequeue = td->last_trb;
2131 ep_ring->deq_seg = td->last_trb_seg;
2132 ep_ring->num_trbs_free += td->num_trbs - 1;
Mathias Nyman69defe02014-11-27 18:19:14 +02002133 inc_deq(xhci, ep_ring);
2134 }
Andiry Xu4422da62010-07-22 15:22:55 -07002135
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002136 return xhci_td_cleanup(xhci, td, ep_ring, td->status);
Andiry Xu4422da62010-07-22 15:22:55 -07002137}
2138
Mathias Nyman30a65b42016-11-11 15:13:17 +02002139/* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */
2140static int sum_trb_lengths(struct xhci_hcd *xhci, struct xhci_ring *ring,
2141 union xhci_trb *stop_trb)
2142{
2143 u32 sum;
2144 union xhci_trb *trb = ring->dequeue;
2145 struct xhci_segment *seg = ring->deq_seg;
2146
2147 for (sum = 0; trb != stop_trb; next_trb(xhci, ring, &seg, &trb)) {
2148 if (!trb_is_noop(trb) && !trb_is_link(trb))
2149 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2]));
2150 }
2151 return sum;
2152}
2153
Andiry Xu4422da62010-07-22 15:22:55 -07002154/*
Andiry Xu8af56be2010-07-22 15:23:03 -07002155 * Process control tds, update urb status and actual_length.
2156 */
2157static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002158 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002159 struct xhci_virt_ep *ep)
Andiry Xu8af56be2010-07-22 15:23:03 -07002160{
Andiry Xu8af56be2010-07-22 15:23:03 -07002161 struct xhci_ep_ctx *ep_ctx;
2162 u32 trb_comp_code;
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002163 u32 remaining, requested;
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002164 u32 trb_type;
Andiry Xu8af56be2010-07-22 15:23:03 -07002165
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002166 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3]));
Mathias Nymanab58f3b2021-01-29 15:00:18 +02002167 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11002168 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002169 requested = td->urb->transfer_buffer_length;
2170 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2171
Andiry Xu8af56be2010-07-22 15:23:03 -07002172 switch (trb_comp_code) {
2173 case COMP_SUCCESS:
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002174 if (trb_type != TRB_STATUS) {
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002175 xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n",
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002176 (trb_type == TRB_DATA) ? "data" : "setup");
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002177 td->status = -ESHUTDOWN;
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002178 break;
Andiry Xu8af56be2010-07-22 15:23:03 -07002179 }
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002180 td->status = 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07002181 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002182 case COMP_SHORT_PACKET:
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002183 td->status = 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07002184 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002185 case COMP_STOPPED_SHORT_PACKET:
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002186 if (trb_type == TRB_DATA || trb_type == TRB_NORMAL)
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002187 td->urb->actual_length = remaining;
Lu Baolu40a3b772015-08-06 19:24:01 +03002188 else
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002189 xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n");
2190 goto finish_td;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002191 case COMP_STOPPED:
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002192 switch (trb_type) {
2193 case TRB_SETUP:
2194 td->urb->actual_length = 0;
2195 goto finish_td;
2196 case TRB_DATA:
2197 case TRB_NORMAL:
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002198 td->urb->actual_length = requested - remaining;
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002199 goto finish_td;
Mathias Nyman0ab28812017-03-28 15:55:29 +03002200 case TRB_STATUS:
2201 td->urb->actual_length = requested;
2202 goto finish_td;
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002203 default:
2204 xhci_warn(xhci, "WARN: unexpected TRB Type %d\n",
2205 trb_type);
2206 goto finish_td;
2207 }
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002208 case COMP_STOPPED_LENGTH_INVALID:
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002209 goto finish_td;
Andiry Xu8af56be2010-07-22 15:23:03 -07002210 default:
2211 if (!xhci_requires_manual_halt_cleanup(xhci,
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002212 ep_ctx, trb_comp_code))
Andiry Xu8af56be2010-07-22 15:23:03 -07002213 break;
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002214 xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n",
Mathias Nymanab58f3b2021-01-29 15:00:18 +02002215 trb_comp_code, ep->ep_index);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002216 fallthrough;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002217 case COMP_STALL_ERROR:
Andiry Xu8af56be2010-07-22 15:23:03 -07002218 /* Did we transfer part of the data (middle) phase? */
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002219 if (trb_type == TRB_DATA || trb_type == TRB_NORMAL)
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002220 td->urb->actual_length = requested - remaining;
Mathias Nyman22ae47e2015-05-29 17:01:53 +03002221 else if (!td->urb_length_set)
Andiry Xu8af56be2010-07-22 15:23:03 -07002222 td->urb->actual_length = 0;
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002223 goto finish_td;
Andiry Xu8af56be2010-07-22 15:23:03 -07002224 }
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002225
2226 /* stopped at setup stage, no data transferred */
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002227 if (trb_type == TRB_SETUP)
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002228 goto finish_td;
2229
Andiry Xu8af56be2010-07-22 15:23:03 -07002230 /*
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002231 * if on data stage then update the actual_length of the URB and flag it
2232 * as set, so it won't be overwritten in the event for the last TRB.
Andiry Xu8af56be2010-07-22 15:23:03 -07002233 */
Felipe Balbi29fc1aa2017-01-03 18:28:53 +02002234 if (trb_type == TRB_DATA ||
2235 trb_type == TRB_NORMAL) {
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002236 td->urb_length_set = true;
2237 td->urb->actual_length = requested - remaining;
2238 xhci_dbg(xhci, "Waiting for status stage event\n");
2239 return 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07002240 }
2241
Mathias Nyman0b6c3242016-11-11 15:13:16 +02002242 /* at status stage */
2243 if (!td->urb_length_set)
2244 td->urb->actual_length = requested;
2245
2246finish_td:
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002247 return finish_td(xhci, td, event, ep);
Andiry Xu8af56be2010-07-22 15:23:03 -07002248}
2249
2250/*
Andiry Xu04e51902010-07-22 15:23:39 -07002251 * Process isochronous tds, update urb packet status and actual_length.
2252 */
2253static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002254 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002255 struct xhci_virt_ep *ep)
Andiry Xu04e51902010-07-22 15:23:39 -07002256{
Andiry Xu04e51902010-07-22 15:23:39 -07002257 struct urb_priv *urb_priv;
2258 int idx;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002259 struct usb_iso_packet_descriptor *frame;
Andiry Xu04e51902010-07-22 15:23:39 -07002260 u32 trb_comp_code;
Mathias Nyman36da3a12016-11-11 15:13:19 +02002261 bool sum_trbs_for_length = false;
2262 u32 remaining, requested, ep_trb_len;
2263 int short_framestatus;
Andiry Xu04e51902010-07-22 15:23:39 -07002264
Matt Evans28ccd292011-03-29 13:40:46 +11002265 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07002266 urb_priv = td->urb->hcpriv;
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02002267 idx = urb_priv->num_tds_done;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002268 frame = &td->urb->iso_frame_desc[idx];
Mathias Nyman36da3a12016-11-11 15:13:19 +02002269 requested = frame->length;
2270 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2271 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
2272 short_framestatus = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
2273 -EREMOTEIO : 0;
Andiry Xu04e51902010-07-22 15:23:39 -07002274
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002275 /* handle completion code */
2276 switch (trb_comp_code) {
2277 case COMP_SUCCESS:
Mathias Nyman36da3a12016-11-11 15:13:19 +02002278 if (remaining) {
2279 frame->status = short_framestatus;
2280 if (xhci->quirks & XHCI_TRUST_TX_LENGTH)
2281 sum_trbs_for_length = true;
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002282 break;
2283 }
Mathias Nyman36da3a12016-11-11 15:13:19 +02002284 frame->status = 0;
2285 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002286 case COMP_SHORT_PACKET:
Mathias Nyman36da3a12016-11-11 15:13:19 +02002287 frame->status = short_framestatus;
2288 sum_trbs_for_length = true;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002289 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002290 case COMP_BANDWIDTH_OVERRUN_ERROR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002291 frame->status = -ECOMM;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002292 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002293 case COMP_ISOCH_BUFFER_OVERRUN:
2294 case COMP_BABBLE_DETECTED_ERROR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002295 frame->status = -EOVERFLOW;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002296 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002297 case COMP_INCOMPATIBLE_DEVICE_ERROR:
2298 case COMP_STALL_ERROR:
Mathias Nymand104d012015-04-30 17:16:02 +03002299 frame->status = -EPROTO;
Mathias Nymand104d012015-04-30 17:16:02 +03002300 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002301 case COMP_USB_TRANSACTION_ERROR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002302 frame->status = -EPROTO;
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002303 if (ep_trb != td->last_trb)
Mathias Nymand104d012015-04-30 17:16:02 +03002304 return 0;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002305 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002306 case COMP_STOPPED:
Mathias Nyman36da3a12016-11-11 15:13:19 +02002307 sum_trbs_for_length = true;
2308 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002309 case COMP_STOPPED_SHORT_PACKET:
Mathias Nyman36da3a12016-11-11 15:13:19 +02002310 /* field normally containing residue now contains tranferred */
2311 frame->status = short_framestatus;
2312 requested = remaining;
2313 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002314 case COMP_STOPPED_LENGTH_INVALID:
Mathias Nyman36da3a12016-11-11 15:13:19 +02002315 requested = 0;
2316 remaining = 0;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002317 break;
2318 default:
Mathias Nyman36da3a12016-11-11 15:13:19 +02002319 sum_trbs_for_length = true;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002320 frame->status = -1;
2321 break;
Andiry Xu04e51902010-07-22 15:23:39 -07002322 }
2323
Mathias Nyman36da3a12016-11-11 15:13:19 +02002324 if (sum_trbs_for_length)
Mathias Nymand4dff8042021-01-29 15:00:19 +02002325 frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) +
Mathias Nyman36da3a12016-11-11 15:13:19 +02002326 ep_trb_len - remaining;
2327 else
2328 frame->actual_length = requested;
Andiry Xu04e51902010-07-22 15:23:39 -07002329
Mathias Nyman36da3a12016-11-11 15:13:19 +02002330 td->urb->actual_length += frame->actual_length;
Andiry Xu04e51902010-07-22 15:23:39 -07002331
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002332 return finish_td(xhci, td, event, ep);
Andiry Xu04e51902010-07-22 15:23:39 -07002333}
2334
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002335static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002336 struct xhci_virt_ep *ep, int status)
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002337{
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002338 struct urb_priv *urb_priv;
2339 struct usb_iso_packet_descriptor *frame;
2340 int idx;
2341
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002342 urb_priv = td->urb->hcpriv;
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02002343 idx = urb_priv->num_tds_done;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002344 frame = &td->urb->iso_frame_desc[idx];
2345
Sarah Sharpb3df3f92011-06-15 19:57:46 -07002346 /* The transfer is partly done. */
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002347 frame->status = -EXDEV;
2348
2349 /* calc actual length */
2350 frame->actual_length = 0;
2351
2352 /* Update ring dequeue pointer */
Mathias Nyman55f61532021-01-29 15:00:28 +02002353 ep->ring->dequeue = td->last_trb;
2354 ep->ring->deq_seg = td->last_trb_seg;
2355 ep->ring->num_trbs_free += td->num_trbs - 1;
Mathias Nymand4dff8042021-01-29 15:00:19 +02002356 inc_deq(xhci, ep->ring);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002357
Mathias Nymand4dff8042021-01-29 15:00:19 +02002358 return xhci_td_cleanup(xhci, td, ep->ring, status);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002359}
2360
Andiry Xu04e51902010-07-22 15:23:39 -07002361/*
Andiry Xu22405ed2010-07-22 15:23:08 -07002362 * Process bulk and interrupt tds, update urb status and actual_length.
2363 */
2364static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002365 union xhci_trb *ep_trb, struct xhci_transfer_event *event,
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002366 struct xhci_virt_ep *ep)
Andiry Xu22405ed2010-07-22 15:23:08 -07002367{
Mathias Nymanf8f80be2018-09-20 19:13:37 +03002368 struct xhci_slot_ctx *slot_ctx;
Andiry Xu22405ed2010-07-22 15:23:08 -07002369 struct xhci_ring *ep_ring;
Andiry Xu22405ed2010-07-22 15:23:08 -07002370 u32 trb_comp_code;
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002371 u32 remaining, requested, ep_trb_len;
Andiry Xu22405ed2010-07-22 15:23:08 -07002372
Mathias Nymanab58f3b2021-01-29 15:00:18 +02002373 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002374 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
2375 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Mathias Nyman30a65b42016-11-11 15:13:17 +02002376 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002377 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
Mathias Nyman30a65b42016-11-11 15:13:17 +02002378 requested = td->urb->transfer_buffer_length;
Andiry Xu22405ed2010-07-22 15:23:08 -07002379
2380 switch (trb_comp_code) {
2381 case COMP_SUCCESS:
Mathias Nymanf8f80be2018-09-20 19:13:37 +03002382 ep_ring->err_count = 0;
Mathias Nyman30a65b42016-11-11 15:13:17 +02002383 /* handle success with untransferred data as short packet */
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002384 if (ep_trb != td->last_trb || remaining) {
Mathias Nyman52ab8682016-11-11 15:13:15 +02002385 xhci_warn(xhci, "WARN Successful completion on short TX\n");
Mathias Nyman30a65b42016-11-11 15:13:17 +02002386 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n",
2387 td->urb->ep->desc.bEndpointAddress,
2388 requested, remaining);
Andiry Xu22405ed2010-07-22 15:23:08 -07002389 }
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002390 td->status = 0;
Andiry Xu22405ed2010-07-22 15:23:08 -07002391 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002392 case COMP_SHORT_PACKET:
Mathias Nyman30a65b42016-11-11 15:13:17 +02002393 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n",
2394 td->urb->ep->desc.bEndpointAddress,
2395 requested, remaining);
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002396 td->status = 0;
Mathias Nyman30a65b42016-11-11 15:13:17 +02002397 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002398 case COMP_STOPPED_SHORT_PACKET:
Mathias Nyman30a65b42016-11-11 15:13:17 +02002399 td->urb->actual_length = remaining;
2400 goto finish_td;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002401 case COMP_STOPPED_LENGTH_INVALID:
Mathias Nyman30a65b42016-11-11 15:13:17 +02002402 /* stopped on ep trb with invalid length, exclude it */
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002403 ep_trb_len = 0;
Mathias Nyman30a65b42016-11-11 15:13:17 +02002404 remaining = 0;
Andiry Xu22405ed2010-07-22 15:23:08 -07002405 break;
Mathias Nymanf8f80be2018-09-20 19:13:37 +03002406 case COMP_USB_TRANSACTION_ERROR:
2407 if ((ep_ring->err_count++ > MAX_SOFT_RETRY) ||
2408 le32_to_cpu(slot_ctx->tt_info) & TT_SLOT)
2409 break;
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002410
2411 td->status = 0;
Mathias Nyman7c6c3342021-01-29 15:00:37 +02002412
2413 xhci_handle_halted_endpoint(xhci, ep, ep_ring->stream_id, td,
2414 EP_SOFT_RESET);
Mathias Nymanf8f80be2018-09-20 19:13:37 +03002415 return 0;
Andiry Xu22405ed2010-07-22 15:23:08 -07002416 default:
Mathias Nyman30a65b42016-11-11 15:13:17 +02002417 /* do nothing */
Andiry Xu22405ed2010-07-22 15:23:08 -07002418 break;
2419 }
Mathias Nyman30a65b42016-11-11 15:13:17 +02002420
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002421 if (ep_trb == td->last_trb)
Mathias Nyman30a65b42016-11-11 15:13:17 +02002422 td->urb->actual_length = requested - remaining;
2423 else
Lu Baolu40a3b772015-08-06 19:24:01 +03002424 td->urb->actual_length =
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002425 sum_trb_lengths(xhci, ep_ring, ep_trb) +
2426 ep_trb_len - remaining;
Mathias Nyman30a65b42016-11-11 15:13:17 +02002427finish_td:
2428 if (remaining > requested) {
2429 xhci_warn(xhci, "bad transfer trb length %d in event trb\n",
2430 remaining);
Andiry Xu22405ed2010-07-22 15:23:08 -07002431 td->urb->actual_length = 0;
Andiry Xu22405ed2010-07-22 15:23:08 -07002432 }
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002433 return finish_td(xhci, td, event, ep);
Andiry Xu22405ed2010-07-22 15:23:08 -07002434}
2435
2436/*
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002437 * If this function returns an error condition, it means it got a Transfer
2438 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
2439 * At this point, the host controller is probably hosed and should be reset.
2440 */
2441static int handle_tx_event(struct xhci_hcd *xhci,
2442 struct xhci_transfer_event *event)
2443{
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002444 struct xhci_virt_ep *ep;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002445 struct xhci_ring *ep_ring;
Sarah Sharp82d10092009-08-07 14:04:52 -07002446 unsigned int slot_id;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002447 int ep_index;
Randy Dunlap326b4812010-04-19 08:53:50 -07002448 struct xhci_td *td = NULL;
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002449 dma_addr_t ep_trb_dma;
2450 struct xhci_segment *ep_seg;
2451 union xhci_trb *ep_trb;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002452 int status = -EINPROGRESS;
John Yound115b042009-07-27 12:05:15 -07002453 struct xhci_ep_ctx *ep_ctx;
Andiry Xuc2d7b492011-09-19 16:05:12 -07002454 struct list_head *tmp;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002455 u32 trb_comp_code;
Andiry Xuc2d7b492011-09-19 16:05:12 -07002456 int td_num = 0;
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002457 bool handling_skipped_tds = false;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002458
Matt Evans28ccd292011-03-29 13:40:46 +11002459 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Mathias Nymanb3368382017-06-15 11:55:43 +03002460 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
2461 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
2462 ep_trb_dma = le64_to_cpu(event->buffer);
2463
Mathias Nymanb1adc422021-01-29 15:00:22 +02002464 ep = xhci_get_virt_ep(xhci, slot_id, ep_index);
2465 if (!ep) {
2466 xhci_err(xhci, "ERROR Invalid Transfer event\n");
Mathias Nymanb3368382017-06-15 11:55:43 +03002467 goto err_out;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002468 }
2469
Mathias Nymanb3368382017-06-15 11:55:43 +03002470 ep_ring = xhci_dma_to_transfer_ring(ep, ep_trb_dma);
Mathias Nymanb1adc422021-01-29 15:00:22 +02002471 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index);
Mathias Nymanb3368382017-06-15 11:55:43 +03002472
Mathias Nymanade2e3a2017-06-15 11:55:46 +03002473 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) {
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002474 xhci_err(xhci,
Mathias Nymanade2e3a2017-06-15 11:55:46 +03002475 "ERROR Transfer event for disabled endpoint slot %u ep %u\n",
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002476 slot_id, ep_index);
Mathias Nymanb3368382017-06-15 11:55:43 +03002477 goto err_out;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002478 }
2479
Mathias Nymanade2e3a2017-06-15 11:55:46 +03002480 /* Some transfer events don't always point to a trb, see xhci 4.17.4 */
2481 if (!ep_ring) {
2482 switch (trb_comp_code) {
2483 case COMP_STALL_ERROR:
2484 case COMP_USB_TRANSACTION_ERROR:
2485 case COMP_INVALID_STREAM_TYPE_ERROR:
2486 case COMP_INVALID_STREAM_ID_ERROR:
Mathias Nyman7c6c3342021-01-29 15:00:37 +02002487 xhci_handle_halted_endpoint(xhci, ep, 0, NULL,
2488 EP_SOFT_RESET);
Mathias Nymanade2e3a2017-06-15 11:55:46 +03002489 goto cleanup;
2490 case COMP_RING_UNDERRUN:
2491 case COMP_RING_OVERRUN:
Sandeep Singhd9193ef2018-11-09 17:21:19 +02002492 case COMP_STOPPED_LENGTH_INVALID:
Mathias Nymanade2e3a2017-06-15 11:55:46 +03002493 goto cleanup;
2494 default:
2495 xhci_err(xhci, "ERROR Transfer event for unknown stream ring slot %u ep %u\n",
2496 slot_id, ep_index);
2497 goto err_out;
2498 }
2499 }
2500
Andiry Xuc2d7b492011-09-19 16:05:12 -07002501 /* Count current td numbers if ep->skip is set */
2502 if (ep->skip) {
2503 list_for_each(tmp, &ep_ring->td_list)
2504 td_num++;
2505 }
2506
Andiry Xu986a92d2010-07-22 15:23:20 -07002507 /* Look for common error cases */
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002508 switch (trb_comp_code) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002509 /* Skip codes that require special handling depending on
2510 * transfer type
2511 */
2512 case COMP_SUCCESS:
Vivek Gautam1c11a172013-03-21 12:06:48 +05302513 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0)
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002514 break;
Mathias Nyman7ff11162019-12-11 16:20:06 +02002515 if (xhci->quirks & XHCI_TRUST_TX_LENGTH ||
2516 ep_ring->last_td_was_short)
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002517 trb_comp_code = COMP_SHORT_PACKET;
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002518 else
Sarah Sharp8202ce22012-07-25 10:52:45 -07002519 xhci_warn_ratelimited(xhci,
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002520 "WARN Successful completion on short TX for slot %u ep %u: needs XHCI_TRUST_TX_LENGTH quirk?\n",
2521 slot_id, ep_index);
Nick Desaulniers1d6903a2020-11-10 17:47:14 -08002522 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002523 case COMP_SHORT_PACKET:
Sarah Sharpb10de142009-04-27 19:58:50 -07002524 break;
Mathias Nymanb3368382017-06-15 11:55:43 +03002525 /* Completion codes for endpoint stopped state */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002526 case COMP_STOPPED:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002527 xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n",
2528 slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07002529 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002530 case COMP_STOPPED_LENGTH_INVALID:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002531 xhci_dbg(xhci,
2532 "Stopped on No-op or Link TRB for slot %u ep %u\n",
2533 slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07002534 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002535 case COMP_STOPPED_SHORT_PACKET:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002536 xhci_dbg(xhci,
2537 "Stopped with short packet transfer detected for slot %u ep %u\n",
2538 slot_id, ep_index);
Lu Baolu40a3b772015-08-06 19:24:01 +03002539 break;
Mathias Nymanb3368382017-06-15 11:55:43 +03002540 /* Completion codes for endpoint halted state */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002541 case COMP_STALL_ERROR:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002542 xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id,
2543 ep_index);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002544 ep->ep_state |= EP_HALTED;
Sarah Sharpb10de142009-04-27 19:58:50 -07002545 status = -EPIPE;
2546 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002547 case COMP_SPLIT_TRANSACTION_ERROR:
Mathias Nyman76eac5d2020-03-12 16:45:10 +02002548 xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n",
2549 slot_id, ep_index);
2550 status = -EPROTO;
2551 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002552 case COMP_USB_TRANSACTION_ERROR:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002553 xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n",
2554 slot_id, ep_index);
Sarah Sharpb10de142009-04-27 19:58:50 -07002555 status = -EPROTO;
2556 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002557 case COMP_BABBLE_DETECTED_ERROR:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002558 xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n",
2559 slot_id, ep_index);
Sarah Sharp4a731432009-07-27 12:04:32 -07002560 status = -EOVERFLOW;
2561 break;
Mathias Nymanb3368382017-06-15 11:55:43 +03002562 /* Completion codes for endpoint error state */
2563 case COMP_TRB_ERROR:
2564 xhci_warn(xhci,
2565 "WARN: TRB error for slot %u ep %u on endpoint\n",
2566 slot_id, ep_index);
2567 status = -EILSEQ;
2568 break;
2569 /* completion codes not indicating endpoint state change */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002570 case COMP_DATA_BUFFER_ERROR:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002571 xhci_warn(xhci,
2572 "WARN: HC couldn't access mem fast enough for slot %u ep %u\n",
2573 slot_id, ep_index);
Sarah Sharpb10de142009-04-27 19:58:50 -07002574 status = -ENOSR;
2575 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002576 case COMP_BANDWIDTH_OVERRUN_ERROR:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002577 xhci_warn(xhci,
2578 "WARN: bandwidth overrun event for slot %u ep %u on endpoint\n",
2579 slot_id, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002580 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002581 case COMP_ISOCH_BUFFER_OVERRUN:
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002582 xhci_warn(xhci,
2583 "WARN: buffer overrun event for slot %u ep %u on endpoint",
2584 slot_id, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002585 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002586 case COMP_RING_UNDERRUN:
Andiry Xu986a92d2010-07-22 15:23:20 -07002587 /*
2588 * When the Isoch ring is empty, the xHC will generate
2589 * a Ring Overrun Event for IN Isoch endpoint or Ring
2590 * Underrun Event for OUT Isoch endpoint.
2591 */
2592 xhci_dbg(xhci, "underrun event on endpoint\n");
2593 if (!list_empty(&ep_ring->td_list))
2594 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2595 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002596 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2597 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002598 goto cleanup;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002599 case COMP_RING_OVERRUN:
Andiry Xu986a92d2010-07-22 15:23:20 -07002600 xhci_dbg(xhci, "overrun event on endpoint\n");
2601 if (!list_empty(&ep_ring->td_list))
2602 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2603 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002604 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2605 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002606 goto cleanup;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002607 case COMP_MISSED_SERVICE_ERROR:
Andiry Xud18240d2010-07-22 15:23:25 -07002608 /*
2609 * When encounter missed service error, one or more isoc tds
2610 * may be missed by xHC.
2611 * Set skip flag of the ep_ring; Complete the missed tds as
2612 * short transfer when process the ep_ring next time.
2613 */
2614 ep->skip = true;
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002615 xhci_dbg(xhci,
2616 "Miss service interval error for slot %u ep %u, set skip flag\n",
2617 slot_id, ep_index);
Andiry Xud18240d2010-07-22 15:23:25 -07002618 goto cleanup;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002619 case COMP_NO_PING_RESPONSE_ERROR:
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002620 ep->skip = true;
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002621 xhci_dbg(xhci,
2622 "No Ping response error for slot %u ep %u, Skip one Isoc TD\n",
2623 slot_id, ep_index);
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002624 goto cleanup;
Mathias Nymanb3368382017-06-15 11:55:43 +03002625
2626 case COMP_INCOMPATIBLE_DEVICE_ERROR:
2627 /* needs disable slot command to recover */
2628 xhci_warn(xhci,
2629 "WARN: detect an incompatible device for slot %u ep %u",
2630 slot_id, ep_index);
2631 status = -EPROTO;
2632 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002633 default:
Sarah Sharpb45b5062009-12-09 15:59:06 -08002634 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
Sarah Sharp5ad6a522009-11-11 10:28:40 -08002635 status = 0;
2636 break;
2637 }
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002638 xhci_warn(xhci,
2639 "ERROR Unknown event condition %u for slot %u ep %u , HC probably busted\n",
2640 trb_comp_code, slot_id, ep_index);
Sarah Sharpb10de142009-04-27 19:58:50 -07002641 goto cleanup;
2642 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002643
Andiry Xud18240d2010-07-22 15:23:25 -07002644 do {
2645 /* This TRB should be in the TD at the head of this ring's
2646 * TD list.
2647 */
2648 if (list_empty(&ep_ring->td_list)) {
Sarah Sharpa83d6752013-03-18 10:19:51 -07002649 /*
Mathias Nymane4ec40e2017-12-01 13:41:19 +02002650 * Don't print wanings if it's due to a stopped endpoint
2651 * generating an extra completion event if the device
2652 * was suspended. Or, a event for the last TRB of a
2653 * short TD we already got a short event for.
2654 * The short TD is already removed from the TD list.
Sarah Sharpa83d6752013-03-18 10:19:51 -07002655 */
Mathias Nymane4ec40e2017-12-01 13:41:19 +02002656
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002657 if (!(trb_comp_code == COMP_STOPPED ||
Mathias Nymane4ec40e2017-12-01 13:41:19 +02002658 trb_comp_code == COMP_STOPPED_LENGTH_INVALID ||
2659 ep_ring->last_td_was_short)) {
Sarah Sharpa83d6752013-03-18 10:19:51 -07002660 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
2661 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2662 ep_index);
Sarah Sharpa83d6752013-03-18 10:19:51 -07002663 }
Andiry Xud18240d2010-07-22 15:23:25 -07002664 if (ep->skip) {
2665 ep->skip = false;
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002666 xhci_dbg(xhci, "td_list is empty while skip flag set. Clear skip flag for slot %u ep %u.\n",
2667 slot_id, ep_index);
Andiry Xud18240d2010-07-22 15:23:25 -07002668 }
Mathias Nyman93ceaa82020-04-21 17:08:20 +03002669 if (trb_comp_code == COMP_STALL_ERROR ||
2670 xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
2671 trb_comp_code)) {
Mathias Nyman7c6c3342021-01-29 15:00:37 +02002672 xhci_handle_halted_endpoint(xhci, ep,
2673 ep_ring->stream_id,
2674 NULL,
2675 EP_HARD_RESET);
Mathias Nyman93ceaa82020-04-21 17:08:20 +03002676 }
Andiry Xud18240d2010-07-22 15:23:25 -07002677 goto cleanup;
2678 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002679
Andiry Xuc2d7b492011-09-19 16:05:12 -07002680 /* We've skipped all the TDs on the ep ring when ep->skip set */
2681 if (ep->skip && td_num == 0) {
2682 ep->skip = false;
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002683 xhci_dbg(xhci, "All tds on the ep_ring skipped. Clear skip flag for slot %u ep %u.\n",
2684 slot_id, ep_index);
Andiry Xuc2d7b492011-09-19 16:05:12 -07002685 goto cleanup;
2686 }
2687
Felipe Balbi04861f82017-01-23 14:20:09 +02002688 td = list_first_entry(&ep_ring->td_list, struct xhci_td,
2689 td_list);
Andiry Xuc2d7b492011-09-19 16:05:12 -07002690 if (ep->skip)
2691 td_num--;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002692
Andiry Xud18240d2010-07-22 15:23:25 -07002693 /* Is this a TRB in the currently executing TD? */
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002694 ep_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue,
2695 td->last_trb, ep_trb_dma, false);
Alex Hee1cf4862011-06-03 15:58:25 +08002696
2697 /*
2698 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
2699 * is not in the current TD pointed by ep_ring->dequeue because
2700 * that the hardware dequeue pointer still at the previous TRB
2701 * of the current TD. The previous TRB maybe a Link TD or the
2702 * last TRB of the previous TD. The command completion handle
2703 * will take care the rest.
2704 */
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002705 if (!ep_seg && (trb_comp_code == COMP_STOPPED ||
2706 trb_comp_code == COMP_STOPPED_LENGTH_INVALID)) {
Alex Hee1cf4862011-06-03 15:58:25 +08002707 goto cleanup;
2708 }
2709
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002710 if (!ep_seg) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002711 if (!ep->skip ||
2712 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
Sarah Sharpad808332011-05-25 10:43:56 -07002713 /* Some host controllers give a spurious
2714 * successful event after a short transfer.
2715 * Ignore it.
2716 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002717 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
Sarah Sharpad808332011-05-25 10:43:56 -07002718 ep_ring->last_td_was_short) {
2719 ep_ring->last_td_was_short = false;
Sarah Sharpad808332011-05-25 10:43:56 -07002720 goto cleanup;
2721 }
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002722 /* HC is busted, give up! */
2723 xhci_err(xhci,
2724 "ERROR Transfer event TRB DMA ptr not "
Hans de Goedecffb9be2014-08-20 16:41:51 +03002725 "part of current TD ep_index %d "
2726 "comp_code %u\n", ep_index,
2727 trb_comp_code);
2728 trb_in_td(xhci, ep_ring->deq_seg,
2729 ep_ring->dequeue, td->last_trb,
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002730 ep_trb_dma, true);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002731 return -ESHUTDOWN;
2732 }
2733
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002734 skip_isoc_td(xhci, td, ep, status);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002735 goto cleanup;
2736 }
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002737 if (trb_comp_code == COMP_SHORT_PACKET)
Sarah Sharpad808332011-05-25 10:43:56 -07002738 ep_ring->last_td_was_short = true;
2739 else
2740 ep_ring->last_td_was_short = false;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002741
2742 if (ep->skip) {
Zhengjun Xingb7f769a2017-04-07 17:56:59 +03002743 xhci_dbg(xhci,
2744 "Found td. Clear skip flag for slot %u ep %u.\n",
2745 slot_id, ep_index);
Andiry Xud18240d2010-07-22 15:23:25 -07002746 ep->skip = false;
2747 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002748
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002749 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) /
2750 sizeof(*ep_trb)];
Felipe Balbia37c3f72017-01-23 14:20:19 +02002751
2752 trace_xhci_handle_transfer(ep_ring,
2753 (struct xhci_generic_trb *) ep_trb);
2754
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002755 /*
Lu Baolu810a6242017-10-06 17:45:29 +03002756 * No-op TRB could trigger interrupts in a case where
2757 * a URB was killed and a STALL_ERROR happens right
2758 * after the endpoint ring stopped. Reset the halted
2759 * endpoint. Otherwise, the endpoint remains stalled
2760 * indefinitely.
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002761 */
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002762
Mathias Nymanf97c08a2016-11-11 15:13:18 +02002763 if (trb_is_noop(ep_trb)) {
Lu Baolu810a6242017-10-06 17:45:29 +03002764 if (trb_comp_code == COMP_STALL_ERROR ||
2765 xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
2766 trb_comp_code))
Mathias Nyman7c6c3342021-01-29 15:00:37 +02002767 xhci_handle_halted_endpoint(xhci, ep,
2768 ep_ring->stream_id,
2769 td, EP_HARD_RESET);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002770 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07002771 }
2772
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002773 td->status = status;
2774
Mathias Nyman0c03d892016-11-11 15:13:23 +02002775 /* update the urb's actual_length and give back to the core */
Andiry Xud18240d2010-07-22 15:23:25 -07002776 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002777 process_ctrl_td(xhci, td, ep_trb, event, ep);
Andiry Xu04e51902010-07-22 15:23:39 -07002778 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002779 process_isoc_td(xhci, td, ep_trb, event, ep);
Andiry Xud18240d2010-07-22 15:23:25 -07002780 else
Mathias Nymana6ccd1f2021-01-29 15:00:35 +02002781 process_bulk_intr_td(xhci, td, ep_trb, event, ep);
Andiry Xu4422da62010-07-22 15:22:55 -07002782cleanup:
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002783 handling_skipped_tds = ep->skip &&
Felipe Balbi0b7c1052017-01-23 14:20:06 +02002784 trb_comp_code != COMP_MISSED_SERVICE_ERROR &&
2785 trb_comp_code != COMP_NO_PING_RESPONSE_ERROR;
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002786
Andiry Xud18240d2010-07-22 15:23:25 -07002787 /*
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002788 * Do not update event ring dequeue pointer if we're in a loop
2789 * processing missed tds.
Sarah Sharp82d10092009-08-07 14:04:52 -07002790 */
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002791 if (!handling_skipped_tds)
Andiry Xu3b72fca2012-03-05 17:49:32 +08002792 inc_deq(xhci, xhci->event_ring);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002793
Andiry Xud18240d2010-07-22 15:23:25 -07002794 /*
2795 * If ep->skip is set, it means there are missed tds on the
2796 * endpoint ring need to take care of.
2797 * Process them as short transfer until reach the td pointed by
2798 * the event.
2799 */
Mathias Nyman3b4739b82015-10-12 11:30:12 +03002800 } while (handling_skipped_tds);
Andiry Xud18240d2010-07-22 15:23:25 -07002801
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002802 return 0;
Mathias Nymanb3368382017-06-15 11:55:43 +03002803
2804err_out:
2805 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
2806 (unsigned long long) xhci_trb_virt_to_dma(
2807 xhci->event_ring->deq_seg,
2808 xhci->event_ring->dequeue),
2809 lower_32_bits(le64_to_cpu(event->buffer)),
2810 upper_32_bits(le64_to_cpu(event->buffer)),
2811 le32_to_cpu(event->transfer_len),
2812 le32_to_cpu(event->flags));
2813 return -ENODEV;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002814}
2815
2816/*
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002817 * This function handles all OS-owned events on the event ring. It may drop
2818 * xhci->lock between event processing (e.g. to pass up port status changes).
Matt Evans9dee9a22011-03-29 13:41:02 +11002819 * Returns >0 for "possibly more events to process" (caller should call again),
2820 * otherwise 0 if done. In future, <0 returns should indicate error code.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002821 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002822static int xhci_handle_event(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002823{
2824 union xhci_trb *event;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002825 int update_ptrs = 1;
Mathias Nyman03538102021-01-29 15:00:29 +02002826 u32 trb_type;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002827 int ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002828
Lu Baoluf4c8f032016-11-11 15:13:25 +02002829 /* Event ring hasn't been allocated yet. */
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002830 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
Lu Baoluf4c8f032016-11-11 15:13:25 +02002831 xhci_err(xhci, "ERROR event ring not ready\n");
2832 return -ENOMEM;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002833 }
2834
2835 event = xhci->event_ring->dequeue;
2836 /* Does the HC or OS own the TRB? */
Matt Evans28ccd292011-03-29 13:40:46 +11002837 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
Lu Baoluf4c8f032016-11-11 15:13:25 +02002838 xhci->event_ring->cycle_state)
Matt Evans9dee9a22011-03-29 13:41:02 +11002839 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002840
Felipe Balbia37c3f72017-01-23 14:20:19 +02002841 trace_xhci_handle_event(xhci->event_ring, &event->generic);
2842
Matt Evans92a3da42011-03-29 13:40:51 +11002843 /*
2844 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2845 * speculative reads of the event's flags/data below.
2846 */
2847 rmb();
Mathias Nyman03538102021-01-29 15:00:29 +02002848 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002849 /* FIXME: Handle more event types. */
Mathias Nyman03538102021-01-29 15:00:29 +02002850
2851 switch (trb_type) {
2852 case TRB_COMPLETION:
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002853 handle_cmd_completion(xhci, &event->event_cmd);
2854 break;
Mathias Nyman03538102021-01-29 15:00:29 +02002855 case TRB_PORT_STATUS:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002856 handle_port_status(xhci, event);
2857 update_ptrs = 0;
2858 break;
Mathias Nyman03538102021-01-29 15:00:29 +02002859 case TRB_TRANSFER:
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002860 ret = handle_tx_event(xhci, &event->trans_event);
Lu Baoluf4c8f032016-11-11 15:13:25 +02002861 if (ret >= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002862 update_ptrs = 0;
2863 break;
Mathias Nyman03538102021-01-29 15:00:29 +02002864 case TRB_DEV_NOTE:
Sarah Sharp623bef92011-11-11 14:57:33 -08002865 handle_device_notification(xhci, event);
2866 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002867 default:
Mathias Nyman03538102021-01-29 15:00:29 +02002868 if (trb_type >= TRB_VENDOR_DEFINED_LOW)
2869 handle_vendor_event(xhci, event, trb_type);
Sarah Sharp02386342010-05-24 13:25:28 -07002870 else
Mathias Nyman03538102021-01-29 15:00:29 +02002871 xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002872 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002873 /* Any of the above functions may drop and re-acquire the lock, so check
2874 * to make sure a watchdog timer didn't mark the host as non-responsive.
2875 */
2876 if (xhci->xhc_state & XHCI_STATE_DYING) {
2877 xhci_dbg(xhci, "xHCI host dying, returning from "
2878 "event handler.\n");
Matt Evans9dee9a22011-03-29 13:41:02 +11002879 return 0;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002880 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002881
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002882 if (update_ptrs)
2883 /* Update SW event ring dequeue pointer */
Andiry Xu3b72fca2012-03-05 17:49:32 +08002884 inc_deq(xhci, xhci->event_ring);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002885
Matt Evans9dee9a22011-03-29 13:41:02 +11002886 /* Are there more items on the event ring? Caller will call us again to
2887 * check.
2888 */
2889 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002890}
Sarah Sharp9032cd52010-07-29 22:12:29 -07002891
2892/*
Peter Chendc0ffbe2019-11-15 18:50:00 +02002893 * Update Event Ring Dequeue Pointer:
2894 * - When all events have finished
2895 * - To avoid "Event Ring Full Error" condition
2896 */
2897static void xhci_update_erst_dequeue(struct xhci_hcd *xhci,
2898 union xhci_trb *event_ring_deq)
2899{
2900 u64 temp_64;
2901 dma_addr_t deq;
2902
2903 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2904 /* If necessary, update the HW's version of the event ring deq ptr. */
2905 if (event_ring_deq != xhci->event_ring->dequeue) {
2906 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2907 xhci->event_ring->dequeue);
2908 if (deq == 0)
2909 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr\n");
2910 /*
2911 * Per 4.9.4, Software writes to the ERDP register shall
2912 * always advance the Event Ring Dequeue Pointer value.
2913 */
2914 if ((temp_64 & (u64) ~ERST_PTR_MASK) ==
2915 ((u64) deq & (u64) ~ERST_PTR_MASK))
2916 return;
2917
2918 /* Update HC event ring dequeue pointer */
2919 temp_64 &= ERST_PTR_MASK;
2920 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2921 }
2922
2923 /* Clear the event handler busy flag (RW1C) */
2924 temp_64 |= ERST_EHB;
2925 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2926}
2927
2928/*
Sarah Sharp9032cd52010-07-29 22:12:29 -07002929 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2930 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2931 * indicators of an event TRB error, but we check the status *first* to be safe.
2932 */
2933irqreturn_t xhci_irq(struct usb_hcd *hcd)
2934{
2935 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002936 union xhci_trb *event_ring_deq;
Felipe Balbi76a35292017-01-23 14:20:07 +02002937 irqreturn_t ret = IRQ_NONE;
Alan Stern63aea0d2017-05-17 18:32:03 +03002938 unsigned long flags;
Felipe Balbi76a35292017-01-23 14:20:07 +02002939 u64 temp_64;
2940 u32 status;
Peter Chendc0ffbe2019-11-15 18:50:00 +02002941 int event_loop = 0;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002942
Alan Stern63aea0d2017-05-17 18:32:03 +03002943 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002944 /* Check if the xHC generated the interrupt, or the irq is shared */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02002945 status = readl(&xhci->op_regs->status);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03002946 if (status == ~(u32)0) {
2947 xhci_hc_died(xhci);
Felipe Balbi76a35292017-01-23 14:20:07 +02002948 ret = IRQ_HANDLED;
2949 goto out;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002950 }
Felipe Balbi76a35292017-01-23 14:20:07 +02002951
2952 if (!(status & STS_EINT))
2953 goto out;
2954
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002955 if (status & STS_FATAL) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002956 xhci_warn(xhci, "WARNING: Host System Error\n");
2957 xhci_halt(xhci);
Felipe Balbi76a35292017-01-23 14:20:07 +02002958 ret = IRQ_HANDLED;
2959 goto out;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002960 }
2961
Sarah Sharpbda53142010-07-29 22:12:38 -07002962 /*
2963 * Clear the op reg interrupt status first,
2964 * so we can receive interrupts from other MSI-X interrupters.
2965 * Write 1 to clear the interrupt status.
2966 */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002967 status |= STS_EINT;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02002968 writel(status, &xhci->op_regs->status);
Sarah Sharpbda53142010-07-29 22:12:38 -07002969
Peter Chen6a29bee2017-05-17 18:32:02 +03002970 if (!hcd->msi_enabled) {
Sarah Sharpc21599a2010-07-29 22:13:00 -07002971 u32 irq_pending;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02002972 irq_pending = readl(&xhci->ir_set->irq_pending);
Felipe Balbi4e833c02012-03-15 16:37:08 +02002973 irq_pending |= IMAN_IP;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02002974 writel(irq_pending, &xhci->ir_set->irq_pending);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002975 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002976
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +03002977 if (xhci->xhc_state & XHCI_STATE_DYING ||
2978 xhci->xhc_state & XHCI_STATE_HALTED) {
Sarah Sharpbda53142010-07-29 22:12:38 -07002979 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2980 "Shouldn't IRQs be disabled?\n");
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002981 /* Clear the event handler busy flag (RW1C);
2982 * the event ring should be empty.
Sarah Sharpbda53142010-07-29 22:12:38 -07002983 */
Sarah Sharpf7b2e402014-01-30 13:27:49 -08002984 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp477632d2014-01-29 14:02:00 -08002985 xhci_write_64(xhci, temp_64 | ERST_EHB,
2986 &xhci->ir_set->erst_dequeue);
Felipe Balbi76a35292017-01-23 14:20:07 +02002987 ret = IRQ_HANDLED;
2988 goto out;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002989 }
2990
2991 event_ring_deq = xhci->event_ring->dequeue;
2992 /* FIXME this should be a delayed service routine
2993 * that clears the EHB.
2994 */
Peter Chendc0ffbe2019-11-15 18:50:00 +02002995 while (xhci_handle_event(xhci) > 0) {
2996 if (event_loop++ < TRBS_PER_SEGMENT / 2)
2997 continue;
2998 xhci_update_erst_dequeue(xhci, event_ring_deq);
2999 event_loop = 0;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07003000 }
Sarah Sharpbda53142010-07-29 22:12:38 -07003001
Peter Chendc0ffbe2019-11-15 18:50:00 +02003002 xhci_update_erst_dequeue(xhci, event_ring_deq);
Felipe Balbi76a35292017-01-23 14:20:07 +02003003 ret = IRQ_HANDLED;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07003004
Felipe Balbi76a35292017-01-23 14:20:07 +02003005out:
Alan Stern63aea0d2017-05-17 18:32:03 +03003006 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp9032cd52010-07-29 22:12:29 -07003007
Felipe Balbi76a35292017-01-23 14:20:07 +02003008 return ret;
Sarah Sharp9032cd52010-07-29 22:12:29 -07003009}
3010
Alex Shi851ec162013-05-24 10:54:19 +08003011irqreturn_t xhci_msi_irq(int irq, void *hcd)
Sarah Sharp9032cd52010-07-29 22:12:29 -07003012{
Alan Stern968b8222011-11-03 12:03:38 -04003013 return xhci_irq(hcd);
Sarah Sharp9032cd52010-07-29 22:12:29 -07003014}
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003015
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003016/**** Endpoint Ring Operations ****/
3017
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003018/*
3019 * Generic function for queueing a TRB on a ring.
3020 * The caller must have checked to make sure there's room on the ring.
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003021 *
3022 * @more_trbs_coming: Will you enqueue more TRBs before calling
3023 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003024 */
3025static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003026 bool more_trbs_coming,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003027 u32 field1, u32 field2, u32 field3, u32 field4)
3028{
3029 struct xhci_generic_trb *trb;
3030
3031 trb = &ring->enqueue->generic;
Matt Evans28ccd292011-03-29 13:40:46 +11003032 trb->field[0] = cpu_to_le32(field1);
3033 trb->field[1] = cpu_to_le32(field2);
3034 trb->field[2] = cpu_to_le32(field3);
Mathias Nyman576667b2021-01-15 18:19:06 +02003035 /* make sure TRB is fully written before giving it to the controller */
3036 wmb();
Matt Evans28ccd292011-03-29 13:40:46 +11003037 trb->field[3] = cpu_to_le32(field4);
Felipe Balbia37c3f72017-01-23 14:20:19 +02003038
3039 trace_xhci_queue_trb(ring, trb);
3040
Andiry Xu3b72fca2012-03-05 17:49:32 +08003041 inc_enq(xhci, ring, more_trbs_coming);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003042}
3043
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003044/*
3045 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
3046 * FIXME allocate segments if the ring is full.
3047 */
3048static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003049 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003050{
Andiry Xu8dfec612012-03-05 17:49:37 +08003051 unsigned int num_trbs_needed;
Mathias Nyman04d21f72021-01-29 15:00:26 +02003052 unsigned int link_trb_count = 0;
Andiry Xu8dfec612012-03-05 17:49:37 +08003053
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003054 /* Make sure the endpoint has been added to xHC schedule */
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003055 switch (ep_state) {
3056 case EP_STATE_DISABLED:
3057 /*
3058 * USB core changed config/interfaces without notifying us,
3059 * or hardware is reporting the wrong state.
3060 */
3061 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
3062 return -ENOENT;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003063 case EP_STATE_ERROR:
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003064 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003065 /* FIXME event handling code for error needs to clear it */
3066 /* XXX not sure if this should be -ENOENT or not */
3067 return -EINVAL;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003068 case EP_STATE_HALTED:
3069 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
Nick Desaulniers1d6903a2020-11-10 17:47:14 -08003070 break;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003071 case EP_STATE_STOPPED:
3072 case EP_STATE_RUNNING:
3073 break;
3074 default:
3075 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
3076 /*
3077 * FIXME issue Configure Endpoint command to try to get the HC
3078 * back into a known state.
3079 */
3080 return -EINVAL;
3081 }
Andiry Xu8dfec612012-03-05 17:49:37 +08003082
3083 while (1) {
Sarah Sharp3d4b81e2014-01-31 11:52:57 -08003084 if (room_on_ring(xhci, ep_ring, num_trbs))
3085 break;
Andiry Xu8dfec612012-03-05 17:49:37 +08003086
3087 if (ep_ring == xhci->cmd_ring) {
3088 xhci_err(xhci, "Do not support expand command ring\n");
3089 return -ENOMEM;
3090 }
3091
Xenia Ragiadakou68ffb012013-08-14 06:33:56 +03003092 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
3093 "ERROR no room on ep ring, try ring expansion");
Andiry Xu8dfec612012-03-05 17:49:37 +08003094 num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
3095 if (xhci_ring_expansion(xhci, ep_ring, num_trbs_needed,
3096 mem_flags)) {
3097 xhci_err(xhci, "Ring expansion failed\n");
3098 return -ENOMEM;
3099 }
Peter Senna Tschudin261fa122012-09-12 19:03:17 +02003100 }
John Youn6c12db92010-05-10 15:33:00 -07003101
Mathias Nymand0c77d82016-06-21 10:58:07 +03003102 while (trb_is_link(ep_ring->enqueue)) {
3103 /* If we're not dealing with 0.95 hardware or isoc rings
3104 * on AMD 0.96 host, clear the chain bit.
3105 */
3106 if (!xhci_link_trb_quirk(xhci) &&
3107 !(ep_ring->type == TYPE_ISOC &&
3108 (xhci->quirks & XHCI_AMD_0x96_HOST)))
3109 ep_ring->enqueue->link.control &=
3110 cpu_to_le32(~TRB_CHAIN);
3111 else
3112 ep_ring->enqueue->link.control |=
3113 cpu_to_le32(TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07003114
Mathias Nymand0c77d82016-06-21 10:58:07 +03003115 wmb();
3116 ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE);
John Youn6c12db92010-05-10 15:33:00 -07003117
Mathias Nymand0c77d82016-06-21 10:58:07 +03003118 /* Toggle the cycle bit after the last ring segment. */
3119 if (link_trb_toggles_cycle(ep_ring->enqueue))
3120 ep_ring->cycle_state ^= 1;
John Youn6c12db92010-05-10 15:33:00 -07003121
Mathias Nymand0c77d82016-06-21 10:58:07 +03003122 ep_ring->enq_seg = ep_ring->enq_seg->next;
3123 ep_ring->enqueue = ep_ring->enq_seg->trbs;
Mathias Nyman04d21f72021-01-29 15:00:26 +02003124
3125 /* prevent infinite loop if all first trbs are link trbs */
3126 if (link_trb_count++ > ep_ring->num_segs) {
3127 xhci_warn(xhci, "Ring is an endless link TRB loop\n");
3128 return -EINVAL;
3129 }
John Youn6c12db92010-05-10 15:33:00 -07003130 }
Mathias Nymanc716e8a2021-01-29 15:00:30 +02003131
3132 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) {
3133 xhci_warn(xhci, "Missing link TRB at end of ring segment\n");
3134 return -EINVAL;
3135 }
3136
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003137 return 0;
3138}
3139
Sarah Sharp23e3be12009-04-29 19:05:20 -07003140static int prepare_transfer(struct xhci_hcd *xhci,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003141 struct xhci_virt_device *xdev,
3142 unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003143 unsigned int stream_id,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003144 unsigned int num_trbs,
3145 struct urb *urb,
Andiry Xu8e51adc2010-07-22 15:23:31 -07003146 unsigned int td_index,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003147 gfp_t mem_flags)
3148{
3149 int ret;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003150 struct urb_priv *urb_priv;
3151 struct xhci_td *td;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003152 struct xhci_ring *ep_ring;
John Yound115b042009-07-27 12:05:15 -07003153 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003154
Mathias Nymanc089cad2021-01-29 15:00:25 +02003155 ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index,
3156 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003157 if (!ep_ring) {
3158 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
3159 stream_id);
3160 return -EINVAL;
3161 }
3162
Mathias Nyman5071e6b2016-11-11 15:13:28 +02003163 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx),
Andiry Xu3b72fca2012-03-05 17:49:32 +08003164 num_trbs, mem_flags);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003165 if (ret)
3166 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003167
Andiry Xu8e51adc2010-07-22 15:23:31 -07003168 urb_priv = urb->hcpriv;
Mathias Nyman7e64b032017-01-23 14:20:26 +02003169 td = &urb_priv->td[td_index];
Andiry Xu8e51adc2010-07-22 15:23:31 -07003170
3171 INIT_LIST_HEAD(&td->td_list);
3172 INIT_LIST_HEAD(&td->cancelled_td_list);
3173
3174 if (td_index == 0) {
Sarah Sharp214f76f2010-10-26 11:22:02 -07003175 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07003176 if (unlikely(ret))
Andiry Xu8e51adc2010-07-22 15:23:31 -07003177 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003178 }
3179
Andiry Xu8e51adc2010-07-22 15:23:31 -07003180 td->urb = urb;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003181 /* Add this TD to the tail of the endpoint ring's TD list */
Andiry Xu8e51adc2010-07-22 15:23:31 -07003182 list_add_tail(&td->td_list, &ep_ring->td_list);
3183 td->start_seg = ep_ring->enq_seg;
3184 td->first_trb = ep_ring->enqueue;
3185
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003186 return 0;
3187}
3188
Lu Baolu67d2ea92017-12-08 17:59:09 +02003189unsigned int count_trbs(u64 addr, u64 len)
Sarah Sharp8a96c052009-04-27 19:59:19 -07003190{
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003191 unsigned int num_trbs;
Sarah Sharp8a96c052009-04-27 19:59:19 -07003192
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003193 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
3194 TRB_MAX_BUFF_SIZE);
3195 if (num_trbs == 0)
3196 num_trbs++;
Sarah Sharp8a96c052009-04-27 19:59:19 -07003197
Sarah Sharp8a96c052009-04-27 19:59:19 -07003198 return num_trbs;
3199}
3200
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003201static inline unsigned int count_trbs_needed(struct urb *urb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07003202{
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003203 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length);
3204}
3205
3206static unsigned int count_sg_trbs_needed(struct urb *urb)
3207{
3208 struct scatterlist *sg;
3209 unsigned int i, len, full_len, num_trbs = 0;
3210
3211 full_len = urb->transfer_buffer_length;
3212
3213 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
3214 len = sg_dma_len(sg);
3215 num_trbs += count_trbs(sg_dma_address(sg), len);
3216 len = min_t(unsigned int, len, full_len);
3217 full_len -= len;
3218 if (full_len == 0)
3219 break;
3220 }
3221
3222 return num_trbs;
3223}
3224
3225static unsigned int count_isoc_trbs_needed(struct urb *urb, int i)
3226{
3227 u64 addr, len;
3228
3229 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
3230 len = urb->iso_frame_desc[i].length;
3231
3232 return count_trbs(addr, len);
3233}
3234
3235static void check_trb_math(struct urb *urb, int running_total)
3236{
3237 if (unlikely(running_total != urb->transfer_buffer_length))
Paul Zimmermana2490182011-02-12 14:06:44 -08003238 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
Sarah Sharp8a96c052009-04-27 19:59:19 -07003239 "queued %#x (%d), asked for %#x (%d)\n",
3240 __func__,
3241 urb->ep->desc.bEndpointAddress,
3242 running_total, running_total,
3243 urb->transfer_buffer_length,
3244 urb->transfer_buffer_length);
3245}
3246
Sarah Sharp23e3be12009-04-29 19:05:20 -07003247static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003248 unsigned int ep_index, unsigned int stream_id, int start_cycle,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003249 struct xhci_generic_trb *start_trb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07003250{
Sarah Sharp8a96c052009-04-27 19:59:19 -07003251 /*
3252 * Pass all the TRBs to the hardware at once and make sure this write
3253 * isn't reordered.
3254 */
3255 wmb();
Andiry Xu50f7b522010-12-20 15:09:34 +08003256 if (start_cycle)
Matt Evans28ccd292011-03-29 13:40:46 +11003257 start_trb->field[3] |= cpu_to_le32(start_cycle);
Andiry Xu50f7b522010-12-20 15:09:34 +08003258 else
Matt Evans28ccd292011-03-29 13:40:46 +11003259 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
Andiry Xube88fe42010-10-14 07:22:57 -07003260 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
Sarah Sharp8a96c052009-04-27 19:59:19 -07003261}
3262
Alexandr Ivanov78140152016-04-22 13:17:11 +03003263static void check_interval(struct xhci_hcd *xhci, struct urb *urb,
3264 struct xhci_ep_ctx *ep_ctx)
Sarah Sharp624defa2009-09-02 12:14:28 -07003265{
Sarah Sharp624defa2009-09-02 12:14:28 -07003266 int xhci_interval;
3267 int ep_interval;
3268
Matt Evans28ccd292011-03-29 13:40:46 +11003269 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Sarah Sharp624defa2009-09-02 12:14:28 -07003270 ep_interval = urb->interval;
Alexandr Ivanov78140152016-04-22 13:17:11 +03003271
Sarah Sharp624defa2009-09-02 12:14:28 -07003272 /* Convert to microframes */
3273 if (urb->dev->speed == USB_SPEED_LOW ||
3274 urb->dev->speed == USB_SPEED_FULL)
3275 ep_interval *= 8;
Alexandr Ivanov78140152016-04-22 13:17:11 +03003276
Sarah Sharp624defa2009-09-02 12:14:28 -07003277 /* FIXME change this to a warning and a suggestion to use the new API
3278 * to set the polling interval (once the API is added).
3279 */
3280 if (xhci_interval != ep_interval) {
Dmitry Kasatkin0730d522013-08-27 17:47:35 +03003281 dev_dbg_ratelimited(&urb->dev->dev,
3282 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n",
3283 ep_interval, ep_interval == 1 ? "" : "s",
3284 xhci_interval, xhci_interval == 1 ? "" : "s");
Sarah Sharp624defa2009-09-02 12:14:28 -07003285 urb->interval = xhci_interval;
3286 /* Convert back to frames for LS/FS devices */
3287 if (urb->dev->speed == USB_SPEED_LOW ||
3288 urb->dev->speed == USB_SPEED_FULL)
3289 urb->interval /= 8;
3290 }
Alexandr Ivanov78140152016-04-22 13:17:11 +03003291}
3292
3293/*
3294 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
3295 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
3296 * (comprised of sg list entries) can take several service intervals to
3297 * transmit.
3298 */
3299int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3300 struct urb *urb, int slot_id, unsigned int ep_index)
3301{
3302 struct xhci_ep_ctx *ep_ctx;
3303
3304 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index);
3305 check_interval(xhci, urb, ep_ctx);
3306
Dan Carpenter3fc82062012-03-28 10:30:26 +03003307 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
Sarah Sharp624defa2009-09-02 12:14:28 -07003308}
3309
Sarah Sharp04dd9502009-11-11 10:28:30 -08003310/*
Sarah Sharp4525c0a2012-10-25 15:56:40 -07003311 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
3312 * packets remaining in the TD (*not* including this TRB).
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003313 *
3314 * Total TD packet count = total_packet_count =
Sarah Sharp4525c0a2012-10-25 15:56:40 -07003315 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003316 *
3317 * Packets transferred up to and including this TRB = packets_transferred =
3318 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
3319 *
3320 * TD size = total_packet_count - packets_transferred
3321 *
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003322 * For xHCI 0.96 and older, TD size field should be the remaining bytes
3323 * including this TRB, right shifted by 10
3324 *
3325 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31.
3326 * This is taken care of in the TRB_TD_SIZE() macro
3327 *
Sarah Sharp4525c0a2012-10-25 15:56:40 -07003328 * The last TRB in a TD must have the TD size set to zero.
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003329 */
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003330static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred,
3331 int trb_buff_len, unsigned int td_total_len,
Mathias Nyman124c3932016-06-21 10:57:59 +03003332 struct urb *urb, bool more_trbs_coming)
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003333{
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003334 u32 maxp, total_packet_count;
3335
Chunfeng Yun72b663a2017-12-08 18:10:06 +02003336 /* MTK xHCI 0.96 contains some features from 1.0 */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003337 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST))
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003338 return ((td_total_len - transferred) >> 10);
3339
Sarah Sharp48df4a62011-08-12 10:23:01 -07003340 /* One TRB with a zero-length data packet. */
Mathias Nyman124c3932016-06-21 10:57:59 +03003341 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003342 trb_buff_len == td_total_len)
Sarah Sharp48df4a62011-08-12 10:23:01 -07003343 return 0;
3344
Chunfeng Yun72b663a2017-12-08 18:10:06 +02003345 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */
3346 if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100))
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003347 trb_buff_len = 0;
3348
Felipe Balbi734d3dd2016-09-28 13:46:37 +03003349 maxp = usb_endpoint_maxp(&urb->ep->desc);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003350 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
3351
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003352 /* Queueing functions don't count the current TRB into transferred */
3353 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003354}
3355
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003356
Mathias Nyman474ed232016-06-21 10:58:01 +03003357static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003358 u32 *trb_buff_len, struct xhci_segment *seg)
Mathias Nyman474ed232016-06-21 10:58:01 +03003359{
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003360 struct device *dev = xhci_to_hcd(xhci)->self.controller;
Mathias Nyman474ed232016-06-21 10:58:01 +03003361 unsigned int unalign;
3362 unsigned int max_pkt;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003363 u32 new_buff_len;
Henry Lin597c56e2019-05-22 14:33:57 +03003364 size_t len;
Mathias Nyman474ed232016-06-21 10:58:01 +03003365
Felipe Balbi734d3dd2016-09-28 13:46:37 +03003366 max_pkt = usb_endpoint_maxp(&urb->ep->desc);
Mathias Nyman474ed232016-06-21 10:58:01 +03003367 unalign = (enqd_len + *trb_buff_len) % max_pkt;
3368
3369 /* we got lucky, last normal TRB data on segment is packet aligned */
3370 if (unalign == 0)
3371 return 0;
3372
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003373 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n",
3374 unalign, *trb_buff_len);
3375
Mathias Nyman474ed232016-06-21 10:58:01 +03003376 /* is the last nornal TRB alignable by splitting it */
3377 if (*trb_buff_len > unalign) {
3378 *trb_buff_len -= unalign;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003379 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len);
Mathias Nyman474ed232016-06-21 10:58:01 +03003380 return 0;
3381 }
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003382
3383 /*
3384 * We want enqd_len + trb_buff_len to sum up to a number aligned to
3385 * number which is divisible by the endpoint's wMaxPacketSize. IOW:
3386 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0.
3387 */
3388 new_buff_len = max_pkt - (enqd_len % max_pkt);
3389
3390 if (new_buff_len > (urb->transfer_buffer_length - enqd_len))
3391 new_buff_len = (urb->transfer_buffer_length - enqd_len);
3392
3393 /* create a max max_pkt sized bounce buffer pointed to by last trb */
3394 if (usb_urb_dir_out(urb)) {
Henry Lin597c56e2019-05-22 14:33:57 +03003395 len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003396 seg->bounce_buf, new_buff_len, enqd_len);
Mathias Nymanc03101ff2019-10-04 14:59:26 +03003397 if (len != new_buff_len)
Henry Lin597c56e2019-05-22 14:33:57 +03003398 xhci_warn(xhci,
Fabio Estevamc1a145a2019-05-22 10:35:29 -03003399 "WARN Wrong bounce buffer write length: %zu != %d\n",
Mathias Nymanc03101ff2019-10-04 14:59:26 +03003400 len, new_buff_len);
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003401 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3402 max_pkt, DMA_TO_DEVICE);
3403 } else {
3404 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3405 max_pkt, DMA_FROM_DEVICE);
3406 }
3407
3408 if (dma_mapping_error(dev, seg->bounce_dma)) {
3409 /* try without aligning. Some host controllers survive */
3410 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n");
3411 return 0;
3412 }
3413 *trb_buff_len = new_buff_len;
3414 seg->bounce_len = new_buff_len;
3415 seg->bounce_offs = enqd_len;
3416
3417 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len);
3418
Mathias Nyman474ed232016-06-21 10:58:01 +03003419 return 1;
3420}
3421
Sarah Sharpb10de142009-04-27 19:58:50 -07003422/* This is very similar to what ehci-q.c qtd_fill() does */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003423int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpb10de142009-04-27 19:58:50 -07003424 struct urb *urb, int slot_id, unsigned int ep_index)
3425{
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003426 struct xhci_ring *ring;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003427 struct urb_priv *urb_priv;
Sarah Sharpb10de142009-04-27 19:58:50 -07003428 struct xhci_td *td;
Sarah Sharpb10de142009-04-27 19:58:50 -07003429 struct xhci_generic_trb *start_trb;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003430 struct scatterlist *sg = NULL;
Mathias Nyman5a83f042016-06-21 10:57:58 +03003431 bool more_trbs_coming = true;
3432 bool need_zero_pkt = false;
Mathias Nyman86065c22016-06-21 10:58:00 +03003433 bool first_trb = true;
3434 unsigned int num_trbs;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003435 unsigned int start_cycle, num_sgs = 0;
Mathias Nyman86065c22016-06-21 10:58:00 +03003436 unsigned int enqd_len, block_len, trb_buff_len, full_len;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003437 int sent_len, ret;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003438 u32 field, length_field, remainder;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003439 u64 addr, send_addr;
Sarah Sharpb10de142009-04-27 19:58:50 -07003440
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003441 ring = xhci_urb_to_transfer_ring(xhci, urb);
3442 if (!ring)
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003443 return -EINVAL;
Sarah Sharpb10de142009-04-27 19:58:50 -07003444
Mathias Nyman86065c22016-06-21 10:58:00 +03003445 full_len = urb->transfer_buffer_length;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003446 /* If we have scatter/gather list, we use it. */
Tejas Joglekar2017a1e2020-12-08 11:29:09 +02003447 if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) {
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003448 num_sgs = urb->num_mapped_sgs;
3449 sg = urb->sg;
Mathias Nyman86065c22016-06-21 10:58:00 +03003450 addr = (u64) sg_dma_address(sg);
3451 block_len = sg_dma_len(sg);
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003452 num_trbs = count_sg_trbs_needed(urb);
Mathias Nyman86065c22016-06-21 10:58:00 +03003453 } else {
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003454 num_trbs = count_trbs_needed(urb);
Mathias Nyman86065c22016-06-21 10:58:00 +03003455 addr = (u64) urb->transfer_dma;
3456 block_len = full_len;
3457 }
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003458 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3459 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003460 num_trbs, urb, 0, mem_flags);
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003461 if (unlikely(ret < 0))
Sarah Sharpb10de142009-04-27 19:58:50 -07003462 return ret;
3463
Andiry Xu8e51adc2010-07-22 15:23:31 -07003464 urb_priv = urb->hcpriv;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03003465
3466 /* Deal with URB_ZERO_PACKET - need one more td/trb */
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02003467 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1)
Mathias Nyman5a83f042016-06-21 10:57:58 +03003468 need_zero_pkt = true;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03003469
Mathias Nyman7e64b032017-01-23 14:20:26 +02003470 td = &urb_priv->td[0];
Andiry Xu8e51adc2010-07-22 15:23:31 -07003471
Sarah Sharpb10de142009-04-27 19:58:50 -07003472 /*
3473 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3474 * until we've finished creating all the other TRBs. The ring's cycle
3475 * state may change as we enqueue the other TRBs, so save it too.
3476 */
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003477 start_trb = &ring->enqueue->generic;
3478 start_cycle = ring->cycle_state;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003479 send_addr = addr;
Sarah Sharpb10de142009-04-27 19:58:50 -07003480
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003481 /* Queue the TRBs, even if they are zero-length */
Alban Browaeys0d2daad2016-08-16 10:18:04 +03003482 for (enqd_len = 0; first_trb || enqd_len < full_len;
3483 enqd_len += trb_buff_len) {
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003484 field = TRB_TYPE(TRB_NORMAL);
3485
Mathias Nyman86065c22016-06-21 10:58:00 +03003486 /* TRB buffer should not cross 64KB boundaries */
3487 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
3488 trb_buff_len = min_t(unsigned int, trb_buff_len, block_len);
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003489
Mathias Nyman86065c22016-06-21 10:58:00 +03003490 if (enqd_len + trb_buff_len > full_len)
3491 trb_buff_len = full_len - enqd_len;
Sarah Sharpb10de142009-04-27 19:58:50 -07003492
3493 /* Don't change the cycle bit of the first TRB until later */
Mathias Nyman86065c22016-06-21 10:58:00 +03003494 if (first_trb) {
3495 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08003496 if (start_cycle == 0)
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003497 field |= TRB_CYCLE;
Andiry Xu50f7b522010-12-20 15:09:34 +08003498 } else
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003499 field |= ring->cycle_state;
Sarah Sharpb10de142009-04-27 19:58:50 -07003500
3501 /* Chain all the TRBs together; clear the chain bit in the last
3502 * TRB to indicate it's the last TRB in the chain.
3503 */
Mathias Nyman86065c22016-06-21 10:58:00 +03003504 if (enqd_len + trb_buff_len < full_len) {
Sarah Sharpb10de142009-04-27 19:58:50 -07003505 field |= TRB_CHAIN;
Mathias Nyman2d98ef42016-06-21 10:58:04 +03003506 if (trb_is_link(ring->enqueue + 1)) {
Mathias Nyman474ed232016-06-21 10:58:01 +03003507 if (xhci_align_td(xhci, urb, enqd_len,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003508 &trb_buff_len,
3509 ring->enq_seg)) {
3510 send_addr = ring->enq_seg->bounce_dma;
3511 /* assuming TD won't span 2 segs */
3512 td->bounce_seg = ring->enq_seg;
3513 }
Mathias Nyman474ed232016-06-21 10:58:01 +03003514 }
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003515 }
3516 if (enqd_len + trb_buff_len >= full_len) {
3517 field &= ~TRB_CHAIN;
Sarah Sharpb10de142009-04-27 19:58:50 -07003518 field |= TRB_IOC;
Mathias Nyman124c3932016-06-21 10:57:59 +03003519 more_trbs_coming = false;
Mathias Nyman5a83f042016-06-21 10:57:58 +03003520 td->last_trb = ring->enqueue;
Mathias Nyman55f61532021-01-29 15:00:28 +02003521 td->last_trb_seg = ring->enq_seg;
Nicolas Saenz Julienne33e39352019-04-26 16:23:29 +03003522 if (xhci_urb_suitable_for_idt(urb)) {
3523 memcpy(&send_addr, urb->transfer_buffer,
3524 trb_buff_len);
Samuel Hollandbfa3dbb2019-10-25 17:30:28 +03003525 le64_to_cpus(&send_addr);
Nicolas Saenz Julienne33e39352019-04-26 16:23:29 +03003526 field |= TRB_IDT;
3527 }
Sarah Sharpb10de142009-04-27 19:58:50 -07003528 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003529
3530 /* Only set interrupt on short packet for IN endpoints */
3531 if (usb_urb_dir_in(urb))
3532 field |= TRB_ISP;
3533
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003534 /* Set the TRB length, TD size, and interrupter fields. */
Mathias Nyman86065c22016-06-21 10:58:00 +03003535 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len,
3536 full_len, urb, more_trbs_coming);
3537
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003538 length_field = TRB_LEN(trb_buff_len) |
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003539 TRB_TD_SIZE(remainder) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003540 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003541
Mathias Nyman124c3932016-06-21 10:57:59 +03003542 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003543 lower_32_bits(send_addr),
3544 upper_32_bits(send_addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003545 length_field,
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003546 field);
Mathias Nyman55f61532021-01-29 15:00:28 +02003547 td->num_trbs++;
Sarah Sharpb10de142009-04-27 19:58:50 -07003548 addr += trb_buff_len;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003549 sent_len = trb_buff_len;
Sarah Sharpb10de142009-04-27 19:58:50 -07003550
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003551 while (sg && sent_len >= block_len) {
Mathias Nyman86065c22016-06-21 10:58:00 +03003552 /* New sg entry */
3553 --num_sgs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003554 sent_len -= block_len;
Sriharsha Allenki3c6f8cb2020-05-14 14:04:31 +03003555 sg = sg_next(sg);
3556 if (num_sgs != 0 && sg) {
Mathias Nyman86065c22016-06-21 10:58:00 +03003557 block_len = sg_dma_len(sg);
3558 addr = (u64) sg_dma_address(sg);
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003559 addr += sent_len;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003560 }
3561 }
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003562 block_len -= sent_len;
3563 send_addr = addr;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003564 }
3565
Mathias Nyman5a83f042016-06-21 10:57:58 +03003566 if (need_zero_pkt) {
3567 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3568 ep_index, urb->stream_id,
3569 1, urb, 1, mem_flags);
Mathias Nyman7e64b032017-01-23 14:20:26 +02003570 urb_priv->td[1].last_trb = ring->enqueue;
Mathias Nyman55f61532021-01-29 15:00:28 +02003571 urb_priv->td[1].last_trb_seg = ring->enq_seg;
Mathias Nyman5a83f042016-06-21 10:57:58 +03003572 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC;
3573 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field);
Mathias Nyman55f61532021-01-29 15:00:28 +02003574 urb_priv->td[1].num_trbs++;
Mathias Nyman5a83f042016-06-21 10:57:58 +03003575 }
3576
Mathias Nyman86065c22016-06-21 10:58:00 +03003577 check_trb_math(urb, enqd_len);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003578 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003579 start_cycle, start_trb);
Sarah Sharpb10de142009-04-27 19:58:50 -07003580 return 0;
3581}
3582
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003583/* Caller must have locked xhci->lock */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003584int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003585 struct urb *urb, int slot_id, unsigned int ep_index)
3586{
3587 struct xhci_ring *ep_ring;
3588 int num_trbs;
3589 int ret;
3590 struct usb_ctrlrequest *setup;
3591 struct xhci_generic_trb *start_trb;
3592 int start_cycle;
Lu Baolufb79a6d2017-01-23 14:20:01 +02003593 u32 field;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003594 struct urb_priv *urb_priv;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003595 struct xhci_td *td;
3596
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003597 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
3598 if (!ep_ring)
3599 return -EINVAL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003600
3601 /*
3602 * Need to copy setup packet into setup TRB, so we can't use the setup
3603 * DMA address.
3604 */
3605 if (!urb->setup_packet)
3606 return -EINVAL;
3607
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003608 /* 1 TRB for setup, 1 for status */
3609 num_trbs = 2;
3610 /*
3611 * Don't need to check if we need additional event data and normal TRBs,
3612 * since data in control transfers will never get bigger than 16MB
3613 * XXX: can we get a buffer that crosses 64KB boundaries?
3614 */
3615 if (urb->transfer_buffer_length > 0)
3616 num_trbs++;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003617 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3618 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003619 num_trbs, urb, 0, mem_flags);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003620 if (ret < 0)
3621 return ret;
3622
Andiry Xu8e51adc2010-07-22 15:23:31 -07003623 urb_priv = urb->hcpriv;
Mathias Nyman7e64b032017-01-23 14:20:26 +02003624 td = &urb_priv->td[0];
Mathias Nyman55f61532021-01-29 15:00:28 +02003625 td->num_trbs = num_trbs;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003626
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003627 /*
3628 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3629 * until we've finished creating all the other TRBs. The ring's cycle
3630 * state may change as we enqueue the other TRBs, so save it too.
3631 */
3632 start_trb = &ep_ring->enqueue->generic;
3633 start_cycle = ep_ring->cycle_state;
3634
3635 /* Queue setup TRB - see section 6.4.1.2.1 */
3636 /* FIXME better way to translate setup_packet into two u32 fields? */
3637 setup = (struct usb_ctrlrequest *) urb->setup_packet;
Andiry Xu50f7b522010-12-20 15:09:34 +08003638 field = 0;
3639 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3640 if (start_cycle == 0)
3641 field |= 0x1;
Andiry Xub83cdc82011-05-05 18:13:56 +08003642
Mathias Nymandca77942015-09-21 17:46:16 +03003643 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003644 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) {
Andiry Xub83cdc82011-05-05 18:13:56 +08003645 if (urb->transfer_buffer_length > 0) {
3646 if (setup->bRequestType & USB_DIR_IN)
3647 field |= TRB_TX_TYPE(TRB_DATA_IN);
3648 else
3649 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3650 }
3651 }
3652
Andiry Xu3b72fca2012-03-05 17:49:32 +08003653 queue_trb(xhci, ep_ring, true,
Matt Evans28ccd292011-03-29 13:40:46 +11003654 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3655 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3656 TRB_LEN(8) | TRB_INTR_TARGET(0),
3657 /* Immediate data in pointer */
3658 field);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003659
3660 /* If there's data, queue data TRBs */
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003661 /* Only set interrupt on short packet for IN endpoints */
3662 if (usb_urb_dir_in(urb))
3663 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3664 else
3665 field = TRB_TYPE(TRB_DATA);
3666
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003667 if (urb->transfer_buffer_length > 0) {
Lu Baolufb79a6d2017-01-23 14:20:01 +02003668 u32 length_field, remainder;
Mathias Nyman13b82b72019-05-22 14:34:00 +03003669 u64 addr;
Lu Baolufb79a6d2017-01-23 14:20:01 +02003670
Nicolas Saenz Julienne33e39352019-04-26 16:23:29 +03003671 if (xhci_urb_suitable_for_idt(urb)) {
Mathias Nyman13b82b72019-05-22 14:34:00 +03003672 memcpy(&addr, urb->transfer_buffer,
Nicolas Saenz Julienne33e39352019-04-26 16:23:29 +03003673 urb->transfer_buffer_length);
Samuel Hollandbfa3dbb2019-10-25 17:30:28 +03003674 le64_to_cpus(&addr);
Nicolas Saenz Julienne33e39352019-04-26 16:23:29 +03003675 field |= TRB_IDT;
Mathias Nyman13b82b72019-05-22 14:34:00 +03003676 } else {
3677 addr = (u64) urb->transfer_dma;
Nicolas Saenz Julienne33e39352019-04-26 16:23:29 +03003678 }
3679
Lu Baolufb79a6d2017-01-23 14:20:01 +02003680 remainder = xhci_td_remainder(xhci, 0,
3681 urb->transfer_buffer_length,
3682 urb->transfer_buffer_length,
3683 urb, 1);
3684 length_field = TRB_LEN(urb->transfer_buffer_length) |
3685 TRB_TD_SIZE(remainder) |
3686 TRB_INTR_TARGET(0);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003687 if (setup->bRequestType & USB_DIR_IN)
3688 field |= TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003689 queue_trb(xhci, ep_ring, true,
Mathias Nyman13b82b72019-05-22 14:34:00 +03003690 lower_32_bits(addr),
3691 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003692 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003693 field | ep_ring->cycle_state);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003694 }
3695
3696 /* Save the DMA address of the last TRB in the TD */
3697 td->last_trb = ep_ring->enqueue;
Mathias Nyman55f61532021-01-29 15:00:28 +02003698 td->last_trb_seg = ep_ring->enq_seg;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003699
3700 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3701 /* If the device sent data, the status stage is an OUT transfer */
3702 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3703 field = 0;
3704 else
3705 field = TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003706 queue_trb(xhci, ep_ring, false,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003707 0,
3708 0,
3709 TRB_INTR_TARGET(0),
3710 /* Event on completion */
3711 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3712
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003713 giveback_first_trb(xhci, slot_id, ep_index, 0,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003714 start_cycle, start_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003715 return 0;
3716}
3717
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003718/*
3719 * The transfer burst count field of the isochronous TRB defines the number of
3720 * bursts that are required to move all packets in this TD. Only SuperSpeed
3721 * devices can burst up to bMaxBurst number of packets per service interval.
3722 * This field is zero based, meaning a value of zero in the field means one
3723 * burst. Basically, for everything but SuperSpeed devices, this field will be
3724 * zero. Only xHCI 1.0 host controllers support this field.
3725 */
3726static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003727 struct urb *urb, unsigned int total_packet_count)
3728{
3729 unsigned int max_burst;
3730
Mathias Nyman09c352e2016-02-12 16:40:17 +02003731 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER)
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003732 return 0;
3733
3734 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
Mathias Nyman3213b152014-06-24 17:14:41 +03003735 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003736}
3737
Sarah Sharpb61d3782011-04-19 17:43:33 -07003738/*
3739 * Returns the number of packets in the last "burst" of packets. This field is
3740 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3741 * the last burst packet count is equal to the total number of packets in the
3742 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3743 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3744 * contain 1 to (bMaxBurst + 1) packets.
3745 */
3746static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
Sarah Sharpb61d3782011-04-19 17:43:33 -07003747 struct urb *urb, unsigned int total_packet_count)
3748{
3749 unsigned int max_burst;
3750 unsigned int residue;
3751
3752 if (xhci->hci_version < 0x100)
3753 return 0;
3754
Mathias Nyman09c352e2016-02-12 16:40:17 +02003755 if (urb->dev->speed >= USB_SPEED_SUPER) {
Sarah Sharpb61d3782011-04-19 17:43:33 -07003756 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3757 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3758 residue = total_packet_count % (max_burst + 1);
3759 /* If residue is zero, the last burst contains (max_burst + 1)
3760 * number of packets, but the TLBPC field is zero-based.
3761 */
3762 if (residue == 0)
3763 return max_burst;
3764 return residue - 1;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003765 }
Mathias Nyman09c352e2016-02-12 16:40:17 +02003766 if (total_packet_count == 0)
3767 return 0;
3768 return total_packet_count - 1;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003769}
3770
Lu Baolu79b80942015-08-06 19:24:00 +03003771/*
3772 * Calculates Frame ID field of the isochronous TRB identifies the
3773 * target frame that the Interval associated with this Isochronous
3774 * Transfer Descriptor will start on. Refer to 4.11.2.5 in 1.1 spec.
3775 *
3776 * Returns actual frame id on success, negative value on error.
3777 */
3778static int xhci_get_isoc_frame_id(struct xhci_hcd *xhci,
3779 struct urb *urb, int index)
3780{
3781 int start_frame, ist, ret = 0;
3782 int start_frame_id, end_frame_id, current_frame_id;
3783
3784 if (urb->dev->speed == USB_SPEED_LOW ||
3785 urb->dev->speed == USB_SPEED_FULL)
3786 start_frame = urb->start_frame + index * urb->interval;
3787 else
3788 start_frame = (urb->start_frame + index * urb->interval) >> 3;
3789
3790 /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2):
3791 *
3792 * If bit [3] of IST is cleared to '0', software can add a TRB no
3793 * later than IST[2:0] Microframes before that TRB is scheduled to
3794 * be executed.
3795 * If bit [3] of IST is set to '1', software can add a TRB no later
3796 * than IST[2:0] Frames before that TRB is scheduled to be executed.
3797 */
3798 ist = HCS_IST(xhci->hcs_params2) & 0x7;
3799 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
3800 ist <<= 3;
3801
3802 /* Software shall not schedule an Isoch TD with a Frame ID value that
3803 * is less than the Start Frame ID or greater than the End Frame ID,
3804 * where:
3805 *
3806 * End Frame ID = (Current MFINDEX register value + 895 ms.) MOD 2048
3807 * Start Frame ID = (Current MFINDEX register value + IST + 1) MOD 2048
3808 *
3809 * Both the End Frame ID and Start Frame ID values are calculated
3810 * in microframes. When software determines the valid Frame ID value;
3811 * The End Frame ID value should be rounded down to the nearest Frame
3812 * boundary, and the Start Frame ID value should be rounded up to the
3813 * nearest Frame boundary.
3814 */
3815 current_frame_id = readl(&xhci->run_regs->microframe_index);
3816 start_frame_id = roundup(current_frame_id + ist + 1, 8);
3817 end_frame_id = rounddown(current_frame_id + 895 * 8, 8);
3818
3819 start_frame &= 0x7ff;
3820 start_frame_id = (start_frame_id >> 3) & 0x7ff;
3821 end_frame_id = (end_frame_id >> 3) & 0x7ff;
3822
3823 xhci_dbg(xhci, "%s: index %d, reg 0x%x start_frame_id 0x%x, end_frame_id 0x%x, start_frame 0x%x\n",
3824 __func__, index, readl(&xhci->run_regs->microframe_index),
3825 start_frame_id, end_frame_id, start_frame);
3826
3827 if (start_frame_id < end_frame_id) {
3828 if (start_frame > end_frame_id ||
3829 start_frame < start_frame_id)
3830 ret = -EINVAL;
3831 } else if (start_frame_id > end_frame_id) {
3832 if ((start_frame > end_frame_id &&
3833 start_frame < start_frame_id))
3834 ret = -EINVAL;
3835 } else {
3836 ret = -EINVAL;
3837 }
3838
3839 if (index == 0) {
3840 if (ret == -EINVAL || start_frame == start_frame_id) {
3841 start_frame = start_frame_id + 1;
3842 if (urb->dev->speed == USB_SPEED_LOW ||
3843 urb->dev->speed == USB_SPEED_FULL)
3844 urb->start_frame = start_frame;
3845 else
3846 urb->start_frame = start_frame << 3;
3847 ret = 0;
3848 }
3849 }
3850
3851 if (ret) {
3852 xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n",
3853 start_frame, current_frame_id, index,
3854 start_frame_id, end_frame_id);
3855 xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n");
3856 return ret;
3857 }
3858
3859 return start_frame;
3860}
3861
Mathias Nymanedc649a2020-09-18 16:17:50 +03003862/* Check if we should generate event interrupt for a TD in an isoc URB */
3863static bool trb_block_event_intr(struct xhci_hcd *xhci, int num_tds, int i)
3864{
3865 if (xhci->hci_version < 0x100)
3866 return false;
3867 /* always generate an event interrupt for the last TD */
3868 if (i == num_tds - 1)
3869 return false;
3870 /*
3871 * If AVOID_BEI is set the host handles full event rings poorly,
3872 * generate an event at least every 8th TD to clear the event ring
3873 */
3874 if (i && xhci->quirks & XHCI_AVOID_BEI)
3875 return !!(i % 8);
3876
3877 return true;
3878}
3879
Andiry Xu04e51902010-07-22 15:23:39 -07003880/* This is for isoc transfer */
3881static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3882 struct urb *urb, int slot_id, unsigned int ep_index)
3883{
3884 struct xhci_ring *ep_ring;
3885 struct urb_priv *urb_priv;
3886 struct xhci_td *td;
3887 int num_tds, trbs_per_td;
3888 struct xhci_generic_trb *start_trb;
3889 bool first_trb;
3890 int start_cycle;
3891 u32 field, length_field;
3892 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3893 u64 start_addr, addr;
3894 int i, j;
Andiry Xu47cbf692010-12-20 14:49:48 +08003895 bool more_trbs_coming;
Lu Baolu79b80942015-08-06 19:24:00 +03003896 struct xhci_virt_ep *xep;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003897 int frame_id;
Andiry Xu04e51902010-07-22 15:23:39 -07003898
Lu Baolu79b80942015-08-06 19:24:00 +03003899 xep = &xhci->devs[slot_id]->eps[ep_index];
Andiry Xu04e51902010-07-22 15:23:39 -07003900 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3901
3902 num_tds = urb->number_of_packets;
3903 if (num_tds < 1) {
3904 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3905 return -EINVAL;
3906 }
Andiry Xu04e51902010-07-22 15:23:39 -07003907 start_addr = (u64) urb->transfer_dma;
3908 start_trb = &ep_ring->enqueue->generic;
3909 start_cycle = ep_ring->cycle_state;
3910
Sarah Sharp522989a2011-07-29 12:44:32 -07003911 urb_priv = urb->hcpriv;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003912 /* Queue the TRBs for each TD, even if they are zero-length */
Andiry Xu04e51902010-07-22 15:23:39 -07003913 for (i = 0; i < num_tds; i++) {
Mathias Nyman09c352e2016-02-12 16:40:17 +02003914 unsigned int total_pkt_count, max_pkt;
3915 unsigned int burst_count, last_burst_pkt_count;
3916 u32 sia_frame_id;
Andiry Xu04e51902010-07-22 15:23:39 -07003917
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003918 first_trb = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003919 running_total = 0;
3920 addr = start_addr + urb->iso_frame_desc[i].offset;
3921 td_len = urb->iso_frame_desc[i].length;
3922 td_remain_len = td_len;
Felipe Balbi734d3dd2016-09-28 13:46:37 +03003923 max_pkt = usb_endpoint_maxp(&urb->ep->desc);
Mathias Nyman09c352e2016-02-12 16:40:17 +02003924 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt);
3925
Sarah Sharp48df4a62011-08-12 10:23:01 -07003926 /* A zero-length transfer still involves at least one packet. */
Mathias Nyman09c352e2016-02-12 16:40:17 +02003927 if (total_pkt_count == 0)
3928 total_pkt_count++;
3929 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count);
3930 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci,
3931 urb, total_pkt_count);
Andiry Xu04e51902010-07-22 15:23:39 -07003932
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003933 trbs_per_td = count_isoc_trbs_needed(urb, i);
Andiry Xu04e51902010-07-22 15:23:39 -07003934
3935 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003936 urb->stream_id, trbs_per_td, urb, i, mem_flags);
Sarah Sharp522989a2011-07-29 12:44:32 -07003937 if (ret < 0) {
3938 if (i == 0)
3939 return ret;
3940 goto cleanup;
3941 }
Mathias Nyman7e64b032017-01-23 14:20:26 +02003942 td = &urb_priv->td[i];
Mathias Nyman55f61532021-01-29 15:00:28 +02003943 td->num_trbs = trbs_per_td;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003944 /* use SIA as default, if frame id is used overwrite it */
3945 sia_frame_id = TRB_SIA;
3946 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3947 HCC_CFC(xhci->hcc_params)) {
3948 frame_id = xhci_get_isoc_frame_id(xhci, urb, i);
3949 if (frame_id >= 0)
3950 sia_frame_id = TRB_FRAME_ID(frame_id);
3951 }
3952 /*
3953 * Set isoc specific data for the first TRB in a TD.
3954 * Prevent HW from getting the TRBs by keeping the cycle state
3955 * inverted in the first TDs isoc TRB.
3956 */
Mathias Nyman2f6d3b62016-02-12 16:40:18 +02003957 field = TRB_TYPE(TRB_ISOC) |
Mathias Nyman09c352e2016-02-12 16:40:17 +02003958 TRB_TLBPC(last_burst_pkt_count) |
3959 sia_frame_id |
3960 (i ? ep_ring->cycle_state : !start_cycle);
3961
Mathias Nyman2f6d3b62016-02-12 16:40:18 +02003962 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */
3963 if (!xep->use_extended_tbc)
3964 field |= TRB_TBC(burst_count);
3965
Mathias Nyman09c352e2016-02-12 16:40:17 +02003966 /* fill the rest of the TRB fields, and remaining normal TRBs */
Andiry Xu04e51902010-07-22 15:23:39 -07003967 for (j = 0; j < trbs_per_td; j++) {
3968 u32 remainder = 0;
Andiry Xu04e51902010-07-22 15:23:39 -07003969
Mathias Nyman09c352e2016-02-12 16:40:17 +02003970 /* only first TRB is isoc, overwrite otherwise */
3971 if (!first_trb)
3972 field = TRB_TYPE(TRB_NORMAL) |
3973 ep_ring->cycle_state;
Andiry Xu04e51902010-07-22 15:23:39 -07003974
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003975 /* Only set interrupt on short packet for IN EPs */
3976 if (usb_urb_dir_in(urb))
3977 field |= TRB_ISP;
3978
Mathias Nyman09c352e2016-02-12 16:40:17 +02003979 /* Set the chain bit for all except the last TRB */
Andiry Xu04e51902010-07-22 15:23:39 -07003980 if (j < trbs_per_td - 1) {
Andiry Xu47cbf692010-12-20 14:49:48 +08003981 more_trbs_coming = true;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003982 field |= TRB_CHAIN;
Andiry Xu04e51902010-07-22 15:23:39 -07003983 } else {
Mathias Nyman09c352e2016-02-12 16:40:17 +02003984 more_trbs_coming = false;
Andiry Xu04e51902010-07-22 15:23:39 -07003985 td->last_trb = ep_ring->enqueue;
Mathias Nyman55f61532021-01-29 15:00:28 +02003986 td->last_trb_seg = ep_ring->enq_seg;
Andiry Xu04e51902010-07-22 15:23:39 -07003987 field |= TRB_IOC;
Mathias Nymanedc649a2020-09-18 16:17:50 +03003988 if (trb_block_event_intr(xhci, num_tds, i))
Mathias Nyman09c352e2016-02-12 16:40:17 +02003989 field |= TRB_BEI;
Andiry Xu04e51902010-07-22 15:23:39 -07003990 }
Andiry Xu04e51902010-07-22 15:23:39 -07003991 /* Calculate TRB length */
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003992 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
Andiry Xu04e51902010-07-22 15:23:39 -07003993 if (trb_buff_len > td_remain_len)
3994 trb_buff_len = td_remain_len;
3995
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003996 /* Set the TRB length, TD size, & interrupter fields. */
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003997 remainder = xhci_td_remainder(xhci, running_total,
3998 trb_buff_len, td_len,
Mathias Nyman124c3932016-06-21 10:57:59 +03003999 urb, more_trbs_coming);
Mathias Nymanc840d6c2015-10-09 13:30:08 +03004000
Andiry Xu04e51902010-07-22 15:23:39 -07004001 length_field = TRB_LEN(trb_buff_len) |
Andiry Xu04e51902010-07-22 15:23:39 -07004002 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07004003
Mathias Nyman2f6d3b62016-02-12 16:40:18 +02004004 /* xhci 1.1 with ETE uses TD Size field for TBC */
4005 if (first_trb && xep->use_extended_tbc)
4006 length_field |= TRB_TD_SIZE_TBC(burst_count);
4007 else
4008 length_field |= TRB_TD_SIZE(remainder);
4009 first_trb = false;
4010
Andiry Xu3b72fca2012-03-05 17:49:32 +08004011 queue_trb(xhci, ep_ring, more_trbs_coming,
Andiry Xu04e51902010-07-22 15:23:39 -07004012 lower_32_bits(addr),
4013 upper_32_bits(addr),
4014 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07004015 field);
Andiry Xu04e51902010-07-22 15:23:39 -07004016 running_total += trb_buff_len;
4017
4018 addr += trb_buff_len;
4019 td_remain_len -= trb_buff_len;
4020 }
4021
4022 /* Check TD length */
4023 if (running_total != td_len) {
4024 xhci_err(xhci, "ISOC TD length unmatch\n");
Andiry Xucf840552012-01-18 17:47:12 +08004025 ret = -EINVAL;
4026 goto cleanup;
Andiry Xu04e51902010-07-22 15:23:39 -07004027 }
4028 }
4029
Lu Baolu79b80942015-08-06 19:24:00 +03004030 /* store the next frame id */
4031 if (HCC_CFC(xhci->hcc_params))
4032 xep->next_frame_id = urb->start_frame + num_tds * urb->interval;
4033
Andiry Xuc41136b2011-03-22 17:08:14 +08004034 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
4035 if (xhci->quirks & XHCI_AMD_PLL_FIX)
4036 usb_amd_quirk_pll_disable();
4037 }
4038 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
4039
Andiry Xue1eab2e2011-01-04 16:30:39 -08004040 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
4041 start_cycle, start_trb);
Andiry Xu04e51902010-07-22 15:23:39 -07004042 return 0;
Sarah Sharp522989a2011-07-29 12:44:32 -07004043cleanup:
4044 /* Clean up a partially enqueued isoc transfer. */
4045
4046 for (i--; i >= 0; i--)
Mathias Nyman7e64b032017-01-23 14:20:26 +02004047 list_del_init(&urb_priv->td[i].td_list);
Sarah Sharp522989a2011-07-29 12:44:32 -07004048
4049 /* Use the first TD as a temporary variable to turn the TDs we've queued
4050 * into No-ops with a software-owned cycle bit. That way the hardware
4051 * won't accidentally start executing bogus TDs when we partially
4052 * overwrite them. td->first_trb and td->start_seg are already set.
4053 */
Mathias Nyman7e64b032017-01-23 14:20:26 +02004054 urb_priv->td[0].last_trb = ep_ring->enqueue;
Sarah Sharp522989a2011-07-29 12:44:32 -07004055 /* Every TRB except the first & last will have its cycle bit flipped. */
Mathias Nyman7e64b032017-01-23 14:20:26 +02004056 td_to_noop(xhci, ep_ring, &urb_priv->td[0], true);
Sarah Sharp522989a2011-07-29 12:44:32 -07004057
4058 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
Mathias Nyman7e64b032017-01-23 14:20:26 +02004059 ep_ring->enqueue = urb_priv->td[0].first_trb;
4060 ep_ring->enq_seg = urb_priv->td[0].start_seg;
Sarah Sharp522989a2011-07-29 12:44:32 -07004061 ep_ring->cycle_state = start_cycle;
Andiry Xub008df62012-03-05 17:49:34 +08004062 ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp;
Sarah Sharp522989a2011-07-29 12:44:32 -07004063 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
4064 return ret;
Andiry Xu04e51902010-07-22 15:23:39 -07004065}
4066
4067/*
4068 * Check transfer ring to guarantee there is enough room for the urb.
4069 * Update ISO URB start_frame and interval.
Lu Baolu79b80942015-08-06 19:24:00 +03004070 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to
4071 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or
4072 * Contiguous Frame ID is not supported by HC.
Andiry Xu04e51902010-07-22 15:23:39 -07004073 */
4074int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
4075 struct urb *urb, int slot_id, unsigned int ep_index)
4076{
4077 struct xhci_virt_device *xdev;
4078 struct xhci_ring *ep_ring;
4079 struct xhci_ep_ctx *ep_ctx;
4080 int start_frame;
Andiry Xu04e51902010-07-22 15:23:39 -07004081 int num_tds, num_trbs, i;
4082 int ret;
Lu Baolu79b80942015-08-06 19:24:00 +03004083 struct xhci_virt_ep *xep;
4084 int ist;
Andiry Xu04e51902010-07-22 15:23:39 -07004085
4086 xdev = xhci->devs[slot_id];
Lu Baolu79b80942015-08-06 19:24:00 +03004087 xep = &xhci->devs[slot_id]->eps[ep_index];
Andiry Xu04e51902010-07-22 15:23:39 -07004088 ep_ring = xdev->eps[ep_index].ring;
4089 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
4090
4091 num_trbs = 0;
4092 num_tds = urb->number_of_packets;
4093 for (i = 0; i < num_tds; i++)
Alexandr Ivanovd2510342016-04-22 13:17:09 +03004094 num_trbs += count_isoc_trbs_needed(urb, i);
Andiry Xu04e51902010-07-22 15:23:39 -07004095
4096 /* Check the ring to guarantee there is enough room for the whole urb.
4097 * Do not insert any td of the urb to the ring if the check failed.
4098 */
Mathias Nyman5071e6b2016-11-11 15:13:28 +02004099 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx),
Andiry Xu3b72fca2012-03-05 17:49:32 +08004100 num_trbs, mem_flags);
Andiry Xu04e51902010-07-22 15:23:39 -07004101 if (ret)
4102 return ret;
4103
Lu Baolu79b80942015-08-06 19:24:00 +03004104 /*
4105 * Check interval value. This should be done before we start to
4106 * calculate the start frame value.
4107 */
Alexandr Ivanov78140152016-04-22 13:17:11 +03004108 check_interval(xhci, urb, ep_ctx);
Lu Baolu79b80942015-08-06 19:24:00 +03004109
4110 /* Calculate the start frame and put it in urb->start_frame. */
Lu Baolu42df7212015-11-18 10:48:21 +02004111 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) {
Mathias Nyman5071e6b2016-11-11 15:13:28 +02004112 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) {
Lu Baolu42df7212015-11-18 10:48:21 +02004113 urb->start_frame = xep->next_frame_id;
4114 goto skip_start_over;
4115 }
Lu Baolu79b80942015-08-06 19:24:00 +03004116 }
4117
4118 start_frame = readl(&xhci->run_regs->microframe_index);
4119 start_frame &= 0x3fff;
4120 /*
4121 * Round up to the next frame and consider the time before trb really
4122 * gets scheduled by hardare.
4123 */
4124 ist = HCS_IST(xhci->hcs_params2) & 0x7;
4125 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
4126 ist <<= 3;
4127 start_frame += ist + XHCI_CFC_DELAY;
4128 start_frame = roundup(start_frame, 8);
4129
4130 /*
4131 * Round up to the next ESIT (Endpoint Service Interval Time) if ESIT
4132 * is greate than 8 microframes.
4133 */
4134 if (urb->dev->speed == USB_SPEED_LOW ||
4135 urb->dev->speed == USB_SPEED_FULL) {
4136 start_frame = roundup(start_frame, urb->interval << 3);
4137 urb->start_frame = start_frame >> 3;
4138 } else {
4139 start_frame = roundup(start_frame, urb->interval);
4140 urb->start_frame = start_frame;
4141 }
4142
4143skip_start_over:
Andiry Xub008df62012-03-05 17:49:34 +08004144 ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free;
4145
Dan Carpenter3fc82062012-03-28 10:30:26 +03004146 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
Andiry Xu04e51902010-07-22 15:23:39 -07004147}
4148
Sarah Sharpd0e96f52009-04-27 19:58:01 -07004149/**** Command Ring Operations ****/
4150
Sarah Sharp913a8a32009-09-04 10:53:13 -07004151/* Generic function for queueing a command TRB on the command ring.
4152 * Check to make sure there's room on the command ring for one command TRB.
4153 * Also check that there's room reserved for commands that must not fail.
4154 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
4155 * then only check for the number of reserved spots.
4156 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
4157 * because the command event handler may want to resubmit a failed command.
4158 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004159static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
4160 u32 field1, u32 field2,
4161 u32 field3, u32 field4, bool command_must_succeed)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07004162{
Sarah Sharp913a8a32009-09-04 10:53:13 -07004163 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
Sarah Sharpd1dc9082010-07-09 17:08:38 +02004164 int ret;
Roger Quadrosad6b1d92015-05-29 17:01:49 +03004165
Mathias Nyman98d74f92016-04-08 16:25:10 +03004166 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
4167 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Roger Quadrosad6b1d92015-05-29 17:01:49 +03004168 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n");
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03004169 return -ESHUTDOWN;
Roger Quadrosad6b1d92015-05-29 17:01:49 +03004170 }
Sarah Sharpd1dc9082010-07-09 17:08:38 +02004171
Sarah Sharp913a8a32009-09-04 10:53:13 -07004172 if (!command_must_succeed)
4173 reserved_trbs++;
4174
Sarah Sharpd1dc9082010-07-09 17:08:38 +02004175 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
Andiry Xu3b72fca2012-03-05 17:49:32 +08004176 reserved_trbs, GFP_ATOMIC);
Sarah Sharpd1dc9082010-07-09 17:08:38 +02004177 if (ret < 0) {
4178 xhci_err(xhci, "ERR: No room for command on command ring\n");
Sarah Sharp913a8a32009-09-04 10:53:13 -07004179 if (command_must_succeed)
4180 xhci_err(xhci, "ERR: Reserved TRB counting for "
4181 "unfailable commands failed.\n");
Sarah Sharpd1dc9082010-07-09 17:08:38 +02004182 return ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07004183 }
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03004184
4185 cmd->command_trb = xhci->cmd_ring->enqueue;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004186
Mathias Nymanc311e392014-05-08 19:26:03 +03004187 /* if there are no other commands queued we start the timeout timer */
Lu Baoludaa47f22017-01-23 14:20:02 +02004188 if (list_empty(&xhci->cmd_list)) {
Mathias Nymanc311e392014-05-08 19:26:03 +03004189 xhci->current_cmd = cmd;
OGAWA Hirofumicb4d5ce2017-01-03 18:28:50 +02004190 xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT);
Mathias Nymanc311e392014-05-08 19:26:03 +03004191 }
4192
Lu Baoludaa47f22017-01-23 14:20:02 +02004193 list_add_tail(&cmd->cmd_list, &xhci->cmd_list);
4194
Andiry Xu3b72fca2012-03-05 17:49:32 +08004195 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
4196 field4 | xhci->cmd_ring->cycle_state);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07004197 return 0;
4198}
4199
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004200/* Queue a slot enable or disable request on the command ring */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004201int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
4202 u32 trb_type, u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004203{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004204 return queue_command(xhci, cmd, 0, 0, 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07004205 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004206}
4207
4208/* Queue an address device command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004209int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
4210 dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004211{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004212 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
Sarah Sharp8e595a52009-07-27 12:03:31 -07004213 upper_32_bits(in_ctx_ptr), 0,
Dan Williams48fc7db2013-12-05 17:07:27 -08004214 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id)
4215 | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004216}
Sarah Sharpf94e01862009-04-27 19:58:38 -07004217
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004218int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
Sarah Sharp02386342010-05-24 13:25:28 -07004219 u32 field1, u32 field2, u32 field3, u32 field4)
4220{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004221 return queue_command(xhci, cmd, field1, field2, field3, field4, false);
Sarah Sharp02386342010-05-24 13:25:28 -07004222}
4223
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08004224/* Queue a reset device command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004225int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
4226 u32 slot_id)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08004227{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004228 return queue_command(xhci, cmd, 0, 0, 0,
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08004229 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
4230 false);
4231}
4232
Sarah Sharpf94e01862009-04-27 19:58:38 -07004233/* Queue a configure endpoint command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004234int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
4235 struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
Sarah Sharp913a8a32009-09-04 10:53:13 -07004236 u32 slot_id, bool command_must_succeed)
Sarah Sharpf94e01862009-04-27 19:58:38 -07004237{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004238 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
Sarah Sharp8e595a52009-07-27 12:03:31 -07004239 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07004240 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
4241 command_must_succeed);
Sarah Sharpf94e01862009-04-27 19:58:38 -07004242}
Sarah Sharpae636742009-04-29 19:02:31 -07004243
Sarah Sharpf2217e82009-08-07 14:04:43 -07004244/* Queue an evaluate context command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004245int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
4246 dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07004247{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004248 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
Sarah Sharpf2217e82009-08-07 14:04:43 -07004249 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07004250 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
Sarah Sharp4b266542012-05-07 15:34:26 -07004251 command_must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07004252}
4253
Andiry Xube88fe42010-10-14 07:22:57 -07004254/*
4255 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
4256 * activity on an endpoint that is about to be suspended.
4257 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004258int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
4259 int slot_id, unsigned int ep_index, int suspend)
Sarah Sharpae636742009-04-29 19:02:31 -07004260{
4261 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4262 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
4263 u32 type = TRB_TYPE(TRB_STOP_RING);
Andiry Xube88fe42010-10-14 07:22:57 -07004264 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
Sarah Sharpae636742009-04-29 19:02:31 -07004265
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004266 return queue_command(xhci, cmd, 0, 0, 0,
Andiry Xube88fe42010-10-14 07:22:57 -07004267 trb_slot_id | trb_ep_index | type | trb_suspend, false);
Sarah Sharpae636742009-04-29 19:02:31 -07004268}
4269
Hans de Goeded3a43e62014-08-20 16:41:53 +03004270/* Set Transfer Ring Dequeue Pointer command */
4271void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
4272 unsigned int slot_id, unsigned int ep_index,
Hans de Goeded3a43e62014-08-20 16:41:53 +03004273 struct xhci_dequeue_state *deq_state)
Sarah Sharpae636742009-04-29 19:02:31 -07004274{
4275 dma_addr_t addr;
4276 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4277 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
Mathias Nyman87907362017-06-02 16:36:23 +03004278 u32 trb_stream_id = STREAM_ID_FOR_TRB(deq_state->stream_id);
Hans de Goede95241db2013-10-04 00:29:48 +02004279 u32 trb_sct = 0;
Sarah Sharpae636742009-04-29 19:02:31 -07004280 u32 type = TRB_TYPE(TRB_SET_DEQ);
Sarah Sharpbf161e82011-02-23 15:46:42 -08004281 struct xhci_virt_ep *ep;
Hans de Goede1e3452e2014-08-20 16:41:52 +03004282 struct xhci_command *cmd;
4283 int ret;
Sarah Sharpae636742009-04-29 19:02:31 -07004284
Hans de Goeded3a43e62014-08-20 16:41:53 +03004285 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
4286 "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), new deq ptr = %p (0x%llx dma), new cycle = %u",
4287 deq_state->new_deq_seg,
4288 (unsigned long long)deq_state->new_deq_seg->dma,
4289 deq_state->new_deq_ptr,
4290 (unsigned long long)xhci_trb_virt_to_dma(
4291 deq_state->new_deq_seg, deq_state->new_deq_ptr),
4292 deq_state->new_cycle_state);
4293
4294 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
4295 deq_state->new_deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07004296 if (addr == 0) {
Sarah Sharpae636742009-04-29 19:02:31 -07004297 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07004298 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
Hans de Goeded3a43e62014-08-20 16:41:53 +03004299 deq_state->new_deq_seg, deq_state->new_deq_ptr);
4300 return;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07004301 }
Sarah Sharpbf161e82011-02-23 15:46:42 -08004302 ep = &xhci->devs[slot_id]->eps[ep_index];
4303 if ((ep->ep_state & SET_DEQ_PENDING)) {
4304 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
4305 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
Hans de Goeded3a43e62014-08-20 16:41:53 +03004306 return;
Sarah Sharpbf161e82011-02-23 15:46:42 -08004307 }
Hans de Goede1e3452e2014-08-20 16:41:52 +03004308
4309 /* This function gets called from contexts where it cannot sleep */
Mathias Nyman103afda2017-12-08 17:59:08 +02004310 cmd = xhci_alloc_command(xhci, false, GFP_ATOMIC);
Lu Baolu74e0b562017-04-07 17:57:05 +03004311 if (!cmd)
Hans de Goeded3a43e62014-08-20 16:41:53 +03004312 return;
Hans de Goede1e3452e2014-08-20 16:41:52 +03004313
Hans de Goeded3a43e62014-08-20 16:41:53 +03004314 ep->queued_deq_seg = deq_state->new_deq_seg;
4315 ep->queued_deq_ptr = deq_state->new_deq_ptr;
Mathias Nyman87907362017-06-02 16:36:23 +03004316 if (deq_state->stream_id)
Hans de Goede95241db2013-10-04 00:29:48 +02004317 trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
Hans de Goede1e3452e2014-08-20 16:41:52 +03004318 ret = queue_command(xhci, cmd,
Hans de Goeded3a43e62014-08-20 16:41:53 +03004319 lower_32_bits(addr) | trb_sct | deq_state->new_cycle_state,
4320 upper_32_bits(addr), trb_stream_id,
4321 trb_slot_id | trb_ep_index | type, false);
Hans de Goede1e3452e2014-08-20 16:41:52 +03004322 if (ret < 0) {
4323 xhci_free_command(xhci, cmd);
Hans de Goeded3a43e62014-08-20 16:41:53 +03004324 return;
Hans de Goede1e3452e2014-08-20 16:41:52 +03004325 }
4326
Hans de Goeded3a43e62014-08-20 16:41:53 +03004327 /* Stop the TD queueing code from ringing the doorbell until
4328 * this command completes. The HC won't set the dequeue pointer
4329 * if the ring is running, and ringing the doorbell starts the
4330 * ring running.
4331 */
4332 ep->ep_state |= SET_DEQ_PENDING;
Sarah Sharpae636742009-04-29 19:02:31 -07004333}
Sarah Sharpa1587d92009-07-27 12:03:15 -07004334
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004335int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
Mathias Nyman21749142017-06-15 11:55:44 +03004336 int slot_id, unsigned int ep_index,
4337 enum xhci_ep_reset_type reset_type)
Sarah Sharpa1587d92009-07-27 12:03:15 -07004338{
4339 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4340 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
4341 u32 type = TRB_TYPE(TRB_RESET_EP);
4342
Mathias Nyman21749142017-06-15 11:55:44 +03004343 if (reset_type == EP_SOFT_RESET)
4344 type |= TRB_TSP;
4345
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004346 return queue_command(xhci, cmd, 0, 0, 0,
4347 trb_slot_id | trb_ep_index | type, false);
Sarah Sharpa1587d92009-07-27 12:03:15 -07004348}