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