blob: 56f6c584c651a0f5763f459c49d98cc261ba593e [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 Evans28ccd292011-03-29 13:40:46 +1100116 return (le32_to_cpu(trb->link.control) & TRB_TYPE_BITMASK)
117 == TRB_TYPE(TRB_LINK);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700118}
119
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700120static int enqueue_is_link_trb(struct xhci_ring *ring)
John Youn6c12db92010-05-10 15:33:00 -0700121{
122 struct xhci_link_trb *link = &ring->enqueue->link;
Matt Evans28ccd292011-03-29 13:40:46 +1100123 return ((le32_to_cpu(link->control) & TRB_TYPE_BITMASK) ==
124 TRB_TYPE(TRB_LINK));
John Youn6c12db92010-05-10 15:33:00 -0700125}
126
Sarah Sharpae636742009-04-29 19:02:31 -0700127/* Updates trb to point to the next TRB in the ring, and updates seg if the next
128 * TRB is in a new segment. This does not skip over link TRBs, and it does not
129 * effect the ring dequeue or enqueue pointers.
130 */
131static void next_trb(struct xhci_hcd *xhci,
132 struct xhci_ring *ring,
133 struct xhci_segment **seg,
134 union xhci_trb **trb)
135{
136 if (last_trb(xhci, ring, *seg, *trb)) {
137 *seg = (*seg)->next;
138 *trb = ((*seg)->trbs);
139 } else {
John Youna1669b22010-08-09 13:56:11 -0700140 (*trb)++;
Sarah Sharpae636742009-04-29 19:02:31 -0700141 }
142}
143
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700144/*
145 * See Cycle bit rules. SW is the consumer for the event ring only.
146 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
147 */
148static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
149{
150 union xhci_trb *next = ++(ring->dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700151 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700152
153 ring->deq_updates++;
154 /* Update the dequeue pointer further if that was a link TRB or we're at
155 * the end of an event ring segment (which doesn't have link TRBS)
156 */
157 while (last_trb(xhci, ring, ring->deq_seg, next)) {
158 if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
159 ring->cycle_state = (ring->cycle_state ? 0 : 1);
160 if (!in_interrupt())
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700161 xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
162 ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700163 (unsigned int) ring->cycle_state);
164 }
165 ring->deq_seg = ring->deq_seg->next;
166 ring->dequeue = ring->deq_seg->trbs;
167 next = ring->dequeue;
168 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700169 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700170}
171
172/*
173 * See Cycle bit rules. SW is the consumer for the event ring only.
174 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
175 *
176 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
177 * chain bit is set), then set the chain bit in all the following link TRBs.
178 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
179 * have their chain bit cleared (so that each Link TRB is a separate TD).
180 *
181 * 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 -0700182 * set, but other sections talk about dealing with the chain bit set. This was
183 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
184 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700185 *
186 * @more_trbs_coming: Will you enqueue more TRBs before calling
187 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700188 */
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700189static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
190 bool consumer, bool more_trbs_coming)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700191{
192 u32 chain;
193 union xhci_trb *next;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700194 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700195
Matt Evans28ccd292011-03-29 13:40:46 +1100196 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700197 next = ++(ring->enqueue);
198
199 ring->enq_updates++;
200 /* Update the dequeue pointer further if that was a link TRB or we're at
201 * the end of an event ring segment (which doesn't have link TRBS)
202 */
203 while (last_trb(xhci, ring, ring->enq_seg, next)) {
204 if (!consumer) {
205 if (ring != xhci->event_ring) {
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700206 /*
207 * If the caller doesn't plan on enqueueing more
208 * TDs before ringing the doorbell, then we
209 * don't want to give the link TRB to the
210 * hardware just yet. We'll give the link TRB
211 * back in prepare_ring() just before we enqueue
212 * the TD at the top of the ring.
213 */
214 if (!chain && !more_trbs_coming)
John Youn6c12db92010-05-10 15:33:00 -0700215 break;
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700216
217 /* If we're not dealing with 0.95 hardware,
218 * carry over the chain bit of the previous TRB
219 * (which may mean the chain bit is cleared).
220 */
221 if (!xhci_link_trb_quirk(xhci)) {
Matt Evans28ccd292011-03-29 13:40:46 +1100222 next->link.control &=
223 cpu_to_le32(~TRB_CHAIN);
224 next->link.control |=
225 cpu_to_le32(chain);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700226 }
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700227 /* Give this link TRB to the hardware */
228 wmb();
Matt Evans28ccd292011-03-29 13:40:46 +1100229 next->link.control ^= cpu_to_le32(TRB_CYCLE);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700230 }
231 /* Toggle the cycle bit after the last ring segment. */
232 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
233 ring->cycle_state = (ring->cycle_state ? 0 : 1);
234 if (!in_interrupt())
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700235 xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
236 ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700237 (unsigned int) ring->cycle_state);
238 }
239 }
240 ring->enq_seg = ring->enq_seg->next;
241 ring->enqueue = ring->enq_seg->trbs;
242 next = ring->enqueue;
243 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700244 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700245}
246
247/*
248 * Check to see if there's room to enqueue num_trbs on the ring. See rules
249 * above.
250 * FIXME: this would be simpler and faster if we just kept track of the number
251 * of free TRBs in a ring.
252 */
253static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
254 unsigned int num_trbs)
255{
256 int i;
257 union xhci_trb *enq = ring->enqueue;
258 struct xhci_segment *enq_seg = ring->enq_seg;
Sarah Sharp44ebd032010-05-18 16:05:26 -0700259 struct xhci_segment *cur_seg;
260 unsigned int left_on_ring;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700261
John Youn6c12db92010-05-10 15:33:00 -0700262 /* If we are currently pointing to a link TRB, advance the
263 * enqueue pointer before checking for space */
264 while (last_trb(xhci, ring, enq_seg, enq)) {
265 enq_seg = enq_seg->next;
266 enq = enq_seg->trbs;
267 }
268
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700269 /* Check if ring is empty */
Sarah Sharp44ebd032010-05-18 16:05:26 -0700270 if (enq == ring->dequeue) {
271 /* Can't use link trbs */
272 left_on_ring = TRBS_PER_SEGMENT - 1;
273 for (cur_seg = enq_seg->next; cur_seg != enq_seg;
274 cur_seg = cur_seg->next)
275 left_on_ring += TRBS_PER_SEGMENT - 1;
276
277 /* Always need one TRB free in the ring. */
278 left_on_ring -= 1;
279 if (num_trbs > left_on_ring) {
280 xhci_warn(xhci, "Not enough room on ring; "
281 "need %u TRBs, %u TRBs left\n",
282 num_trbs, left_on_ring);
283 return 0;
284 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700285 return 1;
Sarah Sharp44ebd032010-05-18 16:05:26 -0700286 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700287 /* Make sure there's an extra empty TRB available */
288 for (i = 0; i <= num_trbs; ++i) {
289 if (enq == ring->dequeue)
290 return 0;
291 enq++;
292 while (last_trb(xhci, ring, enq_seg, enq)) {
293 enq_seg = enq_seg->next;
294 enq = enq_seg->trbs;
295 }
296 }
297 return 1;
298}
299
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700300/* Ring the host controller doorbell after placing a command on the ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700301void xhci_ring_cmd_db(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700302{
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700303 xhci_dbg(xhci, "// Ding dong!\n");
Matthew Wilcox50d646762010-12-15 14:18:11 -0500304 xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700305 /* Flush PCI posted writes */
306 xhci_readl(xhci, &xhci->dba->doorbell[0]);
307}
308
Andiry Xube88fe42010-10-14 07:22:57 -0700309void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700310 unsigned int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700311 unsigned int ep_index,
312 unsigned int stream_id)
Sarah Sharpae636742009-04-29 19:02:31 -0700313{
Matt Evans28ccd292011-03-29 13:40:46 +1100314 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
Matthew Wilcox50d646762010-12-15 14:18:11 -0500315 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
316 unsigned int ep_state = ep->ep_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700317
Sarah Sharpae636742009-04-29 19:02:31 -0700318 /* Don't ring the doorbell for this endpoint if there are pending
Matthew Wilcox50d646762010-12-15 14:18:11 -0500319 * cancellations because we don't want to interrupt processing.
Sarah Sharp8df75f42010-04-02 15:34:16 -0700320 * We don't want to restart any stream rings if there's a set dequeue
321 * pointer command pending because the device can choose to start any
322 * stream once the endpoint is on the HW schedule.
323 * FIXME - check all the stream rings for pending cancellations.
Sarah Sharpae636742009-04-29 19:02:31 -0700324 */
Matthew Wilcox50d646762010-12-15 14:18:11 -0500325 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
326 (ep_state & EP_HALTED))
327 return;
328 xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr);
329 /* The CPU has better things to do at this point than wait for a
330 * write-posting flush. It'll get there soon enough.
331 */
Sarah Sharpae636742009-04-29 19:02:31 -0700332}
333
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700334/* Ring the doorbell for any rings with pending URBs */
335static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
336 unsigned int slot_id,
337 unsigned int ep_index)
338{
339 unsigned int stream_id;
340 struct xhci_virt_ep *ep;
341
342 ep = &xhci->devs[slot_id]->eps[ep_index];
343
344 /* A ring has pending URBs if its TD list is not empty */
345 if (!(ep->ep_state & EP_HAS_STREAMS)) {
346 if (!(list_empty(&ep->ring->td_list)))
Andiry Xube88fe42010-10-14 07:22:57 -0700347 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700348 return;
349 }
350
351 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
352 stream_id++) {
353 struct xhci_stream_info *stream_info = ep->stream_info;
354 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
Andiry Xube88fe42010-10-14 07:22:57 -0700355 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
356 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700357 }
358}
359
Sarah Sharpae636742009-04-29 19:02:31 -0700360/*
361 * Find the segment that trb is in. Start searching in start_seg.
362 * If we must move past a segment that has a link TRB with a toggle cycle state
363 * bit set, then we will toggle the value pointed at by cycle_state.
364 */
365static struct xhci_segment *find_trb_seg(
366 struct xhci_segment *start_seg,
367 union xhci_trb *trb, int *cycle_state)
368{
369 struct xhci_segment *cur_seg = start_seg;
370 struct xhci_generic_trb *generic_trb;
371
372 while (cur_seg->trbs > trb ||
373 &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
374 generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
Matt Evans28ccd292011-03-29 13:40:46 +1100375 if (le32_to_cpu(generic_trb->field[3]) & LINK_TOGGLE)
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800376 *cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700377 cur_seg = cur_seg->next;
378 if (cur_seg == start_seg)
379 /* Looped over the entire list. Oops! */
Randy Dunlap326b4812010-04-19 08:53:50 -0700380 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700381 }
382 return cur_seg;
383}
384
Sarah Sharp021bff92010-07-29 22:12:20 -0700385
386static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
387 unsigned int slot_id, unsigned int ep_index,
388 unsigned int stream_id)
389{
390 struct xhci_virt_ep *ep;
391
392 ep = &xhci->devs[slot_id]->eps[ep_index];
393 /* Common case: no streams */
394 if (!(ep->ep_state & EP_HAS_STREAMS))
395 return ep->ring;
396
397 if (stream_id == 0) {
398 xhci_warn(xhci,
399 "WARN: Slot ID %u, ep index %u has streams, "
400 "but URB has no stream ID.\n",
401 slot_id, ep_index);
402 return NULL;
403 }
404
405 if (stream_id < ep->stream_info->num_streams)
406 return ep->stream_info->stream_rings[stream_id];
407
408 xhci_warn(xhci,
409 "WARN: Slot ID %u, ep index %u has "
410 "stream IDs 1 to %u allocated, "
411 "but stream ID %u is requested.\n",
412 slot_id, ep_index,
413 ep->stream_info->num_streams - 1,
414 stream_id);
415 return NULL;
416}
417
418/* Get the right ring for the given URB.
419 * If the endpoint supports streams, boundary check the URB's stream ID.
420 * If the endpoint doesn't support streams, return the singular endpoint ring.
421 */
422static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
423 struct urb *urb)
424{
425 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
426 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
427}
428
Sarah Sharpae636742009-04-29 19:02:31 -0700429/*
430 * Move the xHC's endpoint ring dequeue pointer past cur_td.
431 * Record the new state of the xHC's endpoint ring dequeue segment,
432 * dequeue pointer, and new consumer cycle state in state.
433 * Update our internal representation of the ring's dequeue pointer.
434 *
435 * We do this in three jumps:
436 * - First we update our new ring state to be the same as when the xHC stopped.
437 * - Then we traverse the ring to find the segment that contains
438 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
439 * any link TRBs with the toggle cycle bit set.
440 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
441 * if we've moved it past a link TRB with the toggle cycle bit set.
Matt Evans28ccd292011-03-29 13:40:46 +1100442 *
443 * Some of the uses of xhci_generic_trb are grotty, but if they're done
444 * with correct __le32 accesses they should work fine. Only users of this are
445 * in here.
Sarah Sharpae636742009-04-29 19:02:31 -0700446 */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700447void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700448 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700449 unsigned int stream_id, struct xhci_td *cur_td,
450 struct xhci_dequeue_state *state)
Sarah Sharpae636742009-04-29 19:02:31 -0700451{
452 struct xhci_virt_device *dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700453 struct xhci_ring *ep_ring;
Sarah Sharpae636742009-04-29 19:02:31 -0700454 struct xhci_generic_trb *trb;
John Yound115b042009-07-27 12:05:15 -0700455 struct xhci_ep_ctx *ep_ctx;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700456 dma_addr_t addr;
Sarah Sharpae636742009-04-29 19:02:31 -0700457
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700458 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
459 ep_index, stream_id);
460 if (!ep_ring) {
461 xhci_warn(xhci, "WARN can't find new dequeue state "
462 "for invalid stream ID %u.\n",
463 stream_id);
464 return;
465 }
Sarah Sharpae636742009-04-29 19:02:31 -0700466 state->new_cycle_state = 0;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700467 xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700468 state->new_deq_seg = find_trb_seg(cur_td->start_seg,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700469 dev->eps[ep_index].stopped_trb,
Sarah Sharpae636742009-04-29 19:02:31 -0700470 &state->new_cycle_state);
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800471 if (!state->new_deq_seg) {
472 WARN_ON(1);
473 return;
474 }
475
Sarah Sharpae636742009-04-29 19:02:31 -0700476 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700477 xhci_dbg(xhci, "Finding endpoint context\n");
John Yound115b042009-07-27 12:05:15 -0700478 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +1100479 state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
Sarah Sharpae636742009-04-29 19:02:31 -0700480
481 state->new_deq_ptr = cur_td->last_trb;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700482 xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700483 state->new_deq_seg = find_trb_seg(state->new_deq_seg,
484 state->new_deq_ptr,
485 &state->new_cycle_state);
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800486 if (!state->new_deq_seg) {
487 WARN_ON(1);
488 return;
489 }
Sarah Sharpae636742009-04-29 19:02:31 -0700490
491 trb = &state->new_deq_ptr->generic;
Matt Evans28ccd292011-03-29 13:40:46 +1100492 if ((le32_to_cpu(trb->field[3]) & TRB_TYPE_BITMASK) ==
493 TRB_TYPE(TRB_LINK) && (le32_to_cpu(trb->field[3]) & LINK_TOGGLE))
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800494 state->new_cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700495 next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
496
Sarah Sharp01a1fdb2011-02-23 18:12:29 -0800497 /*
498 * If there is only one segment in a ring, find_trb_seg()'s while loop
499 * will not run, and it will return before it has a chance to see if it
500 * needs to toggle the cycle bit. It can't tell if the stalled transfer
501 * ended just before the link TRB on a one-segment ring, or if the TD
502 * wrapped around the top of the ring, because it doesn't have the TD in
503 * question. Look for the one-segment case where stalled TRB's address
504 * is greater than the new dequeue pointer address.
505 */
506 if (ep_ring->first_seg == ep_ring->first_seg->next &&
507 state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
508 state->new_cycle_state ^= 0x1;
509 xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
510
Sarah Sharpae636742009-04-29 19:02:31 -0700511 /* Don't update the ring cycle state for the producer (us). */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700512 xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
513 state->new_deq_seg);
514 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
515 xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
516 (unsigned long long) addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700517}
518
Sarah Sharp23e3be12009-04-29 19:05:20 -0700519static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Sarah Sharpae636742009-04-29 19:02:31 -0700520 struct xhci_td *cur_td)
521{
522 struct xhci_segment *cur_seg;
523 union xhci_trb *cur_trb;
524
525 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
526 true;
527 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evans28ccd292011-03-29 13:40:46 +1100528 if ((le32_to_cpu(cur_trb->generic.field[3]) & TRB_TYPE_BITMASK)
529 == TRB_TYPE(TRB_LINK)) {
Sarah Sharpae636742009-04-29 19:02:31 -0700530 /* Unchain any chained Link TRBs, but
531 * leave the pointers intact.
532 */
Matt Evans28ccd292011-03-29 13:40:46 +1100533 cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN);
Sarah Sharpae636742009-04-29 19:02:31 -0700534 xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700535 xhci_dbg(xhci, "Address = %p (0x%llx dma); "
536 "in seg %p (0x%llx dma)\n",
537 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700538 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700539 cur_seg,
540 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700541 } else {
542 cur_trb->generic.field[0] = 0;
543 cur_trb->generic.field[1] = 0;
544 cur_trb->generic.field[2] = 0;
545 /* Preserve only the cycle bit of this TRB */
Matt Evans28ccd292011-03-29 13:40:46 +1100546 cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
547 cur_trb->generic.field[3] |= cpu_to_le32(
548 TRB_TYPE(TRB_TR_NOOP));
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700549 xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
550 "in seg %p (0x%llx dma)\n",
551 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700552 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700553 cur_seg,
554 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700555 }
556 if (cur_trb == cur_td->last_trb)
557 break;
558 }
559}
560
561static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700562 unsigned int ep_index, unsigned int stream_id,
563 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -0700564 union xhci_trb *deq_ptr, u32 cycle_state);
565
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700566void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700567 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700568 unsigned int stream_id,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700569 struct xhci_dequeue_state *deq_state)
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700570{
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700571 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
572
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700573 xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
574 "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
575 deq_state->new_deq_seg,
576 (unsigned long long)deq_state->new_deq_seg->dma,
577 deq_state->new_deq_ptr,
578 (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
579 deq_state->new_cycle_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700580 queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700581 deq_state->new_deq_seg,
582 deq_state->new_deq_ptr,
583 (u32) deq_state->new_cycle_state);
584 /* Stop the TD queueing code from ringing the doorbell until
585 * this command completes. The HC won't set the dequeue pointer
586 * if the ring is running, and ringing the doorbell starts the
587 * ring running.
588 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700589 ep->ep_state |= SET_DEQ_PENDING;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700590}
591
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700592static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700593 struct xhci_virt_ep *ep)
594{
595 ep->ep_state &= ~EP_HALT_PENDING;
596 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
597 * timer is running on another CPU, we don't decrement stop_cmds_pending
598 * (since we didn't successfully stop the watchdog timer).
599 */
600 if (del_timer(&ep->stop_cmd_timer))
601 ep->stop_cmds_pending--;
602}
603
604/* Must be called with xhci->lock held in interrupt context */
605static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
606 struct xhci_td *cur_td, int status, char *adjective)
607{
Sarah Sharp214f76f2010-10-26 11:22:02 -0700608 struct usb_hcd *hcd;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700609 struct urb *urb;
610 struct urb_priv *urb_priv;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700611
Andiry Xu8e51adc2010-07-22 15:23:31 -0700612 urb = cur_td->urb;
613 urb_priv = urb->hcpriv;
614 urb_priv->td_cnt++;
Sarah Sharp214f76f2010-10-26 11:22:02 -0700615 hcd = bus_to_hcd(urb->dev->bus);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700616
Andiry Xu8e51adc2010-07-22 15:23:31 -0700617 /* Only giveback urb when this is the last td in urb */
618 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xuc41136b2011-03-22 17:08:14 +0800619 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
620 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
621 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
622 if (xhci->quirks & XHCI_AMD_PLL_FIX)
623 usb_amd_quirk_pll_enable();
624 }
625 }
Andiry Xu8e51adc2010-07-22 15:23:31 -0700626 usb_hcd_unlink_urb_from_ep(hcd, urb);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700627
628 spin_unlock(&xhci->lock);
629 usb_hcd_giveback_urb(hcd, urb, status);
630 xhci_urb_free_priv(xhci, urb_priv);
631 spin_lock(&xhci->lock);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700632 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700633}
634
Sarah Sharpae636742009-04-29 19:02:31 -0700635/*
636 * When we get a command completion for a Stop Endpoint Command, we need to
637 * unlink any cancelled TDs from the ring. There are two ways to do that:
638 *
639 * 1. If the HW was in the middle of processing the TD that needs to be
640 * cancelled, then we must move the ring's dequeue pointer past the last TRB
641 * in the TD with a Set Dequeue Pointer Command.
642 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
643 * bit cleared) so that the HW will skip over them.
644 */
645static void handle_stopped_endpoint(struct xhci_hcd *xhci,
Andiry Xube88fe42010-10-14 07:22:57 -0700646 union xhci_trb *trb, struct xhci_event_cmd *event)
Sarah Sharpae636742009-04-29 19:02:31 -0700647{
648 unsigned int slot_id;
649 unsigned int ep_index;
Andiry Xube88fe42010-10-14 07:22:57 -0700650 struct xhci_virt_device *virt_dev;
Sarah Sharpae636742009-04-29 19:02:31 -0700651 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700652 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700653 struct list_head *entry;
Randy Dunlap326b4812010-04-19 08:53:50 -0700654 struct xhci_td *cur_td = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700655 struct xhci_td *last_unlinked_td;
656
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700657 struct xhci_dequeue_state deq_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700658
Andiry Xube88fe42010-10-14 07:22:57 -0700659 if (unlikely(TRB_TO_SUSPEND_PORT(
Matt Evans28ccd292011-03-29 13:40:46 +1100660 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])))) {
Andiry Xube88fe42010-10-14 07:22:57 -0700661 slot_id = TRB_TO_SLOT_ID(
Matt Evans28ccd292011-03-29 13:40:46 +1100662 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
Andiry Xube88fe42010-10-14 07:22:57 -0700663 virt_dev = xhci->devs[slot_id];
664 if (virt_dev)
665 handle_cmd_in_cmd_wait_list(xhci, virt_dev,
666 event);
667 else
668 xhci_warn(xhci, "Stop endpoint command "
669 "completion for disabled slot %u\n",
670 slot_id);
671 return;
672 }
673
Sarah Sharpae636742009-04-29 19:02:31 -0700674 memset(&deq_state, 0, sizeof(deq_state));
Matt Evans28ccd292011-03-29 13:40:46 +1100675 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
676 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700677 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharpae636742009-04-29 19:02:31 -0700678
Sarah Sharp678539c2009-10-27 10:55:52 -0700679 if (list_empty(&ep->cancelled_td_list)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700680 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharp0714a572011-05-24 11:53:29 -0700681 ep->stopped_td = NULL;
682 ep->stopped_trb = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700683 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700684 return;
Sarah Sharp678539c2009-10-27 10:55:52 -0700685 }
Sarah Sharpae636742009-04-29 19:02:31 -0700686
687 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
688 * We have the xHCI lock, so nothing can modify this list until we drop
689 * it. We're also in the event handler, so we can't get re-interrupted
690 * if another Stop Endpoint command completes
691 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700692 list_for_each(entry, &ep->cancelled_td_list) {
Sarah Sharpae636742009-04-29 19:02:31 -0700693 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700694 xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
695 cur_td->first_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700696 (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700697 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
698 if (!ep_ring) {
699 /* This shouldn't happen unless a driver is mucking
700 * with the stream ID after submission. This will
701 * leave the TD on the hardware ring, and the hardware
702 * will try to execute it, and may access a buffer
703 * that has already been freed. In the best case, the
704 * hardware will execute it, and the event handler will
705 * ignore the completion event for that TD, since it was
706 * removed from the td_list for that endpoint. In
707 * short, don't muck with the stream ID after
708 * submission.
709 */
710 xhci_warn(xhci, "WARN Cancelled URB %p "
711 "has invalid stream ID %u.\n",
712 cur_td->urb,
713 cur_td->urb->stream_id);
714 goto remove_finished_td;
715 }
Sarah Sharpae636742009-04-29 19:02:31 -0700716 /*
717 * If we stopped on the TD we need to cancel, then we have to
718 * move the xHC endpoint ring dequeue pointer past this TD.
719 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700720 if (cur_td == ep->stopped_td)
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700721 xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
722 cur_td->urb->stream_id,
723 cur_td, &deq_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700724 else
725 td_to_noop(xhci, ep_ring, cur_td);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700726remove_finished_td:
Sarah Sharpae636742009-04-29 19:02:31 -0700727 /*
728 * The event handler won't see a completion for this TD anymore,
729 * so remove it from the endpoint ring's TD list. Keep it in
730 * the cancelled TD list for URB completion later.
731 */
732 list_del(&cur_td->td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700733 }
734 last_unlinked_td = cur_td;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700735 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharpae636742009-04-29 19:02:31 -0700736
737 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
738 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700739 xhci_queue_new_dequeue_state(xhci,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700740 slot_id, ep_index,
741 ep->stopped_td->urb->stream_id,
742 &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700743 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700744 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700745 /* Otherwise ring the doorbell(s) to restart queued transfers */
746 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700747 }
Sarah Sharp1624ae12010-05-06 13:40:08 -0700748 ep->stopped_td = NULL;
749 ep->stopped_trb = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700750
751 /*
752 * Drop the lock and complete the URBs in the cancelled TD list.
753 * New TDs to be cancelled might be added to the end of the list before
754 * we can complete all the URBs for the TDs we already unlinked.
755 * So stop when we've completed the URB for the last TD we unlinked.
756 */
757 do {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700758 cur_td = list_entry(ep->cancelled_td_list.next,
Sarah Sharpae636742009-04-29 19:02:31 -0700759 struct xhci_td, cancelled_td_list);
760 list_del(&cur_td->cancelled_td_list);
761
762 /* Clean up the cancelled URB */
Sarah Sharpae636742009-04-29 19:02:31 -0700763 /* Doesn't matter what we pass for status, since the core will
764 * just overwrite it (because the URB has been unlinked).
765 */
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700766 xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
Sarah Sharpae636742009-04-29 19:02:31 -0700767
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700768 /* Stop processing the cancelled list if the watchdog timer is
769 * running.
770 */
771 if (xhci->xhc_state & XHCI_STATE_DYING)
772 return;
Sarah Sharpae636742009-04-29 19:02:31 -0700773 } while (cur_td != last_unlinked_td);
774
775 /* Return to the event handler with xhci->lock re-acquired */
776}
777
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700778/* Watchdog timer function for when a stop endpoint command fails to complete.
779 * In this case, we assume the host controller is broken or dying or dead. The
780 * host may still be completing some other events, so we have to be careful to
781 * let the event ring handler and the URB dequeueing/enqueueing functions know
782 * through xhci->state.
783 *
784 * The timer may also fire if the host takes a very long time to respond to the
785 * command, and the stop endpoint command completion handler cannot delete the
786 * timer before the timer function is called. Another endpoint cancellation may
787 * sneak in before the timer function can grab the lock, and that may queue
788 * another stop endpoint command and add the timer back. So we cannot use a
789 * simple flag to say whether there is a pending stop endpoint command for a
790 * particular endpoint.
791 *
792 * Instead we use a combination of that flag and a counter for the number of
793 * pending stop endpoint commands. If the timer is the tail end of the last
794 * stop endpoint command, and the endpoint's command is still pending, we assume
795 * the host is dying.
796 */
797void xhci_stop_endpoint_command_watchdog(unsigned long arg)
798{
799 struct xhci_hcd *xhci;
800 struct xhci_virt_ep *ep;
801 struct xhci_virt_ep *temp_ep;
802 struct xhci_ring *ring;
803 struct xhci_td *cur_td;
804 int ret, i, j;
805
806 ep = (struct xhci_virt_ep *) arg;
807 xhci = ep->xhci;
808
809 spin_lock(&xhci->lock);
810
811 ep->stop_cmds_pending--;
812 if (xhci->xhc_state & XHCI_STATE_DYING) {
813 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
814 "xHCI as DYING, exiting.\n");
815 spin_unlock(&xhci->lock);
816 return;
817 }
818 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
819 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
820 "exiting.\n");
821 spin_unlock(&xhci->lock);
822 return;
823 }
824
825 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
826 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
827 /* Oops, HC is dead or dying or at least not responding to the stop
828 * endpoint command.
829 */
830 xhci->xhc_state |= XHCI_STATE_DYING;
831 /* Disable interrupts from the host controller and start halting it */
832 xhci_quiesce(xhci);
833 spin_unlock(&xhci->lock);
834
835 ret = xhci_halt(xhci);
836
837 spin_lock(&xhci->lock);
838 if (ret < 0) {
839 /* This is bad; the host is not responding to commands and it's
840 * not allowing itself to be halted. At least interrupts are
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800841 * disabled. If we call usb_hc_died(), it will attempt to
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700842 * disconnect all device drivers under this host. Those
843 * disconnect() methods will wait for all URBs to be unlinked,
844 * so we must complete them.
845 */
846 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
847 xhci_warn(xhci, "Completing active URBs anyway.\n");
848 /* We could turn all TDs on the rings to no-ops. This won't
849 * help if the host has cached part of the ring, and is slow if
850 * we want to preserve the cycle bit. Skip it and hope the host
851 * doesn't touch the memory.
852 */
853 }
854 for (i = 0; i < MAX_HC_SLOTS; i++) {
855 if (!xhci->devs[i])
856 continue;
857 for (j = 0; j < 31; j++) {
858 temp_ep = &xhci->devs[i]->eps[j];
859 ring = temp_ep->ring;
860 if (!ring)
861 continue;
862 xhci_dbg(xhci, "Killing URBs for slot ID %u, "
863 "ep index %u\n", i, j);
864 while (!list_empty(&ring->td_list)) {
865 cur_td = list_first_entry(&ring->td_list,
866 struct xhci_td,
867 td_list);
868 list_del(&cur_td->td_list);
869 if (!list_empty(&cur_td->cancelled_td_list))
870 list_del(&cur_td->cancelled_td_list);
871 xhci_giveback_urb_in_irq(xhci, cur_td,
872 -ESHUTDOWN, "killed");
873 }
874 while (!list_empty(&temp_ep->cancelled_td_list)) {
875 cur_td = list_first_entry(
876 &temp_ep->cancelled_td_list,
877 struct xhci_td,
878 cancelled_td_list);
879 list_del(&cur_td->cancelled_td_list);
880 xhci_giveback_urb_in_irq(xhci, cur_td,
881 -ESHUTDOWN, "killed");
882 }
883 }
884 }
885 spin_unlock(&xhci->lock);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700886 xhci_dbg(xhci, "Calling usb_hc_died()\n");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800887 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700888 xhci_dbg(xhci, "xHCI host controller is dead.\n");
889}
890
Sarah Sharpae636742009-04-29 19:02:31 -0700891/*
892 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
893 * we need to clear the set deq pending flag in the endpoint ring state, so that
894 * the TD queueing code can ring the doorbell again. We also need to ring the
895 * endpoint doorbell to restart the ring, but only if there aren't more
896 * cancellations pending.
897 */
898static void handle_set_deq_completion(struct xhci_hcd *xhci,
899 struct xhci_event_cmd *event,
900 union xhci_trb *trb)
901{
902 unsigned int slot_id;
903 unsigned int ep_index;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700904 unsigned int stream_id;
Sarah Sharpae636742009-04-29 19:02:31 -0700905 struct xhci_ring *ep_ring;
906 struct xhci_virt_device *dev;
John Yound115b042009-07-27 12:05:15 -0700907 struct xhci_ep_ctx *ep_ctx;
908 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpae636742009-04-29 19:02:31 -0700909
Matt Evans28ccd292011-03-29 13:40:46 +1100910 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
911 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
912 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
Sarah Sharpae636742009-04-29 19:02:31 -0700913 dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700914
915 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
916 if (!ep_ring) {
917 xhci_warn(xhci, "WARN Set TR deq ptr command for "
918 "freed stream ID %u\n",
919 stream_id);
920 /* XXX: Harmless??? */
921 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
922 return;
923 }
924
John Yound115b042009-07-27 12:05:15 -0700925 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
926 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
Sarah Sharpae636742009-04-29 19:02:31 -0700927
Matt Evans28ccd292011-03-29 13:40:46 +1100928 if (GET_COMP_CODE(le32_to_cpu(event->status)) != COMP_SUCCESS) {
Sarah Sharpae636742009-04-29 19:02:31 -0700929 unsigned int ep_state;
930 unsigned int slot_state;
931
Matt Evans28ccd292011-03-29 13:40:46 +1100932 switch (GET_COMP_CODE(le32_to_cpu(event->status))) {
Sarah Sharpae636742009-04-29 19:02:31 -0700933 case COMP_TRB_ERR:
934 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
935 "of stream ID configuration\n");
936 break;
937 case COMP_CTX_STATE:
938 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
939 "to incorrect slot or ep state.\n");
Matt Evans28ccd292011-03-29 13:40:46 +1100940 ep_state = le32_to_cpu(ep_ctx->ep_info);
Sarah Sharpae636742009-04-29 19:02:31 -0700941 ep_state &= EP_STATE_MASK;
Matt Evans28ccd292011-03-29 13:40:46 +1100942 slot_state = le32_to_cpu(slot_ctx->dev_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700943 slot_state = GET_SLOT_STATE(slot_state);
944 xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
945 slot_state, ep_state);
946 break;
947 case COMP_EBADSLT:
948 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
949 "slot %u was not enabled.\n", slot_id);
950 break;
951 default:
952 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
953 "completion code of %u.\n",
Matt Evans28ccd292011-03-29 13:40:46 +1100954 GET_COMP_CODE(le32_to_cpu(event->status)));
Sarah Sharpae636742009-04-29 19:02:31 -0700955 break;
956 }
957 /* OK what do we do now? The endpoint state is hosed, and we
958 * should never get to this point if the synchronization between
959 * queueing, and endpoint state are correct. This might happen
960 * if the device gets disconnected after we've finished
961 * cancelling URBs, which might not be an error...
962 */
963 } else {
Sarah Sharp8e595a52009-07-27 12:03:31 -0700964 xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
Matt Evans28ccd292011-03-29 13:40:46 +1100965 le64_to_cpu(ep_ctx->deq));
Sarah Sharpbf161e82011-02-23 15:46:42 -0800966 if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg,
Matt Evans28ccd292011-03-29 13:40:46 +1100967 dev->eps[ep_index].queued_deq_ptr) ==
968 (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) {
Sarah Sharpbf161e82011-02-23 15:46:42 -0800969 /* Update the ring's dequeue segment and dequeue pointer
970 * to reflect the new position.
971 */
972 ep_ring->deq_seg = dev->eps[ep_index].queued_deq_seg;
973 ep_ring->dequeue = dev->eps[ep_index].queued_deq_ptr;
974 } else {
975 xhci_warn(xhci, "Mismatch between completed Set TR Deq "
976 "Ptr command & xHCI internal state.\n");
977 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
978 dev->eps[ep_index].queued_deq_seg,
979 dev->eps[ep_index].queued_deq_ptr);
980 }
Sarah Sharpae636742009-04-29 19:02:31 -0700981 }
982
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700983 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
Sarah Sharpbf161e82011-02-23 15:46:42 -0800984 dev->eps[ep_index].queued_deq_seg = NULL;
985 dev->eps[ep_index].queued_deq_ptr = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700986 /* Restart any rings with pending URBs */
987 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700988}
989
Sarah Sharpa1587d92009-07-27 12:03:15 -0700990static void handle_reset_ep_completion(struct xhci_hcd *xhci,
991 struct xhci_event_cmd *event,
992 union xhci_trb *trb)
993{
994 int slot_id;
995 unsigned int ep_index;
996
Matt Evans28ccd292011-03-29 13:40:46 +1100997 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
998 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharpa1587d92009-07-27 12:03:15 -0700999 /* This command will only fail if the endpoint wasn't halted,
1000 * but we don't care.
1001 */
1002 xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001003 (unsigned int) GET_COMP_CODE(le32_to_cpu(event->status)));
Sarah Sharpa1587d92009-07-27 12:03:15 -07001004
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001005 /* HW with the reset endpoint quirk needs to have a configure endpoint
1006 * command complete before the endpoint can be used. Queue that here
1007 * because the HW can't handle two commands being queued in a row.
1008 */
1009 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
1010 xhci_dbg(xhci, "Queueing configure endpoint command\n");
1011 xhci_queue_configure_endpoint(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001012 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1013 false);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001014 xhci_ring_cmd_db(xhci);
1015 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001016 /* Clear our internal halted state and restart the ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001017 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001018 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001019 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001020}
Sarah Sharpae636742009-04-29 19:02:31 -07001021
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001022/* Check to see if a command in the device's command queue matches this one.
1023 * Signal the completion or free the command, and return 1. Return 0 if the
1024 * completed command isn't at the head of the command list.
1025 */
1026static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
1027 struct xhci_virt_device *virt_dev,
1028 struct xhci_event_cmd *event)
1029{
1030 struct xhci_command *command;
1031
1032 if (list_empty(&virt_dev->cmd_list))
1033 return 0;
1034
1035 command = list_entry(virt_dev->cmd_list.next,
1036 struct xhci_command, cmd_list);
1037 if (xhci->cmd_ring->dequeue != command->command_trb)
1038 return 0;
1039
Matt Evans28ccd292011-03-29 13:40:46 +11001040 command->status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001041 list_del(&command->cmd_list);
1042 if (command->completion)
1043 complete(command->completion);
1044 else
1045 xhci_free_command(xhci, command);
1046 return 1;
1047}
1048
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001049static void handle_cmd_completion(struct xhci_hcd *xhci,
1050 struct xhci_event_cmd *event)
1051{
Matt Evans28ccd292011-03-29 13:40:46 +11001052 int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001053 u64 cmd_dma;
1054 dma_addr_t cmd_dequeue_dma;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001055 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001056 struct xhci_virt_device *virt_dev;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001057 unsigned int ep_index;
1058 struct xhci_ring *ep_ring;
1059 unsigned int ep_state;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001060
Matt Evans28ccd292011-03-29 13:40:46 +11001061 cmd_dma = le64_to_cpu(event->cmd_trb);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001062 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001063 xhci->cmd_ring->dequeue);
1064 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1065 if (cmd_dequeue_dma == 0) {
1066 xhci->error_bitmask |= 1 << 4;
1067 return;
1068 }
1069 /* Does the DMA address match our internal dequeue pointer address? */
1070 if (cmd_dma != (u64) cmd_dequeue_dma) {
1071 xhci->error_bitmask |= 1 << 5;
1072 return;
1073 }
Matt Evans28ccd292011-03-29 13:40:46 +11001074 switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])
1075 & TRB_TYPE_BITMASK) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001076 case TRB_TYPE(TRB_ENABLE_SLOT):
Matt Evans28ccd292011-03-29 13:40:46 +11001077 if (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_SUCCESS)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001078 xhci->slot_id = slot_id;
1079 else
1080 xhci->slot_id = 0;
1081 complete(&xhci->addr_dev);
1082 break;
1083 case TRB_TYPE(TRB_DISABLE_SLOT):
1084 if (xhci->devs[slot_id])
1085 xhci_free_virt_device(xhci, slot_id);
1086 break;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001087 case TRB_TYPE(TRB_CONFIG_EP):
Sarah Sharp913a8a32009-09-04 10:53:13 -07001088 virt_dev = xhci->devs[slot_id];
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001089 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
Sarah Sharp913a8a32009-09-04 10:53:13 -07001090 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001091 /*
1092 * Configure endpoint commands can come from the USB core
1093 * configuration or alt setting changes, or because the HW
1094 * needed an extra configure endpoint command after a reset
Sarah Sharp8df75f42010-04-02 15:34:16 -07001095 * endpoint command or streams were being configured.
1096 * If the command was for a halted endpoint, the xHCI driver
1097 * is not waiting on the configure endpoint command.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001098 */
1099 ctrl_ctx = xhci_get_input_control_ctx(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001100 virt_dev->in_ctx);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001101 /* Input ctx add_flags are the endpoint index plus one */
Matt Evans28ccd292011-03-29 13:40:46 +11001102 ep_index = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags)) - 1;
Sarah Sharp06df5722009-12-03 09:44:31 -08001103 /* A usb_set_interface() call directly after clearing a halted
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001104 * condition may race on this quirky hardware. Not worth
1105 * worrying about, since this is prototype hardware. Not sure
1106 * if this will work for streams, but streams support was
1107 * untested on this prototype.
Sarah Sharp06df5722009-12-03 09:44:31 -08001108 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001109 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
Sarah Sharp06df5722009-12-03 09:44:31 -08001110 ep_index != (unsigned int) -1 &&
Matt Evans28ccd292011-03-29 13:40:46 +11001111 le32_to_cpu(ctrl_ctx->add_flags) - SLOT_FLAG ==
1112 le32_to_cpu(ctrl_ctx->drop_flags)) {
Sarah Sharp06df5722009-12-03 09:44:31 -08001113 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
1114 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1115 if (!(ep_state & EP_HALTED))
1116 goto bandwidth_change;
1117 xhci_dbg(xhci, "Completed config ep cmd - "
1118 "last ep index = %d, state = %d\n",
1119 ep_index, ep_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001120 /* Clear internal halted state and restart ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001121 xhci->devs[slot_id]->eps[ep_index].ep_state &=
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001122 ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001123 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharp06df5722009-12-03 09:44:31 -08001124 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001125 }
Sarah Sharp06df5722009-12-03 09:44:31 -08001126bandwidth_change:
1127 xhci_dbg(xhci, "Completed config ep cmd\n");
1128 xhci->devs[slot_id]->cmd_status =
Matt Evans28ccd292011-03-29 13:40:46 +11001129 GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp06df5722009-12-03 09:44:31 -08001130 complete(&xhci->devs[slot_id]->cmd_completion);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001131 break;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001132 case TRB_TYPE(TRB_EVAL_CONTEXT):
Sarah Sharpac1c1b72009-09-04 10:53:20 -07001133 virt_dev = xhci->devs[slot_id];
1134 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
1135 break;
Matt Evans28ccd292011-03-29 13:40:46 +11001136 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001137 complete(&xhci->devs[slot_id]->cmd_completion);
1138 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001139 case TRB_TYPE(TRB_ADDR_DEV):
Matt Evans28ccd292011-03-29 13:40:46 +11001140 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001141 complete(&xhci->addr_dev);
1142 break;
Sarah Sharpae636742009-04-29 19:02:31 -07001143 case TRB_TYPE(TRB_STOP_RING):
Andiry Xube88fe42010-10-14 07:22:57 -07001144 handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event);
Sarah Sharpae636742009-04-29 19:02:31 -07001145 break;
1146 case TRB_TYPE(TRB_SET_DEQ):
1147 handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
1148 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001149 case TRB_TYPE(TRB_CMD_NOOP):
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001150 break;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001151 case TRB_TYPE(TRB_RESET_EP):
1152 handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
1153 break;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001154 case TRB_TYPE(TRB_RESET_DEV):
1155 xhci_dbg(xhci, "Completed reset device command.\n");
1156 slot_id = TRB_TO_SLOT_ID(
Matt Evans28ccd292011-03-29 13:40:46 +11001157 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001158 virt_dev = xhci->devs[slot_id];
1159 if (virt_dev)
1160 handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
1161 else
1162 xhci_warn(xhci, "Reset device command completion "
1163 "for disabled slot %u\n", slot_id);
1164 break;
Sarah Sharp02386342010-05-24 13:25:28 -07001165 case TRB_TYPE(TRB_NEC_GET_FW):
1166 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1167 xhci->error_bitmask |= 1 << 6;
1168 break;
1169 }
1170 xhci_dbg(xhci, "NEC firmware version %2x.%02x\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001171 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1172 NEC_FW_MINOR(le32_to_cpu(event->status)));
Sarah Sharp02386342010-05-24 13:25:28 -07001173 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001174 default:
1175 /* Skip over unknown commands on the event ring */
1176 xhci->error_bitmask |= 1 << 6;
1177 break;
1178 }
1179 inc_deq(xhci, xhci->cmd_ring, false);
1180}
1181
Sarah Sharp02386342010-05-24 13:25:28 -07001182static void handle_vendor_event(struct xhci_hcd *xhci,
1183 union xhci_trb *event)
1184{
1185 u32 trb_type;
1186
Matt Evans28ccd292011-03-29 13:40:46 +11001187 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
Sarah Sharp02386342010-05-24 13:25:28 -07001188 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1189 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1190 handle_cmd_completion(xhci, &event->event_cmd);
1191}
1192
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001193/* @port_id: the one-based port ID from the hardware (indexed from array of all
1194 * port registers -- USB 3.0 and USB 2.0).
1195 *
1196 * Returns a zero-based port number, which is suitable for indexing into each of
1197 * the split roothubs' port arrays and bus state arrays.
1198 */
1199static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1200 struct xhci_hcd *xhci, u32 port_id)
1201{
1202 unsigned int i;
1203 unsigned int num_similar_speed_ports = 0;
1204
1205 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1206 * and usb2_ports are 0-based indexes. Count the number of similar
1207 * speed ports, up to 1 port before this port.
1208 */
1209 for (i = 0; i < (port_id - 1); i++) {
1210 u8 port_speed = xhci->port_array[i];
1211
1212 /*
1213 * Skip ports that don't have known speeds, or have duplicate
1214 * Extended Capabilities port speed entries.
1215 */
Dan Carpenter22e04872011-03-17 22:39:49 +03001216 if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001217 continue;
1218
1219 /*
1220 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1221 * 1.1 ports are under the USB 2.0 hub. If the port speed
1222 * matches the device speed, it's a similar speed port.
1223 */
1224 if ((port_speed == 0x03) == (hcd->speed == HCD_USB3))
1225 num_similar_speed_ports++;
1226 }
1227 return num_similar_speed_ports;
1228}
1229
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001230static void handle_port_status(struct xhci_hcd *xhci,
1231 union xhci_trb *event)
1232{
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001233 struct usb_hcd *hcd;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001234 u32 port_id;
Andiry Xu56192532010-10-14 07:23:00 -07001235 u32 temp, temp1;
Sarah Sharp518e8482010-12-15 11:56:29 -08001236 int max_ports;
Andiry Xu56192532010-10-14 07:23:00 -07001237 int slot_id;
Sarah Sharp5308a912010-12-01 11:34:59 -08001238 unsigned int faked_port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001239 u8 major_revision;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001240 struct xhci_bus_state *bus_state;
Matt Evans28ccd292011-03-29 13:40:46 +11001241 __le32 __iomem **port_array;
Sarah Sharp386139d2011-03-24 08:02:58 -07001242 bool bogus_port_status = false;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001243
1244 /* Port status change events always have a successful completion code */
Matt Evans28ccd292011-03-29 13:40:46 +11001245 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001246 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1247 xhci->error_bitmask |= 1 << 8;
1248 }
Matt Evans28ccd292011-03-29 13:40:46 +11001249 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001250 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1251
Sarah Sharp518e8482010-12-15 11:56:29 -08001252 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1253 if ((port_id <= 0) || (port_id > max_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001254 xhci_warn(xhci, "Invalid port id %d\n", port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001255 bogus_port_status = true;
Andiry Xu56192532010-10-14 07:23:00 -07001256 goto cleanup;
1257 }
1258
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001259 /* Figure out which usb_hcd this port is attached to:
1260 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1261 */
1262 major_revision = xhci->port_array[port_id - 1];
1263 if (major_revision == 0) {
1264 xhci_warn(xhci, "Event for port %u not in "
1265 "Extended Capabilities, ignoring.\n",
1266 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001267 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001268 goto cleanup;
1269 }
Dan Carpenter22e04872011-03-17 22:39:49 +03001270 if (major_revision == DUPLICATE_ENTRY) {
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001271 xhci_warn(xhci, "Event for port %u duplicated in"
1272 "Extended Capabilities, ignoring.\n",
1273 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001274 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001275 goto cleanup;
Sarah Sharp5308a912010-12-01 11:34:59 -08001276 }
1277
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001278 /*
1279 * Hardware port IDs reported by a Port Status Change Event include USB
1280 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1281 * resume event, but we first need to translate the hardware port ID
1282 * into the index into the ports on the correct split roothub, and the
1283 * correct bus_state structure.
1284 */
1285 /* Find the right roothub. */
1286 hcd = xhci_to_hcd(xhci);
1287 if ((major_revision == 0x03) != (hcd->speed == HCD_USB3))
1288 hcd = xhci->shared_hcd;
1289 bus_state = &xhci->bus_state[hcd_index(hcd)];
1290 if (hcd->speed == HCD_USB3)
1291 port_array = xhci->usb3_ports;
1292 else
1293 port_array = xhci->usb2_ports;
1294 /* Find the faked port hub number */
1295 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1296 port_id);
1297
Sarah Sharp5308a912010-12-01 11:34:59 -08001298 temp = xhci_readl(xhci, port_array[faked_port_index]);
Sarah Sharp7111ebc2010-12-14 13:24:55 -08001299 if (hcd->state == HC_STATE_SUSPENDED) {
Andiry Xu56192532010-10-14 07:23:00 -07001300 xhci_dbg(xhci, "resume root hub\n");
1301 usb_hcd_resume_root_hub(hcd);
1302 }
1303
1304 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1305 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1306
1307 temp1 = xhci_readl(xhci, &xhci->op_regs->command);
1308 if (!(temp1 & CMD_RUN)) {
1309 xhci_warn(xhci, "xHC is not running.\n");
1310 goto cleanup;
1311 }
1312
1313 if (DEV_SUPERSPEED(temp)) {
1314 xhci_dbg(xhci, "resume SS port %d\n", port_id);
1315 temp = xhci_port_state_to_neutral(temp);
1316 temp &= ~PORT_PLS_MASK;
1317 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -08001318 xhci_writel(xhci, temp, port_array[faked_port_index]);
Sarah Sharp52336302010-12-16 10:49:09 -08001319 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1320 faked_port_index);
Andiry Xu56192532010-10-14 07:23:00 -07001321 if (!slot_id) {
1322 xhci_dbg(xhci, "slot_id is zero\n");
1323 goto cleanup;
1324 }
1325 xhci_ring_device(xhci, slot_id);
1326 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
1327 /* Clear PORT_PLC */
Sarah Sharp5308a912010-12-01 11:34:59 -08001328 temp = xhci_readl(xhci, port_array[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001329 temp = xhci_port_state_to_neutral(temp);
1330 temp |= PORT_PLC;
Sarah Sharp5308a912010-12-01 11:34:59 -08001331 xhci_writel(xhci, temp, port_array[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001332 } else {
1333 xhci_dbg(xhci, "resume HS port %d\n", port_id);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001334 bus_state->resume_done[faked_port_index] = jiffies +
Andiry Xu56192532010-10-14 07:23:00 -07001335 msecs_to_jiffies(20);
1336 mod_timer(&hcd->rh_timer,
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001337 bus_state->resume_done[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001338 /* Do the rest in GetPortStatus */
1339 }
1340 }
1341
1342cleanup:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001343 /* Update event ring dequeue pointer before dropping the lock */
1344 inc_deq(xhci, xhci->event_ring, true);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001345
Sarah Sharp386139d2011-03-24 08:02:58 -07001346 /* Don't make the USB core poll the roothub if we got a bad port status
1347 * change event. Besides, at that point we can't tell which roothub
1348 * (USB 2.0 or USB 3.0) to kick.
1349 */
1350 if (bogus_port_status)
1351 return;
1352
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001353 spin_unlock(&xhci->lock);
1354 /* Pass this up to the core */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001355 usb_hcd_poll_rh_status(hcd);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001356 spin_lock(&xhci->lock);
1357}
1358
1359/*
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001360 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1361 * at end_trb, which may be in another segment. If the suspect DMA address is a
1362 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1363 * returns 0.
1364 */
Sarah Sharp6648f292009-11-09 13:35:23 -08001365struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001366 union xhci_trb *start_trb,
1367 union xhci_trb *end_trb,
1368 dma_addr_t suspect_dma)
1369{
1370 dma_addr_t start_dma;
1371 dma_addr_t end_seg_dma;
1372 dma_addr_t end_trb_dma;
1373 struct xhci_segment *cur_seg;
1374
Sarah Sharp23e3be12009-04-29 19:05:20 -07001375 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001376 cur_seg = start_seg;
1377
1378 do {
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001379 if (start_dma == 0)
Randy Dunlap326b4812010-04-19 08:53:50 -07001380 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -07001381 /* We may get an event for a Link TRB in the middle of a TD */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001382 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001383 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001384 /* If the end TRB isn't in this segment, this is set to 0 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001385 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001386
1387 if (end_trb_dma > 0) {
1388 /* The end TRB is in this segment, so suspect should be here */
1389 if (start_dma <= end_trb_dma) {
1390 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1391 return cur_seg;
1392 } else {
1393 /* Case for one segment with
1394 * a TD wrapped around to the top
1395 */
1396 if ((suspect_dma >= start_dma &&
1397 suspect_dma <= end_seg_dma) ||
1398 (suspect_dma >= cur_seg->dma &&
1399 suspect_dma <= end_trb_dma))
1400 return cur_seg;
1401 }
Randy Dunlap326b4812010-04-19 08:53:50 -07001402 return NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001403 } else {
1404 /* Might still be somewhere in this segment */
1405 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1406 return cur_seg;
1407 }
1408 cur_seg = cur_seg->next;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001409 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001410 } while (cur_seg != start_seg);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001411
Randy Dunlap326b4812010-04-19 08:53:50 -07001412 return NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001413}
1414
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001415static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1416 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001417 unsigned int stream_id,
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001418 struct xhci_td *td, union xhci_trb *event_trb)
1419{
1420 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
1421 ep->ep_state |= EP_HALTED;
1422 ep->stopped_td = td;
1423 ep->stopped_trb = event_trb;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001424 ep->stopped_stream = stream_id;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001425
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001426 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1427 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
Sarah Sharp1624ae12010-05-06 13:40:08 -07001428
1429 ep->stopped_td = NULL;
1430 ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001431 ep->stopped_stream = 0;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001432
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001433 xhci_ring_cmd_db(xhci);
1434}
1435
1436/* Check if an error has halted the endpoint ring. The class driver will
1437 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1438 * However, a babble and other errors also halt the endpoint ring, and the class
1439 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1440 * Ring Dequeue Pointer command manually.
1441 */
1442static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1443 struct xhci_ep_ctx *ep_ctx,
1444 unsigned int trb_comp_code)
1445{
1446 /* TRB completion codes that may require a manual halt cleanup */
1447 if (trb_comp_code == COMP_TX_ERR ||
1448 trb_comp_code == COMP_BABBLE ||
1449 trb_comp_code == COMP_SPLIT_ERR)
1450 /* The 0.96 spec says a babbling control endpoint
1451 * is not halted. The 0.96 spec says it is. Some HW
1452 * claims to be 0.95 compliant, but it halts the control
1453 * endpoint anyway. Check if a babble halted the
1454 * endpoint.
1455 */
Matt Evans28ccd292011-03-29 13:40:46 +11001456 if ((le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) == EP_STATE_HALTED)
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001457 return 1;
1458
1459 return 0;
1460}
1461
Sarah Sharpb45b5062009-12-09 15:59:06 -08001462int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1463{
1464 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1465 /* Vendor defined "informational" completion code,
1466 * treat as not-an-error.
1467 */
1468 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1469 trb_comp_code);
1470 xhci_dbg(xhci, "Treating code as success.\n");
1471 return 1;
1472 }
1473 return 0;
1474}
1475
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001476/*
Andiry Xu4422da62010-07-22 15:22:55 -07001477 * Finish the td processing, remove the td from td list;
1478 * Return 1 if the urb can be given back.
1479 */
1480static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1481 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1482 struct xhci_virt_ep *ep, int *status, bool skip)
1483{
1484 struct xhci_virt_device *xdev;
1485 struct xhci_ring *ep_ring;
1486 unsigned int slot_id;
1487 int ep_index;
1488 struct urb *urb = NULL;
1489 struct xhci_ep_ctx *ep_ctx;
1490 int ret = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001491 struct urb_priv *urb_priv;
Andiry Xu4422da62010-07-22 15:22:55 -07001492 u32 trb_comp_code;
1493
Matt Evans28ccd292011-03-29 13:40:46 +11001494 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu4422da62010-07-22 15:22:55 -07001495 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001496 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1497 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu4422da62010-07-22 15:22:55 -07001498 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001499 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu4422da62010-07-22 15:22:55 -07001500
1501 if (skip)
1502 goto td_cleanup;
1503
1504 if (trb_comp_code == COMP_STOP_INVAL ||
1505 trb_comp_code == COMP_STOP) {
1506 /* The Endpoint Stop Command completion will take care of any
1507 * stopped TDs. A stopped TD may be restarted, so don't update
1508 * the ring dequeue pointer or take this TD off any lists yet.
1509 */
1510 ep->stopped_td = td;
1511 ep->stopped_trb = event_trb;
1512 return 0;
1513 } else {
1514 if (trb_comp_code == COMP_STALL) {
1515 /* The transfer is completed from the driver's
1516 * perspective, but we need to issue a set dequeue
1517 * command for this stalled endpoint to move the dequeue
1518 * pointer past the TD. We can't do that here because
1519 * the halt condition must be cleared first. Let the
1520 * USB class driver clear the stall later.
1521 */
1522 ep->stopped_td = td;
1523 ep->stopped_trb = event_trb;
1524 ep->stopped_stream = ep_ring->stream_id;
1525 } else if (xhci_requires_manual_halt_cleanup(xhci,
1526 ep_ctx, trb_comp_code)) {
1527 /* Other types of errors halt the endpoint, but the
1528 * class driver doesn't call usb_reset_endpoint() unless
1529 * the error is -EPIPE. Clear the halted status in the
1530 * xHCI hardware manually.
1531 */
1532 xhci_cleanup_halted_endpoint(xhci,
1533 slot_id, ep_index, ep_ring->stream_id,
1534 td, event_trb);
1535 } else {
1536 /* Update ring dequeue pointer */
1537 while (ep_ring->dequeue != td->last_trb)
1538 inc_deq(xhci, ep_ring, false);
1539 inc_deq(xhci, ep_ring, false);
1540 }
1541
1542td_cleanup:
1543 /* Clean up the endpoint's TD list */
1544 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001545 urb_priv = urb->hcpriv;
Andiry Xu4422da62010-07-22 15:22:55 -07001546
1547 /* Do one last check of the actual transfer length.
1548 * If the host controller said we transferred more data than
1549 * the buffer length, urb->actual_length will be a very big
1550 * number (since it's unsigned). Play it safe and say we didn't
1551 * transfer anything.
1552 */
1553 if (urb->actual_length > urb->transfer_buffer_length) {
1554 xhci_warn(xhci, "URB transfer length is wrong, "
1555 "xHC issue? req. len = %u, "
1556 "act. len = %u\n",
1557 urb->transfer_buffer_length,
1558 urb->actual_length);
1559 urb->actual_length = 0;
1560 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1561 *status = -EREMOTEIO;
1562 else
1563 *status = 0;
1564 }
1565 list_del(&td->td_list);
1566 /* Was this TD slated to be cancelled but completed anyway? */
1567 if (!list_empty(&td->cancelled_td_list))
1568 list_del(&td->cancelled_td_list);
1569
Andiry Xu8e51adc2010-07-22 15:23:31 -07001570 urb_priv->td_cnt++;
1571 /* Giveback the urb when all the tds are completed */
Andiry Xuc41136b2011-03-22 17:08:14 +08001572 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001573 ret = 1;
Andiry Xuc41136b2011-03-22 17:08:14 +08001574 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1575 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
1576 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs
1577 == 0) {
1578 if (xhci->quirks & XHCI_AMD_PLL_FIX)
1579 usb_amd_quirk_pll_enable();
1580 }
1581 }
1582 }
Andiry Xu4422da62010-07-22 15:22:55 -07001583 }
1584
1585 return ret;
1586}
1587
1588/*
Andiry Xu8af56be2010-07-22 15:23:03 -07001589 * Process control tds, update urb status and actual_length.
1590 */
1591static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1592 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1593 struct xhci_virt_ep *ep, int *status)
1594{
1595 struct xhci_virt_device *xdev;
1596 struct xhci_ring *ep_ring;
1597 unsigned int slot_id;
1598 int ep_index;
1599 struct xhci_ep_ctx *ep_ctx;
1600 u32 trb_comp_code;
1601
Matt Evans28ccd292011-03-29 13:40:46 +11001602 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu8af56be2010-07-22 15:23:03 -07001603 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001604 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1605 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu8af56be2010-07-22 15:23:03 -07001606 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001607 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001608
1609 xhci_debug_trb(xhci, xhci->event_ring->dequeue);
1610 switch (trb_comp_code) {
1611 case COMP_SUCCESS:
1612 if (event_trb == ep_ring->dequeue) {
1613 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1614 "without IOC set??\n");
1615 *status = -ESHUTDOWN;
1616 } else if (event_trb != td->last_trb) {
1617 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1618 "without IOC set??\n");
1619 *status = -ESHUTDOWN;
1620 } else {
Andiry Xu8af56be2010-07-22 15:23:03 -07001621 *status = 0;
1622 }
1623 break;
1624 case COMP_SHORT_TX:
1625 xhci_warn(xhci, "WARN: short transfer on control ep\n");
1626 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1627 *status = -EREMOTEIO;
1628 else
1629 *status = 0;
1630 break;
Sarah Sharp3abeca92011-05-05 19:08:09 -07001631 case COMP_STOP_INVAL:
1632 case COMP_STOP:
1633 return finish_td(xhci, td, event_trb, event, ep, status, false);
Andiry Xu8af56be2010-07-22 15:23:03 -07001634 default:
1635 if (!xhci_requires_manual_halt_cleanup(xhci,
1636 ep_ctx, trb_comp_code))
1637 break;
1638 xhci_dbg(xhci, "TRB error code %u, "
1639 "halted endpoint index = %u\n",
1640 trb_comp_code, ep_index);
1641 /* else fall through */
1642 case COMP_STALL:
1643 /* Did we transfer part of the data (middle) phase? */
1644 if (event_trb != ep_ring->dequeue &&
1645 event_trb != td->last_trb)
1646 td->urb->actual_length =
1647 td->urb->transfer_buffer_length
Matt Evans28ccd292011-03-29 13:40:46 +11001648 - TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001649 else
1650 td->urb->actual_length = 0;
1651
1652 xhci_cleanup_halted_endpoint(xhci,
1653 slot_id, ep_index, 0, td, event_trb);
1654 return finish_td(xhci, td, event_trb, event, ep, status, true);
1655 }
1656 /*
1657 * Did we transfer any data, despite the errors that might have
1658 * happened? I.e. did we get past the setup stage?
1659 */
1660 if (event_trb != ep_ring->dequeue) {
1661 /* The event was for the status stage */
1662 if (event_trb == td->last_trb) {
1663 if (td->urb->actual_length != 0) {
1664 /* Don't overwrite a previously set error code
1665 */
1666 if ((*status == -EINPROGRESS || *status == 0) &&
1667 (td->urb->transfer_flags
1668 & URB_SHORT_NOT_OK))
1669 /* Did we already see a short data
1670 * stage? */
1671 *status = -EREMOTEIO;
1672 } else {
1673 td->urb->actual_length =
1674 td->urb->transfer_buffer_length;
1675 }
1676 } else {
1677 /* Maybe the event was for the data stage? */
Sarah Sharp3abeca92011-05-05 19:08:09 -07001678 td->urb->actual_length =
1679 td->urb->transfer_buffer_length -
1680 TRB_LEN(le32_to_cpu(event->transfer_len));
1681 xhci_dbg(xhci, "Waiting for status "
1682 "stage event\n");
1683 return 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07001684 }
1685 }
1686
1687 return finish_td(xhci, td, event_trb, event, ep, status, false);
1688}
1689
1690/*
Andiry Xu04e51902010-07-22 15:23:39 -07001691 * Process isochronous tds, update urb packet status and actual_length.
1692 */
1693static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1694 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1695 struct xhci_virt_ep *ep, int *status)
1696{
1697 struct xhci_ring *ep_ring;
1698 struct urb_priv *urb_priv;
1699 int idx;
1700 int len = 0;
Andiry Xu04e51902010-07-22 15:23:39 -07001701 union xhci_trb *cur_trb;
1702 struct xhci_segment *cur_seg;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001703 struct usb_iso_packet_descriptor *frame;
Andiry Xu04e51902010-07-22 15:23:39 -07001704 u32 trb_comp_code;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001705 bool skip_td = false;
Andiry Xu04e51902010-07-22 15:23:39 -07001706
Matt Evans28ccd292011-03-29 13:40:46 +11001707 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1708 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07001709 urb_priv = td->urb->hcpriv;
1710 idx = urb_priv->td_cnt;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001711 frame = &td->urb->iso_frame_desc[idx];
Andiry Xu04e51902010-07-22 15:23:39 -07001712
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001713 /* handle completion code */
1714 switch (trb_comp_code) {
1715 case COMP_SUCCESS:
1716 frame->status = 0;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001717 break;
1718 case COMP_SHORT_TX:
1719 frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
1720 -EREMOTEIO : 0;
1721 break;
1722 case COMP_BW_OVER:
1723 frame->status = -ECOMM;
1724 skip_td = true;
1725 break;
1726 case COMP_BUFF_OVER:
1727 case COMP_BABBLE:
1728 frame->status = -EOVERFLOW;
1729 skip_td = true;
1730 break;
1731 case COMP_STALL:
1732 frame->status = -EPROTO;
1733 skip_td = true;
1734 break;
1735 case COMP_STOP:
1736 case COMP_STOP_INVAL:
1737 break;
1738 default:
1739 frame->status = -1;
1740 break;
Andiry Xu04e51902010-07-22 15:23:39 -07001741 }
1742
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001743 if (trb_comp_code == COMP_SUCCESS || skip_td) {
1744 frame->actual_length = frame->length;
1745 td->urb->actual_length += frame->length;
Andiry Xu04e51902010-07-22 15:23:39 -07001746 } else {
1747 for (cur_trb = ep_ring->dequeue,
1748 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
1749 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evans28ccd292011-03-29 13:40:46 +11001750 if ((le32_to_cpu(cur_trb->generic.field[3]) &
Andiry Xu04e51902010-07-22 15:23:39 -07001751 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) &&
Matt Evans28ccd292011-03-29 13:40:46 +11001752 (le32_to_cpu(cur_trb->generic.field[3]) &
Andiry Xu04e51902010-07-22 15:23:39 -07001753 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK))
Matt Evans28ccd292011-03-29 13:40:46 +11001754 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu04e51902010-07-22 15:23:39 -07001755 }
Matt Evans28ccd292011-03-29 13:40:46 +11001756 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1757 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07001758
1759 if (trb_comp_code != COMP_STOP_INVAL) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001760 frame->actual_length = len;
Andiry Xu04e51902010-07-22 15:23:39 -07001761 td->urb->actual_length += len;
1762 }
1763 }
1764
1765 if ((idx == urb_priv->length - 1) && *status == -EINPROGRESS)
1766 *status = 0;
1767
1768 return finish_td(xhci, td, event_trb, event, ep, status, false);
1769}
1770
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001771static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1772 struct xhci_transfer_event *event,
1773 struct xhci_virt_ep *ep, int *status)
1774{
1775 struct xhci_ring *ep_ring;
1776 struct urb_priv *urb_priv;
1777 struct usb_iso_packet_descriptor *frame;
1778 int idx;
1779
1780 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1781 urb_priv = td->urb->hcpriv;
1782 idx = urb_priv->td_cnt;
1783 frame = &td->urb->iso_frame_desc[idx];
1784
1785 /* The transfer is partly done */
1786 *status = -EXDEV;
1787 frame->status = -EXDEV;
1788
1789 /* calc actual length */
1790 frame->actual_length = 0;
1791
1792 /* Update ring dequeue pointer */
1793 while (ep_ring->dequeue != td->last_trb)
1794 inc_deq(xhci, ep_ring, false);
1795 inc_deq(xhci, ep_ring, false);
1796
1797 return finish_td(xhci, td, NULL, event, ep, status, true);
1798}
1799
Andiry Xu04e51902010-07-22 15:23:39 -07001800/*
Andiry Xu22405ed2010-07-22 15:23:08 -07001801 * Process bulk and interrupt tds, update urb status and actual_length.
1802 */
1803static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
1804 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1805 struct xhci_virt_ep *ep, int *status)
1806{
1807 struct xhci_ring *ep_ring;
1808 union xhci_trb *cur_trb;
1809 struct xhci_segment *cur_seg;
1810 u32 trb_comp_code;
1811
Matt Evans28ccd292011-03-29 13:40:46 +11001812 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1813 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001814
1815 switch (trb_comp_code) {
1816 case COMP_SUCCESS:
1817 /* Double check that the HW transferred everything. */
1818 if (event_trb != td->last_trb) {
1819 xhci_warn(xhci, "WARN Successful completion "
1820 "on short TX\n");
1821 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1822 *status = -EREMOTEIO;
1823 else
1824 *status = 0;
1825 } else {
Andiry Xu22405ed2010-07-22 15:23:08 -07001826 *status = 0;
1827 }
1828 break;
1829 case COMP_SHORT_TX:
1830 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1831 *status = -EREMOTEIO;
1832 else
1833 *status = 0;
1834 break;
1835 default:
1836 /* Others already handled above */
1837 break;
1838 }
Sarah Sharpf444ff22011-04-05 15:53:47 -07001839 if (trb_comp_code == COMP_SHORT_TX)
1840 xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
1841 "%d bytes untransferred\n",
1842 td->urb->ep->desc.bEndpointAddress,
1843 td->urb->transfer_buffer_length,
1844 TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07001845 /* Fast path - was this the last TRB in the TD for this URB? */
1846 if (event_trb == td->last_trb) {
Matt Evans28ccd292011-03-29 13:40:46 +11001847 if (TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
Andiry Xu22405ed2010-07-22 15:23:08 -07001848 td->urb->actual_length =
1849 td->urb->transfer_buffer_length -
Matt Evans28ccd292011-03-29 13:40:46 +11001850 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001851 if (td->urb->transfer_buffer_length <
1852 td->urb->actual_length) {
1853 xhci_warn(xhci, "HC gave bad length "
1854 "of %d bytes left\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001855 TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07001856 td->urb->actual_length = 0;
1857 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1858 *status = -EREMOTEIO;
1859 else
1860 *status = 0;
1861 }
1862 /* Don't overwrite a previously set error code */
1863 if (*status == -EINPROGRESS) {
1864 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1865 *status = -EREMOTEIO;
1866 else
1867 *status = 0;
1868 }
1869 } else {
1870 td->urb->actual_length =
1871 td->urb->transfer_buffer_length;
1872 /* Ignore a short packet completion if the
1873 * untransferred length was zero.
1874 */
1875 if (*status == -EREMOTEIO)
1876 *status = 0;
1877 }
1878 } else {
1879 /* Slow path - walk the list, starting from the dequeue
1880 * pointer, to get the actual length transferred.
1881 */
1882 td->urb->actual_length = 0;
1883 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
1884 cur_trb != event_trb;
1885 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evans28ccd292011-03-29 13:40:46 +11001886 if ((le32_to_cpu(cur_trb->generic.field[3]) &
Andiry Xu22405ed2010-07-22 15:23:08 -07001887 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) &&
Matt Evans28ccd292011-03-29 13:40:46 +11001888 (le32_to_cpu(cur_trb->generic.field[3]) &
Andiry Xu22405ed2010-07-22 15:23:08 -07001889 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK))
1890 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11001891 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu22405ed2010-07-22 15:23:08 -07001892 }
1893 /* If the ring didn't stop on a Link or No-op TRB, add
1894 * in the actual bytes transferred from the Normal TRB
1895 */
1896 if (trb_comp_code != COMP_STOP_INVAL)
1897 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11001898 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1899 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001900 }
1901
1902 return finish_td(xhci, td, event_trb, event, ep, status, false);
1903}
1904
1905/*
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001906 * If this function returns an error condition, it means it got a Transfer
1907 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
1908 * At this point, the host controller is probably hosed and should be reset.
1909 */
1910static int handle_tx_event(struct xhci_hcd *xhci,
1911 struct xhci_transfer_event *event)
1912{
1913 struct xhci_virt_device *xdev;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001914 struct xhci_virt_ep *ep;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001915 struct xhci_ring *ep_ring;
Sarah Sharp82d10092009-08-07 14:04:52 -07001916 unsigned int slot_id;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001917 int ep_index;
Randy Dunlap326b4812010-04-19 08:53:50 -07001918 struct xhci_td *td = NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001919 dma_addr_t event_dma;
1920 struct xhci_segment *event_seg;
1921 union xhci_trb *event_trb;
Randy Dunlap326b4812010-04-19 08:53:50 -07001922 struct urb *urb = NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001923 int status = -EINPROGRESS;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001924 struct urb_priv *urb_priv;
John Yound115b042009-07-27 12:05:15 -07001925 struct xhci_ep_ctx *ep_ctx;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07001926 u32 trb_comp_code;
Andiry Xu4422da62010-07-22 15:22:55 -07001927 int ret = 0;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001928
Matt Evans28ccd292011-03-29 13:40:46 +11001929 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp82d10092009-08-07 14:04:52 -07001930 xdev = xhci->devs[slot_id];
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001931 if (!xdev) {
1932 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
1933 return -ENODEV;
1934 }
1935
1936 /* Endpoint ID is 1 based, our index is zero based */
Matt Evans28ccd292011-03-29 13:40:46 +11001937 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001938 ep = &xdev->eps[ep_index];
Matt Evans28ccd292011-03-29 13:40:46 +11001939 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
John Yound115b042009-07-27 12:05:15 -07001940 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07001941 if (!ep_ring ||
Matt Evans28ccd292011-03-29 13:40:46 +11001942 (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
1943 EP_STATE_DISABLED) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001944 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
1945 "or incorrect stream ring\n");
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001946 return -ENODEV;
1947 }
1948
Matt Evans28ccd292011-03-29 13:40:46 +11001949 event_dma = le64_to_cpu(event->buffer);
1950 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu986a92d2010-07-22 15:23:20 -07001951 /* Look for common error cases */
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07001952 switch (trb_comp_code) {
Sarah Sharpb10de142009-04-27 19:58:50 -07001953 /* Skip codes that require special handling depending on
1954 * transfer type
1955 */
1956 case COMP_SUCCESS:
1957 case COMP_SHORT_TX:
1958 break;
Sarah Sharpae636742009-04-29 19:02:31 -07001959 case COMP_STOP:
1960 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
1961 break;
1962 case COMP_STOP_INVAL:
1963 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
1964 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07001965 case COMP_STALL:
1966 xhci_warn(xhci, "WARN: Stalled endpoint\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001967 ep->ep_state |= EP_HALTED;
Sarah Sharpb10de142009-04-27 19:58:50 -07001968 status = -EPIPE;
1969 break;
1970 case COMP_TRB_ERR:
1971 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
1972 status = -EILSEQ;
1973 break;
Sarah Sharpec74e402009-11-11 10:28:36 -08001974 case COMP_SPLIT_ERR:
Sarah Sharpb10de142009-04-27 19:58:50 -07001975 case COMP_TX_ERR:
1976 xhci_warn(xhci, "WARN: transfer error on endpoint\n");
1977 status = -EPROTO;
1978 break;
Sarah Sharp4a731432009-07-27 12:04:32 -07001979 case COMP_BABBLE:
1980 xhci_warn(xhci, "WARN: babble error on endpoint\n");
1981 status = -EOVERFLOW;
1982 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07001983 case COMP_DB_ERR:
1984 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
1985 status = -ENOSR;
1986 break;
Andiry Xu986a92d2010-07-22 15:23:20 -07001987 case COMP_BW_OVER:
1988 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
1989 break;
1990 case COMP_BUFF_OVER:
1991 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
1992 break;
1993 case COMP_UNDERRUN:
1994 /*
1995 * When the Isoch ring is empty, the xHC will generate
1996 * a Ring Overrun Event for IN Isoch endpoint or Ring
1997 * Underrun Event for OUT Isoch endpoint.
1998 */
1999 xhci_dbg(xhci, "underrun event on endpoint\n");
2000 if (!list_empty(&ep_ring->td_list))
2001 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2002 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002003 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2004 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002005 goto cleanup;
2006 case COMP_OVERRUN:
2007 xhci_dbg(xhci, "overrun event on endpoint\n");
2008 if (!list_empty(&ep_ring->td_list))
2009 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2010 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002011 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2012 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002013 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07002014 case COMP_MISSED_INT:
2015 /*
2016 * When encounter missed service error, one or more isoc tds
2017 * may be missed by xHC.
2018 * Set skip flag of the ep_ring; Complete the missed tds as
2019 * short transfer when process the ep_ring next time.
2020 */
2021 ep->skip = true;
2022 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
2023 goto cleanup;
Sarah Sharpb10de142009-04-27 19:58:50 -07002024 default:
Sarah Sharpb45b5062009-12-09 15:59:06 -08002025 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
Sarah Sharp5ad6a522009-11-11 10:28:40 -08002026 status = 0;
2027 break;
2028 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002029 xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
2030 "busted\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002031 goto cleanup;
2032 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002033
Andiry Xud18240d2010-07-22 15:23:25 -07002034 do {
2035 /* This TRB should be in the TD at the head of this ring's
2036 * TD list.
2037 */
2038 if (list_empty(&ep_ring->td_list)) {
2039 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
2040 "with no TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002041 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2042 ep_index);
Andiry Xud18240d2010-07-22 15:23:25 -07002043 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002044 (unsigned int) (le32_to_cpu(event->flags)
2045 & TRB_TYPE_BITMASK)>>10);
Andiry Xud18240d2010-07-22 15:23:25 -07002046 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2047 if (ep->skip) {
2048 ep->skip = false;
2049 xhci_dbg(xhci, "td_list is empty while skip "
2050 "flag set. Clear skip flag.\n");
2051 }
2052 ret = 0;
2053 goto cleanup;
2054 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002055
Andiry Xud18240d2010-07-22 15:23:25 -07002056 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002057
Andiry Xud18240d2010-07-22 15:23:25 -07002058 /* Is this a TRB in the currently executing TD? */
2059 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
2060 td->last_trb, event_dma);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002061 if (!event_seg) {
2062 if (!ep->skip ||
2063 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
Sarah Sharpad808332011-05-25 10:43:56 -07002064 /* Some host controllers give a spurious
2065 * successful event after a short transfer.
2066 * Ignore it.
2067 */
2068 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
2069 ep_ring->last_td_was_short) {
2070 ep_ring->last_td_was_short = false;
2071 ret = 0;
2072 goto cleanup;
2073 }
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002074 /* HC is busted, give up! */
2075 xhci_err(xhci,
2076 "ERROR Transfer event TRB DMA ptr not "
2077 "part of current TD\n");
2078 return -ESHUTDOWN;
2079 }
2080
2081 ret = skip_isoc_td(xhci, td, event, ep, &status);
2082 goto cleanup;
2083 }
Sarah Sharpad808332011-05-25 10:43:56 -07002084 if (trb_comp_code == COMP_SHORT_TX)
2085 ep_ring->last_td_was_short = true;
2086 else
2087 ep_ring->last_td_was_short = false;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002088
2089 if (ep->skip) {
Andiry Xud18240d2010-07-22 15:23:25 -07002090 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2091 ep->skip = false;
2092 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002093
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002094 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) /
2095 sizeof(*event_trb)];
2096 /*
2097 * No-op TRB should not trigger interrupts.
2098 * If event_trb is a no-op TRB, it means the
2099 * corresponding TD has been cancelled. Just ignore
2100 * the TD.
2101 */
Matt Evans28ccd292011-03-29 13:40:46 +11002102 if ((le32_to_cpu(event_trb->generic.field[3])
2103 & TRB_TYPE_BITMASK)
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002104 == TRB_TYPE(TRB_TR_NOOP)) {
2105 xhci_dbg(xhci,
2106 "event_trb is a no-op TRB. Skip it\n");
2107 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07002108 }
2109
2110 /* Now update the urb's actual_length and give back to
2111 * the core
2112 */
2113 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2114 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
2115 &status);
Andiry Xu04e51902010-07-22 15:23:39 -07002116 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2117 ret = process_isoc_td(xhci, td, event_trb, event, ep,
2118 &status);
Andiry Xud18240d2010-07-22 15:23:25 -07002119 else
2120 ret = process_bulk_intr_td(xhci, td, event_trb, event,
2121 ep, &status);
Andiry Xu4422da62010-07-22 15:22:55 -07002122
2123cleanup:
Andiry Xud18240d2010-07-22 15:23:25 -07002124 /*
2125 * Do not update event ring dequeue pointer if ep->skip is set.
2126 * Will roll back to continue process missed tds.
Sarah Sharp82d10092009-08-07 14:04:52 -07002127 */
Andiry Xud18240d2010-07-22 15:23:25 -07002128 if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
2129 inc_deq(xhci, xhci->event_ring, true);
Andiry Xud18240d2010-07-22 15:23:25 -07002130 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002131
Andiry Xud18240d2010-07-22 15:23:25 -07002132 if (ret) {
2133 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002134 urb_priv = urb->hcpriv;
Andiry Xud18240d2010-07-22 15:23:25 -07002135 /* Leave the TD around for the reset endpoint function
2136 * to use(but only if it's not a control endpoint,
2137 * since we already queued the Set TR dequeue pointer
2138 * command for stalled control endpoints).
2139 */
2140 if (usb_endpoint_xfer_control(&urb->ep->desc) ||
2141 (trb_comp_code != COMP_STALL &&
2142 trb_comp_code != COMP_BABBLE))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002143 xhci_urb_free_priv(xhci, urb_priv);
Andiry Xud18240d2010-07-22 15:23:25 -07002144
Sarah Sharp214f76f2010-10-26 11:22:02 -07002145 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpf444ff22011-04-05 15:53:47 -07002146 if ((urb->actual_length != urb->transfer_buffer_length &&
2147 (urb->transfer_flags &
2148 URB_SHORT_NOT_OK)) ||
2149 status != 0)
2150 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
2151 "expected = %x, status = %d\n",
2152 urb, urb->actual_length,
2153 urb->transfer_buffer_length,
2154 status);
Andiry Xud18240d2010-07-22 15:23:25 -07002155 spin_unlock(&xhci->lock);
Sarah Sharp214f76f2010-10-26 11:22:02 -07002156 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
Andiry Xud18240d2010-07-22 15:23:25 -07002157 spin_lock(&xhci->lock);
2158 }
2159
2160 /*
2161 * If ep->skip is set, it means there are missed tds on the
2162 * endpoint ring need to take care of.
2163 * Process them as short transfer until reach the td pointed by
2164 * the event.
2165 */
2166 } while (ep->skip && trb_comp_code != COMP_MISSED_INT);
2167
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002168 return 0;
2169}
2170
2171/*
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002172 * This function handles all OS-owned events on the event ring. It may drop
2173 * xhci->lock between event processing (e.g. to pass up port status changes).
Matt Evans9dee9a22011-03-29 13:41:02 +11002174 * Returns >0 for "possibly more events to process" (caller should call again),
2175 * otherwise 0 if done. In future, <0 returns should indicate error code.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002176 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002177static int xhci_handle_event(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002178{
2179 union xhci_trb *event;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002180 int update_ptrs = 1;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002181 int ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002182
2183 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2184 xhci->error_bitmask |= 1 << 1;
Matt Evans9dee9a22011-03-29 13:41:02 +11002185 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002186 }
2187
2188 event = xhci->event_ring->dequeue;
2189 /* Does the HC or OS own the TRB? */
Matt Evans28ccd292011-03-29 13:40:46 +11002190 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
2191 xhci->event_ring->cycle_state) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002192 xhci->error_bitmask |= 1 << 2;
Matt Evans9dee9a22011-03-29 13:41:02 +11002193 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002194 }
2195
Matt Evans92a3da42011-03-29 13:40:51 +11002196 /*
2197 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2198 * speculative reads of the event's flags/data below.
2199 */
2200 rmb();
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002201 /* FIXME: Handle more event types. */
Matt Evans28ccd292011-03-29 13:40:46 +11002202 switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002203 case TRB_TYPE(TRB_COMPLETION):
2204 handle_cmd_completion(xhci, &event->event_cmd);
2205 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002206 case TRB_TYPE(TRB_PORT_STATUS):
2207 handle_port_status(xhci, event);
2208 update_ptrs = 0;
2209 break;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002210 case TRB_TYPE(TRB_TRANSFER):
2211 ret = handle_tx_event(xhci, &event->trans_event);
2212 if (ret < 0)
2213 xhci->error_bitmask |= 1 << 9;
2214 else
2215 update_ptrs = 0;
2216 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002217 default:
Matt Evans28ccd292011-03-29 13:40:46 +11002218 if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
2219 TRB_TYPE(48))
Sarah Sharp02386342010-05-24 13:25:28 -07002220 handle_vendor_event(xhci, event);
2221 else
2222 xhci->error_bitmask |= 1 << 3;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002223 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002224 /* Any of the above functions may drop and re-acquire the lock, so check
2225 * to make sure a watchdog timer didn't mark the host as non-responsive.
2226 */
2227 if (xhci->xhc_state & XHCI_STATE_DYING) {
2228 xhci_dbg(xhci, "xHCI host dying, returning from "
2229 "event handler.\n");
Matt Evans9dee9a22011-03-29 13:41:02 +11002230 return 0;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002231 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002232
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002233 if (update_ptrs)
2234 /* Update SW event ring dequeue pointer */
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002235 inc_deq(xhci, xhci->event_ring, true);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002236
Matt Evans9dee9a22011-03-29 13:41:02 +11002237 /* Are there more items on the event ring? Caller will call us again to
2238 * check.
2239 */
2240 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002241}
Sarah Sharp9032cd52010-07-29 22:12:29 -07002242
2243/*
2244 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2245 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2246 * indicators of an event TRB error, but we check the status *first* to be safe.
2247 */
2248irqreturn_t xhci_irq(struct usb_hcd *hcd)
2249{
2250 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002251 u32 status;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002252 union xhci_trb *trb;
Sarah Sharpbda53142010-07-29 22:12:38 -07002253 u64 temp_64;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002254 union xhci_trb *event_ring_deq;
2255 dma_addr_t deq;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002256
2257 spin_lock(&xhci->lock);
2258 trb = xhci->event_ring->dequeue;
2259 /* Check if the xHC generated the interrupt, or the irq is shared */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002260 status = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002261 if (status == 0xffffffff)
Sarah Sharp9032cd52010-07-29 22:12:29 -07002262 goto hw_died;
2263
Sarah Sharpc21599a2010-07-29 22:13:00 -07002264 if (!(status & STS_EINT)) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002265 spin_unlock(&xhci->lock);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002266 return IRQ_NONE;
2267 }
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002268 if (status & STS_FATAL) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002269 xhci_warn(xhci, "WARNING: Host System Error\n");
2270 xhci_halt(xhci);
2271hw_died:
Sarah Sharp9032cd52010-07-29 22:12:29 -07002272 spin_unlock(&xhci->lock);
2273 return -ESHUTDOWN;
2274 }
2275
Sarah Sharpbda53142010-07-29 22:12:38 -07002276 /*
2277 * Clear the op reg interrupt status first,
2278 * so we can receive interrupts from other MSI-X interrupters.
2279 * Write 1 to clear the interrupt status.
2280 */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002281 status |= STS_EINT;
2282 xhci_writel(xhci, status, &xhci->op_regs->status);
Sarah Sharpbda53142010-07-29 22:12:38 -07002283 /* FIXME when MSI-X is supported and there are multiple vectors */
2284 /* Clear the MSI-X event interrupt status */
2285
Sarah Sharpc21599a2010-07-29 22:13:00 -07002286 if (hcd->irq != -1) {
2287 u32 irq_pending;
2288 /* Acknowledge the PCI interrupt */
2289 irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
2290 irq_pending |= 0x3;
2291 xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
2292 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002293
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002294 if (xhci->xhc_state & XHCI_STATE_DYING) {
Sarah Sharpbda53142010-07-29 22:12:38 -07002295 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2296 "Shouldn't IRQs be disabled?\n");
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002297 /* Clear the event handler busy flag (RW1C);
2298 * the event ring should be empty.
Sarah Sharpbda53142010-07-29 22:12:38 -07002299 */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002300 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2301 xhci_write_64(xhci, temp_64 | ERST_EHB,
2302 &xhci->ir_set->erst_dequeue);
2303 spin_unlock(&xhci->lock);
2304
2305 return IRQ_HANDLED;
2306 }
2307
2308 event_ring_deq = xhci->event_ring->dequeue;
2309 /* FIXME this should be a delayed service routine
2310 * that clears the EHB.
2311 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002312 while (xhci_handle_event(xhci) > 0) {}
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002313
2314 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2315 /* If necessary, update the HW's version of the event ring deq ptr. */
2316 if (event_ring_deq != xhci->event_ring->dequeue) {
2317 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2318 xhci->event_ring->dequeue);
2319 if (deq == 0)
2320 xhci_warn(xhci, "WARN something wrong with SW event "
2321 "ring dequeue ptr.\n");
2322 /* Update HC event ring dequeue pointer */
2323 temp_64 &= ERST_PTR_MASK;
2324 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2325 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002326
2327 /* Clear the event handler busy flag (RW1C); event ring is empty. */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002328 temp_64 |= ERST_EHB;
2329 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2330
Sarah Sharp9032cd52010-07-29 22:12:29 -07002331 spin_unlock(&xhci->lock);
2332
2333 return IRQ_HANDLED;
2334}
2335
2336irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2337{
2338 irqreturn_t ret;
Sarah Sharpb32093792011-03-07 11:24:07 -08002339 struct xhci_hcd *xhci;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002340
Sarah Sharpb32093792011-03-07 11:24:07 -08002341 xhci = hcd_to_xhci(hcd);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002342 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
Sarah Sharpb32093792011-03-07 11:24:07 -08002343 if (xhci->shared_hcd)
2344 set_bit(HCD_FLAG_SAW_IRQ, &xhci->shared_hcd->flags);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002345
2346 ret = xhci_irq(hcd);
2347
2348 return ret;
2349}
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002350
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002351/**** Endpoint Ring Operations ****/
2352
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002353/*
2354 * Generic function for queueing a TRB on a ring.
2355 * The caller must have checked to make sure there's room on the ring.
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002356 *
2357 * @more_trbs_coming: Will you enqueue more TRBs before calling
2358 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002359 */
2360static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002361 bool consumer, bool more_trbs_coming,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002362 u32 field1, u32 field2, u32 field3, u32 field4)
2363{
2364 struct xhci_generic_trb *trb;
2365
2366 trb = &ring->enqueue->generic;
Matt Evans28ccd292011-03-29 13:40:46 +11002367 trb->field[0] = cpu_to_le32(field1);
2368 trb->field[1] = cpu_to_le32(field2);
2369 trb->field[2] = cpu_to_le32(field3);
2370 trb->field[3] = cpu_to_le32(field4);
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002371 inc_enq(xhci, ring, consumer, more_trbs_coming);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002372}
2373
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002374/*
2375 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2376 * FIXME allocate segments if the ring is full.
2377 */
2378static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
2379 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
2380{
2381 /* Make sure the endpoint has been added to xHC schedule */
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002382 switch (ep_state) {
2383 case EP_STATE_DISABLED:
2384 /*
2385 * USB core changed config/interfaces without notifying us,
2386 * or hardware is reporting the wrong state.
2387 */
2388 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2389 return -ENOENT;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002390 case EP_STATE_ERROR:
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002391 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002392 /* FIXME event handling code for error needs to clear it */
2393 /* XXX not sure if this should be -ENOENT or not */
2394 return -EINVAL;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002395 case EP_STATE_HALTED:
2396 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002397 case EP_STATE_STOPPED:
2398 case EP_STATE_RUNNING:
2399 break;
2400 default:
2401 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2402 /*
2403 * FIXME issue Configure Endpoint command to try to get the HC
2404 * back into a known state.
2405 */
2406 return -EINVAL;
2407 }
2408 if (!room_on_ring(xhci, ep_ring, num_trbs)) {
2409 /* FIXME allocate more room */
2410 xhci_err(xhci, "ERROR no room on ep ring\n");
2411 return -ENOMEM;
2412 }
John Youn6c12db92010-05-10 15:33:00 -07002413
2414 if (enqueue_is_link_trb(ep_ring)) {
2415 struct xhci_ring *ring = ep_ring;
2416 union xhci_trb *next;
John Youn6c12db92010-05-10 15:33:00 -07002417
John Youn6c12db92010-05-10 15:33:00 -07002418 next = ring->enqueue;
2419
2420 while (last_trb(xhci, ring, ring->enq_seg, next)) {
John Youn6c12db92010-05-10 15:33:00 -07002421 /* If we're not dealing with 0.95 hardware,
2422 * clear the chain bit.
2423 */
2424 if (!xhci_link_trb_quirk(xhci))
Matt Evans28ccd292011-03-29 13:40:46 +11002425 next->link.control &= cpu_to_le32(~TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002426 else
Matt Evans28ccd292011-03-29 13:40:46 +11002427 next->link.control |= cpu_to_le32(TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002428
2429 wmb();
Matt Evans28ccd292011-03-29 13:40:46 +11002430 next->link.control ^= cpu_to_le32((u32) TRB_CYCLE);
John Youn6c12db92010-05-10 15:33:00 -07002431
2432 /* Toggle the cycle bit after the last ring segment. */
2433 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
2434 ring->cycle_state = (ring->cycle_state ? 0 : 1);
2435 if (!in_interrupt()) {
2436 xhci_dbg(xhci, "queue_trb: Toggle cycle "
2437 "state for ring %p = %i\n",
2438 ring, (unsigned int)ring->cycle_state);
2439 }
2440 }
2441 ring->enq_seg = ring->enq_seg->next;
2442 ring->enqueue = ring->enq_seg->trbs;
2443 next = ring->enqueue;
2444 }
2445 }
2446
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002447 return 0;
2448}
2449
Sarah Sharp23e3be12009-04-29 19:05:20 -07002450static int prepare_transfer(struct xhci_hcd *xhci,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002451 struct xhci_virt_device *xdev,
2452 unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002453 unsigned int stream_id,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002454 unsigned int num_trbs,
2455 struct urb *urb,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002456 unsigned int td_index,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002457 gfp_t mem_flags)
2458{
2459 int ret;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002460 struct urb_priv *urb_priv;
2461 struct xhci_td *td;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002462 struct xhci_ring *ep_ring;
John Yound115b042009-07-27 12:05:15 -07002463 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002464
2465 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2466 if (!ep_ring) {
2467 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2468 stream_id);
2469 return -EINVAL;
2470 }
2471
2472 ret = prepare_ring(xhci, ep_ring,
Matt Evans28ccd292011-03-29 13:40:46 +11002473 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
2474 num_trbs, mem_flags);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002475 if (ret)
2476 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002477
Andiry Xu8e51adc2010-07-22 15:23:31 -07002478 urb_priv = urb->hcpriv;
2479 td = urb_priv->td[td_index];
2480
2481 INIT_LIST_HEAD(&td->td_list);
2482 INIT_LIST_HEAD(&td->cancelled_td_list);
2483
2484 if (td_index == 0) {
Sarah Sharp214f76f2010-10-26 11:22:02 -07002485 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
Andiry Xu8e51adc2010-07-22 15:23:31 -07002486 if (unlikely(ret)) {
2487 xhci_urb_free_priv(xhci, urb_priv);
2488 urb->hcpriv = NULL;
2489 return ret;
2490 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002491 }
2492
Andiry Xu8e51adc2010-07-22 15:23:31 -07002493 td->urb = urb;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002494 /* Add this TD to the tail of the endpoint ring's TD list */
Andiry Xu8e51adc2010-07-22 15:23:31 -07002495 list_add_tail(&td->td_list, &ep_ring->td_list);
2496 td->start_seg = ep_ring->enq_seg;
2497 td->first_trb = ep_ring->enqueue;
2498
2499 urb_priv->td[td_index] = td;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002500
2501 return 0;
2502}
2503
Sarah Sharp23e3be12009-04-29 19:05:20 -07002504static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002505{
2506 int num_sgs, num_trbs, running_total, temp, i;
2507 struct scatterlist *sg;
2508
2509 sg = NULL;
2510 num_sgs = urb->num_sgs;
2511 temp = urb->transfer_buffer_length;
2512
2513 xhci_dbg(xhci, "count sg list trbs: \n");
2514 num_trbs = 0;
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002515 for_each_sg(urb->sg, sg, num_sgs, i) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002516 unsigned int previous_total_trbs = num_trbs;
2517 unsigned int len = sg_dma_len(sg);
2518
2519 /* Scatter gather list entries may cross 64KB boundaries */
2520 running_total = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002521 (sg_dma_address(sg) & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08002522 running_total &= TRB_MAX_BUFF_SIZE - 1;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002523 if (running_total != 0)
2524 num_trbs++;
2525
2526 /* How many more 64KB chunks to transfer, how many more TRBs? */
Paul Zimmermanbcd2fde2011-02-12 14:07:57 -08002527 while (running_total < sg_dma_len(sg) && running_total < temp) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002528 num_trbs++;
2529 running_total += TRB_MAX_BUFF_SIZE;
2530 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002531 xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
2532 i, (unsigned long long)sg_dma_address(sg),
2533 len, len, num_trbs - previous_total_trbs);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002534
2535 len = min_t(int, len, temp);
2536 temp -= len;
2537 if (temp == 0)
2538 break;
2539 }
2540 xhci_dbg(xhci, "\n");
2541 if (!in_interrupt())
Andiry Xuf2c565e2010-12-20 17:12:24 +08002542 xhci_dbg(xhci, "ep %#x - urb len = %d, sglist used, "
2543 "num_trbs = %d\n",
Sarah Sharp8a96c052009-04-27 19:59:19 -07002544 urb->ep->desc.bEndpointAddress,
2545 urb->transfer_buffer_length,
2546 num_trbs);
2547 return num_trbs;
2548}
2549
Sarah Sharp23e3be12009-04-29 19:05:20 -07002550static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002551{
2552 if (num_trbs != 0)
Paul Zimmermana2490182011-02-12 14:06:44 -08002553 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
Sarah Sharp8a96c052009-04-27 19:59:19 -07002554 "TRBs, %d left\n", __func__,
2555 urb->ep->desc.bEndpointAddress, num_trbs);
2556 if (running_total != urb->transfer_buffer_length)
Paul Zimmermana2490182011-02-12 14:06:44 -08002557 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
Sarah Sharp8a96c052009-04-27 19:59:19 -07002558 "queued %#x (%d), asked for %#x (%d)\n",
2559 __func__,
2560 urb->ep->desc.bEndpointAddress,
2561 running_total, running_total,
2562 urb->transfer_buffer_length,
2563 urb->transfer_buffer_length);
2564}
2565
Sarah Sharp23e3be12009-04-29 19:05:20 -07002566static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002567 unsigned int ep_index, unsigned int stream_id, int start_cycle,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002568 struct xhci_generic_trb *start_trb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002569{
Sarah Sharp8a96c052009-04-27 19:59:19 -07002570 /*
2571 * Pass all the TRBs to the hardware at once and make sure this write
2572 * isn't reordered.
2573 */
2574 wmb();
Andiry Xu50f7b522010-12-20 15:09:34 +08002575 if (start_cycle)
Matt Evans28ccd292011-03-29 13:40:46 +11002576 start_trb->field[3] |= cpu_to_le32(start_cycle);
Andiry Xu50f7b522010-12-20 15:09:34 +08002577 else
Matt Evans28ccd292011-03-29 13:40:46 +11002578 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
Andiry Xube88fe42010-10-14 07:22:57 -07002579 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002580}
2581
Sarah Sharp624defa2009-09-02 12:14:28 -07002582/*
2583 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
2584 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
2585 * (comprised of sg list entries) can take several service intervals to
2586 * transmit.
2587 */
2588int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2589 struct urb *urb, int slot_id, unsigned int ep_index)
2590{
2591 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
2592 xhci->devs[slot_id]->out_ctx, ep_index);
2593 int xhci_interval;
2594 int ep_interval;
2595
Matt Evans28ccd292011-03-29 13:40:46 +11002596 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Sarah Sharp624defa2009-09-02 12:14:28 -07002597 ep_interval = urb->interval;
2598 /* Convert to microframes */
2599 if (urb->dev->speed == USB_SPEED_LOW ||
2600 urb->dev->speed == USB_SPEED_FULL)
2601 ep_interval *= 8;
2602 /* FIXME change this to a warning and a suggestion to use the new API
2603 * to set the polling interval (once the API is added).
2604 */
2605 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08002606 if (printk_ratelimit())
Sarah Sharp624defa2009-09-02 12:14:28 -07002607 dev_dbg(&urb->dev->dev, "Driver uses different interval"
2608 " (%d microframe%s) than xHCI "
2609 "(%d microframe%s)\n",
2610 ep_interval,
2611 ep_interval == 1 ? "" : "s",
2612 xhci_interval,
2613 xhci_interval == 1 ? "" : "s");
2614 urb->interval = xhci_interval;
2615 /* Convert back to frames for LS/FS devices */
2616 if (urb->dev->speed == USB_SPEED_LOW ||
2617 urb->dev->speed == USB_SPEED_FULL)
2618 urb->interval /= 8;
2619 }
2620 return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
2621}
2622
Sarah Sharp04dd9502009-11-11 10:28:30 -08002623/*
2624 * The TD size is the number of bytes remaining in the TD (including this TRB),
2625 * right shifted by 10.
2626 * It must fit in bits 21:17, so it can't be bigger than 31.
2627 */
2628static u32 xhci_td_remainder(unsigned int remainder)
2629{
2630 u32 max = (1 << (21 - 17 + 1)) - 1;
2631
2632 if ((remainder >> 10) >= max)
2633 return max << 17;
2634 else
2635 return (remainder >> 10) << 17;
2636}
2637
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002638/*
2639 * For xHCI 1.0 host controllers, TD size is the number of packets remaining in
2640 * the TD (*not* including this TRB).
2641 *
2642 * Total TD packet count = total_packet_count =
2643 * roundup(TD size in bytes / wMaxPacketSize)
2644 *
2645 * Packets transferred up to and including this TRB = packets_transferred =
2646 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
2647 *
2648 * TD size = total_packet_count - packets_transferred
2649 *
2650 * It must fit in bits 21:17, so it can't be bigger than 31.
2651 */
2652
2653static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
2654 unsigned int total_packet_count, struct urb *urb)
2655{
2656 int packets_transferred;
2657
2658 /* All the TRB queueing functions don't count the current TRB in
2659 * running_total.
2660 */
2661 packets_transferred = (running_total + trb_buff_len) /
2662 le16_to_cpu(urb->ep->desc.wMaxPacketSize);
2663
2664 return xhci_td_remainder(total_packet_count - packets_transferred);
2665}
2666
Sarah Sharp23e3be12009-04-29 19:05:20 -07002667static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002668 struct urb *urb, int slot_id, unsigned int ep_index)
2669{
2670 struct xhci_ring *ep_ring;
2671 unsigned int num_trbs;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002672 struct urb_priv *urb_priv;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002673 struct xhci_td *td;
2674 struct scatterlist *sg;
2675 int num_sgs;
2676 int trb_buff_len, this_sg_len, running_total;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002677 unsigned int total_packet_count;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002678 bool first_trb;
2679 u64 addr;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002680 bool more_trbs_coming;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002681
2682 struct xhci_generic_trb *start_trb;
2683 int start_cycle;
2684
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002685 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2686 if (!ep_ring)
2687 return -EINVAL;
2688
Sarah Sharp8a96c052009-04-27 19:59:19 -07002689 num_trbs = count_sg_trbs_needed(xhci, urb);
2690 num_sgs = urb->num_sgs;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002691 total_packet_count = roundup(urb->transfer_buffer_length,
2692 le16_to_cpu(urb->ep->desc.wMaxPacketSize));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002693
Sarah Sharp23e3be12009-04-29 19:05:20 -07002694 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002695 ep_index, urb->stream_id,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002696 num_trbs, urb, 0, mem_flags);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002697 if (trb_buff_len < 0)
2698 return trb_buff_len;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002699
2700 urb_priv = urb->hcpriv;
2701 td = urb_priv->td[0];
2702
Sarah Sharp8a96c052009-04-27 19:59:19 -07002703 /*
2704 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2705 * until we've finished creating all the other TRBs. The ring's cycle
2706 * state may change as we enqueue the other TRBs, so save it too.
2707 */
2708 start_trb = &ep_ring->enqueue->generic;
2709 start_cycle = ep_ring->cycle_state;
2710
2711 running_total = 0;
2712 /*
2713 * How much data is in the first TRB?
2714 *
2715 * There are three forces at work for TRB buffer pointers and lengths:
2716 * 1. We don't want to walk off the end of this sg-list entry buffer.
2717 * 2. The transfer length that the driver requested may be smaller than
2718 * the amount of memory allocated for this scatter-gather list.
2719 * 3. TRBs buffers can't cross 64KB boundaries.
2720 */
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002721 sg = urb->sg;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002722 addr = (u64) sg_dma_address(sg);
2723 this_sg_len = sg_dma_len(sg);
Paul Zimmermana2490182011-02-12 14:06:44 -08002724 trb_buff_len = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002725 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2726 if (trb_buff_len > urb->transfer_buffer_length)
2727 trb_buff_len = urb->transfer_buffer_length;
2728 xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
2729 trb_buff_len);
2730
2731 first_trb = true;
2732 /* Queue the first TRB, even if it's zero-length */
2733 do {
2734 u32 field = 0;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002735 u32 length_field = 0;
Sarah Sharp04dd9502009-11-11 10:28:30 -08002736 u32 remainder = 0;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002737
2738 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002739 if (first_trb) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002740 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002741 if (start_cycle == 0)
2742 field |= 0x1;
2743 } else
Sarah Sharp8a96c052009-04-27 19:59:19 -07002744 field |= ep_ring->cycle_state;
2745
2746 /* Chain all the TRBs together; clear the chain bit in the last
2747 * TRB to indicate it's the last TRB in the chain.
2748 */
2749 if (num_trbs > 1) {
2750 field |= TRB_CHAIN;
2751 } else {
2752 /* FIXME - add check for ZERO_PACKET flag before this */
2753 td->last_trb = ep_ring->enqueue;
2754 field |= TRB_IOC;
2755 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002756
2757 /* Only set interrupt on short packet for IN endpoints */
2758 if (usb_urb_dir_in(urb))
2759 field |= TRB_ISP;
2760
Sarah Sharp8a96c052009-04-27 19:59:19 -07002761 xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
2762 "64KB boundary at %#x, end dma = %#x\n",
2763 (unsigned int) addr, trb_buff_len, trb_buff_len,
2764 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2765 (unsigned int) addr + trb_buff_len);
2766 if (TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002767 (addr & (TRB_MAX_BUFF_SIZE - 1)) < trb_buff_len) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002768 xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
2769 xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
2770 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2771 (unsigned int) addr + trb_buff_len);
2772 }
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002773
2774 /* Set the TRB length, TD size, and interrupter fields. */
2775 if (xhci->hci_version < 0x100) {
2776 remainder = xhci_td_remainder(
2777 urb->transfer_buffer_length -
2778 running_total);
2779 } else {
2780 remainder = xhci_v1_0_td_remainder(running_total,
2781 trb_buff_len, total_packet_count, urb);
2782 }
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002783 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002784 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002785 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002786
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002787 if (num_trbs > 1)
2788 more_trbs_coming = true;
2789 else
2790 more_trbs_coming = false;
2791 queue_trb(xhci, ep_ring, false, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002792 lower_32_bits(addr),
2793 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002794 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002795 field | TRB_TYPE(TRB_NORMAL));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002796 --num_trbs;
2797 running_total += trb_buff_len;
2798
2799 /* Calculate length for next transfer --
2800 * Are we done queueing all the TRBs for this sg entry?
2801 */
2802 this_sg_len -= trb_buff_len;
2803 if (this_sg_len == 0) {
2804 --num_sgs;
2805 if (num_sgs == 0)
2806 break;
2807 sg = sg_next(sg);
2808 addr = (u64) sg_dma_address(sg);
2809 this_sg_len = sg_dma_len(sg);
2810 } else {
2811 addr += trb_buff_len;
2812 }
2813
2814 trb_buff_len = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002815 (addr & (TRB_MAX_BUFF_SIZE - 1));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002816 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2817 if (running_total + trb_buff_len > urb->transfer_buffer_length)
2818 trb_buff_len =
2819 urb->transfer_buffer_length - running_total;
2820 } while (running_total < urb->transfer_buffer_length);
2821
2822 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002823 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002824 start_cycle, start_trb);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002825 return 0;
2826}
2827
Sarah Sharpb10de142009-04-27 19:58:50 -07002828/* This is very similar to what ehci-q.c qtd_fill() does */
Sarah Sharp23e3be12009-04-29 19:05:20 -07002829int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpb10de142009-04-27 19:58:50 -07002830 struct urb *urb, int slot_id, unsigned int ep_index)
2831{
2832 struct xhci_ring *ep_ring;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002833 struct urb_priv *urb_priv;
Sarah Sharpb10de142009-04-27 19:58:50 -07002834 struct xhci_td *td;
2835 int num_trbs;
2836 struct xhci_generic_trb *start_trb;
2837 bool first_trb;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002838 bool more_trbs_coming;
Sarah Sharpb10de142009-04-27 19:58:50 -07002839 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002840 u32 field, length_field;
Sarah Sharpb10de142009-04-27 19:58:50 -07002841
2842 int running_total, trb_buff_len, ret;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002843 unsigned int total_packet_count;
Sarah Sharpb10de142009-04-27 19:58:50 -07002844 u64 addr;
2845
Alan Sternff9c8952010-04-02 13:27:28 -04002846 if (urb->num_sgs)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002847 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
2848
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002849 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2850 if (!ep_ring)
2851 return -EINVAL;
Sarah Sharpb10de142009-04-27 19:58:50 -07002852
2853 num_trbs = 0;
2854 /* How much data is (potentially) left before the 64KB boundary? */
2855 running_total = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002856 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08002857 running_total &= TRB_MAX_BUFF_SIZE - 1;
Sarah Sharpb10de142009-04-27 19:58:50 -07002858
2859 /* If there's some data on this 64KB chunk, or we have to send a
2860 * zero-length transfer, we need at least one TRB
2861 */
2862 if (running_total != 0 || urb->transfer_buffer_length == 0)
2863 num_trbs++;
2864 /* How many more 64KB chunks to transfer, how many more TRBs? */
2865 while (running_total < urb->transfer_buffer_length) {
2866 num_trbs++;
2867 running_total += TRB_MAX_BUFF_SIZE;
2868 }
2869 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
2870
2871 if (!in_interrupt())
Andiry Xuf2c565e2010-12-20 17:12:24 +08002872 xhci_dbg(xhci, "ep %#x - urb len = %#x (%d), "
2873 "addr = %#llx, num_trbs = %d\n",
Sarah Sharpb10de142009-04-27 19:58:50 -07002874 urb->ep->desc.bEndpointAddress,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002875 urb->transfer_buffer_length,
2876 urb->transfer_buffer_length,
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002877 (unsigned long long)urb->transfer_dma,
Sarah Sharpb10de142009-04-27 19:58:50 -07002878 num_trbs);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002879
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002880 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2881 ep_index, urb->stream_id,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002882 num_trbs, urb, 0, mem_flags);
Sarah Sharpb10de142009-04-27 19:58:50 -07002883 if (ret < 0)
2884 return ret;
2885
Andiry Xu8e51adc2010-07-22 15:23:31 -07002886 urb_priv = urb->hcpriv;
2887 td = urb_priv->td[0];
2888
Sarah Sharpb10de142009-04-27 19:58:50 -07002889 /*
2890 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2891 * until we've finished creating all the other TRBs. The ring's cycle
2892 * state may change as we enqueue the other TRBs, so save it too.
2893 */
2894 start_trb = &ep_ring->enqueue->generic;
2895 start_cycle = ep_ring->cycle_state;
2896
2897 running_total = 0;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002898 total_packet_count = roundup(urb->transfer_buffer_length,
2899 le16_to_cpu(urb->ep->desc.wMaxPacketSize));
Sarah Sharpb10de142009-04-27 19:58:50 -07002900 /* How much data is in the first TRB? */
2901 addr = (u64) urb->transfer_dma;
2902 trb_buff_len = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002903 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
2904 if (trb_buff_len > urb->transfer_buffer_length)
Sarah Sharpb10de142009-04-27 19:58:50 -07002905 trb_buff_len = urb->transfer_buffer_length;
2906
2907 first_trb = true;
2908
2909 /* Queue the first TRB, even if it's zero-length */
2910 do {
Sarah Sharp04dd9502009-11-11 10:28:30 -08002911 u32 remainder = 0;
Sarah Sharpb10de142009-04-27 19:58:50 -07002912 field = 0;
2913
2914 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002915 if (first_trb) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002916 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002917 if (start_cycle == 0)
2918 field |= 0x1;
2919 } else
Sarah Sharpb10de142009-04-27 19:58:50 -07002920 field |= ep_ring->cycle_state;
2921
2922 /* Chain all the TRBs together; clear the chain bit in the last
2923 * TRB to indicate it's the last TRB in the chain.
2924 */
2925 if (num_trbs > 1) {
2926 field |= TRB_CHAIN;
2927 } else {
2928 /* FIXME - add check for ZERO_PACKET flag before this */
2929 td->last_trb = ep_ring->enqueue;
2930 field |= TRB_IOC;
2931 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002932
2933 /* Only set interrupt on short packet for IN endpoints */
2934 if (usb_urb_dir_in(urb))
2935 field |= TRB_ISP;
2936
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002937 /* Set the TRB length, TD size, and interrupter fields. */
2938 if (xhci->hci_version < 0x100) {
2939 remainder = xhci_td_remainder(
2940 urb->transfer_buffer_length -
2941 running_total);
2942 } else {
2943 remainder = xhci_v1_0_td_remainder(running_total,
2944 trb_buff_len, total_packet_count, urb);
2945 }
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002946 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002947 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002948 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002949
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002950 if (num_trbs > 1)
2951 more_trbs_coming = true;
2952 else
2953 more_trbs_coming = false;
2954 queue_trb(xhci, ep_ring, false, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002955 lower_32_bits(addr),
2956 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002957 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002958 field | TRB_TYPE(TRB_NORMAL));
Sarah Sharpb10de142009-04-27 19:58:50 -07002959 --num_trbs;
2960 running_total += trb_buff_len;
2961
2962 /* Calculate length for next transfer */
2963 addr += trb_buff_len;
2964 trb_buff_len = urb->transfer_buffer_length - running_total;
2965 if (trb_buff_len > TRB_MAX_BUFF_SIZE)
2966 trb_buff_len = TRB_MAX_BUFF_SIZE;
2967 } while (running_total < urb->transfer_buffer_length);
2968
Sarah Sharp8a96c052009-04-27 19:59:19 -07002969 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002970 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002971 start_cycle, start_trb);
Sarah Sharpb10de142009-04-27 19:58:50 -07002972 return 0;
2973}
2974
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002975/* Caller must have locked xhci->lock */
Sarah Sharp23e3be12009-04-29 19:05:20 -07002976int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002977 struct urb *urb, int slot_id, unsigned int ep_index)
2978{
2979 struct xhci_ring *ep_ring;
2980 int num_trbs;
2981 int ret;
2982 struct usb_ctrlrequest *setup;
2983 struct xhci_generic_trb *start_trb;
2984 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002985 u32 field, length_field;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002986 struct urb_priv *urb_priv;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002987 struct xhci_td *td;
2988
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002989 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2990 if (!ep_ring)
2991 return -EINVAL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002992
2993 /*
2994 * Need to copy setup packet into setup TRB, so we can't use the setup
2995 * DMA address.
2996 */
2997 if (!urb->setup_packet)
2998 return -EINVAL;
2999
3000 if (!in_interrupt())
3001 xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
3002 slot_id, ep_index);
3003 /* 1 TRB for setup, 1 for status */
3004 num_trbs = 2;
3005 /*
3006 * Don't need to check if we need additional event data and normal TRBs,
3007 * since data in control transfers will never get bigger than 16MB
3008 * XXX: can we get a buffer that crosses 64KB boundaries?
3009 */
3010 if (urb->transfer_buffer_length > 0)
3011 num_trbs++;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003012 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3013 ep_index, urb->stream_id,
Andiry Xu8e51adc2010-07-22 15:23:31 -07003014 num_trbs, urb, 0, mem_flags);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003015 if (ret < 0)
3016 return ret;
3017
Andiry Xu8e51adc2010-07-22 15:23:31 -07003018 urb_priv = urb->hcpriv;
3019 td = urb_priv->td[0];
3020
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003021 /*
3022 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3023 * until we've finished creating all the other TRBs. The ring's cycle
3024 * state may change as we enqueue the other TRBs, so save it too.
3025 */
3026 start_trb = &ep_ring->enqueue->generic;
3027 start_cycle = ep_ring->cycle_state;
3028
3029 /* Queue setup TRB - see section 6.4.1.2.1 */
3030 /* FIXME better way to translate setup_packet into two u32 fields? */
3031 setup = (struct usb_ctrlrequest *) urb->setup_packet;
Andiry Xu50f7b522010-12-20 15:09:34 +08003032 field = 0;
3033 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3034 if (start_cycle == 0)
3035 field |= 0x1;
Andiry Xub83cdc82011-05-05 18:13:56 +08003036
3037 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
3038 if (xhci->hci_version == 0x100) {
3039 if (urb->transfer_buffer_length > 0) {
3040 if (setup->bRequestType & USB_DIR_IN)
3041 field |= TRB_TX_TYPE(TRB_DATA_IN);
3042 else
3043 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3044 }
3045 }
3046
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003047 queue_trb(xhci, ep_ring, false, true,
Matt Evans28ccd292011-03-29 13:40:46 +11003048 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3049 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3050 TRB_LEN(8) | TRB_INTR_TARGET(0),
3051 /* Immediate data in pointer */
3052 field);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003053
3054 /* If there's data, queue data TRBs */
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003055 /* Only set interrupt on short packet for IN endpoints */
3056 if (usb_urb_dir_in(urb))
3057 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3058 else
3059 field = TRB_TYPE(TRB_DATA);
3060
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003061 length_field = TRB_LEN(urb->transfer_buffer_length) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08003062 xhci_td_remainder(urb->transfer_buffer_length) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003063 TRB_INTR_TARGET(0);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003064 if (urb->transfer_buffer_length > 0) {
3065 if (setup->bRequestType & USB_DIR_IN)
3066 field |= TRB_DIR_IN;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003067 queue_trb(xhci, ep_ring, false, true,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003068 lower_32_bits(urb->transfer_dma),
3069 upper_32_bits(urb->transfer_dma),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003070 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003071 field | ep_ring->cycle_state);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003072 }
3073
3074 /* Save the DMA address of the last TRB in the TD */
3075 td->last_trb = ep_ring->enqueue;
3076
3077 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3078 /* If the device sent data, the status stage is an OUT transfer */
3079 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3080 field = 0;
3081 else
3082 field = TRB_DIR_IN;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003083 queue_trb(xhci, ep_ring, false, false,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003084 0,
3085 0,
3086 TRB_INTR_TARGET(0),
3087 /* Event on completion */
3088 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3089
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003090 giveback_first_trb(xhci, slot_id, ep_index, 0,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003091 start_cycle, start_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003092 return 0;
3093}
3094
Andiry Xu04e51902010-07-22 15:23:39 -07003095static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
3096 struct urb *urb, int i)
3097{
3098 int num_trbs = 0;
3099 u64 addr, td_len, running_total;
3100
3101 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
3102 td_len = urb->iso_frame_desc[i].length;
3103
Paul Zimmermana2490182011-02-12 14:06:44 -08003104 running_total = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08003105 running_total &= TRB_MAX_BUFF_SIZE - 1;
Andiry Xu04e51902010-07-22 15:23:39 -07003106 if (running_total != 0)
3107 num_trbs++;
3108
3109 while (running_total < td_len) {
3110 num_trbs++;
3111 running_total += TRB_MAX_BUFF_SIZE;
3112 }
3113
3114 return num_trbs;
3115}
3116
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003117/*
3118 * The transfer burst count field of the isochronous TRB defines the number of
3119 * bursts that are required to move all packets in this TD. Only SuperSpeed
3120 * devices can burst up to bMaxBurst number of packets per service interval.
3121 * This field is zero based, meaning a value of zero in the field means one
3122 * burst. Basically, for everything but SuperSpeed devices, this field will be
3123 * zero. Only xHCI 1.0 host controllers support this field.
3124 */
3125static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
3126 struct usb_device *udev,
3127 struct urb *urb, unsigned int total_packet_count)
3128{
3129 unsigned int max_burst;
3130
3131 if (xhci->hci_version < 0x100 || udev->speed != USB_SPEED_SUPER)
3132 return 0;
3133
3134 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3135 return roundup(total_packet_count, max_burst + 1) - 1;
3136}
3137
Sarah Sharpb61d3782011-04-19 17:43:33 -07003138/*
3139 * Returns the number of packets in the last "burst" of packets. This field is
3140 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3141 * the last burst packet count is equal to the total number of packets in the
3142 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3143 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3144 * contain 1 to (bMaxBurst + 1) packets.
3145 */
3146static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
3147 struct usb_device *udev,
3148 struct urb *urb, unsigned int total_packet_count)
3149{
3150 unsigned int max_burst;
3151 unsigned int residue;
3152
3153 if (xhci->hci_version < 0x100)
3154 return 0;
3155
3156 switch (udev->speed) {
3157 case USB_SPEED_SUPER:
3158 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3159 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3160 residue = total_packet_count % (max_burst + 1);
3161 /* If residue is zero, the last burst contains (max_burst + 1)
3162 * number of packets, but the TLBPC field is zero-based.
3163 */
3164 if (residue == 0)
3165 return max_burst;
3166 return residue - 1;
3167 default:
3168 if (total_packet_count == 0)
3169 return 0;
3170 return total_packet_count - 1;
3171 }
3172}
3173
Andiry Xu04e51902010-07-22 15:23:39 -07003174/* This is for isoc transfer */
3175static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3176 struct urb *urb, int slot_id, unsigned int ep_index)
3177{
3178 struct xhci_ring *ep_ring;
3179 struct urb_priv *urb_priv;
3180 struct xhci_td *td;
3181 int num_tds, trbs_per_td;
3182 struct xhci_generic_trb *start_trb;
3183 bool first_trb;
3184 int start_cycle;
3185 u32 field, length_field;
3186 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3187 u64 start_addr, addr;
3188 int i, j;
Andiry Xu47cbf692010-12-20 14:49:48 +08003189 bool more_trbs_coming;
Andiry Xu04e51902010-07-22 15:23:39 -07003190
3191 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3192
3193 num_tds = urb->number_of_packets;
3194 if (num_tds < 1) {
3195 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3196 return -EINVAL;
3197 }
3198
3199 if (!in_interrupt())
Andiry Xuf2c565e2010-12-20 17:12:24 +08003200 xhci_dbg(xhci, "ep %#x - urb len = %#x (%d),"
Andiry Xu04e51902010-07-22 15:23:39 -07003201 " addr = %#llx, num_tds = %d\n",
3202 urb->ep->desc.bEndpointAddress,
3203 urb->transfer_buffer_length,
3204 urb->transfer_buffer_length,
3205 (unsigned long long)urb->transfer_dma,
3206 num_tds);
3207
3208 start_addr = (u64) urb->transfer_dma;
3209 start_trb = &ep_ring->enqueue->generic;
3210 start_cycle = ep_ring->cycle_state;
3211
3212 /* Queue the first TRB, even if it's zero-length */
3213 for (i = 0; i < num_tds; i++) {
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003214 unsigned int total_packet_count;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003215 unsigned int burst_count;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003216 unsigned int residue;
Andiry Xu04e51902010-07-22 15:23:39 -07003217
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003218 first_trb = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003219 running_total = 0;
3220 addr = start_addr + urb->iso_frame_desc[i].offset;
3221 td_len = urb->iso_frame_desc[i].length;
3222 td_remain_len = td_len;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003223 /* FIXME: Ignoring zero-length packets, can those happen? */
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003224 total_packet_count = roundup(td_len,
3225 le16_to_cpu(urb->ep->desc.wMaxPacketSize));
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003226 burst_count = xhci_get_burst_count(xhci, urb->dev, urb,
3227 total_packet_count);
Sarah Sharpb61d3782011-04-19 17:43:33 -07003228 residue = xhci_get_last_burst_packet_count(xhci,
3229 urb->dev, urb, total_packet_count);
Andiry Xu04e51902010-07-22 15:23:39 -07003230
3231 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
3232
3233 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
3234 urb->stream_id, trbs_per_td, urb, i, mem_flags);
3235 if (ret < 0)
3236 return ret;
3237
3238 urb_priv = urb->hcpriv;
3239 td = urb_priv->td[i];
3240
3241 for (j = 0; j < trbs_per_td; j++) {
3242 u32 remainder = 0;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003243 field = TRB_TBC(burst_count) | TRB_TLBPC(residue);
Andiry Xu04e51902010-07-22 15:23:39 -07003244
3245 if (first_trb) {
3246 /* Queue the isoc TRB */
3247 field |= TRB_TYPE(TRB_ISOC);
3248 /* Assume URB_ISO_ASAP is set */
3249 field |= TRB_SIA;
Andiry Xu50f7b522010-12-20 15:09:34 +08003250 if (i == 0) {
3251 if (start_cycle == 0)
3252 field |= 0x1;
3253 } else
Andiry Xu04e51902010-07-22 15:23:39 -07003254 field |= ep_ring->cycle_state;
3255 first_trb = false;
3256 } else {
3257 /* Queue other normal TRBs */
3258 field |= TRB_TYPE(TRB_NORMAL);
3259 field |= ep_ring->cycle_state;
3260 }
3261
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003262 /* Only set interrupt on short packet for IN EPs */
3263 if (usb_urb_dir_in(urb))
3264 field |= TRB_ISP;
3265
Andiry Xu04e51902010-07-22 15:23:39 -07003266 /* Chain all the TRBs together; clear the chain bit in
3267 * the last TRB to indicate it's the last TRB in the
3268 * chain.
3269 */
3270 if (j < trbs_per_td - 1) {
3271 field |= TRB_CHAIN;
Andiry Xu47cbf692010-12-20 14:49:48 +08003272 more_trbs_coming = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003273 } else {
3274 td->last_trb = ep_ring->enqueue;
3275 field |= TRB_IOC;
Andiry Xuad106f22011-05-05 18:14:02 +08003276 if (xhci->hci_version == 0x100) {
3277 /* Set BEI bit except for the last td */
3278 if (i < num_tds - 1)
3279 field |= TRB_BEI;
3280 }
Andiry Xu47cbf692010-12-20 14:49:48 +08003281 more_trbs_coming = false;
Andiry Xu04e51902010-07-22 15:23:39 -07003282 }
3283
3284 /* Calculate TRB length */
3285 trb_buff_len = TRB_MAX_BUFF_SIZE -
3286 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
3287 if (trb_buff_len > td_remain_len)
3288 trb_buff_len = td_remain_len;
3289
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003290 /* Set the TRB length, TD size, & interrupter fields. */
3291 if (xhci->hci_version < 0x100) {
3292 remainder = xhci_td_remainder(
3293 td_len - running_total);
3294 } else {
3295 remainder = xhci_v1_0_td_remainder(
3296 running_total, trb_buff_len,
3297 total_packet_count, urb);
3298 }
Andiry Xu04e51902010-07-22 15:23:39 -07003299 length_field = TRB_LEN(trb_buff_len) |
3300 remainder |
3301 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003302
Andiry Xu47cbf692010-12-20 14:49:48 +08003303 queue_trb(xhci, ep_ring, false, more_trbs_coming,
Andiry Xu04e51902010-07-22 15:23:39 -07003304 lower_32_bits(addr),
3305 upper_32_bits(addr),
3306 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003307 field);
Andiry Xu04e51902010-07-22 15:23:39 -07003308 running_total += trb_buff_len;
3309
3310 addr += trb_buff_len;
3311 td_remain_len -= trb_buff_len;
3312 }
3313
3314 /* Check TD length */
3315 if (running_total != td_len) {
3316 xhci_err(xhci, "ISOC TD length unmatch\n");
3317 return -EINVAL;
3318 }
3319 }
3320
Andiry Xuc41136b2011-03-22 17:08:14 +08003321 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
3322 if (xhci->quirks & XHCI_AMD_PLL_FIX)
3323 usb_amd_quirk_pll_disable();
3324 }
3325 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
3326
Andiry Xue1eab2e2011-01-04 16:30:39 -08003327 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3328 start_cycle, start_trb);
Andiry Xu04e51902010-07-22 15:23:39 -07003329 return 0;
3330}
3331
3332/*
3333 * Check transfer ring to guarantee there is enough room for the urb.
3334 * Update ISO URB start_frame and interval.
3335 * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
3336 * update the urb->start_frame by now.
3337 * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
3338 */
3339int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3340 struct urb *urb, int slot_id, unsigned int ep_index)
3341{
3342 struct xhci_virt_device *xdev;
3343 struct xhci_ring *ep_ring;
3344 struct xhci_ep_ctx *ep_ctx;
3345 int start_frame;
3346 int xhci_interval;
3347 int ep_interval;
3348 int num_tds, num_trbs, i;
3349 int ret;
3350
3351 xdev = xhci->devs[slot_id];
3352 ep_ring = xdev->eps[ep_index].ring;
3353 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3354
3355 num_trbs = 0;
3356 num_tds = urb->number_of_packets;
3357 for (i = 0; i < num_tds; i++)
3358 num_trbs += count_isoc_trbs_needed(xhci, urb, i);
3359
3360 /* Check the ring to guarantee there is enough room for the whole urb.
3361 * Do not insert any td of the urb to the ring if the check failed.
3362 */
Matt Evans28ccd292011-03-29 13:40:46 +11003363 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
3364 num_trbs, mem_flags);
Andiry Xu04e51902010-07-22 15:23:39 -07003365 if (ret)
3366 return ret;
3367
3368 start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
3369 start_frame &= 0x3fff;
3370
3371 urb->start_frame = start_frame;
3372 if (urb->dev->speed == USB_SPEED_LOW ||
3373 urb->dev->speed == USB_SPEED_FULL)
3374 urb->start_frame >>= 3;
3375
Matt Evans28ccd292011-03-29 13:40:46 +11003376 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Andiry Xu04e51902010-07-22 15:23:39 -07003377 ep_interval = urb->interval;
3378 /* Convert to microframes */
3379 if (urb->dev->speed == USB_SPEED_LOW ||
3380 urb->dev->speed == USB_SPEED_FULL)
3381 ep_interval *= 8;
3382 /* FIXME change this to a warning and a suggestion to use the new API
3383 * to set the polling interval (once the API is added).
3384 */
3385 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08003386 if (printk_ratelimit())
Andiry Xu04e51902010-07-22 15:23:39 -07003387 dev_dbg(&urb->dev->dev, "Driver uses different interval"
3388 " (%d microframe%s) than xHCI "
3389 "(%d microframe%s)\n",
3390 ep_interval,
3391 ep_interval == 1 ? "" : "s",
3392 xhci_interval,
3393 xhci_interval == 1 ? "" : "s");
3394 urb->interval = xhci_interval;
3395 /* Convert back to frames for LS/FS devices */
3396 if (urb->dev->speed == USB_SPEED_LOW ||
3397 urb->dev->speed == USB_SPEED_FULL)
3398 urb->interval /= 8;
3399 }
3400 return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
3401}
3402
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003403/**** Command Ring Operations ****/
3404
Sarah Sharp913a8a32009-09-04 10:53:13 -07003405/* Generic function for queueing a command TRB on the command ring.
3406 * Check to make sure there's room on the command ring for one command TRB.
3407 * Also check that there's room reserved for commands that must not fail.
3408 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3409 * then only check for the number of reserved spots.
3410 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3411 * because the command event handler may want to resubmit a failed command.
3412 */
3413static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3414 u32 field3, u32 field4, bool command_must_succeed)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003415{
Sarah Sharp913a8a32009-09-04 10:53:13 -07003416 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003417 int ret;
3418
Sarah Sharp913a8a32009-09-04 10:53:13 -07003419 if (!command_must_succeed)
3420 reserved_trbs++;
3421
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003422 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
3423 reserved_trbs, GFP_ATOMIC);
3424 if (ret < 0) {
3425 xhci_err(xhci, "ERR: No room for command on command ring\n");
Sarah Sharp913a8a32009-09-04 10:53:13 -07003426 if (command_must_succeed)
3427 xhci_err(xhci, "ERR: Reserved TRB counting for "
3428 "unfailable commands failed.\n");
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003429 return ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003430 }
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003431 queue_trb(xhci, xhci->cmd_ring, false, false, field1, field2, field3,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003432 field4 | xhci->cmd_ring->cycle_state);
3433 return 0;
3434}
3435
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003436/* Queue a slot enable or disable request on the command ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003437int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003438{
3439 return queue_command(xhci, 0, 0, 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003440 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003441}
3442
3443/* Queue an address device command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003444int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3445 u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003446{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003447 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3448 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003449 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
3450 false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003451}
Sarah Sharpf94e01862009-04-27 19:58:38 -07003452
Sarah Sharp02386342010-05-24 13:25:28 -07003453int xhci_queue_vendor_command(struct xhci_hcd *xhci,
3454 u32 field1, u32 field2, u32 field3, u32 field4)
3455{
3456 return queue_command(xhci, field1, field2, field3, field4, false);
3457}
3458
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003459/* Queue a reset device command TRB */
3460int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
3461{
3462 return queue_command(xhci, 0, 0, 0,
3463 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
3464 false);
3465}
3466
Sarah Sharpf94e01862009-04-27 19:58:38 -07003467/* Queue a configure endpoint command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003468int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003469 u32 slot_id, bool command_must_succeed)
Sarah Sharpf94e01862009-04-27 19:58:38 -07003470{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003471 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3472 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003473 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3474 command_must_succeed);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003475}
Sarah Sharpae636742009-04-29 19:02:31 -07003476
Sarah Sharpf2217e82009-08-07 14:04:43 -07003477/* Queue an evaluate context command TRB */
3478int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3479 u32 slot_id)
3480{
3481 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3482 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003483 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
3484 false);
Sarah Sharpf2217e82009-08-07 14:04:43 -07003485}
3486
Andiry Xube88fe42010-10-14 07:22:57 -07003487/*
3488 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3489 * activity on an endpoint that is about to be suspended.
3490 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003491int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
Andiry Xube88fe42010-10-14 07:22:57 -07003492 unsigned int ep_index, int suspend)
Sarah Sharpae636742009-04-29 19:02:31 -07003493{
3494 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3495 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3496 u32 type = TRB_TYPE(TRB_STOP_RING);
Andiry Xube88fe42010-10-14 07:22:57 -07003497 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
Sarah Sharpae636742009-04-29 19:02:31 -07003498
3499 return queue_command(xhci, 0, 0, 0,
Andiry Xube88fe42010-10-14 07:22:57 -07003500 trb_slot_id | trb_ep_index | type | trb_suspend, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003501}
3502
3503/* Set Transfer Ring Dequeue Pointer command.
3504 * This should not be used for endpoints that have streams enabled.
3505 */
3506static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003507 unsigned int ep_index, unsigned int stream_id,
3508 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -07003509 union xhci_trb *deq_ptr, u32 cycle_state)
3510{
3511 dma_addr_t addr;
3512 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3513 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003514 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
Sarah Sharpae636742009-04-29 19:02:31 -07003515 u32 type = TRB_TYPE(TRB_SET_DEQ);
Sarah Sharpbf161e82011-02-23 15:46:42 -08003516 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07003517
Sarah Sharp23e3be12009-04-29 19:05:20 -07003518 addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003519 if (addr == 0) {
Sarah Sharpae636742009-04-29 19:02:31 -07003520 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003521 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
3522 deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003523 return 0;
3524 }
Sarah Sharpbf161e82011-02-23 15:46:42 -08003525 ep = &xhci->devs[slot_id]->eps[ep_index];
3526 if ((ep->ep_state & SET_DEQ_PENDING)) {
3527 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
3528 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
3529 return 0;
3530 }
3531 ep->queued_deq_seg = deq_seg;
3532 ep->queued_deq_ptr = deq_ptr;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003533 return queue_command(xhci, lower_32_bits(addr) | cycle_state,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003534 upper_32_bits(addr), trb_stream_id,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003535 trb_slot_id | trb_ep_index | type, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003536}
Sarah Sharpa1587d92009-07-27 12:03:15 -07003537
3538int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
3539 unsigned int ep_index)
3540{
3541 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3542 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3543 u32 type = TRB_TYPE(TRB_RESET_EP);
3544
Sarah Sharp913a8a32009-09-04 10:53:13 -07003545 return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
3546 false);
Sarah Sharpa1587d92009-07-27 12:03:15 -07003547}