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