xhci: Remove debugging about ring structure allocation.
[linux-2.6-block.git] / drivers / usb / host / xhci-ring.c
CommitLineData
7f84eef0
SS
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Ring initialization rules:
25 * 1. Each segment is initialized to zero, except for link TRBs.
26 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
27 * Consumer Cycle State (CCS), depending on ring function.
28 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
29 *
30 * Ring behavior rules:
31 * 1. A ring is empty if enqueue == dequeue. This means there will always be at
32 * least one free TRB in the ring. This is useful if you want to turn that
33 * into a link TRB and expand the ring.
34 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
35 * link TRB, then load the pointer with the address in the link TRB. If the
36 * link TRB had its toggle bit set, you may need to update the ring cycle
37 * state (see cycle bit rules). You may have to do this multiple times
38 * until you reach a non-link TRB.
39 * 3. A ring is full if enqueue++ (for the definition of increment above)
40 * equals the dequeue pointer.
41 *
42 * Cycle bit rules:
43 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
44 * in a link TRB, it must toggle the ring cycle state.
45 * 2. When a producer increments an enqueue pointer and encounters a toggle bit
46 * in a link TRB, it must toggle the ring cycle state.
47 *
48 * Producer rules:
49 * 1. Check if ring is full before you enqueue.
50 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
51 * Update enqueue pointer between each write (which may update the ring
52 * cycle state).
53 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
54 * and endpoint rings. If HC is the producer for the event ring,
55 * and it generates an interrupt according to interrupt modulation rules.
56 *
57 * Consumer rules:
58 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
59 * the TRB is owned by the consumer.
60 * 2. Update dequeue pointer (which may update the ring cycle state) and
61 * continue processing TRBs until you reach a TRB which is not owned by you.
62 * 3. Notify the producer. SW is the consumer for the event ring, and it
63 * updates event ring dequeue pointer. HC is the consumer for the command and
64 * endpoint rings; it generates events on the event ring for these.
65 */
66
8a96c052 67#include <linux/scatterlist.h>
5a0e3ad6 68#include <linux/slab.h>
7f84eef0
SS
69#include "xhci.h"
70
be88fe4f
AX
71static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
72 struct xhci_virt_device *virt_dev,
73 struct xhci_event_cmd *event);
74
7f84eef0
SS
75/*
76 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
77 * address of the TRB.
78 */
23e3be11 79dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
7f84eef0
SS
80 union xhci_trb *trb)
81{
6071d836 82 unsigned long segment_offset;
7f84eef0 83
6071d836 84 if (!seg || !trb || trb < seg->trbs)
7f84eef0 85 return 0;
6071d836
SS
86 /* offset in TRBs */
87 segment_offset = trb - seg->trbs;
88 if (segment_offset > TRBS_PER_SEGMENT)
7f84eef0 89 return 0;
6071d836 90 return seg->dma + (segment_offset * sizeof(*trb));
7f84eef0
SS
91}
92
93/* Does this link TRB point to the first segment in a ring,
94 * or was the previous TRB the last TRB on the last segment in the ERST?
95 */
575688e1 96static bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
7f84eef0
SS
97 struct xhci_segment *seg, union xhci_trb *trb)
98{
99 if (ring == xhci->event_ring)
100 return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
101 (seg->next == xhci->event_ring->first_seg);
102 else
28ccd296 103 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
7f84eef0
SS
104}
105
106/* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
107 * segment? I.e. would the updated event TRB pointer step off the end of the
108 * event seg?
109 */
575688e1 110static int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
7f84eef0
SS
111 struct xhci_segment *seg, union xhci_trb *trb)
112{
113 if (ring == xhci->event_ring)
114 return trb == &seg->trbs[TRBS_PER_SEGMENT];
115 else
f5960b69 116 return TRB_TYPE_LINK_LE32(trb->link.control);
7f84eef0
SS
117}
118
575688e1 119static int enqueue_is_link_trb(struct xhci_ring *ring)
6c12db90
JY
120{
121 struct xhci_link_trb *link = &ring->enqueue->link;
f5960b69 122 return TRB_TYPE_LINK_LE32(link->control);
6c12db90
JY
123}
124
ae636747
SS
125/* Updates trb to point to the next TRB in the ring, and updates seg if the next
126 * TRB is in a new segment. This does not skip over link TRBs, and it does not
127 * effect the ring dequeue or enqueue pointers.
128 */
129static void next_trb(struct xhci_hcd *xhci,
130 struct xhci_ring *ring,
131 struct xhci_segment **seg,
132 union xhci_trb **trb)
133{
134 if (last_trb(xhci, ring, *seg, *trb)) {
135 *seg = (*seg)->next;
136 *trb = ((*seg)->trbs);
137 } else {
a1669b2c 138 (*trb)++;
ae636747
SS
139 }
140}
141
7f84eef0
SS
142/*
143 * See Cycle bit rules. SW is the consumer for the event ring only.
144 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
145 */
146static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
147{
148 union xhci_trb *next = ++(ring->dequeue);
66e49d87 149 unsigned long long addr;
7f84eef0
SS
150
151 ring->deq_updates++;
152 /* Update the dequeue pointer further if that was a link TRB or we're at
153 * the end of an event ring segment (which doesn't have link TRBS)
154 */
155 while (last_trb(xhci, ring, ring->deq_seg, next)) {
156 if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
157 ring->cycle_state = (ring->cycle_state ? 0 : 1);
7f84eef0
SS
158 }
159 ring->deq_seg = ring->deq_seg->next;
160 ring->dequeue = ring->deq_seg->trbs;
161 next = ring->dequeue;
162 }
66e49d87 163 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
7f84eef0
SS
164}
165
166/*
167 * See Cycle bit rules. SW is the consumer for the event ring only.
168 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
169 *
170 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
171 * chain bit is set), then set the chain bit in all the following link TRBs.
172 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
173 * have their chain bit cleared (so that each Link TRB is a separate TD).
174 *
175 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
b0567b3f
SS
176 * set, but other sections talk about dealing with the chain bit set. This was
177 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
178 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
6cc30d85
SS
179 *
180 * @more_trbs_coming: Will you enqueue more TRBs before calling
181 * prepare_transfer()?
7f84eef0 182 */
6cc30d85 183static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
7e393a83 184 bool consumer, bool more_trbs_coming, bool isoc)
7f84eef0
SS
185{
186 u32 chain;
187 union xhci_trb *next;
66e49d87 188 unsigned long long addr;
7f84eef0 189
28ccd296 190 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
7f84eef0
SS
191 next = ++(ring->enqueue);
192
193 ring->enq_updates++;
194 /* Update the dequeue pointer further if that was a link TRB or we're at
195 * the end of an event ring segment (which doesn't have link TRBS)
196 */
197 while (last_trb(xhci, ring, ring->enq_seg, next)) {
198 if (!consumer) {
199 if (ring != xhci->event_ring) {
6cc30d85
SS
200 /*
201 * If the caller doesn't plan on enqueueing more
202 * TDs before ringing the doorbell, then we
203 * don't want to give the link TRB to the
204 * hardware just yet. We'll give the link TRB
205 * back in prepare_ring() just before we enqueue
206 * the TD at the top of the ring.
207 */
208 if (!chain && !more_trbs_coming)
6c12db90 209 break;
6cc30d85 210
7e393a83
AX
211 /* If we're not dealing with 0.95 hardware or
212 * isoc rings on AMD 0.96 host,
6cc30d85
SS
213 * carry over the chain bit of the previous TRB
214 * (which may mean the chain bit is cleared).
215 */
7e393a83
AX
216 if (!(isoc && (xhci->quirks & XHCI_AMD_0x96_HOST))
217 && !xhci_link_trb_quirk(xhci)) {
28ccd296
ME
218 next->link.control &=
219 cpu_to_le32(~TRB_CHAIN);
220 next->link.control |=
221 cpu_to_le32(chain);
b0567b3f 222 }
6cc30d85
SS
223 /* Give this link TRB to the hardware */
224 wmb();
28ccd296 225 next->link.control ^= cpu_to_le32(TRB_CYCLE);
7f84eef0
SS
226 }
227 /* Toggle the cycle bit after the last ring segment. */
228 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
229 ring->cycle_state = (ring->cycle_state ? 0 : 1);
7f84eef0
SS
230 }
231 }
232 ring->enq_seg = ring->enq_seg->next;
233 ring->enqueue = ring->enq_seg->trbs;
234 next = ring->enqueue;
235 }
66e49d87 236 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
7f84eef0
SS
237}
238
239/*
240 * Check to see if there's room to enqueue num_trbs on the ring. See rules
241 * above.
242 * FIXME: this would be simpler and faster if we just kept track of the number
243 * of free TRBs in a ring.
244 */
245static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
246 unsigned int num_trbs)
247{
248 int i;
249 union xhci_trb *enq = ring->enqueue;
250 struct xhci_segment *enq_seg = ring->enq_seg;
44ebd037
SS
251 struct xhci_segment *cur_seg;
252 unsigned int left_on_ring;
7f84eef0 253
6c12db90
JY
254 /* If we are currently pointing to a link TRB, advance the
255 * enqueue pointer before checking for space */
256 while (last_trb(xhci, ring, enq_seg, enq)) {
257 enq_seg = enq_seg->next;
258 enq = enq_seg->trbs;
259 }
260
7f84eef0 261 /* Check if ring is empty */
44ebd037
SS
262 if (enq == ring->dequeue) {
263 /* Can't use link trbs */
264 left_on_ring = TRBS_PER_SEGMENT - 1;
265 for (cur_seg = enq_seg->next; cur_seg != enq_seg;
266 cur_seg = cur_seg->next)
267 left_on_ring += TRBS_PER_SEGMENT - 1;
268
269 /* Always need one TRB free in the ring. */
270 left_on_ring -= 1;
271 if (num_trbs > left_on_ring) {
272 xhci_warn(xhci, "Not enough room on ring; "
273 "need %u TRBs, %u TRBs left\n",
274 num_trbs, left_on_ring);
275 return 0;
276 }
7f84eef0 277 return 1;
44ebd037 278 }
7f84eef0
SS
279 /* Make sure there's an extra empty TRB available */
280 for (i = 0; i <= num_trbs; ++i) {
281 if (enq == ring->dequeue)
282 return 0;
283 enq++;
284 while (last_trb(xhci, ring, enq_seg, enq)) {
285 enq_seg = enq_seg->next;
286 enq = enq_seg->trbs;
287 }
288 }
289 return 1;
290}
291
7f84eef0 292/* Ring the host controller doorbell after placing a command on the ring */
23e3be11 293void xhci_ring_cmd_db(struct xhci_hcd *xhci)
7f84eef0 294{
7f84eef0 295 xhci_dbg(xhci, "// Ding dong!\n");
50d64676 296 xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
7f84eef0
SS
297 /* Flush PCI posted writes */
298 xhci_readl(xhci, &xhci->dba->doorbell[0]);
299}
300
be88fe4f 301void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
ae636747 302 unsigned int slot_id,
e9df17eb
SS
303 unsigned int ep_index,
304 unsigned int stream_id)
ae636747 305{
28ccd296 306 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
50d64676
MW
307 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
308 unsigned int ep_state = ep->ep_state;
ae636747 309
ae636747 310 /* Don't ring the doorbell for this endpoint if there are pending
50d64676 311 * cancellations because we don't want to interrupt processing.
8df75f42
SS
312 * We don't want to restart any stream rings if there's a set dequeue
313 * pointer command pending because the device can choose to start any
314 * stream once the endpoint is on the HW schedule.
315 * FIXME - check all the stream rings for pending cancellations.
ae636747 316 */
50d64676
MW
317 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
318 (ep_state & EP_HALTED))
319 return;
320 xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr);
321 /* The CPU has better things to do at this point than wait for a
322 * write-posting flush. It'll get there soon enough.
323 */
ae636747
SS
324}
325
e9df17eb
SS
326/* Ring the doorbell for any rings with pending URBs */
327static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
328 unsigned int slot_id,
329 unsigned int ep_index)
330{
331 unsigned int stream_id;
332 struct xhci_virt_ep *ep;
333
334 ep = &xhci->devs[slot_id]->eps[ep_index];
335
336 /* A ring has pending URBs if its TD list is not empty */
337 if (!(ep->ep_state & EP_HAS_STREAMS)) {
338 if (!(list_empty(&ep->ring->td_list)))
be88fe4f 339 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
e9df17eb
SS
340 return;
341 }
342
343 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
344 stream_id++) {
345 struct xhci_stream_info *stream_info = ep->stream_info;
346 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
be88fe4f
AX
347 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
348 stream_id);
e9df17eb
SS
349 }
350}
351
ae636747
SS
352/*
353 * Find the segment that trb is in. Start searching in start_seg.
354 * If we must move past a segment that has a link TRB with a toggle cycle state
355 * bit set, then we will toggle the value pointed at by cycle_state.
356 */
357static struct xhci_segment *find_trb_seg(
358 struct xhci_segment *start_seg,
359 union xhci_trb *trb, int *cycle_state)
360{
361 struct xhci_segment *cur_seg = start_seg;
362 struct xhci_generic_trb *generic_trb;
363
364 while (cur_seg->trbs > trb ||
365 &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
366 generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
f5960b69 367 if (generic_trb->field[3] & cpu_to_le32(LINK_TOGGLE))
ba0a4d9a 368 *cycle_state ^= 0x1;
ae636747
SS
369 cur_seg = cur_seg->next;
370 if (cur_seg == start_seg)
371 /* Looped over the entire list. Oops! */
326b4810 372 return NULL;
ae636747
SS
373 }
374 return cur_seg;
375}
376
021bff91
SS
377
378static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
379 unsigned int slot_id, unsigned int ep_index,
380 unsigned int stream_id)
381{
382 struct xhci_virt_ep *ep;
383
384 ep = &xhci->devs[slot_id]->eps[ep_index];
385 /* Common case: no streams */
386 if (!(ep->ep_state & EP_HAS_STREAMS))
387 return ep->ring;
388
389 if (stream_id == 0) {
390 xhci_warn(xhci,
391 "WARN: Slot ID %u, ep index %u has streams, "
392 "but URB has no stream ID.\n",
393 slot_id, ep_index);
394 return NULL;
395 }
396
397 if (stream_id < ep->stream_info->num_streams)
398 return ep->stream_info->stream_rings[stream_id];
399
400 xhci_warn(xhci,
401 "WARN: Slot ID %u, ep index %u has "
402 "stream IDs 1 to %u allocated, "
403 "but stream ID %u is requested.\n",
404 slot_id, ep_index,
405 ep->stream_info->num_streams - 1,
406 stream_id);
407 return NULL;
408}
409
410/* Get the right ring for the given URB.
411 * If the endpoint supports streams, boundary check the URB's stream ID.
412 * If the endpoint doesn't support streams, return the singular endpoint ring.
413 */
414static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
415 struct urb *urb)
416{
417 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
418 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
419}
420
ae636747
SS
421/*
422 * Move the xHC's endpoint ring dequeue pointer past cur_td.
423 * Record the new state of the xHC's endpoint ring dequeue segment,
424 * dequeue pointer, and new consumer cycle state in state.
425 * Update our internal representation of the ring's dequeue pointer.
426 *
427 * We do this in three jumps:
428 * - First we update our new ring state to be the same as when the xHC stopped.
429 * - Then we traverse the ring to find the segment that contains
430 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
431 * any link TRBs with the toggle cycle bit set.
432 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
433 * if we've moved it past a link TRB with the toggle cycle bit set.
28ccd296
ME
434 *
435 * Some of the uses of xhci_generic_trb are grotty, but if they're done
436 * with correct __le32 accesses they should work fine. Only users of this are
437 * in here.
ae636747 438 */
c92bcfa7 439void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
ae636747 440 unsigned int slot_id, unsigned int ep_index,
e9df17eb
SS
441 unsigned int stream_id, struct xhci_td *cur_td,
442 struct xhci_dequeue_state *state)
ae636747
SS
443{
444 struct xhci_virt_device *dev = xhci->devs[slot_id];
e9df17eb 445 struct xhci_ring *ep_ring;
ae636747 446 struct xhci_generic_trb *trb;
d115b048 447 struct xhci_ep_ctx *ep_ctx;
c92bcfa7 448 dma_addr_t addr;
ae636747 449
e9df17eb
SS
450 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
451 ep_index, stream_id);
452 if (!ep_ring) {
453 xhci_warn(xhci, "WARN can't find new dequeue state "
454 "for invalid stream ID %u.\n",
455 stream_id);
456 return;
457 }
ae636747 458 state->new_cycle_state = 0;
c92bcfa7 459 xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
ae636747 460 state->new_deq_seg = find_trb_seg(cur_td->start_seg,
63a0d9ab 461 dev->eps[ep_index].stopped_trb,
ae636747 462 &state->new_cycle_state);
68e41c5d
PZ
463 if (!state->new_deq_seg) {
464 WARN_ON(1);
465 return;
466 }
467
ae636747 468 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
c92bcfa7 469 xhci_dbg(xhci, "Finding endpoint context\n");
d115b048 470 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
28ccd296 471 state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
ae636747
SS
472
473 state->new_deq_ptr = cur_td->last_trb;
c92bcfa7 474 xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
ae636747
SS
475 state->new_deq_seg = find_trb_seg(state->new_deq_seg,
476 state->new_deq_ptr,
477 &state->new_cycle_state);
68e41c5d
PZ
478 if (!state->new_deq_seg) {
479 WARN_ON(1);
480 return;
481 }
ae636747
SS
482
483 trb = &state->new_deq_ptr->generic;
f5960b69
ME
484 if (TRB_TYPE_LINK_LE32(trb->field[3]) &&
485 (trb->field[3] & cpu_to_le32(LINK_TOGGLE)))
ba0a4d9a 486 state->new_cycle_state ^= 0x1;
ae636747
SS
487 next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
488
01a1fdb9
SS
489 /*
490 * If there is only one segment in a ring, find_trb_seg()'s while loop
491 * will not run, and it will return before it has a chance to see if it
492 * needs to toggle the cycle bit. It can't tell if the stalled transfer
493 * ended just before the link TRB on a one-segment ring, or if the TD
494 * wrapped around the top of the ring, because it doesn't have the TD in
495 * question. Look for the one-segment case where stalled TRB's address
496 * is greater than the new dequeue pointer address.
497 */
498 if (ep_ring->first_seg == ep_ring->first_seg->next &&
499 state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
500 state->new_cycle_state ^= 0x1;
501 xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
502
ae636747 503 /* Don't update the ring cycle state for the producer (us). */
c92bcfa7
SS
504 xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
505 state->new_deq_seg);
506 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
507 xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
508 (unsigned long long) addr);
ae636747
SS
509}
510
522989a2
SS
511/* flip_cycle means flip the cycle bit of all but the first and last TRB.
512 * (The last TRB actually points to the ring enqueue pointer, which is not part
513 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
514 */
23e3be11 515static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
522989a2 516 struct xhci_td *cur_td, bool flip_cycle)
ae636747
SS
517{
518 struct xhci_segment *cur_seg;
519 union xhci_trb *cur_trb;
520
521 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
522 true;
523 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
f5960b69 524 if (TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) {
ae636747
SS
525 /* Unchain any chained Link TRBs, but
526 * leave the pointers intact.
527 */
28ccd296 528 cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN);
522989a2
SS
529 /* Flip the cycle bit (link TRBs can't be the first
530 * or last TRB).
531 */
532 if (flip_cycle)
533 cur_trb->generic.field[3] ^=
534 cpu_to_le32(TRB_CYCLE);
ae636747 535 xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
700e2052
GKH
536 xhci_dbg(xhci, "Address = %p (0x%llx dma); "
537 "in seg %p (0x%llx dma)\n",
538 cur_trb,
23e3be11 539 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
700e2052
GKH
540 cur_seg,
541 (unsigned long long)cur_seg->dma);
ae636747
SS
542 } else {
543 cur_trb->generic.field[0] = 0;
544 cur_trb->generic.field[1] = 0;
545 cur_trb->generic.field[2] = 0;
546 /* Preserve only the cycle bit of this TRB */
28ccd296 547 cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
522989a2
SS
548 /* Flip the cycle bit except on the first or last TRB */
549 if (flip_cycle && cur_trb != cur_td->first_trb &&
550 cur_trb != cur_td->last_trb)
551 cur_trb->generic.field[3] ^=
552 cpu_to_le32(TRB_CYCLE);
28ccd296
ME
553 cur_trb->generic.field[3] |= cpu_to_le32(
554 TRB_TYPE(TRB_TR_NOOP));
700e2052
GKH
555 xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
556 "in seg %p (0x%llx dma)\n",
557 cur_trb,
23e3be11 558 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
700e2052
GKH
559 cur_seg,
560 (unsigned long long)cur_seg->dma);
ae636747
SS
561 }
562 if (cur_trb == cur_td->last_trb)
563 break;
564 }
565}
566
567static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
e9df17eb
SS
568 unsigned int ep_index, unsigned int stream_id,
569 struct xhci_segment *deq_seg,
ae636747
SS
570 union xhci_trb *deq_ptr, u32 cycle_state);
571
c92bcfa7 572void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
63a0d9ab 573 unsigned int slot_id, unsigned int ep_index,
e9df17eb 574 unsigned int stream_id,
63a0d9ab 575 struct xhci_dequeue_state *deq_state)
c92bcfa7 576{
63a0d9ab
SS
577 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
578
c92bcfa7
SS
579 xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
580 "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
581 deq_state->new_deq_seg,
582 (unsigned long long)deq_state->new_deq_seg->dma,
583 deq_state->new_deq_ptr,
584 (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
585 deq_state->new_cycle_state);
e9df17eb 586 queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
c92bcfa7
SS
587 deq_state->new_deq_seg,
588 deq_state->new_deq_ptr,
589 (u32) deq_state->new_cycle_state);
590 /* Stop the TD queueing code from ringing the doorbell until
591 * this command completes. The HC won't set the dequeue pointer
592 * if the ring is running, and ringing the doorbell starts the
593 * ring running.
594 */
63a0d9ab 595 ep->ep_state |= SET_DEQ_PENDING;
c92bcfa7
SS
596}
597
575688e1 598static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
6f5165cf
SS
599 struct xhci_virt_ep *ep)
600{
601 ep->ep_state &= ~EP_HALT_PENDING;
602 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
603 * timer is running on another CPU, we don't decrement stop_cmds_pending
604 * (since we didn't successfully stop the watchdog timer).
605 */
606 if (del_timer(&ep->stop_cmd_timer))
607 ep->stop_cmds_pending--;
608}
609
610/* Must be called with xhci->lock held in interrupt context */
611static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
612 struct xhci_td *cur_td, int status, char *adjective)
613{
214f76f7 614 struct usb_hcd *hcd;
8e51adcc
AX
615 struct urb *urb;
616 struct urb_priv *urb_priv;
6f5165cf 617
8e51adcc
AX
618 urb = cur_td->urb;
619 urb_priv = urb->hcpriv;
620 urb_priv->td_cnt++;
214f76f7 621 hcd = bus_to_hcd(urb->dev->bus);
6f5165cf 622
8e51adcc
AX
623 /* Only giveback urb when this is the last td in urb */
624 if (urb_priv->td_cnt == urb_priv->length) {
c41136b0
AX
625 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
626 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
627 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
628 if (xhci->quirks & XHCI_AMD_PLL_FIX)
629 usb_amd_quirk_pll_enable();
630 }
631 }
8e51adcc 632 usb_hcd_unlink_urb_from_ep(hcd, urb);
8e51adcc
AX
633
634 spin_unlock(&xhci->lock);
635 usb_hcd_giveback_urb(hcd, urb, status);
636 xhci_urb_free_priv(xhci, urb_priv);
637 spin_lock(&xhci->lock);
8e51adcc 638 }
6f5165cf
SS
639}
640
ae636747
SS
641/*
642 * When we get a command completion for a Stop Endpoint Command, we need to
643 * unlink any cancelled TDs from the ring. There are two ways to do that:
644 *
645 * 1. If the HW was in the middle of processing the TD that needs to be
646 * cancelled, then we must move the ring's dequeue pointer past the last TRB
647 * in the TD with a Set Dequeue Pointer Command.
648 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
649 * bit cleared) so that the HW will skip over them.
650 */
651static void handle_stopped_endpoint(struct xhci_hcd *xhci,
be88fe4f 652 union xhci_trb *trb, struct xhci_event_cmd *event)
ae636747
SS
653{
654 unsigned int slot_id;
655 unsigned int ep_index;
be88fe4f 656 struct xhci_virt_device *virt_dev;
ae636747 657 struct xhci_ring *ep_ring;
63a0d9ab 658 struct xhci_virt_ep *ep;
ae636747 659 struct list_head *entry;
326b4810 660 struct xhci_td *cur_td = NULL;
ae636747
SS
661 struct xhci_td *last_unlinked_td;
662
c92bcfa7 663 struct xhci_dequeue_state deq_state;
ae636747 664
be88fe4f 665 if (unlikely(TRB_TO_SUSPEND_PORT(
28ccd296 666 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])))) {
be88fe4f 667 slot_id = TRB_TO_SLOT_ID(
28ccd296 668 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
be88fe4f
AX
669 virt_dev = xhci->devs[slot_id];
670 if (virt_dev)
671 handle_cmd_in_cmd_wait_list(xhci, virt_dev,
672 event);
673 else
674 xhci_warn(xhci, "Stop endpoint command "
675 "completion for disabled slot %u\n",
676 slot_id);
677 return;
678 }
679
ae636747 680 memset(&deq_state, 0, sizeof(deq_state));
28ccd296
ME
681 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
682 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
63a0d9ab 683 ep = &xhci->devs[slot_id]->eps[ep_index];
ae636747 684
678539cf 685 if (list_empty(&ep->cancelled_td_list)) {
6f5165cf 686 xhci_stop_watchdog_timer_in_irq(xhci, ep);
0714a57c
SS
687 ep->stopped_td = NULL;
688 ep->stopped_trb = NULL;
e9df17eb 689 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ae636747 690 return;
678539cf 691 }
ae636747
SS
692
693 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
694 * We have the xHCI lock, so nothing can modify this list until we drop
695 * it. We're also in the event handler, so we can't get re-interrupted
696 * if another Stop Endpoint command completes
697 */
63a0d9ab 698 list_for_each(entry, &ep->cancelled_td_list) {
ae636747 699 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
700e2052
GKH
700 xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
701 cur_td->first_trb,
23e3be11 702 (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
e9df17eb
SS
703 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
704 if (!ep_ring) {
705 /* This shouldn't happen unless a driver is mucking
706 * with the stream ID after submission. This will
707 * leave the TD on the hardware ring, and the hardware
708 * will try to execute it, and may access a buffer
709 * that has already been freed. In the best case, the
710 * hardware will execute it, and the event handler will
711 * ignore the completion event for that TD, since it was
712 * removed from the td_list for that endpoint. In
713 * short, don't muck with the stream ID after
714 * submission.
715 */
716 xhci_warn(xhci, "WARN Cancelled URB %p "
717 "has invalid stream ID %u.\n",
718 cur_td->urb,
719 cur_td->urb->stream_id);
720 goto remove_finished_td;
721 }
ae636747
SS
722 /*
723 * If we stopped on the TD we need to cancel, then we have to
724 * move the xHC endpoint ring dequeue pointer past this TD.
725 */
63a0d9ab 726 if (cur_td == ep->stopped_td)
e9df17eb
SS
727 xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
728 cur_td->urb->stream_id,
729 cur_td, &deq_state);
ae636747 730 else
522989a2 731 td_to_noop(xhci, ep_ring, cur_td, false);
e9df17eb 732remove_finished_td:
ae636747
SS
733 /*
734 * The event handler won't see a completion for this TD anymore,
735 * so remove it from the endpoint ring's TD list. Keep it in
736 * the cancelled TD list for URB completion later.
737 */
585df1d9 738 list_del_init(&cur_td->td_list);
ae636747
SS
739 }
740 last_unlinked_td = cur_td;
6f5165cf 741 xhci_stop_watchdog_timer_in_irq(xhci, ep);
ae636747
SS
742
743 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
744 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
63a0d9ab 745 xhci_queue_new_dequeue_state(xhci,
e9df17eb
SS
746 slot_id, ep_index,
747 ep->stopped_td->urb->stream_id,
748 &deq_state);
ac9d8fe7 749 xhci_ring_cmd_db(xhci);
ae636747 750 } else {
e9df17eb
SS
751 /* Otherwise ring the doorbell(s) to restart queued transfers */
752 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ae636747 753 }
1624ae1c
SS
754 ep->stopped_td = NULL;
755 ep->stopped_trb = NULL;
ae636747
SS
756
757 /*
758 * Drop the lock and complete the URBs in the cancelled TD list.
759 * New TDs to be cancelled might be added to the end of the list before
760 * we can complete all the URBs for the TDs we already unlinked.
761 * So stop when we've completed the URB for the last TD we unlinked.
762 */
763 do {
63a0d9ab 764 cur_td = list_entry(ep->cancelled_td_list.next,
ae636747 765 struct xhci_td, cancelled_td_list);
585df1d9 766 list_del_init(&cur_td->cancelled_td_list);
ae636747
SS
767
768 /* Clean up the cancelled URB */
ae636747
SS
769 /* Doesn't matter what we pass for status, since the core will
770 * just overwrite it (because the URB has been unlinked).
771 */
6f5165cf 772 xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
ae636747 773
6f5165cf
SS
774 /* Stop processing the cancelled list if the watchdog timer is
775 * running.
776 */
777 if (xhci->xhc_state & XHCI_STATE_DYING)
778 return;
ae636747
SS
779 } while (cur_td != last_unlinked_td);
780
781 /* Return to the event handler with xhci->lock re-acquired */
782}
783
6f5165cf
SS
784/* Watchdog timer function for when a stop endpoint command fails to complete.
785 * In this case, we assume the host controller is broken or dying or dead. The
786 * host may still be completing some other events, so we have to be careful to
787 * let the event ring handler and the URB dequeueing/enqueueing functions know
788 * through xhci->state.
789 *
790 * The timer may also fire if the host takes a very long time to respond to the
791 * command, and the stop endpoint command completion handler cannot delete the
792 * timer before the timer function is called. Another endpoint cancellation may
793 * sneak in before the timer function can grab the lock, and that may queue
794 * another stop endpoint command and add the timer back. So we cannot use a
795 * simple flag to say whether there is a pending stop endpoint command for a
796 * particular endpoint.
797 *
798 * Instead we use a combination of that flag and a counter for the number of
799 * pending stop endpoint commands. If the timer is the tail end of the last
800 * stop endpoint command, and the endpoint's command is still pending, we assume
801 * the host is dying.
802 */
803void xhci_stop_endpoint_command_watchdog(unsigned long arg)
804{
805 struct xhci_hcd *xhci;
806 struct xhci_virt_ep *ep;
807 struct xhci_virt_ep *temp_ep;
808 struct xhci_ring *ring;
809 struct xhci_td *cur_td;
810 int ret, i, j;
f43d6231 811 unsigned long flags;
6f5165cf
SS
812
813 ep = (struct xhci_virt_ep *) arg;
814 xhci = ep->xhci;
815
f43d6231 816 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
817
818 ep->stop_cmds_pending--;
819 if (xhci->xhc_state & XHCI_STATE_DYING) {
820 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
821 "xHCI as DYING, exiting.\n");
f43d6231 822 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf
SS
823 return;
824 }
825 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
826 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
827 "exiting.\n");
f43d6231 828 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf
SS
829 return;
830 }
831
832 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
833 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
834 /* Oops, HC is dead or dying or at least not responding to the stop
835 * endpoint command.
836 */
837 xhci->xhc_state |= XHCI_STATE_DYING;
838 /* Disable interrupts from the host controller and start halting it */
839 xhci_quiesce(xhci);
f43d6231 840 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf
SS
841
842 ret = xhci_halt(xhci);
843
f43d6231 844 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
845 if (ret < 0) {
846 /* This is bad; the host is not responding to commands and it's
847 * not allowing itself to be halted. At least interrupts are
ac04e6ff 848 * disabled. If we call usb_hc_died(), it will attempt to
6f5165cf
SS
849 * disconnect all device drivers under this host. Those
850 * disconnect() methods will wait for all URBs to be unlinked,
851 * so we must complete them.
852 */
853 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
854 xhci_warn(xhci, "Completing active URBs anyway.\n");
855 /* We could turn all TDs on the rings to no-ops. This won't
856 * help if the host has cached part of the ring, and is slow if
857 * we want to preserve the cycle bit. Skip it and hope the host
858 * doesn't touch the memory.
859 */
860 }
861 for (i = 0; i < MAX_HC_SLOTS; i++) {
862 if (!xhci->devs[i])
863 continue;
864 for (j = 0; j < 31; j++) {
865 temp_ep = &xhci->devs[i]->eps[j];
866 ring = temp_ep->ring;
867 if (!ring)
868 continue;
869 xhci_dbg(xhci, "Killing URBs for slot ID %u, "
870 "ep index %u\n", i, j);
871 while (!list_empty(&ring->td_list)) {
872 cur_td = list_first_entry(&ring->td_list,
873 struct xhci_td,
874 td_list);
585df1d9 875 list_del_init(&cur_td->td_list);
6f5165cf 876 if (!list_empty(&cur_td->cancelled_td_list))
585df1d9 877 list_del_init(&cur_td->cancelled_td_list);
6f5165cf
SS
878 xhci_giveback_urb_in_irq(xhci, cur_td,
879 -ESHUTDOWN, "killed");
880 }
881 while (!list_empty(&temp_ep->cancelled_td_list)) {
882 cur_td = list_first_entry(
883 &temp_ep->cancelled_td_list,
884 struct xhci_td,
885 cancelled_td_list);
585df1d9 886 list_del_init(&cur_td->cancelled_td_list);
6f5165cf
SS
887 xhci_giveback_urb_in_irq(xhci, cur_td,
888 -ESHUTDOWN, "killed");
889 }
890 }
891 }
f43d6231 892 spin_unlock_irqrestore(&xhci->lock, flags);
6f5165cf 893 xhci_dbg(xhci, "Calling usb_hc_died()\n");
f6ff0ac8 894 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
6f5165cf
SS
895 xhci_dbg(xhci, "xHCI host controller is dead.\n");
896}
897
ae636747
SS
898/*
899 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
900 * we need to clear the set deq pending flag in the endpoint ring state, so that
901 * the TD queueing code can ring the doorbell again. We also need to ring the
902 * endpoint doorbell to restart the ring, but only if there aren't more
903 * cancellations pending.
904 */
905static void handle_set_deq_completion(struct xhci_hcd *xhci,
906 struct xhci_event_cmd *event,
907 union xhci_trb *trb)
908{
909 unsigned int slot_id;
910 unsigned int ep_index;
e9df17eb 911 unsigned int stream_id;
ae636747
SS
912 struct xhci_ring *ep_ring;
913 struct xhci_virt_device *dev;
d115b048
JY
914 struct xhci_ep_ctx *ep_ctx;
915 struct xhci_slot_ctx *slot_ctx;
ae636747 916
28ccd296
ME
917 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
918 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
919 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
ae636747 920 dev = xhci->devs[slot_id];
e9df17eb
SS
921
922 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
923 if (!ep_ring) {
924 xhci_warn(xhci, "WARN Set TR deq ptr command for "
925 "freed stream ID %u\n",
926 stream_id);
927 /* XXX: Harmless??? */
928 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
929 return;
930 }
931
d115b048
JY
932 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
933 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
ae636747 934
28ccd296 935 if (GET_COMP_CODE(le32_to_cpu(event->status)) != COMP_SUCCESS) {
ae636747
SS
936 unsigned int ep_state;
937 unsigned int slot_state;
938
28ccd296 939 switch (GET_COMP_CODE(le32_to_cpu(event->status))) {
ae636747
SS
940 case COMP_TRB_ERR:
941 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
942 "of stream ID configuration\n");
943 break;
944 case COMP_CTX_STATE:
945 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
946 "to incorrect slot or ep state.\n");
28ccd296 947 ep_state = le32_to_cpu(ep_ctx->ep_info);
ae636747 948 ep_state &= EP_STATE_MASK;
28ccd296 949 slot_state = le32_to_cpu(slot_ctx->dev_state);
ae636747
SS
950 slot_state = GET_SLOT_STATE(slot_state);
951 xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
952 slot_state, ep_state);
953 break;
954 case COMP_EBADSLT:
955 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
956 "slot %u was not enabled.\n", slot_id);
957 break;
958 default:
959 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
960 "completion code of %u.\n",
28ccd296 961 GET_COMP_CODE(le32_to_cpu(event->status)));
ae636747
SS
962 break;
963 }
964 /* OK what do we do now? The endpoint state is hosed, and we
965 * should never get to this point if the synchronization between
966 * queueing, and endpoint state are correct. This might happen
967 * if the device gets disconnected after we've finished
968 * cancelling URBs, which might not be an error...
969 */
970 } else {
8e595a5d 971 xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
28ccd296 972 le64_to_cpu(ep_ctx->deq));
bf161e85 973 if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg,
28ccd296
ME
974 dev->eps[ep_index].queued_deq_ptr) ==
975 (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) {
bf161e85
SS
976 /* Update the ring's dequeue segment and dequeue pointer
977 * to reflect the new position.
978 */
979 ep_ring->deq_seg = dev->eps[ep_index].queued_deq_seg;
980 ep_ring->dequeue = dev->eps[ep_index].queued_deq_ptr;
981 } else {
982 xhci_warn(xhci, "Mismatch between completed Set TR Deq "
983 "Ptr command & xHCI internal state.\n");
984 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
985 dev->eps[ep_index].queued_deq_seg,
986 dev->eps[ep_index].queued_deq_ptr);
987 }
ae636747
SS
988 }
989
63a0d9ab 990 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
bf161e85
SS
991 dev->eps[ep_index].queued_deq_seg = NULL;
992 dev->eps[ep_index].queued_deq_ptr = NULL;
e9df17eb
SS
993 /* Restart any rings with pending URBs */
994 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ae636747
SS
995}
996
a1587d97
SS
997static void handle_reset_ep_completion(struct xhci_hcd *xhci,
998 struct xhci_event_cmd *event,
999 union xhci_trb *trb)
1000{
1001 int slot_id;
1002 unsigned int ep_index;
1003
28ccd296
ME
1004 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
1005 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
a1587d97
SS
1006 /* This command will only fail if the endpoint wasn't halted,
1007 * but we don't care.
1008 */
1009 xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
f5960b69 1010 GET_COMP_CODE(le32_to_cpu(event->status)));
a1587d97 1011
ac9d8fe7
SS
1012 /* HW with the reset endpoint quirk needs to have a configure endpoint
1013 * command complete before the endpoint can be used. Queue that here
1014 * because the HW can't handle two commands being queued in a row.
1015 */
1016 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
1017 xhci_dbg(xhci, "Queueing configure endpoint command\n");
1018 xhci_queue_configure_endpoint(xhci,
913a8a34
SS
1019 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1020 false);
ac9d8fe7
SS
1021 xhci_ring_cmd_db(xhci);
1022 } else {
e9df17eb 1023 /* Clear our internal halted state and restart the ring(s) */
63a0d9ab 1024 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
e9df17eb 1025 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
ac9d8fe7 1026 }
a1587d97 1027}
ae636747 1028
a50c8aa9
SS
1029/* Check to see if a command in the device's command queue matches this one.
1030 * Signal the completion or free the command, and return 1. Return 0 if the
1031 * completed command isn't at the head of the command list.
1032 */
1033static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
1034 struct xhci_virt_device *virt_dev,
1035 struct xhci_event_cmd *event)
1036{
1037 struct xhci_command *command;
1038
1039 if (list_empty(&virt_dev->cmd_list))
1040 return 0;
1041
1042 command = list_entry(virt_dev->cmd_list.next,
1043 struct xhci_command, cmd_list);
1044 if (xhci->cmd_ring->dequeue != command->command_trb)
1045 return 0;
1046
28ccd296 1047 command->status = GET_COMP_CODE(le32_to_cpu(event->status));
a50c8aa9
SS
1048 list_del(&command->cmd_list);
1049 if (command->completion)
1050 complete(command->completion);
1051 else
1052 xhci_free_command(xhci, command);
1053 return 1;
1054}
1055
7f84eef0
SS
1056static void handle_cmd_completion(struct xhci_hcd *xhci,
1057 struct xhci_event_cmd *event)
1058{
28ccd296 1059 int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
7f84eef0
SS
1060 u64 cmd_dma;
1061 dma_addr_t cmd_dequeue_dma;
ac9d8fe7 1062 struct xhci_input_control_ctx *ctrl_ctx;
913a8a34 1063 struct xhci_virt_device *virt_dev;
ac9d8fe7
SS
1064 unsigned int ep_index;
1065 struct xhci_ring *ep_ring;
1066 unsigned int ep_state;
7f84eef0 1067
28ccd296 1068 cmd_dma = le64_to_cpu(event->cmd_trb);
23e3be11 1069 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
7f84eef0
SS
1070 xhci->cmd_ring->dequeue);
1071 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1072 if (cmd_dequeue_dma == 0) {
1073 xhci->error_bitmask |= 1 << 4;
1074 return;
1075 }
1076 /* Does the DMA address match our internal dequeue pointer address? */
1077 if (cmd_dma != (u64) cmd_dequeue_dma) {
1078 xhci->error_bitmask |= 1 << 5;
1079 return;
1080 }
28ccd296
ME
1081 switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])
1082 & TRB_TYPE_BITMASK) {
3ffbba95 1083 case TRB_TYPE(TRB_ENABLE_SLOT):
28ccd296 1084 if (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_SUCCESS)
3ffbba95
SS
1085 xhci->slot_id = slot_id;
1086 else
1087 xhci->slot_id = 0;
1088 complete(&xhci->addr_dev);
1089 break;
1090 case TRB_TYPE(TRB_DISABLE_SLOT):
2cf95c18
SS
1091 if (xhci->devs[slot_id]) {
1092 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1093 /* Delete default control endpoint resources */
1094 xhci_free_device_endpoint_resources(xhci,
1095 xhci->devs[slot_id], true);
3ffbba95 1096 xhci_free_virt_device(xhci, slot_id);
2cf95c18 1097 }
3ffbba95 1098 break;
f94e0186 1099 case TRB_TYPE(TRB_CONFIG_EP):
913a8a34 1100 virt_dev = xhci->devs[slot_id];
a50c8aa9 1101 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
913a8a34 1102 break;
ac9d8fe7
SS
1103 /*
1104 * Configure endpoint commands can come from the USB core
1105 * configuration or alt setting changes, or because the HW
1106 * needed an extra configure endpoint command after a reset
8df75f42
SS
1107 * endpoint command or streams were being configured.
1108 * If the command was for a halted endpoint, the xHCI driver
1109 * is not waiting on the configure endpoint command.
ac9d8fe7
SS
1110 */
1111 ctrl_ctx = xhci_get_input_control_ctx(xhci,
913a8a34 1112 virt_dev->in_ctx);
ac9d8fe7 1113 /* Input ctx add_flags are the endpoint index plus one */
28ccd296 1114 ep_index = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags)) - 1;
06df5729 1115 /* A usb_set_interface() call directly after clearing a halted
e9df17eb
SS
1116 * condition may race on this quirky hardware. Not worth
1117 * worrying about, since this is prototype hardware. Not sure
1118 * if this will work for streams, but streams support was
1119 * untested on this prototype.
06df5729 1120 */
ac9d8fe7 1121 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
06df5729 1122 ep_index != (unsigned int) -1 &&
28ccd296
ME
1123 le32_to_cpu(ctrl_ctx->add_flags) - SLOT_FLAG ==
1124 le32_to_cpu(ctrl_ctx->drop_flags)) {
06df5729
SS
1125 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
1126 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1127 if (!(ep_state & EP_HALTED))
1128 goto bandwidth_change;
1129 xhci_dbg(xhci, "Completed config ep cmd - "
1130 "last ep index = %d, state = %d\n",
1131 ep_index, ep_state);
e9df17eb 1132 /* Clear internal halted state and restart ring(s) */
63a0d9ab 1133 xhci->devs[slot_id]->eps[ep_index].ep_state &=
ac9d8fe7 1134 ~EP_HALTED;
e9df17eb 1135 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
06df5729 1136 break;
ac9d8fe7 1137 }
06df5729
SS
1138bandwidth_change:
1139 xhci_dbg(xhci, "Completed config ep cmd\n");
1140 xhci->devs[slot_id]->cmd_status =
28ccd296 1141 GET_COMP_CODE(le32_to_cpu(event->status));
06df5729 1142 complete(&xhci->devs[slot_id]->cmd_completion);
f94e0186 1143 break;
2d3f1fac 1144 case TRB_TYPE(TRB_EVAL_CONTEXT):
ac1c1b7f
SS
1145 virt_dev = xhci->devs[slot_id];
1146 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
1147 break;
28ccd296 1148 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
2d3f1fac
SS
1149 complete(&xhci->devs[slot_id]->cmd_completion);
1150 break;
3ffbba95 1151 case TRB_TYPE(TRB_ADDR_DEV):
28ccd296 1152 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
3ffbba95
SS
1153 complete(&xhci->addr_dev);
1154 break;
ae636747 1155 case TRB_TYPE(TRB_STOP_RING):
be88fe4f 1156 handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event);
ae636747
SS
1157 break;
1158 case TRB_TYPE(TRB_SET_DEQ):
1159 handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
1160 break;
7f84eef0 1161 case TRB_TYPE(TRB_CMD_NOOP):
7f84eef0 1162 break;
a1587d97
SS
1163 case TRB_TYPE(TRB_RESET_EP):
1164 handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
1165 break;
2a8f82c4
SS
1166 case TRB_TYPE(TRB_RESET_DEV):
1167 xhci_dbg(xhci, "Completed reset device command.\n");
1168 slot_id = TRB_TO_SLOT_ID(
28ccd296 1169 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
2a8f82c4
SS
1170 virt_dev = xhci->devs[slot_id];
1171 if (virt_dev)
1172 handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
1173 else
1174 xhci_warn(xhci, "Reset device command completion "
1175 "for disabled slot %u\n", slot_id);
1176 break;
0238634d
SS
1177 case TRB_TYPE(TRB_NEC_GET_FW):
1178 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1179 xhci->error_bitmask |= 1 << 6;
1180 break;
1181 }
1182 xhci_dbg(xhci, "NEC firmware version %2x.%02x\n",
28ccd296
ME
1183 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1184 NEC_FW_MINOR(le32_to_cpu(event->status)));
0238634d 1185 break;
7f84eef0
SS
1186 default:
1187 /* Skip over unknown commands on the event ring */
1188 xhci->error_bitmask |= 1 << 6;
1189 break;
1190 }
1191 inc_deq(xhci, xhci->cmd_ring, false);
1192}
1193
0238634d
SS
1194static void handle_vendor_event(struct xhci_hcd *xhci,
1195 union xhci_trb *event)
1196{
1197 u32 trb_type;
1198
28ccd296 1199 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
0238634d
SS
1200 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1201 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1202 handle_cmd_completion(xhci, &event->event_cmd);
1203}
1204
f6ff0ac8
SS
1205/* @port_id: the one-based port ID from the hardware (indexed from array of all
1206 * port registers -- USB 3.0 and USB 2.0).
1207 *
1208 * Returns a zero-based port number, which is suitable for indexing into each of
1209 * the split roothubs' port arrays and bus state arrays.
1210 */
1211static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1212 struct xhci_hcd *xhci, u32 port_id)
1213{
1214 unsigned int i;
1215 unsigned int num_similar_speed_ports = 0;
1216
1217 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1218 * and usb2_ports are 0-based indexes. Count the number of similar
1219 * speed ports, up to 1 port before this port.
1220 */
1221 for (i = 0; i < (port_id - 1); i++) {
1222 u8 port_speed = xhci->port_array[i];
1223
1224 /*
1225 * Skip ports that don't have known speeds, or have duplicate
1226 * Extended Capabilities port speed entries.
1227 */
22e04870 1228 if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
f6ff0ac8
SS
1229 continue;
1230
1231 /*
1232 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1233 * 1.1 ports are under the USB 2.0 hub. If the port speed
1234 * matches the device speed, it's a similar speed port.
1235 */
1236 if ((port_speed == 0x03) == (hcd->speed == HCD_USB3))
1237 num_similar_speed_ports++;
1238 }
1239 return num_similar_speed_ports;
1240}
1241
0f2a7930
SS
1242static void handle_port_status(struct xhci_hcd *xhci,
1243 union xhci_trb *event)
1244{
f6ff0ac8 1245 struct usb_hcd *hcd;
0f2a7930 1246 u32 port_id;
56192531 1247 u32 temp, temp1;
518e848e 1248 int max_ports;
56192531 1249 int slot_id;
5308a91b 1250 unsigned int faked_port_index;
f6ff0ac8 1251 u8 major_revision;
20b67cf5 1252 struct xhci_bus_state *bus_state;
28ccd296 1253 __le32 __iomem **port_array;
386139d7 1254 bool bogus_port_status = false;
0f2a7930
SS
1255
1256 /* Port status change events always have a successful completion code */
28ccd296 1257 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
0f2a7930
SS
1258 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1259 xhci->error_bitmask |= 1 << 8;
1260 }
28ccd296 1261 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
0f2a7930
SS
1262 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1263
518e848e
SS
1264 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1265 if ((port_id <= 0) || (port_id > max_ports)) {
56192531 1266 xhci_warn(xhci, "Invalid port id %d\n", port_id);
386139d7 1267 bogus_port_status = true;
56192531
AX
1268 goto cleanup;
1269 }
1270
f6ff0ac8
SS
1271 /* Figure out which usb_hcd this port is attached to:
1272 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1273 */
1274 major_revision = xhci->port_array[port_id - 1];
1275 if (major_revision == 0) {
1276 xhci_warn(xhci, "Event for port %u not in "
1277 "Extended Capabilities, ignoring.\n",
1278 port_id);
386139d7 1279 bogus_port_status = true;
f6ff0ac8 1280 goto cleanup;
5308a91b 1281 }
22e04870 1282 if (major_revision == DUPLICATE_ENTRY) {
f6ff0ac8
SS
1283 xhci_warn(xhci, "Event for port %u duplicated in"
1284 "Extended Capabilities, ignoring.\n",
1285 port_id);
386139d7 1286 bogus_port_status = true;
f6ff0ac8
SS
1287 goto cleanup;
1288 }
1289
1290 /*
1291 * Hardware port IDs reported by a Port Status Change Event include USB
1292 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1293 * resume event, but we first need to translate the hardware port ID
1294 * into the index into the ports on the correct split roothub, and the
1295 * correct bus_state structure.
1296 */
1297 /* Find the right roothub. */
1298 hcd = xhci_to_hcd(xhci);
1299 if ((major_revision == 0x03) != (hcd->speed == HCD_USB3))
1300 hcd = xhci->shared_hcd;
1301 bus_state = &xhci->bus_state[hcd_index(hcd)];
1302 if (hcd->speed == HCD_USB3)
1303 port_array = xhci->usb3_ports;
1304 else
1305 port_array = xhci->usb2_ports;
1306 /* Find the faked port hub number */
1307 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1308 port_id);
5308a91b 1309
5308a91b 1310 temp = xhci_readl(xhci, port_array[faked_port_index]);
7111ebc9 1311 if (hcd->state == HC_STATE_SUSPENDED) {
56192531
AX
1312 xhci_dbg(xhci, "resume root hub\n");
1313 usb_hcd_resume_root_hub(hcd);
1314 }
1315
1316 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1317 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1318
1319 temp1 = xhci_readl(xhci, &xhci->op_regs->command);
1320 if (!(temp1 & CMD_RUN)) {
1321 xhci_warn(xhci, "xHC is not running.\n");
1322 goto cleanup;
1323 }
1324
1325 if (DEV_SUPERSPEED(temp)) {
1326 xhci_dbg(xhci, "resume SS port %d\n", port_id);
c9682dff
AX
1327 xhci_set_link_state(xhci, port_array, faked_port_index,
1328 XDEV_U0);
5233630f
SS
1329 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1330 faked_port_index);
56192531
AX
1331 if (!slot_id) {
1332 xhci_dbg(xhci, "slot_id is zero\n");
1333 goto cleanup;
1334 }
1335 xhci_ring_device(xhci, slot_id);
1336 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
1337 /* Clear PORT_PLC */
d2f52c9e
AX
1338 xhci_test_and_clear_bit(xhci, port_array,
1339 faked_port_index, PORT_PLC);
56192531
AX
1340 } else {
1341 xhci_dbg(xhci, "resume HS port %d\n", port_id);
f6ff0ac8 1342 bus_state->resume_done[faked_port_index] = jiffies +
56192531
AX
1343 msecs_to_jiffies(20);
1344 mod_timer(&hcd->rh_timer,
f6ff0ac8 1345 bus_state->resume_done[faked_port_index]);
56192531
AX
1346 /* Do the rest in GetPortStatus */
1347 }
1348 }
1349
6fd45621
AX
1350 if (hcd->speed != HCD_USB3)
1351 xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
1352 PORT_PLC);
1353
56192531 1354cleanup:
0f2a7930
SS
1355 /* Update event ring dequeue pointer before dropping the lock */
1356 inc_deq(xhci, xhci->event_ring, true);
0f2a7930 1357
386139d7
SS
1358 /* Don't make the USB core poll the roothub if we got a bad port status
1359 * change event. Besides, at that point we can't tell which roothub
1360 * (USB 2.0 or USB 3.0) to kick.
1361 */
1362 if (bogus_port_status)
1363 return;
1364
0f2a7930
SS
1365 spin_unlock(&xhci->lock);
1366 /* Pass this up to the core */
f6ff0ac8 1367 usb_hcd_poll_rh_status(hcd);
0f2a7930
SS
1368 spin_lock(&xhci->lock);
1369}
1370
d0e96f5a
SS
1371/*
1372 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1373 * at end_trb, which may be in another segment. If the suspect DMA address is a
1374 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1375 * returns 0.
1376 */
6648f29d 1377struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
d0e96f5a
SS
1378 union xhci_trb *start_trb,
1379 union xhci_trb *end_trb,
1380 dma_addr_t suspect_dma)
1381{
1382 dma_addr_t start_dma;
1383 dma_addr_t end_seg_dma;
1384 dma_addr_t end_trb_dma;
1385 struct xhci_segment *cur_seg;
1386
23e3be11 1387 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
d0e96f5a
SS
1388 cur_seg = start_seg;
1389
1390 do {
2fa88daa 1391 if (start_dma == 0)
326b4810 1392 return NULL;
ae636747 1393 /* We may get an event for a Link TRB in the middle of a TD */
23e3be11 1394 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
2fa88daa 1395 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
d0e96f5a 1396 /* If the end TRB isn't in this segment, this is set to 0 */
23e3be11 1397 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
d0e96f5a
SS
1398
1399 if (end_trb_dma > 0) {
1400 /* The end TRB is in this segment, so suspect should be here */
1401 if (start_dma <= end_trb_dma) {
1402 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1403 return cur_seg;
1404 } else {
1405 /* Case for one segment with
1406 * a TD wrapped around to the top
1407 */
1408 if ((suspect_dma >= start_dma &&
1409 suspect_dma <= end_seg_dma) ||
1410 (suspect_dma >= cur_seg->dma &&
1411 suspect_dma <= end_trb_dma))
1412 return cur_seg;
1413 }
326b4810 1414 return NULL;
d0e96f5a
SS
1415 } else {
1416 /* Might still be somewhere in this segment */
1417 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1418 return cur_seg;
1419 }
1420 cur_seg = cur_seg->next;
23e3be11 1421 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
2fa88daa 1422 } while (cur_seg != start_seg);
d0e96f5a 1423
326b4810 1424 return NULL;
d0e96f5a
SS
1425}
1426
bcef3fd5
SS
1427static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1428 unsigned int slot_id, unsigned int ep_index,
e9df17eb 1429 unsigned int stream_id,
bcef3fd5
SS
1430 struct xhci_td *td, union xhci_trb *event_trb)
1431{
1432 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
1433 ep->ep_state |= EP_HALTED;
1434 ep->stopped_td = td;
1435 ep->stopped_trb = event_trb;
e9df17eb 1436 ep->stopped_stream = stream_id;
1624ae1c 1437
bcef3fd5
SS
1438 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1439 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
1624ae1c
SS
1440
1441 ep->stopped_td = NULL;
1442 ep->stopped_trb = NULL;
5e5cf6fc 1443 ep->stopped_stream = 0;
1624ae1c 1444
bcef3fd5
SS
1445 xhci_ring_cmd_db(xhci);
1446}
1447
1448/* Check if an error has halted the endpoint ring. The class driver will
1449 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1450 * However, a babble and other errors also halt the endpoint ring, and the class
1451 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1452 * Ring Dequeue Pointer command manually.
1453 */
1454static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1455 struct xhci_ep_ctx *ep_ctx,
1456 unsigned int trb_comp_code)
1457{
1458 /* TRB completion codes that may require a manual halt cleanup */
1459 if (trb_comp_code == COMP_TX_ERR ||
1460 trb_comp_code == COMP_BABBLE ||
1461 trb_comp_code == COMP_SPLIT_ERR)
1462 /* The 0.96 spec says a babbling control endpoint
1463 * is not halted. The 0.96 spec says it is. Some HW
1464 * claims to be 0.95 compliant, but it halts the control
1465 * endpoint anyway. Check if a babble halted the
1466 * endpoint.
1467 */
f5960b69
ME
1468 if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1469 cpu_to_le32(EP_STATE_HALTED))
bcef3fd5
SS
1470 return 1;
1471
1472 return 0;
1473}
1474
b45b5069
SS
1475int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1476{
1477 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1478 /* Vendor defined "informational" completion code,
1479 * treat as not-an-error.
1480 */
1481 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1482 trb_comp_code);
1483 xhci_dbg(xhci, "Treating code as success.\n");
1484 return 1;
1485 }
1486 return 0;
1487}
1488
4422da61
AX
1489/*
1490 * Finish the td processing, remove the td from td list;
1491 * Return 1 if the urb can be given back.
1492 */
1493static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1494 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1495 struct xhci_virt_ep *ep, int *status, bool skip)
1496{
1497 struct xhci_virt_device *xdev;
1498 struct xhci_ring *ep_ring;
1499 unsigned int slot_id;
1500 int ep_index;
1501 struct urb *urb = NULL;
1502 struct xhci_ep_ctx *ep_ctx;
1503 int ret = 0;
8e51adcc 1504 struct urb_priv *urb_priv;
4422da61
AX
1505 u32 trb_comp_code;
1506
28ccd296 1507 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
4422da61 1508 xdev = xhci->devs[slot_id];
28ccd296
ME
1509 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1510 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
4422da61 1511 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
28ccd296 1512 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
4422da61
AX
1513
1514 if (skip)
1515 goto td_cleanup;
1516
1517 if (trb_comp_code == COMP_STOP_INVAL ||
1518 trb_comp_code == COMP_STOP) {
1519 /* The Endpoint Stop Command completion will take care of any
1520 * stopped TDs. A stopped TD may be restarted, so don't update
1521 * the ring dequeue pointer or take this TD off any lists yet.
1522 */
1523 ep->stopped_td = td;
1524 ep->stopped_trb = event_trb;
1525 return 0;
1526 } else {
1527 if (trb_comp_code == COMP_STALL) {
1528 /* The transfer is completed from the driver's
1529 * perspective, but we need to issue a set dequeue
1530 * command for this stalled endpoint to move the dequeue
1531 * pointer past the TD. We can't do that here because
1532 * the halt condition must be cleared first. Let the
1533 * USB class driver clear the stall later.
1534 */
1535 ep->stopped_td = td;
1536 ep->stopped_trb = event_trb;
1537 ep->stopped_stream = ep_ring->stream_id;
1538 } else if (xhci_requires_manual_halt_cleanup(xhci,
1539 ep_ctx, trb_comp_code)) {
1540 /* Other types of errors halt the endpoint, but the
1541 * class driver doesn't call usb_reset_endpoint() unless
1542 * the error is -EPIPE. Clear the halted status in the
1543 * xHCI hardware manually.
1544 */
1545 xhci_cleanup_halted_endpoint(xhci,
1546 slot_id, ep_index, ep_ring->stream_id,
1547 td, event_trb);
1548 } else {
1549 /* Update ring dequeue pointer */
1550 while (ep_ring->dequeue != td->last_trb)
1551 inc_deq(xhci, ep_ring, false);
1552 inc_deq(xhci, ep_ring, false);
1553 }
1554
1555td_cleanup:
1556 /* Clean up the endpoint's TD list */
1557 urb = td->urb;
8e51adcc 1558 urb_priv = urb->hcpriv;
4422da61
AX
1559
1560 /* Do one last check of the actual transfer length.
1561 * If the host controller said we transferred more data than
1562 * the buffer length, urb->actual_length will be a very big
1563 * number (since it's unsigned). Play it safe and say we didn't
1564 * transfer anything.
1565 */
1566 if (urb->actual_length > urb->transfer_buffer_length) {
1567 xhci_warn(xhci, "URB transfer length is wrong, "
1568 "xHC issue? req. len = %u, "
1569 "act. len = %u\n",
1570 urb->transfer_buffer_length,
1571 urb->actual_length);
1572 urb->actual_length = 0;
1573 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1574 *status = -EREMOTEIO;
1575 else
1576 *status = 0;
1577 }
585df1d9 1578 list_del_init(&td->td_list);
4422da61
AX
1579 /* Was this TD slated to be cancelled but completed anyway? */
1580 if (!list_empty(&td->cancelled_td_list))
585df1d9 1581 list_del_init(&td->cancelled_td_list);
4422da61 1582
8e51adcc
AX
1583 urb_priv->td_cnt++;
1584 /* Giveback the urb when all the tds are completed */
c41136b0 1585 if (urb_priv->td_cnt == urb_priv->length) {
8e51adcc 1586 ret = 1;
c41136b0
AX
1587 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1588 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
1589 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs
1590 == 0) {
1591 if (xhci->quirks & XHCI_AMD_PLL_FIX)
1592 usb_amd_quirk_pll_enable();
1593 }
1594 }
1595 }
4422da61
AX
1596 }
1597
1598 return ret;
1599}
1600
8af56be1
AX
1601/*
1602 * Process control tds, update urb status and actual_length.
1603 */
1604static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1605 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1606 struct xhci_virt_ep *ep, int *status)
1607{
1608 struct xhci_virt_device *xdev;
1609 struct xhci_ring *ep_ring;
1610 unsigned int slot_id;
1611 int ep_index;
1612 struct xhci_ep_ctx *ep_ctx;
1613 u32 trb_comp_code;
1614
28ccd296 1615 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
8af56be1 1616 xdev = xhci->devs[slot_id];
28ccd296
ME
1617 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1618 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
8af56be1 1619 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
28ccd296 1620 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
8af56be1 1621
8af56be1
AX
1622 switch (trb_comp_code) {
1623 case COMP_SUCCESS:
1624 if (event_trb == ep_ring->dequeue) {
1625 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1626 "without IOC set??\n");
1627 *status = -ESHUTDOWN;
1628 } else if (event_trb != td->last_trb) {
1629 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1630 "without IOC set??\n");
1631 *status = -ESHUTDOWN;
1632 } else {
8af56be1
AX
1633 *status = 0;
1634 }
1635 break;
1636 case COMP_SHORT_TX:
8af56be1
AX
1637 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1638 *status = -EREMOTEIO;
1639 else
1640 *status = 0;
1641 break;
3abeca99
SS
1642 case COMP_STOP_INVAL:
1643 case COMP_STOP:
1644 return finish_td(xhci, td, event_trb, event, ep, status, false);
8af56be1
AX
1645 default:
1646 if (!xhci_requires_manual_halt_cleanup(xhci,
1647 ep_ctx, trb_comp_code))
1648 break;
1649 xhci_dbg(xhci, "TRB error code %u, "
1650 "halted endpoint index = %u\n",
1651 trb_comp_code, ep_index);
1652 /* else fall through */
1653 case COMP_STALL:
1654 /* Did we transfer part of the data (middle) phase? */
1655 if (event_trb != ep_ring->dequeue &&
1656 event_trb != td->last_trb)
1657 td->urb->actual_length =
1658 td->urb->transfer_buffer_length
28ccd296 1659 - TRB_LEN(le32_to_cpu(event->transfer_len));
8af56be1
AX
1660 else
1661 td->urb->actual_length = 0;
1662
1663 xhci_cleanup_halted_endpoint(xhci,
1664 slot_id, ep_index, 0, td, event_trb);
1665 return finish_td(xhci, td, event_trb, event, ep, status, true);
1666 }
1667 /*
1668 * Did we transfer any data, despite the errors that might have
1669 * happened? I.e. did we get past the setup stage?
1670 */
1671 if (event_trb != ep_ring->dequeue) {
1672 /* The event was for the status stage */
1673 if (event_trb == td->last_trb) {
1674 if (td->urb->actual_length != 0) {
1675 /* Don't overwrite a previously set error code
1676 */
1677 if ((*status == -EINPROGRESS || *status == 0) &&
1678 (td->urb->transfer_flags
1679 & URB_SHORT_NOT_OK))
1680 /* Did we already see a short data
1681 * stage? */
1682 *status = -EREMOTEIO;
1683 } else {
1684 td->urb->actual_length =
1685 td->urb->transfer_buffer_length;
1686 }
1687 } else {
1688 /* Maybe the event was for the data stage? */
3abeca99
SS
1689 td->urb->actual_length =
1690 td->urb->transfer_buffer_length -
1691 TRB_LEN(le32_to_cpu(event->transfer_len));
1692 xhci_dbg(xhci, "Waiting for status "
1693 "stage event\n");
1694 return 0;
8af56be1
AX
1695 }
1696 }
1697
1698 return finish_td(xhci, td, event_trb, event, ep, status, false);
1699}
1700
04e51901
AX
1701/*
1702 * Process isochronous tds, update urb packet status and actual_length.
1703 */
1704static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1705 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1706 struct xhci_virt_ep *ep, int *status)
1707{
1708 struct xhci_ring *ep_ring;
1709 struct urb_priv *urb_priv;
1710 int idx;
1711 int len = 0;
04e51901
AX
1712 union xhci_trb *cur_trb;
1713 struct xhci_segment *cur_seg;
926008c9 1714 struct usb_iso_packet_descriptor *frame;
04e51901 1715 u32 trb_comp_code;
926008c9 1716 bool skip_td = false;
04e51901 1717
28ccd296
ME
1718 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1719 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
04e51901
AX
1720 urb_priv = td->urb->hcpriv;
1721 idx = urb_priv->td_cnt;
926008c9 1722 frame = &td->urb->iso_frame_desc[idx];
04e51901 1723
926008c9
DT
1724 /* handle completion code */
1725 switch (trb_comp_code) {
1726 case COMP_SUCCESS:
1727 frame->status = 0;
926008c9
DT
1728 break;
1729 case COMP_SHORT_TX:
1730 frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
1731 -EREMOTEIO : 0;
1732 break;
1733 case COMP_BW_OVER:
1734 frame->status = -ECOMM;
1735 skip_td = true;
1736 break;
1737 case COMP_BUFF_OVER:
1738 case COMP_BABBLE:
1739 frame->status = -EOVERFLOW;
1740 skip_td = true;
1741 break;
f6ba6fe2 1742 case COMP_DEV_ERR:
926008c9
DT
1743 case COMP_STALL:
1744 frame->status = -EPROTO;
1745 skip_td = true;
1746 break;
1747 case COMP_STOP:
1748 case COMP_STOP_INVAL:
1749 break;
1750 default:
1751 frame->status = -1;
1752 break;
04e51901
AX
1753 }
1754
926008c9
DT
1755 if (trb_comp_code == COMP_SUCCESS || skip_td) {
1756 frame->actual_length = frame->length;
1757 td->urb->actual_length += frame->length;
04e51901
AX
1758 } else {
1759 for (cur_trb = ep_ring->dequeue,
1760 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
1761 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
f5960b69
ME
1762 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
1763 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
28ccd296 1764 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
04e51901 1765 }
28ccd296
ME
1766 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1767 TRB_LEN(le32_to_cpu(event->transfer_len));
04e51901
AX
1768
1769 if (trb_comp_code != COMP_STOP_INVAL) {
926008c9 1770 frame->actual_length = len;
04e51901
AX
1771 td->urb->actual_length += len;
1772 }
1773 }
1774
04e51901
AX
1775 return finish_td(xhci, td, event_trb, event, ep, status, false);
1776}
1777
926008c9
DT
1778static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1779 struct xhci_transfer_event *event,
1780 struct xhci_virt_ep *ep, int *status)
1781{
1782 struct xhci_ring *ep_ring;
1783 struct urb_priv *urb_priv;
1784 struct usb_iso_packet_descriptor *frame;
1785 int idx;
1786
f6975314 1787 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
926008c9
DT
1788 urb_priv = td->urb->hcpriv;
1789 idx = urb_priv->td_cnt;
1790 frame = &td->urb->iso_frame_desc[idx];
1791
b3df3f9c 1792 /* The transfer is partly done. */
926008c9
DT
1793 frame->status = -EXDEV;
1794
1795 /* calc actual length */
1796 frame->actual_length = 0;
1797
1798 /* Update ring dequeue pointer */
1799 while (ep_ring->dequeue != td->last_trb)
1800 inc_deq(xhci, ep_ring, false);
1801 inc_deq(xhci, ep_ring, false);
1802
1803 return finish_td(xhci, td, NULL, event, ep, status, true);
1804}
1805
22405ed2
AX
1806/*
1807 * Process bulk and interrupt tds, update urb status and actual_length.
1808 */
1809static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
1810 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1811 struct xhci_virt_ep *ep, int *status)
1812{
1813 struct xhci_ring *ep_ring;
1814 union xhci_trb *cur_trb;
1815 struct xhci_segment *cur_seg;
1816 u32 trb_comp_code;
1817
28ccd296
ME
1818 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1819 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
22405ed2
AX
1820
1821 switch (trb_comp_code) {
1822 case COMP_SUCCESS:
1823 /* Double check that the HW transferred everything. */
1824 if (event_trb != td->last_trb) {
1825 xhci_warn(xhci, "WARN Successful completion "
1826 "on short TX\n");
1827 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1828 *status = -EREMOTEIO;
1829 else
1830 *status = 0;
1831 } else {
22405ed2
AX
1832 *status = 0;
1833 }
1834 break;
1835 case COMP_SHORT_TX:
1836 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1837 *status = -EREMOTEIO;
1838 else
1839 *status = 0;
1840 break;
1841 default:
1842 /* Others already handled above */
1843 break;
1844 }
f444ff27
SS
1845 if (trb_comp_code == COMP_SHORT_TX)
1846 xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
1847 "%d bytes untransferred\n",
1848 td->urb->ep->desc.bEndpointAddress,
1849 td->urb->transfer_buffer_length,
1850 TRB_LEN(le32_to_cpu(event->transfer_len)));
22405ed2
AX
1851 /* Fast path - was this the last TRB in the TD for this URB? */
1852 if (event_trb == td->last_trb) {
28ccd296 1853 if (TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
22405ed2
AX
1854 td->urb->actual_length =
1855 td->urb->transfer_buffer_length -
28ccd296 1856 TRB_LEN(le32_to_cpu(event->transfer_len));
22405ed2
AX
1857 if (td->urb->transfer_buffer_length <
1858 td->urb->actual_length) {
1859 xhci_warn(xhci, "HC gave bad length "
1860 "of %d bytes left\n",
28ccd296 1861 TRB_LEN(le32_to_cpu(event->transfer_len)));
22405ed2
AX
1862 td->urb->actual_length = 0;
1863 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1864 *status = -EREMOTEIO;
1865 else
1866 *status = 0;
1867 }
1868 /* Don't overwrite a previously set error code */
1869 if (*status == -EINPROGRESS) {
1870 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1871 *status = -EREMOTEIO;
1872 else
1873 *status = 0;
1874 }
1875 } else {
1876 td->urb->actual_length =
1877 td->urb->transfer_buffer_length;
1878 /* Ignore a short packet completion if the
1879 * untransferred length was zero.
1880 */
1881 if (*status == -EREMOTEIO)
1882 *status = 0;
1883 }
1884 } else {
1885 /* Slow path - walk the list, starting from the dequeue
1886 * pointer, to get the actual length transferred.
1887 */
1888 td->urb->actual_length = 0;
1889 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
1890 cur_trb != event_trb;
1891 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
f5960b69
ME
1892 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
1893 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
22405ed2 1894 td->urb->actual_length +=
28ccd296 1895 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
22405ed2
AX
1896 }
1897 /* If the ring didn't stop on a Link or No-op TRB, add
1898 * in the actual bytes transferred from the Normal TRB
1899 */
1900 if (trb_comp_code != COMP_STOP_INVAL)
1901 td->urb->actual_length +=
28ccd296
ME
1902 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1903 TRB_LEN(le32_to_cpu(event->transfer_len));
22405ed2
AX
1904 }
1905
1906 return finish_td(xhci, td, event_trb, event, ep, status, false);
1907}
1908
d0e96f5a
SS
1909/*
1910 * If this function returns an error condition, it means it got a Transfer
1911 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
1912 * At this point, the host controller is probably hosed and should be reset.
1913 */
1914static int handle_tx_event(struct xhci_hcd *xhci,
1915 struct xhci_transfer_event *event)
1916{
1917 struct xhci_virt_device *xdev;
63a0d9ab 1918 struct xhci_virt_ep *ep;
d0e96f5a 1919 struct xhci_ring *ep_ring;
82d1009f 1920 unsigned int slot_id;
d0e96f5a 1921 int ep_index;
326b4810 1922 struct xhci_td *td = NULL;
d0e96f5a
SS
1923 dma_addr_t event_dma;
1924 struct xhci_segment *event_seg;
1925 union xhci_trb *event_trb;
326b4810 1926 struct urb *urb = NULL;
d0e96f5a 1927 int status = -EINPROGRESS;
8e51adcc 1928 struct urb_priv *urb_priv;
d115b048 1929 struct xhci_ep_ctx *ep_ctx;
c2d7b49f 1930 struct list_head *tmp;
66d1eebc 1931 u32 trb_comp_code;
4422da61 1932 int ret = 0;
c2d7b49f 1933 int td_num = 0;
d0e96f5a 1934
28ccd296 1935 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
82d1009f 1936 xdev = xhci->devs[slot_id];
d0e96f5a
SS
1937 if (!xdev) {
1938 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
1939 return -ENODEV;
1940 }
1941
1942 /* Endpoint ID is 1 based, our index is zero based */
28ccd296 1943 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
63a0d9ab 1944 ep = &xdev->eps[ep_index];
28ccd296 1945 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
d115b048 1946 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
986a92d4 1947 if (!ep_ring ||
28ccd296
ME
1948 (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
1949 EP_STATE_DISABLED) {
e9df17eb
SS
1950 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
1951 "or incorrect stream ring\n");
d0e96f5a
SS
1952 return -ENODEV;
1953 }
1954
c2d7b49f
AX
1955 /* Count current td numbers if ep->skip is set */
1956 if (ep->skip) {
1957 list_for_each(tmp, &ep_ring->td_list)
1958 td_num++;
1959 }
1960
28ccd296
ME
1961 event_dma = le64_to_cpu(event->buffer);
1962 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
986a92d4 1963 /* Look for common error cases */
66d1eebc 1964 switch (trb_comp_code) {
b10de142
SS
1965 /* Skip codes that require special handling depending on
1966 * transfer type
1967 */
1968 case COMP_SUCCESS:
1969 case COMP_SHORT_TX:
1970 break;
ae636747
SS
1971 case COMP_STOP:
1972 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
1973 break;
1974 case COMP_STOP_INVAL:
1975 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
1976 break;
b10de142 1977 case COMP_STALL:
2a9227a5 1978 xhci_dbg(xhci, "Stalled endpoint\n");
63a0d9ab 1979 ep->ep_state |= EP_HALTED;
b10de142
SS
1980 status = -EPIPE;
1981 break;
1982 case COMP_TRB_ERR:
1983 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
1984 status = -EILSEQ;
1985 break;
ec74e403 1986 case COMP_SPLIT_ERR:
b10de142 1987 case COMP_TX_ERR:
2a9227a5 1988 xhci_dbg(xhci, "Transfer error on endpoint\n");
b10de142
SS
1989 status = -EPROTO;
1990 break;
4a73143c 1991 case COMP_BABBLE:
2a9227a5 1992 xhci_dbg(xhci, "Babble error on endpoint\n");
4a73143c
SS
1993 status = -EOVERFLOW;
1994 break;
b10de142
SS
1995 case COMP_DB_ERR:
1996 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
1997 status = -ENOSR;
1998 break;
986a92d4
AX
1999 case COMP_BW_OVER:
2000 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
2001 break;
2002 case COMP_BUFF_OVER:
2003 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
2004 break;
2005 case COMP_UNDERRUN:
2006 /*
2007 * When the Isoch ring is empty, the xHC will generate
2008 * a Ring Overrun Event for IN Isoch endpoint or Ring
2009 * Underrun Event for OUT Isoch endpoint.
2010 */
2011 xhci_dbg(xhci, "underrun event on endpoint\n");
2012 if (!list_empty(&ep_ring->td_list))
2013 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2014 "still with TDs queued?\n",
28ccd296
ME
2015 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2016 ep_index);
986a92d4
AX
2017 goto cleanup;
2018 case COMP_OVERRUN:
2019 xhci_dbg(xhci, "overrun event on endpoint\n");
2020 if (!list_empty(&ep_ring->td_list))
2021 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2022 "still with TDs queued?\n",
28ccd296
ME
2023 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2024 ep_index);
986a92d4 2025 goto cleanup;
f6ba6fe2
AH
2026 case COMP_DEV_ERR:
2027 xhci_warn(xhci, "WARN: detect an incompatible device");
2028 status = -EPROTO;
2029 break;
d18240db
AX
2030 case COMP_MISSED_INT:
2031 /*
2032 * When encounter missed service error, one or more isoc tds
2033 * may be missed by xHC.
2034 * Set skip flag of the ep_ring; Complete the missed tds as
2035 * short transfer when process the ep_ring next time.
2036 */
2037 ep->skip = true;
2038 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
2039 goto cleanup;
b10de142 2040 default:
b45b5069 2041 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
5ad6a529
SS
2042 status = 0;
2043 break;
2044 }
986a92d4
AX
2045 xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
2046 "busted\n");
2047 goto cleanup;
2048 }
2049
d18240db
AX
2050 do {
2051 /* This TRB should be in the TD at the head of this ring's
2052 * TD list.
2053 */
2054 if (list_empty(&ep_ring->td_list)) {
2055 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
2056 "with no TDs queued?\n",
28ccd296
ME
2057 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2058 ep_index);
d18240db 2059 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
f5960b69
ME
2060 (le32_to_cpu(event->flags) &
2061 TRB_TYPE_BITMASK)>>10);
d18240db
AX
2062 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2063 if (ep->skip) {
2064 ep->skip = false;
2065 xhci_dbg(xhci, "td_list is empty while skip "
2066 "flag set. Clear skip flag.\n");
2067 }
2068 ret = 0;
2069 goto cleanup;
2070 }
986a92d4 2071
c2d7b49f
AX
2072 /* We've skipped all the TDs on the ep ring when ep->skip set */
2073 if (ep->skip && td_num == 0) {
2074 ep->skip = false;
2075 xhci_dbg(xhci, "All tds on the ep_ring skipped. "
2076 "Clear skip flag.\n");
2077 ret = 0;
2078 goto cleanup;
2079 }
2080
d18240db 2081 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
c2d7b49f
AX
2082 if (ep->skip)
2083 td_num--;
926008c9 2084
d18240db
AX
2085 /* Is this a TRB in the currently executing TD? */
2086 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
2087 td->last_trb, event_dma);
e1cf486d
AH
2088
2089 /*
2090 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
2091 * is not in the current TD pointed by ep_ring->dequeue because
2092 * that the hardware dequeue pointer still at the previous TRB
2093 * of the current TD. The previous TRB maybe a Link TD or the
2094 * last TRB of the previous TD. The command completion handle
2095 * will take care the rest.
2096 */
2097 if (!event_seg && trb_comp_code == COMP_STOP_INVAL) {
2098 ret = 0;
2099 goto cleanup;
2100 }
2101
926008c9
DT
2102 if (!event_seg) {
2103 if (!ep->skip ||
2104 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
ad808333
SS
2105 /* Some host controllers give a spurious
2106 * successful event after a short transfer.
2107 * Ignore it.
2108 */
2109 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
2110 ep_ring->last_td_was_short) {
2111 ep_ring->last_td_was_short = false;
2112 ret = 0;
2113 goto cleanup;
2114 }
926008c9
DT
2115 /* HC is busted, give up! */
2116 xhci_err(xhci,
2117 "ERROR Transfer event TRB DMA ptr not "
2118 "part of current TD\n");
2119 return -ESHUTDOWN;
2120 }
2121
2122 ret = skip_isoc_td(xhci, td, event, ep, &status);
2123 goto cleanup;
2124 }
ad808333
SS
2125 if (trb_comp_code == COMP_SHORT_TX)
2126 ep_ring->last_td_was_short = true;
2127 else
2128 ep_ring->last_td_was_short = false;
926008c9
DT
2129
2130 if (ep->skip) {
d18240db
AX
2131 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2132 ep->skip = false;
2133 }
678539cf 2134
926008c9
DT
2135 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) /
2136 sizeof(*event_trb)];
2137 /*
2138 * No-op TRB should not trigger interrupts.
2139 * If event_trb is a no-op TRB, it means the
2140 * corresponding TD has been cancelled. Just ignore
2141 * the TD.
2142 */
f5960b69 2143 if (TRB_TYPE_NOOP_LE32(event_trb->generic.field[3])) {
926008c9
DT
2144 xhci_dbg(xhci,
2145 "event_trb is a no-op TRB. Skip it\n");
2146 goto cleanup;
d18240db 2147 }
4422da61 2148
d18240db
AX
2149 /* Now update the urb's actual_length and give back to
2150 * the core
82d1009f 2151 */
d18240db
AX
2152 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2153 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
2154 &status);
04e51901
AX
2155 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2156 ret = process_isoc_td(xhci, td, event_trb, event, ep,
2157 &status);
d18240db
AX
2158 else
2159 ret = process_bulk_intr_td(xhci, td, event_trb, event,
2160 ep, &status);
2161
2162cleanup:
2163 /*
2164 * Do not update event ring dequeue pointer if ep->skip is set.
2165 * Will roll back to continue process missed tds.
2166 */
2167 if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
2168 inc_deq(xhci, xhci->event_ring, true);
d18240db
AX
2169 }
2170
2171 if (ret) {
2172 urb = td->urb;
8e51adcc 2173 urb_priv = urb->hcpriv;
d18240db
AX
2174 /* Leave the TD around for the reset endpoint function
2175 * to use(but only if it's not a control endpoint,
2176 * since we already queued the Set TR dequeue pointer
2177 * command for stalled control endpoints).
2178 */
2179 if (usb_endpoint_xfer_control(&urb->ep->desc) ||
2180 (trb_comp_code != COMP_STALL &&
2181 trb_comp_code != COMP_BABBLE))
8e51adcc 2182 xhci_urb_free_priv(xhci, urb_priv);
d18240db 2183
214f76f7 2184 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
f444ff27
SS
2185 if ((urb->actual_length != urb->transfer_buffer_length &&
2186 (urb->transfer_flags &
2187 URB_SHORT_NOT_OK)) ||
fd984d24
SS
2188 (status != 0 &&
2189 !usb_endpoint_xfer_isoc(&urb->ep->desc)))
f444ff27
SS
2190 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
2191 "expected = %x, status = %d\n",
2192 urb, urb->actual_length,
2193 urb->transfer_buffer_length,
2194 status);
d18240db 2195 spin_unlock(&xhci->lock);
b3df3f9c
SS
2196 /* EHCI, UHCI, and OHCI always unconditionally set the
2197 * urb->status of an isochronous endpoint to 0.
2198 */
2199 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2200 status = 0;
214f76f7 2201 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
d18240db
AX
2202 spin_lock(&xhci->lock);
2203 }
2204
2205 /*
2206 * If ep->skip is set, it means there are missed tds on the
2207 * endpoint ring need to take care of.
2208 * Process them as short transfer until reach the td pointed by
2209 * the event.
2210 */
2211 } while (ep->skip && trb_comp_code != COMP_MISSED_INT);
2212
d0e96f5a
SS
2213 return 0;
2214}
2215
0f2a7930
SS
2216/*
2217 * This function handles all OS-owned events on the event ring. It may drop
2218 * xhci->lock between event processing (e.g. to pass up port status changes).
9dee9a21
ME
2219 * Returns >0 for "possibly more events to process" (caller should call again),
2220 * otherwise 0 if done. In future, <0 returns should indicate error code.
0f2a7930 2221 */
9dee9a21 2222static int xhci_handle_event(struct xhci_hcd *xhci)
7f84eef0
SS
2223{
2224 union xhci_trb *event;
0f2a7930 2225 int update_ptrs = 1;
d0e96f5a 2226 int ret;
7f84eef0
SS
2227
2228 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2229 xhci->error_bitmask |= 1 << 1;
9dee9a21 2230 return 0;
7f84eef0
SS
2231 }
2232
2233 event = xhci->event_ring->dequeue;
2234 /* Does the HC or OS own the TRB? */
28ccd296
ME
2235 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
2236 xhci->event_ring->cycle_state) {
7f84eef0 2237 xhci->error_bitmask |= 1 << 2;
9dee9a21 2238 return 0;
7f84eef0
SS
2239 }
2240
92a3da41
ME
2241 /*
2242 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2243 * speculative reads of the event's flags/data below.
2244 */
2245 rmb();
0f2a7930 2246 /* FIXME: Handle more event types. */
28ccd296 2247 switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
7f84eef0
SS
2248 case TRB_TYPE(TRB_COMPLETION):
2249 handle_cmd_completion(xhci, &event->event_cmd);
2250 break;
0f2a7930
SS
2251 case TRB_TYPE(TRB_PORT_STATUS):
2252 handle_port_status(xhci, event);
2253 update_ptrs = 0;
2254 break;
d0e96f5a
SS
2255 case TRB_TYPE(TRB_TRANSFER):
2256 ret = handle_tx_event(xhci, &event->trans_event);
2257 if (ret < 0)
2258 xhci->error_bitmask |= 1 << 9;
2259 else
2260 update_ptrs = 0;
2261 break;
7f84eef0 2262 default:
28ccd296
ME
2263 if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
2264 TRB_TYPE(48))
0238634d
SS
2265 handle_vendor_event(xhci, event);
2266 else
2267 xhci->error_bitmask |= 1 << 3;
7f84eef0 2268 }
6f5165cf
SS
2269 /* Any of the above functions may drop and re-acquire the lock, so check
2270 * to make sure a watchdog timer didn't mark the host as non-responsive.
2271 */
2272 if (xhci->xhc_state & XHCI_STATE_DYING) {
2273 xhci_dbg(xhci, "xHCI host dying, returning from "
2274 "event handler.\n");
9dee9a21 2275 return 0;
6f5165cf 2276 }
7f84eef0 2277
c06d68b8
SS
2278 if (update_ptrs)
2279 /* Update SW event ring dequeue pointer */
0f2a7930 2280 inc_deq(xhci, xhci->event_ring, true);
c06d68b8 2281
9dee9a21
ME
2282 /* Are there more items on the event ring? Caller will call us again to
2283 * check.
2284 */
2285 return 1;
7f84eef0 2286}
9032cd52
SS
2287
2288/*
2289 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2290 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2291 * indicators of an event TRB error, but we check the status *first* to be safe.
2292 */
2293irqreturn_t xhci_irq(struct usb_hcd *hcd)
2294{
2295 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
c21599a3 2296 u32 status;
9032cd52 2297 union xhci_trb *trb;
bda53145 2298 u64 temp_64;
c06d68b8
SS
2299 union xhci_trb *event_ring_deq;
2300 dma_addr_t deq;
9032cd52
SS
2301
2302 spin_lock(&xhci->lock);
2303 trb = xhci->event_ring->dequeue;
2304 /* Check if the xHC generated the interrupt, or the irq is shared */
27e0dd4d 2305 status = xhci_readl(xhci, &xhci->op_regs->status);
c21599a3 2306 if (status == 0xffffffff)
9032cd52
SS
2307 goto hw_died;
2308
c21599a3 2309 if (!(status & STS_EINT)) {
9032cd52 2310 spin_unlock(&xhci->lock);
9032cd52
SS
2311 return IRQ_NONE;
2312 }
27e0dd4d 2313 if (status & STS_FATAL) {
9032cd52
SS
2314 xhci_warn(xhci, "WARNING: Host System Error\n");
2315 xhci_halt(xhci);
2316hw_died:
9032cd52
SS
2317 spin_unlock(&xhci->lock);
2318 return -ESHUTDOWN;
2319 }
2320
bda53145
SS
2321 /*
2322 * Clear the op reg interrupt status first,
2323 * so we can receive interrupts from other MSI-X interrupters.
2324 * Write 1 to clear the interrupt status.
2325 */
27e0dd4d
SS
2326 status |= STS_EINT;
2327 xhci_writel(xhci, status, &xhci->op_regs->status);
bda53145
SS
2328 /* FIXME when MSI-X is supported and there are multiple vectors */
2329 /* Clear the MSI-X event interrupt status */
2330
c21599a3
SS
2331 if (hcd->irq != -1) {
2332 u32 irq_pending;
2333 /* Acknowledge the PCI interrupt */
2334 irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
2335 irq_pending |= 0x3;
2336 xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
2337 }
bda53145 2338
c06d68b8 2339 if (xhci->xhc_state & XHCI_STATE_DYING) {
bda53145
SS
2340 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2341 "Shouldn't IRQs be disabled?\n");
c06d68b8
SS
2342 /* Clear the event handler busy flag (RW1C);
2343 * the event ring should be empty.
bda53145 2344 */
c06d68b8
SS
2345 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2346 xhci_write_64(xhci, temp_64 | ERST_EHB,
2347 &xhci->ir_set->erst_dequeue);
2348 spin_unlock(&xhci->lock);
2349
2350 return IRQ_HANDLED;
2351 }
2352
2353 event_ring_deq = xhci->event_ring->dequeue;
2354 /* FIXME this should be a delayed service routine
2355 * that clears the EHB.
2356 */
9dee9a21 2357 while (xhci_handle_event(xhci) > 0) {}
bda53145 2358
bda53145 2359 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
c06d68b8
SS
2360 /* If necessary, update the HW's version of the event ring deq ptr. */
2361 if (event_ring_deq != xhci->event_ring->dequeue) {
2362 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2363 xhci->event_ring->dequeue);
2364 if (deq == 0)
2365 xhci_warn(xhci, "WARN something wrong with SW event "
2366 "ring dequeue ptr.\n");
2367 /* Update HC event ring dequeue pointer */
2368 temp_64 &= ERST_PTR_MASK;
2369 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2370 }
2371
2372 /* Clear the event handler busy flag (RW1C); event ring is empty. */
2373 temp_64 |= ERST_EHB;
2374 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2375
9032cd52
SS
2376 spin_unlock(&xhci->lock);
2377
2378 return IRQ_HANDLED;
2379}
2380
2381irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2382{
968b822c 2383 return xhci_irq(hcd);
9032cd52 2384}
7f84eef0 2385
d0e96f5a
SS
2386/**** Endpoint Ring Operations ****/
2387
7f84eef0
SS
2388/*
2389 * Generic function for queueing a TRB on a ring.
2390 * The caller must have checked to make sure there's room on the ring.
6cc30d85
SS
2391 *
2392 * @more_trbs_coming: Will you enqueue more TRBs before calling
2393 * prepare_transfer()?
7f84eef0
SS
2394 */
2395static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
7e393a83 2396 bool consumer, bool more_trbs_coming, bool isoc,
7f84eef0
SS
2397 u32 field1, u32 field2, u32 field3, u32 field4)
2398{
2399 struct xhci_generic_trb *trb;
2400
2401 trb = &ring->enqueue->generic;
28ccd296
ME
2402 trb->field[0] = cpu_to_le32(field1);
2403 trb->field[1] = cpu_to_le32(field2);
2404 trb->field[2] = cpu_to_le32(field3);
2405 trb->field[3] = cpu_to_le32(field4);
7e393a83 2406 inc_enq(xhci, ring, consumer, more_trbs_coming, isoc);
7f84eef0
SS
2407}
2408
d0e96f5a
SS
2409/*
2410 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2411 * FIXME allocate segments if the ring is full.
2412 */
2413static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
7e393a83 2414 u32 ep_state, unsigned int num_trbs, bool isoc, gfp_t mem_flags)
d0e96f5a
SS
2415{
2416 /* Make sure the endpoint has been added to xHC schedule */
d0e96f5a
SS
2417 switch (ep_state) {
2418 case EP_STATE_DISABLED:
2419 /*
2420 * USB core changed config/interfaces without notifying us,
2421 * or hardware is reporting the wrong state.
2422 */
2423 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2424 return -ENOENT;
d0e96f5a 2425 case EP_STATE_ERROR:
c92bcfa7 2426 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
d0e96f5a
SS
2427 /* FIXME event handling code for error needs to clear it */
2428 /* XXX not sure if this should be -ENOENT or not */
2429 return -EINVAL;
c92bcfa7
SS
2430 case EP_STATE_HALTED:
2431 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
d0e96f5a
SS
2432 case EP_STATE_STOPPED:
2433 case EP_STATE_RUNNING:
2434 break;
2435 default:
2436 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2437 /*
2438 * FIXME issue Configure Endpoint command to try to get the HC
2439 * back into a known state.
2440 */
2441 return -EINVAL;
2442 }
2443 if (!room_on_ring(xhci, ep_ring, num_trbs)) {
2444 /* FIXME allocate more room */
2445 xhci_err(xhci, "ERROR no room on ep ring\n");
2446 return -ENOMEM;
2447 }
6c12db90
JY
2448
2449 if (enqueue_is_link_trb(ep_ring)) {
2450 struct xhci_ring *ring = ep_ring;
2451 union xhci_trb *next;
6c12db90 2452
6c12db90
JY
2453 next = ring->enqueue;
2454
2455 while (last_trb(xhci, ring, ring->enq_seg, next)) {
7e393a83
AX
2456 /* If we're not dealing with 0.95 hardware or isoc rings
2457 * on AMD 0.96 host, clear the chain bit.
6c12db90 2458 */
7e393a83
AX
2459 if (!xhci_link_trb_quirk(xhci) && !(isoc &&
2460 (xhci->quirks & XHCI_AMD_0x96_HOST)))
28ccd296 2461 next->link.control &= cpu_to_le32(~TRB_CHAIN);
6c12db90 2462 else
28ccd296 2463 next->link.control |= cpu_to_le32(TRB_CHAIN);
6c12db90
JY
2464
2465 wmb();
f5960b69 2466 next->link.control ^= cpu_to_le32(TRB_CYCLE);
6c12db90
JY
2467
2468 /* Toggle the cycle bit after the last ring segment. */
2469 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
2470 ring->cycle_state = (ring->cycle_state ? 0 : 1);
6c12db90
JY
2471 }
2472 ring->enq_seg = ring->enq_seg->next;
2473 ring->enqueue = ring->enq_seg->trbs;
2474 next = ring->enqueue;
2475 }
2476 }
2477
d0e96f5a
SS
2478 return 0;
2479}
2480
23e3be11 2481static int prepare_transfer(struct xhci_hcd *xhci,
d0e96f5a
SS
2482 struct xhci_virt_device *xdev,
2483 unsigned int ep_index,
e9df17eb 2484 unsigned int stream_id,
d0e96f5a
SS
2485 unsigned int num_trbs,
2486 struct urb *urb,
8e51adcc 2487 unsigned int td_index,
7e393a83 2488 bool isoc,
d0e96f5a
SS
2489 gfp_t mem_flags)
2490{
2491 int ret;
8e51adcc
AX
2492 struct urb_priv *urb_priv;
2493 struct xhci_td *td;
e9df17eb 2494 struct xhci_ring *ep_ring;
d115b048 2495 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
e9df17eb
SS
2496
2497 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2498 if (!ep_ring) {
2499 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2500 stream_id);
2501 return -EINVAL;
2502 }
2503
2504 ret = prepare_ring(xhci, ep_ring,
28ccd296 2505 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
7e393a83 2506 num_trbs, isoc, mem_flags);
d0e96f5a
SS
2507 if (ret)
2508 return ret;
d0e96f5a 2509
8e51adcc
AX
2510 urb_priv = urb->hcpriv;
2511 td = urb_priv->td[td_index];
2512
2513 INIT_LIST_HEAD(&td->td_list);
2514 INIT_LIST_HEAD(&td->cancelled_td_list);
2515
2516 if (td_index == 0) {
214f76f7 2517 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
d13565c1 2518 if (unlikely(ret))
8e51adcc 2519 return ret;
d0e96f5a
SS
2520 }
2521
8e51adcc 2522 td->urb = urb;
d0e96f5a 2523 /* Add this TD to the tail of the endpoint ring's TD list */
8e51adcc
AX
2524 list_add_tail(&td->td_list, &ep_ring->td_list);
2525 td->start_seg = ep_ring->enq_seg;
2526 td->first_trb = ep_ring->enqueue;
2527
2528 urb_priv->td[td_index] = td;
d0e96f5a
SS
2529
2530 return 0;
2531}
2532
23e3be11 2533static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
8a96c052
SS
2534{
2535 int num_sgs, num_trbs, running_total, temp, i;
2536 struct scatterlist *sg;
2537
2538 sg = NULL;
bc677d5b 2539 num_sgs = urb->num_mapped_sgs;
8a96c052
SS
2540 temp = urb->transfer_buffer_length;
2541
8a96c052 2542 num_trbs = 0;
910f8d0c 2543 for_each_sg(urb->sg, sg, num_sgs, i) {
8a96c052
SS
2544 unsigned int len = sg_dma_len(sg);
2545
2546 /* Scatter gather list entries may cross 64KB boundaries */
2547 running_total = TRB_MAX_BUFF_SIZE -
a2490187 2548 (sg_dma_address(sg) & (TRB_MAX_BUFF_SIZE - 1));
5807795b 2549 running_total &= TRB_MAX_BUFF_SIZE - 1;
8a96c052
SS
2550 if (running_total != 0)
2551 num_trbs++;
2552
2553 /* How many more 64KB chunks to transfer, how many more TRBs? */
bcd2fde0 2554 while (running_total < sg_dma_len(sg) && running_total < temp) {
8a96c052
SS
2555 num_trbs++;
2556 running_total += TRB_MAX_BUFF_SIZE;
2557 }
8a96c052
SS
2558 len = min_t(int, len, temp);
2559 temp -= len;
2560 if (temp == 0)
2561 break;
2562 }
8a96c052
SS
2563 return num_trbs;
2564}
2565
23e3be11 2566static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
8a96c052
SS
2567{
2568 if (num_trbs != 0)
a2490187 2569 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
8a96c052
SS
2570 "TRBs, %d left\n", __func__,
2571 urb->ep->desc.bEndpointAddress, num_trbs);
2572 if (running_total != urb->transfer_buffer_length)
a2490187 2573 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
8a96c052
SS
2574 "queued %#x (%d), asked for %#x (%d)\n",
2575 __func__,
2576 urb->ep->desc.bEndpointAddress,
2577 running_total, running_total,
2578 urb->transfer_buffer_length,
2579 urb->transfer_buffer_length);
2580}
2581
23e3be11 2582static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
e9df17eb 2583 unsigned int ep_index, unsigned int stream_id, int start_cycle,
e1eab2e0 2584 struct xhci_generic_trb *start_trb)
8a96c052 2585{
8a96c052
SS
2586 /*
2587 * Pass all the TRBs to the hardware at once and make sure this write
2588 * isn't reordered.
2589 */
2590 wmb();
50f7b52a 2591 if (start_cycle)
28ccd296 2592 start_trb->field[3] |= cpu_to_le32(start_cycle);
50f7b52a 2593 else
28ccd296 2594 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
be88fe4f 2595 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
8a96c052
SS
2596}
2597
624defa1
SS
2598/*
2599 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
2600 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
2601 * (comprised of sg list entries) can take several service intervals to
2602 * transmit.
2603 */
2604int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2605 struct urb *urb, int slot_id, unsigned int ep_index)
2606{
2607 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
2608 xhci->devs[slot_id]->out_ctx, ep_index);
2609 int xhci_interval;
2610 int ep_interval;
2611
28ccd296 2612 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
624defa1
SS
2613 ep_interval = urb->interval;
2614 /* Convert to microframes */
2615 if (urb->dev->speed == USB_SPEED_LOW ||
2616 urb->dev->speed == USB_SPEED_FULL)
2617 ep_interval *= 8;
2618 /* FIXME change this to a warning and a suggestion to use the new API
2619 * to set the polling interval (once the API is added).
2620 */
2621 if (xhci_interval != ep_interval) {
7961acd7 2622 if (printk_ratelimit())
624defa1
SS
2623 dev_dbg(&urb->dev->dev, "Driver uses different interval"
2624 " (%d microframe%s) than xHCI "
2625 "(%d microframe%s)\n",
2626 ep_interval,
2627 ep_interval == 1 ? "" : "s",
2628 xhci_interval,
2629 xhci_interval == 1 ? "" : "s");
2630 urb->interval = xhci_interval;
2631 /* Convert back to frames for LS/FS devices */
2632 if (urb->dev->speed == USB_SPEED_LOW ||
2633 urb->dev->speed == USB_SPEED_FULL)
2634 urb->interval /= 8;
2635 }
2636 return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
2637}
2638
04dd950d
SS
2639/*
2640 * The TD size is the number of bytes remaining in the TD (including this TRB),
2641 * right shifted by 10.
2642 * It must fit in bits 21:17, so it can't be bigger than 31.
2643 */
2644static u32 xhci_td_remainder(unsigned int remainder)
2645{
2646 u32 max = (1 << (21 - 17 + 1)) - 1;
2647
2648 if ((remainder >> 10) >= max)
2649 return max << 17;
2650 else
2651 return (remainder >> 10) << 17;
2652}
2653
4da6e6f2
SS
2654/*
2655 * For xHCI 1.0 host controllers, TD size is the number of packets remaining in
2656 * the TD (*not* including this TRB).
2657 *
2658 * Total TD packet count = total_packet_count =
2659 * roundup(TD size in bytes / wMaxPacketSize)
2660 *
2661 * Packets transferred up to and including this TRB = packets_transferred =
2662 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
2663 *
2664 * TD size = total_packet_count - packets_transferred
2665 *
2666 * It must fit in bits 21:17, so it can't be bigger than 31.
2667 */
2668
2669static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
2670 unsigned int total_packet_count, struct urb *urb)
2671{
2672 int packets_transferred;
2673
48df4a6f
SS
2674 /* One TRB with a zero-length data packet. */
2675 if (running_total == 0 && trb_buff_len == 0)
2676 return 0;
2677
4da6e6f2
SS
2678 /* All the TRB queueing functions don't count the current TRB in
2679 * running_total.
2680 */
2681 packets_transferred = (running_total + trb_buff_len) /
29cc8897 2682 usb_endpoint_maxp(&urb->ep->desc);
4da6e6f2
SS
2683
2684 return xhci_td_remainder(total_packet_count - packets_transferred);
2685}
2686
23e3be11 2687static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
8a96c052
SS
2688 struct urb *urb, int slot_id, unsigned int ep_index)
2689{
2690 struct xhci_ring *ep_ring;
2691 unsigned int num_trbs;
8e51adcc 2692 struct urb_priv *urb_priv;
8a96c052
SS
2693 struct xhci_td *td;
2694 struct scatterlist *sg;
2695 int num_sgs;
2696 int trb_buff_len, this_sg_len, running_total;
4da6e6f2 2697 unsigned int total_packet_count;
8a96c052
SS
2698 bool first_trb;
2699 u64 addr;
6cc30d85 2700 bool more_trbs_coming;
8a96c052
SS
2701
2702 struct xhci_generic_trb *start_trb;
2703 int start_cycle;
2704
e9df17eb
SS
2705 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2706 if (!ep_ring)
2707 return -EINVAL;
2708
8a96c052 2709 num_trbs = count_sg_trbs_needed(xhci, urb);
bc677d5b 2710 num_sgs = urb->num_mapped_sgs;
4da6e6f2 2711 total_packet_count = roundup(urb->transfer_buffer_length,
29cc8897 2712 usb_endpoint_maxp(&urb->ep->desc));
8a96c052 2713
23e3be11 2714 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
e9df17eb 2715 ep_index, urb->stream_id,
7e393a83 2716 num_trbs, urb, 0, false, mem_flags);
8a96c052
SS
2717 if (trb_buff_len < 0)
2718 return trb_buff_len;
8e51adcc
AX
2719
2720 urb_priv = urb->hcpriv;
2721 td = urb_priv->td[0];
2722
8a96c052
SS
2723 /*
2724 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2725 * until we've finished creating all the other TRBs. The ring's cycle
2726 * state may change as we enqueue the other TRBs, so save it too.
2727 */
2728 start_trb = &ep_ring->enqueue->generic;
2729 start_cycle = ep_ring->cycle_state;
2730
2731 running_total = 0;
2732 /*
2733 * How much data is in the first TRB?
2734 *
2735 * There are three forces at work for TRB buffer pointers and lengths:
2736 * 1. We don't want to walk off the end of this sg-list entry buffer.
2737 * 2. The transfer length that the driver requested may be smaller than
2738 * the amount of memory allocated for this scatter-gather list.
2739 * 3. TRBs buffers can't cross 64KB boundaries.
2740 */
910f8d0c 2741 sg = urb->sg;
8a96c052
SS
2742 addr = (u64) sg_dma_address(sg);
2743 this_sg_len = sg_dma_len(sg);
a2490187 2744 trb_buff_len = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1));
8a96c052
SS
2745 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2746 if (trb_buff_len > urb->transfer_buffer_length)
2747 trb_buff_len = urb->transfer_buffer_length;
8a96c052
SS
2748
2749 first_trb = true;
2750 /* Queue the first TRB, even if it's zero-length */
2751 do {
2752 u32 field = 0;
f9dc68fe 2753 u32 length_field = 0;
04dd950d 2754 u32 remainder = 0;
8a96c052
SS
2755
2756 /* Don't change the cycle bit of the first TRB until later */
50f7b52a 2757 if (first_trb) {
8a96c052 2758 first_trb = false;
50f7b52a
AX
2759 if (start_cycle == 0)
2760 field |= 0x1;
2761 } else
8a96c052
SS
2762 field |= ep_ring->cycle_state;
2763
2764 /* Chain all the TRBs together; clear the chain bit in the last
2765 * TRB to indicate it's the last TRB in the chain.
2766 */
2767 if (num_trbs > 1) {
2768 field |= TRB_CHAIN;
2769 } else {
2770 /* FIXME - add check for ZERO_PACKET flag before this */
2771 td->last_trb = ep_ring->enqueue;
2772 field |= TRB_IOC;
2773 }
af8b9e63
SS
2774
2775 /* Only set interrupt on short packet for IN endpoints */
2776 if (usb_urb_dir_in(urb))
2777 field |= TRB_ISP;
2778
8a96c052 2779 if (TRB_MAX_BUFF_SIZE -
a2490187 2780 (addr & (TRB_MAX_BUFF_SIZE - 1)) < trb_buff_len) {
8a96c052
SS
2781 xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
2782 xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
2783 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2784 (unsigned int) addr + trb_buff_len);
2785 }
4da6e6f2
SS
2786
2787 /* Set the TRB length, TD size, and interrupter fields. */
2788 if (xhci->hci_version < 0x100) {
2789 remainder = xhci_td_remainder(
2790 urb->transfer_buffer_length -
2791 running_total);
2792 } else {
2793 remainder = xhci_v1_0_td_remainder(running_total,
2794 trb_buff_len, total_packet_count, urb);
2795 }
f9dc68fe 2796 length_field = TRB_LEN(trb_buff_len) |
04dd950d 2797 remainder |
f9dc68fe 2798 TRB_INTR_TARGET(0);
4da6e6f2 2799
6cc30d85
SS
2800 if (num_trbs > 1)
2801 more_trbs_coming = true;
2802 else
2803 more_trbs_coming = false;
7e393a83 2804 queue_trb(xhci, ep_ring, false, more_trbs_coming, false,
8e595a5d
SS
2805 lower_32_bits(addr),
2806 upper_32_bits(addr),
f9dc68fe 2807 length_field,
af8b9e63 2808 field | TRB_TYPE(TRB_NORMAL));
8a96c052
SS
2809 --num_trbs;
2810 running_total += trb_buff_len;
2811
2812 /* Calculate length for next transfer --
2813 * Are we done queueing all the TRBs for this sg entry?
2814 */
2815 this_sg_len -= trb_buff_len;
2816 if (this_sg_len == 0) {
2817 --num_sgs;
2818 if (num_sgs == 0)
2819 break;
2820 sg = sg_next(sg);
2821 addr = (u64) sg_dma_address(sg);
2822 this_sg_len = sg_dma_len(sg);
2823 } else {
2824 addr += trb_buff_len;
2825 }
2826
2827 trb_buff_len = TRB_MAX_BUFF_SIZE -
a2490187 2828 (addr & (TRB_MAX_BUFF_SIZE - 1));
8a96c052
SS
2829 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2830 if (running_total + trb_buff_len > urb->transfer_buffer_length)
2831 trb_buff_len =
2832 urb->transfer_buffer_length - running_total;
2833 } while (running_total < urb->transfer_buffer_length);
2834
2835 check_trb_math(urb, num_trbs, running_total);
e9df17eb 2836 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
e1eab2e0 2837 start_cycle, start_trb);
8a96c052
SS
2838 return 0;
2839}
2840
b10de142 2841/* This is very similar to what ehci-q.c qtd_fill() does */
23e3be11 2842int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
b10de142
SS
2843 struct urb *urb, int slot_id, unsigned int ep_index)
2844{
2845 struct xhci_ring *ep_ring;
8e51adcc 2846 struct urb_priv *urb_priv;
b10de142
SS
2847 struct xhci_td *td;
2848 int num_trbs;
2849 struct xhci_generic_trb *start_trb;
2850 bool first_trb;
6cc30d85 2851 bool more_trbs_coming;
b10de142 2852 int start_cycle;
f9dc68fe 2853 u32 field, length_field;
b10de142
SS
2854
2855 int running_total, trb_buff_len, ret;
4da6e6f2 2856 unsigned int total_packet_count;
b10de142
SS
2857 u64 addr;
2858
ff9c895f 2859 if (urb->num_sgs)
8a96c052
SS
2860 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
2861
e9df17eb
SS
2862 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2863 if (!ep_ring)
2864 return -EINVAL;
b10de142
SS
2865
2866 num_trbs = 0;
2867 /* How much data is (potentially) left before the 64KB boundary? */
2868 running_total = TRB_MAX_BUFF_SIZE -
a2490187 2869 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
5807795b 2870 running_total &= TRB_MAX_BUFF_SIZE - 1;
b10de142
SS
2871
2872 /* If there's some data on this 64KB chunk, or we have to send a
2873 * zero-length transfer, we need at least one TRB
2874 */
2875 if (running_total != 0 || urb->transfer_buffer_length == 0)
2876 num_trbs++;
2877 /* How many more 64KB chunks to transfer, how many more TRBs? */
2878 while (running_total < urb->transfer_buffer_length) {
2879 num_trbs++;
2880 running_total += TRB_MAX_BUFF_SIZE;
2881 }
2882 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
2883
e9df17eb
SS
2884 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2885 ep_index, urb->stream_id,
7e393a83 2886 num_trbs, urb, 0, false, mem_flags);
b10de142
SS
2887 if (ret < 0)
2888 return ret;
2889
8e51adcc
AX
2890 urb_priv = urb->hcpriv;
2891 td = urb_priv->td[0];
2892
b10de142
SS
2893 /*
2894 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2895 * until we've finished creating all the other TRBs. The ring's cycle
2896 * state may change as we enqueue the other TRBs, so save it too.
2897 */
2898 start_trb = &ep_ring->enqueue->generic;
2899 start_cycle = ep_ring->cycle_state;
2900
2901 running_total = 0;
4da6e6f2 2902 total_packet_count = roundup(urb->transfer_buffer_length,
29cc8897 2903 usb_endpoint_maxp(&urb->ep->desc));
b10de142
SS
2904 /* How much data is in the first TRB? */
2905 addr = (u64) urb->transfer_dma;
2906 trb_buff_len = TRB_MAX_BUFF_SIZE -
a2490187
PZ
2907 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
2908 if (trb_buff_len > urb->transfer_buffer_length)
b10de142
SS
2909 trb_buff_len = urb->transfer_buffer_length;
2910
2911 first_trb = true;
2912
2913 /* Queue the first TRB, even if it's zero-length */
2914 do {
04dd950d 2915 u32 remainder = 0;
b10de142
SS
2916 field = 0;
2917
2918 /* Don't change the cycle bit of the first TRB until later */
50f7b52a 2919 if (first_trb) {
b10de142 2920 first_trb = false;
50f7b52a
AX
2921 if (start_cycle == 0)
2922 field |= 0x1;
2923 } else
b10de142
SS
2924 field |= ep_ring->cycle_state;
2925
2926 /* Chain all the TRBs together; clear the chain bit in the last
2927 * TRB to indicate it's the last TRB in the chain.
2928 */
2929 if (num_trbs > 1) {
2930 field |= TRB_CHAIN;
2931 } else {
2932 /* FIXME - add check for ZERO_PACKET flag before this */
2933 td->last_trb = ep_ring->enqueue;
2934 field |= TRB_IOC;
2935 }
af8b9e63
SS
2936
2937 /* Only set interrupt on short packet for IN endpoints */
2938 if (usb_urb_dir_in(urb))
2939 field |= TRB_ISP;
2940
4da6e6f2
SS
2941 /* Set the TRB length, TD size, and interrupter fields. */
2942 if (xhci->hci_version < 0x100) {
2943 remainder = xhci_td_remainder(
2944 urb->transfer_buffer_length -
2945 running_total);
2946 } else {
2947 remainder = xhci_v1_0_td_remainder(running_total,
2948 trb_buff_len, total_packet_count, urb);
2949 }
f9dc68fe 2950 length_field = TRB_LEN(trb_buff_len) |
04dd950d 2951 remainder |
f9dc68fe 2952 TRB_INTR_TARGET(0);
4da6e6f2 2953
6cc30d85
SS
2954 if (num_trbs > 1)
2955 more_trbs_coming = true;
2956 else
2957 more_trbs_coming = false;
7e393a83 2958 queue_trb(xhci, ep_ring, false, more_trbs_coming, false,
8e595a5d
SS
2959 lower_32_bits(addr),
2960 upper_32_bits(addr),
f9dc68fe 2961 length_field,
af8b9e63 2962 field | TRB_TYPE(TRB_NORMAL));
b10de142
SS
2963 --num_trbs;
2964 running_total += trb_buff_len;
2965
2966 /* Calculate length for next transfer */
2967 addr += trb_buff_len;
2968 trb_buff_len = urb->transfer_buffer_length - running_total;
2969 if (trb_buff_len > TRB_MAX_BUFF_SIZE)
2970 trb_buff_len = TRB_MAX_BUFF_SIZE;
2971 } while (running_total < urb->transfer_buffer_length);
2972
8a96c052 2973 check_trb_math(urb, num_trbs, running_total);
e9df17eb 2974 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
e1eab2e0 2975 start_cycle, start_trb);
b10de142
SS
2976 return 0;
2977}
2978
d0e96f5a 2979/* Caller must have locked xhci->lock */
23e3be11 2980int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
d0e96f5a
SS
2981 struct urb *urb, int slot_id, unsigned int ep_index)
2982{
2983 struct xhci_ring *ep_ring;
2984 int num_trbs;
2985 int ret;
2986 struct usb_ctrlrequest *setup;
2987 struct xhci_generic_trb *start_trb;
2988 int start_cycle;
f9dc68fe 2989 u32 field, length_field;
8e51adcc 2990 struct urb_priv *urb_priv;
d0e96f5a
SS
2991 struct xhci_td *td;
2992
e9df17eb
SS
2993 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2994 if (!ep_ring)
2995 return -EINVAL;
d0e96f5a
SS
2996
2997 /*
2998 * Need to copy setup packet into setup TRB, so we can't use the setup
2999 * DMA address.
3000 */
3001 if (!urb->setup_packet)
3002 return -EINVAL;
3003
d0e96f5a
SS
3004 /* 1 TRB for setup, 1 for status */
3005 num_trbs = 2;
3006 /*
3007 * Don't need to check if we need additional event data and normal TRBs,
3008 * since data in control transfers will never get bigger than 16MB
3009 * XXX: can we get a buffer that crosses 64KB boundaries?
3010 */
3011 if (urb->transfer_buffer_length > 0)
3012 num_trbs++;
e9df17eb
SS
3013 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3014 ep_index, urb->stream_id,
7e393a83 3015 num_trbs, urb, 0, false, mem_flags);
d0e96f5a
SS
3016 if (ret < 0)
3017 return ret;
3018
8e51adcc
AX
3019 urb_priv = urb->hcpriv;
3020 td = urb_priv->td[0];
3021
d0e96f5a
SS
3022 /*
3023 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3024 * until we've finished creating all the other TRBs. The ring's cycle
3025 * state may change as we enqueue the other TRBs, so save it too.
3026 */
3027 start_trb = &ep_ring->enqueue->generic;
3028 start_cycle = ep_ring->cycle_state;
3029
3030 /* Queue setup TRB - see section 6.4.1.2.1 */
3031 /* FIXME better way to translate setup_packet into two u32 fields? */
3032 setup = (struct usb_ctrlrequest *) urb->setup_packet;
50f7b52a
AX
3033 field = 0;
3034 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3035 if (start_cycle == 0)
3036 field |= 0x1;
b83cdc8f
AX
3037
3038 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
3039 if (xhci->hci_version == 0x100) {
3040 if (urb->transfer_buffer_length > 0) {
3041 if (setup->bRequestType & USB_DIR_IN)
3042 field |= TRB_TX_TYPE(TRB_DATA_IN);
3043 else
3044 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3045 }
3046 }
3047
7e393a83 3048 queue_trb(xhci, ep_ring, false, true, false,
28ccd296
ME
3049 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3050 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3051 TRB_LEN(8) | TRB_INTR_TARGET(0),
3052 /* Immediate data in pointer */
3053 field);
d0e96f5a
SS
3054
3055 /* If there's data, queue data TRBs */
af8b9e63
SS
3056 /* Only set interrupt on short packet for IN endpoints */
3057 if (usb_urb_dir_in(urb))
3058 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3059 else
3060 field = TRB_TYPE(TRB_DATA);
3061
f9dc68fe 3062 length_field = TRB_LEN(urb->transfer_buffer_length) |
04dd950d 3063 xhci_td_remainder(urb->transfer_buffer_length) |
f9dc68fe 3064 TRB_INTR_TARGET(0);
d0e96f5a
SS
3065 if (urb->transfer_buffer_length > 0) {
3066 if (setup->bRequestType & USB_DIR_IN)
3067 field |= TRB_DIR_IN;
7e393a83 3068 queue_trb(xhci, ep_ring, false, true, false,
d0e96f5a
SS
3069 lower_32_bits(urb->transfer_dma),
3070 upper_32_bits(urb->transfer_dma),
f9dc68fe 3071 length_field,
af8b9e63 3072 field | ep_ring->cycle_state);
d0e96f5a
SS
3073 }
3074
3075 /* Save the DMA address of the last TRB in the TD */
3076 td->last_trb = ep_ring->enqueue;
3077
3078 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3079 /* If the device sent data, the status stage is an OUT transfer */
3080 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3081 field = 0;
3082 else
3083 field = TRB_DIR_IN;
7e393a83 3084 queue_trb(xhci, ep_ring, false, false, false,
d0e96f5a
SS
3085 0,
3086 0,
3087 TRB_INTR_TARGET(0),
3088 /* Event on completion */
3089 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3090
e9df17eb 3091 giveback_first_trb(xhci, slot_id, ep_index, 0,
e1eab2e0 3092 start_cycle, start_trb);
d0e96f5a
SS
3093 return 0;
3094}
3095
04e51901
AX
3096static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
3097 struct urb *urb, int i)
3098{
3099 int num_trbs = 0;
48df4a6f 3100 u64 addr, td_len;
04e51901
AX
3101
3102 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
3103 td_len = urb->iso_frame_desc[i].length;
3104
48df4a6f
SS
3105 num_trbs = DIV_ROUND_UP(td_len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
3106 TRB_MAX_BUFF_SIZE);
3107 if (num_trbs == 0)
04e51901 3108 num_trbs++;
04e51901
AX
3109
3110 return num_trbs;
3111}
3112
5cd43e33
SS
3113/*
3114 * The transfer burst count field of the isochronous TRB defines the number of
3115 * bursts that are required to move all packets in this TD. Only SuperSpeed
3116 * devices can burst up to bMaxBurst number of packets per service interval.
3117 * This field is zero based, meaning a value of zero in the field means one
3118 * burst. Basically, for everything but SuperSpeed devices, this field will be
3119 * zero. Only xHCI 1.0 host controllers support this field.
3120 */
3121static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
3122 struct usb_device *udev,
3123 struct urb *urb, unsigned int total_packet_count)
3124{
3125 unsigned int max_burst;
3126
3127 if (xhci->hci_version < 0x100 || udev->speed != USB_SPEED_SUPER)
3128 return 0;
3129
3130 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3131 return roundup(total_packet_count, max_burst + 1) - 1;
3132}
3133
b61d378f
SS
3134/*
3135 * Returns the number of packets in the last "burst" of packets. This field is
3136 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3137 * the last burst packet count is equal to the total number of packets in the
3138 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3139 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3140 * contain 1 to (bMaxBurst + 1) packets.
3141 */
3142static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
3143 struct usb_device *udev,
3144 struct urb *urb, unsigned int total_packet_count)
3145{
3146 unsigned int max_burst;
3147 unsigned int residue;
3148
3149 if (xhci->hci_version < 0x100)
3150 return 0;
3151
3152 switch (udev->speed) {
3153 case USB_SPEED_SUPER:
3154 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3155 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3156 residue = total_packet_count % (max_burst + 1);
3157 /* If residue is zero, the last burst contains (max_burst + 1)
3158 * number of packets, but the TLBPC field is zero-based.
3159 */
3160 if (residue == 0)
3161 return max_burst;
3162 return residue - 1;
3163 default:
3164 if (total_packet_count == 0)
3165 return 0;
3166 return total_packet_count - 1;
3167 }
3168}
3169
04e51901
AX
3170/* This is for isoc transfer */
3171static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3172 struct urb *urb, int slot_id, unsigned int ep_index)
3173{
3174 struct xhci_ring *ep_ring;
3175 struct urb_priv *urb_priv;
3176 struct xhci_td *td;
3177 int num_tds, trbs_per_td;
3178 struct xhci_generic_trb *start_trb;
3179 bool first_trb;
3180 int start_cycle;
3181 u32 field, length_field;
3182 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3183 u64 start_addr, addr;
3184 int i, j;
47cbf692 3185 bool more_trbs_coming;
04e51901
AX
3186
3187 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3188
3189 num_tds = urb->number_of_packets;
3190 if (num_tds < 1) {
3191 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3192 return -EINVAL;
3193 }
3194
04e51901
AX
3195 start_addr = (u64) urb->transfer_dma;
3196 start_trb = &ep_ring->enqueue->generic;
3197 start_cycle = ep_ring->cycle_state;
3198
522989a2 3199 urb_priv = urb->hcpriv;
04e51901
AX
3200 /* Queue the first TRB, even if it's zero-length */
3201 for (i = 0; i < num_tds; i++) {
4da6e6f2 3202 unsigned int total_packet_count;
5cd43e33 3203 unsigned int burst_count;
b61d378f 3204 unsigned int residue;
04e51901 3205
4da6e6f2 3206 first_trb = true;
04e51901
AX
3207 running_total = 0;
3208 addr = start_addr + urb->iso_frame_desc[i].offset;
3209 td_len = urb->iso_frame_desc[i].length;
3210 td_remain_len = td_len;
4da6e6f2 3211 total_packet_count = roundup(td_len,
29cc8897 3212 usb_endpoint_maxp(&urb->ep->desc));
48df4a6f
SS
3213 /* A zero-length transfer still involves at least one packet. */
3214 if (total_packet_count == 0)
3215 total_packet_count++;
5cd43e33
SS
3216 burst_count = xhci_get_burst_count(xhci, urb->dev, urb,
3217 total_packet_count);
b61d378f
SS
3218 residue = xhci_get_last_burst_packet_count(xhci,
3219 urb->dev, urb, total_packet_count);
04e51901
AX
3220
3221 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
3222
3223 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
7e393a83
AX
3224 urb->stream_id, trbs_per_td, urb, i, true,
3225 mem_flags);
522989a2
SS
3226 if (ret < 0) {
3227 if (i == 0)
3228 return ret;
3229 goto cleanup;
3230 }
04e51901 3231
04e51901 3232 td = urb_priv->td[i];
04e51901
AX
3233 for (j = 0; j < trbs_per_td; j++) {
3234 u32 remainder = 0;
b61d378f 3235 field = TRB_TBC(burst_count) | TRB_TLBPC(residue);
04e51901
AX
3236
3237 if (first_trb) {
3238 /* Queue the isoc TRB */
3239 field |= TRB_TYPE(TRB_ISOC);
3240 /* Assume URB_ISO_ASAP is set */
3241 field |= TRB_SIA;
50f7b52a
AX
3242 if (i == 0) {
3243 if (start_cycle == 0)
3244 field |= 0x1;
3245 } else
04e51901
AX
3246 field |= ep_ring->cycle_state;
3247 first_trb = false;
3248 } else {
3249 /* Queue other normal TRBs */
3250 field |= TRB_TYPE(TRB_NORMAL);
3251 field |= ep_ring->cycle_state;
3252 }
3253
af8b9e63
SS
3254 /* Only set interrupt on short packet for IN EPs */
3255 if (usb_urb_dir_in(urb))
3256 field |= TRB_ISP;
3257
04e51901
AX
3258 /* Chain all the TRBs together; clear the chain bit in
3259 * the last TRB to indicate it's the last TRB in the
3260 * chain.
3261 */
3262 if (j < trbs_per_td - 1) {
3263 field |= TRB_CHAIN;
47cbf692 3264 more_trbs_coming = true;
04e51901
AX
3265 } else {
3266 td->last_trb = ep_ring->enqueue;
3267 field |= TRB_IOC;
ad106f29
AX
3268 if (xhci->hci_version == 0x100) {
3269 /* Set BEI bit except for the last td */
3270 if (i < num_tds - 1)
3271 field |= TRB_BEI;
3272 }
47cbf692 3273 more_trbs_coming = false;
04e51901
AX
3274 }
3275
3276 /* Calculate TRB length */
3277 trb_buff_len = TRB_MAX_BUFF_SIZE -
3278 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
3279 if (trb_buff_len > td_remain_len)
3280 trb_buff_len = td_remain_len;
3281
4da6e6f2
SS
3282 /* Set the TRB length, TD size, & interrupter fields. */
3283 if (xhci->hci_version < 0x100) {
3284 remainder = xhci_td_remainder(
3285 td_len - running_total);
3286 } else {
3287 remainder = xhci_v1_0_td_remainder(
3288 running_total, trb_buff_len,
3289 total_packet_count, urb);
3290 }
04e51901
AX
3291 length_field = TRB_LEN(trb_buff_len) |
3292 remainder |
3293 TRB_INTR_TARGET(0);
4da6e6f2 3294
7e393a83 3295 queue_trb(xhci, ep_ring, false, more_trbs_coming, true,
04e51901
AX
3296 lower_32_bits(addr),
3297 upper_32_bits(addr),
3298 length_field,
af8b9e63 3299 field);
04e51901
AX
3300 running_total += trb_buff_len;
3301
3302 addr += trb_buff_len;
3303 td_remain_len -= trb_buff_len;
3304 }
3305
3306 /* Check TD length */
3307 if (running_total != td_len) {
3308 xhci_err(xhci, "ISOC TD length unmatch\n");
3309 return -EINVAL;
3310 }
3311 }
3312
c41136b0
AX
3313 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
3314 if (xhci->quirks & XHCI_AMD_PLL_FIX)
3315 usb_amd_quirk_pll_disable();
3316 }
3317 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
3318
e1eab2e0
AX
3319 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3320 start_cycle, start_trb);
04e51901 3321 return 0;
522989a2
SS
3322cleanup:
3323 /* Clean up a partially enqueued isoc transfer. */
3324
3325 for (i--; i >= 0; i--)
585df1d9 3326 list_del_init(&urb_priv->td[i]->td_list);
522989a2
SS
3327
3328 /* Use the first TD as a temporary variable to turn the TDs we've queued
3329 * into No-ops with a software-owned cycle bit. That way the hardware
3330 * won't accidentally start executing bogus TDs when we partially
3331 * overwrite them. td->first_trb and td->start_seg are already set.
3332 */
3333 urb_priv->td[0]->last_trb = ep_ring->enqueue;
3334 /* Every TRB except the first & last will have its cycle bit flipped. */
3335 td_to_noop(xhci, ep_ring, urb_priv->td[0], true);
3336
3337 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
3338 ep_ring->enqueue = urb_priv->td[0]->first_trb;
3339 ep_ring->enq_seg = urb_priv->td[0]->start_seg;
3340 ep_ring->cycle_state = start_cycle;
3341 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
3342 return ret;
04e51901
AX
3343}
3344
3345/*
3346 * Check transfer ring to guarantee there is enough room for the urb.
3347 * Update ISO URB start_frame and interval.
3348 * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
3349 * update the urb->start_frame by now.
3350 * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
3351 */
3352int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3353 struct urb *urb, int slot_id, unsigned int ep_index)
3354{
3355 struct xhci_virt_device *xdev;
3356 struct xhci_ring *ep_ring;
3357 struct xhci_ep_ctx *ep_ctx;
3358 int start_frame;
3359 int xhci_interval;
3360 int ep_interval;
3361 int num_tds, num_trbs, i;
3362 int ret;
3363
3364 xdev = xhci->devs[slot_id];
3365 ep_ring = xdev->eps[ep_index].ring;
3366 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3367
3368 num_trbs = 0;
3369 num_tds = urb->number_of_packets;
3370 for (i = 0; i < num_tds; i++)
3371 num_trbs += count_isoc_trbs_needed(xhci, urb, i);
3372
3373 /* Check the ring to guarantee there is enough room for the whole urb.
3374 * Do not insert any td of the urb to the ring if the check failed.
3375 */
28ccd296 3376 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
7e393a83 3377 num_trbs, true, mem_flags);
04e51901
AX
3378 if (ret)
3379 return ret;
3380
3381 start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
3382 start_frame &= 0x3fff;
3383
3384 urb->start_frame = start_frame;
3385 if (urb->dev->speed == USB_SPEED_LOW ||
3386 urb->dev->speed == USB_SPEED_FULL)
3387 urb->start_frame >>= 3;
3388
28ccd296 3389 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
04e51901
AX
3390 ep_interval = urb->interval;
3391 /* Convert to microframes */
3392 if (urb->dev->speed == USB_SPEED_LOW ||
3393 urb->dev->speed == USB_SPEED_FULL)
3394 ep_interval *= 8;
3395 /* FIXME change this to a warning and a suggestion to use the new API
3396 * to set the polling interval (once the API is added).
3397 */
3398 if (xhci_interval != ep_interval) {
7961acd7 3399 if (printk_ratelimit())
04e51901
AX
3400 dev_dbg(&urb->dev->dev, "Driver uses different interval"
3401 " (%d microframe%s) than xHCI "
3402 "(%d microframe%s)\n",
3403 ep_interval,
3404 ep_interval == 1 ? "" : "s",
3405 xhci_interval,
3406 xhci_interval == 1 ? "" : "s");
3407 urb->interval = xhci_interval;
3408 /* Convert back to frames for LS/FS devices */
3409 if (urb->dev->speed == USB_SPEED_LOW ||
3410 urb->dev->speed == USB_SPEED_FULL)
3411 urb->interval /= 8;
3412 }
3413 return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
3414}
3415
d0e96f5a
SS
3416/**** Command Ring Operations ****/
3417
913a8a34
SS
3418/* Generic function for queueing a command TRB on the command ring.
3419 * Check to make sure there's room on the command ring for one command TRB.
3420 * Also check that there's room reserved for commands that must not fail.
3421 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3422 * then only check for the number of reserved spots.
3423 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3424 * because the command event handler may want to resubmit a failed command.
3425 */
3426static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3427 u32 field3, u32 field4, bool command_must_succeed)
7f84eef0 3428{
913a8a34 3429 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
d1dc908a
SS
3430 int ret;
3431
913a8a34
SS
3432 if (!command_must_succeed)
3433 reserved_trbs++;
3434
d1dc908a 3435 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
7e393a83 3436 reserved_trbs, false, GFP_ATOMIC);
d1dc908a
SS
3437 if (ret < 0) {
3438 xhci_err(xhci, "ERR: No room for command on command ring\n");
913a8a34
SS
3439 if (command_must_succeed)
3440 xhci_err(xhci, "ERR: Reserved TRB counting for "
3441 "unfailable commands failed.\n");
d1dc908a 3442 return ret;
7f84eef0 3443 }
7e393a83
AX
3444 queue_trb(xhci, xhci->cmd_ring, false, false, false, field1, field2,
3445 field3, field4 | xhci->cmd_ring->cycle_state);
7f84eef0
SS
3446 return 0;
3447}
3448
3ffbba95 3449/* Queue a slot enable or disable request on the command ring */
23e3be11 3450int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
3ffbba95
SS
3451{
3452 return queue_command(xhci, 0, 0, 0,
913a8a34 3453 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
3ffbba95
SS
3454}
3455
3456/* Queue an address device command TRB */
23e3be11
SS
3457int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3458 u32 slot_id)
3ffbba95 3459{
8e595a5d
SS
3460 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3461 upper_32_bits(in_ctx_ptr), 0,
913a8a34 3462 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
2a8f82c4
SS
3463 false);
3464}
3465
0238634d
SS
3466int xhci_queue_vendor_command(struct xhci_hcd *xhci,
3467 u32 field1, u32 field2, u32 field3, u32 field4)
3468{
3469 return queue_command(xhci, field1, field2, field3, field4, false);
3470}
3471
2a8f82c4
SS
3472/* Queue a reset device command TRB */
3473int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
3474{
3475 return queue_command(xhci, 0, 0, 0,
3476 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
913a8a34 3477 false);
3ffbba95 3478}
f94e0186
SS
3479
3480/* Queue a configure endpoint command TRB */
23e3be11 3481int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
913a8a34 3482 u32 slot_id, bool command_must_succeed)
f94e0186 3483{
8e595a5d
SS
3484 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3485 upper_32_bits(in_ctx_ptr), 0,
913a8a34
SS
3486 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3487 command_must_succeed);
f94e0186 3488}
ae636747 3489
f2217e8e
SS
3490/* Queue an evaluate context command TRB */
3491int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3492 u32 slot_id)
3493{
3494 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3495 upper_32_bits(in_ctx_ptr), 0,
913a8a34
SS
3496 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
3497 false);
f2217e8e
SS
3498}
3499
be88fe4f
AX
3500/*
3501 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3502 * activity on an endpoint that is about to be suspended.
3503 */
23e3be11 3504int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
be88fe4f 3505 unsigned int ep_index, int suspend)
ae636747
SS
3506{
3507 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3508 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3509 u32 type = TRB_TYPE(TRB_STOP_RING);
be88fe4f 3510 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
ae636747
SS
3511
3512 return queue_command(xhci, 0, 0, 0,
be88fe4f 3513 trb_slot_id | trb_ep_index | type | trb_suspend, false);
ae636747
SS
3514}
3515
3516/* Set Transfer Ring Dequeue Pointer command.
3517 * This should not be used for endpoints that have streams enabled.
3518 */
3519static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
e9df17eb
SS
3520 unsigned int ep_index, unsigned int stream_id,
3521 struct xhci_segment *deq_seg,
ae636747
SS
3522 union xhci_trb *deq_ptr, u32 cycle_state)
3523{
3524 dma_addr_t addr;
3525 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3526 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
e9df17eb 3527 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
ae636747 3528 u32 type = TRB_TYPE(TRB_SET_DEQ);
bf161e85 3529 struct xhci_virt_ep *ep;
ae636747 3530
23e3be11 3531 addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
c92bcfa7 3532 if (addr == 0) {
ae636747 3533 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
700e2052
GKH
3534 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
3535 deq_seg, deq_ptr);
c92bcfa7
SS
3536 return 0;
3537 }
bf161e85
SS
3538 ep = &xhci->devs[slot_id]->eps[ep_index];
3539 if ((ep->ep_state & SET_DEQ_PENDING)) {
3540 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
3541 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
3542 return 0;
3543 }
3544 ep->queued_deq_seg = deq_seg;
3545 ep->queued_deq_ptr = deq_ptr;
8e595a5d 3546 return queue_command(xhci, lower_32_bits(addr) | cycle_state,
e9df17eb 3547 upper_32_bits(addr), trb_stream_id,
913a8a34 3548 trb_slot_id | trb_ep_index | type, false);
ae636747 3549}
a1587d97
SS
3550
3551int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
3552 unsigned int ep_index)
3553{
3554 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3555 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3556 u32 type = TRB_TYPE(TRB_RESET_EP);
3557
913a8a34
SS
3558 return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
3559 false);
a1587d97 3560}