blob: 032af7e8a6bfc60bcd28f82d071238da541f7a5a [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 */
96static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
97 struct xhci_segment *seg, union xhci_trb *trb)
98{
99 if (ring == xhci->event_ring)
100 return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
101 (seg->next == xhci->event_ring->first_seg);
102 else
103 return trb->link.control & LINK_TOGGLE;
104}
105
106/* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
107 * segment? I.e. would the updated event TRB pointer step off the end of the
108 * event seg?
109 */
110static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
111 struct xhci_segment *seg, union xhci_trb *trb)
112{
113 if (ring == xhci->event_ring)
114 return trb == &seg->trbs[TRBS_PER_SEGMENT];
115 else
116 return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
117}
118
John Youn6c12db92010-05-10 15:33:00 -0700119static inline int enqueue_is_link_trb(struct xhci_ring *ring)
120{
121 struct xhci_link_trb *link = &ring->enqueue->link;
122 return ((link->control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK));
123}
124
Sarah Sharpae636742009-04-29 19:02:31 -0700125/* Updates trb to point to the next TRB in the ring, and updates seg if the next
126 * TRB is in a new segment. This does not skip over link TRBs, and it does not
127 * effect the ring dequeue or enqueue pointers.
128 */
129static void next_trb(struct xhci_hcd *xhci,
130 struct xhci_ring *ring,
131 struct xhci_segment **seg,
132 union xhci_trb **trb)
133{
134 if (last_trb(xhci, ring, *seg, *trb)) {
135 *seg = (*seg)->next;
136 *trb = ((*seg)->trbs);
137 } else {
John Youna1669b22010-08-09 13:56:11 -0700138 (*trb)++;
Sarah Sharpae636742009-04-29 19:02:31 -0700139 }
140}
141
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700142/*
143 * See Cycle bit rules. SW is the consumer for the event ring only.
144 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
145 */
146static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
147{
148 union xhci_trb *next = ++(ring->dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700149 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700150
151 ring->deq_updates++;
152 /* Update the dequeue pointer further if that was a link TRB or we're at
153 * the end of an event ring segment (which doesn't have link TRBS)
154 */
155 while (last_trb(xhci, ring, ring->deq_seg, next)) {
156 if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
157 ring->cycle_state = (ring->cycle_state ? 0 : 1);
158 if (!in_interrupt())
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700159 xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
160 ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700161 (unsigned int) ring->cycle_state);
162 }
163 ring->deq_seg = ring->deq_seg->next;
164 ring->dequeue = ring->deq_seg->trbs;
165 next = ring->dequeue;
166 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700167 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
168 if (ring == xhci->event_ring)
169 xhci_dbg(xhci, "Event ring deq = 0x%llx (DMA)\n", addr);
170 else if (ring == xhci->cmd_ring)
171 xhci_dbg(xhci, "Command ring deq = 0x%llx (DMA)\n", addr);
172 else
173 xhci_dbg(xhci, "Ring deq = 0x%llx (DMA)\n", addr);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700174}
175
176/*
177 * See Cycle bit rules. SW is the consumer for the event ring only.
178 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
179 *
180 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
181 * chain bit is set), then set the chain bit in all the following link TRBs.
182 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
183 * have their chain bit cleared (so that each Link TRB is a separate TD).
184 *
185 * 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 -0700186 * set, but other sections talk about dealing with the chain bit set. This was
187 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
188 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700189 *
190 * @more_trbs_coming: Will you enqueue more TRBs before calling
191 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700192 */
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700193static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
194 bool consumer, bool more_trbs_coming)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700195{
196 u32 chain;
197 union xhci_trb *next;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700198 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700199
200 chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
201 next = ++(ring->enqueue);
202
203 ring->enq_updates++;
204 /* Update the dequeue pointer further if that was a link TRB or we're at
205 * the end of an event ring segment (which doesn't have link TRBS)
206 */
207 while (last_trb(xhci, ring, ring->enq_seg, next)) {
208 if (!consumer) {
209 if (ring != xhci->event_ring) {
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700210 /*
211 * If the caller doesn't plan on enqueueing more
212 * TDs before ringing the doorbell, then we
213 * don't want to give the link TRB to the
214 * hardware just yet. We'll give the link TRB
215 * back in prepare_ring() just before we enqueue
216 * the TD at the top of the ring.
217 */
218 if (!chain && !more_trbs_coming)
John Youn6c12db92010-05-10 15:33:00 -0700219 break;
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700220
221 /* If we're not dealing with 0.95 hardware,
222 * carry over the chain bit of the previous TRB
223 * (which may mean the chain bit is cleared).
224 */
225 if (!xhci_link_trb_quirk(xhci)) {
226 next->link.control &= ~TRB_CHAIN;
227 next->link.control |= chain;
Sarah Sharpb0567b32009-08-07 14:04:36 -0700228 }
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700229 /* Give this link TRB to the hardware */
230 wmb();
231 next->link.control ^= TRB_CYCLE;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700232 }
233 /* Toggle the cycle bit after the last ring segment. */
234 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
235 ring->cycle_state = (ring->cycle_state ? 0 : 1);
236 if (!in_interrupt())
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700237 xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
238 ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700239 (unsigned int) ring->cycle_state);
240 }
241 }
242 ring->enq_seg = ring->enq_seg->next;
243 ring->enqueue = ring->enq_seg->trbs;
244 next = ring->enqueue;
245 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700246 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
247 if (ring == xhci->event_ring)
248 xhci_dbg(xhci, "Event ring enq = 0x%llx (DMA)\n", addr);
249 else if (ring == xhci->cmd_ring)
250 xhci_dbg(xhci, "Command ring enq = 0x%llx (DMA)\n", addr);
251 else
252 xhci_dbg(xhci, "Ring enq = 0x%llx (DMA)\n", addr);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700253}
254
255/*
256 * Check to see if there's room to enqueue num_trbs on the ring. See rules
257 * above.
258 * FIXME: this would be simpler and faster if we just kept track of the number
259 * of free TRBs in a ring.
260 */
261static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
262 unsigned int num_trbs)
263{
264 int i;
265 union xhci_trb *enq = ring->enqueue;
266 struct xhci_segment *enq_seg = ring->enq_seg;
Sarah Sharp44ebd032010-05-18 16:05:26 -0700267 struct xhci_segment *cur_seg;
268 unsigned int left_on_ring;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700269
John Youn6c12db92010-05-10 15:33:00 -0700270 /* If we are currently pointing to a link TRB, advance the
271 * enqueue pointer before checking for space */
272 while (last_trb(xhci, ring, enq_seg, enq)) {
273 enq_seg = enq_seg->next;
274 enq = enq_seg->trbs;
275 }
276
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700277 /* Check if ring is empty */
Sarah Sharp44ebd032010-05-18 16:05:26 -0700278 if (enq == ring->dequeue) {
279 /* Can't use link trbs */
280 left_on_ring = TRBS_PER_SEGMENT - 1;
281 for (cur_seg = enq_seg->next; cur_seg != enq_seg;
282 cur_seg = cur_seg->next)
283 left_on_ring += TRBS_PER_SEGMENT - 1;
284
285 /* Always need one TRB free in the ring. */
286 left_on_ring -= 1;
287 if (num_trbs > left_on_ring) {
288 xhci_warn(xhci, "Not enough room on ring; "
289 "need %u TRBs, %u TRBs left\n",
290 num_trbs, left_on_ring);
291 return 0;
292 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700293 return 1;
Sarah Sharp44ebd032010-05-18 16:05:26 -0700294 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700295 /* Make sure there's an extra empty TRB available */
296 for (i = 0; i <= num_trbs; ++i) {
297 if (enq == ring->dequeue)
298 return 0;
299 enq++;
300 while (last_trb(xhci, ring, enq_seg, enq)) {
301 enq_seg = enq_seg->next;
302 enq = enq_seg->trbs;
303 }
304 }
305 return 1;
306}
307
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700308/* Ring the host controller doorbell after placing a command on the ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700309void xhci_ring_cmd_db(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700310{
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700311 xhci_dbg(xhci, "// Ding dong!\n");
Matthew Wilcox50d646762010-12-15 14:18:11 -0500312 xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700313 /* Flush PCI posted writes */
314 xhci_readl(xhci, &xhci->dba->doorbell[0]);
315}
316
Andiry Xube88fe42010-10-14 07:22:57 -0700317void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700318 unsigned int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700319 unsigned int ep_index,
320 unsigned int stream_id)
Sarah Sharpae636742009-04-29 19:02:31 -0700321{
Sarah Sharpae636742009-04-29 19:02:31 -0700322 __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
Matthew Wilcox50d646762010-12-15 14:18:11 -0500323 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
324 unsigned int ep_state = ep->ep_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700325
Sarah Sharpae636742009-04-29 19:02:31 -0700326 /* Don't ring the doorbell for this endpoint if there are pending
Matthew Wilcox50d646762010-12-15 14:18:11 -0500327 * cancellations because we don't want to interrupt processing.
Sarah Sharp8df75f42010-04-02 15:34:16 -0700328 * We don't want to restart any stream rings if there's a set dequeue
329 * pointer command pending because the device can choose to start any
330 * stream once the endpoint is on the HW schedule.
331 * FIXME - check all the stream rings for pending cancellations.
Sarah Sharpae636742009-04-29 19:02:31 -0700332 */
Matthew Wilcox50d646762010-12-15 14:18:11 -0500333 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
334 (ep_state & EP_HALTED))
335 return;
336 xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr);
337 /* The CPU has better things to do at this point than wait for a
338 * write-posting flush. It'll get there soon enough.
339 */
Sarah Sharpae636742009-04-29 19:02:31 -0700340}
341
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700342/* Ring the doorbell for any rings with pending URBs */
343static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
344 unsigned int slot_id,
345 unsigned int ep_index)
346{
347 unsigned int stream_id;
348 struct xhci_virt_ep *ep;
349
350 ep = &xhci->devs[slot_id]->eps[ep_index];
351
352 /* A ring has pending URBs if its TD list is not empty */
353 if (!(ep->ep_state & EP_HAS_STREAMS)) {
354 if (!(list_empty(&ep->ring->td_list)))
Andiry Xube88fe42010-10-14 07:22:57 -0700355 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700356 return;
357 }
358
359 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
360 stream_id++) {
361 struct xhci_stream_info *stream_info = ep->stream_info;
362 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
Andiry Xube88fe42010-10-14 07:22:57 -0700363 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
364 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700365 }
366}
367
Sarah Sharpae636742009-04-29 19:02:31 -0700368/*
369 * Find the segment that trb is in. Start searching in start_seg.
370 * If we must move past a segment that has a link TRB with a toggle cycle state
371 * bit set, then we will toggle the value pointed at by cycle_state.
372 */
373static struct xhci_segment *find_trb_seg(
374 struct xhci_segment *start_seg,
375 union xhci_trb *trb, int *cycle_state)
376{
377 struct xhci_segment *cur_seg = start_seg;
378 struct xhci_generic_trb *generic_trb;
379
380 while (cur_seg->trbs > trb ||
381 &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
382 generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800383 if (generic_trb->field[3] & LINK_TOGGLE)
384 *cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700385 cur_seg = cur_seg->next;
386 if (cur_seg == start_seg)
387 /* Looped over the entire list. Oops! */
Randy Dunlap326b4812010-04-19 08:53:50 -0700388 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700389 }
390 return cur_seg;
391}
392
Sarah Sharp021bff92010-07-29 22:12:20 -0700393
394static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
395 unsigned int slot_id, unsigned int ep_index,
396 unsigned int stream_id)
397{
398 struct xhci_virt_ep *ep;
399
400 ep = &xhci->devs[slot_id]->eps[ep_index];
401 /* Common case: no streams */
402 if (!(ep->ep_state & EP_HAS_STREAMS))
403 return ep->ring;
404
405 if (stream_id == 0) {
406 xhci_warn(xhci,
407 "WARN: Slot ID %u, ep index %u has streams, "
408 "but URB has no stream ID.\n",
409 slot_id, ep_index);
410 return NULL;
411 }
412
413 if (stream_id < ep->stream_info->num_streams)
414 return ep->stream_info->stream_rings[stream_id];
415
416 xhci_warn(xhci,
417 "WARN: Slot ID %u, ep index %u has "
418 "stream IDs 1 to %u allocated, "
419 "but stream ID %u is requested.\n",
420 slot_id, ep_index,
421 ep->stream_info->num_streams - 1,
422 stream_id);
423 return NULL;
424}
425
426/* Get the right ring for the given URB.
427 * If the endpoint supports streams, boundary check the URB's stream ID.
428 * If the endpoint doesn't support streams, return the singular endpoint ring.
429 */
430static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
431 struct urb *urb)
432{
433 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
434 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
435}
436
Sarah Sharpae636742009-04-29 19:02:31 -0700437/*
438 * Move the xHC's endpoint ring dequeue pointer past cur_td.
439 * Record the new state of the xHC's endpoint ring dequeue segment,
440 * dequeue pointer, and new consumer cycle state in state.
441 * Update our internal representation of the ring's dequeue pointer.
442 *
443 * We do this in three jumps:
444 * - First we update our new ring state to be the same as when the xHC stopped.
445 * - Then we traverse the ring to find the segment that contains
446 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
447 * any link TRBs with the toggle cycle bit set.
448 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
449 * if we've moved it past a link TRB with the toggle cycle bit set.
450 */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700451void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700452 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700453 unsigned int stream_id, struct xhci_td *cur_td,
454 struct xhci_dequeue_state *state)
Sarah Sharpae636742009-04-29 19:02:31 -0700455{
456 struct xhci_virt_device *dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700457 struct xhci_ring *ep_ring;
Sarah Sharpae636742009-04-29 19:02:31 -0700458 struct xhci_generic_trb *trb;
John Yound115b042009-07-27 12:05:15 -0700459 struct xhci_ep_ctx *ep_ctx;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700460 dma_addr_t addr;
Sarah Sharpae636742009-04-29 19:02:31 -0700461
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700462 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
463 ep_index, stream_id);
464 if (!ep_ring) {
465 xhci_warn(xhci, "WARN can't find new dequeue state "
466 "for invalid stream ID %u.\n",
467 stream_id);
468 return;
469 }
Sarah Sharpae636742009-04-29 19:02:31 -0700470 state->new_cycle_state = 0;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700471 xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700472 state->new_deq_seg = find_trb_seg(cur_td->start_seg,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700473 dev->eps[ep_index].stopped_trb,
Sarah Sharpae636742009-04-29 19:02:31 -0700474 &state->new_cycle_state);
475 if (!state->new_deq_seg)
476 BUG();
477 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700478 xhci_dbg(xhci, "Finding endpoint context\n");
John Yound115b042009-07-27 12:05:15 -0700479 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
480 state->new_cycle_state = 0x1 & ep_ctx->deq;
Sarah Sharpae636742009-04-29 19:02:31 -0700481
482 state->new_deq_ptr = cur_td->last_trb;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700483 xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700484 state->new_deq_seg = find_trb_seg(state->new_deq_seg,
485 state->new_deq_ptr,
486 &state->new_cycle_state);
487 if (!state->new_deq_seg)
488 BUG();
489
490 trb = &state->new_deq_ptr->generic;
Andiry Xu54b5acf2010-05-10 19:57:17 -0700491 if ((trb->field[3] & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK) &&
Sarah Sharpae636742009-04-29 19:02:31 -0700492 (trb->field[3] & LINK_TOGGLE))
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800493 state->new_cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700494 next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
495
Sarah Sharp01a1fdb2011-02-23 18:12:29 -0800496 /*
497 * If there is only one segment in a ring, find_trb_seg()'s while loop
498 * will not run, and it will return before it has a chance to see if it
499 * needs to toggle the cycle bit. It can't tell if the stalled transfer
500 * ended just before the link TRB on a one-segment ring, or if the TD
501 * wrapped around the top of the ring, because it doesn't have the TD in
502 * question. Look for the one-segment case where stalled TRB's address
503 * is greater than the new dequeue pointer address.
504 */
505 if (ep_ring->first_seg == ep_ring->first_seg->next &&
506 state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
507 state->new_cycle_state ^= 0x1;
508 xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
509
Sarah Sharpae636742009-04-29 19:02:31 -0700510 /* Don't update the ring cycle state for the producer (us). */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700511 xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
512 state->new_deq_seg);
513 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
514 xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
515 (unsigned long long) addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700516}
517
Sarah Sharp23e3be12009-04-29 19:05:20 -0700518static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Sarah Sharpae636742009-04-29 19:02:31 -0700519 struct xhci_td *cur_td)
520{
521 struct xhci_segment *cur_seg;
522 union xhci_trb *cur_trb;
523
524 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
525 true;
526 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
527 if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
528 TRB_TYPE(TRB_LINK)) {
529 /* Unchain any chained Link TRBs, but
530 * leave the pointers intact.
531 */
532 cur_trb->generic.field[3] &= ~TRB_CHAIN;
533 xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700534 xhci_dbg(xhci, "Address = %p (0x%llx dma); "
535 "in seg %p (0x%llx dma)\n",
536 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700537 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700538 cur_seg,
539 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700540 } else {
541 cur_trb->generic.field[0] = 0;
542 cur_trb->generic.field[1] = 0;
543 cur_trb->generic.field[2] = 0;
544 /* Preserve only the cycle bit of this TRB */
545 cur_trb->generic.field[3] &= TRB_CYCLE;
546 cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700547 xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
548 "in seg %p (0x%llx dma)\n",
549 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700550 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700551 cur_seg,
552 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700553 }
554 if (cur_trb == cur_td->last_trb)
555 break;
556 }
557}
558
559static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700560 unsigned int ep_index, unsigned int stream_id,
561 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -0700562 union xhci_trb *deq_ptr, u32 cycle_state);
563
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700564void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700565 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700566 unsigned int stream_id,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700567 struct xhci_dequeue_state *deq_state)
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700568{
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700569 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
570
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700571 xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
572 "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
573 deq_state->new_deq_seg,
574 (unsigned long long)deq_state->new_deq_seg->dma,
575 deq_state->new_deq_ptr,
576 (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
577 deq_state->new_cycle_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700578 queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700579 deq_state->new_deq_seg,
580 deq_state->new_deq_ptr,
581 (u32) deq_state->new_cycle_state);
582 /* Stop the TD queueing code from ringing the doorbell until
583 * this command completes. The HC won't set the dequeue pointer
584 * if the ring is running, and ringing the doorbell starts the
585 * ring running.
586 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700587 ep->ep_state |= SET_DEQ_PENDING;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700588}
589
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700590static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
591 struct xhci_virt_ep *ep)
592{
593 ep->ep_state &= ~EP_HALT_PENDING;
594 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
595 * timer is running on another CPU, we don't decrement stop_cmds_pending
596 * (since we didn't successfully stop the watchdog timer).
597 */
598 if (del_timer(&ep->stop_cmd_timer))
599 ep->stop_cmds_pending--;
600}
601
602/* Must be called with xhci->lock held in interrupt context */
603static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
604 struct xhci_td *cur_td, int status, char *adjective)
605{
Sarah Sharp214f76f2010-10-26 11:22:02 -0700606 struct usb_hcd *hcd;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700607 struct urb *urb;
608 struct urb_priv *urb_priv;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700609
Andiry Xu8e51adc2010-07-22 15:23:31 -0700610 urb = cur_td->urb;
611 urb_priv = urb->hcpriv;
612 urb_priv->td_cnt++;
Sarah Sharp214f76f2010-10-26 11:22:02 -0700613 hcd = bus_to_hcd(urb->dev->bus);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700614
Andiry Xu8e51adc2010-07-22 15:23:31 -0700615 /* Only giveback urb when this is the last td in urb */
616 if (urb_priv->td_cnt == urb_priv->length) {
617 usb_hcd_unlink_urb_from_ep(hcd, urb);
618 xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, urb);
619
620 spin_unlock(&xhci->lock);
621 usb_hcd_giveback_urb(hcd, urb, status);
622 xhci_urb_free_priv(xhci, urb_priv);
623 spin_lock(&xhci->lock);
624 xhci_dbg(xhci, "%s URB given back\n", adjective);
625 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700626}
627
Sarah Sharpae636742009-04-29 19:02:31 -0700628/*
629 * When we get a command completion for a Stop Endpoint Command, we need to
630 * unlink any cancelled TDs from the ring. There are two ways to do that:
631 *
632 * 1. If the HW was in the middle of processing the TD that needs to be
633 * cancelled, then we must move the ring's dequeue pointer past the last TRB
634 * in the TD with a Set Dequeue Pointer Command.
635 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
636 * bit cleared) so that the HW will skip over them.
637 */
638static void handle_stopped_endpoint(struct xhci_hcd *xhci,
Andiry Xube88fe42010-10-14 07:22:57 -0700639 union xhci_trb *trb, struct xhci_event_cmd *event)
Sarah Sharpae636742009-04-29 19:02:31 -0700640{
641 unsigned int slot_id;
642 unsigned int ep_index;
Andiry Xube88fe42010-10-14 07:22:57 -0700643 struct xhci_virt_device *virt_dev;
Sarah Sharpae636742009-04-29 19:02:31 -0700644 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700645 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700646 struct list_head *entry;
Randy Dunlap326b4812010-04-19 08:53:50 -0700647 struct xhci_td *cur_td = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700648 struct xhci_td *last_unlinked_td;
649
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700650 struct xhci_dequeue_state deq_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700651
Andiry Xube88fe42010-10-14 07:22:57 -0700652 if (unlikely(TRB_TO_SUSPEND_PORT(
653 xhci->cmd_ring->dequeue->generic.field[3]))) {
654 slot_id = TRB_TO_SLOT_ID(
655 xhci->cmd_ring->dequeue->generic.field[3]);
656 virt_dev = xhci->devs[slot_id];
657 if (virt_dev)
658 handle_cmd_in_cmd_wait_list(xhci, virt_dev,
659 event);
660 else
661 xhci_warn(xhci, "Stop endpoint command "
662 "completion for disabled slot %u\n",
663 slot_id);
664 return;
665 }
666
Sarah Sharpae636742009-04-29 19:02:31 -0700667 memset(&deq_state, 0, sizeof(deq_state));
668 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
669 ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700670 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharpae636742009-04-29 19:02:31 -0700671
Sarah Sharp678539c2009-10-27 10:55:52 -0700672 if (list_empty(&ep->cancelled_td_list)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700673 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700674 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700675 return;
Sarah Sharp678539c2009-10-27 10:55:52 -0700676 }
Sarah Sharpae636742009-04-29 19:02:31 -0700677
678 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
679 * We have the xHCI lock, so nothing can modify this list until we drop
680 * it. We're also in the event handler, so we can't get re-interrupted
681 * if another Stop Endpoint command completes
682 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700683 list_for_each(entry, &ep->cancelled_td_list) {
Sarah Sharpae636742009-04-29 19:02:31 -0700684 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700685 xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
686 cur_td->first_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700687 (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700688 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
689 if (!ep_ring) {
690 /* This shouldn't happen unless a driver is mucking
691 * with the stream ID after submission. This will
692 * leave the TD on the hardware ring, and the hardware
693 * will try to execute it, and may access a buffer
694 * that has already been freed. In the best case, the
695 * hardware will execute it, and the event handler will
696 * ignore the completion event for that TD, since it was
697 * removed from the td_list for that endpoint. In
698 * short, don't muck with the stream ID after
699 * submission.
700 */
701 xhci_warn(xhci, "WARN Cancelled URB %p "
702 "has invalid stream ID %u.\n",
703 cur_td->urb,
704 cur_td->urb->stream_id);
705 goto remove_finished_td;
706 }
Sarah Sharpae636742009-04-29 19:02:31 -0700707 /*
708 * If we stopped on the TD we need to cancel, then we have to
709 * move the xHC endpoint ring dequeue pointer past this TD.
710 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700711 if (cur_td == ep->stopped_td)
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700712 xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
713 cur_td->urb->stream_id,
714 cur_td, &deq_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700715 else
716 td_to_noop(xhci, ep_ring, cur_td);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700717remove_finished_td:
Sarah Sharpae636742009-04-29 19:02:31 -0700718 /*
719 * The event handler won't see a completion for this TD anymore,
720 * so remove it from the endpoint ring's TD list. Keep it in
721 * the cancelled TD list for URB completion later.
722 */
723 list_del(&cur_td->td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700724 }
725 last_unlinked_td = cur_td;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700726 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharpae636742009-04-29 19:02:31 -0700727
728 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
729 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700730 xhci_queue_new_dequeue_state(xhci,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700731 slot_id, ep_index,
732 ep->stopped_td->urb->stream_id,
733 &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700734 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700735 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700736 /* Otherwise ring the doorbell(s) to restart queued transfers */
737 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700738 }
Sarah Sharp1624ae12010-05-06 13:40:08 -0700739 ep->stopped_td = NULL;
740 ep->stopped_trb = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700741
742 /*
743 * Drop the lock and complete the URBs in the cancelled TD list.
744 * New TDs to be cancelled might be added to the end of the list before
745 * we can complete all the URBs for the TDs we already unlinked.
746 * So stop when we've completed the URB for the last TD we unlinked.
747 */
748 do {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700749 cur_td = list_entry(ep->cancelled_td_list.next,
Sarah Sharpae636742009-04-29 19:02:31 -0700750 struct xhci_td, cancelled_td_list);
751 list_del(&cur_td->cancelled_td_list);
752
753 /* Clean up the cancelled URB */
Sarah Sharpae636742009-04-29 19:02:31 -0700754 /* Doesn't matter what we pass for status, since the core will
755 * just overwrite it (because the URB has been unlinked).
756 */
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700757 xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
Sarah Sharpae636742009-04-29 19:02:31 -0700758
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700759 /* Stop processing the cancelled list if the watchdog timer is
760 * running.
761 */
762 if (xhci->xhc_state & XHCI_STATE_DYING)
763 return;
Sarah Sharpae636742009-04-29 19:02:31 -0700764 } while (cur_td != last_unlinked_td);
765
766 /* Return to the event handler with xhci->lock re-acquired */
767}
768
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700769/* Watchdog timer function for when a stop endpoint command fails to complete.
770 * In this case, we assume the host controller is broken or dying or dead. The
771 * host may still be completing some other events, so we have to be careful to
772 * let the event ring handler and the URB dequeueing/enqueueing functions know
773 * through xhci->state.
774 *
775 * The timer may also fire if the host takes a very long time to respond to the
776 * command, and the stop endpoint command completion handler cannot delete the
777 * timer before the timer function is called. Another endpoint cancellation may
778 * sneak in before the timer function can grab the lock, and that may queue
779 * another stop endpoint command and add the timer back. So we cannot use a
780 * simple flag to say whether there is a pending stop endpoint command for a
781 * particular endpoint.
782 *
783 * Instead we use a combination of that flag and a counter for the number of
784 * pending stop endpoint commands. If the timer is the tail end of the last
785 * stop endpoint command, and the endpoint's command is still pending, we assume
786 * the host is dying.
787 */
788void xhci_stop_endpoint_command_watchdog(unsigned long arg)
789{
790 struct xhci_hcd *xhci;
791 struct xhci_virt_ep *ep;
792 struct xhci_virt_ep *temp_ep;
793 struct xhci_ring *ring;
794 struct xhci_td *cur_td;
795 int ret, i, j;
796
797 ep = (struct xhci_virt_ep *) arg;
798 xhci = ep->xhci;
799
800 spin_lock(&xhci->lock);
801
802 ep->stop_cmds_pending--;
803 if (xhci->xhc_state & XHCI_STATE_DYING) {
804 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
805 "xHCI as DYING, exiting.\n");
806 spin_unlock(&xhci->lock);
807 return;
808 }
809 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
810 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
811 "exiting.\n");
812 spin_unlock(&xhci->lock);
813 return;
814 }
815
816 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
817 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
818 /* Oops, HC is dead or dying or at least not responding to the stop
819 * endpoint command.
820 */
821 xhci->xhc_state |= XHCI_STATE_DYING;
822 /* Disable interrupts from the host controller and start halting it */
823 xhci_quiesce(xhci);
824 spin_unlock(&xhci->lock);
825
826 ret = xhci_halt(xhci);
827
828 spin_lock(&xhci->lock);
829 if (ret < 0) {
830 /* This is bad; the host is not responding to commands and it's
831 * not allowing itself to be halted. At least interrupts are
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800832 * disabled. If we call usb_hc_died(), it will attempt to
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700833 * disconnect all device drivers under this host. Those
834 * disconnect() methods will wait for all URBs to be unlinked,
835 * so we must complete them.
836 */
837 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
838 xhci_warn(xhci, "Completing active URBs anyway.\n");
839 /* We could turn all TDs on the rings to no-ops. This won't
840 * help if the host has cached part of the ring, and is slow if
841 * we want to preserve the cycle bit. Skip it and hope the host
842 * doesn't touch the memory.
843 */
844 }
845 for (i = 0; i < MAX_HC_SLOTS; i++) {
846 if (!xhci->devs[i])
847 continue;
848 for (j = 0; j < 31; j++) {
849 temp_ep = &xhci->devs[i]->eps[j];
850 ring = temp_ep->ring;
851 if (!ring)
852 continue;
853 xhci_dbg(xhci, "Killing URBs for slot ID %u, "
854 "ep index %u\n", i, j);
855 while (!list_empty(&ring->td_list)) {
856 cur_td = list_first_entry(&ring->td_list,
857 struct xhci_td,
858 td_list);
859 list_del(&cur_td->td_list);
860 if (!list_empty(&cur_td->cancelled_td_list))
861 list_del(&cur_td->cancelled_td_list);
862 xhci_giveback_urb_in_irq(xhci, cur_td,
863 -ESHUTDOWN, "killed");
864 }
865 while (!list_empty(&temp_ep->cancelled_td_list)) {
866 cur_td = list_first_entry(
867 &temp_ep->cancelled_td_list,
868 struct xhci_td,
869 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 }
875 }
876 spin_unlock(&xhci->lock);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700877 xhci_dbg(xhci, "Calling usb_hc_died()\n");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800878 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700879 xhci_dbg(xhci, "xHCI host controller is dead.\n");
880}
881
Sarah Sharpae636742009-04-29 19:02:31 -0700882/*
883 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
884 * we need to clear the set deq pending flag in the endpoint ring state, so that
885 * the TD queueing code can ring the doorbell again. We also need to ring the
886 * endpoint doorbell to restart the ring, but only if there aren't more
887 * cancellations pending.
888 */
889static void handle_set_deq_completion(struct xhci_hcd *xhci,
890 struct xhci_event_cmd *event,
891 union xhci_trb *trb)
892{
893 unsigned int slot_id;
894 unsigned int ep_index;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700895 unsigned int stream_id;
Sarah Sharpae636742009-04-29 19:02:31 -0700896 struct xhci_ring *ep_ring;
897 struct xhci_virt_device *dev;
John Yound115b042009-07-27 12:05:15 -0700898 struct xhci_ep_ctx *ep_ctx;
899 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpae636742009-04-29 19:02:31 -0700900
901 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
902 ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700903 stream_id = TRB_TO_STREAM_ID(trb->generic.field[2]);
Sarah Sharpae636742009-04-29 19:02:31 -0700904 dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700905
906 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
907 if (!ep_ring) {
908 xhci_warn(xhci, "WARN Set TR deq ptr command for "
909 "freed stream ID %u\n",
910 stream_id);
911 /* XXX: Harmless??? */
912 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
913 return;
914 }
915
John Yound115b042009-07-27 12:05:15 -0700916 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
917 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
Sarah Sharpae636742009-04-29 19:02:31 -0700918
919 if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
920 unsigned int ep_state;
921 unsigned int slot_state;
922
923 switch (GET_COMP_CODE(event->status)) {
924 case COMP_TRB_ERR:
925 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
926 "of stream ID configuration\n");
927 break;
928 case COMP_CTX_STATE:
929 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
930 "to incorrect slot or ep state.\n");
John Yound115b042009-07-27 12:05:15 -0700931 ep_state = ep_ctx->ep_info;
Sarah Sharpae636742009-04-29 19:02:31 -0700932 ep_state &= EP_STATE_MASK;
John Yound115b042009-07-27 12:05:15 -0700933 slot_state = slot_ctx->dev_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700934 slot_state = GET_SLOT_STATE(slot_state);
935 xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
936 slot_state, ep_state);
937 break;
938 case COMP_EBADSLT:
939 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
940 "slot %u was not enabled.\n", slot_id);
941 break;
942 default:
943 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
944 "completion code of %u.\n",
945 GET_COMP_CODE(event->status));
946 break;
947 }
948 /* OK what do we do now? The endpoint state is hosed, and we
949 * should never get to this point if the synchronization between
950 * queueing, and endpoint state are correct. This might happen
951 * if the device gets disconnected after we've finished
952 * cancelling URBs, which might not be an error...
953 */
954 } else {
Sarah Sharp8e595a52009-07-27 12:03:31 -0700955 xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
John Yound115b042009-07-27 12:05:15 -0700956 ep_ctx->deq);
Sarah Sharpbf161e82011-02-23 15:46:42 -0800957 if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg,
958 dev->eps[ep_index].queued_deq_ptr) ==
959 (ep_ctx->deq & ~(EP_CTX_CYCLE_MASK))) {
960 /* Update the ring's dequeue segment and dequeue pointer
961 * to reflect the new position.
962 */
963 ep_ring->deq_seg = dev->eps[ep_index].queued_deq_seg;
964 ep_ring->dequeue = dev->eps[ep_index].queued_deq_ptr;
965 } else {
966 xhci_warn(xhci, "Mismatch between completed Set TR Deq "
967 "Ptr command & xHCI internal state.\n");
968 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
969 dev->eps[ep_index].queued_deq_seg,
970 dev->eps[ep_index].queued_deq_ptr);
971 }
Sarah Sharpae636742009-04-29 19:02:31 -0700972 }
973
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700974 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
Sarah Sharpbf161e82011-02-23 15:46:42 -0800975 dev->eps[ep_index].queued_deq_seg = NULL;
976 dev->eps[ep_index].queued_deq_ptr = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700977 /* Restart any rings with pending URBs */
978 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700979}
980
Sarah Sharpa1587d92009-07-27 12:03:15 -0700981static void handle_reset_ep_completion(struct xhci_hcd *xhci,
982 struct xhci_event_cmd *event,
983 union xhci_trb *trb)
984{
985 int slot_id;
986 unsigned int ep_index;
987
988 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
989 ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
990 /* This command will only fail if the endpoint wasn't halted,
991 * but we don't care.
992 */
993 xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
994 (unsigned int) GET_COMP_CODE(event->status));
995
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700996 /* HW with the reset endpoint quirk needs to have a configure endpoint
997 * command complete before the endpoint can be used. Queue that here
998 * because the HW can't handle two commands being queued in a row.
999 */
1000 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
1001 xhci_dbg(xhci, "Queueing configure endpoint command\n");
1002 xhci_queue_configure_endpoint(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001003 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1004 false);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001005 xhci_ring_cmd_db(xhci);
1006 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001007 /* Clear our internal halted state and restart the ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001008 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001009 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001010 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001011}
Sarah Sharpae636742009-04-29 19:02:31 -07001012
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001013/* Check to see if a command in the device's command queue matches this one.
1014 * Signal the completion or free the command, and return 1. Return 0 if the
1015 * completed command isn't at the head of the command list.
1016 */
1017static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
1018 struct xhci_virt_device *virt_dev,
1019 struct xhci_event_cmd *event)
1020{
1021 struct xhci_command *command;
1022
1023 if (list_empty(&virt_dev->cmd_list))
1024 return 0;
1025
1026 command = list_entry(virt_dev->cmd_list.next,
1027 struct xhci_command, cmd_list);
1028 if (xhci->cmd_ring->dequeue != command->command_trb)
1029 return 0;
1030
1031 command->status =
1032 GET_COMP_CODE(event->status);
1033 list_del(&command->cmd_list);
1034 if (command->completion)
1035 complete(command->completion);
1036 else
1037 xhci_free_command(xhci, command);
1038 return 1;
1039}
1040
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001041static void handle_cmd_completion(struct xhci_hcd *xhci,
1042 struct xhci_event_cmd *event)
1043{
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001044 int slot_id = TRB_TO_SLOT_ID(event->flags);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001045 u64 cmd_dma;
1046 dma_addr_t cmd_dequeue_dma;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001047 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001048 struct xhci_virt_device *virt_dev;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001049 unsigned int ep_index;
1050 struct xhci_ring *ep_ring;
1051 unsigned int ep_state;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001052
Sarah Sharp8e595a52009-07-27 12:03:31 -07001053 cmd_dma = event->cmd_trb;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001054 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001055 xhci->cmd_ring->dequeue);
1056 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1057 if (cmd_dequeue_dma == 0) {
1058 xhci->error_bitmask |= 1 << 4;
1059 return;
1060 }
1061 /* Does the DMA address match our internal dequeue pointer address? */
1062 if (cmd_dma != (u64) cmd_dequeue_dma) {
1063 xhci->error_bitmask |= 1 << 5;
1064 return;
1065 }
1066 switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001067 case TRB_TYPE(TRB_ENABLE_SLOT):
1068 if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
1069 xhci->slot_id = slot_id;
1070 else
1071 xhci->slot_id = 0;
1072 complete(&xhci->addr_dev);
1073 break;
1074 case TRB_TYPE(TRB_DISABLE_SLOT):
1075 if (xhci->devs[slot_id])
1076 xhci_free_virt_device(xhci, slot_id);
1077 break;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001078 case TRB_TYPE(TRB_CONFIG_EP):
Sarah Sharp913a8a32009-09-04 10:53:13 -07001079 virt_dev = xhci->devs[slot_id];
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001080 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
Sarah Sharp913a8a32009-09-04 10:53:13 -07001081 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001082 /*
1083 * Configure endpoint commands can come from the USB core
1084 * configuration or alt setting changes, or because the HW
1085 * needed an extra configure endpoint command after a reset
Sarah Sharp8df75f42010-04-02 15:34:16 -07001086 * endpoint command or streams were being configured.
1087 * If the command was for a halted endpoint, the xHCI driver
1088 * is not waiting on the configure endpoint command.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001089 */
1090 ctrl_ctx = xhci_get_input_control_ctx(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001091 virt_dev->in_ctx);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001092 /* Input ctx add_flags are the endpoint index plus one */
1093 ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
Sarah Sharp06df5722009-12-03 09:44:31 -08001094 /* A usb_set_interface() call directly after clearing a halted
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001095 * condition may race on this quirky hardware. Not worth
1096 * worrying about, since this is prototype hardware. Not sure
1097 * if this will work for streams, but streams support was
1098 * untested on this prototype.
Sarah Sharp06df5722009-12-03 09:44:31 -08001099 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001100 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
Sarah Sharp06df5722009-12-03 09:44:31 -08001101 ep_index != (unsigned int) -1 &&
1102 ctrl_ctx->add_flags - SLOT_FLAG ==
1103 ctrl_ctx->drop_flags) {
1104 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
1105 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1106 if (!(ep_state & EP_HALTED))
1107 goto bandwidth_change;
1108 xhci_dbg(xhci, "Completed config ep cmd - "
1109 "last ep index = %d, state = %d\n",
1110 ep_index, ep_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001111 /* Clear internal halted state and restart ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001112 xhci->devs[slot_id]->eps[ep_index].ep_state &=
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001113 ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001114 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharp06df5722009-12-03 09:44:31 -08001115 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001116 }
Sarah Sharp06df5722009-12-03 09:44:31 -08001117bandwidth_change:
1118 xhci_dbg(xhci, "Completed config ep cmd\n");
1119 xhci->devs[slot_id]->cmd_status =
1120 GET_COMP_CODE(event->status);
1121 complete(&xhci->devs[slot_id]->cmd_completion);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001122 break;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001123 case TRB_TYPE(TRB_EVAL_CONTEXT):
Sarah Sharpac1c1b72009-09-04 10:53:20 -07001124 virt_dev = xhci->devs[slot_id];
1125 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
1126 break;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001127 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
1128 complete(&xhci->devs[slot_id]->cmd_completion);
1129 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001130 case TRB_TYPE(TRB_ADDR_DEV):
1131 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
1132 complete(&xhci->addr_dev);
1133 break;
Sarah Sharpae636742009-04-29 19:02:31 -07001134 case TRB_TYPE(TRB_STOP_RING):
Andiry Xube88fe42010-10-14 07:22:57 -07001135 handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event);
Sarah Sharpae636742009-04-29 19:02:31 -07001136 break;
1137 case TRB_TYPE(TRB_SET_DEQ):
1138 handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
1139 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001140 case TRB_TYPE(TRB_CMD_NOOP):
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001141 break;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001142 case TRB_TYPE(TRB_RESET_EP):
1143 handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
1144 break;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001145 case TRB_TYPE(TRB_RESET_DEV):
1146 xhci_dbg(xhci, "Completed reset device command.\n");
1147 slot_id = TRB_TO_SLOT_ID(
1148 xhci->cmd_ring->dequeue->generic.field[3]);
1149 virt_dev = xhci->devs[slot_id];
1150 if (virt_dev)
1151 handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
1152 else
1153 xhci_warn(xhci, "Reset device command completion "
1154 "for disabled slot %u\n", slot_id);
1155 break;
Sarah Sharp02386342010-05-24 13:25:28 -07001156 case TRB_TYPE(TRB_NEC_GET_FW):
1157 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1158 xhci->error_bitmask |= 1 << 6;
1159 break;
1160 }
1161 xhci_dbg(xhci, "NEC firmware version %2x.%02x\n",
1162 NEC_FW_MAJOR(event->status),
1163 NEC_FW_MINOR(event->status));
1164 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001165 default:
1166 /* Skip over unknown commands on the event ring */
1167 xhci->error_bitmask |= 1 << 6;
1168 break;
1169 }
1170 inc_deq(xhci, xhci->cmd_ring, false);
1171}
1172
Sarah Sharp02386342010-05-24 13:25:28 -07001173static void handle_vendor_event(struct xhci_hcd *xhci,
1174 union xhci_trb *event)
1175{
1176 u32 trb_type;
1177
1178 trb_type = TRB_FIELD_TO_TYPE(event->generic.field[3]);
1179 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1180 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1181 handle_cmd_completion(xhci, &event->event_cmd);
1182}
1183
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001184/* @port_id: the one-based port ID from the hardware (indexed from array of all
1185 * port registers -- USB 3.0 and USB 2.0).
1186 *
1187 * Returns a zero-based port number, which is suitable for indexing into each of
1188 * the split roothubs' port arrays and bus state arrays.
1189 */
1190static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1191 struct xhci_hcd *xhci, u32 port_id)
1192{
1193 unsigned int i;
1194 unsigned int num_similar_speed_ports = 0;
1195
1196 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1197 * and usb2_ports are 0-based indexes. Count the number of similar
1198 * speed ports, up to 1 port before this port.
1199 */
1200 for (i = 0; i < (port_id - 1); i++) {
1201 u8 port_speed = xhci->port_array[i];
1202
1203 /*
1204 * Skip ports that don't have known speeds, or have duplicate
1205 * Extended Capabilities port speed entries.
1206 */
1207 if (port_speed == 0 || port_speed == -1)
1208 continue;
1209
1210 /*
1211 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1212 * 1.1 ports are under the USB 2.0 hub. If the port speed
1213 * matches the device speed, it's a similar speed port.
1214 */
1215 if ((port_speed == 0x03) == (hcd->speed == HCD_USB3))
1216 num_similar_speed_ports++;
1217 }
1218 return num_similar_speed_ports;
1219}
1220
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001221static void handle_port_status(struct xhci_hcd *xhci,
1222 union xhci_trb *event)
1223{
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001224 struct usb_hcd *hcd;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001225 u32 port_id;
Andiry Xu56192532010-10-14 07:23:00 -07001226 u32 temp, temp1;
Sarah Sharp518e8482010-12-15 11:56:29 -08001227 int max_ports;
Andiry Xu56192532010-10-14 07:23:00 -07001228 int slot_id;
Sarah Sharp5308a912010-12-01 11:34:59 -08001229 unsigned int faked_port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001230 u8 major_revision;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001231 struct xhci_bus_state *bus_state;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001232 u32 __iomem **port_array;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001233
1234 /* Port status change events always have a successful completion code */
1235 if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
1236 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1237 xhci->error_bitmask |= 1 << 8;
1238 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001239 port_id = GET_PORT_ID(event->generic.field[0]);
1240 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1241
Sarah Sharp518e8482010-12-15 11:56:29 -08001242 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1243 if ((port_id <= 0) || (port_id > max_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001244 xhci_warn(xhci, "Invalid port id %d\n", port_id);
1245 goto cleanup;
1246 }
1247
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001248 /* Figure out which usb_hcd this port is attached to:
1249 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1250 */
1251 major_revision = xhci->port_array[port_id - 1];
1252 if (major_revision == 0) {
1253 xhci_warn(xhci, "Event for port %u not in "
1254 "Extended Capabilities, ignoring.\n",
1255 port_id);
1256 goto cleanup;
1257 }
1258 if (major_revision == (u8) -1) {
1259 xhci_warn(xhci, "Event for port %u duplicated in"
1260 "Extended Capabilities, ignoring.\n",
1261 port_id);
1262 goto cleanup;
Sarah Sharp5308a912010-12-01 11:34:59 -08001263 }
1264
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001265 /*
1266 * Hardware port IDs reported by a Port Status Change Event include USB
1267 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1268 * resume event, but we first need to translate the hardware port ID
1269 * into the index into the ports on the correct split roothub, and the
1270 * correct bus_state structure.
1271 */
1272 /* Find the right roothub. */
1273 hcd = xhci_to_hcd(xhci);
1274 if ((major_revision == 0x03) != (hcd->speed == HCD_USB3))
1275 hcd = xhci->shared_hcd;
1276 bus_state = &xhci->bus_state[hcd_index(hcd)];
1277 if (hcd->speed == HCD_USB3)
1278 port_array = xhci->usb3_ports;
1279 else
1280 port_array = xhci->usb2_ports;
1281 /* Find the faked port hub number */
1282 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1283 port_id);
1284
Sarah Sharp5308a912010-12-01 11:34:59 -08001285 temp = xhci_readl(xhci, port_array[faked_port_index]);
Sarah Sharp7111ebc2010-12-14 13:24:55 -08001286 if (hcd->state == HC_STATE_SUSPENDED) {
Andiry Xu56192532010-10-14 07:23:00 -07001287 xhci_dbg(xhci, "resume root hub\n");
1288 usb_hcd_resume_root_hub(hcd);
1289 }
1290
1291 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1292 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1293
1294 temp1 = xhci_readl(xhci, &xhci->op_regs->command);
1295 if (!(temp1 & CMD_RUN)) {
1296 xhci_warn(xhci, "xHC is not running.\n");
1297 goto cleanup;
1298 }
1299
1300 if (DEV_SUPERSPEED(temp)) {
1301 xhci_dbg(xhci, "resume SS port %d\n", port_id);
1302 temp = xhci_port_state_to_neutral(temp);
1303 temp &= ~PORT_PLS_MASK;
1304 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -08001305 xhci_writel(xhci, temp, port_array[faked_port_index]);
Sarah Sharp52336302010-12-16 10:49:09 -08001306 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1307 faked_port_index);
Andiry Xu56192532010-10-14 07:23:00 -07001308 if (!slot_id) {
1309 xhci_dbg(xhci, "slot_id is zero\n");
1310 goto cleanup;
1311 }
1312 xhci_ring_device(xhci, slot_id);
1313 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
1314 /* Clear PORT_PLC */
Sarah Sharp5308a912010-12-01 11:34:59 -08001315 temp = xhci_readl(xhci, port_array[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001316 temp = xhci_port_state_to_neutral(temp);
1317 temp |= PORT_PLC;
Sarah Sharp5308a912010-12-01 11:34:59 -08001318 xhci_writel(xhci, temp, port_array[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001319 } else {
1320 xhci_dbg(xhci, "resume HS port %d\n", port_id);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001321 bus_state->resume_done[faked_port_index] = jiffies +
Andiry Xu56192532010-10-14 07:23:00 -07001322 msecs_to_jiffies(20);
1323 mod_timer(&hcd->rh_timer,
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001324 bus_state->resume_done[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001325 /* Do the rest in GetPortStatus */
1326 }
1327 }
1328
1329cleanup:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001330 /* Update event ring dequeue pointer before dropping the lock */
1331 inc_deq(xhci, xhci->event_ring, true);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001332
1333 spin_unlock(&xhci->lock);
1334 /* Pass this up to the core */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001335 usb_hcd_poll_rh_status(hcd);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001336 spin_lock(&xhci->lock);
1337}
1338
1339/*
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001340 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1341 * at end_trb, which may be in another segment. If the suspect DMA address is a
1342 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1343 * returns 0.
1344 */
Sarah Sharp6648f292009-11-09 13:35:23 -08001345struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001346 union xhci_trb *start_trb,
1347 union xhci_trb *end_trb,
1348 dma_addr_t suspect_dma)
1349{
1350 dma_addr_t start_dma;
1351 dma_addr_t end_seg_dma;
1352 dma_addr_t end_trb_dma;
1353 struct xhci_segment *cur_seg;
1354
Sarah Sharp23e3be12009-04-29 19:05:20 -07001355 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001356 cur_seg = start_seg;
1357
1358 do {
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001359 if (start_dma == 0)
Randy Dunlap326b4812010-04-19 08:53:50 -07001360 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -07001361 /* We may get an event for a Link TRB in the middle of a TD */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001362 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001363 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001364 /* If the end TRB isn't in this segment, this is set to 0 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001365 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001366
1367 if (end_trb_dma > 0) {
1368 /* The end TRB is in this segment, so suspect should be here */
1369 if (start_dma <= end_trb_dma) {
1370 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1371 return cur_seg;
1372 } else {
1373 /* Case for one segment with
1374 * a TD wrapped around to the top
1375 */
1376 if ((suspect_dma >= start_dma &&
1377 suspect_dma <= end_seg_dma) ||
1378 (suspect_dma >= cur_seg->dma &&
1379 suspect_dma <= end_trb_dma))
1380 return cur_seg;
1381 }
Randy Dunlap326b4812010-04-19 08:53:50 -07001382 return NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001383 } else {
1384 /* Might still be somewhere in this segment */
1385 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1386 return cur_seg;
1387 }
1388 cur_seg = cur_seg->next;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001389 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001390 } while (cur_seg != start_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001391
Randy Dunlap326b4812010-04-19 08:53:50 -07001392 return NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001393}
1394
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001395static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1396 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001397 unsigned int stream_id,
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001398 struct xhci_td *td, union xhci_trb *event_trb)
1399{
1400 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
1401 ep->ep_state |= EP_HALTED;
1402 ep->stopped_td = td;
1403 ep->stopped_trb = event_trb;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001404 ep->stopped_stream = stream_id;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001405
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001406 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1407 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
Sarah Sharp1624ae12010-05-06 13:40:08 -07001408
1409 ep->stopped_td = NULL;
1410 ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001411 ep->stopped_stream = 0;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001412
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001413 xhci_ring_cmd_db(xhci);
1414}
1415
1416/* Check if an error has halted the endpoint ring. The class driver will
1417 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1418 * However, a babble and other errors also halt the endpoint ring, and the class
1419 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1420 * Ring Dequeue Pointer command manually.
1421 */
1422static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1423 struct xhci_ep_ctx *ep_ctx,
1424 unsigned int trb_comp_code)
1425{
1426 /* TRB completion codes that may require a manual halt cleanup */
1427 if (trb_comp_code == COMP_TX_ERR ||
1428 trb_comp_code == COMP_BABBLE ||
1429 trb_comp_code == COMP_SPLIT_ERR)
1430 /* The 0.96 spec says a babbling control endpoint
1431 * is not halted. The 0.96 spec says it is. Some HW
1432 * claims to be 0.95 compliant, but it halts the control
1433 * endpoint anyway. Check if a babble halted the
1434 * endpoint.
1435 */
1436 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED)
1437 return 1;
1438
1439 return 0;
1440}
1441
Sarah Sharpb45b5062009-12-09 15:59:06 -08001442int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1443{
1444 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1445 /* Vendor defined "informational" completion code,
1446 * treat as not-an-error.
1447 */
1448 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1449 trb_comp_code);
1450 xhci_dbg(xhci, "Treating code as success.\n");
1451 return 1;
1452 }
1453 return 0;
1454}
1455
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001456/*
Andiry Xu4422da62010-07-22 15:22:55 -07001457 * Finish the td processing, remove the td from td list;
1458 * Return 1 if the urb can be given back.
1459 */
1460static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1461 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1462 struct xhci_virt_ep *ep, int *status, bool skip)
1463{
1464 struct xhci_virt_device *xdev;
1465 struct xhci_ring *ep_ring;
1466 unsigned int slot_id;
1467 int ep_index;
1468 struct urb *urb = NULL;
1469 struct xhci_ep_ctx *ep_ctx;
1470 int ret = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001471 struct urb_priv *urb_priv;
Andiry Xu4422da62010-07-22 15:22:55 -07001472 u32 trb_comp_code;
1473
1474 slot_id = TRB_TO_SLOT_ID(event->flags);
1475 xdev = xhci->devs[slot_id];
1476 ep_index = TRB_TO_EP_ID(event->flags) - 1;
1477 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1478 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
1479 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1480
1481 if (skip)
1482 goto td_cleanup;
1483
1484 if (trb_comp_code == COMP_STOP_INVAL ||
1485 trb_comp_code == COMP_STOP) {
1486 /* The Endpoint Stop Command completion will take care of any
1487 * stopped TDs. A stopped TD may be restarted, so don't update
1488 * the ring dequeue pointer or take this TD off any lists yet.
1489 */
1490 ep->stopped_td = td;
1491 ep->stopped_trb = event_trb;
1492 return 0;
1493 } else {
1494 if (trb_comp_code == COMP_STALL) {
1495 /* The transfer is completed from the driver's
1496 * perspective, but we need to issue a set dequeue
1497 * command for this stalled endpoint to move the dequeue
1498 * pointer past the TD. We can't do that here because
1499 * the halt condition must be cleared first. Let the
1500 * USB class driver clear the stall later.
1501 */
1502 ep->stopped_td = td;
1503 ep->stopped_trb = event_trb;
1504 ep->stopped_stream = ep_ring->stream_id;
1505 } else if (xhci_requires_manual_halt_cleanup(xhci,
1506 ep_ctx, trb_comp_code)) {
1507 /* Other types of errors halt the endpoint, but the
1508 * class driver doesn't call usb_reset_endpoint() unless
1509 * the error is -EPIPE. Clear the halted status in the
1510 * xHCI hardware manually.
1511 */
1512 xhci_cleanup_halted_endpoint(xhci,
1513 slot_id, ep_index, ep_ring->stream_id,
1514 td, event_trb);
1515 } else {
1516 /* Update ring dequeue pointer */
1517 while (ep_ring->dequeue != td->last_trb)
1518 inc_deq(xhci, ep_ring, false);
1519 inc_deq(xhci, ep_ring, false);
1520 }
1521
1522td_cleanup:
1523 /* Clean up the endpoint's TD list */
1524 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001525 urb_priv = urb->hcpriv;
Andiry Xu4422da62010-07-22 15:22:55 -07001526
1527 /* Do one last check of the actual transfer length.
1528 * If the host controller said we transferred more data than
1529 * the buffer length, urb->actual_length will be a very big
1530 * number (since it's unsigned). Play it safe and say we didn't
1531 * transfer anything.
1532 */
1533 if (urb->actual_length > urb->transfer_buffer_length) {
1534 xhci_warn(xhci, "URB transfer length is wrong, "
1535 "xHC issue? req. len = %u, "
1536 "act. len = %u\n",
1537 urb->transfer_buffer_length,
1538 urb->actual_length);
1539 urb->actual_length = 0;
1540 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1541 *status = -EREMOTEIO;
1542 else
1543 *status = 0;
1544 }
1545 list_del(&td->td_list);
1546 /* Was this TD slated to be cancelled but completed anyway? */
1547 if (!list_empty(&td->cancelled_td_list))
1548 list_del(&td->cancelled_td_list);
1549
Andiry Xu8e51adc2010-07-22 15:23:31 -07001550 urb_priv->td_cnt++;
1551 /* Giveback the urb when all the tds are completed */
1552 if (urb_priv->td_cnt == urb_priv->length)
1553 ret = 1;
Andiry Xu4422da62010-07-22 15:22:55 -07001554 }
1555
1556 return ret;
1557}
1558
1559/*
Andiry Xu8af56be2010-07-22 15:23:03 -07001560 * Process control tds, update urb status and actual_length.
1561 */
1562static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1563 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1564 struct xhci_virt_ep *ep, int *status)
1565{
1566 struct xhci_virt_device *xdev;
1567 struct xhci_ring *ep_ring;
1568 unsigned int slot_id;
1569 int ep_index;
1570 struct xhci_ep_ctx *ep_ctx;
1571 u32 trb_comp_code;
1572
1573 slot_id = TRB_TO_SLOT_ID(event->flags);
1574 xdev = xhci->devs[slot_id];
1575 ep_index = TRB_TO_EP_ID(event->flags) - 1;
1576 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1577 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
1578 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1579
1580 xhci_debug_trb(xhci, xhci->event_ring->dequeue);
1581 switch (trb_comp_code) {
1582 case COMP_SUCCESS:
1583 if (event_trb == ep_ring->dequeue) {
1584 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1585 "without IOC set??\n");
1586 *status = -ESHUTDOWN;
1587 } else if (event_trb != td->last_trb) {
1588 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1589 "without IOC set??\n");
1590 *status = -ESHUTDOWN;
1591 } else {
1592 xhci_dbg(xhci, "Successful control transfer!\n");
1593 *status = 0;
1594 }
1595 break;
1596 case COMP_SHORT_TX:
1597 xhci_warn(xhci, "WARN: short transfer on control ep\n");
1598 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1599 *status = -EREMOTEIO;
1600 else
1601 *status = 0;
1602 break;
1603 default:
1604 if (!xhci_requires_manual_halt_cleanup(xhci,
1605 ep_ctx, trb_comp_code))
1606 break;
1607 xhci_dbg(xhci, "TRB error code %u, "
1608 "halted endpoint index = %u\n",
1609 trb_comp_code, ep_index);
1610 /* else fall through */
1611 case COMP_STALL:
1612 /* Did we transfer part of the data (middle) phase? */
1613 if (event_trb != ep_ring->dequeue &&
1614 event_trb != td->last_trb)
1615 td->urb->actual_length =
1616 td->urb->transfer_buffer_length
1617 - TRB_LEN(event->transfer_len);
1618 else
1619 td->urb->actual_length = 0;
1620
1621 xhci_cleanup_halted_endpoint(xhci,
1622 slot_id, ep_index, 0, td, event_trb);
1623 return finish_td(xhci, td, event_trb, event, ep, status, true);
1624 }
1625 /*
1626 * Did we transfer any data, despite the errors that might have
1627 * happened? I.e. did we get past the setup stage?
1628 */
1629 if (event_trb != ep_ring->dequeue) {
1630 /* The event was for the status stage */
1631 if (event_trb == td->last_trb) {
1632 if (td->urb->actual_length != 0) {
1633 /* Don't overwrite a previously set error code
1634 */
1635 if ((*status == -EINPROGRESS || *status == 0) &&
1636 (td->urb->transfer_flags
1637 & URB_SHORT_NOT_OK))
1638 /* Did we already see a short data
1639 * stage? */
1640 *status = -EREMOTEIO;
1641 } else {
1642 td->urb->actual_length =
1643 td->urb->transfer_buffer_length;
1644 }
1645 } else {
1646 /* Maybe the event was for the data stage? */
1647 if (trb_comp_code != COMP_STOP_INVAL) {
1648 /* We didn't stop on a link TRB in the middle */
1649 td->urb->actual_length =
1650 td->urb->transfer_buffer_length -
1651 TRB_LEN(event->transfer_len);
1652 xhci_dbg(xhci, "Waiting for status "
1653 "stage event\n");
1654 return 0;
1655 }
1656 }
1657 }
1658
1659 return finish_td(xhci, td, event_trb, event, ep, status, false);
1660}
1661
1662/*
Andiry Xu04e51902010-07-22 15:23:39 -07001663 * Process isochronous tds, update urb packet status and actual_length.
1664 */
1665static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1666 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1667 struct xhci_virt_ep *ep, int *status)
1668{
1669 struct xhci_ring *ep_ring;
1670 struct urb_priv *urb_priv;
1671 int idx;
1672 int len = 0;
1673 int skip_td = 0;
1674 union xhci_trb *cur_trb;
1675 struct xhci_segment *cur_seg;
1676 u32 trb_comp_code;
1677
1678 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1679 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1680 urb_priv = td->urb->hcpriv;
1681 idx = urb_priv->td_cnt;
1682
1683 if (ep->skip) {
1684 /* The transfer is partly done */
1685 *status = -EXDEV;
1686 td->urb->iso_frame_desc[idx].status = -EXDEV;
1687 } else {
1688 /* handle completion code */
1689 switch (trb_comp_code) {
1690 case COMP_SUCCESS:
1691 td->urb->iso_frame_desc[idx].status = 0;
1692 xhci_dbg(xhci, "Successful isoc transfer!\n");
1693 break;
1694 case COMP_SHORT_TX:
1695 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1696 td->urb->iso_frame_desc[idx].status =
1697 -EREMOTEIO;
1698 else
1699 td->urb->iso_frame_desc[idx].status = 0;
1700 break;
1701 case COMP_BW_OVER:
1702 td->urb->iso_frame_desc[idx].status = -ECOMM;
1703 skip_td = 1;
1704 break;
1705 case COMP_BUFF_OVER:
1706 case COMP_BABBLE:
1707 td->urb->iso_frame_desc[idx].status = -EOVERFLOW;
1708 skip_td = 1;
1709 break;
1710 case COMP_STALL:
1711 td->urb->iso_frame_desc[idx].status = -EPROTO;
1712 skip_td = 1;
1713 break;
1714 case COMP_STOP:
1715 case COMP_STOP_INVAL:
1716 break;
1717 default:
1718 td->urb->iso_frame_desc[idx].status = -1;
1719 break;
1720 }
1721 }
1722
1723 /* calc actual length */
1724 if (ep->skip) {
1725 td->urb->iso_frame_desc[idx].actual_length = 0;
Andiry Xu14184f92010-08-09 13:56:15 -07001726 /* Update ring dequeue pointer */
1727 while (ep_ring->dequeue != td->last_trb)
1728 inc_deq(xhci, ep_ring, false);
1729 inc_deq(xhci, ep_ring, false);
Andiry Xu04e51902010-07-22 15:23:39 -07001730 return finish_td(xhci, td, event_trb, event, ep, status, true);
1731 }
1732
1733 if (trb_comp_code == COMP_SUCCESS || skip_td == 1) {
1734 td->urb->iso_frame_desc[idx].actual_length =
1735 td->urb->iso_frame_desc[idx].length;
1736 td->urb->actual_length +=
1737 td->urb->iso_frame_desc[idx].length;
1738 } else {
1739 for (cur_trb = ep_ring->dequeue,
1740 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
1741 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
1742 if ((cur_trb->generic.field[3] &
1743 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) &&
1744 (cur_trb->generic.field[3] &
1745 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK))
1746 len +=
1747 TRB_LEN(cur_trb->generic.field[2]);
1748 }
1749 len += TRB_LEN(cur_trb->generic.field[2]) -
1750 TRB_LEN(event->transfer_len);
1751
1752 if (trb_comp_code != COMP_STOP_INVAL) {
1753 td->urb->iso_frame_desc[idx].actual_length = len;
1754 td->urb->actual_length += len;
1755 }
1756 }
1757
1758 if ((idx == urb_priv->length - 1) && *status == -EINPROGRESS)
1759 *status = 0;
1760
1761 return finish_td(xhci, td, event_trb, event, ep, status, false);
1762}
1763
1764/*
Andiry Xu22405ed2010-07-22 15:23:08 -07001765 * Process bulk and interrupt tds, update urb status and actual_length.
1766 */
1767static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
1768 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1769 struct xhci_virt_ep *ep, int *status)
1770{
1771 struct xhci_ring *ep_ring;
1772 union xhci_trb *cur_trb;
1773 struct xhci_segment *cur_seg;
1774 u32 trb_comp_code;
1775
1776 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
1777 trb_comp_code = GET_COMP_CODE(event->transfer_len);
1778
1779 switch (trb_comp_code) {
1780 case COMP_SUCCESS:
1781 /* Double check that the HW transferred everything. */
1782 if (event_trb != td->last_trb) {
1783 xhci_warn(xhci, "WARN Successful completion "
1784 "on short TX\n");
1785 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1786 *status = -EREMOTEIO;
1787 else
1788 *status = 0;
1789 } else {
1790 if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
1791 xhci_dbg(xhci, "Successful bulk "
1792 "transfer!\n");
1793 else
1794 xhci_dbg(xhci, "Successful interrupt "
1795 "transfer!\n");
1796 *status = 0;
1797 }
1798 break;
1799 case COMP_SHORT_TX:
1800 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1801 *status = -EREMOTEIO;
1802 else
1803 *status = 0;
1804 break;
1805 default:
1806 /* Others already handled above */
1807 break;
1808 }
Andiry Xuf2c565e2010-12-20 17:12:24 +08001809 xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
Andiry Xu22405ed2010-07-22 15:23:08 -07001810 "%d bytes untransferred\n",
1811 td->urb->ep->desc.bEndpointAddress,
1812 td->urb->transfer_buffer_length,
1813 TRB_LEN(event->transfer_len));
1814 /* Fast path - was this the last TRB in the TD for this URB? */
1815 if (event_trb == td->last_trb) {
1816 if (TRB_LEN(event->transfer_len) != 0) {
1817 td->urb->actual_length =
1818 td->urb->transfer_buffer_length -
1819 TRB_LEN(event->transfer_len);
1820 if (td->urb->transfer_buffer_length <
1821 td->urb->actual_length) {
1822 xhci_warn(xhci, "HC gave bad length "
1823 "of %d bytes left\n",
1824 TRB_LEN(event->transfer_len));
1825 td->urb->actual_length = 0;
1826 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1827 *status = -EREMOTEIO;
1828 else
1829 *status = 0;
1830 }
1831 /* Don't overwrite a previously set error code */
1832 if (*status == -EINPROGRESS) {
1833 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1834 *status = -EREMOTEIO;
1835 else
1836 *status = 0;
1837 }
1838 } else {
1839 td->urb->actual_length =
1840 td->urb->transfer_buffer_length;
1841 /* Ignore a short packet completion if the
1842 * untransferred length was zero.
1843 */
1844 if (*status == -EREMOTEIO)
1845 *status = 0;
1846 }
1847 } else {
1848 /* Slow path - walk the list, starting from the dequeue
1849 * pointer, to get the actual length transferred.
1850 */
1851 td->urb->actual_length = 0;
1852 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
1853 cur_trb != event_trb;
1854 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
1855 if ((cur_trb->generic.field[3] &
1856 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) &&
1857 (cur_trb->generic.field[3] &
1858 TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK))
1859 td->urb->actual_length +=
1860 TRB_LEN(cur_trb->generic.field[2]);
1861 }
1862 /* If the ring didn't stop on a Link or No-op TRB, add
1863 * in the actual bytes transferred from the Normal TRB
1864 */
1865 if (trb_comp_code != COMP_STOP_INVAL)
1866 td->urb->actual_length +=
1867 TRB_LEN(cur_trb->generic.field[2]) -
1868 TRB_LEN(event->transfer_len);
1869 }
1870
1871 return finish_td(xhci, td, event_trb, event, ep, status, false);
1872}
1873
1874/*
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001875 * If this function returns an error condition, it means it got a Transfer
1876 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
1877 * At this point, the host controller is probably hosed and should be reset.
1878 */
1879static int handle_tx_event(struct xhci_hcd *xhci,
1880 struct xhci_transfer_event *event)
1881{
1882 struct xhci_virt_device *xdev;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001883 struct xhci_virt_ep *ep;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001884 struct xhci_ring *ep_ring;
Sarah Sharp82d10092009-08-07 14:04:52 -07001885 unsigned int slot_id;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001886 int ep_index;
Randy Dunlap326b4812010-04-19 08:53:50 -07001887 struct xhci_td *td = NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001888 dma_addr_t event_dma;
1889 struct xhci_segment *event_seg;
1890 union xhci_trb *event_trb;
Randy Dunlap326b4812010-04-19 08:53:50 -07001891 struct urb *urb = NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001892 int status = -EINPROGRESS;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001893 struct urb_priv *urb_priv;
John Yound115b042009-07-27 12:05:15 -07001894 struct xhci_ep_ctx *ep_ctx;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07001895 u32 trb_comp_code;
Andiry Xu4422da62010-07-22 15:22:55 -07001896 int ret = 0;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001897
Sarah Sharp82d10092009-08-07 14:04:52 -07001898 slot_id = TRB_TO_SLOT_ID(event->flags);
1899 xdev = xhci->devs[slot_id];
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001900 if (!xdev) {
1901 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
1902 return -ENODEV;
1903 }
1904
1905 /* Endpoint ID is 1 based, our index is zero based */
1906 ep_index = TRB_TO_EP_ID(event->flags) - 1;
Sarah Sharp66e49d82009-07-27 12:03:46 -07001907 xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001908 ep = &xdev->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001909 ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer);
John Yound115b042009-07-27 12:05:15 -07001910 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07001911 if (!ep_ring ||
1912 (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001913 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
1914 "or incorrect stream ring\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001915 return -ENODEV;
1916 }
1917
Sarah Sharp8e595a52009-07-27 12:03:31 -07001918 event_dma = event->buffer;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07001919 trb_comp_code = GET_COMP_CODE(event->transfer_len);
Andiry Xu986a92d2010-07-22 15:23:20 -07001920 /* Look for common error cases */
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07001921 switch (trb_comp_code) {
Sarah Sharpb10de142009-04-27 19:58:50 -07001922 /* Skip codes that require special handling depending on
1923 * transfer type
1924 */
1925 case COMP_SUCCESS:
1926 case COMP_SHORT_TX:
1927 break;
Sarah Sharpae636742009-04-29 19:02:31 -07001928 case COMP_STOP:
1929 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
1930 break;
1931 case COMP_STOP_INVAL:
1932 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
1933 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07001934 case COMP_STALL:
1935 xhci_warn(xhci, "WARN: Stalled endpoint\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001936 ep->ep_state |= EP_HALTED;
Sarah Sharpb10de142009-04-27 19:58:50 -07001937 status = -EPIPE;
1938 break;
1939 case COMP_TRB_ERR:
1940 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
1941 status = -EILSEQ;
1942 break;
Sarah Sharpec74e402009-11-11 10:28:36 -08001943 case COMP_SPLIT_ERR:
Sarah Sharpb10de142009-04-27 19:58:50 -07001944 case COMP_TX_ERR:
1945 xhci_warn(xhci, "WARN: transfer error on endpoint\n");
1946 status = -EPROTO;
1947 break;
Sarah Sharp4a731432009-07-27 12:04:32 -07001948 case COMP_BABBLE:
1949 xhci_warn(xhci, "WARN: babble error on endpoint\n");
1950 status = -EOVERFLOW;
1951 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07001952 case COMP_DB_ERR:
1953 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
1954 status = -ENOSR;
1955 break;
Andiry Xu986a92d2010-07-22 15:23:20 -07001956 case COMP_BW_OVER:
1957 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
1958 break;
1959 case COMP_BUFF_OVER:
1960 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
1961 break;
1962 case COMP_UNDERRUN:
1963 /*
1964 * When the Isoch ring is empty, the xHC will generate
1965 * a Ring Overrun Event for IN Isoch endpoint or Ring
1966 * Underrun Event for OUT Isoch endpoint.
1967 */
1968 xhci_dbg(xhci, "underrun event on endpoint\n");
1969 if (!list_empty(&ep_ring->td_list))
1970 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
1971 "still with TDs queued?\n",
1972 TRB_TO_SLOT_ID(event->flags), ep_index);
1973 goto cleanup;
1974 case COMP_OVERRUN:
1975 xhci_dbg(xhci, "overrun event on endpoint\n");
1976 if (!list_empty(&ep_ring->td_list))
1977 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
1978 "still with TDs queued?\n",
1979 TRB_TO_SLOT_ID(event->flags), ep_index);
1980 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07001981 case COMP_MISSED_INT:
1982 /*
1983 * When encounter missed service error, one or more isoc tds
1984 * may be missed by xHC.
1985 * Set skip flag of the ep_ring; Complete the missed tds as
1986 * short transfer when process the ep_ring next time.
1987 */
1988 ep->skip = true;
1989 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
1990 goto cleanup;
Sarah Sharpb10de142009-04-27 19:58:50 -07001991 default:
Sarah Sharpb45b5062009-12-09 15:59:06 -08001992 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
Sarah Sharp5ad6a522009-11-11 10:28:40 -08001993 status = 0;
1994 break;
1995 }
Andiry Xu986a92d2010-07-22 15:23:20 -07001996 xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
1997 "busted\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07001998 goto cleanup;
1999 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002000
Andiry Xud18240d2010-07-22 15:23:25 -07002001 do {
2002 /* This TRB should be in the TD at the head of this ring's
2003 * TD list.
2004 */
2005 if (list_empty(&ep_ring->td_list)) {
2006 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
2007 "with no TDs queued?\n",
2008 TRB_TO_SLOT_ID(event->flags), ep_index);
2009 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
2010 (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
2011 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2012 if (ep->skip) {
2013 ep->skip = false;
2014 xhci_dbg(xhci, "td_list is empty while skip "
2015 "flag set. Clear skip flag.\n");
2016 }
2017 ret = 0;
2018 goto cleanup;
2019 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002020
Andiry Xud18240d2010-07-22 15:23:25 -07002021 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
2022 /* Is this a TRB in the currently executing TD? */
2023 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
2024 td->last_trb, event_dma);
2025 if (event_seg && ep->skip) {
2026 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2027 ep->skip = false;
2028 }
2029 if (!event_seg &&
2030 (!ep->skip || !usb_endpoint_xfer_isoc(&td->urb->ep->desc))) {
2031 /* HC is busted, give up! */
2032 xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not "
2033 "part of current TD\n");
2034 return -ESHUTDOWN;
2035 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002036
Andiry Xud18240d2010-07-22 15:23:25 -07002037 if (event_seg) {
2038 event_trb = &event_seg->trbs[(event_dma -
2039 event_seg->dma) / sizeof(*event_trb)];
2040 /*
2041 * No-op TRB should not trigger interrupts.
2042 * If event_trb is a no-op TRB, it means the
2043 * corresponding TD has been cancelled. Just ignore
2044 * the TD.
2045 */
2046 if ((event_trb->generic.field[3] & TRB_TYPE_BITMASK)
2047 == TRB_TYPE(TRB_TR_NOOP)) {
2048 xhci_dbg(xhci, "event_trb is a no-op TRB. "
2049 "Skip it\n");
2050 goto cleanup;
2051 }
2052 }
2053
2054 /* Now update the urb's actual_length and give back to
2055 * the core
2056 */
2057 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2058 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
2059 &status);
Andiry Xu04e51902010-07-22 15:23:39 -07002060 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2061 ret = process_isoc_td(xhci, td, event_trb, event, ep,
2062 &status);
Andiry Xud18240d2010-07-22 15:23:25 -07002063 else
2064 ret = process_bulk_intr_td(xhci, td, event_trb, event,
2065 ep, &status);
Andiry Xu4422da62010-07-22 15:22:55 -07002066
2067cleanup:
Andiry Xud18240d2010-07-22 15:23:25 -07002068 /*
2069 * Do not update event ring dequeue pointer if ep->skip is set.
2070 * Will roll back to continue process missed tds.
Sarah Sharp82d10092009-08-07 14:04:52 -07002071 */
Andiry Xud18240d2010-07-22 15:23:25 -07002072 if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
2073 inc_deq(xhci, xhci->event_ring, true);
Andiry Xud18240d2010-07-22 15:23:25 -07002074 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002075
Andiry Xud18240d2010-07-22 15:23:25 -07002076 if (ret) {
2077 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002078 urb_priv = urb->hcpriv;
Andiry Xud18240d2010-07-22 15:23:25 -07002079 /* Leave the TD around for the reset endpoint function
2080 * to use(but only if it's not a control endpoint,
2081 * since we already queued the Set TR dequeue pointer
2082 * command for stalled control endpoints).
2083 */
2084 if (usb_endpoint_xfer_control(&urb->ep->desc) ||
2085 (trb_comp_code != COMP_STALL &&
2086 trb_comp_code != COMP_BABBLE))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002087 xhci_urb_free_priv(xhci, urb_priv);
Andiry Xud18240d2010-07-22 15:23:25 -07002088
Sarah Sharp214f76f2010-10-26 11:22:02 -07002089 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
Andiry Xud18240d2010-07-22 15:23:25 -07002090 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
2091 "status = %d\n",
2092 urb, urb->actual_length, status);
2093 spin_unlock(&xhci->lock);
Sarah Sharp214f76f2010-10-26 11:22:02 -07002094 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
Andiry Xud18240d2010-07-22 15:23:25 -07002095 spin_lock(&xhci->lock);
2096 }
2097
2098 /*
2099 * If ep->skip is set, it means there are missed tds on the
2100 * endpoint ring need to take care of.
2101 * Process them as short transfer until reach the td pointed by
2102 * the event.
2103 */
2104 } while (ep->skip && trb_comp_code != COMP_MISSED_INT);
2105
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002106 return 0;
2107}
2108
2109/*
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002110 * This function handles all OS-owned events on the event ring. It may drop
2111 * xhci->lock between event processing (e.g. to pass up port status changes).
2112 */
Sarah Sharpd6d98a42010-07-29 22:12:46 -07002113static void xhci_handle_event(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002114{
2115 union xhci_trb *event;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002116 int update_ptrs = 1;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002117 int ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002118
Sarah Sharp66e49d82009-07-27 12:03:46 -07002119 xhci_dbg(xhci, "In %s\n", __func__);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002120 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2121 xhci->error_bitmask |= 1 << 1;
2122 return;
2123 }
2124
2125 event = xhci->event_ring->dequeue;
2126 /* Does the HC or OS own the TRB? */
2127 if ((event->event_cmd.flags & TRB_CYCLE) !=
2128 xhci->event_ring->cycle_state) {
2129 xhci->error_bitmask |= 1 << 2;
2130 return;
2131 }
Sarah Sharp66e49d82009-07-27 12:03:46 -07002132 xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002133
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002134 /* FIXME: Handle more event types. */
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002135 switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
2136 case TRB_TYPE(TRB_COMPLETION):
Sarah Sharp66e49d82009-07-27 12:03:46 -07002137 xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002138 handle_cmd_completion(xhci, &event->event_cmd);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002139 xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002140 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002141 case TRB_TYPE(TRB_PORT_STATUS):
Sarah Sharp66e49d82009-07-27 12:03:46 -07002142 xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002143 handle_port_status(xhci, event);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002144 xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002145 update_ptrs = 0;
2146 break;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002147 case TRB_TYPE(TRB_TRANSFER):
Sarah Sharp66e49d82009-07-27 12:03:46 -07002148 xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002149 ret = handle_tx_event(xhci, &event->trans_event);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002150 xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002151 if (ret < 0)
2152 xhci->error_bitmask |= 1 << 9;
2153 else
2154 update_ptrs = 0;
2155 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002156 default:
Sarah Sharp02386342010-05-24 13:25:28 -07002157 if ((event->event_cmd.flags & TRB_TYPE_BITMASK) >= TRB_TYPE(48))
2158 handle_vendor_event(xhci, event);
2159 else
2160 xhci->error_bitmask |= 1 << 3;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002161 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002162 /* Any of the above functions may drop and re-acquire the lock, so check
2163 * to make sure a watchdog timer didn't mark the host as non-responsive.
2164 */
2165 if (xhci->xhc_state & XHCI_STATE_DYING) {
2166 xhci_dbg(xhci, "xHCI host dying, returning from "
2167 "event handler.\n");
2168 return;
2169 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002170
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002171 if (update_ptrs)
2172 /* Update SW event ring dequeue pointer */
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002173 inc_deq(xhci, xhci->event_ring, true);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002174
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002175 /* Are there more items on the event ring? */
Stephen Rothwellb7258a42009-04-29 19:02:47 -07002176 xhci_handle_event(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002177}
Sarah Sharp9032cd52010-07-29 22:12:29 -07002178
2179/*
2180 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2181 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2182 * indicators of an event TRB error, but we check the status *first* to be safe.
2183 */
2184irqreturn_t xhci_irq(struct usb_hcd *hcd)
2185{
2186 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002187 u32 status;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002188 union xhci_trb *trb;
Sarah Sharpbda53142010-07-29 22:12:38 -07002189 u64 temp_64;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002190 union xhci_trb *event_ring_deq;
2191 dma_addr_t deq;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002192
2193 spin_lock(&xhci->lock);
2194 trb = xhci->event_ring->dequeue;
2195 /* Check if the xHC generated the interrupt, or the irq is shared */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002196 status = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002197 if (status == 0xffffffff)
Sarah Sharp9032cd52010-07-29 22:12:29 -07002198 goto hw_died;
2199
Sarah Sharpc21599a2010-07-29 22:13:00 -07002200 if (!(status & STS_EINT)) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002201 spin_unlock(&xhci->lock);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002202 return IRQ_NONE;
2203 }
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002204 xhci_dbg(xhci, "op reg status = %08x\n", status);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002205 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
2206 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
2207 (unsigned long long)
2208 xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
2209 lower_32_bits(trb->link.segment_ptr),
2210 upper_32_bits(trb->link.segment_ptr),
2211 (unsigned int) trb->link.intr_target,
2212 (unsigned int) trb->link.control);
2213
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002214 if (status & STS_FATAL) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002215 xhci_warn(xhci, "WARNING: Host System Error\n");
2216 xhci_halt(xhci);
2217hw_died:
Sarah Sharp9032cd52010-07-29 22:12:29 -07002218 spin_unlock(&xhci->lock);
2219 return -ESHUTDOWN;
2220 }
2221
Sarah Sharpbda53142010-07-29 22:12:38 -07002222 /*
2223 * Clear the op reg interrupt status first,
2224 * so we can receive interrupts from other MSI-X interrupters.
2225 * Write 1 to clear the interrupt status.
2226 */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002227 status |= STS_EINT;
2228 xhci_writel(xhci, status, &xhci->op_regs->status);
Sarah Sharpbda53142010-07-29 22:12:38 -07002229 /* FIXME when MSI-X is supported and there are multiple vectors */
2230 /* Clear the MSI-X event interrupt status */
2231
Sarah Sharpc21599a2010-07-29 22:13:00 -07002232 if (hcd->irq != -1) {
2233 u32 irq_pending;
2234 /* Acknowledge the PCI interrupt */
2235 irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
2236 irq_pending |= 0x3;
2237 xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
2238 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002239
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002240 if (xhci->xhc_state & XHCI_STATE_DYING) {
Sarah Sharpbda53142010-07-29 22:12:38 -07002241 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2242 "Shouldn't IRQs be disabled?\n");
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002243 /* Clear the event handler busy flag (RW1C);
2244 * the event ring should be empty.
Sarah Sharpbda53142010-07-29 22:12:38 -07002245 */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002246 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2247 xhci_write_64(xhci, temp_64 | ERST_EHB,
2248 &xhci->ir_set->erst_dequeue);
2249 spin_unlock(&xhci->lock);
2250
2251 return IRQ_HANDLED;
2252 }
2253
2254 event_ring_deq = xhci->event_ring->dequeue;
2255 /* FIXME this should be a delayed service routine
2256 * that clears the EHB.
2257 */
2258 xhci_handle_event(xhci);
2259
2260 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2261 /* If necessary, update the HW's version of the event ring deq ptr. */
2262 if (event_ring_deq != xhci->event_ring->dequeue) {
2263 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2264 xhci->event_ring->dequeue);
2265 if (deq == 0)
2266 xhci_warn(xhci, "WARN something wrong with SW event "
2267 "ring dequeue ptr.\n");
2268 /* Update HC event ring dequeue pointer */
2269 temp_64 &= ERST_PTR_MASK;
2270 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2271 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002272
2273 /* Clear the event handler busy flag (RW1C); event ring is empty. */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002274 temp_64 |= ERST_EHB;
2275 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2276
Sarah Sharp9032cd52010-07-29 22:12:29 -07002277 spin_unlock(&xhci->lock);
2278
2279 return IRQ_HANDLED;
2280}
2281
2282irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2283{
2284 irqreturn_t ret;
Sarah Sharpb3209372011-03-07 11:24:07 -08002285 struct xhci_hcd *xhci;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002286
Sarah Sharpb3209372011-03-07 11:24:07 -08002287 xhci = hcd_to_xhci(hcd);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002288 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -08002289 if (xhci->shared_hcd)
2290 set_bit(HCD_FLAG_SAW_IRQ, &xhci->shared_hcd->flags);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002291
2292 ret = xhci_irq(hcd);
2293
2294 return ret;
2295}
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002296
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002297/**** Endpoint Ring Operations ****/
2298
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002299/*
2300 * Generic function for queueing a TRB on a ring.
2301 * The caller must have checked to make sure there's room on the ring.
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002302 *
2303 * @more_trbs_coming: Will you enqueue more TRBs before calling
2304 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002305 */
2306static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002307 bool consumer, bool more_trbs_coming,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002308 u32 field1, u32 field2, u32 field3, u32 field4)
2309{
2310 struct xhci_generic_trb *trb;
2311
2312 trb = &ring->enqueue->generic;
2313 trb->field[0] = field1;
2314 trb->field[1] = field2;
2315 trb->field[2] = field3;
2316 trb->field[3] = field4;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002317 inc_enq(xhci, ring, consumer, more_trbs_coming);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002318}
2319
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002320/*
2321 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2322 * FIXME allocate segments if the ring is full.
2323 */
2324static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
2325 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
2326{
2327 /* Make sure the endpoint has been added to xHC schedule */
2328 xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
2329 switch (ep_state) {
2330 case EP_STATE_DISABLED:
2331 /*
2332 * USB core changed config/interfaces without notifying us,
2333 * or hardware is reporting the wrong state.
2334 */
2335 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2336 return -ENOENT;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002337 case EP_STATE_ERROR:
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002338 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002339 /* FIXME event handling code for error needs to clear it */
2340 /* XXX not sure if this should be -ENOENT or not */
2341 return -EINVAL;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002342 case EP_STATE_HALTED:
2343 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002344 case EP_STATE_STOPPED:
2345 case EP_STATE_RUNNING:
2346 break;
2347 default:
2348 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2349 /*
2350 * FIXME issue Configure Endpoint command to try to get the HC
2351 * back into a known state.
2352 */
2353 return -EINVAL;
2354 }
2355 if (!room_on_ring(xhci, ep_ring, num_trbs)) {
2356 /* FIXME allocate more room */
2357 xhci_err(xhci, "ERROR no room on ep ring\n");
2358 return -ENOMEM;
2359 }
John Youn6c12db92010-05-10 15:33:00 -07002360
2361 if (enqueue_is_link_trb(ep_ring)) {
2362 struct xhci_ring *ring = ep_ring;
2363 union xhci_trb *next;
John Youn6c12db92010-05-10 15:33:00 -07002364
2365 xhci_dbg(xhci, "prepare_ring: pointing to link trb\n");
2366 next = ring->enqueue;
2367
2368 while (last_trb(xhci, ring, ring->enq_seg, next)) {
2369
2370 /* If we're not dealing with 0.95 hardware,
2371 * clear the chain bit.
2372 */
2373 if (!xhci_link_trb_quirk(xhci))
2374 next->link.control &= ~TRB_CHAIN;
2375 else
2376 next->link.control |= TRB_CHAIN;
2377
2378 wmb();
2379 next->link.control ^= (u32) TRB_CYCLE;
2380
2381 /* Toggle the cycle bit after the last ring segment. */
2382 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
2383 ring->cycle_state = (ring->cycle_state ? 0 : 1);
2384 if (!in_interrupt()) {
2385 xhci_dbg(xhci, "queue_trb: Toggle cycle "
2386 "state for ring %p = %i\n",
2387 ring, (unsigned int)ring->cycle_state);
2388 }
2389 }
2390 ring->enq_seg = ring->enq_seg->next;
2391 ring->enqueue = ring->enq_seg->trbs;
2392 next = ring->enqueue;
2393 }
2394 }
2395
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002396 return 0;
2397}
2398
Sarah Sharp23e3be12009-04-29 19:05:20 -07002399static int prepare_transfer(struct xhci_hcd *xhci,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002400 struct xhci_virt_device *xdev,
2401 unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002402 unsigned int stream_id,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002403 unsigned int num_trbs,
2404 struct urb *urb,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002405 unsigned int td_index,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002406 gfp_t mem_flags)
2407{
2408 int ret;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002409 struct urb_priv *urb_priv;
2410 struct xhci_td *td;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002411 struct xhci_ring *ep_ring;
John Yound115b042009-07-27 12:05:15 -07002412 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002413
2414 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2415 if (!ep_ring) {
2416 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2417 stream_id);
2418 return -EINVAL;
2419 }
2420
2421 ret = prepare_ring(xhci, ep_ring,
John Yound115b042009-07-27 12:05:15 -07002422 ep_ctx->ep_info & EP_STATE_MASK,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002423 num_trbs, mem_flags);
2424 if (ret)
2425 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002426
Andiry Xu8e51adc2010-07-22 15:23:31 -07002427 urb_priv = urb->hcpriv;
2428 td = urb_priv->td[td_index];
2429
2430 INIT_LIST_HEAD(&td->td_list);
2431 INIT_LIST_HEAD(&td->cancelled_td_list);
2432
2433 if (td_index == 0) {
Sarah Sharp214f76f2010-10-26 11:22:02 -07002434 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
Andiry Xu8e51adc2010-07-22 15:23:31 -07002435 if (unlikely(ret)) {
2436 xhci_urb_free_priv(xhci, urb_priv);
2437 urb->hcpriv = NULL;
2438 return ret;
2439 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002440 }
2441
Andiry Xu8e51adc2010-07-22 15:23:31 -07002442 td->urb = urb;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002443 /* Add this TD to the tail of the endpoint ring's TD list */
Andiry Xu8e51adc2010-07-22 15:23:31 -07002444 list_add_tail(&td->td_list, &ep_ring->td_list);
2445 td->start_seg = ep_ring->enq_seg;
2446 td->first_trb = ep_ring->enqueue;
2447
2448 urb_priv->td[td_index] = td;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002449
2450 return 0;
2451}
2452
Sarah Sharp23e3be12009-04-29 19:05:20 -07002453static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002454{
2455 int num_sgs, num_trbs, running_total, temp, i;
2456 struct scatterlist *sg;
2457
2458 sg = NULL;
2459 num_sgs = urb->num_sgs;
2460 temp = urb->transfer_buffer_length;
2461
2462 xhci_dbg(xhci, "count sg list trbs: \n");
2463 num_trbs = 0;
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002464 for_each_sg(urb->sg, sg, num_sgs, i) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002465 unsigned int previous_total_trbs = num_trbs;
2466 unsigned int len = sg_dma_len(sg);
2467
2468 /* Scatter gather list entries may cross 64KB boundaries */
2469 running_total = TRB_MAX_BUFF_SIZE -
2470 (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2471 if (running_total != 0)
2472 num_trbs++;
2473
2474 /* How many more 64KB chunks to transfer, how many more TRBs? */
2475 while (running_total < sg_dma_len(sg)) {
2476 num_trbs++;
2477 running_total += TRB_MAX_BUFF_SIZE;
2478 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002479 xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
2480 i, (unsigned long long)sg_dma_address(sg),
2481 len, len, num_trbs - previous_total_trbs);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002482
2483 len = min_t(int, len, temp);
2484 temp -= len;
2485 if (temp == 0)
2486 break;
2487 }
2488 xhci_dbg(xhci, "\n");
2489 if (!in_interrupt())
Andiry Xuf2c565e2010-12-20 17:12:24 +08002490 xhci_dbg(xhci, "ep %#x - urb len = %d, sglist used, "
2491 "num_trbs = %d\n",
Sarah Sharp8a96c052009-04-27 19:59:19 -07002492 urb->ep->desc.bEndpointAddress,
2493 urb->transfer_buffer_length,
2494 num_trbs);
2495 return num_trbs;
2496}
2497
Sarah Sharp23e3be12009-04-29 19:05:20 -07002498static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002499{
2500 if (num_trbs != 0)
2501 dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
2502 "TRBs, %d left\n", __func__,
2503 urb->ep->desc.bEndpointAddress, num_trbs);
2504 if (running_total != urb->transfer_buffer_length)
2505 dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
2506 "queued %#x (%d), asked for %#x (%d)\n",
2507 __func__,
2508 urb->ep->desc.bEndpointAddress,
2509 running_total, running_total,
2510 urb->transfer_buffer_length,
2511 urb->transfer_buffer_length);
2512}
2513
Sarah Sharp23e3be12009-04-29 19:05:20 -07002514static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002515 unsigned int ep_index, unsigned int stream_id, int start_cycle,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002516 struct xhci_generic_trb *start_trb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002517{
Sarah Sharp8a96c052009-04-27 19:59:19 -07002518 /*
2519 * Pass all the TRBs to the hardware at once and make sure this write
2520 * isn't reordered.
2521 */
2522 wmb();
Andiry Xu50f7b522010-12-20 15:09:34 +08002523 if (start_cycle)
2524 start_trb->field[3] |= start_cycle;
2525 else
2526 start_trb->field[3] &= ~0x1;
Andiry Xube88fe42010-10-14 07:22:57 -07002527 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002528}
2529
Sarah Sharp624defa2009-09-02 12:14:28 -07002530/*
2531 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
2532 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
2533 * (comprised of sg list entries) can take several service intervals to
2534 * transmit.
2535 */
2536int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2537 struct urb *urb, int slot_id, unsigned int ep_index)
2538{
2539 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
2540 xhci->devs[slot_id]->out_ctx, ep_index);
2541 int xhci_interval;
2542 int ep_interval;
2543
2544 xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
2545 ep_interval = urb->interval;
2546 /* Convert to microframes */
2547 if (urb->dev->speed == USB_SPEED_LOW ||
2548 urb->dev->speed == USB_SPEED_FULL)
2549 ep_interval *= 8;
2550 /* FIXME change this to a warning and a suggestion to use the new API
2551 * to set the polling interval (once the API is added).
2552 */
2553 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08002554 if (printk_ratelimit())
Sarah Sharp624defa2009-09-02 12:14:28 -07002555 dev_dbg(&urb->dev->dev, "Driver uses different interval"
2556 " (%d microframe%s) than xHCI "
2557 "(%d microframe%s)\n",
2558 ep_interval,
2559 ep_interval == 1 ? "" : "s",
2560 xhci_interval,
2561 xhci_interval == 1 ? "" : "s");
2562 urb->interval = xhci_interval;
2563 /* Convert back to frames for LS/FS devices */
2564 if (urb->dev->speed == USB_SPEED_LOW ||
2565 urb->dev->speed == USB_SPEED_FULL)
2566 urb->interval /= 8;
2567 }
2568 return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
2569}
2570
Sarah Sharp04dd9502009-11-11 10:28:30 -08002571/*
2572 * The TD size is the number of bytes remaining in the TD (including this TRB),
2573 * right shifted by 10.
2574 * It must fit in bits 21:17, so it can't be bigger than 31.
2575 */
2576static u32 xhci_td_remainder(unsigned int remainder)
2577{
2578 u32 max = (1 << (21 - 17 + 1)) - 1;
2579
2580 if ((remainder >> 10) >= max)
2581 return max << 17;
2582 else
2583 return (remainder >> 10) << 17;
2584}
2585
Sarah Sharp23e3be12009-04-29 19:05:20 -07002586static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002587 struct urb *urb, int slot_id, unsigned int ep_index)
2588{
2589 struct xhci_ring *ep_ring;
2590 unsigned int num_trbs;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002591 struct urb_priv *urb_priv;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002592 struct xhci_td *td;
2593 struct scatterlist *sg;
2594 int num_sgs;
2595 int trb_buff_len, this_sg_len, running_total;
2596 bool first_trb;
2597 u64 addr;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002598 bool more_trbs_coming;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002599
2600 struct xhci_generic_trb *start_trb;
2601 int start_cycle;
2602
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002603 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2604 if (!ep_ring)
2605 return -EINVAL;
2606
Sarah Sharp8a96c052009-04-27 19:59:19 -07002607 num_trbs = count_sg_trbs_needed(xhci, urb);
2608 num_sgs = urb->num_sgs;
2609
Sarah Sharp23e3be12009-04-29 19:05:20 -07002610 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002611 ep_index, urb->stream_id,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002612 num_trbs, urb, 0, mem_flags);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002613 if (trb_buff_len < 0)
2614 return trb_buff_len;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002615
2616 urb_priv = urb->hcpriv;
2617 td = urb_priv->td[0];
2618
Sarah Sharp8a96c052009-04-27 19:59:19 -07002619 /*
2620 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2621 * until we've finished creating all the other TRBs. The ring's cycle
2622 * state may change as we enqueue the other TRBs, so save it too.
2623 */
2624 start_trb = &ep_ring->enqueue->generic;
2625 start_cycle = ep_ring->cycle_state;
2626
2627 running_total = 0;
2628 /*
2629 * How much data is in the first TRB?
2630 *
2631 * There are three forces at work for TRB buffer pointers and lengths:
2632 * 1. We don't want to walk off the end of this sg-list entry buffer.
2633 * 2. The transfer length that the driver requested may be smaller than
2634 * the amount of memory allocated for this scatter-gather list.
2635 * 3. TRBs buffers can't cross 64KB boundaries.
2636 */
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002637 sg = urb->sg;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002638 addr = (u64) sg_dma_address(sg);
2639 this_sg_len = sg_dma_len(sg);
2640 trb_buff_len = TRB_MAX_BUFF_SIZE -
2641 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2642 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2643 if (trb_buff_len > urb->transfer_buffer_length)
2644 trb_buff_len = urb->transfer_buffer_length;
2645 xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
2646 trb_buff_len);
2647
2648 first_trb = true;
2649 /* Queue the first TRB, even if it's zero-length */
2650 do {
2651 u32 field = 0;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002652 u32 length_field = 0;
Sarah Sharp04dd9502009-11-11 10:28:30 -08002653 u32 remainder = 0;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002654
2655 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002656 if (first_trb) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002657 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002658 if (start_cycle == 0)
2659 field |= 0x1;
2660 } else
Sarah Sharp8a96c052009-04-27 19:59:19 -07002661 field |= ep_ring->cycle_state;
2662
2663 /* Chain all the TRBs together; clear the chain bit in the last
2664 * TRB to indicate it's the last TRB in the chain.
2665 */
2666 if (num_trbs > 1) {
2667 field |= TRB_CHAIN;
2668 } else {
2669 /* FIXME - add check for ZERO_PACKET flag before this */
2670 td->last_trb = ep_ring->enqueue;
2671 field |= TRB_IOC;
2672 }
2673 xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
2674 "64KB boundary at %#x, end dma = %#x\n",
2675 (unsigned int) addr, trb_buff_len, trb_buff_len,
2676 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2677 (unsigned int) addr + trb_buff_len);
2678 if (TRB_MAX_BUFF_SIZE -
2679 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
2680 xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
2681 xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
2682 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2683 (unsigned int) addr + trb_buff_len);
2684 }
Sarah Sharp04dd9502009-11-11 10:28:30 -08002685 remainder = xhci_td_remainder(urb->transfer_buffer_length -
2686 running_total) ;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002687 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002688 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002689 TRB_INTR_TARGET(0);
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002690 if (num_trbs > 1)
2691 more_trbs_coming = true;
2692 else
2693 more_trbs_coming = false;
2694 queue_trb(xhci, ep_ring, false, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002695 lower_32_bits(addr),
2696 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002697 length_field,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002698 /* We always want to know if the TRB was short,
2699 * or we won't get an event when it completes.
2700 * (Unless we use event data TRBs, which are a
2701 * waste of space and HC resources.)
2702 */
2703 field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
2704 --num_trbs;
2705 running_total += trb_buff_len;
2706
2707 /* Calculate length for next transfer --
2708 * Are we done queueing all the TRBs for this sg entry?
2709 */
2710 this_sg_len -= trb_buff_len;
2711 if (this_sg_len == 0) {
2712 --num_sgs;
2713 if (num_sgs == 0)
2714 break;
2715 sg = sg_next(sg);
2716 addr = (u64) sg_dma_address(sg);
2717 this_sg_len = sg_dma_len(sg);
2718 } else {
2719 addr += trb_buff_len;
2720 }
2721
2722 trb_buff_len = TRB_MAX_BUFF_SIZE -
2723 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2724 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2725 if (running_total + trb_buff_len > urb->transfer_buffer_length)
2726 trb_buff_len =
2727 urb->transfer_buffer_length - running_total;
2728 } while (running_total < urb->transfer_buffer_length);
2729
2730 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002731 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002732 start_cycle, start_trb);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002733 return 0;
2734}
2735
Sarah Sharpb10de142009-04-27 19:58:50 -07002736/* This is very similar to what ehci-q.c qtd_fill() does */
Sarah Sharp23e3be12009-04-29 19:05:20 -07002737int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpb10de142009-04-27 19:58:50 -07002738 struct urb *urb, int slot_id, unsigned int ep_index)
2739{
2740 struct xhci_ring *ep_ring;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002741 struct urb_priv *urb_priv;
Sarah Sharpb10de142009-04-27 19:58:50 -07002742 struct xhci_td *td;
2743 int num_trbs;
2744 struct xhci_generic_trb *start_trb;
2745 bool first_trb;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002746 bool more_trbs_coming;
Sarah Sharpb10de142009-04-27 19:58:50 -07002747 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002748 u32 field, length_field;
Sarah Sharpb10de142009-04-27 19:58:50 -07002749
2750 int running_total, trb_buff_len, ret;
2751 u64 addr;
2752
Alan Sternff9c8952010-04-02 13:27:28 -04002753 if (urb->num_sgs)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002754 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
2755
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002756 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2757 if (!ep_ring)
2758 return -EINVAL;
Sarah Sharpb10de142009-04-27 19:58:50 -07002759
2760 num_trbs = 0;
2761 /* How much data is (potentially) left before the 64KB boundary? */
2762 running_total = TRB_MAX_BUFF_SIZE -
2763 (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2764
2765 /* If there's some data on this 64KB chunk, or we have to send a
2766 * zero-length transfer, we need at least one TRB
2767 */
2768 if (running_total != 0 || urb->transfer_buffer_length == 0)
2769 num_trbs++;
2770 /* How many more 64KB chunks to transfer, how many more TRBs? */
2771 while (running_total < urb->transfer_buffer_length) {
2772 num_trbs++;
2773 running_total += TRB_MAX_BUFF_SIZE;
2774 }
2775 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
2776
2777 if (!in_interrupt())
Andiry Xuf2c565e2010-12-20 17:12:24 +08002778 xhci_dbg(xhci, "ep %#x - urb len = %#x (%d), "
2779 "addr = %#llx, num_trbs = %d\n",
Sarah Sharpb10de142009-04-27 19:58:50 -07002780 urb->ep->desc.bEndpointAddress,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002781 urb->transfer_buffer_length,
2782 urb->transfer_buffer_length,
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002783 (unsigned long long)urb->transfer_dma,
Sarah Sharpb10de142009-04-27 19:58:50 -07002784 num_trbs);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002785
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002786 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2787 ep_index, urb->stream_id,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002788 num_trbs, urb, 0, mem_flags);
Sarah Sharpb10de142009-04-27 19:58:50 -07002789 if (ret < 0)
2790 return ret;
2791
Andiry Xu8e51adc2010-07-22 15:23:31 -07002792 urb_priv = urb->hcpriv;
2793 td = urb_priv->td[0];
2794
Sarah Sharpb10de142009-04-27 19:58:50 -07002795 /*
2796 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2797 * until we've finished creating all the other TRBs. The ring's cycle
2798 * state may change as we enqueue the other TRBs, so save it too.
2799 */
2800 start_trb = &ep_ring->enqueue->generic;
2801 start_cycle = ep_ring->cycle_state;
2802
2803 running_total = 0;
2804 /* How much data is in the first TRB? */
2805 addr = (u64) urb->transfer_dma;
2806 trb_buff_len = TRB_MAX_BUFF_SIZE -
2807 (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2808 if (urb->transfer_buffer_length < trb_buff_len)
2809 trb_buff_len = urb->transfer_buffer_length;
2810
2811 first_trb = true;
2812
2813 /* Queue the first TRB, even if it's zero-length */
2814 do {
Sarah Sharp04dd9502009-11-11 10:28:30 -08002815 u32 remainder = 0;
Sarah Sharpb10de142009-04-27 19:58:50 -07002816 field = 0;
2817
2818 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002819 if (first_trb) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002820 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002821 if (start_cycle == 0)
2822 field |= 0x1;
2823 } else
Sarah Sharpb10de142009-04-27 19:58:50 -07002824 field |= ep_ring->cycle_state;
2825
2826 /* Chain all the TRBs together; clear the chain bit in the last
2827 * TRB to indicate it's the last TRB in the chain.
2828 */
2829 if (num_trbs > 1) {
2830 field |= TRB_CHAIN;
2831 } else {
2832 /* FIXME - add check for ZERO_PACKET flag before this */
2833 td->last_trb = ep_ring->enqueue;
2834 field |= TRB_IOC;
2835 }
Sarah Sharp04dd9502009-11-11 10:28:30 -08002836 remainder = xhci_td_remainder(urb->transfer_buffer_length -
2837 running_total);
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002838 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002839 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002840 TRB_INTR_TARGET(0);
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002841 if (num_trbs > 1)
2842 more_trbs_coming = true;
2843 else
2844 more_trbs_coming = false;
2845 queue_trb(xhci, ep_ring, false, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002846 lower_32_bits(addr),
2847 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002848 length_field,
Sarah Sharpb10de142009-04-27 19:58:50 -07002849 /* We always want to know if the TRB was short,
2850 * or we won't get an event when it completes.
2851 * (Unless we use event data TRBs, which are a
2852 * waste of space and HC resources.)
2853 */
2854 field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
2855 --num_trbs;
2856 running_total += trb_buff_len;
2857
2858 /* Calculate length for next transfer */
2859 addr += trb_buff_len;
2860 trb_buff_len = urb->transfer_buffer_length - running_total;
2861 if (trb_buff_len > TRB_MAX_BUFF_SIZE)
2862 trb_buff_len = TRB_MAX_BUFF_SIZE;
2863 } while (running_total < urb->transfer_buffer_length);
2864
Sarah Sharp8a96c052009-04-27 19:59:19 -07002865 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002866 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002867 start_cycle, start_trb);
Sarah Sharpb10de142009-04-27 19:58:50 -07002868 return 0;
2869}
2870
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002871/* Caller must have locked xhci->lock */
Sarah Sharp23e3be12009-04-29 19:05:20 -07002872int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002873 struct urb *urb, int slot_id, unsigned int ep_index)
2874{
2875 struct xhci_ring *ep_ring;
2876 int num_trbs;
2877 int ret;
2878 struct usb_ctrlrequest *setup;
2879 struct xhci_generic_trb *start_trb;
2880 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002881 u32 field, length_field;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002882 struct urb_priv *urb_priv;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002883 struct xhci_td *td;
2884
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002885 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2886 if (!ep_ring)
2887 return -EINVAL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002888
2889 /*
2890 * Need to copy setup packet into setup TRB, so we can't use the setup
2891 * DMA address.
2892 */
2893 if (!urb->setup_packet)
2894 return -EINVAL;
2895
2896 if (!in_interrupt())
2897 xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
2898 slot_id, ep_index);
2899 /* 1 TRB for setup, 1 for status */
2900 num_trbs = 2;
2901 /*
2902 * Don't need to check if we need additional event data and normal TRBs,
2903 * since data in control transfers will never get bigger than 16MB
2904 * XXX: can we get a buffer that crosses 64KB boundaries?
2905 */
2906 if (urb->transfer_buffer_length > 0)
2907 num_trbs++;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002908 ret = prepare_transfer(xhci, xhci->devs[slot_id],
2909 ep_index, urb->stream_id,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002910 num_trbs, urb, 0, mem_flags);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002911 if (ret < 0)
2912 return ret;
2913
Andiry Xu8e51adc2010-07-22 15:23:31 -07002914 urb_priv = urb->hcpriv;
2915 td = urb_priv->td[0];
2916
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002917 /*
2918 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2919 * until we've finished creating all the other TRBs. The ring's cycle
2920 * state may change as we enqueue the other TRBs, so save it too.
2921 */
2922 start_trb = &ep_ring->enqueue->generic;
2923 start_cycle = ep_ring->cycle_state;
2924
2925 /* Queue setup TRB - see section 6.4.1.2.1 */
2926 /* FIXME better way to translate setup_packet into two u32 fields? */
2927 setup = (struct usb_ctrlrequest *) urb->setup_packet;
Andiry Xu50f7b522010-12-20 15:09:34 +08002928 field = 0;
2929 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
2930 if (start_cycle == 0)
2931 field |= 0x1;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002932 queue_trb(xhci, ep_ring, false, true,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002933 /* FIXME endianness is probably going to bite my ass here. */
2934 setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
2935 setup->wIndex | setup->wLength << 16,
2936 TRB_LEN(8) | TRB_INTR_TARGET(0),
2937 /* Immediate data in pointer */
Andiry Xu50f7b522010-12-20 15:09:34 +08002938 field);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002939
2940 /* If there's data, queue data TRBs */
2941 field = 0;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002942 length_field = TRB_LEN(urb->transfer_buffer_length) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002943 xhci_td_remainder(urb->transfer_buffer_length) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002944 TRB_INTR_TARGET(0);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002945 if (urb->transfer_buffer_length > 0) {
2946 if (setup->bRequestType & USB_DIR_IN)
2947 field |= TRB_DIR_IN;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002948 queue_trb(xhci, ep_ring, false, true,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002949 lower_32_bits(urb->transfer_dma),
2950 upper_32_bits(urb->transfer_dma),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002951 length_field,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002952 /* Event on short tx */
2953 field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
2954 }
2955
2956 /* Save the DMA address of the last TRB in the TD */
2957 td->last_trb = ep_ring->enqueue;
2958
2959 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
2960 /* If the device sent data, the status stage is an OUT transfer */
2961 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
2962 field = 0;
2963 else
2964 field = TRB_DIR_IN;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002965 queue_trb(xhci, ep_ring, false, false,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002966 0,
2967 0,
2968 TRB_INTR_TARGET(0),
2969 /* Event on completion */
2970 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
2971
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002972 giveback_first_trb(xhci, slot_id, ep_index, 0,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002973 start_cycle, start_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002974 return 0;
2975}
2976
Andiry Xu04e51902010-07-22 15:23:39 -07002977static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
2978 struct urb *urb, int i)
2979{
2980 int num_trbs = 0;
2981 u64 addr, td_len, running_total;
2982
2983 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
2984 td_len = urb->iso_frame_desc[i].length;
2985
2986 running_total = TRB_MAX_BUFF_SIZE -
2987 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
2988 if (running_total != 0)
2989 num_trbs++;
2990
2991 while (running_total < td_len) {
2992 num_trbs++;
2993 running_total += TRB_MAX_BUFF_SIZE;
2994 }
2995
2996 return num_trbs;
2997}
2998
2999/* This is for isoc transfer */
3000static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3001 struct urb *urb, int slot_id, unsigned int ep_index)
3002{
3003 struct xhci_ring *ep_ring;
3004 struct urb_priv *urb_priv;
3005 struct xhci_td *td;
3006 int num_tds, trbs_per_td;
3007 struct xhci_generic_trb *start_trb;
3008 bool first_trb;
3009 int start_cycle;
3010 u32 field, length_field;
3011 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3012 u64 start_addr, addr;
3013 int i, j;
Andiry Xu47cbf692010-12-20 14:49:48 +08003014 bool more_trbs_coming;
Andiry Xu04e51902010-07-22 15:23:39 -07003015
3016 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3017
3018 num_tds = urb->number_of_packets;
3019 if (num_tds < 1) {
3020 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3021 return -EINVAL;
3022 }
3023
3024 if (!in_interrupt())
Andiry Xuf2c565e2010-12-20 17:12:24 +08003025 xhci_dbg(xhci, "ep %#x - urb len = %#x (%d),"
Andiry Xu04e51902010-07-22 15:23:39 -07003026 " addr = %#llx, num_tds = %d\n",
3027 urb->ep->desc.bEndpointAddress,
3028 urb->transfer_buffer_length,
3029 urb->transfer_buffer_length,
3030 (unsigned long long)urb->transfer_dma,
3031 num_tds);
3032
3033 start_addr = (u64) urb->transfer_dma;
3034 start_trb = &ep_ring->enqueue->generic;
3035 start_cycle = ep_ring->cycle_state;
3036
3037 /* Queue the first TRB, even if it's zero-length */
3038 for (i = 0; i < num_tds; i++) {
3039 first_trb = true;
3040
3041 running_total = 0;
3042 addr = start_addr + urb->iso_frame_desc[i].offset;
3043 td_len = urb->iso_frame_desc[i].length;
3044 td_remain_len = td_len;
3045
3046 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
3047
3048 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
3049 urb->stream_id, trbs_per_td, urb, i, mem_flags);
3050 if (ret < 0)
3051 return ret;
3052
3053 urb_priv = urb->hcpriv;
3054 td = urb_priv->td[i];
3055
3056 for (j = 0; j < trbs_per_td; j++) {
3057 u32 remainder = 0;
3058 field = 0;
3059
3060 if (first_trb) {
3061 /* Queue the isoc TRB */
3062 field |= TRB_TYPE(TRB_ISOC);
3063 /* Assume URB_ISO_ASAP is set */
3064 field |= TRB_SIA;
Andiry Xu50f7b522010-12-20 15:09:34 +08003065 if (i == 0) {
3066 if (start_cycle == 0)
3067 field |= 0x1;
3068 } else
Andiry Xu04e51902010-07-22 15:23:39 -07003069 field |= ep_ring->cycle_state;
3070 first_trb = false;
3071 } else {
3072 /* Queue other normal TRBs */
3073 field |= TRB_TYPE(TRB_NORMAL);
3074 field |= ep_ring->cycle_state;
3075 }
3076
3077 /* Chain all the TRBs together; clear the chain bit in
3078 * the last TRB to indicate it's the last TRB in the
3079 * chain.
3080 */
3081 if (j < trbs_per_td - 1) {
3082 field |= TRB_CHAIN;
Andiry Xu47cbf692010-12-20 14:49:48 +08003083 more_trbs_coming = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003084 } else {
3085 td->last_trb = ep_ring->enqueue;
3086 field |= TRB_IOC;
Andiry Xu47cbf692010-12-20 14:49:48 +08003087 more_trbs_coming = false;
Andiry Xu04e51902010-07-22 15:23:39 -07003088 }
3089
3090 /* Calculate TRB length */
3091 trb_buff_len = TRB_MAX_BUFF_SIZE -
3092 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
3093 if (trb_buff_len > td_remain_len)
3094 trb_buff_len = td_remain_len;
3095
3096 remainder = xhci_td_remainder(td_len - running_total);
3097 length_field = TRB_LEN(trb_buff_len) |
3098 remainder |
3099 TRB_INTR_TARGET(0);
Andiry Xu47cbf692010-12-20 14:49:48 +08003100 queue_trb(xhci, ep_ring, false, more_trbs_coming,
Andiry Xu04e51902010-07-22 15:23:39 -07003101 lower_32_bits(addr),
3102 upper_32_bits(addr),
3103 length_field,
3104 /* We always want to know if the TRB was short,
3105 * or we won't get an event when it completes.
3106 * (Unless we use event data TRBs, which are a
3107 * waste of space and HC resources.)
3108 */
3109 field | TRB_ISP);
3110 running_total += trb_buff_len;
3111
3112 addr += trb_buff_len;
3113 td_remain_len -= trb_buff_len;
3114 }
3115
3116 /* Check TD length */
3117 if (running_total != td_len) {
3118 xhci_err(xhci, "ISOC TD length unmatch\n");
3119 return -EINVAL;
3120 }
3121 }
3122
Andiry Xue1eab2e2011-01-04 16:30:39 -08003123 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3124 start_cycle, start_trb);
Andiry Xu04e51902010-07-22 15:23:39 -07003125 return 0;
3126}
3127
3128/*
3129 * Check transfer ring to guarantee there is enough room for the urb.
3130 * Update ISO URB start_frame and interval.
3131 * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
3132 * update the urb->start_frame by now.
3133 * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
3134 */
3135int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3136 struct urb *urb, int slot_id, unsigned int ep_index)
3137{
3138 struct xhci_virt_device *xdev;
3139 struct xhci_ring *ep_ring;
3140 struct xhci_ep_ctx *ep_ctx;
3141 int start_frame;
3142 int xhci_interval;
3143 int ep_interval;
3144 int num_tds, num_trbs, i;
3145 int ret;
3146
3147 xdev = xhci->devs[slot_id];
3148 ep_ring = xdev->eps[ep_index].ring;
3149 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3150
3151 num_trbs = 0;
3152 num_tds = urb->number_of_packets;
3153 for (i = 0; i < num_tds; i++)
3154 num_trbs += count_isoc_trbs_needed(xhci, urb, i);
3155
3156 /* Check the ring to guarantee there is enough room for the whole urb.
3157 * Do not insert any td of the urb to the ring if the check failed.
3158 */
3159 ret = prepare_ring(xhci, ep_ring, ep_ctx->ep_info & EP_STATE_MASK,
3160 num_trbs, mem_flags);
3161 if (ret)
3162 return ret;
3163
3164 start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
3165 start_frame &= 0x3fff;
3166
3167 urb->start_frame = start_frame;
3168 if (urb->dev->speed == USB_SPEED_LOW ||
3169 urb->dev->speed == USB_SPEED_FULL)
3170 urb->start_frame >>= 3;
3171
3172 xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
3173 ep_interval = urb->interval;
3174 /* Convert to microframes */
3175 if (urb->dev->speed == USB_SPEED_LOW ||
3176 urb->dev->speed == USB_SPEED_FULL)
3177 ep_interval *= 8;
3178 /* FIXME change this to a warning and a suggestion to use the new API
3179 * to set the polling interval (once the API is added).
3180 */
3181 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08003182 if (printk_ratelimit())
Andiry Xu04e51902010-07-22 15:23:39 -07003183 dev_dbg(&urb->dev->dev, "Driver uses different interval"
3184 " (%d microframe%s) than xHCI "
3185 "(%d microframe%s)\n",
3186 ep_interval,
3187 ep_interval == 1 ? "" : "s",
3188 xhci_interval,
3189 xhci_interval == 1 ? "" : "s");
3190 urb->interval = xhci_interval;
3191 /* Convert back to frames for LS/FS devices */
3192 if (urb->dev->speed == USB_SPEED_LOW ||
3193 urb->dev->speed == USB_SPEED_FULL)
3194 urb->interval /= 8;
3195 }
3196 return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
3197}
3198
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003199/**** Command Ring Operations ****/
3200
Sarah Sharp913a8a32009-09-04 10:53:13 -07003201/* Generic function for queueing a command TRB on the command ring.
3202 * Check to make sure there's room on the command ring for one command TRB.
3203 * Also check that there's room reserved for commands that must not fail.
3204 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3205 * then only check for the number of reserved spots.
3206 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3207 * because the command event handler may want to resubmit a failed command.
3208 */
3209static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3210 u32 field3, u32 field4, bool command_must_succeed)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003211{
Sarah Sharp913a8a32009-09-04 10:53:13 -07003212 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003213 int ret;
3214
Sarah Sharp913a8a32009-09-04 10:53:13 -07003215 if (!command_must_succeed)
3216 reserved_trbs++;
3217
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003218 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
3219 reserved_trbs, GFP_ATOMIC);
3220 if (ret < 0) {
3221 xhci_err(xhci, "ERR: No room for command on command ring\n");
Sarah Sharp913a8a32009-09-04 10:53:13 -07003222 if (command_must_succeed)
3223 xhci_err(xhci, "ERR: Reserved TRB counting for "
3224 "unfailable commands failed.\n");
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003225 return ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003226 }
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003227 queue_trb(xhci, xhci->cmd_ring, false, false, field1, field2, field3,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003228 field4 | xhci->cmd_ring->cycle_state);
3229 return 0;
3230}
3231
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003232/* Queue a slot enable or disable request on the command ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003233int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003234{
3235 return queue_command(xhci, 0, 0, 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003236 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003237}
3238
3239/* Queue an address device command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003240int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3241 u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003242{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003243 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3244 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003245 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
3246 false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003247}
Sarah Sharpf94e01862009-04-27 19:58:38 -07003248
Sarah Sharp02386342010-05-24 13:25:28 -07003249int xhci_queue_vendor_command(struct xhci_hcd *xhci,
3250 u32 field1, u32 field2, u32 field3, u32 field4)
3251{
3252 return queue_command(xhci, field1, field2, field3, field4, false);
3253}
3254
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003255/* Queue a reset device command TRB */
3256int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
3257{
3258 return queue_command(xhci, 0, 0, 0,
3259 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
3260 false);
3261}
3262
Sarah Sharpf94e01862009-04-27 19:58:38 -07003263/* Queue a configure endpoint command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003264int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003265 u32 slot_id, bool command_must_succeed)
Sarah Sharpf94e01862009-04-27 19:58:38 -07003266{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003267 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3268 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003269 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3270 command_must_succeed);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003271}
Sarah Sharpae636742009-04-29 19:02:31 -07003272
Sarah Sharpf2217e82009-08-07 14:04:43 -07003273/* Queue an evaluate context command TRB */
3274int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3275 u32 slot_id)
3276{
3277 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3278 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003279 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
3280 false);
Sarah Sharpf2217e82009-08-07 14:04:43 -07003281}
3282
Andiry Xube88fe42010-10-14 07:22:57 -07003283/*
3284 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3285 * activity on an endpoint that is about to be suspended.
3286 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003287int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
Andiry Xube88fe42010-10-14 07:22:57 -07003288 unsigned int ep_index, int suspend)
Sarah Sharpae636742009-04-29 19:02:31 -07003289{
3290 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3291 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3292 u32 type = TRB_TYPE(TRB_STOP_RING);
Andiry Xube88fe42010-10-14 07:22:57 -07003293 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
Sarah Sharpae636742009-04-29 19:02:31 -07003294
3295 return queue_command(xhci, 0, 0, 0,
Andiry Xube88fe42010-10-14 07:22:57 -07003296 trb_slot_id | trb_ep_index | type | trb_suspend, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003297}
3298
3299/* Set Transfer Ring Dequeue Pointer command.
3300 * This should not be used for endpoints that have streams enabled.
3301 */
3302static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003303 unsigned int ep_index, unsigned int stream_id,
3304 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -07003305 union xhci_trb *deq_ptr, u32 cycle_state)
3306{
3307 dma_addr_t addr;
3308 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3309 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003310 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
Sarah Sharpae636742009-04-29 19:02:31 -07003311 u32 type = TRB_TYPE(TRB_SET_DEQ);
Sarah Sharpbf161e82011-02-23 15:46:42 -08003312 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07003313
Sarah Sharp23e3be12009-04-29 19:05:20 -07003314 addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003315 if (addr == 0) {
Sarah Sharpae636742009-04-29 19:02:31 -07003316 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003317 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
3318 deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003319 return 0;
3320 }
Sarah Sharpbf161e82011-02-23 15:46:42 -08003321 ep = &xhci->devs[slot_id]->eps[ep_index];
3322 if ((ep->ep_state & SET_DEQ_PENDING)) {
3323 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
3324 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
3325 return 0;
3326 }
3327 ep->queued_deq_seg = deq_seg;
3328 ep->queued_deq_ptr = deq_ptr;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003329 return queue_command(xhci, lower_32_bits(addr) | cycle_state,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003330 upper_32_bits(addr), trb_stream_id,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003331 trb_slot_id | trb_ep_index | type, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003332}
Sarah Sharpa1587d92009-07-27 12:03:15 -07003333
3334int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
3335 unsigned int ep_index)
3336{
3337 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3338 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3339 u32 type = TRB_TYPE(TRB_RESET_EP);
3340
Sarah Sharp913a8a32009-09-04 10:53:13 -07003341 return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
3342 false);
Sarah Sharpa1587d92009-07-27 12:03:15 -07003343}