blob: f9b6fa364f22ecd0572231171722b0176966e2dc [file] [log] [blame]
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001/*
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
Sarah Sharp8a96c052009-04-27 19:59:19 -070067#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090068#include <linux/slab.h>
Sarah Sharp7f84eef2009-04-27 19:53:56 -070069#include "xhci.h"
70
Andiry Xube88fe42010-10-14 07:22:57 -070071static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
72 struct xhci_virt_device *virt_dev,
73 struct xhci_event_cmd *event);
74
Sarah Sharp7f84eef2009-04-27 19:53:56 -070075/*
76 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
77 * address of the TRB.
78 */
Sarah Sharp23e3be12009-04-29 19:05:20 -070079dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -070080 union xhci_trb *trb)
81{
Sarah Sharp6071d832009-05-14 11:44:14 -070082 unsigned long segment_offset;
Sarah Sharp7f84eef2009-04-27 19:53:56 -070083
Sarah Sharp6071d832009-05-14 11:44:14 -070084 if (!seg || !trb || trb < seg->trbs)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070085 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070086 /* offset in TRBs */
87 segment_offset = trb - seg->trbs;
88 if (segment_offset > TRBS_PER_SEGMENT)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070089 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070090 return seg->dma + (segment_offset * sizeof(*trb));
Sarah Sharp7f84eef2009-04-27 19:53:56 -070091}
92
93/* Does this link TRB point to the first segment in a ring,
94 * or was the previous TRB the last TRB on the last segment in the ERST?
95 */
Dmitry Torokhov575688e2011-03-20 02:15:16 -070096static bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -070097 struct xhci_segment *seg, union xhci_trb *trb)
98{
99 if (ring == xhci->event_ring)
100 return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
101 (seg->next == xhci->event_ring->first_seg);
102 else
Matt Evans28ccd292011-03-29 13:40:46 +1100103 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700104}
105
106/* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
107 * segment? I.e. would the updated event TRB pointer step off the end of the
108 * event seg?
109 */
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700110static int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700111 struct xhci_segment *seg, union xhci_trb *trb)
112{
113 if (ring == xhci->event_ring)
114 return trb == &seg->trbs[TRBS_PER_SEGMENT];
115 else
Matt Evansf5960b62011-06-01 10:22:55 +1000116 return TRB_TYPE_LINK_LE32(trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700117}
118
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700119static int enqueue_is_link_trb(struct xhci_ring *ring)
John Youn6c12db92010-05-10 15:33:00 -0700120{
121 struct xhci_link_trb *link = &ring->enqueue->link;
Matt Evansf5960b62011-06-01 10:22:55 +1000122 return TRB_TYPE_LINK_LE32(link->control);
John Youn6c12db92010-05-10 15:33:00 -0700123}
124
Sarah Sharpae636742009-04-29 19:02:31 -0700125/* Updates trb to point to the next TRB in the ring, and updates seg if the next
126 * TRB is in a new segment. This does not skip over link TRBs, and it does not
127 * effect the ring dequeue or enqueue pointers.
128 */
129static void next_trb(struct xhci_hcd *xhci,
130 struct xhci_ring *ring,
131 struct xhci_segment **seg,
132 union xhci_trb **trb)
133{
134 if (last_trb(xhci, ring, *seg, *trb)) {
135 *seg = (*seg)->next;
136 *trb = ((*seg)->trbs);
137 } else {
John Youna1669b22010-08-09 13:56:11 -0700138 (*trb)++;
Sarah Sharpae636742009-04-29 19:02:31 -0700139 }
140}
141
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700142/*
143 * See Cycle bit rules. SW is the consumer for the event ring only.
144 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
145 */
Andiry Xu3b72fca2012-03-05 17:49:32 +0800146static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700147{
Andiry Xub008df62012-03-05 17:49:34 +0800148 union xhci_trb *next;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700149 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700150
151 ring->deq_updates++;
Andiry Xub008df62012-03-05 17:49:34 +0800152
153 /* If this is not event ring, there is one more usable TRB */
154 if (ring->type != TYPE_EVENT &&
155 !last_trb(xhci, ring, ring->deq_seg, ring->dequeue))
156 ring->num_trbs_free++;
157 next = ++(ring->dequeue);
158
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700159 /* Update the dequeue pointer further if that was a link TRB or we're at
160 * the end of an event ring segment (which doesn't have link TRBS)
161 */
162 while (last_trb(xhci, ring, ring->deq_seg, next)) {
Andiry Xu3b72fca2012-03-05 17:49:32 +0800163 if (ring->type == TYPE_EVENT && last_trb_on_last_seg(xhci,
164 ring, ring->deq_seg, next)) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700165 ring->cycle_state = (ring->cycle_state ? 0 : 1);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700166 }
167 ring->deq_seg = ring->deq_seg->next;
168 ring->dequeue = ring->deq_seg->trbs;
169 next = ring->dequeue;
170 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700171 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700172}
173
174/*
175 * See Cycle bit rules. SW is the consumer for the event ring only.
176 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
177 *
178 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
179 * chain bit is set), then set the chain bit in all the following link TRBs.
180 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
181 * have their chain bit cleared (so that each Link TRB is a separate TD).
182 *
183 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
Sarah Sharpb0567b32009-08-07 14:04:36 -0700184 * set, but other sections talk about dealing with the chain bit set. This was
185 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
186 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700187 *
188 * @more_trbs_coming: Will you enqueue more TRBs before calling
189 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700190 */
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700191static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +0800192 bool more_trbs_coming)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700193{
194 u32 chain;
195 union xhci_trb *next;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700196 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700197
Matt Evans28ccd292011-03-29 13:40:46 +1100198 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
Andiry Xub008df62012-03-05 17:49:34 +0800199 /* If this is not event ring, there is one less usable TRB */
200 if (ring->type != TYPE_EVENT &&
201 !last_trb(xhci, ring, ring->enq_seg, ring->enqueue))
202 ring->num_trbs_free--;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700203 next = ++(ring->enqueue);
204
205 ring->enq_updates++;
206 /* Update the dequeue pointer further if that was a link TRB or we're at
207 * the end of an event ring segment (which doesn't have link TRBS)
208 */
209 while (last_trb(xhci, ring, ring->enq_seg, next)) {
Andiry Xu3b72fca2012-03-05 17:49:32 +0800210 if (ring->type != TYPE_EVENT) {
211 /*
212 * If the caller doesn't plan on enqueueing more
213 * TDs before ringing the doorbell, then we
214 * don't want to give the link TRB to the
215 * hardware just yet. We'll give the link TRB
216 * back in prepare_ring() just before we enqueue
217 * the TD at the top of the ring.
218 */
219 if (!chain && !more_trbs_coming)
220 break;
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700221
Andiry Xu3b72fca2012-03-05 17:49:32 +0800222 /* If we're not dealing with 0.95 hardware or
223 * isoc rings on AMD 0.96 host,
224 * carry over the chain bit of the previous TRB
225 * (which may mean the chain bit is cleared).
226 */
227 if (!(ring->type == TYPE_ISOC &&
228 (xhci->quirks & XHCI_AMD_0x96_HOST))
Andiry Xu7e393a82011-09-23 14:19:54 -0700229 && !xhci_link_trb_quirk(xhci)) {
Andiry Xu3b72fca2012-03-05 17:49:32 +0800230 next->link.control &=
231 cpu_to_le32(~TRB_CHAIN);
232 next->link.control |=
233 cpu_to_le32(chain);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700234 }
Andiry Xu3b72fca2012-03-05 17:49:32 +0800235 /* Give this link TRB to the hardware */
236 wmb();
237 next->link.control ^= cpu_to_le32(TRB_CYCLE);
238
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700239 /* Toggle the cycle bit after the last ring segment. */
240 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
241 ring->cycle_state = (ring->cycle_state ? 0 : 1);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700242 }
243 }
244 ring->enq_seg = ring->enq_seg->next;
245 ring->enqueue = ring->enq_seg->trbs;
246 next = ring->enqueue;
247 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700248 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700249}
250
251/*
252 * Check to see if there's room to enqueue num_trbs on the ring. See rules
253 * above.
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700254 */
Andiry Xub008df62012-03-05 17:49:34 +0800255static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700256 unsigned int num_trbs)
257{
Andiry Xub008df62012-03-05 17:49:34 +0800258 if (ring->num_trbs_free >= num_trbs)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700259 return 1;
Andiry Xub008df62012-03-05 17:49:34 +0800260
261 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700262}
263
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700264/* Ring the host controller doorbell after placing a command on the ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700265void xhci_ring_cmd_db(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700266{
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700267 xhci_dbg(xhci, "// Ding dong!\n");
Matthew Wilcox50d646762010-12-15 14:18:11 -0500268 xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700269 /* Flush PCI posted writes */
270 xhci_readl(xhci, &xhci->dba->doorbell[0]);
271}
272
Andiry Xube88fe42010-10-14 07:22:57 -0700273void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700274 unsigned int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700275 unsigned int ep_index,
276 unsigned int stream_id)
Sarah Sharpae636742009-04-29 19:02:31 -0700277{
Matt Evans28ccd292011-03-29 13:40:46 +1100278 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
Matthew Wilcox50d646762010-12-15 14:18:11 -0500279 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
280 unsigned int ep_state = ep->ep_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700281
Sarah Sharpae636742009-04-29 19:02:31 -0700282 /* Don't ring the doorbell for this endpoint if there are pending
Matthew Wilcox50d646762010-12-15 14:18:11 -0500283 * cancellations because we don't want to interrupt processing.
Sarah Sharp8df75f42010-04-02 15:34:16 -0700284 * We don't want to restart any stream rings if there's a set dequeue
285 * pointer command pending because the device can choose to start any
286 * stream once the endpoint is on the HW schedule.
287 * FIXME - check all the stream rings for pending cancellations.
Sarah Sharpae636742009-04-29 19:02:31 -0700288 */
Matthew Wilcox50d646762010-12-15 14:18:11 -0500289 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
290 (ep_state & EP_HALTED))
291 return;
292 xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr);
293 /* The CPU has better things to do at this point than wait for a
294 * write-posting flush. It'll get there soon enough.
295 */
Sarah Sharpae636742009-04-29 19:02:31 -0700296}
297
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700298/* Ring the doorbell for any rings with pending URBs */
299static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
300 unsigned int slot_id,
301 unsigned int ep_index)
302{
303 unsigned int stream_id;
304 struct xhci_virt_ep *ep;
305
306 ep = &xhci->devs[slot_id]->eps[ep_index];
307
308 /* A ring has pending URBs if its TD list is not empty */
309 if (!(ep->ep_state & EP_HAS_STREAMS)) {
310 if (!(list_empty(&ep->ring->td_list)))
Andiry Xube88fe42010-10-14 07:22:57 -0700311 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700312 return;
313 }
314
315 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
316 stream_id++) {
317 struct xhci_stream_info *stream_info = ep->stream_info;
318 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
Andiry Xube88fe42010-10-14 07:22:57 -0700319 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
320 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700321 }
322}
323
Sarah Sharpae636742009-04-29 19:02:31 -0700324/*
325 * Find the segment that trb is in. Start searching in start_seg.
326 * If we must move past a segment that has a link TRB with a toggle cycle state
327 * bit set, then we will toggle the value pointed at by cycle_state.
328 */
329static struct xhci_segment *find_trb_seg(
330 struct xhci_segment *start_seg,
331 union xhci_trb *trb, int *cycle_state)
332{
333 struct xhci_segment *cur_seg = start_seg;
334 struct xhci_generic_trb *generic_trb;
335
336 while (cur_seg->trbs > trb ||
337 &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
338 generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
Matt Evansf5960b62011-06-01 10:22:55 +1000339 if (generic_trb->field[3] & cpu_to_le32(LINK_TOGGLE))
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800340 *cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700341 cur_seg = cur_seg->next;
342 if (cur_seg == start_seg)
343 /* Looped over the entire list. Oops! */
Randy Dunlap326b4812010-04-19 08:53:50 -0700344 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700345 }
346 return cur_seg;
347}
348
Sarah Sharp021bff92010-07-29 22:12:20 -0700349
350static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
351 unsigned int slot_id, unsigned int ep_index,
352 unsigned int stream_id)
353{
354 struct xhci_virt_ep *ep;
355
356 ep = &xhci->devs[slot_id]->eps[ep_index];
357 /* Common case: no streams */
358 if (!(ep->ep_state & EP_HAS_STREAMS))
359 return ep->ring;
360
361 if (stream_id == 0) {
362 xhci_warn(xhci,
363 "WARN: Slot ID %u, ep index %u has streams, "
364 "but URB has no stream ID.\n",
365 slot_id, ep_index);
366 return NULL;
367 }
368
369 if (stream_id < ep->stream_info->num_streams)
370 return ep->stream_info->stream_rings[stream_id];
371
372 xhci_warn(xhci,
373 "WARN: Slot ID %u, ep index %u has "
374 "stream IDs 1 to %u allocated, "
375 "but stream ID %u is requested.\n",
376 slot_id, ep_index,
377 ep->stream_info->num_streams - 1,
378 stream_id);
379 return NULL;
380}
381
382/* Get the right ring for the given URB.
383 * If the endpoint supports streams, boundary check the URB's stream ID.
384 * If the endpoint doesn't support streams, return the singular endpoint ring.
385 */
386static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
387 struct urb *urb)
388{
389 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
390 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
391}
392
Sarah Sharpae636742009-04-29 19:02:31 -0700393/*
394 * Move the xHC's endpoint ring dequeue pointer past cur_td.
395 * Record the new state of the xHC's endpoint ring dequeue segment,
396 * dequeue pointer, and new consumer cycle state in state.
397 * Update our internal representation of the ring's dequeue pointer.
398 *
399 * We do this in three jumps:
400 * - First we update our new ring state to be the same as when the xHC stopped.
401 * - Then we traverse the ring to find the segment that contains
402 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
403 * any link TRBs with the toggle cycle bit set.
404 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
405 * if we've moved it past a link TRB with the toggle cycle bit set.
Matt Evans28ccd292011-03-29 13:40:46 +1100406 *
407 * Some of the uses of xhci_generic_trb are grotty, but if they're done
408 * with correct __le32 accesses they should work fine. Only users of this are
409 * in here.
Sarah Sharpae636742009-04-29 19:02:31 -0700410 */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700411void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700412 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700413 unsigned int stream_id, struct xhci_td *cur_td,
414 struct xhci_dequeue_state *state)
Sarah Sharpae636742009-04-29 19:02:31 -0700415{
416 struct xhci_virt_device *dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700417 struct xhci_ring *ep_ring;
Sarah Sharpae636742009-04-29 19:02:31 -0700418 struct xhci_generic_trb *trb;
John Yound115b042009-07-27 12:05:15 -0700419 struct xhci_ep_ctx *ep_ctx;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700420 dma_addr_t addr;
Sarah Sharpae636742009-04-29 19:02:31 -0700421
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700422 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
423 ep_index, stream_id);
424 if (!ep_ring) {
425 xhci_warn(xhci, "WARN can't find new dequeue state "
426 "for invalid stream ID %u.\n",
427 stream_id);
428 return;
429 }
Sarah Sharpae636742009-04-29 19:02:31 -0700430 state->new_cycle_state = 0;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700431 xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700432 state->new_deq_seg = find_trb_seg(cur_td->start_seg,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700433 dev->eps[ep_index].stopped_trb,
Sarah Sharpae636742009-04-29 19:02:31 -0700434 &state->new_cycle_state);
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800435 if (!state->new_deq_seg) {
436 WARN_ON(1);
437 return;
438 }
439
Sarah Sharpae636742009-04-29 19:02:31 -0700440 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700441 xhci_dbg(xhci, "Finding endpoint context\n");
John Yound115b042009-07-27 12:05:15 -0700442 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +1100443 state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
Sarah Sharpae636742009-04-29 19:02:31 -0700444
445 state->new_deq_ptr = cur_td->last_trb;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700446 xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700447 state->new_deq_seg = find_trb_seg(state->new_deq_seg,
448 state->new_deq_ptr,
449 &state->new_cycle_state);
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800450 if (!state->new_deq_seg) {
451 WARN_ON(1);
452 return;
453 }
Sarah Sharpae636742009-04-29 19:02:31 -0700454
455 trb = &state->new_deq_ptr->generic;
Matt Evansf5960b62011-06-01 10:22:55 +1000456 if (TRB_TYPE_LINK_LE32(trb->field[3]) &&
457 (trb->field[3] & cpu_to_le32(LINK_TOGGLE)))
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800458 state->new_cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700459 next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
460
Sarah Sharp01a1fdb2011-02-23 18:12:29 -0800461 /*
462 * If there is only one segment in a ring, find_trb_seg()'s while loop
463 * will not run, and it will return before it has a chance to see if it
464 * needs to toggle the cycle bit. It can't tell if the stalled transfer
465 * ended just before the link TRB on a one-segment ring, or if the TD
466 * wrapped around the top of the ring, because it doesn't have the TD in
467 * question. Look for the one-segment case where stalled TRB's address
468 * is greater than the new dequeue pointer address.
469 */
470 if (ep_ring->first_seg == ep_ring->first_seg->next &&
471 state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
472 state->new_cycle_state ^= 0x1;
473 xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
474
Sarah Sharpae636742009-04-29 19:02:31 -0700475 /* Don't update the ring cycle state for the producer (us). */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700476 xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
477 state->new_deq_seg);
478 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
479 xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
480 (unsigned long long) addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700481}
482
Sarah Sharp522989a2011-07-29 12:44:32 -0700483/* flip_cycle means flip the cycle bit of all but the first and last TRB.
484 * (The last TRB actually points to the ring enqueue pointer, which is not part
485 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
486 */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700487static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Sarah Sharp522989a2011-07-29 12:44:32 -0700488 struct xhci_td *cur_td, bool flip_cycle)
Sarah Sharpae636742009-04-29 19:02:31 -0700489{
490 struct xhci_segment *cur_seg;
491 union xhci_trb *cur_trb;
492
493 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
494 true;
495 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +1000496 if (TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) {
Sarah Sharpae636742009-04-29 19:02:31 -0700497 /* Unchain any chained Link TRBs, but
498 * leave the pointers intact.
499 */
Matt Evans28ccd292011-03-29 13:40:46 +1100500 cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN);
Sarah Sharp522989a2011-07-29 12:44:32 -0700501 /* Flip the cycle bit (link TRBs can't be the first
502 * or last TRB).
503 */
504 if (flip_cycle)
505 cur_trb->generic.field[3] ^=
506 cpu_to_le32(TRB_CYCLE);
Sarah Sharpae636742009-04-29 19:02:31 -0700507 xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700508 xhci_dbg(xhci, "Address = %p (0x%llx dma); "
509 "in seg %p (0x%llx dma)\n",
510 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700511 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700512 cur_seg,
513 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700514 } else {
515 cur_trb->generic.field[0] = 0;
516 cur_trb->generic.field[1] = 0;
517 cur_trb->generic.field[2] = 0;
518 /* Preserve only the cycle bit of this TRB */
Matt Evans28ccd292011-03-29 13:40:46 +1100519 cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
Sarah Sharp522989a2011-07-29 12:44:32 -0700520 /* Flip the cycle bit except on the first or last TRB */
521 if (flip_cycle && cur_trb != cur_td->first_trb &&
522 cur_trb != cur_td->last_trb)
523 cur_trb->generic.field[3] ^=
524 cpu_to_le32(TRB_CYCLE);
Matt Evans28ccd292011-03-29 13:40:46 +1100525 cur_trb->generic.field[3] |= cpu_to_le32(
526 TRB_TYPE(TRB_TR_NOOP));
Sarah Sharp79688ac2011-12-19 16:56:04 -0800527 xhci_dbg(xhci, "TRB to noop at offset 0x%llx\n",
528 (unsigned long long)
529 xhci_trb_virt_to_dma(cur_seg, cur_trb));
Sarah Sharpae636742009-04-29 19:02:31 -0700530 }
531 if (cur_trb == cur_td->last_trb)
532 break;
533 }
534}
535
536static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700537 unsigned int ep_index, unsigned int stream_id,
538 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -0700539 union xhci_trb *deq_ptr, u32 cycle_state);
540
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700541void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700542 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700543 unsigned int stream_id,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700544 struct xhci_dequeue_state *deq_state)
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700545{
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700546 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
547
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700548 xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
549 "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
550 deq_state->new_deq_seg,
551 (unsigned long long)deq_state->new_deq_seg->dma,
552 deq_state->new_deq_ptr,
553 (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
554 deq_state->new_cycle_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700555 queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700556 deq_state->new_deq_seg,
557 deq_state->new_deq_ptr,
558 (u32) deq_state->new_cycle_state);
559 /* Stop the TD queueing code from ringing the doorbell until
560 * this command completes. The HC won't set the dequeue pointer
561 * if the ring is running, and ringing the doorbell starts the
562 * ring running.
563 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700564 ep->ep_state |= SET_DEQ_PENDING;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700565}
566
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700567static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700568 struct xhci_virt_ep *ep)
569{
570 ep->ep_state &= ~EP_HALT_PENDING;
571 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
572 * timer is running on another CPU, we don't decrement stop_cmds_pending
573 * (since we didn't successfully stop the watchdog timer).
574 */
575 if (del_timer(&ep->stop_cmd_timer))
576 ep->stop_cmds_pending--;
577}
578
579/* Must be called with xhci->lock held in interrupt context */
580static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
581 struct xhci_td *cur_td, int status, char *adjective)
582{
Sarah Sharp214f76f2010-10-26 11:22:02 -0700583 struct usb_hcd *hcd;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700584 struct urb *urb;
585 struct urb_priv *urb_priv;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700586
Andiry Xu8e51adc2010-07-22 15:23:31 -0700587 urb = cur_td->urb;
588 urb_priv = urb->hcpriv;
589 urb_priv->td_cnt++;
Sarah Sharp214f76f2010-10-26 11:22:02 -0700590 hcd = bus_to_hcd(urb->dev->bus);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700591
Andiry Xu8e51adc2010-07-22 15:23:31 -0700592 /* Only giveback urb when this is the last td in urb */
593 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xuc41136b2011-03-22 17:08:14 +0800594 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
595 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
596 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
597 if (xhci->quirks & XHCI_AMD_PLL_FIX)
598 usb_amd_quirk_pll_enable();
599 }
600 }
Andiry Xu8e51adc2010-07-22 15:23:31 -0700601 usb_hcd_unlink_urb_from_ep(hcd, urb);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700602
603 spin_unlock(&xhci->lock);
604 usb_hcd_giveback_urb(hcd, urb, status);
605 xhci_urb_free_priv(xhci, urb_priv);
606 spin_lock(&xhci->lock);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700607 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700608}
609
Sarah Sharpae636742009-04-29 19:02:31 -0700610/*
611 * When we get a command completion for a Stop Endpoint Command, we need to
612 * unlink any cancelled TDs from the ring. There are two ways to do that:
613 *
614 * 1. If the HW was in the middle of processing the TD that needs to be
615 * cancelled, then we must move the ring's dequeue pointer past the last TRB
616 * in the TD with a Set Dequeue Pointer Command.
617 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
618 * bit cleared) so that the HW will skip over them.
619 */
620static void handle_stopped_endpoint(struct xhci_hcd *xhci,
Andiry Xube88fe42010-10-14 07:22:57 -0700621 union xhci_trb *trb, struct xhci_event_cmd *event)
Sarah Sharpae636742009-04-29 19:02:31 -0700622{
623 unsigned int slot_id;
624 unsigned int ep_index;
Andiry Xube88fe42010-10-14 07:22:57 -0700625 struct xhci_virt_device *virt_dev;
Sarah Sharpae636742009-04-29 19:02:31 -0700626 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700627 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700628 struct list_head *entry;
Randy Dunlap326b4812010-04-19 08:53:50 -0700629 struct xhci_td *cur_td = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700630 struct xhci_td *last_unlinked_td;
631
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700632 struct xhci_dequeue_state deq_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700633
Andiry Xube88fe42010-10-14 07:22:57 -0700634 if (unlikely(TRB_TO_SUSPEND_PORT(
Matt Evans28ccd292011-03-29 13:40:46 +1100635 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])))) {
Andiry Xube88fe42010-10-14 07:22:57 -0700636 slot_id = TRB_TO_SLOT_ID(
Matt Evans28ccd292011-03-29 13:40:46 +1100637 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
Andiry Xube88fe42010-10-14 07:22:57 -0700638 virt_dev = xhci->devs[slot_id];
639 if (virt_dev)
640 handle_cmd_in_cmd_wait_list(xhci, virt_dev,
641 event);
642 else
643 xhci_warn(xhci, "Stop endpoint command "
644 "completion for disabled slot %u\n",
645 slot_id);
646 return;
647 }
648
Sarah Sharpae636742009-04-29 19:02:31 -0700649 memset(&deq_state, 0, sizeof(deq_state));
Matt Evans28ccd292011-03-29 13:40:46 +1100650 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
651 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700652 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharpae636742009-04-29 19:02:31 -0700653
Sarah Sharp678539c2009-10-27 10:55:52 -0700654 if (list_empty(&ep->cancelled_td_list)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700655 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharp0714a572011-05-24 11:53:29 -0700656 ep->stopped_td = NULL;
657 ep->stopped_trb = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700658 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700659 return;
Sarah Sharp678539c2009-10-27 10:55:52 -0700660 }
Sarah Sharpae636742009-04-29 19:02:31 -0700661
662 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
663 * We have the xHCI lock, so nothing can modify this list until we drop
664 * it. We're also in the event handler, so we can't get re-interrupted
665 * if another Stop Endpoint command completes
666 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700667 list_for_each(entry, &ep->cancelled_td_list) {
Sarah Sharpae636742009-04-29 19:02:31 -0700668 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
Sarah Sharp79688ac2011-12-19 16:56:04 -0800669 xhci_dbg(xhci, "Removing canceled TD starting at 0x%llx (dma).\n",
670 (unsigned long long)xhci_trb_virt_to_dma(
671 cur_td->start_seg, cur_td->first_trb));
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700672 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
673 if (!ep_ring) {
674 /* This shouldn't happen unless a driver is mucking
675 * with the stream ID after submission. This will
676 * leave the TD on the hardware ring, and the hardware
677 * will try to execute it, and may access a buffer
678 * that has already been freed. In the best case, the
679 * hardware will execute it, and the event handler will
680 * ignore the completion event for that TD, since it was
681 * removed from the td_list for that endpoint. In
682 * short, don't muck with the stream ID after
683 * submission.
684 */
685 xhci_warn(xhci, "WARN Cancelled URB %p "
686 "has invalid stream ID %u.\n",
687 cur_td->urb,
688 cur_td->urb->stream_id);
689 goto remove_finished_td;
690 }
Sarah Sharpae636742009-04-29 19:02:31 -0700691 /*
692 * If we stopped on the TD we need to cancel, then we have to
693 * move the xHC endpoint ring dequeue pointer past this TD.
694 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700695 if (cur_td == ep->stopped_td)
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700696 xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
697 cur_td->urb->stream_id,
698 cur_td, &deq_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700699 else
Sarah Sharp522989a2011-07-29 12:44:32 -0700700 td_to_noop(xhci, ep_ring, cur_td, false);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700701remove_finished_td:
Sarah Sharpae636742009-04-29 19:02:31 -0700702 /*
703 * The event handler won't see a completion for this TD anymore,
704 * so remove it from the endpoint ring's TD list. Keep it in
705 * the cancelled TD list for URB completion later.
706 */
Sarah Sharp585df1d2011-08-02 15:43:40 -0700707 list_del_init(&cur_td->td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700708 }
709 last_unlinked_td = cur_td;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700710 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharpae636742009-04-29 19:02:31 -0700711
712 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
713 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700714 xhci_queue_new_dequeue_state(xhci,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700715 slot_id, ep_index,
716 ep->stopped_td->urb->stream_id,
717 &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700718 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700719 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700720 /* Otherwise ring the doorbell(s) to restart queued transfers */
721 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700722 }
Sarah Sharp1624ae12010-05-06 13:40:08 -0700723 ep->stopped_td = NULL;
724 ep->stopped_trb = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700725
726 /*
727 * Drop the lock and complete the URBs in the cancelled TD list.
728 * New TDs to be cancelled might be added to the end of the list before
729 * we can complete all the URBs for the TDs we already unlinked.
730 * So stop when we've completed the URB for the last TD we unlinked.
731 */
732 do {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700733 cur_td = list_entry(ep->cancelled_td_list.next,
Sarah Sharpae636742009-04-29 19:02:31 -0700734 struct xhci_td, cancelled_td_list);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700735 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700736
737 /* Clean up the cancelled URB */
Sarah Sharpae636742009-04-29 19:02:31 -0700738 /* Doesn't matter what we pass for status, since the core will
739 * just overwrite it (because the URB has been unlinked).
740 */
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700741 xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
Sarah Sharpae636742009-04-29 19:02:31 -0700742
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700743 /* Stop processing the cancelled list if the watchdog timer is
744 * running.
745 */
746 if (xhci->xhc_state & XHCI_STATE_DYING)
747 return;
Sarah Sharpae636742009-04-29 19:02:31 -0700748 } while (cur_td != last_unlinked_td);
749
750 /* Return to the event handler with xhci->lock re-acquired */
751}
752
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700753/* Watchdog timer function for when a stop endpoint command fails to complete.
754 * In this case, we assume the host controller is broken or dying or dead. The
755 * host may still be completing some other events, so we have to be careful to
756 * let the event ring handler and the URB dequeueing/enqueueing functions know
757 * through xhci->state.
758 *
759 * The timer may also fire if the host takes a very long time to respond to the
760 * command, and the stop endpoint command completion handler cannot delete the
761 * timer before the timer function is called. Another endpoint cancellation may
762 * sneak in before the timer function can grab the lock, and that may queue
763 * another stop endpoint command and add the timer back. So we cannot use a
764 * simple flag to say whether there is a pending stop endpoint command for a
765 * particular endpoint.
766 *
767 * Instead we use a combination of that flag and a counter for the number of
768 * pending stop endpoint commands. If the timer is the tail end of the last
769 * stop endpoint command, and the endpoint's command is still pending, we assume
770 * the host is dying.
771 */
772void xhci_stop_endpoint_command_watchdog(unsigned long arg)
773{
774 struct xhci_hcd *xhci;
775 struct xhci_virt_ep *ep;
776 struct xhci_virt_ep *temp_ep;
777 struct xhci_ring *ring;
778 struct xhci_td *cur_td;
779 int ret, i, j;
Don Zickusf43d6232011-10-20 23:52:14 -0400780 unsigned long flags;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700781
782 ep = (struct xhci_virt_ep *) arg;
783 xhci = ep->xhci;
784
Don Zickusf43d6232011-10-20 23:52:14 -0400785 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700786
787 ep->stop_cmds_pending--;
788 if (xhci->xhc_state & XHCI_STATE_DYING) {
789 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
790 "xHCI as DYING, exiting.\n");
Don Zickusf43d6232011-10-20 23:52:14 -0400791 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700792 return;
793 }
794 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
795 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
796 "exiting.\n");
Don Zickusf43d6232011-10-20 23:52:14 -0400797 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700798 return;
799 }
800
801 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
802 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
803 /* Oops, HC is dead or dying or at least not responding to the stop
804 * endpoint command.
805 */
806 xhci->xhc_state |= XHCI_STATE_DYING;
807 /* Disable interrupts from the host controller and start halting it */
808 xhci_quiesce(xhci);
Don Zickusf43d6232011-10-20 23:52:14 -0400809 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700810
811 ret = xhci_halt(xhci);
812
Don Zickusf43d6232011-10-20 23:52:14 -0400813 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700814 if (ret < 0) {
815 /* This is bad; the host is not responding to commands and it's
816 * not allowing itself to be halted. At least interrupts are
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800817 * disabled. If we call usb_hc_died(), it will attempt to
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700818 * disconnect all device drivers under this host. Those
819 * disconnect() methods will wait for all URBs to be unlinked,
820 * so we must complete them.
821 */
822 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
823 xhci_warn(xhci, "Completing active URBs anyway.\n");
824 /* We could turn all TDs on the rings to no-ops. This won't
825 * help if the host has cached part of the ring, and is slow if
826 * we want to preserve the cycle bit. Skip it and hope the host
827 * doesn't touch the memory.
828 */
829 }
830 for (i = 0; i < MAX_HC_SLOTS; i++) {
831 if (!xhci->devs[i])
832 continue;
833 for (j = 0; j < 31; j++) {
834 temp_ep = &xhci->devs[i]->eps[j];
835 ring = temp_ep->ring;
836 if (!ring)
837 continue;
838 xhci_dbg(xhci, "Killing URBs for slot ID %u, "
839 "ep index %u\n", i, j);
840 while (!list_empty(&ring->td_list)) {
841 cur_td = list_first_entry(&ring->td_list,
842 struct xhci_td,
843 td_list);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700844 list_del_init(&cur_td->td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700845 if (!list_empty(&cur_td->cancelled_td_list))
Sarah Sharp585df1d2011-08-02 15:43:40 -0700846 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700847 xhci_giveback_urb_in_irq(xhci, cur_td,
848 -ESHUTDOWN, "killed");
849 }
850 while (!list_empty(&temp_ep->cancelled_td_list)) {
851 cur_td = list_first_entry(
852 &temp_ep->cancelled_td_list,
853 struct xhci_td,
854 cancelled_td_list);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700855 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700856 xhci_giveback_urb_in_irq(xhci, cur_td,
857 -ESHUTDOWN, "killed");
858 }
859 }
860 }
Don Zickusf43d6232011-10-20 23:52:14 -0400861 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700862 xhci_dbg(xhci, "Calling usb_hc_died()\n");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800863 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700864 xhci_dbg(xhci, "xHCI host controller is dead.\n");
865}
866
Andiry Xub008df62012-03-05 17:49:34 +0800867
868static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci,
869 struct xhci_virt_device *dev,
870 struct xhci_ring *ep_ring,
871 unsigned int ep_index)
872{
873 union xhci_trb *dequeue_temp;
874 int num_trbs_free_temp;
875 bool revert = false;
876
877 num_trbs_free_temp = ep_ring->num_trbs_free;
878 dequeue_temp = ep_ring->dequeue;
879
880 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) {
881 /* We have more usable TRBs */
882 ep_ring->num_trbs_free++;
883 ep_ring->dequeue++;
884 if (last_trb(xhci, ep_ring, ep_ring->deq_seg,
885 ep_ring->dequeue)) {
886 if (ep_ring->dequeue ==
887 dev->eps[ep_index].queued_deq_ptr)
888 break;
889 ep_ring->deq_seg = ep_ring->deq_seg->next;
890 ep_ring->dequeue = ep_ring->deq_seg->trbs;
891 }
892 if (ep_ring->dequeue == dequeue_temp) {
893 revert = true;
894 break;
895 }
896 }
897
898 if (revert) {
899 xhci_dbg(xhci, "Unable to find new dequeue pointer\n");
900 ep_ring->num_trbs_free = num_trbs_free_temp;
901 }
902}
903
Sarah Sharpae636742009-04-29 19:02:31 -0700904/*
905 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
906 * we need to clear the set deq pending flag in the endpoint ring state, so that
907 * the TD queueing code can ring the doorbell again. We also need to ring the
908 * endpoint doorbell to restart the ring, but only if there aren't more
909 * cancellations pending.
910 */
911static void handle_set_deq_completion(struct xhci_hcd *xhci,
912 struct xhci_event_cmd *event,
913 union xhci_trb *trb)
914{
915 unsigned int slot_id;
916 unsigned int ep_index;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700917 unsigned int stream_id;
Sarah Sharpae636742009-04-29 19:02:31 -0700918 struct xhci_ring *ep_ring;
919 struct xhci_virt_device *dev;
John Yound115b042009-07-27 12:05:15 -0700920 struct xhci_ep_ctx *ep_ctx;
921 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpae636742009-04-29 19:02:31 -0700922
Matt Evans28ccd292011-03-29 13:40:46 +1100923 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
924 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
925 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
Sarah Sharpae636742009-04-29 19:02:31 -0700926 dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700927
928 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
929 if (!ep_ring) {
930 xhci_warn(xhci, "WARN Set TR deq ptr command for "
931 "freed stream ID %u\n",
932 stream_id);
933 /* XXX: Harmless??? */
934 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
935 return;
936 }
937
John Yound115b042009-07-27 12:05:15 -0700938 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
939 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
Sarah Sharpae636742009-04-29 19:02:31 -0700940
Matt Evans28ccd292011-03-29 13:40:46 +1100941 if (GET_COMP_CODE(le32_to_cpu(event->status)) != COMP_SUCCESS) {
Sarah Sharpae636742009-04-29 19:02:31 -0700942 unsigned int ep_state;
943 unsigned int slot_state;
944
Matt Evans28ccd292011-03-29 13:40:46 +1100945 switch (GET_COMP_CODE(le32_to_cpu(event->status))) {
Sarah Sharpae636742009-04-29 19:02:31 -0700946 case COMP_TRB_ERR:
947 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
948 "of stream ID configuration\n");
949 break;
950 case COMP_CTX_STATE:
951 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
952 "to incorrect slot or ep state.\n");
Matt Evans28ccd292011-03-29 13:40:46 +1100953 ep_state = le32_to_cpu(ep_ctx->ep_info);
Sarah Sharpae636742009-04-29 19:02:31 -0700954 ep_state &= EP_STATE_MASK;
Matt Evans28ccd292011-03-29 13:40:46 +1100955 slot_state = le32_to_cpu(slot_ctx->dev_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700956 slot_state = GET_SLOT_STATE(slot_state);
957 xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
958 slot_state, ep_state);
959 break;
960 case COMP_EBADSLT:
961 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
962 "slot %u was not enabled.\n", slot_id);
963 break;
964 default:
965 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
966 "completion code of %u.\n",
Matt Evans28ccd292011-03-29 13:40:46 +1100967 GET_COMP_CODE(le32_to_cpu(event->status)));
Sarah Sharpae636742009-04-29 19:02:31 -0700968 break;
969 }
970 /* OK what do we do now? The endpoint state is hosed, and we
971 * should never get to this point if the synchronization between
972 * queueing, and endpoint state are correct. This might happen
973 * if the device gets disconnected after we've finished
974 * cancelling URBs, which might not be an error...
975 */
976 } else {
Sarah Sharp8e595a52009-07-27 12:03:31 -0700977 xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
Matt Evans28ccd292011-03-29 13:40:46 +1100978 le64_to_cpu(ep_ctx->deq));
Sarah Sharpbf161e82011-02-23 15:46:42 -0800979 if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg,
Matt Evans28ccd292011-03-29 13:40:46 +1100980 dev->eps[ep_index].queued_deq_ptr) ==
981 (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) {
Sarah Sharpbf161e82011-02-23 15:46:42 -0800982 /* Update the ring's dequeue segment and dequeue pointer
983 * to reflect the new position.
984 */
Andiry Xub008df62012-03-05 17:49:34 +0800985 update_ring_for_set_deq_completion(xhci, dev,
986 ep_ring, ep_index);
Sarah Sharpbf161e82011-02-23 15:46:42 -0800987 } else {
988 xhci_warn(xhci, "Mismatch between completed Set TR Deq "
989 "Ptr command & xHCI internal state.\n");
990 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
991 dev->eps[ep_index].queued_deq_seg,
992 dev->eps[ep_index].queued_deq_ptr);
993 }
Sarah Sharpae636742009-04-29 19:02:31 -0700994 }
995
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700996 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
Sarah Sharpbf161e82011-02-23 15:46:42 -0800997 dev->eps[ep_index].queued_deq_seg = NULL;
998 dev->eps[ep_index].queued_deq_ptr = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700999 /* Restart any rings with pending URBs */
1000 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07001001}
1002
Sarah Sharpa1587d92009-07-27 12:03:15 -07001003static void handle_reset_ep_completion(struct xhci_hcd *xhci,
1004 struct xhci_event_cmd *event,
1005 union xhci_trb *trb)
1006{
1007 int slot_id;
1008 unsigned int ep_index;
1009
Matt Evans28ccd292011-03-29 13:40:46 +11001010 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
1011 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharpa1587d92009-07-27 12:03:15 -07001012 /* This command will only fail if the endpoint wasn't halted,
1013 * but we don't care.
1014 */
1015 xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
Matt Evansf5960b62011-06-01 10:22:55 +10001016 GET_COMP_CODE(le32_to_cpu(event->status)));
Sarah Sharpa1587d92009-07-27 12:03:15 -07001017
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001018 /* HW with the reset endpoint quirk needs to have a configure endpoint
1019 * command complete before the endpoint can be used. Queue that here
1020 * because the HW can't handle two commands being queued in a row.
1021 */
1022 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
1023 xhci_dbg(xhci, "Queueing configure endpoint command\n");
1024 xhci_queue_configure_endpoint(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001025 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1026 false);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001027 xhci_ring_cmd_db(xhci);
1028 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001029 /* Clear our internal halted state and restart the ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001030 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001031 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001032 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001033}
Sarah Sharpae636742009-04-29 19:02:31 -07001034
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001035/* Check to see if a command in the device's command queue matches this one.
1036 * Signal the completion or free the command, and return 1. Return 0 if the
1037 * completed command isn't at the head of the command list.
1038 */
1039static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
1040 struct xhci_virt_device *virt_dev,
1041 struct xhci_event_cmd *event)
1042{
1043 struct xhci_command *command;
1044
1045 if (list_empty(&virt_dev->cmd_list))
1046 return 0;
1047
1048 command = list_entry(virt_dev->cmd_list.next,
1049 struct xhci_command, cmd_list);
1050 if (xhci->cmd_ring->dequeue != command->command_trb)
1051 return 0;
1052
Matt Evans28ccd292011-03-29 13:40:46 +11001053 command->status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001054 list_del(&command->cmd_list);
1055 if (command->completion)
1056 complete(command->completion);
1057 else
1058 xhci_free_command(xhci, command);
1059 return 1;
1060}
1061
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001062static void handle_cmd_completion(struct xhci_hcd *xhci,
1063 struct xhci_event_cmd *event)
1064{
Matt Evans28ccd292011-03-29 13:40:46 +11001065 int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001066 u64 cmd_dma;
1067 dma_addr_t cmd_dequeue_dma;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001068 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001069 struct xhci_virt_device *virt_dev;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001070 unsigned int ep_index;
1071 struct xhci_ring *ep_ring;
1072 unsigned int ep_state;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001073
Matt Evans28ccd292011-03-29 13:40:46 +11001074 cmd_dma = le64_to_cpu(event->cmd_trb);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001075 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001076 xhci->cmd_ring->dequeue);
1077 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1078 if (cmd_dequeue_dma == 0) {
1079 xhci->error_bitmask |= 1 << 4;
1080 return;
1081 }
1082 /* Does the DMA address match our internal dequeue pointer address? */
1083 if (cmd_dma != (u64) cmd_dequeue_dma) {
1084 xhci->error_bitmask |= 1 << 5;
1085 return;
1086 }
Matt Evans28ccd292011-03-29 13:40:46 +11001087 switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])
1088 & TRB_TYPE_BITMASK) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001089 case TRB_TYPE(TRB_ENABLE_SLOT):
Matt Evans28ccd292011-03-29 13:40:46 +11001090 if (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_SUCCESS)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001091 xhci->slot_id = slot_id;
1092 else
1093 xhci->slot_id = 0;
1094 complete(&xhci->addr_dev);
1095 break;
1096 case TRB_TYPE(TRB_DISABLE_SLOT):
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001097 if (xhci->devs[slot_id]) {
1098 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1099 /* Delete default control endpoint resources */
1100 xhci_free_device_endpoint_resources(xhci,
1101 xhci->devs[slot_id], true);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001102 xhci_free_virt_device(xhci, slot_id);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001103 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001104 break;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001105 case TRB_TYPE(TRB_CONFIG_EP):
Sarah Sharp913a8a32009-09-04 10:53:13 -07001106 virt_dev = xhci->devs[slot_id];
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001107 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
Sarah Sharp913a8a32009-09-04 10:53:13 -07001108 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001109 /*
1110 * Configure endpoint commands can come from the USB core
1111 * configuration or alt setting changes, or because the HW
1112 * needed an extra configure endpoint command after a reset
Sarah Sharp8df75f42010-04-02 15:34:16 -07001113 * endpoint command or streams were being configured.
1114 * If the command was for a halted endpoint, the xHCI driver
1115 * is not waiting on the configure endpoint command.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001116 */
1117 ctrl_ctx = xhci_get_input_control_ctx(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001118 virt_dev->in_ctx);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001119 /* Input ctx add_flags are the endpoint index plus one */
Matt Evans28ccd292011-03-29 13:40:46 +11001120 ep_index = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags)) - 1;
Sarah Sharp06df5722009-12-03 09:44:31 -08001121 /* A usb_set_interface() call directly after clearing a halted
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001122 * condition may race on this quirky hardware. Not worth
1123 * worrying about, since this is prototype hardware. Not sure
1124 * if this will work for streams, but streams support was
1125 * untested on this prototype.
Sarah Sharp06df5722009-12-03 09:44:31 -08001126 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001127 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
Sarah Sharp06df5722009-12-03 09:44:31 -08001128 ep_index != (unsigned int) -1 &&
Matt Evans28ccd292011-03-29 13:40:46 +11001129 le32_to_cpu(ctrl_ctx->add_flags) - SLOT_FLAG ==
1130 le32_to_cpu(ctrl_ctx->drop_flags)) {
Sarah Sharp06df5722009-12-03 09:44:31 -08001131 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
1132 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1133 if (!(ep_state & EP_HALTED))
1134 goto bandwidth_change;
1135 xhci_dbg(xhci, "Completed config ep cmd - "
1136 "last ep index = %d, state = %d\n",
1137 ep_index, ep_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001138 /* Clear internal halted state and restart ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001139 xhci->devs[slot_id]->eps[ep_index].ep_state &=
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001140 ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001141 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharp06df5722009-12-03 09:44:31 -08001142 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001143 }
Sarah Sharp06df5722009-12-03 09:44:31 -08001144bandwidth_change:
1145 xhci_dbg(xhci, "Completed config ep cmd\n");
1146 xhci->devs[slot_id]->cmd_status =
Matt Evans28ccd292011-03-29 13:40:46 +11001147 GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp06df5722009-12-03 09:44:31 -08001148 complete(&xhci->devs[slot_id]->cmd_completion);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001149 break;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001150 case TRB_TYPE(TRB_EVAL_CONTEXT):
Sarah Sharpac1c1b72009-09-04 10:53:20 -07001151 virt_dev = xhci->devs[slot_id];
1152 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
1153 break;
Matt Evans28ccd292011-03-29 13:40:46 +11001154 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001155 complete(&xhci->devs[slot_id]->cmd_completion);
1156 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001157 case TRB_TYPE(TRB_ADDR_DEV):
Matt Evans28ccd292011-03-29 13:40:46 +11001158 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001159 complete(&xhci->addr_dev);
1160 break;
Sarah Sharpae636742009-04-29 19:02:31 -07001161 case TRB_TYPE(TRB_STOP_RING):
Andiry Xube88fe42010-10-14 07:22:57 -07001162 handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event);
Sarah Sharpae636742009-04-29 19:02:31 -07001163 break;
1164 case TRB_TYPE(TRB_SET_DEQ):
1165 handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
1166 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001167 case TRB_TYPE(TRB_CMD_NOOP):
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001168 break;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001169 case TRB_TYPE(TRB_RESET_EP):
1170 handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
1171 break;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001172 case TRB_TYPE(TRB_RESET_DEV):
1173 xhci_dbg(xhci, "Completed reset device command.\n");
1174 slot_id = TRB_TO_SLOT_ID(
Matt Evans28ccd292011-03-29 13:40:46 +11001175 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001176 virt_dev = xhci->devs[slot_id];
1177 if (virt_dev)
1178 handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
1179 else
1180 xhci_warn(xhci, "Reset device command completion "
1181 "for disabled slot %u\n", slot_id);
1182 break;
Sarah Sharp02386342010-05-24 13:25:28 -07001183 case TRB_TYPE(TRB_NEC_GET_FW):
1184 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1185 xhci->error_bitmask |= 1 << 6;
1186 break;
1187 }
1188 xhci_dbg(xhci, "NEC firmware version %2x.%02x\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001189 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1190 NEC_FW_MINOR(le32_to_cpu(event->status)));
Sarah Sharp02386342010-05-24 13:25:28 -07001191 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001192 default:
1193 /* Skip over unknown commands on the event ring */
1194 xhci->error_bitmask |= 1 << 6;
1195 break;
1196 }
Andiry Xu3b72fca2012-03-05 17:49:32 +08001197 inc_deq(xhci, xhci->cmd_ring);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001198}
1199
Sarah Sharp02386342010-05-24 13:25:28 -07001200static void handle_vendor_event(struct xhci_hcd *xhci,
1201 union xhci_trb *event)
1202{
1203 u32 trb_type;
1204
Matt Evans28ccd292011-03-29 13:40:46 +11001205 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
Sarah Sharp02386342010-05-24 13:25:28 -07001206 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1207 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1208 handle_cmd_completion(xhci, &event->event_cmd);
1209}
1210
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001211/* @port_id: the one-based port ID from the hardware (indexed from array of all
1212 * port registers -- USB 3.0 and USB 2.0).
1213 *
1214 * Returns a zero-based port number, which is suitable for indexing into each of
1215 * the split roothubs' port arrays and bus state arrays.
Sarah Sharpd0cd5d42011-11-14 17:51:39 -08001216 * Add one to it in order to call xhci_find_slot_id_by_port.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001217 */
1218static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1219 struct xhci_hcd *xhci, u32 port_id)
1220{
1221 unsigned int i;
1222 unsigned int num_similar_speed_ports = 0;
1223
1224 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1225 * and usb2_ports are 0-based indexes. Count the number of similar
1226 * speed ports, up to 1 port before this port.
1227 */
1228 for (i = 0; i < (port_id - 1); i++) {
1229 u8 port_speed = xhci->port_array[i];
1230
1231 /*
1232 * Skip ports that don't have known speeds, or have duplicate
1233 * Extended Capabilities port speed entries.
1234 */
Dan Carpenter22e04872011-03-17 22:39:49 +03001235 if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001236 continue;
1237
1238 /*
1239 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1240 * 1.1 ports are under the USB 2.0 hub. If the port speed
1241 * matches the device speed, it's a similar speed port.
1242 */
1243 if ((port_speed == 0x03) == (hcd->speed == HCD_USB3))
1244 num_similar_speed_ports++;
1245 }
1246 return num_similar_speed_ports;
1247}
1248
Sarah Sharp623bef92011-11-11 14:57:33 -08001249static void handle_device_notification(struct xhci_hcd *xhci,
1250 union xhci_trb *event)
1251{
1252 u32 slot_id;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001253 struct usb_device *udev;
Sarah Sharp623bef92011-11-11 14:57:33 -08001254
1255 slot_id = TRB_TO_SLOT_ID(event->generic.field[3]);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001256 if (!xhci->devs[slot_id]) {
Sarah Sharp623bef92011-11-11 14:57:33 -08001257 xhci_warn(xhci, "Device Notification event for "
1258 "unused slot %u\n", slot_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001259 return;
1260 }
1261
1262 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
1263 slot_id);
1264 udev = xhci->devs[slot_id]->udev;
1265 if (udev && udev->parent)
1266 usb_wakeup_notification(udev->parent, udev->portnum);
Sarah Sharp623bef92011-11-11 14:57:33 -08001267}
1268
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001269static void handle_port_status(struct xhci_hcd *xhci,
1270 union xhci_trb *event)
1271{
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001272 struct usb_hcd *hcd;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001273 u32 port_id;
Andiry Xu56192532010-10-14 07:23:00 -07001274 u32 temp, temp1;
Sarah Sharp518e8482010-12-15 11:56:29 -08001275 int max_ports;
Andiry Xu56192532010-10-14 07:23:00 -07001276 int slot_id;
Sarah Sharp5308a912010-12-01 11:34:59 -08001277 unsigned int faked_port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001278 u8 major_revision;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001279 struct xhci_bus_state *bus_state;
Matt Evans28ccd292011-03-29 13:40:46 +11001280 __le32 __iomem **port_array;
Sarah Sharp386139d2011-03-24 08:02:58 -07001281 bool bogus_port_status = false;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001282
1283 /* Port status change events always have a successful completion code */
Matt Evans28ccd292011-03-29 13:40:46 +11001284 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001285 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1286 xhci->error_bitmask |= 1 << 8;
1287 }
Matt Evans28ccd292011-03-29 13:40:46 +11001288 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001289 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1290
Sarah Sharp518e8482010-12-15 11:56:29 -08001291 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1292 if ((port_id <= 0) || (port_id > max_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001293 xhci_warn(xhci, "Invalid port id %d\n", port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001294 bogus_port_status = true;
Andiry Xu56192532010-10-14 07:23:00 -07001295 goto cleanup;
1296 }
1297
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001298 /* Figure out which usb_hcd this port is attached to:
1299 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1300 */
1301 major_revision = xhci->port_array[port_id - 1];
1302 if (major_revision == 0) {
1303 xhci_warn(xhci, "Event for port %u not in "
1304 "Extended Capabilities, ignoring.\n",
1305 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001306 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001307 goto cleanup;
1308 }
Dan Carpenter22e04872011-03-17 22:39:49 +03001309 if (major_revision == DUPLICATE_ENTRY) {
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001310 xhci_warn(xhci, "Event for port %u duplicated in"
1311 "Extended Capabilities, ignoring.\n",
1312 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001313 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001314 goto cleanup;
Sarah Sharp5308a912010-12-01 11:34:59 -08001315 }
1316
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001317 /*
1318 * Hardware port IDs reported by a Port Status Change Event include USB
1319 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1320 * resume event, but we first need to translate the hardware port ID
1321 * into the index into the ports on the correct split roothub, and the
1322 * correct bus_state structure.
1323 */
1324 /* Find the right roothub. */
1325 hcd = xhci_to_hcd(xhci);
1326 if ((major_revision == 0x03) != (hcd->speed == HCD_USB3))
1327 hcd = xhci->shared_hcd;
1328 bus_state = &xhci->bus_state[hcd_index(hcd)];
1329 if (hcd->speed == HCD_USB3)
1330 port_array = xhci->usb3_ports;
1331 else
1332 port_array = xhci->usb2_ports;
1333 /* Find the faked port hub number */
1334 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1335 port_id);
1336
Sarah Sharp5308a912010-12-01 11:34:59 -08001337 temp = xhci_readl(xhci, port_array[faked_port_index]);
Sarah Sharp7111ebc2010-12-14 13:24:55 -08001338 if (hcd->state == HC_STATE_SUSPENDED) {
Andiry Xu56192532010-10-14 07:23:00 -07001339 xhci_dbg(xhci, "resume root hub\n");
1340 usb_hcd_resume_root_hub(hcd);
1341 }
1342
1343 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1344 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1345
1346 temp1 = xhci_readl(xhci, &xhci->op_regs->command);
1347 if (!(temp1 & CMD_RUN)) {
1348 xhci_warn(xhci, "xHC is not running.\n");
1349 goto cleanup;
1350 }
1351
1352 if (DEV_SUPERSPEED(temp)) {
Sarah Sharpd93814c2012-01-24 16:39:02 -08001353 xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001354 /* Set a flag to say the port signaled remote wakeup,
1355 * so we can tell the difference between the end of
1356 * device and host initiated resume.
1357 */
1358 bus_state->port_remote_wakeup |= 1 << faked_port_index;
Sarah Sharpd93814c2012-01-24 16:39:02 -08001359 xhci_test_and_clear_bit(xhci, port_array,
1360 faked_port_index, PORT_PLC);
Andiry Xuc9682df2011-09-23 14:19:48 -07001361 xhci_set_link_state(xhci, port_array, faked_port_index,
1362 XDEV_U0);
Sarah Sharpd93814c2012-01-24 16:39:02 -08001363 /* Need to wait until the next link state change
1364 * indicates the device is actually in U0.
1365 */
1366 bogus_port_status = true;
1367 goto cleanup;
Andiry Xu56192532010-10-14 07:23:00 -07001368 } else {
1369 xhci_dbg(xhci, "resume HS port %d\n", port_id);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001370 bus_state->resume_done[faked_port_index] = jiffies +
Andiry Xu56192532010-10-14 07:23:00 -07001371 msecs_to_jiffies(20);
1372 mod_timer(&hcd->rh_timer,
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001373 bus_state->resume_done[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001374 /* Do the rest in GetPortStatus */
1375 }
1376 }
1377
Sarah Sharpd93814c2012-01-24 16:39:02 -08001378 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_U0 &&
1379 DEV_SUPERSPEED(temp)) {
1380 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001381 /* We've just brought the device into U0 through either the
1382 * Resume state after a device remote wakeup, or through the
1383 * U3Exit state after a host-initiated resume. If it's a device
1384 * initiated remote wake, don't pass up the link state change,
1385 * so the roothub behavior is consistent with external
1386 * USB 3.0 hub behavior.
1387 */
Sarah Sharpd93814c2012-01-24 16:39:02 -08001388 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1389 faked_port_index + 1);
1390 if (slot_id && xhci->devs[slot_id])
1391 xhci_ring_device(xhci, slot_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001392 if (bus_state->port_remote_wakeup && (1 << faked_port_index)) {
1393 bus_state->port_remote_wakeup &=
1394 ~(1 << faked_port_index);
1395 xhci_test_and_clear_bit(xhci, port_array,
1396 faked_port_index, PORT_PLC);
1397 usb_wakeup_notification(hcd->self.root_hub,
1398 faked_port_index + 1);
1399 bogus_port_status = true;
1400 goto cleanup;
1401 }
Sarah Sharpd93814c2012-01-24 16:39:02 -08001402 }
1403
Andiry Xu6fd45622011-09-23 14:19:50 -07001404 if (hcd->speed != HCD_USB3)
1405 xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
1406 PORT_PLC);
1407
Andiry Xu56192532010-10-14 07:23:00 -07001408cleanup:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001409 /* Update event ring dequeue pointer before dropping the lock */
Andiry Xu3b72fca2012-03-05 17:49:32 +08001410 inc_deq(xhci, xhci->event_ring);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001411
Sarah Sharp386139d2011-03-24 08:02:58 -07001412 /* Don't make the USB core poll the roothub if we got a bad port status
1413 * change event. Besides, at that point we can't tell which roothub
1414 * (USB 2.0 or USB 3.0) to kick.
1415 */
1416 if (bogus_port_status)
1417 return;
1418
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001419 spin_unlock(&xhci->lock);
1420 /* Pass this up to the core */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001421 usb_hcd_poll_rh_status(hcd);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001422 spin_lock(&xhci->lock);
1423}
1424
1425/*
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001426 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1427 * at end_trb, which may be in another segment. If the suspect DMA address is a
1428 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1429 * returns 0.
1430 */
Sarah Sharp6648f292009-11-09 13:35:23 -08001431struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001432 union xhci_trb *start_trb,
1433 union xhci_trb *end_trb,
1434 dma_addr_t suspect_dma)
1435{
1436 dma_addr_t start_dma;
1437 dma_addr_t end_seg_dma;
1438 dma_addr_t end_trb_dma;
1439 struct xhci_segment *cur_seg;
1440
Sarah Sharp23e3be12009-04-29 19:05:20 -07001441 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001442 cur_seg = start_seg;
1443
1444 do {
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001445 if (start_dma == 0)
Randy Dunlap326b4812010-04-19 08:53:50 -07001446 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -07001447 /* We may get an event for a Link TRB in the middle of a TD */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001448 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001449 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001450 /* If the end TRB isn't in this segment, this is set to 0 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001451 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001452
1453 if (end_trb_dma > 0) {
1454 /* The end TRB is in this segment, so suspect should be here */
1455 if (start_dma <= end_trb_dma) {
1456 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1457 return cur_seg;
1458 } else {
1459 /* Case for one segment with
1460 * a TD wrapped around to the top
1461 */
1462 if ((suspect_dma >= start_dma &&
1463 suspect_dma <= end_seg_dma) ||
1464 (suspect_dma >= cur_seg->dma &&
1465 suspect_dma <= end_trb_dma))
1466 return cur_seg;
1467 }
Randy Dunlap326b4812010-04-19 08:53:50 -07001468 return NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001469 } else {
1470 /* Might still be somewhere in this segment */
1471 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1472 return cur_seg;
1473 }
1474 cur_seg = cur_seg->next;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001475 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001476 } while (cur_seg != start_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001477
Randy Dunlap326b4812010-04-19 08:53:50 -07001478 return NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001479}
1480
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001481static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1482 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001483 unsigned int stream_id,
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001484 struct xhci_td *td, union xhci_trb *event_trb)
1485{
1486 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
1487 ep->ep_state |= EP_HALTED;
1488 ep->stopped_td = td;
1489 ep->stopped_trb = event_trb;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001490 ep->stopped_stream = stream_id;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001491
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001492 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1493 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
Sarah Sharp1624ae12010-05-06 13:40:08 -07001494
1495 ep->stopped_td = NULL;
1496 ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001497 ep->stopped_stream = 0;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001498
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001499 xhci_ring_cmd_db(xhci);
1500}
1501
1502/* Check if an error has halted the endpoint ring. The class driver will
1503 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1504 * However, a babble and other errors also halt the endpoint ring, and the class
1505 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1506 * Ring Dequeue Pointer command manually.
1507 */
1508static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1509 struct xhci_ep_ctx *ep_ctx,
1510 unsigned int trb_comp_code)
1511{
1512 /* TRB completion codes that may require a manual halt cleanup */
1513 if (trb_comp_code == COMP_TX_ERR ||
1514 trb_comp_code == COMP_BABBLE ||
1515 trb_comp_code == COMP_SPLIT_ERR)
1516 /* The 0.96 spec says a babbling control endpoint
1517 * is not halted. The 0.96 spec says it is. Some HW
1518 * claims to be 0.95 compliant, but it halts the control
1519 * endpoint anyway. Check if a babble halted the
1520 * endpoint.
1521 */
Matt Evansf5960b62011-06-01 10:22:55 +10001522 if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1523 cpu_to_le32(EP_STATE_HALTED))
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001524 return 1;
1525
1526 return 0;
1527}
1528
Sarah Sharpb45b5062009-12-09 15:59:06 -08001529int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1530{
1531 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1532 /* Vendor defined "informational" completion code,
1533 * treat as not-an-error.
1534 */
1535 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1536 trb_comp_code);
1537 xhci_dbg(xhci, "Treating code as success.\n");
1538 return 1;
1539 }
1540 return 0;
1541}
1542
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001543/*
Andiry Xu4422da62010-07-22 15:22:55 -07001544 * Finish the td processing, remove the td from td list;
1545 * Return 1 if the urb can be given back.
1546 */
1547static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1548 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1549 struct xhci_virt_ep *ep, int *status, bool skip)
1550{
1551 struct xhci_virt_device *xdev;
1552 struct xhci_ring *ep_ring;
1553 unsigned int slot_id;
1554 int ep_index;
1555 struct urb *urb = NULL;
1556 struct xhci_ep_ctx *ep_ctx;
1557 int ret = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001558 struct urb_priv *urb_priv;
Andiry Xu4422da62010-07-22 15:22:55 -07001559 u32 trb_comp_code;
1560
Matt Evans28ccd292011-03-29 13:40:46 +11001561 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu4422da62010-07-22 15:22:55 -07001562 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001563 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1564 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu4422da62010-07-22 15:22:55 -07001565 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001566 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu4422da62010-07-22 15:22:55 -07001567
1568 if (skip)
1569 goto td_cleanup;
1570
1571 if (trb_comp_code == COMP_STOP_INVAL ||
1572 trb_comp_code == COMP_STOP) {
1573 /* The Endpoint Stop Command completion will take care of any
1574 * stopped TDs. A stopped TD may be restarted, so don't update
1575 * the ring dequeue pointer or take this TD off any lists yet.
1576 */
1577 ep->stopped_td = td;
1578 ep->stopped_trb = event_trb;
1579 return 0;
1580 } else {
1581 if (trb_comp_code == COMP_STALL) {
1582 /* The transfer is completed from the driver's
1583 * perspective, but we need to issue a set dequeue
1584 * command for this stalled endpoint to move the dequeue
1585 * pointer past the TD. We can't do that here because
1586 * the halt condition must be cleared first. Let the
1587 * USB class driver clear the stall later.
1588 */
1589 ep->stopped_td = td;
1590 ep->stopped_trb = event_trb;
1591 ep->stopped_stream = ep_ring->stream_id;
1592 } else if (xhci_requires_manual_halt_cleanup(xhci,
1593 ep_ctx, trb_comp_code)) {
1594 /* Other types of errors halt the endpoint, but the
1595 * class driver doesn't call usb_reset_endpoint() unless
1596 * the error is -EPIPE. Clear the halted status in the
1597 * xHCI hardware manually.
1598 */
1599 xhci_cleanup_halted_endpoint(xhci,
1600 slot_id, ep_index, ep_ring->stream_id,
1601 td, event_trb);
1602 } else {
1603 /* Update ring dequeue pointer */
1604 while (ep_ring->dequeue != td->last_trb)
Andiry Xu3b72fca2012-03-05 17:49:32 +08001605 inc_deq(xhci, ep_ring);
1606 inc_deq(xhci, ep_ring);
Andiry Xu4422da62010-07-22 15:22:55 -07001607 }
1608
1609td_cleanup:
1610 /* Clean up the endpoint's TD list */
1611 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001612 urb_priv = urb->hcpriv;
Andiry Xu4422da62010-07-22 15:22:55 -07001613
1614 /* Do one last check of the actual transfer length.
1615 * If the host controller said we transferred more data than
1616 * the buffer length, urb->actual_length will be a very big
1617 * number (since it's unsigned). Play it safe and say we didn't
1618 * transfer anything.
1619 */
1620 if (urb->actual_length > urb->transfer_buffer_length) {
1621 xhci_warn(xhci, "URB transfer length is wrong, "
1622 "xHC issue? req. len = %u, "
1623 "act. len = %u\n",
1624 urb->transfer_buffer_length,
1625 urb->actual_length);
1626 urb->actual_length = 0;
1627 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1628 *status = -EREMOTEIO;
1629 else
1630 *status = 0;
1631 }
Sarah Sharp585df1d2011-08-02 15:43:40 -07001632 list_del_init(&td->td_list);
Andiry Xu4422da62010-07-22 15:22:55 -07001633 /* Was this TD slated to be cancelled but completed anyway? */
1634 if (!list_empty(&td->cancelled_td_list))
Sarah Sharp585df1d2011-08-02 15:43:40 -07001635 list_del_init(&td->cancelled_td_list);
Andiry Xu4422da62010-07-22 15:22:55 -07001636
Andiry Xu8e51adc2010-07-22 15:23:31 -07001637 urb_priv->td_cnt++;
1638 /* Giveback the urb when all the tds are completed */
Andiry Xuc41136b2011-03-22 17:08:14 +08001639 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001640 ret = 1;
Andiry Xuc41136b2011-03-22 17:08:14 +08001641 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1642 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
1643 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs
1644 == 0) {
1645 if (xhci->quirks & XHCI_AMD_PLL_FIX)
1646 usb_amd_quirk_pll_enable();
1647 }
1648 }
1649 }
Andiry Xu4422da62010-07-22 15:22:55 -07001650 }
1651
1652 return ret;
1653}
1654
1655/*
Andiry Xu8af56be2010-07-22 15:23:03 -07001656 * Process control tds, update urb status and actual_length.
1657 */
1658static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1659 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1660 struct xhci_virt_ep *ep, int *status)
1661{
1662 struct xhci_virt_device *xdev;
1663 struct xhci_ring *ep_ring;
1664 unsigned int slot_id;
1665 int ep_index;
1666 struct xhci_ep_ctx *ep_ctx;
1667 u32 trb_comp_code;
1668
Matt Evans28ccd292011-03-29 13:40:46 +11001669 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu8af56be2010-07-22 15:23:03 -07001670 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001671 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1672 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu8af56be2010-07-22 15:23:03 -07001673 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001674 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001675
Andiry Xu8af56be2010-07-22 15:23:03 -07001676 switch (trb_comp_code) {
1677 case COMP_SUCCESS:
1678 if (event_trb == ep_ring->dequeue) {
1679 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1680 "without IOC set??\n");
1681 *status = -ESHUTDOWN;
1682 } else if (event_trb != td->last_trb) {
1683 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1684 "without IOC set??\n");
1685 *status = -ESHUTDOWN;
1686 } else {
Andiry Xu8af56be2010-07-22 15:23:03 -07001687 *status = 0;
1688 }
1689 break;
1690 case COMP_SHORT_TX:
Andiry Xu8af56be2010-07-22 15:23:03 -07001691 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1692 *status = -EREMOTEIO;
1693 else
1694 *status = 0;
1695 break;
Sarah Sharp3abeca92011-05-05 19:08:09 -07001696 case COMP_STOP_INVAL:
1697 case COMP_STOP:
1698 return finish_td(xhci, td, event_trb, event, ep, status, false);
Andiry Xu8af56be2010-07-22 15:23:03 -07001699 default:
1700 if (!xhci_requires_manual_halt_cleanup(xhci,
1701 ep_ctx, trb_comp_code))
1702 break;
1703 xhci_dbg(xhci, "TRB error code %u, "
1704 "halted endpoint index = %u\n",
1705 trb_comp_code, ep_index);
1706 /* else fall through */
1707 case COMP_STALL:
1708 /* Did we transfer part of the data (middle) phase? */
1709 if (event_trb != ep_ring->dequeue &&
1710 event_trb != td->last_trb)
1711 td->urb->actual_length =
1712 td->urb->transfer_buffer_length
Matt Evans28ccd292011-03-29 13:40:46 +11001713 - TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001714 else
1715 td->urb->actual_length = 0;
1716
1717 xhci_cleanup_halted_endpoint(xhci,
1718 slot_id, ep_index, 0, td, event_trb);
1719 return finish_td(xhci, td, event_trb, event, ep, status, true);
1720 }
1721 /*
1722 * Did we transfer any data, despite the errors that might have
1723 * happened? I.e. did we get past the setup stage?
1724 */
1725 if (event_trb != ep_ring->dequeue) {
1726 /* The event was for the status stage */
1727 if (event_trb == td->last_trb) {
1728 if (td->urb->actual_length != 0) {
1729 /* Don't overwrite a previously set error code
1730 */
1731 if ((*status == -EINPROGRESS || *status == 0) &&
1732 (td->urb->transfer_flags
1733 & URB_SHORT_NOT_OK))
1734 /* Did we already see a short data
1735 * stage? */
1736 *status = -EREMOTEIO;
1737 } else {
1738 td->urb->actual_length =
1739 td->urb->transfer_buffer_length;
1740 }
1741 } else {
1742 /* Maybe the event was for the data stage? */
Sarah Sharp3abeca92011-05-05 19:08:09 -07001743 td->urb->actual_length =
1744 td->urb->transfer_buffer_length -
1745 TRB_LEN(le32_to_cpu(event->transfer_len));
1746 xhci_dbg(xhci, "Waiting for status "
1747 "stage event\n");
1748 return 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07001749 }
1750 }
1751
1752 return finish_td(xhci, td, event_trb, event, ep, status, false);
1753}
1754
1755/*
Andiry Xu04e51902010-07-22 15:23:39 -07001756 * Process isochronous tds, update urb packet status and actual_length.
1757 */
1758static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1759 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1760 struct xhci_virt_ep *ep, int *status)
1761{
1762 struct xhci_ring *ep_ring;
1763 struct urb_priv *urb_priv;
1764 int idx;
1765 int len = 0;
Andiry Xu04e51902010-07-22 15:23:39 -07001766 union xhci_trb *cur_trb;
1767 struct xhci_segment *cur_seg;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001768 struct usb_iso_packet_descriptor *frame;
Andiry Xu04e51902010-07-22 15:23:39 -07001769 u32 trb_comp_code;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001770 bool skip_td = false;
Andiry Xu04e51902010-07-22 15:23:39 -07001771
Matt Evans28ccd292011-03-29 13:40:46 +11001772 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1773 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07001774 urb_priv = td->urb->hcpriv;
1775 idx = urb_priv->td_cnt;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001776 frame = &td->urb->iso_frame_desc[idx];
Andiry Xu04e51902010-07-22 15:23:39 -07001777
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001778 /* handle completion code */
1779 switch (trb_comp_code) {
1780 case COMP_SUCCESS:
1781 frame->status = 0;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001782 break;
1783 case COMP_SHORT_TX:
1784 frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
1785 -EREMOTEIO : 0;
1786 break;
1787 case COMP_BW_OVER:
1788 frame->status = -ECOMM;
1789 skip_td = true;
1790 break;
1791 case COMP_BUFF_OVER:
1792 case COMP_BABBLE:
1793 frame->status = -EOVERFLOW;
1794 skip_td = true;
1795 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001796 case COMP_DEV_ERR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001797 case COMP_STALL:
1798 frame->status = -EPROTO;
1799 skip_td = true;
1800 break;
1801 case COMP_STOP:
1802 case COMP_STOP_INVAL:
1803 break;
1804 default:
1805 frame->status = -1;
1806 break;
Andiry Xu04e51902010-07-22 15:23:39 -07001807 }
1808
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001809 if (trb_comp_code == COMP_SUCCESS || skip_td) {
1810 frame->actual_length = frame->length;
1811 td->urb->actual_length += frame->length;
Andiry Xu04e51902010-07-22 15:23:39 -07001812 } else {
1813 for (cur_trb = ep_ring->dequeue,
1814 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
1815 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +10001816 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
1817 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
Matt Evans28ccd292011-03-29 13:40:46 +11001818 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu04e51902010-07-22 15:23:39 -07001819 }
Matt Evans28ccd292011-03-29 13:40:46 +11001820 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1821 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07001822
1823 if (trb_comp_code != COMP_STOP_INVAL) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001824 frame->actual_length = len;
Andiry Xu04e51902010-07-22 15:23:39 -07001825 td->urb->actual_length += len;
1826 }
1827 }
1828
Andiry Xu04e51902010-07-22 15:23:39 -07001829 return finish_td(xhci, td, event_trb, event, ep, status, false);
1830}
1831
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001832static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1833 struct xhci_transfer_event *event,
1834 struct xhci_virt_ep *ep, int *status)
1835{
1836 struct xhci_ring *ep_ring;
1837 struct urb_priv *urb_priv;
1838 struct usb_iso_packet_descriptor *frame;
1839 int idx;
1840
Matt Evansf6975312011-06-01 13:01:01 +10001841 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001842 urb_priv = td->urb->hcpriv;
1843 idx = urb_priv->td_cnt;
1844 frame = &td->urb->iso_frame_desc[idx];
1845
Sarah Sharpb3df3f92011-06-15 19:57:46 -07001846 /* The transfer is partly done. */
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001847 frame->status = -EXDEV;
1848
1849 /* calc actual length */
1850 frame->actual_length = 0;
1851
1852 /* Update ring dequeue pointer */
1853 while (ep_ring->dequeue != td->last_trb)
Andiry Xu3b72fca2012-03-05 17:49:32 +08001854 inc_deq(xhci, ep_ring);
1855 inc_deq(xhci, ep_ring);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001856
1857 return finish_td(xhci, td, NULL, event, ep, status, true);
1858}
1859
Andiry Xu04e51902010-07-22 15:23:39 -07001860/*
Andiry Xu22405ed2010-07-22 15:23:08 -07001861 * Process bulk and interrupt tds, update urb status and actual_length.
1862 */
1863static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
1864 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1865 struct xhci_virt_ep *ep, int *status)
1866{
1867 struct xhci_ring *ep_ring;
1868 union xhci_trb *cur_trb;
1869 struct xhci_segment *cur_seg;
1870 u32 trb_comp_code;
1871
Matt Evans28ccd292011-03-29 13:40:46 +11001872 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1873 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001874
1875 switch (trb_comp_code) {
1876 case COMP_SUCCESS:
1877 /* Double check that the HW transferred everything. */
1878 if (event_trb != td->last_trb) {
1879 xhci_warn(xhci, "WARN Successful completion "
1880 "on short TX\n");
1881 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1882 *status = -EREMOTEIO;
1883 else
1884 *status = 0;
1885 } else {
Andiry Xu22405ed2010-07-22 15:23:08 -07001886 *status = 0;
1887 }
1888 break;
1889 case COMP_SHORT_TX:
1890 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1891 *status = -EREMOTEIO;
1892 else
1893 *status = 0;
1894 break;
1895 default:
1896 /* Others already handled above */
1897 break;
1898 }
Sarah Sharpf444ff22011-04-05 15:53:47 -07001899 if (trb_comp_code == COMP_SHORT_TX)
1900 xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
1901 "%d bytes untransferred\n",
1902 td->urb->ep->desc.bEndpointAddress,
1903 td->urb->transfer_buffer_length,
1904 TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07001905 /* Fast path - was this the last TRB in the TD for this URB? */
1906 if (event_trb == td->last_trb) {
Matt Evans28ccd292011-03-29 13:40:46 +11001907 if (TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
Andiry Xu22405ed2010-07-22 15:23:08 -07001908 td->urb->actual_length =
1909 td->urb->transfer_buffer_length -
Matt Evans28ccd292011-03-29 13:40:46 +11001910 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001911 if (td->urb->transfer_buffer_length <
1912 td->urb->actual_length) {
1913 xhci_warn(xhci, "HC gave bad length "
1914 "of %d bytes left\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001915 TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07001916 td->urb->actual_length = 0;
1917 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1918 *status = -EREMOTEIO;
1919 else
1920 *status = 0;
1921 }
1922 /* Don't overwrite a previously set error code */
1923 if (*status == -EINPROGRESS) {
1924 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1925 *status = -EREMOTEIO;
1926 else
1927 *status = 0;
1928 }
1929 } else {
1930 td->urb->actual_length =
1931 td->urb->transfer_buffer_length;
1932 /* Ignore a short packet completion if the
1933 * untransferred length was zero.
1934 */
1935 if (*status == -EREMOTEIO)
1936 *status = 0;
1937 }
1938 } else {
1939 /* Slow path - walk the list, starting from the dequeue
1940 * pointer, to get the actual length transferred.
1941 */
1942 td->urb->actual_length = 0;
1943 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
1944 cur_trb != event_trb;
1945 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +10001946 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
1947 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
Andiry Xu22405ed2010-07-22 15:23:08 -07001948 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11001949 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu22405ed2010-07-22 15:23:08 -07001950 }
1951 /* If the ring didn't stop on a Link or No-op TRB, add
1952 * in the actual bytes transferred from the Normal TRB
1953 */
1954 if (trb_comp_code != COMP_STOP_INVAL)
1955 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11001956 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1957 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001958 }
1959
1960 return finish_td(xhci, td, event_trb, event, ep, status, false);
1961}
1962
1963/*
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001964 * If this function returns an error condition, it means it got a Transfer
1965 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
1966 * At this point, the host controller is probably hosed and should be reset.
1967 */
1968static int handle_tx_event(struct xhci_hcd *xhci,
1969 struct xhci_transfer_event *event)
1970{
1971 struct xhci_virt_device *xdev;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001972 struct xhci_virt_ep *ep;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001973 struct xhci_ring *ep_ring;
Sarah Sharp82d10092009-08-07 14:04:52 -07001974 unsigned int slot_id;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001975 int ep_index;
Randy Dunlap326b4812010-04-19 08:53:50 -07001976 struct xhci_td *td = NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001977 dma_addr_t event_dma;
1978 struct xhci_segment *event_seg;
1979 union xhci_trb *event_trb;
Randy Dunlap326b4812010-04-19 08:53:50 -07001980 struct urb *urb = NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001981 int status = -EINPROGRESS;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001982 struct urb_priv *urb_priv;
John Yound115b042009-07-27 12:05:15 -07001983 struct xhci_ep_ctx *ep_ctx;
Andiry Xuc2d7b492011-09-19 16:05:12 -07001984 struct list_head *tmp;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07001985 u32 trb_comp_code;
Andiry Xu4422da62010-07-22 15:22:55 -07001986 int ret = 0;
Andiry Xuc2d7b492011-09-19 16:05:12 -07001987 int td_num = 0;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001988
Matt Evans28ccd292011-03-29 13:40:46 +11001989 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp82d10092009-08-07 14:04:52 -07001990 xdev = xhci->devs[slot_id];
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001991 if (!xdev) {
1992 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
Sarah Sharp9258c0b2011-12-01 14:50:30 -08001993 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
Sarah Sharpe910b442012-01-04 16:54:12 -08001994 (unsigned long long) xhci_trb_virt_to_dma(
1995 xhci->event_ring->deq_seg,
Sarah Sharp9258c0b2011-12-01 14:50:30 -08001996 xhci->event_ring->dequeue),
1997 lower_32_bits(le64_to_cpu(event->buffer)),
1998 upper_32_bits(le64_to_cpu(event->buffer)),
1999 le32_to_cpu(event->transfer_len),
2000 le32_to_cpu(event->flags));
2001 xhci_dbg(xhci, "Event ring:\n");
2002 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002003 return -ENODEV;
2004 }
2005
2006 /* Endpoint ID is 1 based, our index is zero based */
Matt Evans28ccd292011-03-29 13:40:46 +11002007 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002008 ep = &xdev->eps[ep_index];
Matt Evans28ccd292011-03-29 13:40:46 +11002009 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
John Yound115b042009-07-27 12:05:15 -07002010 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002011 if (!ep_ring ||
Matt Evans28ccd292011-03-29 13:40:46 +11002012 (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
2013 EP_STATE_DISABLED) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002014 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
2015 "or incorrect stream ring\n");
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002016 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
Sarah Sharpe910b442012-01-04 16:54:12 -08002017 (unsigned long long) xhci_trb_virt_to_dma(
2018 xhci->event_ring->deq_seg,
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002019 xhci->event_ring->dequeue),
2020 lower_32_bits(le64_to_cpu(event->buffer)),
2021 upper_32_bits(le64_to_cpu(event->buffer)),
2022 le32_to_cpu(event->transfer_len),
2023 le32_to_cpu(event->flags));
2024 xhci_dbg(xhci, "Event ring:\n");
2025 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002026 return -ENODEV;
2027 }
2028
Andiry Xuc2d7b492011-09-19 16:05:12 -07002029 /* Count current td numbers if ep->skip is set */
2030 if (ep->skip) {
2031 list_for_each(tmp, &ep_ring->td_list)
2032 td_num++;
2033 }
2034
Matt Evans28ccd292011-03-29 13:40:46 +11002035 event_dma = le64_to_cpu(event->buffer);
2036 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu986a92d2010-07-22 15:23:20 -07002037 /* Look for common error cases */
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002038 switch (trb_comp_code) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002039 /* Skip codes that require special handling depending on
2040 * transfer type
2041 */
2042 case COMP_SUCCESS:
2043 case COMP_SHORT_TX:
2044 break;
Sarah Sharpae636742009-04-29 19:02:31 -07002045 case COMP_STOP:
2046 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
2047 break;
2048 case COMP_STOP_INVAL:
2049 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
2050 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002051 case COMP_STALL:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002052 xhci_dbg(xhci, "Stalled endpoint\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002053 ep->ep_state |= EP_HALTED;
Sarah Sharpb10de142009-04-27 19:58:50 -07002054 status = -EPIPE;
2055 break;
2056 case COMP_TRB_ERR:
2057 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
2058 status = -EILSEQ;
2059 break;
Sarah Sharpec74e402009-11-11 10:28:36 -08002060 case COMP_SPLIT_ERR:
Sarah Sharpb10de142009-04-27 19:58:50 -07002061 case COMP_TX_ERR:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002062 xhci_dbg(xhci, "Transfer error on endpoint\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002063 status = -EPROTO;
2064 break;
Sarah Sharp4a731432009-07-27 12:04:32 -07002065 case COMP_BABBLE:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002066 xhci_dbg(xhci, "Babble error on endpoint\n");
Sarah Sharp4a731432009-07-27 12:04:32 -07002067 status = -EOVERFLOW;
2068 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002069 case COMP_DB_ERR:
2070 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
2071 status = -ENOSR;
2072 break;
Andiry Xu986a92d2010-07-22 15:23:20 -07002073 case COMP_BW_OVER:
2074 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
2075 break;
2076 case COMP_BUFF_OVER:
2077 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
2078 break;
2079 case COMP_UNDERRUN:
2080 /*
2081 * When the Isoch ring is empty, the xHC will generate
2082 * a Ring Overrun Event for IN Isoch endpoint or Ring
2083 * Underrun Event for OUT Isoch endpoint.
2084 */
2085 xhci_dbg(xhci, "underrun event on endpoint\n");
2086 if (!list_empty(&ep_ring->td_list))
2087 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2088 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002089 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2090 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002091 goto cleanup;
2092 case COMP_OVERRUN:
2093 xhci_dbg(xhci, "overrun event on endpoint\n");
2094 if (!list_empty(&ep_ring->td_list))
2095 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2096 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002097 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2098 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002099 goto cleanup;
Alex Hef6ba6fe2011-06-08 18:34:06 +08002100 case COMP_DEV_ERR:
2101 xhci_warn(xhci, "WARN: detect an incompatible device");
2102 status = -EPROTO;
2103 break;
Andiry Xud18240d2010-07-22 15:23:25 -07002104 case COMP_MISSED_INT:
2105 /*
2106 * When encounter missed service error, one or more isoc tds
2107 * may be missed by xHC.
2108 * Set skip flag of the ep_ring; Complete the missed tds as
2109 * short transfer when process the ep_ring next time.
2110 */
2111 ep->skip = true;
2112 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
2113 goto cleanup;
Sarah Sharpb10de142009-04-27 19:58:50 -07002114 default:
Sarah Sharpb45b5062009-12-09 15:59:06 -08002115 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
Sarah Sharp5ad6a522009-11-11 10:28:40 -08002116 status = 0;
2117 break;
2118 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002119 xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
2120 "busted\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002121 goto cleanup;
2122 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002123
Andiry Xud18240d2010-07-22 15:23:25 -07002124 do {
2125 /* This TRB should be in the TD at the head of this ring's
2126 * TD list.
2127 */
2128 if (list_empty(&ep_ring->td_list)) {
2129 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
2130 "with no TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002131 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2132 ep_index);
Andiry Xud18240d2010-07-22 15:23:25 -07002133 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
Matt Evansf5960b62011-06-01 10:22:55 +10002134 (le32_to_cpu(event->flags) &
2135 TRB_TYPE_BITMASK)>>10);
Andiry Xud18240d2010-07-22 15:23:25 -07002136 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2137 if (ep->skip) {
2138 ep->skip = false;
2139 xhci_dbg(xhci, "td_list is empty while skip "
2140 "flag set. Clear skip flag.\n");
2141 }
2142 ret = 0;
2143 goto cleanup;
2144 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002145
Andiry Xuc2d7b492011-09-19 16:05:12 -07002146 /* We've skipped all the TDs on the ep ring when ep->skip set */
2147 if (ep->skip && td_num == 0) {
2148 ep->skip = false;
2149 xhci_dbg(xhci, "All tds on the ep_ring skipped. "
2150 "Clear skip flag.\n");
2151 ret = 0;
2152 goto cleanup;
2153 }
2154
Andiry Xud18240d2010-07-22 15:23:25 -07002155 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
Andiry Xuc2d7b492011-09-19 16:05:12 -07002156 if (ep->skip)
2157 td_num--;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002158
Andiry Xud18240d2010-07-22 15:23:25 -07002159 /* Is this a TRB in the currently executing TD? */
2160 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
2161 td->last_trb, event_dma);
Alex Hee1cf4862011-06-03 15:58:25 +08002162
2163 /*
2164 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
2165 * is not in the current TD pointed by ep_ring->dequeue because
2166 * that the hardware dequeue pointer still at the previous TRB
2167 * of the current TD. The previous TRB maybe a Link TD or the
2168 * last TRB of the previous TD. The command completion handle
2169 * will take care the rest.
2170 */
2171 if (!event_seg && trb_comp_code == COMP_STOP_INVAL) {
2172 ret = 0;
2173 goto cleanup;
2174 }
2175
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002176 if (!event_seg) {
2177 if (!ep->skip ||
2178 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
Sarah Sharpad808332011-05-25 10:43:56 -07002179 /* Some host controllers give a spurious
2180 * successful event after a short transfer.
2181 * Ignore it.
2182 */
2183 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
2184 ep_ring->last_td_was_short) {
2185 ep_ring->last_td_was_short = false;
2186 ret = 0;
2187 goto cleanup;
2188 }
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002189 /* HC is busted, give up! */
2190 xhci_err(xhci,
2191 "ERROR Transfer event TRB DMA ptr not "
2192 "part of current TD\n");
2193 return -ESHUTDOWN;
2194 }
2195
2196 ret = skip_isoc_td(xhci, td, event, ep, &status);
2197 goto cleanup;
2198 }
Sarah Sharpad808332011-05-25 10:43:56 -07002199 if (trb_comp_code == COMP_SHORT_TX)
2200 ep_ring->last_td_was_short = true;
2201 else
2202 ep_ring->last_td_was_short = false;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002203
2204 if (ep->skip) {
Andiry Xud18240d2010-07-22 15:23:25 -07002205 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2206 ep->skip = false;
2207 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002208
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002209 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) /
2210 sizeof(*event_trb)];
2211 /*
2212 * No-op TRB should not trigger interrupts.
2213 * If event_trb is a no-op TRB, it means the
2214 * corresponding TD has been cancelled. Just ignore
2215 * the TD.
2216 */
Matt Evansf5960b62011-06-01 10:22:55 +10002217 if (TRB_TYPE_NOOP_LE32(event_trb->generic.field[3])) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002218 xhci_dbg(xhci,
2219 "event_trb is a no-op TRB. Skip it\n");
2220 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07002221 }
2222
2223 /* Now update the urb's actual_length and give back to
2224 * the core
2225 */
2226 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2227 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
2228 &status);
Andiry Xu04e51902010-07-22 15:23:39 -07002229 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2230 ret = process_isoc_td(xhci, td, event_trb, event, ep,
2231 &status);
Andiry Xud18240d2010-07-22 15:23:25 -07002232 else
2233 ret = process_bulk_intr_td(xhci, td, event_trb, event,
2234 ep, &status);
Andiry Xu4422da62010-07-22 15:22:55 -07002235
2236cleanup:
Andiry Xud18240d2010-07-22 15:23:25 -07002237 /*
2238 * Do not update event ring dequeue pointer if ep->skip is set.
2239 * Will roll back to continue process missed tds.
Sarah Sharp82d10092009-08-07 14:04:52 -07002240 */
Andiry Xud18240d2010-07-22 15:23:25 -07002241 if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
Andiry Xu3b72fca2012-03-05 17:49:32 +08002242 inc_deq(xhci, xhci->event_ring);
Andiry Xud18240d2010-07-22 15:23:25 -07002243 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002244
Andiry Xud18240d2010-07-22 15:23:25 -07002245 if (ret) {
2246 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002247 urb_priv = urb->hcpriv;
Andiry Xud18240d2010-07-22 15:23:25 -07002248 /* Leave the TD around for the reset endpoint function
2249 * to use(but only if it's not a control endpoint,
2250 * since we already queued the Set TR dequeue pointer
2251 * command for stalled control endpoints).
2252 */
2253 if (usb_endpoint_xfer_control(&urb->ep->desc) ||
2254 (trb_comp_code != COMP_STALL &&
2255 trb_comp_code != COMP_BABBLE))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002256 xhci_urb_free_priv(xhci, urb_priv);
Andiry Xud18240d2010-07-22 15:23:25 -07002257
Sarah Sharp214f76f2010-10-26 11:22:02 -07002258 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpf444ff22011-04-05 15:53:47 -07002259 if ((urb->actual_length != urb->transfer_buffer_length &&
2260 (urb->transfer_flags &
2261 URB_SHORT_NOT_OK)) ||
Sarah Sharpfd984d22011-09-02 11:05:56 -07002262 (status != 0 &&
2263 !usb_endpoint_xfer_isoc(&urb->ep->desc)))
Sarah Sharpf444ff22011-04-05 15:53:47 -07002264 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
2265 "expected = %x, status = %d\n",
2266 urb, urb->actual_length,
2267 urb->transfer_buffer_length,
2268 status);
Andiry Xud18240d2010-07-22 15:23:25 -07002269 spin_unlock(&xhci->lock);
Sarah Sharpb3df3f92011-06-15 19:57:46 -07002270 /* EHCI, UHCI, and OHCI always unconditionally set the
2271 * urb->status of an isochronous endpoint to 0.
2272 */
2273 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2274 status = 0;
Sarah Sharp214f76f2010-10-26 11:22:02 -07002275 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
Andiry Xud18240d2010-07-22 15:23:25 -07002276 spin_lock(&xhci->lock);
2277 }
2278
2279 /*
2280 * If ep->skip is set, it means there are missed tds on the
2281 * endpoint ring need to take care of.
2282 * Process them as short transfer until reach the td pointed by
2283 * the event.
2284 */
2285 } while (ep->skip && trb_comp_code != COMP_MISSED_INT);
2286
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002287 return 0;
2288}
2289
2290/*
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002291 * This function handles all OS-owned events on the event ring. It may drop
2292 * xhci->lock between event processing (e.g. to pass up port status changes).
Matt Evans9dee9a22011-03-29 13:41:02 +11002293 * Returns >0 for "possibly more events to process" (caller should call again),
2294 * otherwise 0 if done. In future, <0 returns should indicate error code.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002295 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002296static int xhci_handle_event(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002297{
2298 union xhci_trb *event;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002299 int update_ptrs = 1;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002300 int ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002301
2302 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2303 xhci->error_bitmask |= 1 << 1;
Matt Evans9dee9a22011-03-29 13:41:02 +11002304 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002305 }
2306
2307 event = xhci->event_ring->dequeue;
2308 /* Does the HC or OS own the TRB? */
Matt Evans28ccd292011-03-29 13:40:46 +11002309 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
2310 xhci->event_ring->cycle_state) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002311 xhci->error_bitmask |= 1 << 2;
Matt Evans9dee9a22011-03-29 13:41:02 +11002312 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002313 }
2314
Matt Evans92a3da42011-03-29 13:40:51 +11002315 /*
2316 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2317 * speculative reads of the event's flags/data below.
2318 */
2319 rmb();
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002320 /* FIXME: Handle more event types. */
Matt Evans28ccd292011-03-29 13:40:46 +11002321 switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002322 case TRB_TYPE(TRB_COMPLETION):
2323 handle_cmd_completion(xhci, &event->event_cmd);
2324 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002325 case TRB_TYPE(TRB_PORT_STATUS):
2326 handle_port_status(xhci, event);
2327 update_ptrs = 0;
2328 break;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002329 case TRB_TYPE(TRB_TRANSFER):
2330 ret = handle_tx_event(xhci, &event->trans_event);
2331 if (ret < 0)
2332 xhci->error_bitmask |= 1 << 9;
2333 else
2334 update_ptrs = 0;
2335 break;
Sarah Sharp623bef92011-11-11 14:57:33 -08002336 case TRB_TYPE(TRB_DEV_NOTE):
2337 handle_device_notification(xhci, event);
2338 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002339 default:
Matt Evans28ccd292011-03-29 13:40:46 +11002340 if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
2341 TRB_TYPE(48))
Sarah Sharp02386342010-05-24 13:25:28 -07002342 handle_vendor_event(xhci, event);
2343 else
2344 xhci->error_bitmask |= 1 << 3;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002345 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002346 /* Any of the above functions may drop and re-acquire the lock, so check
2347 * to make sure a watchdog timer didn't mark the host as non-responsive.
2348 */
2349 if (xhci->xhc_state & XHCI_STATE_DYING) {
2350 xhci_dbg(xhci, "xHCI host dying, returning from "
2351 "event handler.\n");
Matt Evans9dee9a22011-03-29 13:41:02 +11002352 return 0;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002353 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002354
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002355 if (update_ptrs)
2356 /* Update SW event ring dequeue pointer */
Andiry Xu3b72fca2012-03-05 17:49:32 +08002357 inc_deq(xhci, xhci->event_ring);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002358
Matt Evans9dee9a22011-03-29 13:41:02 +11002359 /* Are there more items on the event ring? Caller will call us again to
2360 * check.
2361 */
2362 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002363}
Sarah Sharp9032cd52010-07-29 22:12:29 -07002364
2365/*
2366 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2367 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2368 * indicators of an event TRB error, but we check the status *first* to be safe.
2369 */
2370irqreturn_t xhci_irq(struct usb_hcd *hcd)
2371{
2372 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002373 u32 status;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002374 union xhci_trb *trb;
Sarah Sharpbda53142010-07-29 22:12:38 -07002375 u64 temp_64;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002376 union xhci_trb *event_ring_deq;
2377 dma_addr_t deq;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002378
2379 spin_lock(&xhci->lock);
2380 trb = xhci->event_ring->dequeue;
2381 /* Check if the xHC generated the interrupt, or the irq is shared */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002382 status = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002383 if (status == 0xffffffff)
Sarah Sharp9032cd52010-07-29 22:12:29 -07002384 goto hw_died;
2385
Sarah Sharpc21599a2010-07-29 22:13:00 -07002386 if (!(status & STS_EINT)) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002387 spin_unlock(&xhci->lock);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002388 return IRQ_NONE;
2389 }
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002390 if (status & STS_FATAL) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002391 xhci_warn(xhci, "WARNING: Host System Error\n");
2392 xhci_halt(xhci);
2393hw_died:
Sarah Sharp9032cd52010-07-29 22:12:29 -07002394 spin_unlock(&xhci->lock);
2395 return -ESHUTDOWN;
2396 }
2397
Sarah Sharpbda53142010-07-29 22:12:38 -07002398 /*
2399 * Clear the op reg interrupt status first,
2400 * so we can receive interrupts from other MSI-X interrupters.
2401 * Write 1 to clear the interrupt status.
2402 */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002403 status |= STS_EINT;
2404 xhci_writel(xhci, status, &xhci->op_regs->status);
Sarah Sharpbda53142010-07-29 22:12:38 -07002405 /* FIXME when MSI-X is supported and there are multiple vectors */
2406 /* Clear the MSI-X event interrupt status */
2407
Felipe Balbicd704692012-02-29 16:46:23 +02002408 if (hcd->irq) {
Sarah Sharpc21599a2010-07-29 22:13:00 -07002409 u32 irq_pending;
2410 /* Acknowledge the PCI interrupt */
2411 irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
2412 irq_pending |= 0x3;
2413 xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
2414 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002415
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002416 if (xhci->xhc_state & XHCI_STATE_DYING) {
Sarah Sharpbda53142010-07-29 22:12:38 -07002417 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2418 "Shouldn't IRQs be disabled?\n");
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002419 /* Clear the event handler busy flag (RW1C);
2420 * the event ring should be empty.
Sarah Sharpbda53142010-07-29 22:12:38 -07002421 */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002422 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2423 xhci_write_64(xhci, temp_64 | ERST_EHB,
2424 &xhci->ir_set->erst_dequeue);
2425 spin_unlock(&xhci->lock);
2426
2427 return IRQ_HANDLED;
2428 }
2429
2430 event_ring_deq = xhci->event_ring->dequeue;
2431 /* FIXME this should be a delayed service routine
2432 * that clears the EHB.
2433 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002434 while (xhci_handle_event(xhci) > 0) {}
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002435
2436 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2437 /* If necessary, update the HW's version of the event ring deq ptr. */
2438 if (event_ring_deq != xhci->event_ring->dequeue) {
2439 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2440 xhci->event_ring->dequeue);
2441 if (deq == 0)
2442 xhci_warn(xhci, "WARN something wrong with SW event "
2443 "ring dequeue ptr.\n");
2444 /* Update HC event ring dequeue pointer */
2445 temp_64 &= ERST_PTR_MASK;
2446 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2447 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002448
2449 /* Clear the event handler busy flag (RW1C); event ring is empty. */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002450 temp_64 |= ERST_EHB;
2451 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2452
Sarah Sharp9032cd52010-07-29 22:12:29 -07002453 spin_unlock(&xhci->lock);
2454
2455 return IRQ_HANDLED;
2456}
2457
2458irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2459{
Alan Stern968b8222011-11-03 12:03:38 -04002460 return xhci_irq(hcd);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002461}
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002462
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002463/**** Endpoint Ring Operations ****/
2464
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002465/*
2466 * Generic function for queueing a TRB on a ring.
2467 * The caller must have checked to make sure there's room on the ring.
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002468 *
2469 * @more_trbs_coming: Will you enqueue more TRBs before calling
2470 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002471 */
2472static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002473 bool more_trbs_coming,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002474 u32 field1, u32 field2, u32 field3, u32 field4)
2475{
2476 struct xhci_generic_trb *trb;
2477
2478 trb = &ring->enqueue->generic;
Matt Evans28ccd292011-03-29 13:40:46 +11002479 trb->field[0] = cpu_to_le32(field1);
2480 trb->field[1] = cpu_to_le32(field2);
2481 trb->field[2] = cpu_to_le32(field3);
2482 trb->field[3] = cpu_to_le32(field4);
Andiry Xu3b72fca2012-03-05 17:49:32 +08002483 inc_enq(xhci, ring, more_trbs_coming);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002484}
2485
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002486/*
2487 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2488 * FIXME allocate segments if the ring is full.
2489 */
2490static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002491 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002492{
2493 /* Make sure the endpoint has been added to xHC schedule */
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002494 switch (ep_state) {
2495 case EP_STATE_DISABLED:
2496 /*
2497 * USB core changed config/interfaces without notifying us,
2498 * or hardware is reporting the wrong state.
2499 */
2500 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2501 return -ENOENT;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002502 case EP_STATE_ERROR:
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002503 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002504 /* FIXME event handling code for error needs to clear it */
2505 /* XXX not sure if this should be -ENOENT or not */
2506 return -EINVAL;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002507 case EP_STATE_HALTED:
2508 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002509 case EP_STATE_STOPPED:
2510 case EP_STATE_RUNNING:
2511 break;
2512 default:
2513 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2514 /*
2515 * FIXME issue Configure Endpoint command to try to get the HC
2516 * back into a known state.
2517 */
2518 return -EINVAL;
2519 }
2520 if (!room_on_ring(xhci, ep_ring, num_trbs)) {
2521 /* FIXME allocate more room */
2522 xhci_err(xhci, "ERROR no room on ep ring\n");
2523 return -ENOMEM;
2524 }
John Youn6c12db92010-05-10 15:33:00 -07002525
2526 if (enqueue_is_link_trb(ep_ring)) {
2527 struct xhci_ring *ring = ep_ring;
2528 union xhci_trb *next;
John Youn6c12db92010-05-10 15:33:00 -07002529
John Youn6c12db92010-05-10 15:33:00 -07002530 next = ring->enqueue;
2531
2532 while (last_trb(xhci, ring, ring->enq_seg, next)) {
Andiry Xu7e393a82011-09-23 14:19:54 -07002533 /* If we're not dealing with 0.95 hardware or isoc rings
2534 * on AMD 0.96 host, clear the chain bit.
John Youn6c12db92010-05-10 15:33:00 -07002535 */
Andiry Xu3b72fca2012-03-05 17:49:32 +08002536 if (!xhci_link_trb_quirk(xhci) &&
2537 !(ring->type == TYPE_ISOC &&
2538 (xhci->quirks & XHCI_AMD_0x96_HOST)))
Matt Evans28ccd292011-03-29 13:40:46 +11002539 next->link.control &= cpu_to_le32(~TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002540 else
Matt Evans28ccd292011-03-29 13:40:46 +11002541 next->link.control |= cpu_to_le32(TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002542
2543 wmb();
Matt Evansf5960b62011-06-01 10:22:55 +10002544 next->link.control ^= cpu_to_le32(TRB_CYCLE);
John Youn6c12db92010-05-10 15:33:00 -07002545
2546 /* Toggle the cycle bit after the last ring segment. */
2547 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
2548 ring->cycle_state = (ring->cycle_state ? 0 : 1);
John Youn6c12db92010-05-10 15:33:00 -07002549 }
2550 ring->enq_seg = ring->enq_seg->next;
2551 ring->enqueue = ring->enq_seg->trbs;
2552 next = ring->enqueue;
2553 }
2554 }
2555
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002556 return 0;
2557}
2558
Sarah Sharp23e3be12009-04-29 19:05:20 -07002559static int prepare_transfer(struct xhci_hcd *xhci,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002560 struct xhci_virt_device *xdev,
2561 unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002562 unsigned int stream_id,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002563 unsigned int num_trbs,
2564 struct urb *urb,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002565 unsigned int td_index,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002566 gfp_t mem_flags)
2567{
2568 int ret;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002569 struct urb_priv *urb_priv;
2570 struct xhci_td *td;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002571 struct xhci_ring *ep_ring;
John Yound115b042009-07-27 12:05:15 -07002572 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002573
2574 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2575 if (!ep_ring) {
2576 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2577 stream_id);
2578 return -EINVAL;
2579 }
2580
2581 ret = prepare_ring(xhci, ep_ring,
Matt Evans28ccd292011-03-29 13:40:46 +11002582 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002583 num_trbs, mem_flags);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002584 if (ret)
2585 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002586
Andiry Xu8e51adc2010-07-22 15:23:31 -07002587 urb_priv = urb->hcpriv;
2588 td = urb_priv->td[td_index];
2589
2590 INIT_LIST_HEAD(&td->td_list);
2591 INIT_LIST_HEAD(&td->cancelled_td_list);
2592
2593 if (td_index == 0) {
Sarah Sharp214f76f2010-10-26 11:22:02 -07002594 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07002595 if (unlikely(ret))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002596 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002597 }
2598
Andiry Xu8e51adc2010-07-22 15:23:31 -07002599 td->urb = urb;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002600 /* Add this TD to the tail of the endpoint ring's TD list */
Andiry Xu8e51adc2010-07-22 15:23:31 -07002601 list_add_tail(&td->td_list, &ep_ring->td_list);
2602 td->start_seg = ep_ring->enq_seg;
2603 td->first_trb = ep_ring->enqueue;
2604
2605 urb_priv->td[td_index] = td;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002606
2607 return 0;
2608}
2609
Sarah Sharp23e3be12009-04-29 19:05:20 -07002610static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002611{
2612 int num_sgs, num_trbs, running_total, temp, i;
2613 struct scatterlist *sg;
2614
2615 sg = NULL;
Clemens Ladischbc677d52011-12-03 23:41:31 +01002616 num_sgs = urb->num_mapped_sgs;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002617 temp = urb->transfer_buffer_length;
2618
Sarah Sharp8a96c052009-04-27 19:59:19 -07002619 num_trbs = 0;
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002620 for_each_sg(urb->sg, sg, num_sgs, i) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002621 unsigned int len = sg_dma_len(sg);
2622
2623 /* Scatter gather list entries may cross 64KB boundaries */
2624 running_total = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002625 (sg_dma_address(sg) & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08002626 running_total &= TRB_MAX_BUFF_SIZE - 1;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002627 if (running_total != 0)
2628 num_trbs++;
2629
2630 /* How many more 64KB chunks to transfer, how many more TRBs? */
Paul Zimmermanbcd2fde2011-02-12 14:07:57 -08002631 while (running_total < sg_dma_len(sg) && running_total < temp) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002632 num_trbs++;
2633 running_total += TRB_MAX_BUFF_SIZE;
2634 }
Sarah Sharp8a96c052009-04-27 19:59:19 -07002635 len = min_t(int, len, temp);
2636 temp -= len;
2637 if (temp == 0)
2638 break;
2639 }
Sarah Sharp8a96c052009-04-27 19:59:19 -07002640 return num_trbs;
2641}
2642
Sarah Sharp23e3be12009-04-29 19:05:20 -07002643static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002644{
2645 if (num_trbs != 0)
Paul Zimmermana2490182011-02-12 14:06:44 -08002646 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
Sarah Sharp8a96c052009-04-27 19:59:19 -07002647 "TRBs, %d left\n", __func__,
2648 urb->ep->desc.bEndpointAddress, num_trbs);
2649 if (running_total != urb->transfer_buffer_length)
Paul Zimmermana2490182011-02-12 14:06:44 -08002650 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
Sarah Sharp8a96c052009-04-27 19:59:19 -07002651 "queued %#x (%d), asked for %#x (%d)\n",
2652 __func__,
2653 urb->ep->desc.bEndpointAddress,
2654 running_total, running_total,
2655 urb->transfer_buffer_length,
2656 urb->transfer_buffer_length);
2657}
2658
Sarah Sharp23e3be12009-04-29 19:05:20 -07002659static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002660 unsigned int ep_index, unsigned int stream_id, int start_cycle,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002661 struct xhci_generic_trb *start_trb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002662{
Sarah Sharp8a96c052009-04-27 19:59:19 -07002663 /*
2664 * Pass all the TRBs to the hardware at once and make sure this write
2665 * isn't reordered.
2666 */
2667 wmb();
Andiry Xu50f7b522010-12-20 15:09:34 +08002668 if (start_cycle)
Matt Evans28ccd292011-03-29 13:40:46 +11002669 start_trb->field[3] |= cpu_to_le32(start_cycle);
Andiry Xu50f7b522010-12-20 15:09:34 +08002670 else
Matt Evans28ccd292011-03-29 13:40:46 +11002671 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
Andiry Xube88fe42010-10-14 07:22:57 -07002672 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002673}
2674
Sarah Sharp624defa2009-09-02 12:14:28 -07002675/*
2676 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
2677 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
2678 * (comprised of sg list entries) can take several service intervals to
2679 * transmit.
2680 */
2681int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2682 struct urb *urb, int slot_id, unsigned int ep_index)
2683{
2684 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
2685 xhci->devs[slot_id]->out_ctx, ep_index);
2686 int xhci_interval;
2687 int ep_interval;
2688
Matt Evans28ccd292011-03-29 13:40:46 +11002689 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Sarah Sharp624defa2009-09-02 12:14:28 -07002690 ep_interval = urb->interval;
2691 /* Convert to microframes */
2692 if (urb->dev->speed == USB_SPEED_LOW ||
2693 urb->dev->speed == USB_SPEED_FULL)
2694 ep_interval *= 8;
2695 /* FIXME change this to a warning and a suggestion to use the new API
2696 * to set the polling interval (once the API is added).
2697 */
2698 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08002699 if (printk_ratelimit())
Sarah Sharp624defa2009-09-02 12:14:28 -07002700 dev_dbg(&urb->dev->dev, "Driver uses different interval"
2701 " (%d microframe%s) than xHCI "
2702 "(%d microframe%s)\n",
2703 ep_interval,
2704 ep_interval == 1 ? "" : "s",
2705 xhci_interval,
2706 xhci_interval == 1 ? "" : "s");
2707 urb->interval = xhci_interval;
2708 /* Convert back to frames for LS/FS devices */
2709 if (urb->dev->speed == USB_SPEED_LOW ||
2710 urb->dev->speed == USB_SPEED_FULL)
2711 urb->interval /= 8;
2712 }
2713 return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
2714}
2715
Sarah Sharp04dd9502009-11-11 10:28:30 -08002716/*
2717 * The TD size is the number of bytes remaining in the TD (including this TRB),
2718 * right shifted by 10.
2719 * It must fit in bits 21:17, so it can't be bigger than 31.
2720 */
2721static u32 xhci_td_remainder(unsigned int remainder)
2722{
2723 u32 max = (1 << (21 - 17 + 1)) - 1;
2724
2725 if ((remainder >> 10) >= max)
2726 return max << 17;
2727 else
2728 return (remainder >> 10) << 17;
2729}
2730
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002731/*
2732 * For xHCI 1.0 host controllers, TD size is the number of packets remaining in
2733 * the TD (*not* including this TRB).
2734 *
2735 * Total TD packet count = total_packet_count =
2736 * roundup(TD size in bytes / wMaxPacketSize)
2737 *
2738 * Packets transferred up to and including this TRB = packets_transferred =
2739 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
2740 *
2741 * TD size = total_packet_count - packets_transferred
2742 *
2743 * It must fit in bits 21:17, so it can't be bigger than 31.
2744 */
2745
2746static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
2747 unsigned int total_packet_count, struct urb *urb)
2748{
2749 int packets_transferred;
2750
Sarah Sharp48df4a62011-08-12 10:23:01 -07002751 /* One TRB with a zero-length data packet. */
2752 if (running_total == 0 && trb_buff_len == 0)
2753 return 0;
2754
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002755 /* All the TRB queueing functions don't count the current TRB in
2756 * running_total.
2757 */
2758 packets_transferred = (running_total + trb_buff_len) /
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002759 usb_endpoint_maxp(&urb->ep->desc);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002760
2761 return xhci_td_remainder(total_packet_count - packets_transferred);
2762}
2763
Sarah Sharp23e3be12009-04-29 19:05:20 -07002764static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002765 struct urb *urb, int slot_id, unsigned int ep_index)
2766{
2767 struct xhci_ring *ep_ring;
2768 unsigned int num_trbs;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002769 struct urb_priv *urb_priv;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002770 struct xhci_td *td;
2771 struct scatterlist *sg;
2772 int num_sgs;
2773 int trb_buff_len, this_sg_len, running_total;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002774 unsigned int total_packet_count;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002775 bool first_trb;
2776 u64 addr;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002777 bool more_trbs_coming;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002778
2779 struct xhci_generic_trb *start_trb;
2780 int start_cycle;
2781
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002782 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2783 if (!ep_ring)
2784 return -EINVAL;
2785
Sarah Sharp8a96c052009-04-27 19:59:19 -07002786 num_trbs = count_sg_trbs_needed(xhci, urb);
Clemens Ladischbc677d52011-12-03 23:41:31 +01002787 num_sgs = urb->num_mapped_sgs;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002788 total_packet_count = roundup(urb->transfer_buffer_length,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002789 usb_endpoint_maxp(&urb->ep->desc));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002790
Sarah Sharp23e3be12009-04-29 19:05:20 -07002791 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002792 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002793 num_trbs, urb, 0, mem_flags);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002794 if (trb_buff_len < 0)
2795 return trb_buff_len;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002796
2797 urb_priv = urb->hcpriv;
2798 td = urb_priv->td[0];
2799
Sarah Sharp8a96c052009-04-27 19:59:19 -07002800 /*
2801 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2802 * until we've finished creating all the other TRBs. The ring's cycle
2803 * state may change as we enqueue the other TRBs, so save it too.
2804 */
2805 start_trb = &ep_ring->enqueue->generic;
2806 start_cycle = ep_ring->cycle_state;
2807
2808 running_total = 0;
2809 /*
2810 * How much data is in the first TRB?
2811 *
2812 * There are three forces at work for TRB buffer pointers and lengths:
2813 * 1. We don't want to walk off the end of this sg-list entry buffer.
2814 * 2. The transfer length that the driver requested may be smaller than
2815 * the amount of memory allocated for this scatter-gather list.
2816 * 3. TRBs buffers can't cross 64KB boundaries.
2817 */
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002818 sg = urb->sg;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002819 addr = (u64) sg_dma_address(sg);
2820 this_sg_len = sg_dma_len(sg);
Paul Zimmermana2490182011-02-12 14:06:44 -08002821 trb_buff_len = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002822 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2823 if (trb_buff_len > urb->transfer_buffer_length)
2824 trb_buff_len = urb->transfer_buffer_length;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002825
2826 first_trb = true;
2827 /* Queue the first TRB, even if it's zero-length */
2828 do {
2829 u32 field = 0;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002830 u32 length_field = 0;
Sarah Sharp04dd9502009-11-11 10:28:30 -08002831 u32 remainder = 0;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002832
2833 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002834 if (first_trb) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002835 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002836 if (start_cycle == 0)
2837 field |= 0x1;
2838 } else
Sarah Sharp8a96c052009-04-27 19:59:19 -07002839 field |= ep_ring->cycle_state;
2840
2841 /* Chain all the TRBs together; clear the chain bit in the last
2842 * TRB to indicate it's the last TRB in the chain.
2843 */
2844 if (num_trbs > 1) {
2845 field |= TRB_CHAIN;
2846 } else {
2847 /* FIXME - add check for ZERO_PACKET flag before this */
2848 td->last_trb = ep_ring->enqueue;
2849 field |= TRB_IOC;
2850 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002851
2852 /* Only set interrupt on short packet for IN endpoints */
2853 if (usb_urb_dir_in(urb))
2854 field |= TRB_ISP;
2855
Sarah Sharp8a96c052009-04-27 19:59:19 -07002856 if (TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002857 (addr & (TRB_MAX_BUFF_SIZE - 1)) < trb_buff_len) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002858 xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
2859 xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
2860 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2861 (unsigned int) addr + trb_buff_len);
2862 }
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002863
2864 /* Set the TRB length, TD size, and interrupter fields. */
2865 if (xhci->hci_version < 0x100) {
2866 remainder = xhci_td_remainder(
2867 urb->transfer_buffer_length -
2868 running_total);
2869 } else {
2870 remainder = xhci_v1_0_td_remainder(running_total,
2871 trb_buff_len, total_packet_count, urb);
2872 }
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002873 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002874 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002875 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002876
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002877 if (num_trbs > 1)
2878 more_trbs_coming = true;
2879 else
2880 more_trbs_coming = false;
Andiry Xu3b72fca2012-03-05 17:49:32 +08002881 queue_trb(xhci, ep_ring, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002882 lower_32_bits(addr),
2883 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002884 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002885 field | TRB_TYPE(TRB_NORMAL));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002886 --num_trbs;
2887 running_total += trb_buff_len;
2888
2889 /* Calculate length for next transfer --
2890 * Are we done queueing all the TRBs for this sg entry?
2891 */
2892 this_sg_len -= trb_buff_len;
2893 if (this_sg_len == 0) {
2894 --num_sgs;
2895 if (num_sgs == 0)
2896 break;
2897 sg = sg_next(sg);
2898 addr = (u64) sg_dma_address(sg);
2899 this_sg_len = sg_dma_len(sg);
2900 } else {
2901 addr += trb_buff_len;
2902 }
2903
2904 trb_buff_len = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002905 (addr & (TRB_MAX_BUFF_SIZE - 1));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002906 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2907 if (running_total + trb_buff_len > urb->transfer_buffer_length)
2908 trb_buff_len =
2909 urb->transfer_buffer_length - running_total;
2910 } while (running_total < urb->transfer_buffer_length);
2911
2912 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002913 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002914 start_cycle, start_trb);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002915 return 0;
2916}
2917
Sarah Sharpb10de142009-04-27 19:58:50 -07002918/* This is very similar to what ehci-q.c qtd_fill() does */
Sarah Sharp23e3be12009-04-29 19:05:20 -07002919int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpb10de142009-04-27 19:58:50 -07002920 struct urb *urb, int slot_id, unsigned int ep_index)
2921{
2922 struct xhci_ring *ep_ring;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002923 struct urb_priv *urb_priv;
Sarah Sharpb10de142009-04-27 19:58:50 -07002924 struct xhci_td *td;
2925 int num_trbs;
2926 struct xhci_generic_trb *start_trb;
2927 bool first_trb;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002928 bool more_trbs_coming;
Sarah Sharpb10de142009-04-27 19:58:50 -07002929 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002930 u32 field, length_field;
Sarah Sharpb10de142009-04-27 19:58:50 -07002931
2932 int running_total, trb_buff_len, ret;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002933 unsigned int total_packet_count;
Sarah Sharpb10de142009-04-27 19:58:50 -07002934 u64 addr;
2935
Alan Sternff9c8952010-04-02 13:27:28 -04002936 if (urb->num_sgs)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002937 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
2938
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002939 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2940 if (!ep_ring)
2941 return -EINVAL;
Sarah Sharpb10de142009-04-27 19:58:50 -07002942
2943 num_trbs = 0;
2944 /* How much data is (potentially) left before the 64KB boundary? */
2945 running_total = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002946 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08002947 running_total &= TRB_MAX_BUFF_SIZE - 1;
Sarah Sharpb10de142009-04-27 19:58:50 -07002948
2949 /* If there's some data on this 64KB chunk, or we have to send a
2950 * zero-length transfer, we need at least one TRB
2951 */
2952 if (running_total != 0 || urb->transfer_buffer_length == 0)
2953 num_trbs++;
2954 /* How many more 64KB chunks to transfer, how many more TRBs? */
2955 while (running_total < urb->transfer_buffer_length) {
2956 num_trbs++;
2957 running_total += TRB_MAX_BUFF_SIZE;
2958 }
2959 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
2960
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002961 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2962 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002963 num_trbs, urb, 0, mem_flags);
Sarah Sharpb10de142009-04-27 19:58:50 -07002964 if (ret < 0)
2965 return ret;
2966
Andiry Xu8e51adc2010-07-22 15:23:31 -07002967 urb_priv = urb->hcpriv;
2968 td = urb_priv->td[0];
2969
Sarah Sharpb10de142009-04-27 19:58:50 -07002970 /*
2971 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2972 * until we've finished creating all the other TRBs. The ring's cycle
2973 * state may change as we enqueue the other TRBs, so save it too.
2974 */
2975 start_trb = &ep_ring->enqueue->generic;
2976 start_cycle = ep_ring->cycle_state;
2977
2978 running_total = 0;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002979 total_packet_count = roundup(urb->transfer_buffer_length,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002980 usb_endpoint_maxp(&urb->ep->desc));
Sarah Sharpb10de142009-04-27 19:58:50 -07002981 /* How much data is in the first TRB? */
2982 addr = (u64) urb->transfer_dma;
2983 trb_buff_len = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002984 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
2985 if (trb_buff_len > urb->transfer_buffer_length)
Sarah Sharpb10de142009-04-27 19:58:50 -07002986 trb_buff_len = urb->transfer_buffer_length;
2987
2988 first_trb = true;
2989
2990 /* Queue the first TRB, even if it's zero-length */
2991 do {
Sarah Sharp04dd9502009-11-11 10:28:30 -08002992 u32 remainder = 0;
Sarah Sharpb10de142009-04-27 19:58:50 -07002993 field = 0;
2994
2995 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002996 if (first_trb) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002997 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002998 if (start_cycle == 0)
2999 field |= 0x1;
3000 } else
Sarah Sharpb10de142009-04-27 19:58:50 -07003001 field |= ep_ring->cycle_state;
3002
3003 /* Chain all the TRBs together; clear the chain bit in the last
3004 * TRB to indicate it's the last TRB in the chain.
3005 */
3006 if (num_trbs > 1) {
3007 field |= TRB_CHAIN;
3008 } else {
3009 /* FIXME - add check for ZERO_PACKET flag before this */
3010 td->last_trb = ep_ring->enqueue;
3011 field |= TRB_IOC;
3012 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003013
3014 /* Only set interrupt on short packet for IN endpoints */
3015 if (usb_urb_dir_in(urb))
3016 field |= TRB_ISP;
3017
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003018 /* Set the TRB length, TD size, and interrupter fields. */
3019 if (xhci->hci_version < 0x100) {
3020 remainder = xhci_td_remainder(
3021 urb->transfer_buffer_length -
3022 running_total);
3023 } else {
3024 remainder = xhci_v1_0_td_remainder(running_total,
3025 trb_buff_len, total_packet_count, urb);
3026 }
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003027 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08003028 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003029 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003030
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003031 if (num_trbs > 1)
3032 more_trbs_coming = true;
3033 else
3034 more_trbs_coming = false;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003035 queue_trb(xhci, ep_ring, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07003036 lower_32_bits(addr),
3037 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003038 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003039 field | TRB_TYPE(TRB_NORMAL));
Sarah Sharpb10de142009-04-27 19:58:50 -07003040 --num_trbs;
3041 running_total += trb_buff_len;
3042
3043 /* Calculate length for next transfer */
3044 addr += trb_buff_len;
3045 trb_buff_len = urb->transfer_buffer_length - running_total;
3046 if (trb_buff_len > TRB_MAX_BUFF_SIZE)
3047 trb_buff_len = TRB_MAX_BUFF_SIZE;
3048 } while (running_total < urb->transfer_buffer_length);
3049
Sarah Sharp8a96c052009-04-27 19:59:19 -07003050 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003051 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003052 start_cycle, start_trb);
Sarah Sharpb10de142009-04-27 19:58:50 -07003053 return 0;
3054}
3055
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003056/* Caller must have locked xhci->lock */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003057int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003058 struct urb *urb, int slot_id, unsigned int ep_index)
3059{
3060 struct xhci_ring *ep_ring;
3061 int num_trbs;
3062 int ret;
3063 struct usb_ctrlrequest *setup;
3064 struct xhci_generic_trb *start_trb;
3065 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003066 u32 field, length_field;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003067 struct urb_priv *urb_priv;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003068 struct xhci_td *td;
3069
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003070 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
3071 if (!ep_ring)
3072 return -EINVAL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003073
3074 /*
3075 * Need to copy setup packet into setup TRB, so we can't use the setup
3076 * DMA address.
3077 */
3078 if (!urb->setup_packet)
3079 return -EINVAL;
3080
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003081 /* 1 TRB for setup, 1 for status */
3082 num_trbs = 2;
3083 /*
3084 * Don't need to check if we need additional event data and normal TRBs,
3085 * since data in control transfers will never get bigger than 16MB
3086 * XXX: can we get a buffer that crosses 64KB boundaries?
3087 */
3088 if (urb->transfer_buffer_length > 0)
3089 num_trbs++;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003090 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3091 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003092 num_trbs, urb, 0, mem_flags);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003093 if (ret < 0)
3094 return ret;
3095
Andiry Xu8e51adc2010-07-22 15:23:31 -07003096 urb_priv = urb->hcpriv;
3097 td = urb_priv->td[0];
3098
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003099 /*
3100 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3101 * until we've finished creating all the other TRBs. The ring's cycle
3102 * state may change as we enqueue the other TRBs, so save it too.
3103 */
3104 start_trb = &ep_ring->enqueue->generic;
3105 start_cycle = ep_ring->cycle_state;
3106
3107 /* Queue setup TRB - see section 6.4.1.2.1 */
3108 /* FIXME better way to translate setup_packet into two u32 fields? */
3109 setup = (struct usb_ctrlrequest *) urb->setup_packet;
Andiry Xu50f7b522010-12-20 15:09:34 +08003110 field = 0;
3111 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3112 if (start_cycle == 0)
3113 field |= 0x1;
Andiry Xub83cdc82011-05-05 18:13:56 +08003114
3115 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
3116 if (xhci->hci_version == 0x100) {
3117 if (urb->transfer_buffer_length > 0) {
3118 if (setup->bRequestType & USB_DIR_IN)
3119 field |= TRB_TX_TYPE(TRB_DATA_IN);
3120 else
3121 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3122 }
3123 }
3124
Andiry Xu3b72fca2012-03-05 17:49:32 +08003125 queue_trb(xhci, ep_ring, true,
Matt Evans28ccd292011-03-29 13:40:46 +11003126 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3127 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3128 TRB_LEN(8) | TRB_INTR_TARGET(0),
3129 /* Immediate data in pointer */
3130 field);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003131
3132 /* If there's data, queue data TRBs */
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003133 /* Only set interrupt on short packet for IN endpoints */
3134 if (usb_urb_dir_in(urb))
3135 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3136 else
3137 field = TRB_TYPE(TRB_DATA);
3138
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003139 length_field = TRB_LEN(urb->transfer_buffer_length) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08003140 xhci_td_remainder(urb->transfer_buffer_length) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003141 TRB_INTR_TARGET(0);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003142 if (urb->transfer_buffer_length > 0) {
3143 if (setup->bRequestType & USB_DIR_IN)
3144 field |= TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003145 queue_trb(xhci, ep_ring, true,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003146 lower_32_bits(urb->transfer_dma),
3147 upper_32_bits(urb->transfer_dma),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003148 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003149 field | ep_ring->cycle_state);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003150 }
3151
3152 /* Save the DMA address of the last TRB in the TD */
3153 td->last_trb = ep_ring->enqueue;
3154
3155 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3156 /* If the device sent data, the status stage is an OUT transfer */
3157 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3158 field = 0;
3159 else
3160 field = TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003161 queue_trb(xhci, ep_ring, false,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003162 0,
3163 0,
3164 TRB_INTR_TARGET(0),
3165 /* Event on completion */
3166 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3167
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003168 giveback_first_trb(xhci, slot_id, ep_index, 0,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003169 start_cycle, start_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003170 return 0;
3171}
3172
Andiry Xu04e51902010-07-22 15:23:39 -07003173static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
3174 struct urb *urb, int i)
3175{
3176 int num_trbs = 0;
Sarah Sharp48df4a62011-08-12 10:23:01 -07003177 u64 addr, td_len;
Andiry Xu04e51902010-07-22 15:23:39 -07003178
3179 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
3180 td_len = urb->iso_frame_desc[i].length;
3181
Sarah Sharp48df4a62011-08-12 10:23:01 -07003182 num_trbs = DIV_ROUND_UP(td_len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
3183 TRB_MAX_BUFF_SIZE);
3184 if (num_trbs == 0)
Andiry Xu04e51902010-07-22 15:23:39 -07003185 num_trbs++;
3186
Andiry Xu04e51902010-07-22 15:23:39 -07003187 return num_trbs;
3188}
3189
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003190/*
3191 * The transfer burst count field of the isochronous TRB defines the number of
3192 * bursts that are required to move all packets in this TD. Only SuperSpeed
3193 * devices can burst up to bMaxBurst number of packets per service interval.
3194 * This field is zero based, meaning a value of zero in the field means one
3195 * burst. Basically, for everything but SuperSpeed devices, this field will be
3196 * zero. Only xHCI 1.0 host controllers support this field.
3197 */
3198static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
3199 struct usb_device *udev,
3200 struct urb *urb, unsigned int total_packet_count)
3201{
3202 unsigned int max_burst;
3203
3204 if (xhci->hci_version < 0x100 || udev->speed != USB_SPEED_SUPER)
3205 return 0;
3206
3207 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3208 return roundup(total_packet_count, max_burst + 1) - 1;
3209}
3210
Sarah Sharpb61d3782011-04-19 17:43:33 -07003211/*
3212 * Returns the number of packets in the last "burst" of packets. This field is
3213 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3214 * the last burst packet count is equal to the total number of packets in the
3215 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3216 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3217 * contain 1 to (bMaxBurst + 1) packets.
3218 */
3219static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
3220 struct usb_device *udev,
3221 struct urb *urb, unsigned int total_packet_count)
3222{
3223 unsigned int max_burst;
3224 unsigned int residue;
3225
3226 if (xhci->hci_version < 0x100)
3227 return 0;
3228
3229 switch (udev->speed) {
3230 case USB_SPEED_SUPER:
3231 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3232 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3233 residue = total_packet_count % (max_burst + 1);
3234 /* If residue is zero, the last burst contains (max_burst + 1)
3235 * number of packets, but the TLBPC field is zero-based.
3236 */
3237 if (residue == 0)
3238 return max_burst;
3239 return residue - 1;
3240 default:
3241 if (total_packet_count == 0)
3242 return 0;
3243 return total_packet_count - 1;
3244 }
3245}
3246
Andiry Xu04e51902010-07-22 15:23:39 -07003247/* This is for isoc transfer */
3248static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3249 struct urb *urb, int slot_id, unsigned int ep_index)
3250{
3251 struct xhci_ring *ep_ring;
3252 struct urb_priv *urb_priv;
3253 struct xhci_td *td;
3254 int num_tds, trbs_per_td;
3255 struct xhci_generic_trb *start_trb;
3256 bool first_trb;
3257 int start_cycle;
3258 u32 field, length_field;
3259 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3260 u64 start_addr, addr;
3261 int i, j;
Andiry Xu47cbf692010-12-20 14:49:48 +08003262 bool more_trbs_coming;
Andiry Xu04e51902010-07-22 15:23:39 -07003263
3264 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3265
3266 num_tds = urb->number_of_packets;
3267 if (num_tds < 1) {
3268 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3269 return -EINVAL;
3270 }
3271
Andiry Xu04e51902010-07-22 15:23:39 -07003272 start_addr = (u64) urb->transfer_dma;
3273 start_trb = &ep_ring->enqueue->generic;
3274 start_cycle = ep_ring->cycle_state;
3275
Sarah Sharp522989a2011-07-29 12:44:32 -07003276 urb_priv = urb->hcpriv;
Andiry Xu04e51902010-07-22 15:23:39 -07003277 /* Queue the first TRB, even if it's zero-length */
3278 for (i = 0; i < num_tds; i++) {
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003279 unsigned int total_packet_count;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003280 unsigned int burst_count;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003281 unsigned int residue;
Andiry Xu04e51902010-07-22 15:23:39 -07003282
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003283 first_trb = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003284 running_total = 0;
3285 addr = start_addr + urb->iso_frame_desc[i].offset;
3286 td_len = urb->iso_frame_desc[i].length;
3287 td_remain_len = td_len;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003288 total_packet_count = roundup(td_len,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003289 usb_endpoint_maxp(&urb->ep->desc));
Sarah Sharp48df4a62011-08-12 10:23:01 -07003290 /* A zero-length transfer still involves at least one packet. */
3291 if (total_packet_count == 0)
3292 total_packet_count++;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003293 burst_count = xhci_get_burst_count(xhci, urb->dev, urb,
3294 total_packet_count);
Sarah Sharpb61d3782011-04-19 17:43:33 -07003295 residue = xhci_get_last_burst_packet_count(xhci,
3296 urb->dev, urb, total_packet_count);
Andiry Xu04e51902010-07-22 15:23:39 -07003297
3298 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
3299
3300 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003301 urb->stream_id, trbs_per_td, urb, i, mem_flags);
Sarah Sharp522989a2011-07-29 12:44:32 -07003302 if (ret < 0) {
3303 if (i == 0)
3304 return ret;
3305 goto cleanup;
3306 }
Andiry Xu04e51902010-07-22 15:23:39 -07003307
Andiry Xu04e51902010-07-22 15:23:39 -07003308 td = urb_priv->td[i];
Andiry Xu04e51902010-07-22 15:23:39 -07003309 for (j = 0; j < trbs_per_td; j++) {
3310 u32 remainder = 0;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003311 field = TRB_TBC(burst_count) | TRB_TLBPC(residue);
Andiry Xu04e51902010-07-22 15:23:39 -07003312
3313 if (first_trb) {
3314 /* Queue the isoc TRB */
3315 field |= TRB_TYPE(TRB_ISOC);
3316 /* Assume URB_ISO_ASAP is set */
3317 field |= TRB_SIA;
Andiry Xu50f7b522010-12-20 15:09:34 +08003318 if (i == 0) {
3319 if (start_cycle == 0)
3320 field |= 0x1;
3321 } else
Andiry Xu04e51902010-07-22 15:23:39 -07003322 field |= ep_ring->cycle_state;
3323 first_trb = false;
3324 } else {
3325 /* Queue other normal TRBs */
3326 field |= TRB_TYPE(TRB_NORMAL);
3327 field |= ep_ring->cycle_state;
3328 }
3329
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003330 /* Only set interrupt on short packet for IN EPs */
3331 if (usb_urb_dir_in(urb))
3332 field |= TRB_ISP;
3333
Andiry Xu04e51902010-07-22 15:23:39 -07003334 /* Chain all the TRBs together; clear the chain bit in
3335 * the last TRB to indicate it's the last TRB in the
3336 * chain.
3337 */
3338 if (j < trbs_per_td - 1) {
3339 field |= TRB_CHAIN;
Andiry Xu47cbf692010-12-20 14:49:48 +08003340 more_trbs_coming = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003341 } else {
3342 td->last_trb = ep_ring->enqueue;
3343 field |= TRB_IOC;
Andiry Xuad106f22011-05-05 18:14:02 +08003344 if (xhci->hci_version == 0x100) {
3345 /* Set BEI bit except for the last td */
3346 if (i < num_tds - 1)
3347 field |= TRB_BEI;
3348 }
Andiry Xu47cbf692010-12-20 14:49:48 +08003349 more_trbs_coming = false;
Andiry Xu04e51902010-07-22 15:23:39 -07003350 }
3351
3352 /* Calculate TRB length */
3353 trb_buff_len = TRB_MAX_BUFF_SIZE -
3354 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
3355 if (trb_buff_len > td_remain_len)
3356 trb_buff_len = td_remain_len;
3357
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003358 /* Set the TRB length, TD size, & interrupter fields. */
3359 if (xhci->hci_version < 0x100) {
3360 remainder = xhci_td_remainder(
3361 td_len - running_total);
3362 } else {
3363 remainder = xhci_v1_0_td_remainder(
3364 running_total, trb_buff_len,
3365 total_packet_count, urb);
3366 }
Andiry Xu04e51902010-07-22 15:23:39 -07003367 length_field = TRB_LEN(trb_buff_len) |
3368 remainder |
3369 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003370
Andiry Xu3b72fca2012-03-05 17:49:32 +08003371 queue_trb(xhci, ep_ring, more_trbs_coming,
Andiry Xu04e51902010-07-22 15:23:39 -07003372 lower_32_bits(addr),
3373 upper_32_bits(addr),
3374 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003375 field);
Andiry Xu04e51902010-07-22 15:23:39 -07003376 running_total += trb_buff_len;
3377
3378 addr += trb_buff_len;
3379 td_remain_len -= trb_buff_len;
3380 }
3381
3382 /* Check TD length */
3383 if (running_total != td_len) {
3384 xhci_err(xhci, "ISOC TD length unmatch\n");
Andiry Xucf840552012-01-18 17:47:12 +08003385 ret = -EINVAL;
3386 goto cleanup;
Andiry Xu04e51902010-07-22 15:23:39 -07003387 }
3388 }
3389
Andiry Xuc41136b2011-03-22 17:08:14 +08003390 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
3391 if (xhci->quirks & XHCI_AMD_PLL_FIX)
3392 usb_amd_quirk_pll_disable();
3393 }
3394 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
3395
Andiry Xue1eab2e2011-01-04 16:30:39 -08003396 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3397 start_cycle, start_trb);
Andiry Xu04e51902010-07-22 15:23:39 -07003398 return 0;
Sarah Sharp522989a2011-07-29 12:44:32 -07003399cleanup:
3400 /* Clean up a partially enqueued isoc transfer. */
3401
3402 for (i--; i >= 0; i--)
Sarah Sharp585df1d2011-08-02 15:43:40 -07003403 list_del_init(&urb_priv->td[i]->td_list);
Sarah Sharp522989a2011-07-29 12:44:32 -07003404
3405 /* Use the first TD as a temporary variable to turn the TDs we've queued
3406 * into No-ops with a software-owned cycle bit. That way the hardware
3407 * won't accidentally start executing bogus TDs when we partially
3408 * overwrite them. td->first_trb and td->start_seg are already set.
3409 */
3410 urb_priv->td[0]->last_trb = ep_ring->enqueue;
3411 /* Every TRB except the first & last will have its cycle bit flipped. */
3412 td_to_noop(xhci, ep_ring, urb_priv->td[0], true);
3413
3414 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
3415 ep_ring->enqueue = urb_priv->td[0]->first_trb;
3416 ep_ring->enq_seg = urb_priv->td[0]->start_seg;
3417 ep_ring->cycle_state = start_cycle;
Andiry Xub008df62012-03-05 17:49:34 +08003418 ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp;
Sarah Sharp522989a2011-07-29 12:44:32 -07003419 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
3420 return ret;
Andiry Xu04e51902010-07-22 15:23:39 -07003421}
3422
3423/*
3424 * Check transfer ring to guarantee there is enough room for the urb.
3425 * Update ISO URB start_frame and interval.
3426 * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
3427 * update the urb->start_frame by now.
3428 * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
3429 */
3430int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3431 struct urb *urb, int slot_id, unsigned int ep_index)
3432{
3433 struct xhci_virt_device *xdev;
3434 struct xhci_ring *ep_ring;
3435 struct xhci_ep_ctx *ep_ctx;
3436 int start_frame;
3437 int xhci_interval;
3438 int ep_interval;
3439 int num_tds, num_trbs, i;
3440 int ret;
3441
3442 xdev = xhci->devs[slot_id];
3443 ep_ring = xdev->eps[ep_index].ring;
3444 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3445
3446 num_trbs = 0;
3447 num_tds = urb->number_of_packets;
3448 for (i = 0; i < num_tds; i++)
3449 num_trbs += count_isoc_trbs_needed(xhci, urb, i);
3450
3451 /* Check the ring to guarantee there is enough room for the whole urb.
3452 * Do not insert any td of the urb to the ring if the check failed.
3453 */
Matt Evans28ccd292011-03-29 13:40:46 +11003454 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003455 num_trbs, mem_flags);
Andiry Xu04e51902010-07-22 15:23:39 -07003456 if (ret)
3457 return ret;
3458
3459 start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
3460 start_frame &= 0x3fff;
3461
3462 urb->start_frame = start_frame;
3463 if (urb->dev->speed == USB_SPEED_LOW ||
3464 urb->dev->speed == USB_SPEED_FULL)
3465 urb->start_frame >>= 3;
3466
Matt Evans28ccd292011-03-29 13:40:46 +11003467 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Andiry Xu04e51902010-07-22 15:23:39 -07003468 ep_interval = urb->interval;
3469 /* Convert to microframes */
3470 if (urb->dev->speed == USB_SPEED_LOW ||
3471 urb->dev->speed == USB_SPEED_FULL)
3472 ep_interval *= 8;
3473 /* FIXME change this to a warning and a suggestion to use the new API
3474 * to set the polling interval (once the API is added).
3475 */
3476 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08003477 if (printk_ratelimit())
Andiry Xu04e51902010-07-22 15:23:39 -07003478 dev_dbg(&urb->dev->dev, "Driver uses different interval"
3479 " (%d microframe%s) than xHCI "
3480 "(%d microframe%s)\n",
3481 ep_interval,
3482 ep_interval == 1 ? "" : "s",
3483 xhci_interval,
3484 xhci_interval == 1 ? "" : "s");
3485 urb->interval = xhci_interval;
3486 /* Convert back to frames for LS/FS devices */
3487 if (urb->dev->speed == USB_SPEED_LOW ||
3488 urb->dev->speed == USB_SPEED_FULL)
3489 urb->interval /= 8;
3490 }
Andiry Xub008df62012-03-05 17:49:34 +08003491 ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free;
3492
Andiry Xu04e51902010-07-22 15:23:39 -07003493 return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
3494}
3495
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003496/**** Command Ring Operations ****/
3497
Sarah Sharp913a8a32009-09-04 10:53:13 -07003498/* Generic function for queueing a command TRB on the command ring.
3499 * Check to make sure there's room on the command ring for one command TRB.
3500 * Also check that there's room reserved for commands that must not fail.
3501 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3502 * then only check for the number of reserved spots.
3503 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3504 * because the command event handler may want to resubmit a failed command.
3505 */
3506static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3507 u32 field3, u32 field4, bool command_must_succeed)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003508{
Sarah Sharp913a8a32009-09-04 10:53:13 -07003509 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003510 int ret;
3511
Sarah Sharp913a8a32009-09-04 10:53:13 -07003512 if (!command_must_succeed)
3513 reserved_trbs++;
3514
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003515 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003516 reserved_trbs, GFP_ATOMIC);
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003517 if (ret < 0) {
3518 xhci_err(xhci, "ERR: No room for command on command ring\n");
Sarah Sharp913a8a32009-09-04 10:53:13 -07003519 if (command_must_succeed)
3520 xhci_err(xhci, "ERR: Reserved TRB counting for "
3521 "unfailable commands failed.\n");
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003522 return ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003523 }
Andiry Xu3b72fca2012-03-05 17:49:32 +08003524 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
3525 field4 | xhci->cmd_ring->cycle_state);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003526 return 0;
3527}
3528
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003529/* Queue a slot enable or disable request on the command ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003530int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003531{
3532 return queue_command(xhci, 0, 0, 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003533 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003534}
3535
3536/* Queue an address device command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003537int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3538 u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003539{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003540 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3541 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003542 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
3543 false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003544}
Sarah Sharpf94e01862009-04-27 19:58:38 -07003545
Sarah Sharp02386342010-05-24 13:25:28 -07003546int xhci_queue_vendor_command(struct xhci_hcd *xhci,
3547 u32 field1, u32 field2, u32 field3, u32 field4)
3548{
3549 return queue_command(xhci, field1, field2, field3, field4, false);
3550}
3551
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003552/* Queue a reset device command TRB */
3553int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
3554{
3555 return queue_command(xhci, 0, 0, 0,
3556 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
3557 false);
3558}
3559
Sarah Sharpf94e01862009-04-27 19:58:38 -07003560/* Queue a configure endpoint command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003561int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003562 u32 slot_id, bool command_must_succeed)
Sarah Sharpf94e01862009-04-27 19:58:38 -07003563{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003564 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3565 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003566 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3567 command_must_succeed);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003568}
Sarah Sharpae636742009-04-29 19:02:31 -07003569
Sarah Sharpf2217e82009-08-07 14:04:43 -07003570/* Queue an evaluate context command TRB */
3571int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3572 u32 slot_id)
3573{
3574 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3575 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003576 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
3577 false);
Sarah Sharpf2217e82009-08-07 14:04:43 -07003578}
3579
Andiry Xube88fe42010-10-14 07:22:57 -07003580/*
3581 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3582 * activity on an endpoint that is about to be suspended.
3583 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003584int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
Andiry Xube88fe42010-10-14 07:22:57 -07003585 unsigned int ep_index, int suspend)
Sarah Sharpae636742009-04-29 19:02:31 -07003586{
3587 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3588 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3589 u32 type = TRB_TYPE(TRB_STOP_RING);
Andiry Xube88fe42010-10-14 07:22:57 -07003590 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
Sarah Sharpae636742009-04-29 19:02:31 -07003591
3592 return queue_command(xhci, 0, 0, 0,
Andiry Xube88fe42010-10-14 07:22:57 -07003593 trb_slot_id | trb_ep_index | type | trb_suspend, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003594}
3595
3596/* Set Transfer Ring Dequeue Pointer command.
3597 * This should not be used for endpoints that have streams enabled.
3598 */
3599static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003600 unsigned int ep_index, unsigned int stream_id,
3601 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -07003602 union xhci_trb *deq_ptr, u32 cycle_state)
3603{
3604 dma_addr_t addr;
3605 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3606 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003607 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
Sarah Sharpae636742009-04-29 19:02:31 -07003608 u32 type = TRB_TYPE(TRB_SET_DEQ);
Sarah Sharpbf161e82011-02-23 15:46:42 -08003609 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07003610
Sarah Sharp23e3be12009-04-29 19:05:20 -07003611 addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003612 if (addr == 0) {
Sarah Sharpae636742009-04-29 19:02:31 -07003613 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003614 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
3615 deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003616 return 0;
3617 }
Sarah Sharpbf161e82011-02-23 15:46:42 -08003618 ep = &xhci->devs[slot_id]->eps[ep_index];
3619 if ((ep->ep_state & SET_DEQ_PENDING)) {
3620 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
3621 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
3622 return 0;
3623 }
3624 ep->queued_deq_seg = deq_seg;
3625 ep->queued_deq_ptr = deq_ptr;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003626 return queue_command(xhci, lower_32_bits(addr) | cycle_state,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003627 upper_32_bits(addr), trb_stream_id,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003628 trb_slot_id | trb_ep_index | type, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003629}
Sarah Sharpa1587d92009-07-27 12:03:15 -07003630
3631int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
3632 unsigned int ep_index)
3633{
3634 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3635 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3636 u32 type = TRB_TYPE(TRB_RESET_EP);
3637
Sarah Sharp913a8a32009-09-04 10:53:13 -07003638 return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
3639 false);
Sarah Sharpa1587d92009-07-27 12:03:15 -07003640}