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