Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xHCI host controller driver |
| 4 | * |
| 5 | * Copyright (C) 2008 Intel Corp. |
| 6 | * |
| 7 | * Author: Sarah Sharp |
| 8 | * Some code borrowed from the Linux EHCI driver. |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Ring initialization rules: |
| 13 | * 1. Each segment is initialized to zero, except for link TRBs. |
| 14 | * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or |
| 15 | * Consumer Cycle State (CCS), depending on ring function. |
| 16 | * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment. |
| 17 | * |
| 18 | * Ring behavior rules: |
| 19 | * 1. A ring is empty if enqueue == dequeue. This means there will always be at |
| 20 | * least one free TRB in the ring. This is useful if you want to turn that |
| 21 | * into a link TRB and expand the ring. |
| 22 | * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a |
| 23 | * link TRB, then load the pointer with the address in the link TRB. If the |
| 24 | * link TRB had its toggle bit set, you may need to update the ring cycle |
| 25 | * state (see cycle bit rules). You may have to do this multiple times |
| 26 | * until you reach a non-link TRB. |
| 27 | * 3. A ring is full if enqueue++ (for the definition of increment above) |
| 28 | * equals the dequeue pointer. |
| 29 | * |
| 30 | * Cycle bit rules: |
| 31 | * 1. When a consumer increments a dequeue pointer and encounters a toggle bit |
| 32 | * in a link TRB, it must toggle the ring cycle state. |
| 33 | * 2. When a producer increments an enqueue pointer and encounters a toggle bit |
| 34 | * in a link TRB, it must toggle the ring cycle state. |
| 35 | * |
| 36 | * Producer rules: |
| 37 | * 1. Check if ring is full before you enqueue. |
| 38 | * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing. |
| 39 | * Update enqueue pointer between each write (which may update the ring |
| 40 | * cycle state). |
| 41 | * 3. Notify consumer. If SW is producer, it rings the doorbell for command |
| 42 | * and endpoint rings. If HC is the producer for the event ring, |
| 43 | * and it generates an interrupt according to interrupt modulation rules. |
| 44 | * |
| 45 | * Consumer rules: |
| 46 | * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state, |
| 47 | * the TRB is owned by the consumer. |
| 48 | * 2. Update dequeue pointer (which may update the ring cycle state) and |
| 49 | * continue processing TRBs until you reach a TRB which is not owned by you. |
| 50 | * 3. Notify the producer. SW is the consumer for the event ring, and it |
| 51 | * updates event ring dequeue pointer. HC is the consumer for the command and |
| 52 | * endpoint rings; it generates events on the event ring for these. |
| 53 | */ |
| 54 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 55 | #include <linux/scatterlist.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 56 | #include <linux/slab.h> |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 57 | #include <linux/dma-mapping.h> |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 58 | #include "xhci.h" |
Xenia Ragiadakou | 3a7fa5b | 2013-07-31 07:35:27 +0300 | [diff] [blame] | 59 | #include "xhci-trace.h" |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 60 | #include "xhci-mtk.h" |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA |
| 64 | * address of the TRB. |
| 65 | */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 66 | dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 67 | union xhci_trb *trb) |
| 68 | { |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 69 | unsigned long segment_offset; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 70 | |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 71 | if (!seg || !trb || trb < seg->trbs) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 72 | return 0; |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 73 | /* offset in TRBs */ |
| 74 | segment_offset = trb - seg->trbs; |
Mathias Nyman | 7895086 | 2015-08-03 16:07:48 +0300 | [diff] [blame] | 75 | if (segment_offset >= TRBS_PER_SEGMENT) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 76 | return 0; |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 77 | return seg->dma + (segment_offset * sizeof(*trb)); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Mathias Nyman | 0ce5749 | 2016-11-11 15:13:14 +0200 | [diff] [blame] | 80 | static bool trb_is_noop(union xhci_trb *trb) |
| 81 | { |
| 82 | return TRB_TYPE_NOOP_LE32(trb->generic.field[3]); |
| 83 | } |
| 84 | |
Mathias Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 85 | static bool trb_is_link(union xhci_trb *trb) |
| 86 | { |
| 87 | return TRB_TYPE_LINK_LE32(trb->link.control); |
| 88 | } |
| 89 | |
Mathias Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 90 | static bool last_trb_on_seg(struct xhci_segment *seg, union xhci_trb *trb) |
| 91 | { |
| 92 | return trb == &seg->trbs[TRBS_PER_SEGMENT - 1]; |
| 93 | } |
| 94 | |
| 95 | static bool last_trb_on_ring(struct xhci_ring *ring, |
| 96 | struct xhci_segment *seg, union xhci_trb *trb) |
| 97 | { |
| 98 | return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg); |
| 99 | } |
| 100 | |
Mathias Nyman | d0c77d8 | 2016-06-21 10:58:07 +0300 | [diff] [blame] | 101 | static bool link_trb_toggles_cycle(union xhci_trb *trb) |
| 102 | { |
| 103 | return le32_to_cpu(trb->link.control) & LINK_TOGGLE; |
| 104 | } |
| 105 | |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 106 | static bool last_td_in_urb(struct xhci_td *td) |
| 107 | { |
| 108 | struct urb_priv *urb_priv = td->urb->hcpriv; |
| 109 | |
Mathias Nyman | 9ef7fbb | 2017-01-23 14:20:25 +0200 | [diff] [blame] | 110 | return urb_priv->num_tds_done == urb_priv->num_tds; |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void inc_td_cnt(struct urb *urb) |
| 114 | { |
| 115 | struct urb_priv *urb_priv = urb->hcpriv; |
| 116 | |
Mathias Nyman | 9ef7fbb | 2017-01-23 14:20:25 +0200 | [diff] [blame] | 117 | urb_priv->num_tds_done++; |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Mathias Nyman | ae1e3f0 | 2017-01-23 14:20:15 +0200 | [diff] [blame] | 120 | static void trb_to_noop(union xhci_trb *trb, u32 noop_type) |
| 121 | { |
| 122 | if (trb_is_link(trb)) { |
| 123 | /* unchain chained link TRBs */ |
| 124 | trb->link.control &= cpu_to_le32(~TRB_CHAIN); |
| 125 | } else { |
| 126 | trb->generic.field[0] = 0; |
| 127 | trb->generic.field[1] = 0; |
| 128 | trb->generic.field[2] = 0; |
| 129 | /* Preserve only the cycle bit of this TRB */ |
| 130 | trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); |
| 131 | trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type)); |
| 132 | } |
| 133 | } |
| 134 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 135 | /* Updates trb to point to the next TRB in the ring, and updates seg if the next |
| 136 | * TRB is in a new segment. This does not skip over link TRBs, and it does not |
| 137 | * effect the ring dequeue or enqueue pointers. |
| 138 | */ |
| 139 | static void next_trb(struct xhci_hcd *xhci, |
| 140 | struct xhci_ring *ring, |
| 141 | struct xhci_segment **seg, |
| 142 | union xhci_trb **trb) |
| 143 | { |
Mathias Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 144 | if (trb_is_link(*trb)) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 145 | *seg = (*seg)->next; |
| 146 | *trb = ((*seg)->trbs); |
| 147 | } else { |
John Youn | a1669b2 | 2010-08-09 13:56:11 -0700 | [diff] [blame] | 148 | (*trb)++; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 152 | /* |
| 153 | * See Cycle bit rules. SW is the consumer for the event ring only. |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 154 | */ |
Lu Baolu | 67d2ea9 | 2017-12-08 17:59:09 +0200 | [diff] [blame] | 155 | void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 156 | { |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 157 | unsigned int link_trb_count = 0; |
| 158 | |
Mathias Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 159 | /* 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 Sharp | 50d0206 | 2012-07-26 12:03:59 -0700 | [diff] [blame] | 162 | ring->dequeue++; |
Adam Wallis | 49d5b05 | 2017-10-05 11:21:47 +0300 | [diff] [blame] | 163 | goto out; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 164 | } |
Mathias Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 165 | 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 Wallis | 49d5b05 | 2017-10-05 11:21:47 +0300 | [diff] [blame] | 169 | goto out; |
Mathias Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* All other rings have link trbs */ |
| 173 | if (!trb_is_link(ring->dequeue)) { |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 174 | 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 Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 180 | } |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 181 | |
Mathias Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 182 | while (trb_is_link(ring->dequeue)) { |
| 183 | ring->deq_seg = ring->deq_seg->next; |
| 184 | ring->dequeue = ring->deq_seg->trbs; |
Lu Baolu | b2d6edb | 2017-04-07 17:57:02 +0300 | [diff] [blame] | 185 | |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 186 | if (link_trb_count++ > ring->num_segs) { |
| 187 | xhci_warn(xhci, "Ring is an endless link TRB loop\n"); |
| 188 | break; |
| 189 | } |
| 190 | } |
Adam Wallis | 49d5b05 | 2017-10-05 11:21:47 +0300 | [diff] [blame] | 191 | out: |
Lu Baolu | b2d6edb | 2017-04-07 17:57:02 +0300 | [diff] [blame] | 192 | trace_xhci_inc_deq(ring); |
| 193 | |
Mathias Nyman | bd5e67f | 2016-06-21 10:58:05 +0300 | [diff] [blame] | 194 | return; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
| 198 | * See Cycle bit rules. SW is the consumer for the event ring only. |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 199 | * |
| 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 Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 206 | * 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 Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 209 | * |
| 210 | * @more_trbs_coming: Will you enqueue more TRBs before calling |
| 211 | * prepare_transfer()? |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 212 | */ |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 213 | static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 214 | bool more_trbs_coming) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 215 | { |
| 216 | u32 chain; |
| 217 | union xhci_trb *next; |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 218 | unsigned int link_trb_count = 0; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 219 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 220 | chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 221 | /* If this is not event ring, there is one less usable TRB */ |
Mathias Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 222 | if (!trb_is_link(ring->enqueue)) |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 223 | ring->num_trbs_free--; |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 224 | |
| 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 Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 230 | next = ++(ring->enqueue); |
| 231 | |
Mathias Nyman | 2251198 | 2016-06-21 10:58:03 +0300 | [diff] [blame] | 232 | /* Update the dequeue pointer further if that was a link TRB */ |
Mathias Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 233 | while (trb_is_link(next)) { |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 234 | |
Mathias Nyman | 2251198 | 2016-06-21 10:58:03 +0300 | [diff] [blame] | 235 | /* |
| 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 Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 244 | |
Mathias Nyman | 2251198 | 2016-06-21 10:58:03 +0300 | [diff] [blame] | 245 | /* 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 Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 254 | } |
Mathias Nyman | 2251198 | 2016-06-21 10:58:03 +0300 | [diff] [blame] | 255 | /* 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 Nyman | d0c77d8 | 2016-06-21 10:58:07 +0300 | [diff] [blame] | 260 | if (link_trb_toggles_cycle(next)) |
Mathias Nyman | 2251198 | 2016-06-21 10:58:03 +0300 | [diff] [blame] | 261 | ring->cycle_state ^= 1; |
| 262 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 263 | ring->enq_seg = ring->enq_seg->next; |
| 264 | ring->enqueue = ring->enq_seg->trbs; |
| 265 | next = ring->enqueue; |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 266 | |
| 267 | if (link_trb_count++ > ring->num_segs) { |
| 268 | xhci_warn(xhci, "%s: Ring link TRB loop\n", __func__); |
| 269 | break; |
| 270 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 271 | } |
Lu Baolu | b2d6edb | 2017-04-07 17:57:02 +0300 | [diff] [blame] | 272 | |
| 273 | trace_xhci_inc_enq(ring); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
Andiry Xu | 085deb1 | 2012-03-05 17:49:40 +0800 | [diff] [blame] | 277 | * 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 Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 279 | */ |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 280 | static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 281 | unsigned int num_trbs) |
| 282 | { |
Andiry Xu | 085deb1 | 2012-03-05 17:49:40 +0800 | [diff] [blame] | 283 | int num_trbs_in_deq_seg; |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 284 | |
Andiry Xu | 085deb1 | 2012-03-05 17:49:40 +0800 | [diff] [blame] | 285 | 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 Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 297 | /* Ring the host controller doorbell after placing a command on the ring */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 298 | void xhci_ring_cmd_db(struct xhci_hcd *xhci) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 299 | { |
Elric Fu | c181bc5 | 2012-06-27 16:30:57 +0800 | [diff] [blame] | 300 | if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) |
| 301 | return; |
| 302 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 303 | xhci_dbg(xhci, "// Ding dong!\n"); |
Mathias Nyman | 58b9d71 | 2019-11-15 18:50:01 +0200 | [diff] [blame] | 304 | |
| 305 | trace_xhci_ring_host_doorbell(0, DB_VALUE_HOST); |
| 306 | |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 307 | writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 308 | /* Flush PCI posted writes */ |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 309 | readl(&xhci->dba->doorbell[0]); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 310 | } |
| 311 | |
OGAWA Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 312 | static 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 Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 317 | static 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 | */ |
| 328 | static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci, |
| 329 | struct xhci_command *cur_cmd) |
| 330 | { |
| 331 | struct xhci_command *i_cmd; |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 332 | |
| 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 Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 336 | if (i_cmd->status != COMP_COMMAND_ABORTED) |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 337 | continue; |
| 338 | |
Mathias Nyman | 604d02a | 2017-05-17 18:32:05 +0300 | [diff] [blame] | 339 | i_cmd->status = COMP_COMMAND_RING_STOPPED; |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 340 | |
| 341 | xhci_dbg(xhci, "Turn aborted command %p to no-op\n", |
| 342 | i_cmd->command_trb); |
Mathias Nyman | 5278204 | 2017-01-23 14:20:16 +0200 | [diff] [blame] | 343 | |
| 344 | trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP); |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 345 | |
| 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 */ |
| 364 | static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags) |
Elric Fu | b92cc66 | 2012-06-27 16:31:12 +0800 | [diff] [blame] | 365 | { |
| 366 | u64 temp_64; |
| 367 | int ret; |
| 368 | |
| 369 | xhci_dbg(xhci, "Abort command ring\n"); |
| 370 | |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 371 | reinit_completion(&xhci->cmd_ring_stop_completion); |
Mathias Nyman | 3425aa0 | 2016-06-01 18:09:08 +0300 | [diff] [blame] | 372 | |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 373 | temp_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); |
Sarah Sharp | 477632d | 2014-01-29 14:02:00 -0800 | [diff] [blame] | 374 | xhci_write_64(xhci, temp_64 | CMD_RING_ABORT, |
| 375 | &xhci->op_regs->cmd_ring); |
Elric Fu | b92cc66 | 2012-06-27 16:31:12 +0800 | [diff] [blame] | 376 | |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 377 | /* 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 Fu | b92cc66 | 2012-06-27 16:31:12 +0800 | [diff] [blame] | 382 | */ |
Lin Wang | dc0b177 | 2015-01-09 16:06:28 +0200 | [diff] [blame] | 383 | ret = xhci_handshake(&xhci->op_regs->cmd_ring, |
Elric Fu | b92cc66 | 2012-06-27 16:31:12 +0800 | [diff] [blame] | 384 | CMD_RING_RUNNING, 0, 5 * 1000 * 1000); |
| 385 | if (ret < 0) { |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 386 | xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret); |
Lu Baolu | 1cc6d86 | 2017-01-23 14:19:55 +0200 | [diff] [blame] | 387 | xhci_halt(xhci); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 388 | xhci_hc_died(xhci); |
| 389 | return ret; |
Elric Fu | b92cc66 | 2012-06-27 16:31:12 +0800 | [diff] [blame] | 390 | } |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 391 | /* |
| 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 Fu | b92cc66 | 2012-06-27 16:31:12 +0800 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 410 | void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 411 | unsigned int slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 412 | unsigned int ep_index, |
| 413 | unsigned int stream_id) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 414 | { |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 415 | __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; |
Matthew Wilcox | 50d64676 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 416 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 417 | unsigned int ep_state = ep->ep_state; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 418 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 419 | /* Don't ring the doorbell for this endpoint if there are pending |
Matthew Wilcox | 50d64676 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 420 | * cancellations because we don't want to interrupt processing. |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 421 | * 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 424 | */ |
Mathias Nyman | 9983a5f | 2017-01-23 14:19:52 +0200 | [diff] [blame] | 425 | if ((ep_state & EP_STOP_CMD_PENDING) || (ep_state & SET_DEQ_PENDING) || |
Jim Lin | ef513be | 2019-06-03 18:53:44 +0800 | [diff] [blame] | 426 | (ep_state & EP_HALTED) || (ep_state & EP_CLEARING_TT)) |
Matthew Wilcox | 50d64676 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 427 | return; |
Mathias Nyman | 58b9d71 | 2019-11-15 18:50:01 +0200 | [diff] [blame] | 428 | |
| 429 | trace_xhci_ring_ep_doorbell(slot_id, DB_VALUE(ep_index, stream_id)); |
| 430 | |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 431 | writel(DB_VALUE(ep_index, stream_id), db_addr); |
Mathias Nyman | b05dadb | 2021-01-29 15:00:31 +0200 | [diff] [blame] | 432 | /* flush the write */ |
| 433 | readl(db_addr); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 436 | /* Ring the doorbell for any rings with pending URBs */ |
| 437 | static 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 Rempel | d66eaf9 | 2013-07-21 15:36:19 +0200 | [diff] [blame] | 448 | if (ep->ring && !(list_empty(&ep->ring->td_list))) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 449 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 450 | 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 Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 457 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, |
| 458 | stream_id); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Jim Lin | ef513be | 2019-06-03 18:53:44 +0800 | [diff] [blame] | 462 | void 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 Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 469 | static 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 Nyman | 42f2890 | 2021-01-29 15:00:24 +0200 | [diff] [blame] | 489 | static 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 Ivanov | 75b040e | 2016-04-22 13:17:10 +0300 | [diff] [blame] | 509 | /* 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 | */ |
| 513 | struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci, |
Sarah Sharp | 021bff9 | 2010-07-29 22:12:20 -0700 | [diff] [blame] | 514 | unsigned int slot_id, unsigned int ep_index, |
| 515 | unsigned int stream_id) |
| 516 | { |
| 517 | struct xhci_virt_ep *ep; |
| 518 | |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 519 | ep = xhci_get_virt_ep(xhci, slot_id, ep_index); |
| 520 | if (!ep) |
| 521 | return NULL; |
| 522 | |
Mathias Nyman | 42f2890 | 2021-01-29 15:00:24 +0200 | [diff] [blame] | 523 | return xhci_virt_ep_to_ring(xhci, ep, stream_id); |
Sarah Sharp | 021bff9 | 2010-07-29 22:12:20 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Mathias Nyman | e6b2012 | 2017-06-02 16:36:22 +0300 | [diff] [blame] | 526 | |
| 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 | */ |
| 533 | static 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 550 | /* |
| 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 Nyman | 8790736 | 2017-06-02 16:36:23 +0300 | [diff] [blame] | 553 | * dequeue pointer, stream id, and new consumer cycle state in state. |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 554 | * 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 Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 563 | * |
| 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 567 | */ |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 568 | void xhci_find_new_dequeue_state(struct xhci_hcd *xhci, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 569 | unsigned int slot_id, unsigned int ep_index, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 570 | unsigned int stream_id, struct xhci_td *cur_td, |
| 571 | struct xhci_dequeue_state *state) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 572 | { |
| 573 | struct xhci_virt_device *dev = xhci->devs[slot_id]; |
Hans de Goede | c4bedb7 | 2013-10-04 00:29:47 +0200 | [diff] [blame] | 574 | struct xhci_virt_ep *ep = &dev->eps[ep_index]; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 575 | struct xhci_ring *ep_ring; |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 576 | struct xhci_segment *new_seg; |
| 577 | union xhci_trb *new_deq; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 578 | dma_addr_t addr; |
Julius Werner | 1f81b6d | 2014-04-25 19:20:13 +0300 | [diff] [blame] | 579 | u64 hw_dequeue; |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 580 | bool cycle_found = false; |
| 581 | bool td_last_trb_found = false; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 582 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 583 | 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 Nyman | 93ceaa8 | 2020-04-21 17:08:20 +0300 | [diff] [blame] | 591 | /* |
| 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 608 | /* Dig out the cycle state saved by the xHC during the stop ep cmd */ |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 609 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
| 610 | "Finding endpoint context"); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 611 | |
Mathias Nyman | e6b2012 | 2017-06-02 16:36:22 +0300 | [diff] [blame] | 612 | hw_dequeue = xhci_get_hw_deq(xhci, dev, ep_index, stream_id); |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 613 | new_seg = ep_ring->deq_seg; |
| 614 | new_deq = ep_ring->dequeue; |
| 615 | state->new_cycle_state = hw_dequeue & 0x1; |
Mathias Nyman | 8790736 | 2017-06-02 16:36:23 +0300 | [diff] [blame] | 616 | state->stream_id = stream_id; |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 617 | |
| 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 Nyman | 3495e45 | 2016-11-11 15:13:13 +0200 | [diff] [blame] | 634 | if (cycle_found && trb_is_link(new_deq) && |
| 635 | link_trb_toggles_cycle(new_deq)) |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 636 | 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 Werner | 1f81b6d | 2014-04-25 19:20:13 +0300 | [diff] [blame] | 645 | return; |
| 646 | } |
Julius Werner | 1f81b6d | 2014-04-25 19:20:13 +0300 | [diff] [blame] | 647 | |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 648 | } while (!cycle_found || !td_last_trb_found); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 649 | |
Mathias Nyman | 365038d | 2014-08-19 15:17:58 +0300 | [diff] [blame] | 650 | state->new_deq_seg = new_seg; |
| 651 | state->new_deq_ptr = new_deq; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 652 | |
Mathias Nyman | 93ceaa8 | 2020-04-21 17:08:20 +0300 | [diff] [blame] | 653 | done: |
Julius Werner | 1f81b6d | 2014-04-25 19:20:13 +0300 | [diff] [blame] | 654 | /* Don't update the ring cycle state for the producer (us). */ |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 655 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
| 656 | "Cycle state = 0x%x", state->new_cycle_state); |
Sarah Sharp | 01a1fdb | 2011-02-23 18:12:29 -0800 | [diff] [blame] | 657 | |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 658 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
| 659 | "New dequeue segment = %p (virtual)", |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 660 | state->new_deq_seg); |
| 661 | addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr); |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 662 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
| 663 | "New dequeue pointer = 0x%llx (DMA)", |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 664 | (unsigned long long) addr); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 667 | /* 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 Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 671 | static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, |
Mathias Nyman | 0d58a1a | 2016-11-11 15:13:20 +0200 | [diff] [blame] | 672 | struct xhci_td *td, bool flip_cycle) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 673 | { |
Mathias Nyman | 0d58a1a | 2016-11-11 15:13:20 +0200 | [diff] [blame] | 674 | struct xhci_segment *seg = td->start_seg; |
| 675 | union xhci_trb *trb = td->first_trb; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 676 | |
Mathias Nyman | 0d58a1a | 2016-11-11 15:13:20 +0200 | [diff] [blame] | 677 | while (1) { |
Mathias Nyman | ae1e3f0 | 2017-01-23 14:20:15 +0200 | [diff] [blame] | 678 | trb_to_noop(trb, TRB_TR_NOOP); |
| 679 | |
Mathias Nyman | 0d58a1a | 2016-11-11 15:13:20 +0200 | [diff] [blame] | 680 | /* 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 685 | break; |
Mathias Nyman | 0d58a1a | 2016-11-11 15:13:20 +0200 | [diff] [blame] | 686 | |
| 687 | next_trb(xhci, ep_ring, &seg, &trb); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Dmitry Torokhov | 575688e | 2011-03-20 02:15:16 -0700 | [diff] [blame] | 691 | static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci, |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 692 | struct xhci_virt_ep *ep) |
| 693 | { |
Mathias Nyman | 9983a5f | 2017-01-23 14:19:52 +0200 | [diff] [blame] | 694 | ep->ep_state &= ~EP_STOP_CMD_PENDING; |
Mathias Nyman | f992659 | 2017-01-23 14:19:53 +0200 | [diff] [blame] | 695 | /* Can't del_timer_sync in interrupt */ |
| 696 | del_timer(&ep->stop_cmd_timer); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Mathias Nyman | 446b314 | 2016-11-11 15:13:22 +0200 | [diff] [blame] | 699 | /* |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 700 | * Must be called with xhci->lock held in interrupt context, |
| 701 | * releases and re-acquires xhci->lock |
Mathias Nyman | 446b314 | 2016-11-11 15:13:22 +0200 | [diff] [blame] | 702 | */ |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 703 | static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, |
| 704 | struct xhci_td *cur_td, int status) |
Mathias Nyman | 446b314 | 2016-11-11 15:13:22 +0200 | [diff] [blame] | 705 | { |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 706 | 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 Nyman | 446b314 | 2016-11-11 15:13:22 +0200 | [diff] [blame] | 709 | |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 710 | 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 Nyman | 446b314 | 2016-11-11 15:13:22 +0200 | [diff] [blame] | 717 | xhci_urb_free_priv(urb_priv); |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 718 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Felipe Balbi | 5abdc2e | 2017-01-23 14:20:20 +0200 | [diff] [blame] | 719 | trace_xhci_urb_giveback(urb); |
Mathias Nyman | 7bc5d5a | 2017-05-17 18:31:59 +0300 | [diff] [blame] | 720 | usb_hcd_giveback_urb(hcd, urb, status); |
Mathias Nyman | 446b314 | 2016-11-11 15:13:22 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Wei Yongjun | 2d6d576 | 2016-11-11 15:13:21 +0200 | [diff] [blame] | 723 | static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci, |
| 724 | struct xhci_ring *ring, struct xhci_td *td) |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 725 | { |
| 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 Lin | 597c56e | 2019-05-22 14:33:57 +0300 | [diff] [blame] | 729 | size_t len; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 730 | |
Felipe Balbi | f45e2a0 | 2017-01-23 14:20:13 +0200 | [diff] [blame] | 731 | if (!ring || !seg || !urb) |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 732 | 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 Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 740 | dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, |
| 741 | DMA_FROM_DEVICE); |
Henry Lin | 597c56e | 2019-05-22 14:33:57 +0300 | [diff] [blame] | 742 | /* 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 Estevam | c1a145a | 2019-05-22 10:35:29 -0300 | [diff] [blame] | 746 | xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n", |
Henry Lin | 597c56e | 2019-05-22 14:33:57 +0300 | [diff] [blame] | 747 | len, seg->bounce_len); |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 748 | seg->bounce_len = 0; |
| 749 | seg->bounce_offs = 0; |
| 750 | } |
| 751 | |
Mathias Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 752 | static int xhci_td_cleanup(struct xhci_hcd *xhci, struct xhci_td *td, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 753 | struct xhci_ring *ep_ring, int status) |
Mathias Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 754 | { |
| 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 Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 772 | status = 0; |
Mathias Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 773 | } |
Mathias Nyman | e1a2983 | 2021-01-29 15:00:34 +0200 | [diff] [blame] | 774 | /* 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 Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 778 | 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 Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 786 | (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc))) |
Mathias Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 787 | xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n", |
| 788 | urb, urb->actual_length, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 789 | urb->transfer_buffer_length, status); |
Mathias Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 790 | |
| 791 | /* set isoc urb status to 0 just as EHCI, UHCI, and OHCI */ |
| 792 | if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 793 | status = 0; |
| 794 | xhci_giveback_urb_in_irq(xhci, td, status); |
Mathias Nyman | 69eaf9e7 | 2021-01-29 15:00:33 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 800 | |
| 801 | /* Complete the cancelled URBs we unlinked from td_list. */ |
| 802 | static 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 Nyman | d8ac950 | 2021-01-29 15:00:32 +0200 | [diff] [blame] | 824 | static 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); |
| 837 | done: |
| 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 Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 844 | static 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 Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 861 | /* 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 Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 870 | err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type); |
| 871 | if (err) |
| 872 | return; |
| 873 | |
Mathias Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 874 | xhci_ring_cmd_db(xhci); |
| 875 | } |
| 876 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 877 | /* |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 878 | * 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 Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 882 | * |
| 883 | * only call this when ring is not in a running state |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 884 | */ |
| 885 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 886 | static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep) |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 887 | { |
| 888 | struct xhci_hcd *xhci; |
| 889 | struct xhci_td *td = NULL; |
| 890 | struct xhci_td *tmp_td = NULL; |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 891 | struct xhci_td *cached_td = NULL; |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 892 | struct xhci_ring *ring; |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 893 | struct xhci_dequeue_state deq_state; |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 894 | u64 hw_deq; |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 895 | unsigned int slot_id = ep->vdev->slot_id; |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 896 | |
| 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 Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 921 | 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 Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 931 | } else { |
| 932 | td_to_noop(xhci, ring, td, false); |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 933 | td->cancel_status = TD_CLEARED; |
Mathias Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 934 | } |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 935 | } |
| 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 Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 943 | } |
| 944 | return 0; |
| 945 | } |
| 946 | |
Mathias Nyman | 9ebf300 | 2021-01-29 15:00:39 +0200 | [diff] [blame^] | 947 | |
| 948 | /* |
| 949 | * Returns the TD the endpoint ring halted on. |
| 950 | * Only call for non-running rings without streams. |
| 951 | */ |
| 952 | static 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 Nyman | 4db35692 | 2021-01-29 15:00:36 +0200 | [diff] [blame] | 968 | /* |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 969 | * 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 Ragiadakou | b8200c9 | 2013-09-09 13:30:00 +0300 | [diff] [blame] | 978 | static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id, |
Mathias Nyman | 9ebf300 | 2021-01-29 15:00:39 +0200 | [diff] [blame^] | 979 | union xhci_trb *trb, u32 comp_code) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 980 | { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 981 | unsigned int ep_index; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 982 | struct xhci_virt_ep *ep; |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 983 | struct xhci_ep_ctx *ep_ctx; |
Mathias Nyman | 9ebf300 | 2021-01-29 15:00:39 +0200 | [diff] [blame^] | 984 | struct xhci_td *td = NULL; |
| 985 | enum xhci_ep_reset_type reset_type; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 986 | |
Xenia Ragiadakou | bc752bd | 2013-09-09 13:29:59 +0300 | [diff] [blame] | 987 | if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) { |
Mathias Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 988 | if (!xhci->devs[slot_id]) |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 989 | xhci_warn(xhci, "Stop endpoint command completion for disabled slot %u\n", |
| 990 | slot_id); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 994 | ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 995 | ep = xhci_get_virt_ep(xhci, slot_id, ep_index); |
| 996 | if (!ep) |
| 997 | return; |
| 998 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 999 | ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); |
| 1000 | |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1001 | trace_xhci_handle_cmd_stop_ep(ep_ctx); |
| 1002 | |
Mathias Nyman | 9ebf300 | 2021-01-29 15:00:39 +0200 | [diff] [blame^] | 1003 | 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 Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1037 | /* will queue a set TR deq if stopped on a cancelled, uncleared TD */ |
| 1038 | xhci_invalidate_cancelled_tds(ep); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1039 | xhci_stop_watchdog_timer_in_irq(xhci, ep); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1040 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1041 | /* 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Sarah Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1046 | static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring) |
| 1047 | { |
| 1048 | struct xhci_td *cur_td; |
Felipe Balbi | a54cfae | 2017-01-23 14:20:17 +0200 | [diff] [blame] | 1049 | struct xhci_td *tmp; |
Sarah Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1050 | |
Felipe Balbi | a54cfae | 2017-01-23 14:20:17 +0200 | [diff] [blame] | 1051 | list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) { |
Sarah Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1052 | list_del_init(&cur_td->td_list); |
Felipe Balbi | a54cfae | 2017-01-23 14:20:17 +0200 | [diff] [blame] | 1053 | |
Sarah Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1054 | if (!list_empty(&cur_td->cancelled_td_list)) |
| 1055 | list_del_init(&cur_td->cancelled_td_list); |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 1056 | |
Felipe Balbi | a60f2f2 | 2017-01-23 14:20:14 +0200 | [diff] [blame] | 1057 | xhci_unmap_td_bounce_buffer(xhci, ring, cur_td); |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 1058 | |
| 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 Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci, |
| 1066 | int slot_id, int ep_index) |
| 1067 | { |
| 1068 | struct xhci_td *cur_td; |
Felipe Balbi | a54cfae | 2017-01-23 14:20:17 +0200 | [diff] [blame] | 1069 | struct xhci_td *tmp; |
Sarah Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1070 | struct xhci_virt_ep *ep; |
| 1071 | struct xhci_ring *ring; |
| 1072 | |
| 1073 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
Sarah Sharp | 21d0e51 | 2014-02-21 14:29:02 -0800 | [diff] [blame] | 1074 | if ((ep->ep_state & EP_HAS_STREAMS) || |
| 1075 | (ep->ep_state & EP_GETTING_NO_STREAMS)) { |
| 1076 | int stream_id; |
| 1077 | |
Mathias Nyman | 4b89586 | 2017-07-20 14:48:26 +0300 | [diff] [blame] | 1078 | for (stream_id = 1; stream_id < ep->stream_info->num_streams; |
Sarah Sharp | 21d0e51 | 2014-02-21 14:29:02 -0800 | [diff] [blame] | 1079 | stream_id++) { |
Mathias Nyman | 4b89586 | 2017-07-20 14:48:26 +0300 | [diff] [blame] | 1080 | ring = ep->stream_info->stream_rings[stream_id]; |
| 1081 | if (!ring) |
| 1082 | continue; |
| 1083 | |
Sarah Sharp | 21d0e51 | 2014-02-21 14:29:02 -0800 | [diff] [blame] | 1084 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
| 1085 | "Killing URBs for slot ID %u, ep index %u, stream %u", |
Mathias Nyman | 4b89586 | 2017-07-20 14:48:26 +0300 | [diff] [blame] | 1086 | slot_id, ep_index, stream_id); |
| 1087 | xhci_kill_ring_urbs(xhci, ring); |
Sarah Sharp | 21d0e51 | 2014-02-21 14:29:02 -0800 | [diff] [blame] | 1088 | } |
| 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 Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 1098 | |
Felipe Balbi | a54cfae | 2017-01-23 14:20:17 +0200 | [diff] [blame] | 1099 | 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 Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 1102 | inc_td_cnt(cur_td->urb); |
Felipe Balbi | a54cfae | 2017-01-23 14:20:17 +0200 | [diff] [blame] | 1103 | |
Mathias Nyman | 2a72126 | 2016-11-11 15:13:24 +0200 | [diff] [blame] | 1104 | if (last_td_in_urb(cur_td)) |
| 1105 | xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); |
Sarah Sharp | 50e8725 | 2014-02-21 09:27:30 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1109 | /* |
| 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 | */ |
| 1118 | void 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 Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1143 | /* 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 Nyman | f992659 | 2017-01-23 14:19:53 +0200 | [diff] [blame] | 1157 | * Instead we use a combination of that flag and checking if a new timer is |
| 1158 | * pending. |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1159 | */ |
Kees Cook | 66a4550 | 2017-10-16 16:16:58 -0700 | [diff] [blame] | 1160 | void xhci_stop_endpoint_command_watchdog(struct timer_list *t) |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1161 | { |
Kees Cook | 66a4550 | 2017-10-16 16:16:58 -0700 | [diff] [blame] | 1162 | struct xhci_virt_ep *ep = from_timer(ep, t, stop_cmd_timer); |
| 1163 | struct xhci_hcd *xhci = ep->xhci; |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 1164 | unsigned long flags; |
Mathias Nyman | 9c1aa36 | 2020-03-12 16:45:11 +0200 | [diff] [blame] | 1165 | u32 usbsts; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1166 | |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 1167 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1168 | |
Mathias Nyman | f992659 | 2017-01-23 14:19:53 +0200 | [diff] [blame] | 1169 | /* 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 Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 1172 | spin_unlock_irqrestore(&xhci->lock, flags); |
Mathias Nyman | f992659 | 2017-01-23 14:19:53 +0200 | [diff] [blame] | 1173 | xhci_dbg(xhci, "Stop EP timer raced with cmd completion, exit"); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1174 | return; |
| 1175 | } |
Mathias Nyman | 9c1aa36 | 2020-03-12 16:45:11 +0200 | [diff] [blame] | 1176 | usbsts = readl(&xhci->op_regs->status); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1177 | |
| 1178 | xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n"); |
Mathias Nyman | 9c1aa36 | 2020-03-12 16:45:11 +0200 | [diff] [blame] | 1179 | xhci_warn(xhci, "USBSTS:%s\n", xhci_decode_usbsts(usbsts)); |
| 1180 | |
Mathias Nyman | f992659 | 2017-01-23 14:19:53 +0200 | [diff] [blame] | 1181 | ep->ep_state &= ~EP_STOP_CMD_PENDING; |
| 1182 | |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1183 | xhci_halt(xhci); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1184 | |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1185 | /* |
| 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 Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1191 | |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 1192 | spin_unlock_irqrestore(&xhci->lock, flags); |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 1193 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 1194 | "xHCI host controller is dead."); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 1197 | static 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 Sharp | 0d9f78a | 2012-06-21 16:28:30 -0700 | [diff] [blame] | 1209 | /* 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 Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 1215 | if (trb_is_link(ep_ring->dequeue)) { |
Sarah Sharp | 0d9f78a | 2012-06-21 16:28:30 -0700 | [diff] [blame] | 1216 | ep_ring->deq_seg = ep_ring->deq_seg->next; |
| 1217 | ep_ring->dequeue = ep_ring->deq_seg->trbs; |
| 1218 | } |
| 1219 | |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 1220 | 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 Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 1224 | if (trb_is_link(ep_ring->dequeue)) { |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 1225 | 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1243 | /* |
| 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 Ragiadakou | b8200c9 | 2013-09-09 13:30:00 +0300 | [diff] [blame] | 1250 | static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id, |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1251 | union xhci_trb *trb, u32 cmd_comp_code) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1252 | { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1253 | unsigned int ep_index; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1254 | unsigned int stream_id; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1255 | struct xhci_ring *ep_ring; |
Hans de Goede | 9aad95e | 2013-10-04 00:29:49 +0200 | [diff] [blame] | 1256 | struct xhci_virt_ep *ep; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1257 | struct xhci_ep_ctx *ep_ctx; |
| 1258 | struct xhci_slot_ctx *slot_ctx; |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1259 | struct xhci_td *td, *tmp_td; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1260 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1261 | 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 Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 1263 | ep = xhci_get_virt_ep(xhci, slot_id, ep_index); |
| 1264 | if (!ep) |
| 1265 | return; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1266 | |
Mathias Nyman | 42f2890 | 2021-01-29 15:00:24 +0200 | [diff] [blame] | 1267 | ep_ring = xhci_virt_ep_to_ring(xhci, ep, stream_id); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1268 | if (!ep_ring) { |
Oliver Neukum | e587b8b | 2014-01-08 17:13:11 +0100 | [diff] [blame] | 1269 | xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n", |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1270 | stream_id); |
| 1271 | /* XXX: Harmless??? */ |
Hans de Goede | 0d4976e | 2014-08-20 16:41:55 +0300 | [diff] [blame] | 1272 | goto cleanup; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 1275 | 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 Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1277 | trace_xhci_handle_cmd_set_deq(slot_ctx); |
| 1278 | trace_xhci_handle_cmd_set_deq_ep(ep_ctx); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1279 | |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1280 | if (cmd_comp_code != COMP_SUCCESS) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1281 | unsigned int ep_state; |
| 1282 | unsigned int slot_state; |
| 1283 | |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1284 | switch (cmd_comp_code) { |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1285 | case COMP_TRB_ERROR: |
Oliver Neukum | e587b8b | 2014-01-08 17:13:11 +0100 | [diff] [blame] | 1286 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n"); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1287 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1288 | case COMP_CONTEXT_STATE_ERROR: |
Oliver Neukum | e587b8b | 2014-01-08 17:13:11 +0100 | [diff] [blame] | 1289 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n"); |
Mathias Nyman | 5071e6b | 2016-11-11 15:13:28 +0200 | [diff] [blame] | 1290 | ep_state = GET_EP_CTX_STATE(ep_ctx); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1291 | slot_state = le32_to_cpu(slot_ctx->dev_state); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1292 | slot_state = GET_SLOT_STATE(slot_state); |
Xenia Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 1293 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
| 1294 | "Slot state = %u, EP state = %u", |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1295 | slot_state, ep_state); |
| 1296 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1297 | case COMP_SLOT_NOT_ENABLED_ERROR: |
Oliver Neukum | e587b8b | 2014-01-08 17:13:11 +0100 | [diff] [blame] | 1298 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n", |
| 1299 | slot_id); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1300 | break; |
| 1301 | default: |
Oliver Neukum | e587b8b | 2014-01-08 17:13:11 +0100 | [diff] [blame] | 1302 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n", |
| 1303 | cmd_comp_code); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1304 | 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 Goede | 9aad95e | 2013-10-04 00:29:49 +0200 | [diff] [blame] | 1313 | 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 Ragiadakou | aa50b29 | 2013-08-14 06:33:54 +0300 | [diff] [blame] | 1322 | xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, |
Hans de Goede | 9aad95e | 2013-10-04 00:29:49 +0200 | [diff] [blame] | 1323 | "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 Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 1326 | /* Update the ring's dequeue segment and dequeue pointer |
| 1327 | * to reflect the new position. |
| 1328 | */ |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 1329 | update_ring_for_set_deq_completion(xhci, ep->vdev, |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 1330 | ep_ring, ep_index); |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 1331 | } else { |
Oliver Neukum | e587b8b | 2014-01-08 17:13:11 +0100 | [diff] [blame] | 1332 | xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n"); |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 1333 | xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", |
Hans de Goede | 9aad95e | 2013-10-04 00:29:49 +0200 | [diff] [blame] | 1334 | ep->queued_deq_seg, ep->queued_deq_ptr); |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 1335 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1336 | } |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1337 | /* 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 Goede | 0d4976e | 2014-08-20 16:41:55 +0300 | [diff] [blame] | 1346 | cleanup: |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 1347 | ep->ep_state &= ~SET_DEQ_PENDING; |
| 1348 | ep->queued_deq_seg = NULL; |
| 1349 | ep->queued_deq_ptr = NULL; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1350 | /* Restart any rings with pending URBs */ |
| 1351 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Xenia Ragiadakou | b8200c9 | 2013-09-09 13:30:00 +0300 | [diff] [blame] | 1354 | static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id, |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1355 | union xhci_trb *trb, u32 cmd_comp_code) |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1356 | { |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 1357 | struct xhci_virt_ep *ep; |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1358 | struct xhci_ep_ctx *ep_ctx; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1359 | unsigned int ep_index; |
| 1360 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1361 | ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 1362 | 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 Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1367 | trace_xhci_handle_cmd_reset_ep(ep_ctx); |
| 1368 | |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1369 | /* This command will only fail if the endpoint wasn't halted, |
| 1370 | * but we don't care. |
| 1371 | */ |
Xenia Ragiadakou | a025432 | 2013-08-06 07:52:46 +0300 | [diff] [blame] | 1372 | xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep, |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1373 | "Ignoring reset ep completion code of %u", cmd_comp_code); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1374 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1375 | /* Cleanup cancelled TDs as ep is stopped. May queue a Set TR Deq cmd */ |
| 1376 | xhci_invalidate_cancelled_tds(ep); |
Lu Baolu | 74e0b56 | 2017-04-07 17:57:05 +0300 | [diff] [blame] | 1377 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1378 | 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 Baolu | 74e0b56 | 2017-04-07 17:57:05 +0300 | [diff] [blame] | 1382 | |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 1383 | xhci_giveback_invalidated_tds(ep); |
Mathias Nyman | f8f80be | 2018-09-20 19:13:37 +0300 | [diff] [blame] | 1384 | |
| 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 Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1388 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1389 | |
Xenia Ragiadakou | b244b43 | 2013-09-09 13:29:47 +0300 | [diff] [blame] | 1390 | static void xhci_handle_cmd_enable_slot(struct xhci_hcd *xhci, int slot_id, |
Lu Baolu | c2d3d49 | 2016-11-11 15:13:31 +0200 | [diff] [blame] | 1391 | struct xhci_command *command, u32 cmd_comp_code) |
Xenia Ragiadakou | b244b43 | 2013-09-09 13:29:47 +0300 | [diff] [blame] | 1392 | { |
| 1393 | if (cmd_comp_code == COMP_SUCCESS) |
Lu Baolu | c2d3d49 | 2016-11-11 15:13:31 +0200 | [diff] [blame] | 1394 | command->slot_id = slot_id; |
Xenia Ragiadakou | b244b43 | 2013-09-09 13:29:47 +0300 | [diff] [blame] | 1395 | else |
Lu Baolu | c2d3d49 | 2016-11-11 15:13:31 +0200 | [diff] [blame] | 1396 | command->slot_id = 0; |
Xenia Ragiadakou | b244b43 | 2013-09-09 13:29:47 +0300 | [diff] [blame] | 1397 | } |
| 1398 | |
Xenia Ragiadakou | 6c02dd1 | 2013-09-09 13:29:48 +0300 | [diff] [blame] | 1399 | static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id) |
| 1400 | { |
| 1401 | struct xhci_virt_device *virt_dev; |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1402 | struct xhci_slot_ctx *slot_ctx; |
Xenia Ragiadakou | 6c02dd1 | 2013-09-09 13:29:48 +0300 | [diff] [blame] | 1403 | |
| 1404 | virt_dev = xhci->devs[slot_id]; |
| 1405 | if (!virt_dev) |
| 1406 | return; |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1407 | |
| 1408 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); |
| 1409 | trace_xhci_handle_cmd_disable_slot(slot_ctx); |
| 1410 | |
Xenia Ragiadakou | 6c02dd1 | 2013-09-09 13:29:48 +0300 | [diff] [blame] | 1411 | 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 Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1417 | static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id, |
Mathias Nyman | a1810307 | 2021-01-29 15:00:21 +0200 | [diff] [blame] | 1418 | u32 cmd_comp_code) |
Xenia Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1419 | { |
| 1420 | struct xhci_virt_device *virt_dev; |
| 1421 | struct xhci_input_control_ctx *ctrl_ctx; |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1422 | struct xhci_ep_ctx *ep_ctx; |
Xenia Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1423 | unsigned int ep_index; |
| 1424 | unsigned int ep_state; |
| 1425 | u32 add_flags, drop_flags; |
| 1426 | |
Xenia Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1427 | /* |
| 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 Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 1435 | virt_dev = xhci->devs[slot_id]; |
Mathias Nyman | 03ed579 | 2021-01-29 15:00:23 +0200 | [diff] [blame] | 1436 | if (!virt_dev) |
| 1437 | return; |
Lin Wang | 4daf9df | 2015-01-09 16:06:31 +0200 | [diff] [blame] | 1438 | ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); |
Xenia Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1439 | 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 Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1449 | ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index); |
| 1450 | trace_xhci_handle_cmd_config_ep(ep_ctx); |
| 1451 | |
Xenia Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1452 | /* 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 Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 1463 | return; |
Xenia Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1464 | 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 Ragiadakou | 6ed46d3 | 2013-09-09 13:29:55 +0300 | [diff] [blame] | 1473 | return; |
| 1474 | } |
| 1475 | |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1476 | static 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 Nyman | 03ed579 | 2021-01-29 15:00:23 +0200 | [diff] [blame] | 1482 | if (!vdev) |
| 1483 | return; |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1484 | slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); |
| 1485 | trace_xhci_handle_cmd_addr_dev(slot_ctx); |
| 1486 | } |
| 1487 | |
Mathias Nyman | a1810307 | 2021-01-29 15:00:21 +0200 | [diff] [blame] | 1488 | static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id) |
Xenia Ragiadakou | f681321 | 2013-09-09 13:29:51 +0300 | [diff] [blame] | 1489 | { |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1490 | struct xhci_virt_device *vdev; |
| 1491 | struct xhci_slot_ctx *slot_ctx; |
| 1492 | |
| 1493 | vdev = xhci->devs[slot_id]; |
Mathias Nyman | 03ed579 | 2021-01-29 15:00:23 +0200 | [diff] [blame] | 1494 | if (!vdev) { |
| 1495 | xhci_warn(xhci, "Reset device command completion for disabled slot %u\n", |
| 1496 | slot_id); |
| 1497 | return; |
| 1498 | } |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1499 | slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); |
| 1500 | trace_xhci_handle_cmd_reset_dev(slot_ctx); |
| 1501 | |
Xenia Ragiadakou | f681321 | 2013-09-09 13:29:51 +0300 | [diff] [blame] | 1502 | xhci_dbg(xhci, "Completed reset device command.\n"); |
Xenia Ragiadakou | f681321 | 2013-09-09 13:29:51 +0300 | [diff] [blame] | 1503 | } |
| 1504 | |
Xenia Ragiadakou | 2c07082 | 2013-09-09 13:29:52 +0300 | [diff] [blame] | 1505 | static 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 Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 1509 | xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n"); |
Xenia Ragiadakou | 2c07082 | 2013-09-09 13:29:52 +0300 | [diff] [blame] | 1510 | 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 Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 1518 | static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 status) |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1519 | { |
| 1520 | list_del(&cmd->cmd_list); |
Mathias Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 1521 | |
| 1522 | if (cmd->completion) { |
| 1523 | cmd->status = status; |
| 1524 | complete(cmd->completion); |
| 1525 | } else { |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1526 | kfree(cmd); |
Mathias Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 1527 | } |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | void xhci_cleanup_command_queue(struct xhci_hcd *xhci) |
| 1531 | { |
| 1532 | struct xhci_command *cur_cmd, *tmp_cmd; |
Jeffy Chen | d1aad52 | 2017-10-06 17:45:28 +0300 | [diff] [blame] | 1533 | xhci->current_cmd = NULL; |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1534 | list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list) |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1535 | xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED); |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1536 | } |
| 1537 | |
OGAWA Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 1538 | void xhci_handle_command_timeout(struct work_struct *work) |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1539 | { |
| 1540 | struct xhci_hcd *xhci; |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1541 | unsigned long flags; |
| 1542 | u64 hw_ring_state; |
OGAWA Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 1543 | |
| 1544 | xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer); |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1545 | |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1546 | spin_lock_irqsave(&xhci->lock, flags); |
Lu Baolu | 2b98546 | 2017-01-03 18:28:46 +0200 | [diff] [blame] | 1547 | |
Mathias Nyman | a5a1b95 | 2017-01-03 18:28:48 +0200 | [diff] [blame] | 1548 | /* |
| 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 Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 1552 | if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) { |
Lu Baolu | 2b98546 | 2017-01-03 18:28:46 +0200 | [diff] [blame] | 1553 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1554 | return; |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1555 | } |
Lu Baolu | 2b98546 | 2017-01-03 18:28:46 +0200 | [diff] [blame] | 1556 | /* mark this command to be cancelled */ |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1557 | xhci->current_cmd->status = COMP_COMMAND_ABORTED; |
Lu Baolu | 2b98546 | 2017-01-03 18:28:46 +0200 | [diff] [blame] | 1558 | |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1559 | /* Make sure command ring is running before aborting it */ |
| 1560 | hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1561 | if (hw_ring_state == ~(u64)0) { |
| 1562 | xhci_hc_died(xhci); |
| 1563 | goto time_out_completed; |
| 1564 | } |
| 1565 | |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1566 | if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) && |
| 1567 | (hw_ring_state & CMD_RING_RUNNING)) { |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 1568 | /* Prevent new doorbell, and start command abort */ |
| 1569 | xhci->cmd_ring_state = CMD_RING_STATE_ABORTED; |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1570 | xhci_dbg(xhci, "Command timeout\n"); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 1571 | xhci_abort_cmd_ring(xhci, flags); |
Lu Baolu | 4dea707 | 2017-01-03 18:28:49 +0200 | [diff] [blame] | 1572 | goto time_out_completed; |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1573 | } |
Mathias Nyman | 3425aa0 | 2016-06-01 18:09:08 +0300 | [diff] [blame] | 1574 | |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 1575 | /* host removed. Bail out */ |
| 1576 | if (xhci->xhc_state & XHCI_STATE_REMOVING) { |
| 1577 | xhci_dbg(xhci, "host removed, ring start fail?\n"); |
Mathias Nyman | 3425aa0 | 2016-06-01 18:09:08 +0300 | [diff] [blame] | 1578 | xhci_cleanup_command_queue(xhci); |
Lu Baolu | 4dea707 | 2017-01-03 18:28:49 +0200 | [diff] [blame] | 1579 | |
| 1580 | goto time_out_completed; |
Mathias Nyman | 3425aa0 | 2016-06-01 18:09:08 +0300 | [diff] [blame] | 1581 | } |
| 1582 | |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1583 | /* 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 Baolu | 4dea707 | 2017-01-03 18:28:49 +0200 | [diff] [blame] | 1586 | |
| 1587 | time_out_completed: |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1588 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1589 | return; |
| 1590 | } |
| 1591 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1592 | static void handle_cmd_completion(struct xhci_hcd *xhci, |
| 1593 | struct xhci_event_cmd *event) |
| 1594 | { |
Lalithambika Krishna Kumar | 296fcda | 2021-01-29 15:00:27 +0200 | [diff] [blame] | 1595 | unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1596 | u64 cmd_dma; |
| 1597 | dma_addr_t cmd_dequeue_dma; |
Xenia Ragiadakou | e7a79a1 | 2013-09-09 13:29:56 +0300 | [diff] [blame] | 1598 | u32 cmd_comp_code; |
Xenia Ragiadakou | 9124b12 | 2013-09-09 13:29:57 +0300 | [diff] [blame] | 1599 | union xhci_trb *cmd_trb; |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1600 | struct xhci_command *cmd; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1601 | u32 cmd_type; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1602 | |
Lalithambika Krishna Kumar | 296fcda | 2021-01-29 15:00:27 +0200 | [diff] [blame] | 1603 | if (slot_id >= MAX_HC_SLOTS) { |
| 1604 | xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); |
| 1605 | return; |
| 1606 | } |
| 1607 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1608 | cmd_dma = le64_to_cpu(event->cmd_trb); |
Xenia Ragiadakou | 9124b12 | 2013-09-09 13:29:57 +0300 | [diff] [blame] | 1609 | cmd_trb = xhci->cmd_ring->dequeue; |
Felipe Balbi | a37c3f7 | 2017-01-23 14:20:19 +0200 | [diff] [blame] | 1610 | |
| 1611 | trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic); |
| 1612 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1613 | cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, |
Xenia Ragiadakou | 9124b12 | 2013-09-09 13:29:57 +0300 | [diff] [blame] | 1614 | cmd_trb); |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 1615 | /* |
| 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 Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1622 | return; |
| 1623 | } |
Elric Fu | b63f405 | 2012-06-27 16:55:43 +0800 | [diff] [blame] | 1624 | |
Felipe Balbi | 04861f8 | 2017-01-23 14:20:09 +0200 | [diff] [blame] | 1625 | cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list); |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1626 | |
OGAWA Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 1627 | cancel_delayed_work(&xhci->cmd_timer); |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1628 | |
Xenia Ragiadakou | e7a79a1 | 2013-09-09 13:29:56 +0300 | [diff] [blame] | 1629 | cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status)); |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1630 | |
| 1631 | /* If CMD ring stopped we own the trbs between enqueue and dequeue */ |
Mathias Nyman | 604d02a | 2017-05-17 18:32:05 +0300 | [diff] [blame] | 1632 | if (cmd_comp_code == COMP_COMMAND_RING_STOPPED) { |
OGAWA Hirofumi | 1c111b6 | 2017-01-03 18:28:51 +0200 | [diff] [blame] | 1633 | complete_all(&xhci->cmd_ring_stop_completion); |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1634 | return; |
| 1635 | } |
Mathias Nyman | 33be126 | 2016-08-16 10:18:03 +0300 | [diff] [blame] | 1636 | |
| 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 Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1643 | /* |
| 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 Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1649 | if (cmd_comp_code == COMP_COMMAND_ABORTED) { |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1650 | xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 1651 | if (cmd->status == COMP_COMMAND_ABORTED) { |
Baolin Wang | 2a7cfdf | 2017-01-03 18:28:47 +0200 | [diff] [blame] | 1652 | if (xhci->current_cmd == cmd) |
| 1653 | xhci->current_cmd = NULL; |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1654 | goto event_handled; |
Baolin Wang | 2a7cfdf | 2017-01-03 18:28:47 +0200 | [diff] [blame] | 1655 | } |
Elric Fu | b63f405 | 2012-06-27 16:55:43 +0800 | [diff] [blame] | 1656 | } |
| 1657 | |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1658 | cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3])); |
| 1659 | switch (cmd_type) { |
| 1660 | case TRB_ENABLE_SLOT: |
Lu Baolu | c2d3d49 | 2016-11-11 15:13:31 +0200 | [diff] [blame] | 1661 | xhci_handle_cmd_enable_slot(xhci, slot_id, cmd, cmd_comp_code); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1662 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1663 | case TRB_DISABLE_SLOT: |
Xenia Ragiadakou | 6c02dd1 | 2013-09-09 13:29:48 +0300 | [diff] [blame] | 1664 | xhci_handle_cmd_disable_slot(xhci, slot_id); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1665 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1666 | case TRB_CONFIG_EP: |
Mathias Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 1667 | if (!cmd->completion) |
Mathias Nyman | a1810307 | 2021-01-29 15:00:21 +0200 | [diff] [blame] | 1668 | xhci_handle_cmd_config_ep(xhci, slot_id, cmd_comp_code); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1669 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1670 | case TRB_EVAL_CONTEXT: |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 1671 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1672 | case TRB_ADDR_DEV: |
Felipe Balbi | 19a7d0d6 | 2017-04-07 17:56:57 +0300 | [diff] [blame] | 1673 | xhci_handle_cmd_addr_dev(xhci, slot_id); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1674 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1675 | case TRB_STOP_RING: |
Xenia Ragiadakou | b8200c9 | 2013-09-09 13:30:00 +0300 | [diff] [blame] | 1676 | WARN_ON(slot_id != TRB_TO_SLOT_ID( |
| 1677 | le32_to_cpu(cmd_trb->generic.field[3]))); |
Mathias Nyman | a38fe33 | 2018-03-16 16:33:02 +0200 | [diff] [blame] | 1678 | if (!cmd->completion) |
Mathias Nyman | 9ebf300 | 2021-01-29 15:00:39 +0200 | [diff] [blame^] | 1679 | xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb, |
| 1680 | cmd_comp_code); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1681 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1682 | case TRB_SET_DEQ: |
Xenia Ragiadakou | b8200c9 | 2013-09-09 13:30:00 +0300 | [diff] [blame] | 1683 | WARN_ON(slot_id != TRB_TO_SLOT_ID( |
| 1684 | le32_to_cpu(cmd_trb->generic.field[3]))); |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1685 | xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1686 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1687 | case TRB_CMD_NOOP: |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1688 | /* Is this an aborted command turned to NO-OP? */ |
Mathias Nyman | 604d02a | 2017-05-17 18:32:05 +0300 | [diff] [blame] | 1689 | if (cmd->status == COMP_COMMAND_RING_STOPPED) |
| 1690 | cmd_comp_code = COMP_COMMAND_RING_STOPPED; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1691 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1692 | case TRB_RESET_EP: |
Xenia Ragiadakou | b8200c9 | 2013-09-09 13:30:00 +0300 | [diff] [blame] | 1693 | WARN_ON(slot_id != TRB_TO_SLOT_ID( |
| 1694 | le32_to_cpu(cmd_trb->generic.field[3]))); |
Xenia Ragiadakou | c69a059 | 2013-09-09 13:30:01 +0300 | [diff] [blame] | 1695 | xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1696 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1697 | case TRB_RESET_DEV: |
Mathias Nyman | 6fcfb0d | 2014-06-24 17:14:40 +0300 | [diff] [blame] | 1698 | /* 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 Nyman | a1810307 | 2021-01-29 15:00:21 +0200 | [diff] [blame] | 1703 | xhci_handle_cmd_reset_dev(xhci, slot_id); |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 1704 | break; |
Xenia Ragiadakou | b54fc46 | 2013-09-09 13:29:58 +0300 | [diff] [blame] | 1705 | case TRB_NEC_GET_FW: |
Xenia Ragiadakou | 2c07082 | 2013-09-09 13:29:52 +0300 | [diff] [blame] | 1706 | xhci_handle_cmd_nec_get_fw(xhci, event); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1707 | break; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1708 | default: |
| 1709 | /* Skip over unknown commands on the event ring */ |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 1710 | xhci_info(xhci, "INFO unknown command type %d\n", cmd_type); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1711 | break; |
| 1712 | } |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1713 | |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1714 | /* restart timer if this wasn't the last command */ |
Lu Baolu | daa47f2 | 2017-01-23 14:20:02 +0200 | [diff] [blame] | 1715 | if (!list_is_singular(&xhci->cmd_list)) { |
Felipe Balbi | 04861f8 | 2017-01-23 14:20:09 +0200 | [diff] [blame] | 1716 | xhci->current_cmd = list_first_entry(&cmd->cmd_list, |
| 1717 | struct xhci_command, cmd_list); |
OGAWA Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 1718 | xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT); |
Lu Baolu | 2b98546 | 2017-01-03 18:28:46 +0200 | [diff] [blame] | 1719 | } else if (xhci->current_cmd == cmd) { |
| 1720 | xhci->current_cmd = NULL; |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | event_handled: |
Mathias Nyman | 9ea1833 | 2014-05-08 19:26:02 +0300 | [diff] [blame] | 1724 | xhci_complete_del_and_free_cmd(cmd, cmd_comp_code); |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 1725 | |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 1726 | inc_deq(xhci, xhci->cmd_ring); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1729 | static void handle_vendor_event(struct xhci_hcd *xhci, |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 1730 | union xhci_trb *event, u32 trb_type) |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1731 | { |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1732 | 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 Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1737 | static void handle_device_notification(struct xhci_hcd *xhci, |
| 1738 | union xhci_trb *event) |
| 1739 | { |
| 1740 | u32 slot_id; |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1741 | struct usb_device *udev; |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1742 | |
Xenia Ragiadakou | 7e76ad4 | 2013-09-09 21:03:10 +0300 | [diff] [blame] | 1743 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3])); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1744 | if (!xhci->devs[slot_id]) { |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1745 | xhci_warn(xhci, "Device Notification event for " |
| 1746 | "unused slot %u\n", slot_id); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1747 | 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 Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Cherian, George | 11644a7 | 2018-11-09 17:21:22 +0200 | [diff] [blame] | 1757 | /* |
| 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 | */ |
| 1769 | static 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 Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1786 | static void handle_port_status(struct xhci_hcd *xhci, |
| 1787 | union xhci_trb *event) |
| 1788 | { |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1789 | struct usb_hcd *hcd; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1790 | u32 port_id; |
Mathias Nyman | 76a0f32 | 2017-08-16 14:23:23 +0300 | [diff] [blame] | 1791 | u32 portsc, cmd_reg; |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1792 | int max_ports; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1793 | int slot_id; |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1794 | unsigned int hcd_portnum; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1795 | struct xhci_bus_state *bus_state; |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1796 | bool bogus_port_status = false; |
Mathias Nyman | 52c7755 | 2018-05-21 16:39:57 +0300 | [diff] [blame] | 1797 | struct xhci_port *port; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1798 | |
| 1799 | /* Port status change events always have a successful completion code */ |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 1800 | 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 Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1804 | port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1805 | max_ports = HCS_MAX_PORTS(xhci->hcs_params1); |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 1806 | |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1807 | if ((port_id <= 0) || (port_id > max_ports)) { |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 1808 | xhci_warn(xhci, "Port change event with invalid port ID %d\n", |
| 1809 | port_id); |
Peter Chen | 09ce0c0 | 2013-03-20 09:30:00 +0800 | [diff] [blame] | 1810 | inc_deq(xhci, xhci->event_ring); |
| 1811 | return; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Mathias Nyman | 52c7755 | 2018-05-21 16:39:57 +0300 | [diff] [blame] | 1814 | port = &xhci->hw_ports[port_id - 1]; |
| 1815 | if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) { |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 1816 | xhci_warn(xhci, "Port change event, no port for port ID %u\n", |
| 1817 | port_id); |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1818 | bogus_port_status = true; |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1819 | goto cleanup; |
Sarah Sharp | 5308a91 | 2010-12-01 11:34:59 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
Mathias Nyman | 1245374 | 2018-11-09 17:21:18 +0200 | [diff] [blame] | 1822 | /* 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 Nyman | 52c7755 | 2018-05-21 16:39:57 +0300 | [diff] [blame] | 1829 | hcd = port->rhub->hcd; |
Mathias Nyman | f6187f4 | 2018-12-07 16:19:30 +0200 | [diff] [blame] | 1830 | bus_state = &port->rhub->bus_state; |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1831 | hcd_portnum = port->hcd_portnum; |
Mathias Nyman | 52c7755 | 2018-05-21 16:39:57 +0300 | [diff] [blame] | 1832 | portsc = readl(port->addr); |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1833 | |
Mathias Nyman | d70d5a8 | 2019-04-26 16:23:30 +0300 | [diff] [blame] | 1834 | 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 Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1837 | trace_xhci_handle_port_status(hcd_portnum, portsc); |
Mathias Nyman | 8ca1358 | 2017-08-16 14:23:24 +0300 | [diff] [blame] | 1838 | |
Sarah Sharp | 7111ebc | 2010-12-14 13:24:55 -0800 | [diff] [blame] | 1839 | if (hcd->state == HC_STATE_SUSPENDED) { |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1840 | xhci_dbg(xhci, "resume root hub\n"); |
| 1841 | usb_hcd_resume_root_hub(hcd); |
| 1842 | } |
| 1843 | |
Mathias Nyman | b8c3b71 | 2019-06-18 17:27:47 +0300 | [diff] [blame] | 1844 | 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 Nyman | b8c3b71 | 2019-06-18 17:27:47 +0300 | [diff] [blame] | 1849 | } |
Zhuang Jin Can | fac4271 | 2015-07-21 17:20:30 +0300 | [diff] [blame] | 1850 | |
Mathias Nyman | 76a0f32 | 2017-08-16 14:23:23 +0300 | [diff] [blame] | 1851 | if ((portsc & PORT_PLC) && (portsc & PORT_PLS_MASK) == XDEV_RESUME) { |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1852 | xhci_dbg(xhci, "port resume event for port %d\n", port_id); |
| 1853 | |
Mathias Nyman | 76a0f32 | 2017-08-16 14:23:23 +0300 | [diff] [blame] | 1854 | cmd_reg = readl(&xhci->op_regs->command); |
| 1855 | if (!(cmd_reg & CMD_RUN)) { |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1856 | xhci_warn(xhci, "xHC is not running.\n"); |
| 1857 | goto cleanup; |
| 1858 | } |
| 1859 | |
Mathias Nyman | 76a0f32 | 2017-08-16 14:23:23 +0300 | [diff] [blame] | 1860 | if (DEV_SUPERSPEED_ANY(portsc)) { |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1861 | xhci_dbg(xhci, "remote wake SS port %d\n", port_id); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1862 | /* 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 Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1866 | bus_state->port_remote_wakeup |= 1 << hcd_portnum; |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1867 | xhci_test_and_clear_bit(xhci, port, PORT_PLC); |
Mathias Nyman | 057d476 | 2019-12-11 16:20:03 +0200 | [diff] [blame] | 1868 | usb_hcd_start_port_resume(&hcd->self, hcd_portnum); |
Mathias Nyman | 6b7f40f | 2018-05-21 16:39:59 +0300 | [diff] [blame] | 1869 | xhci_set_link_state(xhci, port, XDEV_U0); |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1870 | /* 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 Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1875 | } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) { |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1876 | xhci_dbg(xhci, "resume HS port %d\n", port_id); |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1877 | bus_state->resume_done[hcd_portnum] = jiffies + |
Felipe Balbi | b9e4518 | 2015-02-13 14:39:13 -0600 | [diff] [blame] | 1878 | msecs_to_jiffies(USB_RESUME_TIMEOUT); |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1879 | set_bit(hcd_portnum, &bus_state->resuming_ports); |
Anshuman Gupta | 0914ea6 | 2017-10-05 11:21:46 +0300 | [diff] [blame] | 1880 | /* 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 Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1885 | mod_timer(&hcd->rh_timer, |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1886 | bus_state->resume_done[hcd_portnum]); |
Anshuman Gupta | 330e2d6 | 2018-09-20 19:13:40 +0300 | [diff] [blame] | 1887 | usb_hcd_start_port_resume(&hcd->self, hcd_portnum); |
Anshuman Gupta | 0914ea6 | 2017-10-05 11:21:46 +0300 | [diff] [blame] | 1888 | bogus_port_status = true; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | |
Mathias Nyman | 6cbcf59 | 2019-03-22 17:50:15 +0200 | [diff] [blame] | 1892 | 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 Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1897 | xhci_dbg(xhci, "resume SS port %d finished\n", port_id); |
Kai-Heng Feng | 0200b9f7 | 2020-03-12 16:45:15 +0200 | [diff] [blame] | 1898 | complete(&bus_state->u3exit_done[hcd_portnum]); |
Mathias Nyman | 6cbcf59 | 2019-03-22 17:50:15 +0200 | [diff] [blame] | 1899 | /* We've just brought the device into U0/1/2 through either the |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1900 | * 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 Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1906 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1); |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1907 | if (slot_id && xhci->devs[slot_id]) |
| 1908 | xhci_ring_device(xhci, slot_id); |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1909 | if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) { |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1910 | xhci_test_and_clear_bit(xhci, port, PORT_PLC); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1911 | usb_wakeup_notification(hcd->self.root_hub, |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1912 | hcd_portnum + 1); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1913 | bogus_port_status = true; |
| 1914 | goto cleanup; |
| 1915 | } |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
Sarah Sharp | 8b3d457 | 2013-08-20 08:12:12 -0700 | [diff] [blame] | 1918 | /* |
| 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 Ma | 958c0bd | 2018-11-09 17:21:20 +0200 | [diff] [blame] | 1923 | if (!DEV_SUPERSPEED_ANY(portsc) && hcd->speed < HCD_USB3 && |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1924 | test_and_clear_bit(hcd_portnum, |
Sarah Sharp | 8b3d457 | 2013-08-20 08:12:12 -0700 | [diff] [blame] | 1925 | &bus_state->rexit_ports)) { |
Mathias Nyman | 74e6ad5 | 2018-05-21 16:39:58 +0300 | [diff] [blame] | 1926 | complete(&bus_state->rexit_done[hcd_portnum]); |
Sarah Sharp | 8b3d457 | 2013-08-20 08:12:12 -0700 | [diff] [blame] | 1927 | bogus_port_status = true; |
| 1928 | goto cleanup; |
| 1929 | } |
| 1930 | |
Cherian, George | 11644a7 | 2018-11-09 17:21:22 +0200 | [diff] [blame] | 1931 | if (hcd->speed < HCD_USB3) { |
Mathias Nyman | eaefcf2 | 2018-05-21 16:40:00 +0300 | [diff] [blame] | 1932 | xhci_test_and_clear_bit(xhci, port, PORT_PLC); |
Cherian, George | 11644a7 | 2018-11-09 17:21:22 +0200 | [diff] [blame] | 1933 | if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) && |
| 1934 | (portsc & PORT_CSC) && !(portsc & PORT_CONNECT)) |
| 1935 | xhci_cavium_reset_phy_quirk(xhci); |
| 1936 | } |
Andiry Xu | 6fd4562 | 2011-09-23 14:19:50 -0700 | [diff] [blame] | 1937 | |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1938 | cleanup: |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1939 | /* Update event ring dequeue pointer before dropping the lock */ |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 1940 | inc_deq(xhci, xhci->event_ring); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1941 | |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1942 | /* 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 Sharp | c52804a | 2012-11-27 12:30:23 -0800 | [diff] [blame] | 1949 | /* |
| 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 Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1958 | spin_unlock(&xhci->lock); |
| 1959 | /* Pass this up to the core */ |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1960 | usb_hcd_poll_rh_status(hcd); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1961 | spin_lock(&xhci->lock); |
| 1962 | } |
| 1963 | |
| 1964 | /* |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1965 | * 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 Goede | cffb9be | 2014-08-20 16:41:51 +0300 | [diff] [blame] | 1970 | struct xhci_segment *trb_in_td(struct xhci_hcd *xhci, |
| 1971 | struct xhci_segment *start_seg, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1972 | union xhci_trb *start_trb, |
| 1973 | union xhci_trb *end_trb, |
Hans de Goede | cffb9be | 2014-08-20 16:41:51 +0300 | [diff] [blame] | 1974 | dma_addr_t suspect_dma, |
| 1975 | bool debug) |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1976 | { |
| 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 Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1982 | start_dma = xhci_trb_virt_to_dma(start_seg, start_trb); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1983 | cur_seg = start_seg; |
| 1984 | |
| 1985 | do { |
Sarah Sharp | 2fa88da | 2009-11-03 22:02:24 -0800 | [diff] [blame] | 1986 | if (start_dma == 0) |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 1987 | return NULL; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1988 | /* We may get an event for a Link TRB in the middle of a TD */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1989 | end_seg_dma = xhci_trb_virt_to_dma(cur_seg, |
Sarah Sharp | 2fa88da | 2009-11-03 22:02:24 -0800 | [diff] [blame] | 1990 | &cur_seg->trbs[TRBS_PER_SEGMENT - 1]); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1991 | /* If the end TRB isn't in this segment, this is set to 0 */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1992 | end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1993 | |
Hans de Goede | cffb9be | 2014-08-20 16:41:51 +0300 | [diff] [blame] | 1994 | 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2003 | 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 Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 2018 | return NULL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2019 | } 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 Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2025 | start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); |
Sarah Sharp | 2fa88da | 2009-11-03 22:02:24 -0800 | [diff] [blame] | 2026 | } while (cur_seg != start_seg); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2027 | |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 2028 | return NULL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Jim Lin | ef513be | 2019-06-03 18:53:44 +0800 | [diff] [blame] | 2031 | static 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 Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 2048 | /* 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 | */ |
| 2054 | static 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 Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2059 | 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 Bhagat | d4fc8bf | 2016-03-11 10:27:49 +0530 | [diff] [blame] | 2062 | /* The 0.95 spec says a babbling control endpoint |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 2063 | * 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 Nyman | 5071e6b | 2016-11-11 15:13:28 +0200 | [diff] [blame] | 2068 | if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_HALTED) |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 2069 | return 1; |
| 2070 | |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
Sarah Sharp | b45b506 | 2009-12-09 15:59:06 -0800 | [diff] [blame] | 2074 | int 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 Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2088 | static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2089 | struct xhci_transfer_event *event, struct xhci_virt_ep *ep) |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2090 | { |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2091 | struct xhci_ep_ctx *ep_ctx; |
Felipe Balbi | be0f50c | 2017-01-23 14:20:10 +0200 | [diff] [blame] | 2092 | struct xhci_ring *ep_ring; |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2093 | u32 trb_comp_code; |
| 2094 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2095 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
Mathias Nyman | ab58f3b | 2021-01-29 15:00:18 +0200 | [diff] [blame] | 2096 | ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2097 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2098 | |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2099 | if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID || |
| 2100 | trb_comp_code == COMP_STOPPED || |
| 2101 | trb_comp_code == COMP_STOPPED_SHORT_PACKET) { |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2102 | /* 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 Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2106 | return 0; |
Mathias Nyman | 69defe0 | 2014-11-27 18:19:14 +0200 | [diff] [blame] | 2107 | } |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2108 | if (trb_comp_code == COMP_STALL_ERROR || |
Mathias Nyman | 69defe0 | 2014-11-27 18:19:14 +0200 | [diff] [blame] | 2109 | xhci_requires_manual_halt_cleanup(xhci, ep_ctx, |
| 2110 | trb_comp_code)) { |
Mathias Nyman | 8f97250 | 2020-04-21 17:08:22 +0300 | [diff] [blame] | 2111 | /* |
| 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 Nyman | 69defe0 | 2014-11-27 18:19:14 +0200 | [diff] [blame] | 2120 | */ |
Mathias Nyman | ab58f3b | 2021-01-29 15:00:18 +0200 | [diff] [blame] | 2121 | if ((ep->ep_index != 0) || (trb_comp_code != COMP_STALL_ERROR)) |
Mathias Nyman | 8f97250 | 2020-04-21 17:08:22 +0300 | [diff] [blame] | 2122 | xhci_clear_hub_tt_buffer(xhci, td, ep); |
Mathias Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 2123 | |
| 2124 | xhci_handle_halted_endpoint(xhci, ep, ep_ring->stream_id, td, |
Mathias Nyman | 674f843 | 2021-01-29 15:00:38 +0200 | [diff] [blame] | 2125 | EP_HARD_RESET); |
| 2126 | |
| 2127 | return 0; /* xhci_handle_halted_endpoint marked td cancelled */ |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2128 | } else { |
Mathias Nyman | 69defe0 | 2014-11-27 18:19:14 +0200 | [diff] [blame] | 2129 | /* Update ring dequeue pointer */ |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 2130 | 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 Nyman | 69defe0 | 2014-11-27 18:19:14 +0200 | [diff] [blame] | 2133 | inc_deq(xhci, ep_ring); |
| 2134 | } |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2135 | |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2136 | return xhci_td_cleanup(xhci, td, ep_ring, td->status); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2139 | /* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */ |
| 2140 | static 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 Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2154 | /* |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2155 | * Process control tds, update urb status and actual_length. |
| 2156 | */ |
| 2157 | static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td, |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2158 | union xhci_trb *ep_trb, struct xhci_transfer_event *event, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2159 | struct xhci_virt_ep *ep) |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2160 | { |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2161 | struct xhci_ep_ctx *ep_ctx; |
| 2162 | u32 trb_comp_code; |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2163 | u32 remaining, requested; |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2164 | u32 trb_type; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2165 | |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2166 | trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3])); |
Mathias Nyman | ab58f3b | 2021-01-29 15:00:18 +0200 | [diff] [blame] | 2167 | ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2168 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2169 | requested = td->urb->transfer_buffer_length; |
| 2170 | remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); |
| 2171 | |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2172 | switch (trb_comp_code) { |
| 2173 | case COMP_SUCCESS: |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2174 | if (trb_type != TRB_STATUS) { |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2175 | xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n", |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2176 | (trb_type == TRB_DATA) ? "data" : "setup"); |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2177 | td->status = -ESHUTDOWN; |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2178 | break; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2179 | } |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2180 | td->status = 0; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2181 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2182 | case COMP_SHORT_PACKET: |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2183 | td->status = 0; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2184 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2185 | case COMP_STOPPED_SHORT_PACKET: |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2186 | if (trb_type == TRB_DATA || trb_type == TRB_NORMAL) |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2187 | td->urb->actual_length = remaining; |
Lu Baolu | 40a3b77 | 2015-08-06 19:24:01 +0300 | [diff] [blame] | 2188 | else |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2189 | xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n"); |
| 2190 | goto finish_td; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2191 | case COMP_STOPPED: |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2192 | 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 Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2198 | td->urb->actual_length = requested - remaining; |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2199 | goto finish_td; |
Mathias Nyman | 0ab2881 | 2017-03-28 15:55:29 +0300 | [diff] [blame] | 2200 | case TRB_STATUS: |
| 2201 | td->urb->actual_length = requested; |
| 2202 | goto finish_td; |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2203 | default: |
| 2204 | xhci_warn(xhci, "WARN: unexpected TRB Type %d\n", |
| 2205 | trb_type); |
| 2206 | goto finish_td; |
| 2207 | } |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2208 | case COMP_STOPPED_LENGTH_INVALID: |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2209 | goto finish_td; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2210 | default: |
| 2211 | if (!xhci_requires_manual_halt_cleanup(xhci, |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2212 | ep_ctx, trb_comp_code)) |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2213 | break; |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2214 | xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n", |
Mathias Nyman | ab58f3b | 2021-01-29 15:00:18 +0200 | [diff] [blame] | 2215 | trb_comp_code, ep->ep_index); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2216 | fallthrough; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2217 | case COMP_STALL_ERROR: |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2218 | /* Did we transfer part of the data (middle) phase? */ |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2219 | if (trb_type == TRB_DATA || trb_type == TRB_NORMAL) |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2220 | td->urb->actual_length = requested - remaining; |
Mathias Nyman | 22ae47e | 2015-05-29 17:01:53 +0300 | [diff] [blame] | 2221 | else if (!td->urb_length_set) |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2222 | td->urb->actual_length = 0; |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2223 | goto finish_td; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2224 | } |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2225 | |
| 2226 | /* stopped at setup stage, no data transferred */ |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2227 | if (trb_type == TRB_SETUP) |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2228 | goto finish_td; |
| 2229 | |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2230 | /* |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2231 | * 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 Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2233 | */ |
Felipe Balbi | 29fc1aa | 2017-01-03 18:28:53 +0200 | [diff] [blame] | 2234 | if (trb_type == TRB_DATA || |
| 2235 | trb_type == TRB_NORMAL) { |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2236 | 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 Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
Mathias Nyman | 0b6c324 | 2016-11-11 15:13:16 +0200 | [diff] [blame] | 2242 | /* at status stage */ |
| 2243 | if (!td->urb_length_set) |
| 2244 | td->urb->actual_length = requested; |
| 2245 | |
| 2246 | finish_td: |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2247 | return finish_td(xhci, td, event, ep); |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /* |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2251 | * Process isochronous tds, update urb packet status and actual_length. |
| 2252 | */ |
| 2253 | static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2254 | union xhci_trb *ep_trb, struct xhci_transfer_event *event, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2255 | struct xhci_virt_ep *ep) |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2256 | { |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2257 | struct urb_priv *urb_priv; |
| 2258 | int idx; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2259 | struct usb_iso_packet_descriptor *frame; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2260 | u32 trb_comp_code; |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2261 | bool sum_trbs_for_length = false; |
| 2262 | u32 remaining, requested, ep_trb_len; |
| 2263 | int short_framestatus; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2264 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2265 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2266 | urb_priv = td->urb->hcpriv; |
Mathias Nyman | 9ef7fbb | 2017-01-23 14:20:25 +0200 | [diff] [blame] | 2267 | idx = urb_priv->num_tds_done; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2268 | frame = &td->urb->iso_frame_desc[idx]; |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2269 | 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 Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2274 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2275 | /* handle completion code */ |
| 2276 | switch (trb_comp_code) { |
| 2277 | case COMP_SUCCESS: |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2278 | if (remaining) { |
| 2279 | frame->status = short_framestatus; |
| 2280 | if (xhci->quirks & XHCI_TRUST_TX_LENGTH) |
| 2281 | sum_trbs_for_length = true; |
Sarah Sharp | 1530bbc6 | 2012-05-08 09:22:49 -0700 | [diff] [blame] | 2282 | break; |
| 2283 | } |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2284 | frame->status = 0; |
| 2285 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2286 | case COMP_SHORT_PACKET: |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2287 | frame->status = short_framestatus; |
| 2288 | sum_trbs_for_length = true; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2289 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2290 | case COMP_BANDWIDTH_OVERRUN_ERROR: |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2291 | frame->status = -ECOMM; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2292 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2293 | case COMP_ISOCH_BUFFER_OVERRUN: |
| 2294 | case COMP_BABBLE_DETECTED_ERROR: |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2295 | frame->status = -EOVERFLOW; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2296 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2297 | case COMP_INCOMPATIBLE_DEVICE_ERROR: |
| 2298 | case COMP_STALL_ERROR: |
Mathias Nyman | d104d01 | 2015-04-30 17:16:02 +0300 | [diff] [blame] | 2299 | frame->status = -EPROTO; |
Mathias Nyman | d104d01 | 2015-04-30 17:16:02 +0300 | [diff] [blame] | 2300 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2301 | case COMP_USB_TRANSACTION_ERROR: |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2302 | frame->status = -EPROTO; |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2303 | if (ep_trb != td->last_trb) |
Mathias Nyman | d104d01 | 2015-04-30 17:16:02 +0300 | [diff] [blame] | 2304 | return 0; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2305 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2306 | case COMP_STOPPED: |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2307 | sum_trbs_for_length = true; |
| 2308 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2309 | case COMP_STOPPED_SHORT_PACKET: |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2310 | /* field normally containing residue now contains tranferred */ |
| 2311 | frame->status = short_framestatus; |
| 2312 | requested = remaining; |
| 2313 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2314 | case COMP_STOPPED_LENGTH_INVALID: |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2315 | requested = 0; |
| 2316 | remaining = 0; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2317 | break; |
| 2318 | default: |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2319 | sum_trbs_for_length = true; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2320 | frame->status = -1; |
| 2321 | break; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2324 | if (sum_trbs_for_length) |
Mathias Nyman | d4dff804 | 2021-01-29 15:00:19 +0200 | [diff] [blame] | 2325 | frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) + |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2326 | ep_trb_len - remaining; |
| 2327 | else |
| 2328 | frame->actual_length = requested; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2329 | |
Mathias Nyman | 36da3a1 | 2016-11-11 15:13:19 +0200 | [diff] [blame] | 2330 | td->urb->actual_length += frame->actual_length; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2331 | |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2332 | return finish_td(xhci, td, event, ep); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2335 | static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2336 | struct xhci_virt_ep *ep, int status) |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2337 | { |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2338 | struct urb_priv *urb_priv; |
| 2339 | struct usb_iso_packet_descriptor *frame; |
| 2340 | int idx; |
| 2341 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2342 | urb_priv = td->urb->hcpriv; |
Mathias Nyman | 9ef7fbb | 2017-01-23 14:20:25 +0200 | [diff] [blame] | 2343 | idx = urb_priv->num_tds_done; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2344 | frame = &td->urb->iso_frame_desc[idx]; |
| 2345 | |
Sarah Sharp | b3df3f9 | 2011-06-15 19:57:46 -0700 | [diff] [blame] | 2346 | /* The transfer is partly done. */ |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2347 | frame->status = -EXDEV; |
| 2348 | |
| 2349 | /* calc actual length */ |
| 2350 | frame->actual_length = 0; |
| 2351 | |
| 2352 | /* Update ring dequeue pointer */ |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 2353 | 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 Nyman | d4dff804 | 2021-01-29 15:00:19 +0200 | [diff] [blame] | 2356 | inc_deq(xhci, ep->ring); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2357 | |
Mathias Nyman | d4dff804 | 2021-01-29 15:00:19 +0200 | [diff] [blame] | 2358 | return xhci_td_cleanup(xhci, td, ep->ring, status); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2361 | /* |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2362 | * Process bulk and interrupt tds, update urb status and actual_length. |
| 2363 | */ |
| 2364 | static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td, |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2365 | union xhci_trb *ep_trb, struct xhci_transfer_event *event, |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2366 | struct xhci_virt_ep *ep) |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2367 | { |
Mathias Nyman | f8f80be | 2018-09-20 19:13:37 +0300 | [diff] [blame] | 2368 | struct xhci_slot_ctx *slot_ctx; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2369 | struct xhci_ring *ep_ring; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2370 | u32 trb_comp_code; |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2371 | u32 remaining, requested, ep_trb_len; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2372 | |
Mathias Nyman | ab58f3b | 2021-01-29 15:00:18 +0200 | [diff] [blame] | 2373 | slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2374 | 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 Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2376 | remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2377 | ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2378 | requested = td->urb->transfer_buffer_length; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2379 | |
| 2380 | switch (trb_comp_code) { |
| 2381 | case COMP_SUCCESS: |
Mathias Nyman | f8f80be | 2018-09-20 19:13:37 +0300 | [diff] [blame] | 2382 | ep_ring->err_count = 0; |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2383 | /* handle success with untransferred data as short packet */ |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2384 | if (ep_trb != td->last_trb || remaining) { |
Mathias Nyman | 52ab868 | 2016-11-11 15:13:15 +0200 | [diff] [blame] | 2385 | xhci_warn(xhci, "WARN Successful completion on short TX\n"); |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2386 | xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", |
| 2387 | td->urb->ep->desc.bEndpointAddress, |
| 2388 | requested, remaining); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2389 | } |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2390 | td->status = 0; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2391 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2392 | case COMP_SHORT_PACKET: |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2393 | xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", |
| 2394 | td->urb->ep->desc.bEndpointAddress, |
| 2395 | requested, remaining); |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2396 | td->status = 0; |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2397 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2398 | case COMP_STOPPED_SHORT_PACKET: |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2399 | td->urb->actual_length = remaining; |
| 2400 | goto finish_td; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2401 | case COMP_STOPPED_LENGTH_INVALID: |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2402 | /* stopped on ep trb with invalid length, exclude it */ |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2403 | ep_trb_len = 0; |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2404 | remaining = 0; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2405 | break; |
Mathias Nyman | f8f80be | 2018-09-20 19:13:37 +0300 | [diff] [blame] | 2406 | 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 Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2410 | |
| 2411 | td->status = 0; |
Mathias Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 2412 | |
| 2413 | xhci_handle_halted_endpoint(xhci, ep, ep_ring->stream_id, td, |
| 2414 | EP_SOFT_RESET); |
Mathias Nyman | f8f80be | 2018-09-20 19:13:37 +0300 | [diff] [blame] | 2415 | return 0; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2416 | default: |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2417 | /* do nothing */ |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2418 | break; |
| 2419 | } |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2420 | |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2421 | if (ep_trb == td->last_trb) |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2422 | td->urb->actual_length = requested - remaining; |
| 2423 | else |
Lu Baolu | 40a3b77 | 2015-08-06 19:24:01 +0300 | [diff] [blame] | 2424 | td->urb->actual_length = |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2425 | sum_trb_lengths(xhci, ep_ring, ep_trb) + |
| 2426 | ep_trb_len - remaining; |
Mathias Nyman | 30a65b4 | 2016-11-11 15:13:17 +0200 | [diff] [blame] | 2427 | finish_td: |
| 2428 | if (remaining > requested) { |
| 2429 | xhci_warn(xhci, "bad transfer trb length %d in event trb\n", |
| 2430 | remaining); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2431 | td->urb->actual_length = 0; |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2432 | } |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2433 | return finish_td(xhci, td, event, ep); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | /* |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2437 | * 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 | */ |
| 2441 | static int handle_tx_event(struct xhci_hcd *xhci, |
| 2442 | struct xhci_transfer_event *event) |
| 2443 | { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 2444 | struct xhci_virt_ep *ep; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2445 | struct xhci_ring *ep_ring; |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 2446 | unsigned int slot_id; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2447 | int ep_index; |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 2448 | struct xhci_td *td = NULL; |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2449 | dma_addr_t ep_trb_dma; |
| 2450 | struct xhci_segment *ep_seg; |
| 2451 | union xhci_trb *ep_trb; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2452 | int status = -EINPROGRESS; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2453 | struct xhci_ep_ctx *ep_ctx; |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2454 | struct list_head *tmp; |
Sarah Sharp | 66d1eeb | 2009-08-27 14:35:53 -0700 | [diff] [blame] | 2455 | u32 trb_comp_code; |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2456 | int td_num = 0; |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2457 | bool handling_skipped_tds = false; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2458 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2459 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2460 | 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 Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 2464 | ep = xhci_get_virt_ep(xhci, slot_id, ep_index); |
| 2465 | if (!ep) { |
| 2466 | xhci_err(xhci, "ERROR Invalid Transfer event\n"); |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2467 | goto err_out; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2470 | ep_ring = xhci_dma_to_transfer_ring(ep, ep_trb_dma); |
Mathias Nyman | b1adc42 | 2021-01-29 15:00:22 +0200 | [diff] [blame] | 2471 | ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2472 | |
Mathias Nyman | ade2e3a | 2017-06-15 11:55:46 +0300 | [diff] [blame] | 2473 | if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) { |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2474 | xhci_err(xhci, |
Mathias Nyman | ade2e3a | 2017-06-15 11:55:46 +0300 | [diff] [blame] | 2475 | "ERROR Transfer event for disabled endpoint slot %u ep %u\n", |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2476 | slot_id, ep_index); |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2477 | goto err_out; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
Mathias Nyman | ade2e3a | 2017-06-15 11:55:46 +0300 | [diff] [blame] | 2480 | /* 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 Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 2487 | xhci_handle_halted_endpoint(xhci, ep, 0, NULL, |
| 2488 | EP_SOFT_RESET); |
Mathias Nyman | ade2e3a | 2017-06-15 11:55:46 +0300 | [diff] [blame] | 2489 | goto cleanup; |
| 2490 | case COMP_RING_UNDERRUN: |
| 2491 | case COMP_RING_OVERRUN: |
Sandeep Singh | d9193ef | 2018-11-09 17:21:19 +0200 | [diff] [blame] | 2492 | case COMP_STOPPED_LENGTH_INVALID: |
Mathias Nyman | ade2e3a | 2017-06-15 11:55:46 +0300 | [diff] [blame] | 2493 | 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 Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2501 | /* 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 Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2507 | /* Look for common error cases */ |
Sarah Sharp | 66d1eeb | 2009-08-27 14:35:53 -0700 | [diff] [blame] | 2508 | switch (trb_comp_code) { |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2509 | /* Skip codes that require special handling depending on |
| 2510 | * transfer type |
| 2511 | */ |
| 2512 | case COMP_SUCCESS: |
Vivek Gautam | 1c11a17 | 2013-03-21 12:06:48 +0530 | [diff] [blame] | 2513 | if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0) |
Sarah Sharp | 1530bbc6 | 2012-05-08 09:22:49 -0700 | [diff] [blame] | 2514 | break; |
Mathias Nyman | 7ff1116 | 2019-12-11 16:20:06 +0200 | [diff] [blame] | 2515 | if (xhci->quirks & XHCI_TRUST_TX_LENGTH || |
| 2516 | ep_ring->last_td_was_short) |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2517 | trb_comp_code = COMP_SHORT_PACKET; |
Sarah Sharp | 1530bbc6 | 2012-05-08 09:22:49 -0700 | [diff] [blame] | 2518 | else |
Sarah Sharp | 8202ce2 | 2012-07-25 10:52:45 -0700 | [diff] [blame] | 2519 | xhci_warn_ratelimited(xhci, |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2520 | "WARN Successful completion on short TX for slot %u ep %u: needs XHCI_TRUST_TX_LENGTH quirk?\n", |
| 2521 | slot_id, ep_index); |
Nick Desaulniers | 1d6903a | 2020-11-10 17:47:14 -0800 | [diff] [blame] | 2522 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2523 | case COMP_SHORT_PACKET: |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2524 | break; |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2525 | /* Completion codes for endpoint stopped state */ |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2526 | case COMP_STOPPED: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2527 | xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n", |
| 2528 | slot_id, ep_index); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 2529 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2530 | case COMP_STOPPED_LENGTH_INVALID: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2531 | xhci_dbg(xhci, |
| 2532 | "Stopped on No-op or Link TRB for slot %u ep %u\n", |
| 2533 | slot_id, ep_index); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 2534 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2535 | case COMP_STOPPED_SHORT_PACKET: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2536 | xhci_dbg(xhci, |
| 2537 | "Stopped with short packet transfer detected for slot %u ep %u\n", |
| 2538 | slot_id, ep_index); |
Lu Baolu | 40a3b77 | 2015-08-06 19:24:01 +0300 | [diff] [blame] | 2539 | break; |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2540 | /* Completion codes for endpoint halted state */ |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2541 | case COMP_STALL_ERROR: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2542 | xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id, |
| 2543 | ep_index); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 2544 | ep->ep_state |= EP_HALTED; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2545 | status = -EPIPE; |
| 2546 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2547 | case COMP_SPLIT_TRANSACTION_ERROR: |
Mathias Nyman | 76eac5d | 2020-03-12 16:45:10 +0200 | [diff] [blame] | 2548 | xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n", |
| 2549 | slot_id, ep_index); |
| 2550 | status = -EPROTO; |
| 2551 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2552 | case COMP_USB_TRANSACTION_ERROR: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2553 | xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n", |
| 2554 | slot_id, ep_index); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2555 | status = -EPROTO; |
| 2556 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2557 | case COMP_BABBLE_DETECTED_ERROR: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2558 | xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n", |
| 2559 | slot_id, ep_index); |
Sarah Sharp | 4a73143 | 2009-07-27 12:04:32 -0700 | [diff] [blame] | 2560 | status = -EOVERFLOW; |
| 2561 | break; |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2562 | /* 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 Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2570 | case COMP_DATA_BUFFER_ERROR: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2571 | xhci_warn(xhci, |
| 2572 | "WARN: HC couldn't access mem fast enough for slot %u ep %u\n", |
| 2573 | slot_id, ep_index); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2574 | status = -ENOSR; |
| 2575 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2576 | case COMP_BANDWIDTH_OVERRUN_ERROR: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2577 | xhci_warn(xhci, |
| 2578 | "WARN: bandwidth overrun event for slot %u ep %u on endpoint\n", |
| 2579 | slot_id, ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2580 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2581 | case COMP_ISOCH_BUFFER_OVERRUN: |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2582 | xhci_warn(xhci, |
| 2583 | "WARN: buffer overrun event for slot %u ep %u on endpoint", |
| 2584 | slot_id, ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2585 | break; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2586 | case COMP_RING_UNDERRUN: |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2587 | /* |
| 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 Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2596 | TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), |
| 2597 | ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2598 | goto cleanup; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2599 | case COMP_RING_OVERRUN: |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2600 | 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 Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2604 | TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), |
| 2605 | ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2606 | goto cleanup; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2607 | case COMP_MISSED_SERVICE_ERROR: |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2608 | /* |
| 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 Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2615 | xhci_dbg(xhci, |
| 2616 | "Miss service interval error for slot %u ep %u, set skip flag\n", |
| 2617 | slot_id, ep_index); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2618 | goto cleanup; |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2619 | case COMP_NO_PING_RESPONSE_ERROR: |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2620 | ep->skip = true; |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2621 | xhci_dbg(xhci, |
| 2622 | "No Ping response error for slot %u ep %u, Skip one Isoc TD\n", |
| 2623 | slot_id, ep_index); |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2624 | goto cleanup; |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2625 | |
| 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 Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2633 | default: |
Sarah Sharp | b45b506 | 2009-12-09 15:59:06 -0800 | [diff] [blame] | 2634 | if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { |
Sarah Sharp | 5ad6a52 | 2009-11-11 10:28:40 -0800 | [diff] [blame] | 2635 | status = 0; |
| 2636 | break; |
| 2637 | } |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2638 | 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 Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2641 | goto cleanup; |
| 2642 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2643 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2644 | 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 Sharp | a83d675 | 2013-03-18 10:19:51 -0700 | [diff] [blame] | 2649 | /* |
Mathias Nyman | e4ec40e | 2017-12-01 13:41:19 +0200 | [diff] [blame] | 2650 | * 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 Sharp | a83d675 | 2013-03-18 10:19:51 -0700 | [diff] [blame] | 2655 | */ |
Mathias Nyman | e4ec40e | 2017-12-01 13:41:19 +0200 | [diff] [blame] | 2656 | |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2657 | if (!(trb_comp_code == COMP_STOPPED || |
Mathias Nyman | e4ec40e | 2017-12-01 13:41:19 +0200 | [diff] [blame] | 2658 | trb_comp_code == COMP_STOPPED_LENGTH_INVALID || |
| 2659 | ep_ring->last_td_was_short)) { |
Sarah Sharp | a83d675 | 2013-03-18 10:19:51 -0700 | [diff] [blame] | 2660 | 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 Sharp | a83d675 | 2013-03-18 10:19:51 -0700 | [diff] [blame] | 2663 | } |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2664 | if (ep->skip) { |
| 2665 | ep->skip = false; |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2666 | 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 Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2668 | } |
Mathias Nyman | 93ceaa8 | 2020-04-21 17:08:20 +0300 | [diff] [blame] | 2669 | if (trb_comp_code == COMP_STALL_ERROR || |
| 2670 | xhci_requires_manual_halt_cleanup(xhci, ep_ctx, |
| 2671 | trb_comp_code)) { |
Mathias Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 2672 | xhci_handle_halted_endpoint(xhci, ep, |
| 2673 | ep_ring->stream_id, |
| 2674 | NULL, |
| 2675 | EP_HARD_RESET); |
Mathias Nyman | 93ceaa8 | 2020-04-21 17:08:20 +0300 | [diff] [blame] | 2676 | } |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2677 | goto cleanup; |
| 2678 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2679 | |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2680 | /* 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 Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2683 | 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 Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2685 | goto cleanup; |
| 2686 | } |
| 2687 | |
Felipe Balbi | 04861f8 | 2017-01-23 14:20:09 +0200 | [diff] [blame] | 2688 | td = list_first_entry(&ep_ring->td_list, struct xhci_td, |
| 2689 | td_list); |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2690 | if (ep->skip) |
| 2691 | td_num--; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2692 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2693 | /* Is this a TRB in the currently executing TD? */ |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2694 | ep_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue, |
| 2695 | td->last_trb, ep_trb_dma, false); |
Alex He | e1cf486 | 2011-06-03 15:58:25 +0800 | [diff] [blame] | 2696 | |
| 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 Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2705 | if (!ep_seg && (trb_comp_code == COMP_STOPPED || |
| 2706 | trb_comp_code == COMP_STOPPED_LENGTH_INVALID)) { |
Alex He | e1cf486 | 2011-06-03 15:58:25 +0800 | [diff] [blame] | 2707 | goto cleanup; |
| 2708 | } |
| 2709 | |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2710 | if (!ep_seg) { |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2711 | if (!ep->skip || |
| 2712 | !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { |
Sarah Sharp | ad80833 | 2011-05-25 10:43:56 -0700 | [diff] [blame] | 2713 | /* Some host controllers give a spurious |
| 2714 | * successful event after a short transfer. |
| 2715 | * Ignore it. |
| 2716 | */ |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 2717 | if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && |
Sarah Sharp | ad80833 | 2011-05-25 10:43:56 -0700 | [diff] [blame] | 2718 | ep_ring->last_td_was_short) { |
| 2719 | ep_ring->last_td_was_short = false; |
Sarah Sharp | ad80833 | 2011-05-25 10:43:56 -0700 | [diff] [blame] | 2720 | goto cleanup; |
| 2721 | } |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2722 | /* HC is busted, give up! */ |
| 2723 | xhci_err(xhci, |
| 2724 | "ERROR Transfer event TRB DMA ptr not " |
Hans de Goede | cffb9be | 2014-08-20 16:41:51 +0300 | [diff] [blame] | 2725 | "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 Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2730 | ep_trb_dma, true); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2731 | return -ESHUTDOWN; |
| 2732 | } |
| 2733 | |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2734 | skip_isoc_td(xhci, td, ep, status); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2735 | goto cleanup; |
| 2736 | } |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2737 | if (trb_comp_code == COMP_SHORT_PACKET) |
Sarah Sharp | ad80833 | 2011-05-25 10:43:56 -0700 | [diff] [blame] | 2738 | ep_ring->last_td_was_short = true; |
| 2739 | else |
| 2740 | ep_ring->last_td_was_short = false; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2741 | |
| 2742 | if (ep->skip) { |
Zhengjun Xing | b7f769a | 2017-04-07 17:56:59 +0300 | [diff] [blame] | 2743 | xhci_dbg(xhci, |
| 2744 | "Found td. Clear skip flag for slot %u ep %u.\n", |
| 2745 | slot_id, ep_index); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2746 | ep->skip = false; |
| 2747 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2748 | |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2749 | ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / |
| 2750 | sizeof(*ep_trb)]; |
Felipe Balbi | a37c3f7 | 2017-01-23 14:20:19 +0200 | [diff] [blame] | 2751 | |
| 2752 | trace_xhci_handle_transfer(ep_ring, |
| 2753 | (struct xhci_generic_trb *) ep_trb); |
| 2754 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2755 | /* |
Lu Baolu | 810a624 | 2017-10-06 17:45:29 +0300 | [diff] [blame] | 2756 | * 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 Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2761 | */ |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2762 | |
Mathias Nyman | f97c08a | 2016-11-11 15:13:18 +0200 | [diff] [blame] | 2763 | if (trb_is_noop(ep_trb)) { |
Lu Baolu | 810a624 | 2017-10-06 17:45:29 +0300 | [diff] [blame] | 2764 | if (trb_comp_code == COMP_STALL_ERROR || |
| 2765 | xhci_requires_manual_halt_cleanup(xhci, ep_ctx, |
| 2766 | trb_comp_code)) |
Mathias Nyman | 7c6c334 | 2021-01-29 15:00:37 +0200 | [diff] [blame] | 2767 | xhci_handle_halted_endpoint(xhci, ep, |
| 2768 | ep_ring->stream_id, |
| 2769 | td, EP_HARD_RESET); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2770 | goto cleanup; |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2771 | } |
| 2772 | |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2773 | td->status = status; |
| 2774 | |
Mathias Nyman | 0c03d89 | 2016-11-11 15:13:23 +0200 | [diff] [blame] | 2775 | /* update the urb's actual_length and give back to the core */ |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2776 | if (usb_endpoint_xfer_control(&td->urb->ep->desc)) |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2777 | process_ctrl_td(xhci, td, ep_trb, event, ep); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2778 | else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2779 | process_isoc_td(xhci, td, ep_trb, event, ep); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2780 | else |
Mathias Nyman | a6ccd1f | 2021-01-29 15:00:35 +0200 | [diff] [blame] | 2781 | process_bulk_intr_td(xhci, td, ep_trb, event, ep); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2782 | cleanup: |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2783 | handling_skipped_tds = ep->skip && |
Felipe Balbi | 0b7c105 | 2017-01-23 14:20:06 +0200 | [diff] [blame] | 2784 | trb_comp_code != COMP_MISSED_SERVICE_ERROR && |
| 2785 | trb_comp_code != COMP_NO_PING_RESPONSE_ERROR; |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2786 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2787 | /* |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2788 | * Do not update event ring dequeue pointer if we're in a loop |
| 2789 | * processing missed tds. |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 2790 | */ |
Mathias Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2791 | if (!handling_skipped_tds) |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 2792 | inc_deq(xhci, xhci->event_ring); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2793 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2794 | /* |
| 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 Nyman | 3b4739b8 | 2015-10-12 11:30:12 +0300 | [diff] [blame] | 2800 | } while (handling_skipped_tds); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2801 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2802 | return 0; |
Mathias Nyman | b336838 | 2017-06-15 11:55:43 +0300 | [diff] [blame] | 2803 | |
| 2804 | err_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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | /* |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2817 | * 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 Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2819 | * 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 Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2821 | */ |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2822 | static int xhci_handle_event(struct xhci_hcd *xhci) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2823 | { |
| 2824 | union xhci_trb *event; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2825 | int update_ptrs = 1; |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2826 | u32 trb_type; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2827 | int ret; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2828 | |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 2829 | /* Event ring hasn't been allocated yet. */ |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2830 | if (!xhci->event_ring || !xhci->event_ring->dequeue) { |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 2831 | xhci_err(xhci, "ERROR event ring not ready\n"); |
| 2832 | return -ENOMEM; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | event = xhci->event_ring->dequeue; |
| 2836 | /* Does the HC or OS own the TRB? */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2837 | if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 2838 | xhci->event_ring->cycle_state) |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2839 | return 0; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2840 | |
Felipe Balbi | a37c3f7 | 2017-01-23 14:20:19 +0200 | [diff] [blame] | 2841 | trace_xhci_handle_event(xhci->event_ring, &event->generic); |
| 2842 | |
Matt Evans | 92a3da4 | 2011-03-29 13:40:51 +1100 | [diff] [blame] | 2843 | /* |
| 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 Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2848 | trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2849 | /* FIXME: Handle more event types. */ |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2850 | |
| 2851 | switch (trb_type) { |
| 2852 | case TRB_COMPLETION: |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2853 | handle_cmd_completion(xhci, &event->event_cmd); |
| 2854 | break; |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2855 | case TRB_PORT_STATUS: |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2856 | handle_port_status(xhci, event); |
| 2857 | update_ptrs = 0; |
| 2858 | break; |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2859 | case TRB_TRANSFER: |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2860 | ret = handle_tx_event(xhci, &event->trans_event); |
Lu Baolu | f4c8f03 | 2016-11-11 15:13:25 +0200 | [diff] [blame] | 2861 | if (ret >= 0) |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2862 | update_ptrs = 0; |
| 2863 | break; |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2864 | case TRB_DEV_NOTE: |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 2865 | handle_device_notification(xhci, event); |
| 2866 | break; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2867 | default: |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2868 | if (trb_type >= TRB_VENDOR_DEFINED_LOW) |
| 2869 | handle_vendor_event(xhci, event, trb_type); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 2870 | else |
Mathias Nyman | 0353810 | 2021-01-29 15:00:29 +0200 | [diff] [blame] | 2871 | xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2872 | } |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2873 | /* 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 Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2879 | return 0; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2880 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2881 | |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2882 | if (update_ptrs) |
| 2883 | /* Update SW event ring dequeue pointer */ |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 2884 | inc_deq(xhci, xhci->event_ring); |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2885 | |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2886 | /* Are there more items on the event ring? Caller will call us again to |
| 2887 | * check. |
| 2888 | */ |
| 2889 | return 1; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2890 | } |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2891 | |
| 2892 | /* |
Peter Chen | dc0ffbe | 2019-11-15 18:50:00 +0200 | [diff] [blame] | 2893 | * Update Event Ring Dequeue Pointer: |
| 2894 | * - When all events have finished |
| 2895 | * - To avoid "Event Ring Full Error" condition |
| 2896 | */ |
| 2897 | static 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 Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2929 | * 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 | */ |
| 2933 | irqreturn_t xhci_irq(struct usb_hcd *hcd) |
| 2934 | { |
| 2935 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2936 | union xhci_trb *event_ring_deq; |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 2937 | irqreturn_t ret = IRQ_NONE; |
Alan Stern | 63aea0d | 2017-05-17 18:32:03 +0300 | [diff] [blame] | 2938 | unsigned long flags; |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 2939 | u64 temp_64; |
| 2940 | u32 status; |
Peter Chen | dc0ffbe | 2019-11-15 18:50:00 +0200 | [diff] [blame] | 2941 | int event_loop = 0; |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2942 | |
Alan Stern | 63aea0d | 2017-05-17 18:32:03 +0300 | [diff] [blame] | 2943 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2944 | /* Check if the xHC generated the interrupt, or the irq is shared */ |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 2945 | status = readl(&xhci->op_regs->status); |
Mathias Nyman | d9f11ba | 2017-04-07 17:57:01 +0300 | [diff] [blame] | 2946 | if (status == ~(u32)0) { |
| 2947 | xhci_hc_died(xhci); |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 2948 | ret = IRQ_HANDLED; |
| 2949 | goto out; |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2950 | } |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 2951 | |
| 2952 | if (!(status & STS_EINT)) |
| 2953 | goto out; |
| 2954 | |
Sarah Sharp | 27e0dd4 | 2010-07-29 22:12:43 -0700 | [diff] [blame] | 2955 | if (status & STS_FATAL) { |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2956 | xhci_warn(xhci, "WARNING: Host System Error\n"); |
| 2957 | xhci_halt(xhci); |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 2958 | ret = IRQ_HANDLED; |
| 2959 | goto out; |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2962 | /* |
| 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 Sharp | 27e0dd4 | 2010-07-29 22:12:43 -0700 | [diff] [blame] | 2967 | status |= STS_EINT; |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 2968 | writel(status, &xhci->op_regs->status); |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2969 | |
Peter Chen | 6a29bee | 2017-05-17 18:32:02 +0300 | [diff] [blame] | 2970 | if (!hcd->msi_enabled) { |
Sarah Sharp | c21599a | 2010-07-29 22:13:00 -0700 | [diff] [blame] | 2971 | u32 irq_pending; |
Xenia Ragiadakou | b0ba972 | 2013-11-15 05:34:06 +0200 | [diff] [blame] | 2972 | irq_pending = readl(&xhci->ir_set->irq_pending); |
Felipe Balbi | 4e833c0 | 2012-03-15 16:37:08 +0200 | [diff] [blame] | 2973 | irq_pending |= IMAN_IP; |
Xenia Ragiadakou | 204b779 | 2013-11-15 05:34:07 +0200 | [diff] [blame] | 2974 | writel(irq_pending, &xhci->ir_set->irq_pending); |
Sarah Sharp | c21599a | 2010-07-29 22:13:00 -0700 | [diff] [blame] | 2975 | } |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2976 | |
Gabriel Krisman Bertazi | 27a41a8 | 2016-06-01 18:09:07 +0300 | [diff] [blame] | 2977 | if (xhci->xhc_state & XHCI_STATE_DYING || |
| 2978 | xhci->xhc_state & XHCI_STATE_HALTED) { |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2979 | xhci_dbg(xhci, "xHCI dying, ignoring interrupt. " |
| 2980 | "Shouldn't IRQs be disabled?\n"); |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2981 | /* Clear the event handler busy flag (RW1C); |
| 2982 | * the event ring should be empty. |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2983 | */ |
Sarah Sharp | f7b2e40 | 2014-01-30 13:27:49 -0800 | [diff] [blame] | 2984 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
Sarah Sharp | 477632d | 2014-01-29 14:02:00 -0800 | [diff] [blame] | 2985 | xhci_write_64(xhci, temp_64 | ERST_EHB, |
| 2986 | &xhci->ir_set->erst_dequeue); |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 2987 | ret = IRQ_HANDLED; |
| 2988 | goto out; |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2989 | } |
| 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 Chen | dc0ffbe | 2019-11-15 18:50:00 +0200 | [diff] [blame] | 2995 | 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 Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 3000 | } |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 3001 | |
Peter Chen | dc0ffbe | 2019-11-15 18:50:00 +0200 | [diff] [blame] | 3002 | xhci_update_erst_dequeue(xhci, event_ring_deq); |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 3003 | ret = IRQ_HANDLED; |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 3004 | |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 3005 | out: |
Alan Stern | 63aea0d | 2017-05-17 18:32:03 +0300 | [diff] [blame] | 3006 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 3007 | |
Felipe Balbi | 76a3529 | 2017-01-23 14:20:07 +0200 | [diff] [blame] | 3008 | return ret; |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Alex Shi | 851ec16 | 2013-05-24 10:54:19 +0800 | [diff] [blame] | 3011 | irqreturn_t xhci_msi_irq(int irq, void *hcd) |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 3012 | { |
Alan Stern | 968b822 | 2011-11-03 12:03:38 -0400 | [diff] [blame] | 3013 | return xhci_irq(hcd); |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 3014 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3015 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3016 | /**** Endpoint Ring Operations ****/ |
| 3017 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3018 | /* |
| 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 Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 3021 | * |
| 3022 | * @more_trbs_coming: Will you enqueue more TRBs before calling |
| 3023 | * prepare_transfer()? |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3024 | */ |
| 3025 | static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3026 | bool more_trbs_coming, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3027 | u32 field1, u32 field2, u32 field3, u32 field4) |
| 3028 | { |
| 3029 | struct xhci_generic_trb *trb; |
| 3030 | |
| 3031 | trb = &ring->enqueue->generic; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3032 | trb->field[0] = cpu_to_le32(field1); |
| 3033 | trb->field[1] = cpu_to_le32(field2); |
| 3034 | trb->field[2] = cpu_to_le32(field3); |
Mathias Nyman | 576667b | 2021-01-15 18:19:06 +0200 | [diff] [blame] | 3035 | /* make sure TRB is fully written before giving it to the controller */ |
| 3036 | wmb(); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3037 | trb->field[3] = cpu_to_le32(field4); |
Felipe Balbi | a37c3f7 | 2017-01-23 14:20:19 +0200 | [diff] [blame] | 3038 | |
| 3039 | trace_xhci_queue_trb(ring, trb); |
| 3040 | |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3041 | inc_enq(xhci, ring, more_trbs_coming); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3044 | /* |
| 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 | */ |
| 3048 | static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3049 | u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3050 | { |
Andiry Xu | 8dfec61 | 2012-03-05 17:49:37 +0800 | [diff] [blame] | 3051 | unsigned int num_trbs_needed; |
Mathias Nyman | 04d21f7 | 2021-01-29 15:00:26 +0200 | [diff] [blame] | 3052 | unsigned int link_trb_count = 0; |
Andiry Xu | 8dfec61 | 2012-03-05 17:49:37 +0800 | [diff] [blame] | 3053 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3054 | /* Make sure the endpoint has been added to xHC schedule */ |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3055 | 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3063 | case EP_STATE_ERROR: |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 3064 | xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3065 | /* 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 Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 3068 | case EP_STATE_HALTED: |
| 3069 | xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); |
Nick Desaulniers | 1d6903a | 2020-11-10 17:47:14 -0800 | [diff] [blame] | 3070 | break; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3071 | 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 Xu | 8dfec61 | 2012-03-05 17:49:37 +0800 | [diff] [blame] | 3082 | |
| 3083 | while (1) { |
Sarah Sharp | 3d4b81e | 2014-01-31 11:52:57 -0800 | [diff] [blame] | 3084 | if (room_on_ring(xhci, ep_ring, num_trbs)) |
| 3085 | break; |
Andiry Xu | 8dfec61 | 2012-03-05 17:49:37 +0800 | [diff] [blame] | 3086 | |
| 3087 | if (ep_ring == xhci->cmd_ring) { |
| 3088 | xhci_err(xhci, "Do not support expand command ring\n"); |
| 3089 | return -ENOMEM; |
| 3090 | } |
| 3091 | |
Xenia Ragiadakou | 68ffb01 | 2013-08-14 06:33:56 +0300 | [diff] [blame] | 3092 | xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, |
| 3093 | "ERROR no room on ep ring, try ring expansion"); |
Andiry Xu | 8dfec61 | 2012-03-05 17:49:37 +0800 | [diff] [blame] | 3094 | 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 Tschudin | 261fa12 | 2012-09-12 19:03:17 +0200 | [diff] [blame] | 3100 | } |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 3101 | |
Mathias Nyman | d0c77d8 | 2016-06-21 10:58:07 +0300 | [diff] [blame] | 3102 | 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 Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 3114 | |
Mathias Nyman | d0c77d8 | 2016-06-21 10:58:07 +0300 | [diff] [blame] | 3115 | wmb(); |
| 3116 | ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE); |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 3117 | |
Mathias Nyman | d0c77d8 | 2016-06-21 10:58:07 +0300 | [diff] [blame] | 3118 | /* 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 Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 3121 | |
Mathias Nyman | d0c77d8 | 2016-06-21 10:58:07 +0300 | [diff] [blame] | 3122 | ep_ring->enq_seg = ep_ring->enq_seg->next; |
| 3123 | ep_ring->enqueue = ep_ring->enq_seg->trbs; |
Mathias Nyman | 04d21f7 | 2021-01-29 15:00:26 +0200 | [diff] [blame] | 3124 | |
| 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 Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 3130 | } |
Mathias Nyman | c716e8a | 2021-01-29 15:00:30 +0200 | [diff] [blame] | 3131 | |
| 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3137 | return 0; |
| 3138 | } |
| 3139 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3140 | static int prepare_transfer(struct xhci_hcd *xhci, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3141 | struct xhci_virt_device *xdev, |
| 3142 | unsigned int ep_index, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3143 | unsigned int stream_id, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3144 | unsigned int num_trbs, |
| 3145 | struct urb *urb, |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3146 | unsigned int td_index, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3147 | gfp_t mem_flags) |
| 3148 | { |
| 3149 | int ret; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3150 | struct urb_priv *urb_priv; |
| 3151 | struct xhci_td *td; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3152 | struct xhci_ring *ep_ring; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 3153 | struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3154 | |
Mathias Nyman | c089cad | 2021-01-29 15:00:25 +0200 | [diff] [blame] | 3155 | ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index, |
| 3156 | stream_id); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3157 | 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 Nyman | 5071e6b | 2016-11-11 15:13:28 +0200 | [diff] [blame] | 3163 | ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3164 | num_trbs, mem_flags); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3165 | if (ret) |
| 3166 | return ret; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3167 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3168 | urb_priv = urb->hcpriv; |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 3169 | td = &urb_priv->td[td_index]; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3170 | |
| 3171 | INIT_LIST_HEAD(&td->td_list); |
| 3172 | INIT_LIST_HEAD(&td->cancelled_td_list); |
| 3173 | |
| 3174 | if (td_index == 0) { |
Sarah Sharp | 214f76f | 2010-10-26 11:22:02 -0700 | [diff] [blame] | 3175 | ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); |
Sarah Sharp | d13565c | 2011-07-22 14:34:34 -0700 | [diff] [blame] | 3176 | if (unlikely(ret)) |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3177 | return ret; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3180 | td->urb = urb; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3181 | /* Add this TD to the tail of the endpoint ring's TD list */ |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3182 | 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3186 | return 0; |
| 3187 | } |
| 3188 | |
Lu Baolu | 67d2ea9 | 2017-12-08 17:59:09 +0200 | [diff] [blame] | 3189 | unsigned int count_trbs(u64 addr, u64 len) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3190 | { |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3191 | unsigned int num_trbs; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3192 | |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3193 | 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 Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3197 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3198 | return num_trbs; |
| 3199 | } |
| 3200 | |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3201 | static inline unsigned int count_trbs_needed(struct urb *urb) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3202 | { |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3203 | return count_trbs(urb->transfer_dma, urb->transfer_buffer_length); |
| 3204 | } |
| 3205 | |
| 3206 | static 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 | |
| 3225 | static 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 | |
| 3235 | static void check_trb_math(struct urb *urb, int running_total) |
| 3236 | { |
| 3237 | if (unlikely(running_total != urb->transfer_buffer_length)) |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 3238 | dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3239 | "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 Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3247 | static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3248 | unsigned int ep_index, unsigned int stream_id, int start_cycle, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 3249 | struct xhci_generic_trb *start_trb) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3250 | { |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3251 | /* |
| 3252 | * Pass all the TRBs to the hardware at once and make sure this write |
| 3253 | * isn't reordered. |
| 3254 | */ |
| 3255 | wmb(); |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3256 | if (start_cycle) |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3257 | start_trb->field[3] |= cpu_to_le32(start_cycle); |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3258 | else |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3259 | start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 3260 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3261 | } |
| 3262 | |
Alexandr Ivanov | 7814015 | 2016-04-22 13:17:11 +0300 | [diff] [blame] | 3263 | static void check_interval(struct xhci_hcd *xhci, struct urb *urb, |
| 3264 | struct xhci_ep_ctx *ep_ctx) |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3265 | { |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3266 | int xhci_interval; |
| 3267 | int ep_interval; |
| 3268 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3269 | xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3270 | ep_interval = urb->interval; |
Alexandr Ivanov | 7814015 | 2016-04-22 13:17:11 +0300 | [diff] [blame] | 3271 | |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3272 | /* Convert to microframes */ |
| 3273 | if (urb->dev->speed == USB_SPEED_LOW || |
| 3274 | urb->dev->speed == USB_SPEED_FULL) |
| 3275 | ep_interval *= 8; |
Alexandr Ivanov | 7814015 | 2016-04-22 13:17:11 +0300 | [diff] [blame] | 3276 | |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3277 | /* 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 Kasatkin | 0730d52 | 2013-08-27 17:47:35 +0300 | [diff] [blame] | 3281 | 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 Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3285 | 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 Ivanov | 7814015 | 2016-04-22 13:17:11 +0300 | [diff] [blame] | 3291 | } |
| 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 | */ |
| 3299 | int 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 Carpenter | 3fc8206 | 2012-03-28 10:30:26 +0300 | [diff] [blame] | 3307 | return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index); |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 3308 | } |
| 3309 | |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 3310 | /* |
Sarah Sharp | 4525c0a | 2012-10-25 15:56:40 -0700 | [diff] [blame] | 3311 | * 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 Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3313 | * |
| 3314 | * Total TD packet count = total_packet_count = |
Sarah Sharp | 4525c0a | 2012-10-25 15:56:40 -0700 | [diff] [blame] | 3315 | * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize) |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3316 | * |
| 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 Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3322 | * 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 Sharp | 4525c0a | 2012-10-25 15:56:40 -0700 | [diff] [blame] | 3328 | * The last TRB in a TD must have the TD size set to zero. |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3329 | */ |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3330 | static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred, |
| 3331 | int trb_buff_len, unsigned int td_total_len, |
Mathias Nyman | 124c393 | 2016-06-21 10:57:59 +0300 | [diff] [blame] | 3332 | struct urb *urb, bool more_trbs_coming) |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3333 | { |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3334 | u32 maxp, total_packet_count; |
| 3335 | |
Chunfeng Yun | 72b663a | 2017-12-08 18:10:06 +0200 | [diff] [blame] | 3336 | /* MTK xHCI 0.96 contains some features from 1.0 */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 3337 | if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST)) |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3338 | return ((td_total_len - transferred) >> 10); |
| 3339 | |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 3340 | /* One TRB with a zero-length data packet. */ |
Mathias Nyman | 124c393 | 2016-06-21 10:57:59 +0300 | [diff] [blame] | 3341 | if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) || |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3342 | trb_buff_len == td_total_len) |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 3343 | return 0; |
| 3344 | |
Chunfeng Yun | 72b663a | 2017-12-08 18:10:06 +0200 | [diff] [blame] | 3345 | /* 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 Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 3347 | trb_buff_len = 0; |
| 3348 | |
Felipe Balbi | 734d3dd | 2016-09-28 13:46:37 +0300 | [diff] [blame] | 3349 | maxp = usb_endpoint_maxp(&urb->ep->desc); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 3350 | total_packet_count = DIV_ROUND_UP(td_total_len, maxp); |
| 3351 | |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3352 | /* Queueing functions don't count the current TRB into transferred */ |
| 3353 | return (total_packet_count - ((transferred + trb_buff_len) / maxp)); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3356 | |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3357 | static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len, |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3358 | u32 *trb_buff_len, struct xhci_segment *seg) |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3359 | { |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3360 | struct device *dev = xhci_to_hcd(xhci)->self.controller; |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3361 | unsigned int unalign; |
| 3362 | unsigned int max_pkt; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3363 | u32 new_buff_len; |
Henry Lin | 597c56e | 2019-05-22 14:33:57 +0300 | [diff] [blame] | 3364 | size_t len; |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3365 | |
Felipe Balbi | 734d3dd | 2016-09-28 13:46:37 +0300 | [diff] [blame] | 3366 | max_pkt = usb_endpoint_maxp(&urb->ep->desc); |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3367 | 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 Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3373 | xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n", |
| 3374 | unalign, *trb_buff_len); |
| 3375 | |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3376 | /* is the last nornal TRB alignable by splitting it */ |
| 3377 | if (*trb_buff_len > unalign) { |
| 3378 | *trb_buff_len -= unalign; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3379 | xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len); |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3380 | return 0; |
| 3381 | } |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3382 | |
| 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 Lin | 597c56e | 2019-05-22 14:33:57 +0300 | [diff] [blame] | 3395 | len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3396 | seg->bounce_buf, new_buff_len, enqd_len); |
Mathias Nyman | c03101ff | 2019-10-04 14:59:26 +0300 | [diff] [blame] | 3397 | if (len != new_buff_len) |
Henry Lin | 597c56e | 2019-05-22 14:33:57 +0300 | [diff] [blame] | 3398 | xhci_warn(xhci, |
Fabio Estevam | c1a145a | 2019-05-22 10:35:29 -0300 | [diff] [blame] | 3399 | "WARN Wrong bounce buffer write length: %zu != %d\n", |
Mathias Nyman | c03101ff | 2019-10-04 14:59:26 +0300 | [diff] [blame] | 3400 | len, new_buff_len); |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3401 | 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 Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3419 | return 1; |
| 3420 | } |
| 3421 | |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3422 | /* This is very similar to what ehci-q.c qtd_fill() does */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3423 | int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3424 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 3425 | { |
Mathias Nyman | 5a5a0b1 | 2016-06-21 10:57:57 +0300 | [diff] [blame] | 3426 | struct xhci_ring *ring; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3427 | struct urb_priv *urb_priv; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3428 | struct xhci_td *td; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3429 | struct xhci_generic_trb *start_trb; |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3430 | struct scatterlist *sg = NULL; |
Mathias Nyman | 5a83f04 | 2016-06-21 10:57:58 +0300 | [diff] [blame] | 3431 | bool more_trbs_coming = true; |
| 3432 | bool need_zero_pkt = false; |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3433 | bool first_trb = true; |
| 3434 | unsigned int num_trbs; |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3435 | unsigned int start_cycle, num_sgs = 0; |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3436 | unsigned int enqd_len, block_len, trb_buff_len, full_len; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3437 | int sent_len, ret; |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3438 | u32 field, length_field, remainder; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3439 | u64 addr, send_addr; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3440 | |
Mathias Nyman | 5a5a0b1 | 2016-06-21 10:57:57 +0300 | [diff] [blame] | 3441 | ring = xhci_urb_to_transfer_ring(xhci, urb); |
| 3442 | if (!ring) |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3443 | return -EINVAL; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3444 | |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3445 | full_len = urb->transfer_buffer_length; |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3446 | /* If we have scatter/gather list, we use it. */ |
Tejas Joglekar | 2017a1e | 2020-12-08 11:29:09 +0200 | [diff] [blame] | 3447 | if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3448 | num_sgs = urb->num_mapped_sgs; |
| 3449 | sg = urb->sg; |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3450 | addr = (u64) sg_dma_address(sg); |
| 3451 | block_len = sg_dma_len(sg); |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3452 | num_trbs = count_sg_trbs_needed(urb); |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3453 | } else { |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3454 | num_trbs = count_trbs_needed(urb); |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3455 | addr = (u64) urb->transfer_dma; |
| 3456 | block_len = full_len; |
| 3457 | } |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3458 | ret = prepare_transfer(xhci, xhci->devs[slot_id], |
| 3459 | ep_index, urb->stream_id, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3460 | num_trbs, urb, 0, mem_flags); |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3461 | if (unlikely(ret < 0)) |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3462 | return ret; |
| 3463 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3464 | urb_priv = urb->hcpriv; |
Reyad Attiyat | 4758dcd | 2015-08-06 19:23:58 +0300 | [diff] [blame] | 3465 | |
| 3466 | /* Deal with URB_ZERO_PACKET - need one more td/trb */ |
Mathias Nyman | 9ef7fbb | 2017-01-23 14:20:25 +0200 | [diff] [blame] | 3467 | if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1) |
Mathias Nyman | 5a83f04 | 2016-06-21 10:57:58 +0300 | [diff] [blame] | 3468 | need_zero_pkt = true; |
Reyad Attiyat | 4758dcd | 2015-08-06 19:23:58 +0300 | [diff] [blame] | 3469 | |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 3470 | td = &urb_priv->td[0]; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3471 | |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3472 | /* |
| 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 Nyman | 5a5a0b1 | 2016-06-21 10:57:57 +0300 | [diff] [blame] | 3477 | start_trb = &ring->enqueue->generic; |
| 3478 | start_cycle = ring->cycle_state; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3479 | send_addr = addr; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3480 | |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3481 | /* Queue the TRBs, even if they are zero-length */ |
Alban Browaeys | 0d2daad | 2016-08-16 10:18:04 +0300 | [diff] [blame] | 3482 | for (enqd_len = 0; first_trb || enqd_len < full_len; |
| 3483 | enqd_len += trb_buff_len) { |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3484 | field = TRB_TYPE(TRB_NORMAL); |
| 3485 | |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3486 | /* 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 Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3489 | |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3490 | if (enqd_len + trb_buff_len > full_len) |
| 3491 | trb_buff_len = full_len - enqd_len; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3492 | |
| 3493 | /* Don't change the cycle bit of the first TRB until later */ |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3494 | if (first_trb) { |
| 3495 | first_trb = false; |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3496 | if (start_cycle == 0) |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3497 | field |= TRB_CYCLE; |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3498 | } else |
Mathias Nyman | 5a5a0b1 | 2016-06-21 10:57:57 +0300 | [diff] [blame] | 3499 | field |= ring->cycle_state; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3500 | |
| 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 Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3504 | if (enqd_len + trb_buff_len < full_len) { |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3505 | field |= TRB_CHAIN; |
Mathias Nyman | 2d98ef4 | 2016-06-21 10:58:04 +0300 | [diff] [blame] | 3506 | if (trb_is_link(ring->enqueue + 1)) { |
Mathias Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3507 | if (xhci_align_td(xhci, urb, enqd_len, |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3508 | &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 Nyman | 474ed23 | 2016-06-21 10:58:01 +0300 | [diff] [blame] | 3514 | } |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3515 | } |
| 3516 | if (enqd_len + trb_buff_len >= full_len) { |
| 3517 | field &= ~TRB_CHAIN; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3518 | field |= TRB_IOC; |
Mathias Nyman | 124c393 | 2016-06-21 10:57:59 +0300 | [diff] [blame] | 3519 | more_trbs_coming = false; |
Mathias Nyman | 5a83f04 | 2016-06-21 10:57:58 +0300 | [diff] [blame] | 3520 | td->last_trb = ring->enqueue; |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3521 | td->last_trb_seg = ring->enq_seg; |
Nicolas Saenz Julienne | 33e3935 | 2019-04-26 16:23:29 +0300 | [diff] [blame] | 3522 | if (xhci_urb_suitable_for_idt(urb)) { |
| 3523 | memcpy(&send_addr, urb->transfer_buffer, |
| 3524 | trb_buff_len); |
Samuel Holland | bfa3dbb | 2019-10-25 17:30:28 +0300 | [diff] [blame] | 3525 | le64_to_cpus(&send_addr); |
Nicolas Saenz Julienne | 33e3935 | 2019-04-26 16:23:29 +0300 | [diff] [blame] | 3526 | field |= TRB_IDT; |
| 3527 | } |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3528 | } |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3529 | |
| 3530 | /* Only set interrupt on short packet for IN endpoints */ |
| 3531 | if (usb_urb_dir_in(urb)) |
| 3532 | field |= TRB_ISP; |
| 3533 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3534 | /* Set the TRB length, TD size, and interrupter fields. */ |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3535 | remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len, |
| 3536 | full_len, urb, more_trbs_coming); |
| 3537 | |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3538 | length_field = TRB_LEN(trb_buff_len) | |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3539 | TRB_TD_SIZE(remainder) | |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3540 | TRB_INTR_TARGET(0); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3541 | |
Mathias Nyman | 124c393 | 2016-06-21 10:57:59 +0300 | [diff] [blame] | 3542 | queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt, |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3543 | lower_32_bits(send_addr), |
| 3544 | upper_32_bits(send_addr), |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3545 | length_field, |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3546 | field); |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3547 | td->num_trbs++; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3548 | addr += trb_buff_len; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3549 | sent_len = trb_buff_len; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3550 | |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3551 | while (sg && sent_len >= block_len) { |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3552 | /* New sg entry */ |
| 3553 | --num_sgs; |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3554 | sent_len -= block_len; |
Sriharsha Allenki | 3c6f8cb | 2020-05-14 14:04:31 +0300 | [diff] [blame] | 3555 | sg = sg_next(sg); |
| 3556 | if (num_sgs != 0 && sg) { |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3557 | block_len = sg_dma_len(sg); |
| 3558 | addr = (u64) sg_dma_address(sg); |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3559 | addr += sent_len; |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3560 | } |
| 3561 | } |
Mathias Nyman | f9c589e | 2016-06-21 10:58:02 +0300 | [diff] [blame] | 3562 | block_len -= sent_len; |
| 3563 | send_addr = addr; |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3564 | } |
| 3565 | |
Mathias Nyman | 5a83f04 | 2016-06-21 10:57:58 +0300 | [diff] [blame] | 3566 | 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 Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 3570 | urb_priv->td[1].last_trb = ring->enqueue; |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3571 | urb_priv->td[1].last_trb_seg = ring->enq_seg; |
Mathias Nyman | 5a83f04 | 2016-06-21 10:57:58 +0300 | [diff] [blame] | 3572 | field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC; |
| 3573 | queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field); |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3574 | urb_priv->td[1].num_trbs++; |
Mathias Nyman | 5a83f04 | 2016-06-21 10:57:58 +0300 | [diff] [blame] | 3575 | } |
| 3576 | |
Mathias Nyman | 86065c2 | 2016-06-21 10:58:00 +0300 | [diff] [blame] | 3577 | check_trb_math(urb, enqd_len); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3578 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 3579 | start_cycle, start_trb); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3580 | return 0; |
| 3581 | } |
| 3582 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3583 | /* Caller must have locked xhci->lock */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3584 | int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3585 | 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 Baolu | fb79a6d | 2017-01-23 14:20:01 +0200 | [diff] [blame] | 3593 | u32 field; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3594 | struct urb_priv *urb_priv; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3595 | struct xhci_td *td; |
| 3596 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3597 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
| 3598 | if (!ep_ring) |
| 3599 | return -EINVAL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3600 | |
| 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3608 | /* 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 Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3617 | ret = prepare_transfer(xhci, xhci->devs[slot_id], |
| 3618 | ep_index, urb->stream_id, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3619 | num_trbs, urb, 0, mem_flags); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3620 | if (ret < 0) |
| 3621 | return ret; |
| 3622 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3623 | urb_priv = urb->hcpriv; |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 3624 | td = &urb_priv->td[0]; |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3625 | td->num_trbs = num_trbs; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3626 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3627 | /* |
| 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 Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3638 | field = 0; |
| 3639 | field |= TRB_IDT | TRB_TYPE(TRB_SETUP); |
| 3640 | if (start_cycle == 0) |
| 3641 | field |= 0x1; |
Andiry Xu | b83cdc8 | 2011-05-05 18:13:56 +0800 | [diff] [blame] | 3642 | |
Mathias Nyman | dca7794 | 2015-09-21 17:46:16 +0300 | [diff] [blame] | 3643 | /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 3644 | if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) { |
Andiry Xu | b83cdc8 | 2011-05-05 18:13:56 +0800 | [diff] [blame] | 3645 | 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 Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3653 | queue_trb(xhci, ep_ring, true, |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3654 | 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3659 | |
| 3660 | /* If there's data, queue data TRBs */ |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3661 | /* 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3667 | if (urb->transfer_buffer_length > 0) { |
Lu Baolu | fb79a6d | 2017-01-23 14:20:01 +0200 | [diff] [blame] | 3668 | u32 length_field, remainder; |
Mathias Nyman | 13b82b7 | 2019-05-22 14:34:00 +0300 | [diff] [blame] | 3669 | u64 addr; |
Lu Baolu | fb79a6d | 2017-01-23 14:20:01 +0200 | [diff] [blame] | 3670 | |
Nicolas Saenz Julienne | 33e3935 | 2019-04-26 16:23:29 +0300 | [diff] [blame] | 3671 | if (xhci_urb_suitable_for_idt(urb)) { |
Mathias Nyman | 13b82b7 | 2019-05-22 14:34:00 +0300 | [diff] [blame] | 3672 | memcpy(&addr, urb->transfer_buffer, |
Nicolas Saenz Julienne | 33e3935 | 2019-04-26 16:23:29 +0300 | [diff] [blame] | 3673 | urb->transfer_buffer_length); |
Samuel Holland | bfa3dbb | 2019-10-25 17:30:28 +0300 | [diff] [blame] | 3674 | le64_to_cpus(&addr); |
Nicolas Saenz Julienne | 33e3935 | 2019-04-26 16:23:29 +0300 | [diff] [blame] | 3675 | field |= TRB_IDT; |
Mathias Nyman | 13b82b7 | 2019-05-22 14:34:00 +0300 | [diff] [blame] | 3676 | } else { |
| 3677 | addr = (u64) urb->transfer_dma; |
Nicolas Saenz Julienne | 33e3935 | 2019-04-26 16:23:29 +0300 | [diff] [blame] | 3678 | } |
| 3679 | |
Lu Baolu | fb79a6d | 2017-01-23 14:20:01 +0200 | [diff] [blame] | 3680 | 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 Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3687 | if (setup->bRequestType & USB_DIR_IN) |
| 3688 | field |= TRB_DIR_IN; |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3689 | queue_trb(xhci, ep_ring, true, |
Mathias Nyman | 13b82b7 | 2019-05-22 14:34:00 +0300 | [diff] [blame] | 3690 | lower_32_bits(addr), |
| 3691 | upper_32_bits(addr), |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3692 | length_field, |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3693 | field | ep_ring->cycle_state); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | /* Save the DMA address of the last TRB in the TD */ |
| 3697 | td->last_trb = ep_ring->enqueue; |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3698 | td->last_trb_seg = ep_ring->enq_seg; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3699 | |
| 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 Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3706 | queue_trb(xhci, ep_ring, false, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3707 | 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 Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3713 | giveback_first_trb(xhci, slot_id, ep_index, 0, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 3714 | start_cycle, start_trb); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3715 | return 0; |
| 3716 | } |
| 3717 | |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3718 | /* |
| 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 | */ |
| 3726 | static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci, |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3727 | struct urb *urb, unsigned int total_packet_count) |
| 3728 | { |
| 3729 | unsigned int max_burst; |
| 3730 | |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3731 | if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER) |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3732 | return 0; |
| 3733 | |
| 3734 | max_burst = urb->ep->ss_ep_comp.bMaxBurst; |
Mathias Nyman | 3213b15 | 2014-06-24 17:14:41 +0300 | [diff] [blame] | 3735 | return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3736 | } |
| 3737 | |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3738 | /* |
| 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 | */ |
| 3746 | static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci, |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3747 | 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 Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3755 | if (urb->dev->speed >= USB_SPEED_SUPER) { |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3756 | /* 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 Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3765 | } |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3766 | if (total_packet_count == 0) |
| 3767 | return 0; |
| 3768 | return total_packet_count - 1; |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 3771 | /* |
| 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 | */ |
| 3778 | static 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 Nyman | edc649a | 2020-09-18 16:17:50 +0300 | [diff] [blame] | 3862 | /* Check if we should generate event interrupt for a TD in an isoc URB */ |
| 3863 | static 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 Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3880 | /* This is for isoc transfer */ |
| 3881 | static 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 Xu | 47cbf69 | 2010-12-20 14:49:48 +0800 | [diff] [blame] | 3895 | bool more_trbs_coming; |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 3896 | struct xhci_virt_ep *xep; |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3897 | int frame_id; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3898 | |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 3899 | xep = &xhci->devs[slot_id]->eps[ep_index]; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3900 | 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 Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3907 | start_addr = (u64) urb->transfer_dma; |
| 3908 | start_trb = &ep_ring->enqueue->generic; |
| 3909 | start_cycle = ep_ring->cycle_state; |
| 3910 | |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 3911 | urb_priv = urb->hcpriv; |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3912 | /* Queue the TRBs for each TD, even if they are zero-length */ |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3913 | for (i = 0; i < num_tds; i++) { |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3914 | unsigned int total_pkt_count, max_pkt; |
| 3915 | unsigned int burst_count, last_burst_pkt_count; |
| 3916 | u32 sia_frame_id; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3917 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3918 | first_trb = true; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3919 | 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 Balbi | 734d3dd | 2016-09-28 13:46:37 +0300 | [diff] [blame] | 3923 | max_pkt = usb_endpoint_maxp(&urb->ep->desc); |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3924 | total_pkt_count = DIV_ROUND_UP(td_len, max_pkt); |
| 3925 | |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 3926 | /* A zero-length transfer still involves at least one packet. */ |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3927 | 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 Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3932 | |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3933 | trbs_per_td = count_isoc_trbs_needed(urb, i); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3934 | |
| 3935 | ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 3936 | urb->stream_id, trbs_per_td, urb, i, mem_flags); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 3937 | if (ret < 0) { |
| 3938 | if (i == 0) |
| 3939 | return ret; |
| 3940 | goto cleanup; |
| 3941 | } |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 3942 | td = &urb_priv->td[i]; |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3943 | td->num_trbs = trbs_per_td; |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3944 | /* 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 Nyman | 2f6d3b6 | 2016-02-12 16:40:18 +0200 | [diff] [blame] | 3957 | field = TRB_TYPE(TRB_ISOC) | |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3958 | TRB_TLBPC(last_burst_pkt_count) | |
| 3959 | sia_frame_id | |
| 3960 | (i ? ep_ring->cycle_state : !start_cycle); |
| 3961 | |
Mathias Nyman | 2f6d3b6 | 2016-02-12 16:40:18 +0200 | [diff] [blame] | 3962 | /* 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 Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3966 | /* fill the rest of the TRB fields, and remaining normal TRBs */ |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3967 | for (j = 0; j < trbs_per_td; j++) { |
| 3968 | u32 remainder = 0; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3969 | |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3970 | /* only first TRB is isoc, overwrite otherwise */ |
| 3971 | if (!first_trb) |
| 3972 | field = TRB_TYPE(TRB_NORMAL) | |
| 3973 | ep_ring->cycle_state; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3974 | |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3975 | /* Only set interrupt on short packet for IN EPs */ |
| 3976 | if (usb_urb_dir_in(urb)) |
| 3977 | field |= TRB_ISP; |
| 3978 | |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3979 | /* Set the chain bit for all except the last TRB */ |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3980 | if (j < trbs_per_td - 1) { |
Andiry Xu | 47cbf69 | 2010-12-20 14:49:48 +0800 | [diff] [blame] | 3981 | more_trbs_coming = true; |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3982 | field |= TRB_CHAIN; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3983 | } else { |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3984 | more_trbs_coming = false; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3985 | td->last_trb = ep_ring->enqueue; |
Mathias Nyman | 55f6153 | 2021-01-29 15:00:28 +0200 | [diff] [blame] | 3986 | td->last_trb_seg = ep_ring->enq_seg; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3987 | field |= TRB_IOC; |
Mathias Nyman | edc649a | 2020-09-18 16:17:50 +0300 | [diff] [blame] | 3988 | if (trb_block_event_intr(xhci, num_tds, i)) |
Mathias Nyman | 09c352e | 2016-02-12 16:40:17 +0200 | [diff] [blame] | 3989 | field |= TRB_BEI; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3990 | } |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3991 | /* Calculate TRB length */ |
Alexandr Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 3992 | trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3993 | if (trb_buff_len > td_remain_len) |
| 3994 | trb_buff_len = td_remain_len; |
| 3995 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3996 | /* Set the TRB length, TD size, & interrupter fields. */ |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 3997 | remainder = xhci_td_remainder(xhci, running_total, |
| 3998 | trb_buff_len, td_len, |
Mathias Nyman | 124c393 | 2016-06-21 10:57:59 +0300 | [diff] [blame] | 3999 | urb, more_trbs_coming); |
Mathias Nyman | c840d6c | 2015-10-09 13:30:08 +0300 | [diff] [blame] | 4000 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4001 | length_field = TRB_LEN(trb_buff_len) | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4002 | TRB_INTR_TARGET(0); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 4003 | |
Mathias Nyman | 2f6d3b6 | 2016-02-12 16:40:18 +0200 | [diff] [blame] | 4004 | /* 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 Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 4011 | queue_trb(xhci, ep_ring, more_trbs_coming, |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4012 | lower_32_bits(addr), |
| 4013 | upper_32_bits(addr), |
| 4014 | length_field, |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 4015 | field); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4016 | 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 Xu | cf84055 | 2012-01-18 17:47:12 +0800 | [diff] [blame] | 4025 | ret = -EINVAL; |
| 4026 | goto cleanup; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4027 | } |
| 4028 | } |
| 4029 | |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4030 | /* 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 Xu | c41136b | 2011-03-22 17:08:14 +0800 | [diff] [blame] | 4034 | 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 Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 4040 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
| 4041 | start_cycle, start_trb); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4042 | return 0; |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 4043 | cleanup: |
| 4044 | /* Clean up a partially enqueued isoc transfer. */ |
| 4045 | |
| 4046 | for (i--; i >= 0; i--) |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 4047 | list_del_init(&urb_priv->td[i].td_list); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 4048 | |
| 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 Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 4054 | urb_priv->td[0].last_trb = ep_ring->enqueue; |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 4055 | /* Every TRB except the first & last will have its cycle bit flipped. */ |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 4056 | td_to_noop(xhci, ep_ring, &urb_priv->td[0], true); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 4057 | |
| 4058 | /* Reset the ring enqueue back to the first TRB and its cycle bit. */ |
Mathias Nyman | 7e64b03 | 2017-01-23 14:20:26 +0200 | [diff] [blame] | 4059 | ep_ring->enqueue = urb_priv->td[0].first_trb; |
| 4060 | ep_ring->enq_seg = urb_priv->td[0].start_seg; |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 4061 | ep_ring->cycle_state = start_cycle; |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 4062 | ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp; |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 4063 | usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); |
| 4064 | return ret; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | /* |
| 4068 | * Check transfer ring to guarantee there is enough room for the urb. |
| 4069 | * Update ISO URB start_frame and interval. |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4070 | * 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 Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4073 | */ |
| 4074 | int 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 Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4081 | int num_tds, num_trbs, i; |
| 4082 | int ret; |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4083 | struct xhci_virt_ep *xep; |
| 4084 | int ist; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4085 | |
| 4086 | xdev = xhci->devs[slot_id]; |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4087 | xep = &xhci->devs[slot_id]->eps[ep_index]; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4088 | 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 Ivanov | d251034 | 2016-04-22 13:17:09 +0300 | [diff] [blame] | 4094 | num_trbs += count_isoc_trbs_needed(urb, i); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4095 | |
| 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 Nyman | 5071e6b | 2016-11-11 15:13:28 +0200 | [diff] [blame] | 4099 | ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 4100 | num_trbs, mem_flags); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4101 | if (ret) |
| 4102 | return ret; |
| 4103 | |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4104 | /* |
| 4105 | * Check interval value. This should be done before we start to |
| 4106 | * calculate the start frame value. |
| 4107 | */ |
Alexandr Ivanov | 7814015 | 2016-04-22 13:17:11 +0300 | [diff] [blame] | 4108 | check_interval(xhci, urb, ep_ctx); |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4109 | |
| 4110 | /* Calculate the start frame and put it in urb->start_frame. */ |
Lu Baolu | 42df721 | 2015-11-18 10:48:21 +0200 | [diff] [blame] | 4111 | if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) { |
Mathias Nyman | 5071e6b | 2016-11-11 15:13:28 +0200 | [diff] [blame] | 4112 | if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) { |
Lu Baolu | 42df721 | 2015-11-18 10:48:21 +0200 | [diff] [blame] | 4113 | urb->start_frame = xep->next_frame_id; |
| 4114 | goto skip_start_over; |
| 4115 | } |
Lu Baolu | 79b8094 | 2015-08-06 19:24:00 +0300 | [diff] [blame] | 4116 | } |
| 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 | |
| 4143 | skip_start_over: |
Andiry Xu | b008df6 | 2012-03-05 17:49:34 +0800 | [diff] [blame] | 4144 | ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free; |
| 4145 | |
Dan Carpenter | 3fc8206 | 2012-03-28 10:30:26 +0300 | [diff] [blame] | 4146 | return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 4147 | } |
| 4148 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 4149 | /**** Command Ring Operations ****/ |
| 4150 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4151 | /* 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 Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4159 | static 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 Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 4162 | { |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4163 | int reserved_trbs = xhci->cmd_ring_reserved_trbs; |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 4164 | int ret; |
Roger Quadros | ad6b1d9 | 2015-05-29 17:01:49 +0300 | [diff] [blame] | 4165 | |
Mathias Nyman | 98d74f9 | 2016-04-08 16:25:10 +0300 | [diff] [blame] | 4166 | if ((xhci->xhc_state & XHCI_STATE_DYING) || |
| 4167 | (xhci->xhc_state & XHCI_STATE_HALTED)) { |
Roger Quadros | ad6b1d9 | 2015-05-29 17:01:49 +0300 | [diff] [blame] | 4168 | xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n"); |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 4169 | return -ESHUTDOWN; |
Roger Quadros | ad6b1d9 | 2015-05-29 17:01:49 +0300 | [diff] [blame] | 4170 | } |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 4171 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4172 | if (!command_must_succeed) |
| 4173 | reserved_trbs++; |
| 4174 | |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 4175 | ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 4176 | reserved_trbs, GFP_ATOMIC); |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 4177 | if (ret < 0) { |
| 4178 | xhci_err(xhci, "ERR: No room for command on command ring\n"); |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4179 | if (command_must_succeed) |
| 4180 | xhci_err(xhci, "ERR: Reserved TRB counting for " |
| 4181 | "unfailable commands failed.\n"); |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 4182 | return ret; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 4183 | } |
Mathias Nyman | c9aa1a2 | 2014-05-08 19:26:01 +0300 | [diff] [blame] | 4184 | |
| 4185 | cmd->command_trb = xhci->cmd_ring->enqueue; |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4186 | |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 4187 | /* if there are no other commands queued we start the timeout timer */ |
Lu Baolu | daa47f2 | 2017-01-23 14:20:02 +0200 | [diff] [blame] | 4188 | if (list_empty(&xhci->cmd_list)) { |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 4189 | xhci->current_cmd = cmd; |
OGAWA Hirofumi | cb4d5ce | 2017-01-03 18:28:50 +0200 | [diff] [blame] | 4190 | xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT); |
Mathias Nyman | c311e39 | 2014-05-08 19:26:03 +0300 | [diff] [blame] | 4191 | } |
| 4192 | |
Lu Baolu | daa47f2 | 2017-01-23 14:20:02 +0200 | [diff] [blame] | 4193 | list_add_tail(&cmd->cmd_list, &xhci->cmd_list); |
| 4194 | |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame] | 4195 | queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, |
| 4196 | field4 | xhci->cmd_ring->cycle_state); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 4197 | return 0; |
| 4198 | } |
| 4199 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 4200 | /* Queue a slot enable or disable request on the command ring */ |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4201 | int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd, |
| 4202 | u32 trb_type, u32 slot_id) |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 4203 | { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4204 | return queue_command(xhci, cmd, 0, 0, 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4205 | TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | /* Queue an address device command TRB */ |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4209 | int 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 Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 4211 | { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4212 | return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 4213 | upper_32_bits(in_ctx_ptr), 0, |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4214 | TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id) |
| 4215 | | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 4216 | } |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 4217 | |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4218 | int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd, |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 4219 | u32 field1, u32 field2, u32 field3, u32 field4) |
| 4220 | { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4221 | return queue_command(xhci, cmd, field1, field2, field3, field4, false); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 4222 | } |
| 4223 | |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 4224 | /* Queue a reset device command TRB */ |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4225 | int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, |
| 4226 | u32 slot_id) |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 4227 | { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4228 | return queue_command(xhci, cmd, 0, 0, 0, |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 4229 | TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id), |
| 4230 | false); |
| 4231 | } |
| 4232 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 4233 | /* Queue a configure endpoint command TRB */ |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4234 | int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, |
| 4235 | struct xhci_command *cmd, dma_addr_t in_ctx_ptr, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4236 | u32 slot_id, bool command_must_succeed) |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 4237 | { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4238 | return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 4239 | upper_32_bits(in_ctx_ptr), 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4240 | TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id), |
| 4241 | command_must_succeed); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 4242 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4243 | |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 4244 | /* Queue an evaluate context command TRB */ |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4245 | int 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 Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 4247 | { |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4248 | return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 4249 | upper_32_bits(in_ctx_ptr), 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 4250 | TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id), |
Sarah Sharp | 4b26654 | 2012-05-07 15:34:26 -0700 | [diff] [blame] | 4251 | command_must_succeed); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 4252 | } |
| 4253 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 4254 | /* |
| 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 Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4258 | int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd, |
| 4259 | int slot_id, unsigned int ep_index, int suspend) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4260 | { |
| 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 Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 4264 | u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4265 | |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4266 | return queue_command(xhci, cmd, 0, 0, 0, |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 4267 | trb_slot_id | trb_ep_index | type | trb_suspend, false); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4268 | } |
| 4269 | |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4270 | /* Set Transfer Ring Dequeue Pointer command */ |
| 4271 | void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci, |
| 4272 | unsigned int slot_id, unsigned int ep_index, |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4273 | struct xhci_dequeue_state *deq_state) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4274 | { |
| 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 Nyman | 8790736 | 2017-06-02 16:36:23 +0300 | [diff] [blame] | 4278 | u32 trb_stream_id = STREAM_ID_FOR_TRB(deq_state->stream_id); |
Hans de Goede | 95241db | 2013-10-04 00:29:48 +0200 | [diff] [blame] | 4279 | u32 trb_sct = 0; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4280 | u32 type = TRB_TYPE(TRB_SET_DEQ); |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 4281 | struct xhci_virt_ep *ep; |
Hans de Goede | 1e3452e | 2014-08-20 16:41:52 +0300 | [diff] [blame] | 4282 | struct xhci_command *cmd; |
| 4283 | int ret; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4284 | |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4285 | 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 Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 4296 | if (addr == 0) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4297 | xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n"); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 4298 | xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n", |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4299 | deq_state->new_deq_seg, deq_state->new_deq_ptr); |
| 4300 | return; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 4301 | } |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 4302 | 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 Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4306 | return; |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 4307 | } |
Hans de Goede | 1e3452e | 2014-08-20 16:41:52 +0300 | [diff] [blame] | 4308 | |
| 4309 | /* This function gets called from contexts where it cannot sleep */ |
Mathias Nyman | 103afda | 2017-12-08 17:59:08 +0200 | [diff] [blame] | 4310 | cmd = xhci_alloc_command(xhci, false, GFP_ATOMIC); |
Lu Baolu | 74e0b56 | 2017-04-07 17:57:05 +0300 | [diff] [blame] | 4311 | if (!cmd) |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4312 | return; |
Hans de Goede | 1e3452e | 2014-08-20 16:41:52 +0300 | [diff] [blame] | 4313 | |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4314 | ep->queued_deq_seg = deq_state->new_deq_seg; |
| 4315 | ep->queued_deq_ptr = deq_state->new_deq_ptr; |
Mathias Nyman | 8790736 | 2017-06-02 16:36:23 +0300 | [diff] [blame] | 4316 | if (deq_state->stream_id) |
Hans de Goede | 95241db | 2013-10-04 00:29:48 +0200 | [diff] [blame] | 4317 | trb_sct = SCT_FOR_TRB(SCT_PRI_TR); |
Hans de Goede | 1e3452e | 2014-08-20 16:41:52 +0300 | [diff] [blame] | 4318 | ret = queue_command(xhci, cmd, |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4319 | 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 Goede | 1e3452e | 2014-08-20 16:41:52 +0300 | [diff] [blame] | 4322 | if (ret < 0) { |
| 4323 | xhci_free_command(xhci, cmd); |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4324 | return; |
Hans de Goede | 1e3452e | 2014-08-20 16:41:52 +0300 | [diff] [blame] | 4325 | } |
| 4326 | |
Hans de Goede | d3a43e6 | 2014-08-20 16:41:53 +0300 | [diff] [blame] | 4327 | /* 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 Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 4333 | } |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 4334 | |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4335 | int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, |
Mathias Nyman | 2174914 | 2017-06-15 11:55:44 +0300 | [diff] [blame] | 4336 | int slot_id, unsigned int ep_index, |
| 4337 | enum xhci_ep_reset_type reset_type) |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 4338 | { |
| 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 Nyman | 2174914 | 2017-06-15 11:55:44 +0300 | [diff] [blame] | 4343 | if (reset_type == EP_SOFT_RESET) |
| 4344 | type |= TRB_TSP; |
| 4345 | |
Mathias Nyman | ddba5cd | 2014-05-08 19:26:00 +0300 | [diff] [blame] | 4346 | return queue_command(xhci, cmd, 0, 0, 0, |
| 4347 | trb_slot_id | trb_ep_index | type, false); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 4348 | } |