blob: 323b48168979a79281bbd6ee00dc50d8a688fd0a [file] [log] [blame]
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Ring initialization rules:
25 * 1. Each segment is initialized to zero, except for link TRBs.
26 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
27 * Consumer Cycle State (CCS), depending on ring function.
28 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
29 *
30 * Ring behavior rules:
31 * 1. A ring is empty if enqueue == dequeue. This means there will always be at
32 * least one free TRB in the ring. This is useful if you want to turn that
33 * into a link TRB and expand the ring.
34 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
35 * link TRB, then load the pointer with the address in the link TRB. If the
36 * link TRB had its toggle bit set, you may need to update the ring cycle
37 * state (see cycle bit rules). You may have to do this multiple times
38 * until you reach a non-link TRB.
39 * 3. A ring is full if enqueue++ (for the definition of increment above)
40 * equals the dequeue pointer.
41 *
42 * Cycle bit rules:
43 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
44 * in a link TRB, it must toggle the ring cycle state.
45 * 2. When a producer increments an enqueue pointer and encounters a toggle bit
46 * in a link TRB, it must toggle the ring cycle state.
47 *
48 * Producer rules:
49 * 1. Check if ring is full before you enqueue.
50 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
51 * Update enqueue pointer between each write (which may update the ring
52 * cycle state).
53 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
54 * and endpoint rings. If HC is the producer for the event ring,
55 * and it generates an interrupt according to interrupt modulation rules.
56 *
57 * Consumer rules:
58 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
59 * the TRB is owned by the consumer.
60 * 2. Update dequeue pointer (which may update the ring cycle state) and
61 * continue processing TRBs until you reach a TRB which is not owned by you.
62 * 3. Notify the producer. SW is the consumer for the event ring, and it
63 * updates event ring dequeue pointer. HC is the consumer for the command and
64 * endpoint rings; it generates events on the event ring for these.
65 */
66
Sarah Sharp8a96c052009-04-27 19:59:19 -070067#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090068#include <linux/slab.h>
Sarah Sharp7f84eef2009-04-27 19:53:56 -070069#include "xhci.h"
70
Andiry Xube88fe42010-10-14 07:22:57 -070071static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
72 struct xhci_virt_device *virt_dev,
73 struct xhci_event_cmd *event);
74
Sarah Sharp7f84eef2009-04-27 19:53:56 -070075/*
76 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
77 * address of the TRB.
78 */
Sarah Sharp23e3be12009-04-29 19:05:20 -070079dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -070080 union xhci_trb *trb)
81{
Sarah Sharp6071d832009-05-14 11:44:14 -070082 unsigned long segment_offset;
Sarah Sharp7f84eef2009-04-27 19:53:56 -070083
Sarah Sharp6071d832009-05-14 11:44:14 -070084 if (!seg || !trb || trb < seg->trbs)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070085 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070086 /* offset in TRBs */
87 segment_offset = trb - seg->trbs;
88 if (segment_offset > TRBS_PER_SEGMENT)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070089 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070090 return seg->dma + (segment_offset * sizeof(*trb));
Sarah Sharp7f84eef2009-04-27 19:53:56 -070091}
92
93/* Does this link TRB point to the first segment in a ring,
94 * or was the previous TRB the last TRB on the last segment in the ERST?
95 */
Dmitry Torokhov575688e2011-03-20 02:15:16 -070096static bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -070097 struct xhci_segment *seg, union xhci_trb *trb)
98{
99 if (ring == xhci->event_ring)
100 return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
101 (seg->next == xhci->event_ring->first_seg);
102 else
Matt Evans28ccd292011-03-29 13:40:46 +1100103 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700104}
105
106/* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
107 * segment? I.e. would the updated event TRB pointer step off the end of the
108 * event seg?
109 */
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700110static int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700111 struct xhci_segment *seg, union xhci_trb *trb)
112{
113 if (ring == xhci->event_ring)
114 return trb == &seg->trbs[TRBS_PER_SEGMENT];
115 else
Matt Evansf5960b62011-06-01 10:22:55 +1000116 return TRB_TYPE_LINK_LE32(trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700117}
118
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700119static int enqueue_is_link_trb(struct xhci_ring *ring)
John Youn6c12db92010-05-10 15:33:00 -0700120{
121 struct xhci_link_trb *link = &ring->enqueue->link;
Matt Evansf5960b62011-06-01 10:22:55 +1000122 return TRB_TYPE_LINK_LE32(link->control);
John Youn6c12db92010-05-10 15:33:00 -0700123}
124
Sarah Sharpae636742009-04-29 19:02:31 -0700125/* Updates trb to point to the next TRB in the ring, and updates seg if the next
126 * TRB is in a new segment. This does not skip over link TRBs, and it does not
127 * effect the ring dequeue or enqueue pointers.
128 */
129static void next_trb(struct xhci_hcd *xhci,
130 struct xhci_ring *ring,
131 struct xhci_segment **seg,
132 union xhci_trb **trb)
133{
134 if (last_trb(xhci, ring, *seg, *trb)) {
135 *seg = (*seg)->next;
136 *trb = ((*seg)->trbs);
137 } else {
John Youna1669b22010-08-09 13:56:11 -0700138 (*trb)++;
Sarah Sharpae636742009-04-29 19:02:31 -0700139 }
140}
141
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700142/*
143 * See Cycle bit rules. SW is the consumer for the event ring only.
144 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
145 */
Andiry Xu3b72fca2012-03-05 17:49:32 +0800146static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700147{
Sarah Sharp66e49d82009-07-27 12:03:46 -0700148 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700149
150 ring->deq_updates++;
Andiry Xub008df62012-03-05 17:49:34 +0800151
Sarah Sharpc65fee42012-07-26 12:03:59 -0700152 /*
153 * If this is not event ring, and the dequeue pointer
154 * is not on a link TRB, there is one more usable TRB
155 */
Andiry Xub008df62012-03-05 17:49:34 +0800156 if (ring->type != TYPE_EVENT &&
157 !last_trb(xhci, ring, ring->deq_seg, ring->dequeue))
158 ring->num_trbs_free++;
Andiry Xub008df62012-03-05 17:49:34 +0800159
Sarah Sharpc65fee42012-07-26 12:03:59 -0700160 do {
161 /*
162 * Update the dequeue pointer further if that was a link TRB or
163 * we're at the end of an event ring segment (which doesn't have
164 * link TRBS)
165 */
166 if (last_trb(xhci, ring, ring->deq_seg, ring->dequeue)) {
167 if (ring->type == TYPE_EVENT &&
168 last_trb_on_last_seg(xhci, ring,
169 ring->deq_seg, ring->dequeue)) {
170 ring->cycle_state = (ring->cycle_state ? 0 : 1);
171 }
172 ring->deq_seg = ring->deq_seg->next;
173 ring->dequeue = ring->deq_seg->trbs;
174 } else {
175 ring->dequeue++;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700176 }
Sarah Sharpc65fee42012-07-26 12:03:59 -0700177 } while (last_trb(xhci, ring, ring->deq_seg, ring->dequeue));
178
Sarah Sharp66e49d82009-07-27 12:03:46 -0700179 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700180}
181
182/*
183 * See Cycle bit rules. SW is the consumer for the event ring only.
184 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
185 *
186 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
187 * chain bit is set), then set the chain bit in all the following link TRBs.
188 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
189 * have their chain bit cleared (so that each Link TRB is a separate TD).
190 *
191 * 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 -0700192 * set, but other sections talk about dealing with the chain bit set. This was
193 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
194 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700195 *
196 * @more_trbs_coming: Will you enqueue more TRBs before calling
197 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700198 */
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700199static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +0800200 bool more_trbs_coming)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700201{
202 u32 chain;
203 union xhci_trb *next;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700204 unsigned long long addr;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700205
Matt Evans28ccd292011-03-29 13:40:46 +1100206 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
Andiry Xub008df62012-03-05 17:49:34 +0800207 /* If this is not event ring, there is one less usable TRB */
208 if (ring->type != TYPE_EVENT &&
209 !last_trb(xhci, ring, ring->enq_seg, ring->enqueue))
210 ring->num_trbs_free--;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700211 next = ++(ring->enqueue);
212
213 ring->enq_updates++;
214 /* Update the dequeue pointer further if that was a link TRB or we're at
215 * the end of an event ring segment (which doesn't have link TRBS)
216 */
217 while (last_trb(xhci, ring, ring->enq_seg, next)) {
Andiry Xu3b72fca2012-03-05 17:49:32 +0800218 if (ring->type != TYPE_EVENT) {
219 /*
220 * If the caller doesn't plan on enqueueing more
221 * TDs before ringing the doorbell, then we
222 * don't want to give the link TRB to the
223 * hardware just yet. We'll give the link TRB
224 * back in prepare_ring() just before we enqueue
225 * the TD at the top of the ring.
226 */
227 if (!chain && !more_trbs_coming)
228 break;
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700229
Andiry Xu3b72fca2012-03-05 17:49:32 +0800230 /* If we're not dealing with 0.95 hardware or
231 * isoc rings on AMD 0.96 host,
232 * carry over the chain bit of the previous TRB
233 * (which may mean the chain bit is cleared).
234 */
235 if (!(ring->type == TYPE_ISOC &&
236 (xhci->quirks & XHCI_AMD_0x96_HOST))
Andiry Xu7e393a82011-09-23 14:19:54 -0700237 && !xhci_link_trb_quirk(xhci)) {
Andiry Xu3b72fca2012-03-05 17:49:32 +0800238 next->link.control &=
239 cpu_to_le32(~TRB_CHAIN);
240 next->link.control |=
241 cpu_to_le32(chain);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700242 }
Andiry Xu3b72fca2012-03-05 17:49:32 +0800243 /* Give this link TRB to the hardware */
244 wmb();
245 next->link.control ^= cpu_to_le32(TRB_CYCLE);
246
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700247 /* Toggle the cycle bit after the last ring segment. */
248 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
249 ring->cycle_state = (ring->cycle_state ? 0 : 1);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700250 }
251 }
252 ring->enq_seg = ring->enq_seg->next;
253 ring->enqueue = ring->enq_seg->trbs;
254 next = ring->enqueue;
255 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700256 addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700257}
258
259/*
Andiry Xu085deb12012-03-05 17:49:40 +0800260 * Check to see if there's room to enqueue num_trbs on the ring and make sure
261 * enqueue pointer will not advance into dequeue segment. See rules above.
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700262 */
Andiry Xub008df62012-03-05 17:49:34 +0800263static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700264 unsigned int num_trbs)
265{
Andiry Xu085deb12012-03-05 17:49:40 +0800266 int num_trbs_in_deq_seg;
Andiry Xub008df62012-03-05 17:49:34 +0800267
Andiry Xu085deb12012-03-05 17:49:40 +0800268 if (ring->num_trbs_free < num_trbs)
269 return 0;
270
271 if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
272 num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
273 if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
274 return 0;
275 }
276
277 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700278}
279
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700280/* Ring the host controller doorbell after placing a command on the ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700281void xhci_ring_cmd_db(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700282{
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700283 xhci_dbg(xhci, "// Ding dong!\n");
Matthew Wilcox50d64672010-12-15 14:18:11 -0500284 xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700285 /* Flush PCI posted writes */
286 xhci_readl(xhci, &xhci->dba->doorbell[0]);
287}
288
Andiry Xube88fe42010-10-14 07:22:57 -0700289void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700290 unsigned int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700291 unsigned int ep_index,
292 unsigned int stream_id)
Sarah Sharpae636742009-04-29 19:02:31 -0700293{
Matt Evans28ccd292011-03-29 13:40:46 +1100294 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
Matthew Wilcox50d64672010-12-15 14:18:11 -0500295 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
296 unsigned int ep_state = ep->ep_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700297
Sarah Sharpae636742009-04-29 19:02:31 -0700298 /* Don't ring the doorbell for this endpoint if there are pending
Matthew Wilcox50d64672010-12-15 14:18:11 -0500299 * cancellations because we don't want to interrupt processing.
Sarah Sharp8df75f42010-04-02 15:34:16 -0700300 * We don't want to restart any stream rings if there's a set dequeue
301 * pointer command pending because the device can choose to start any
302 * stream once the endpoint is on the HW schedule.
303 * FIXME - check all the stream rings for pending cancellations.
Sarah Sharpae636742009-04-29 19:02:31 -0700304 */
Matthew Wilcox50d64672010-12-15 14:18:11 -0500305 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
306 (ep_state & EP_HALTED))
307 return;
308 xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr);
309 /* The CPU has better things to do at this point than wait for a
310 * write-posting flush. It'll get there soon enough.
311 */
Sarah Sharpae636742009-04-29 19:02:31 -0700312}
313
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700314/* Ring the doorbell for any rings with pending URBs */
315static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
316 unsigned int slot_id,
317 unsigned int ep_index)
318{
319 unsigned int stream_id;
320 struct xhci_virt_ep *ep;
321
322 ep = &xhci->devs[slot_id]->eps[ep_index];
323
324 /* A ring has pending URBs if its TD list is not empty */
325 if (!(ep->ep_state & EP_HAS_STREAMS)) {
326 if (!(list_empty(&ep->ring->td_list)))
Andiry Xube88fe42010-10-14 07:22:57 -0700327 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700328 return;
329 }
330
331 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
332 stream_id++) {
333 struct xhci_stream_info *stream_info = ep->stream_info;
334 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
Andiry Xube88fe42010-10-14 07:22:57 -0700335 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
336 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700337 }
338}
339
Sarah Sharpae636742009-04-29 19:02:31 -0700340/*
341 * Find the segment that trb is in. Start searching in start_seg.
342 * If we must move past a segment that has a link TRB with a toggle cycle state
343 * bit set, then we will toggle the value pointed at by cycle_state.
344 */
345static struct xhci_segment *find_trb_seg(
346 struct xhci_segment *start_seg,
347 union xhci_trb *trb, int *cycle_state)
348{
349 struct xhci_segment *cur_seg = start_seg;
350 struct xhci_generic_trb *generic_trb;
351
352 while (cur_seg->trbs > trb ||
353 &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
354 generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
Matt Evansf5960b62011-06-01 10:22:55 +1000355 if (generic_trb->field[3] & cpu_to_le32(LINK_TOGGLE))
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800356 *cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700357 cur_seg = cur_seg->next;
358 if (cur_seg == start_seg)
359 /* Looped over the entire list. Oops! */
Randy Dunlap326b4812010-04-19 08:53:50 -0700360 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700361 }
362 return cur_seg;
363}
364
Sarah Sharp021bff92010-07-29 22:12:20 -0700365
366static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
367 unsigned int slot_id, unsigned int ep_index,
368 unsigned int stream_id)
369{
370 struct xhci_virt_ep *ep;
371
372 ep = &xhci->devs[slot_id]->eps[ep_index];
373 /* Common case: no streams */
374 if (!(ep->ep_state & EP_HAS_STREAMS))
375 return ep->ring;
376
377 if (stream_id == 0) {
378 xhci_warn(xhci,
379 "WARN: Slot ID %u, ep index %u has streams, "
380 "but URB has no stream ID.\n",
381 slot_id, ep_index);
382 return NULL;
383 }
384
385 if (stream_id < ep->stream_info->num_streams)
386 return ep->stream_info->stream_rings[stream_id];
387
388 xhci_warn(xhci,
389 "WARN: Slot ID %u, ep index %u has "
390 "stream IDs 1 to %u allocated, "
391 "but stream ID %u is requested.\n",
392 slot_id, ep_index,
393 ep->stream_info->num_streams - 1,
394 stream_id);
395 return NULL;
396}
397
398/* Get the right ring for the given URB.
399 * If the endpoint supports streams, boundary check the URB's stream ID.
400 * If the endpoint doesn't support streams, return the singular endpoint ring.
401 */
402static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
403 struct urb *urb)
404{
405 return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
406 xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
407}
408
Sarah Sharpae636742009-04-29 19:02:31 -0700409/*
410 * Move the xHC's endpoint ring dequeue pointer past cur_td.
411 * Record the new state of the xHC's endpoint ring dequeue segment,
412 * dequeue pointer, and new consumer cycle state in state.
413 * Update our internal representation of the ring's dequeue pointer.
414 *
415 * We do this in three jumps:
416 * - First we update our new ring state to be the same as when the xHC stopped.
417 * - Then we traverse the ring to find the segment that contains
418 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
419 * any link TRBs with the toggle cycle bit set.
420 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
421 * if we've moved it past a link TRB with the toggle cycle bit set.
Matt Evans28ccd292011-03-29 13:40:46 +1100422 *
423 * Some of the uses of xhci_generic_trb are grotty, but if they're done
424 * with correct __le32 accesses they should work fine. Only users of this are
425 * in here.
Sarah Sharpae636742009-04-29 19:02:31 -0700426 */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700427void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700428 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700429 unsigned int stream_id, struct xhci_td *cur_td,
430 struct xhci_dequeue_state *state)
Sarah Sharpae636742009-04-29 19:02:31 -0700431{
432 struct xhci_virt_device *dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700433 struct xhci_ring *ep_ring;
Sarah Sharpae636742009-04-29 19:02:31 -0700434 struct xhci_generic_trb *trb;
John Yound115b042009-07-27 12:05:15 -0700435 struct xhci_ep_ctx *ep_ctx;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700436 dma_addr_t addr;
Sarah Sharpae636742009-04-29 19:02:31 -0700437
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700438 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
439 ep_index, stream_id);
440 if (!ep_ring) {
441 xhci_warn(xhci, "WARN can't find new dequeue state "
442 "for invalid stream ID %u.\n",
443 stream_id);
444 return;
445 }
Sarah Sharpae636742009-04-29 19:02:31 -0700446 state->new_cycle_state = 0;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700447 xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700448 state->new_deq_seg = find_trb_seg(cur_td->start_seg,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700449 dev->eps[ep_index].stopped_trb,
Sarah Sharpae636742009-04-29 19:02:31 -0700450 &state->new_cycle_state);
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800451 if (!state->new_deq_seg) {
452 WARN_ON(1);
453 return;
454 }
455
Sarah Sharpae636742009-04-29 19:02:31 -0700456 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700457 xhci_dbg(xhci, "Finding endpoint context\n");
John Yound115b042009-07-27 12:05:15 -0700458 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +1100459 state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
Sarah Sharpae636742009-04-29 19:02:31 -0700460
461 state->new_deq_ptr = cur_td->last_trb;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700462 xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700463 state->new_deq_seg = find_trb_seg(state->new_deq_seg,
464 state->new_deq_ptr,
465 &state->new_cycle_state);
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800466 if (!state->new_deq_seg) {
467 WARN_ON(1);
468 return;
469 }
Sarah Sharpae636742009-04-29 19:02:31 -0700470
471 trb = &state->new_deq_ptr->generic;
Matt Evansf5960b62011-06-01 10:22:55 +1000472 if (TRB_TYPE_LINK_LE32(trb->field[3]) &&
473 (trb->field[3] & cpu_to_le32(LINK_TOGGLE)))
Sarah Sharpba0a4d92011-02-23 18:13:43 -0800474 state->new_cycle_state ^= 0x1;
Sarah Sharpae636742009-04-29 19:02:31 -0700475 next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
476
Sarah Sharp01a1fdb2011-02-23 18:12:29 -0800477 /*
478 * If there is only one segment in a ring, find_trb_seg()'s while loop
479 * will not run, and it will return before it has a chance to see if it
480 * needs to toggle the cycle bit. It can't tell if the stalled transfer
481 * ended just before the link TRB on a one-segment ring, or if the TD
482 * wrapped around the top of the ring, because it doesn't have the TD in
483 * question. Look for the one-segment case where stalled TRB's address
484 * is greater than the new dequeue pointer address.
485 */
486 if (ep_ring->first_seg == ep_ring->first_seg->next &&
487 state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
488 state->new_cycle_state ^= 0x1;
489 xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
490
Sarah Sharpae636742009-04-29 19:02:31 -0700491 /* Don't update the ring cycle state for the producer (us). */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700492 xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
493 state->new_deq_seg);
494 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
495 xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
496 (unsigned long long) addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700497}
498
Sarah Sharp522989a2011-07-29 12:44:32 -0700499/* flip_cycle means flip the cycle bit of all but the first and last TRB.
500 * (The last TRB actually points to the ring enqueue pointer, which is not part
501 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
502 */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700503static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Sarah Sharp522989a2011-07-29 12:44:32 -0700504 struct xhci_td *cur_td, bool flip_cycle)
Sarah Sharpae636742009-04-29 19:02:31 -0700505{
506 struct xhci_segment *cur_seg;
507 union xhci_trb *cur_trb;
508
509 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
510 true;
511 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +1000512 if (TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) {
Sarah Sharpae636742009-04-29 19:02:31 -0700513 /* Unchain any chained Link TRBs, but
514 * leave the pointers intact.
515 */
Matt Evans28ccd292011-03-29 13:40:46 +1100516 cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN);
Sarah Sharp522989a2011-07-29 12:44:32 -0700517 /* Flip the cycle bit (link TRBs can't be the first
518 * or last TRB).
519 */
520 if (flip_cycle)
521 cur_trb->generic.field[3] ^=
522 cpu_to_le32(TRB_CYCLE);
Sarah Sharpae636742009-04-29 19:02:31 -0700523 xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700524 xhci_dbg(xhci, "Address = %p (0x%llx dma); "
525 "in seg %p (0x%llx dma)\n",
526 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700527 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700528 cur_seg,
529 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700530 } else {
531 cur_trb->generic.field[0] = 0;
532 cur_trb->generic.field[1] = 0;
533 cur_trb->generic.field[2] = 0;
534 /* Preserve only the cycle bit of this TRB */
Matt Evans28ccd292011-03-29 13:40:46 +1100535 cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
Sarah Sharp522989a2011-07-29 12:44:32 -0700536 /* Flip the cycle bit except on the first or last TRB */
537 if (flip_cycle && cur_trb != cur_td->first_trb &&
538 cur_trb != cur_td->last_trb)
539 cur_trb->generic.field[3] ^=
540 cpu_to_le32(TRB_CYCLE);
Matt Evans28ccd292011-03-29 13:40:46 +1100541 cur_trb->generic.field[3] |= cpu_to_le32(
542 TRB_TYPE(TRB_TR_NOOP));
Sarah Sharp79688ac2011-12-19 16:56:04 -0800543 xhci_dbg(xhci, "TRB to noop at offset 0x%llx\n",
544 (unsigned long long)
545 xhci_trb_virt_to_dma(cur_seg, cur_trb));
Sarah Sharpae636742009-04-29 19:02:31 -0700546 }
547 if (cur_trb == cur_td->last_trb)
548 break;
549 }
550}
551
552static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700553 unsigned int ep_index, unsigned int stream_id,
554 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -0700555 union xhci_trb *deq_ptr, u32 cycle_state);
556
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700557void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700558 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700559 unsigned int stream_id,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700560 struct xhci_dequeue_state *deq_state)
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700561{
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700562 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
563
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700564 xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
565 "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
566 deq_state->new_deq_seg,
567 (unsigned long long)deq_state->new_deq_seg->dma,
568 deq_state->new_deq_ptr,
569 (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
570 deq_state->new_cycle_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700571 queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700572 deq_state->new_deq_seg,
573 deq_state->new_deq_ptr,
574 (u32) deq_state->new_cycle_state);
575 /* Stop the TD queueing code from ringing the doorbell until
576 * this command completes. The HC won't set the dequeue pointer
577 * if the ring is running, and ringing the doorbell starts the
578 * ring running.
579 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700580 ep->ep_state |= SET_DEQ_PENDING;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700581}
582
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700583static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700584 struct xhci_virt_ep *ep)
585{
586 ep->ep_state &= ~EP_HALT_PENDING;
587 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
588 * timer is running on another CPU, we don't decrement stop_cmds_pending
589 * (since we didn't successfully stop the watchdog timer).
590 */
591 if (del_timer(&ep->stop_cmd_timer))
592 ep->stop_cmds_pending--;
593}
594
595/* Must be called with xhci->lock held in interrupt context */
596static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
597 struct xhci_td *cur_td, int status, char *adjective)
598{
Sarah Sharp214f76f2010-10-26 11:22:02 -0700599 struct usb_hcd *hcd;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700600 struct urb *urb;
601 struct urb_priv *urb_priv;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700602
Andiry Xu8e51adc2010-07-22 15:23:31 -0700603 urb = cur_td->urb;
604 urb_priv = urb->hcpriv;
605 urb_priv->td_cnt++;
Sarah Sharp214f76f2010-10-26 11:22:02 -0700606 hcd = bus_to_hcd(urb->dev->bus);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700607
Andiry Xu8e51adc2010-07-22 15:23:31 -0700608 /* Only giveback urb when this is the last td in urb */
609 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xuc41136b2011-03-22 17:08:14 +0800610 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
611 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
612 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
613 if (xhci->quirks & XHCI_AMD_PLL_FIX)
614 usb_amd_quirk_pll_enable();
615 }
616 }
Andiry Xu8e51adc2010-07-22 15:23:31 -0700617 usb_hcd_unlink_urb_from_ep(hcd, urb);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700618
619 spin_unlock(&xhci->lock);
620 usb_hcd_giveback_urb(hcd, urb, status);
621 xhci_urb_free_priv(xhci, urb_priv);
622 spin_lock(&xhci->lock);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700623 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700624}
625
Sarah Sharpae636742009-04-29 19:02:31 -0700626/*
627 * When we get a command completion for a Stop Endpoint Command, we need to
628 * unlink any cancelled TDs from the ring. There are two ways to do that:
629 *
630 * 1. If the HW was in the middle of processing the TD that needs to be
631 * cancelled, then we must move the ring's dequeue pointer past the last TRB
632 * in the TD with a Set Dequeue Pointer Command.
633 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
634 * bit cleared) so that the HW will skip over them.
635 */
636static void handle_stopped_endpoint(struct xhci_hcd *xhci,
Andiry Xube88fe42010-10-14 07:22:57 -0700637 union xhci_trb *trb, struct xhci_event_cmd *event)
Sarah Sharpae636742009-04-29 19:02:31 -0700638{
639 unsigned int slot_id;
640 unsigned int ep_index;
Andiry Xube88fe42010-10-14 07:22:57 -0700641 struct xhci_virt_device *virt_dev;
Sarah Sharpae636742009-04-29 19:02:31 -0700642 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700643 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700644 struct list_head *entry;
Randy Dunlap326b4812010-04-19 08:53:50 -0700645 struct xhci_td *cur_td = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700646 struct xhci_td *last_unlinked_td;
647
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700648 struct xhci_dequeue_state deq_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700649
Andiry Xube88fe42010-10-14 07:22:57 -0700650 if (unlikely(TRB_TO_SUSPEND_PORT(
Matt Evans28ccd292011-03-29 13:40:46 +1100651 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])))) {
Andiry Xube88fe42010-10-14 07:22:57 -0700652 slot_id = TRB_TO_SLOT_ID(
Matt Evans28ccd292011-03-29 13:40:46 +1100653 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
Andiry Xube88fe42010-10-14 07:22:57 -0700654 virt_dev = xhci->devs[slot_id];
655 if (virt_dev)
656 handle_cmd_in_cmd_wait_list(xhci, virt_dev,
657 event);
658 else
659 xhci_warn(xhci, "Stop endpoint command "
660 "completion for disabled slot %u\n",
661 slot_id);
662 return;
663 }
664
Sarah Sharpae636742009-04-29 19:02:31 -0700665 memset(&deq_state, 0, sizeof(deq_state));
Matt Evans28ccd292011-03-29 13:40:46 +1100666 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
667 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700668 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharpae636742009-04-29 19:02:31 -0700669
Sarah Sharp678539c2009-10-27 10:55:52 -0700670 if (list_empty(&ep->cancelled_td_list)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700671 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharp0714a572011-05-24 11:53:29 -0700672 ep->stopped_td = NULL;
673 ep->stopped_trb = NULL;
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);
Sarah Sharp79688ac2011-12-19 16:56:04 -0800685 xhci_dbg(xhci, "Removing canceled TD starting at 0x%llx (dma).\n",
686 (unsigned long long)xhci_trb_virt_to_dma(
687 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
Sarah Sharp522989a2011-07-29 12:44:32 -0700716 td_to_noop(xhci, ep_ring, cur_td, false);
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 */
Sarah Sharp585df1d2011-08-02 15:43:40 -0700723 list_del_init(&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);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700751 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700752
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;
Don Zickusf43d6232011-10-20 23:52:14 -0400796 unsigned long flags;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700797
798 ep = (struct xhci_virt_ep *) arg;
799 xhci = ep->xhci;
800
Don Zickusf43d6232011-10-20 23:52:14 -0400801 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700802
803 ep->stop_cmds_pending--;
804 if (xhci->xhc_state & XHCI_STATE_DYING) {
805 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
806 "xHCI as DYING, exiting.\n");
Don Zickusf43d6232011-10-20 23:52:14 -0400807 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700808 return;
809 }
810 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
811 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
812 "exiting.\n");
Don Zickusf43d6232011-10-20 23:52:14 -0400813 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700814 return;
815 }
816
817 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
818 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
819 /* Oops, HC is dead or dying or at least not responding to the stop
820 * endpoint command.
821 */
822 xhci->xhc_state |= XHCI_STATE_DYING;
823 /* Disable interrupts from the host controller and start halting it */
824 xhci_quiesce(xhci);
Don Zickusf43d6232011-10-20 23:52:14 -0400825 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700826
827 ret = xhci_halt(xhci);
828
Don Zickusf43d6232011-10-20 23:52:14 -0400829 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700830 if (ret < 0) {
831 /* This is bad; the host is not responding to commands and it's
832 * not allowing itself to be halted. At least interrupts are
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800833 * disabled. If we call usb_hc_died(), it will attempt to
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700834 * disconnect all device drivers under this host. Those
835 * disconnect() methods will wait for all URBs to be unlinked,
836 * so we must complete them.
837 */
838 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
839 xhci_warn(xhci, "Completing active URBs anyway.\n");
840 /* We could turn all TDs on the rings to no-ops. This won't
841 * help if the host has cached part of the ring, and is slow if
842 * we want to preserve the cycle bit. Skip it and hope the host
843 * doesn't touch the memory.
844 */
845 }
846 for (i = 0; i < MAX_HC_SLOTS; i++) {
847 if (!xhci->devs[i])
848 continue;
849 for (j = 0; j < 31; j++) {
850 temp_ep = &xhci->devs[i]->eps[j];
851 ring = temp_ep->ring;
852 if (!ring)
853 continue;
854 xhci_dbg(xhci, "Killing URBs for slot ID %u, "
855 "ep index %u\n", i, j);
856 while (!list_empty(&ring->td_list)) {
857 cur_td = list_first_entry(&ring->td_list,
858 struct xhci_td,
859 td_list);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700860 list_del_init(&cur_td->td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700861 if (!list_empty(&cur_td->cancelled_td_list))
Sarah Sharp585df1d2011-08-02 15:43:40 -0700862 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700863 xhci_giveback_urb_in_irq(xhci, cur_td,
864 -ESHUTDOWN, "killed");
865 }
866 while (!list_empty(&temp_ep->cancelled_td_list)) {
867 cur_td = list_first_entry(
868 &temp_ep->cancelled_td_list,
869 struct xhci_td,
870 cancelled_td_list);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700871 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700872 xhci_giveback_urb_in_irq(xhci, cur_td,
873 -ESHUTDOWN, "killed");
874 }
875 }
876 }
Don Zickusf43d6232011-10-20 23:52:14 -0400877 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700878 xhci_dbg(xhci, "Calling usb_hc_died()\n");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800879 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700880 xhci_dbg(xhci, "xHCI host controller is dead.\n");
881}
882
Andiry Xub008df62012-03-05 17:49:34 +0800883
884static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci,
885 struct xhci_virt_device *dev,
886 struct xhci_ring *ep_ring,
887 unsigned int ep_index)
888{
889 union xhci_trb *dequeue_temp;
890 int num_trbs_free_temp;
891 bool revert = false;
892
893 num_trbs_free_temp = ep_ring->num_trbs_free;
894 dequeue_temp = ep_ring->dequeue;
895
Sarah Sharpde85cab2012-06-21 16:28:30 -0700896 /* If we get two back-to-back stalls, and the first stalled transfer
897 * ends just before a link TRB, the dequeue pointer will be left on
898 * the link TRB by the code in the while loop. So we have to update
899 * the dequeue pointer one segment further, or we'll jump off
900 * the segment into la-la-land.
901 */
902 if (last_trb(xhci, ep_ring, ep_ring->deq_seg, ep_ring->dequeue)) {
903 ep_ring->deq_seg = ep_ring->deq_seg->next;
904 ep_ring->dequeue = ep_ring->deq_seg->trbs;
905 }
906
Andiry Xub008df62012-03-05 17:49:34 +0800907 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) {
908 /* We have more usable TRBs */
909 ep_ring->num_trbs_free++;
910 ep_ring->dequeue++;
911 if (last_trb(xhci, ep_ring, ep_ring->deq_seg,
912 ep_ring->dequeue)) {
913 if (ep_ring->dequeue ==
914 dev->eps[ep_index].queued_deq_ptr)
915 break;
916 ep_ring->deq_seg = ep_ring->deq_seg->next;
917 ep_ring->dequeue = ep_ring->deq_seg->trbs;
918 }
919 if (ep_ring->dequeue == dequeue_temp) {
920 revert = true;
921 break;
922 }
923 }
924
925 if (revert) {
926 xhci_dbg(xhci, "Unable to find new dequeue pointer\n");
927 ep_ring->num_trbs_free = num_trbs_free_temp;
928 }
929}
930
Sarah Sharpae636742009-04-29 19:02:31 -0700931/*
932 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
933 * we need to clear the set deq pending flag in the endpoint ring state, so that
934 * the TD queueing code can ring the doorbell again. We also need to ring the
935 * endpoint doorbell to restart the ring, but only if there aren't more
936 * cancellations pending.
937 */
938static void handle_set_deq_completion(struct xhci_hcd *xhci,
939 struct xhci_event_cmd *event,
940 union xhci_trb *trb)
941{
942 unsigned int slot_id;
943 unsigned int ep_index;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700944 unsigned int stream_id;
Sarah Sharpae636742009-04-29 19:02:31 -0700945 struct xhci_ring *ep_ring;
946 struct xhci_virt_device *dev;
John Yound115b042009-07-27 12:05:15 -0700947 struct xhci_ep_ctx *ep_ctx;
948 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpae636742009-04-29 19:02:31 -0700949
Matt Evans28ccd292011-03-29 13:40:46 +1100950 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
951 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
952 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
Sarah Sharpae636742009-04-29 19:02:31 -0700953 dev = xhci->devs[slot_id];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700954
955 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
956 if (!ep_ring) {
957 xhci_warn(xhci, "WARN Set TR deq ptr command for "
958 "freed stream ID %u\n",
959 stream_id);
960 /* XXX: Harmless??? */
961 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
962 return;
963 }
964
John Yound115b042009-07-27 12:05:15 -0700965 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
966 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
Sarah Sharpae636742009-04-29 19:02:31 -0700967
Matt Evans28ccd292011-03-29 13:40:46 +1100968 if (GET_COMP_CODE(le32_to_cpu(event->status)) != COMP_SUCCESS) {
Sarah Sharpae636742009-04-29 19:02:31 -0700969 unsigned int ep_state;
970 unsigned int slot_state;
971
Matt Evans28ccd292011-03-29 13:40:46 +1100972 switch (GET_COMP_CODE(le32_to_cpu(event->status))) {
Sarah Sharpae636742009-04-29 19:02:31 -0700973 case COMP_TRB_ERR:
974 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
975 "of stream ID configuration\n");
976 break;
977 case COMP_CTX_STATE:
978 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
979 "to incorrect slot or ep state.\n");
Matt Evans28ccd292011-03-29 13:40:46 +1100980 ep_state = le32_to_cpu(ep_ctx->ep_info);
Sarah Sharpae636742009-04-29 19:02:31 -0700981 ep_state &= EP_STATE_MASK;
Matt Evans28ccd292011-03-29 13:40:46 +1100982 slot_state = le32_to_cpu(slot_ctx->dev_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700983 slot_state = GET_SLOT_STATE(slot_state);
984 xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
985 slot_state, ep_state);
986 break;
987 case COMP_EBADSLT:
988 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
989 "slot %u was not enabled.\n", slot_id);
990 break;
991 default:
992 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
993 "completion code of %u.\n",
Matt Evans28ccd292011-03-29 13:40:46 +1100994 GET_COMP_CODE(le32_to_cpu(event->status)));
Sarah Sharpae636742009-04-29 19:02:31 -0700995 break;
996 }
997 /* OK what do we do now? The endpoint state is hosed, and we
998 * should never get to this point if the synchronization between
999 * queueing, and endpoint state are correct. This might happen
1000 * if the device gets disconnected after we've finished
1001 * cancelling URBs, which might not be an error...
1002 */
1003 } else {
Sarah Sharp8e595a52009-07-27 12:03:31 -07001004 xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001005 le64_to_cpu(ep_ctx->deq));
Sarah Sharpbf161e82011-02-23 15:46:42 -08001006 if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg,
Matt Evans28ccd292011-03-29 13:40:46 +11001007 dev->eps[ep_index].queued_deq_ptr) ==
1008 (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) {
Sarah Sharpbf161e82011-02-23 15:46:42 -08001009 /* Update the ring's dequeue segment and dequeue pointer
1010 * to reflect the new position.
1011 */
Andiry Xub008df62012-03-05 17:49:34 +08001012 update_ring_for_set_deq_completion(xhci, dev,
1013 ep_ring, ep_index);
Sarah Sharpbf161e82011-02-23 15:46:42 -08001014 } else {
1015 xhci_warn(xhci, "Mismatch between completed Set TR Deq "
1016 "Ptr command & xHCI internal state.\n");
1017 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
1018 dev->eps[ep_index].queued_deq_seg,
1019 dev->eps[ep_index].queued_deq_ptr);
1020 }
Sarah Sharpae636742009-04-29 19:02:31 -07001021 }
1022
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001023 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
Sarah Sharpbf161e82011-02-23 15:46:42 -08001024 dev->eps[ep_index].queued_deq_seg = NULL;
1025 dev->eps[ep_index].queued_deq_ptr = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001026 /* Restart any rings with pending URBs */
1027 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07001028}
1029
Sarah Sharpa1587d92009-07-27 12:03:15 -07001030static void handle_reset_ep_completion(struct xhci_hcd *xhci,
1031 struct xhci_event_cmd *event,
1032 union xhci_trb *trb)
1033{
1034 int slot_id;
1035 unsigned int ep_index;
1036
Matt Evans28ccd292011-03-29 13:40:46 +11001037 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
1038 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharpa1587d92009-07-27 12:03:15 -07001039 /* This command will only fail if the endpoint wasn't halted,
1040 * but we don't care.
1041 */
1042 xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
Matt Evansf5960b62011-06-01 10:22:55 +10001043 GET_COMP_CODE(le32_to_cpu(event->status)));
Sarah Sharpa1587d92009-07-27 12:03:15 -07001044
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001045 /* HW with the reset endpoint quirk needs to have a configure endpoint
1046 * command complete before the endpoint can be used. Queue that here
1047 * because the HW can't handle two commands being queued in a row.
1048 */
1049 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
1050 xhci_dbg(xhci, "Queueing configure endpoint command\n");
1051 xhci_queue_configure_endpoint(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001052 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1053 false);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001054 xhci_ring_cmd_db(xhci);
1055 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001056 /* Clear our internal halted state and restart the ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001057 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001058 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001059 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001060}
Sarah Sharpae636742009-04-29 19:02:31 -07001061
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001062/* Check to see if a command in the device's command queue matches this one.
1063 * Signal the completion or free the command, and return 1. Return 0 if the
1064 * completed command isn't at the head of the command list.
1065 */
1066static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
1067 struct xhci_virt_device *virt_dev,
1068 struct xhci_event_cmd *event)
1069{
1070 struct xhci_command *command;
1071
1072 if (list_empty(&virt_dev->cmd_list))
1073 return 0;
1074
1075 command = list_entry(virt_dev->cmd_list.next,
1076 struct xhci_command, cmd_list);
1077 if (xhci->cmd_ring->dequeue != command->command_trb)
1078 return 0;
1079
Matt Evans28ccd292011-03-29 13:40:46 +11001080 command->status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001081 list_del(&command->cmd_list);
1082 if (command->completion)
1083 complete(command->completion);
1084 else
1085 xhci_free_command(xhci, command);
1086 return 1;
1087}
1088
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001089static void handle_cmd_completion(struct xhci_hcd *xhci,
1090 struct xhci_event_cmd *event)
1091{
Matt Evans28ccd292011-03-29 13:40:46 +11001092 int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001093 u64 cmd_dma;
1094 dma_addr_t cmd_dequeue_dma;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001095 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001096 struct xhci_virt_device *virt_dev;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001097 unsigned int ep_index;
1098 struct xhci_ring *ep_ring;
1099 unsigned int ep_state;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001100
Matt Evans28ccd292011-03-29 13:40:46 +11001101 cmd_dma = le64_to_cpu(event->cmd_trb);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001102 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001103 xhci->cmd_ring->dequeue);
1104 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1105 if (cmd_dequeue_dma == 0) {
1106 xhci->error_bitmask |= 1 << 4;
1107 return;
1108 }
1109 /* Does the DMA address match our internal dequeue pointer address? */
1110 if (cmd_dma != (u64) cmd_dequeue_dma) {
1111 xhci->error_bitmask |= 1 << 5;
1112 return;
1113 }
Matt Evans28ccd292011-03-29 13:40:46 +11001114 switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])
1115 & TRB_TYPE_BITMASK) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001116 case TRB_TYPE(TRB_ENABLE_SLOT):
Matt Evans28ccd292011-03-29 13:40:46 +11001117 if (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_SUCCESS)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001118 xhci->slot_id = slot_id;
1119 else
1120 xhci->slot_id = 0;
1121 complete(&xhci->addr_dev);
1122 break;
1123 case TRB_TYPE(TRB_DISABLE_SLOT):
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001124 if (xhci->devs[slot_id]) {
1125 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1126 /* Delete default control endpoint resources */
1127 xhci_free_device_endpoint_resources(xhci,
1128 xhci->devs[slot_id], true);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001129 xhci_free_virt_device(xhci, slot_id);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001130 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001131 break;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001132 case TRB_TYPE(TRB_CONFIG_EP):
Sarah Sharp913a8a32009-09-04 10:53:13 -07001133 virt_dev = xhci->devs[slot_id];
Sarah Sharpa50c8aa2009-09-04 10:53:15 -07001134 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
Sarah Sharp913a8a32009-09-04 10:53:13 -07001135 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001136 /*
1137 * Configure endpoint commands can come from the USB core
1138 * configuration or alt setting changes, or because the HW
1139 * needed an extra configure endpoint command after a reset
Sarah Sharp8df75f42010-04-02 15:34:16 -07001140 * endpoint command or streams were being configured.
1141 * If the command was for a halted endpoint, the xHCI driver
1142 * is not waiting on the configure endpoint command.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001143 */
1144 ctrl_ctx = xhci_get_input_control_ctx(xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001145 virt_dev->in_ctx);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001146 /* Input ctx add_flags are the endpoint index plus one */
Matt Evans28ccd292011-03-29 13:40:46 +11001147 ep_index = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags)) - 1;
Sarah Sharp06df5722009-12-03 09:44:31 -08001148 /* A usb_set_interface() call directly after clearing a halted
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001149 * condition may race on this quirky hardware. Not worth
1150 * worrying about, since this is prototype hardware. Not sure
1151 * if this will work for streams, but streams support was
1152 * untested on this prototype.
Sarah Sharp06df5722009-12-03 09:44:31 -08001153 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001154 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
Sarah Sharp06df5722009-12-03 09:44:31 -08001155 ep_index != (unsigned int) -1 &&
Matt Evans28ccd292011-03-29 13:40:46 +11001156 le32_to_cpu(ctrl_ctx->add_flags) - SLOT_FLAG ==
1157 le32_to_cpu(ctrl_ctx->drop_flags)) {
Sarah Sharp06df5722009-12-03 09:44:31 -08001158 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
1159 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1160 if (!(ep_state & EP_HALTED))
1161 goto bandwidth_change;
1162 xhci_dbg(xhci, "Completed config ep cmd - "
1163 "last ep index = %d, state = %d\n",
1164 ep_index, ep_state);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001165 /* Clear internal halted state and restart ring(s) */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001166 xhci->devs[slot_id]->eps[ep_index].ep_state &=
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001167 ~EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001168 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharp06df5722009-12-03 09:44:31 -08001169 break;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001170 }
Sarah Sharp06df5722009-12-03 09:44:31 -08001171bandwidth_change:
1172 xhci_dbg(xhci, "Completed config ep cmd\n");
1173 xhci->devs[slot_id]->cmd_status =
Matt Evans28ccd292011-03-29 13:40:46 +11001174 GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp06df5722009-12-03 09:44:31 -08001175 complete(&xhci->devs[slot_id]->cmd_completion);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001176 break;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001177 case TRB_TYPE(TRB_EVAL_CONTEXT):
Sarah Sharpac1c1b72009-09-04 10:53:20 -07001178 virt_dev = xhci->devs[slot_id];
1179 if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
1180 break;
Matt Evans28ccd292011-03-29 13:40:46 +11001181 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001182 complete(&xhci->devs[slot_id]->cmd_completion);
1183 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001184 case TRB_TYPE(TRB_ADDR_DEV):
Matt Evans28ccd292011-03-29 13:40:46 +11001185 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001186 complete(&xhci->addr_dev);
1187 break;
Sarah Sharpae636742009-04-29 19:02:31 -07001188 case TRB_TYPE(TRB_STOP_RING):
Andiry Xube88fe42010-10-14 07:22:57 -07001189 handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event);
Sarah Sharpae636742009-04-29 19:02:31 -07001190 break;
1191 case TRB_TYPE(TRB_SET_DEQ):
1192 handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
1193 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001194 case TRB_TYPE(TRB_CMD_NOOP):
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001195 break;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001196 case TRB_TYPE(TRB_RESET_EP):
1197 handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
1198 break;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001199 case TRB_TYPE(TRB_RESET_DEV):
1200 xhci_dbg(xhci, "Completed reset device command.\n");
1201 slot_id = TRB_TO_SLOT_ID(
Matt Evans28ccd292011-03-29 13:40:46 +11001202 le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001203 virt_dev = xhci->devs[slot_id];
1204 if (virt_dev)
1205 handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
1206 else
1207 xhci_warn(xhci, "Reset device command completion "
1208 "for disabled slot %u\n", slot_id);
1209 break;
Sarah Sharp02386342010-05-24 13:25:28 -07001210 case TRB_TYPE(TRB_NEC_GET_FW):
1211 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1212 xhci->error_bitmask |= 1 << 6;
1213 break;
1214 }
1215 xhci_dbg(xhci, "NEC firmware version %2x.%02x\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001216 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1217 NEC_FW_MINOR(le32_to_cpu(event->status)));
Sarah Sharp02386342010-05-24 13:25:28 -07001218 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001219 default:
1220 /* Skip over unknown commands on the event ring */
1221 xhci->error_bitmask |= 1 << 6;
1222 break;
1223 }
Andiry Xu3b72fca2012-03-05 17:49:32 +08001224 inc_deq(xhci, xhci->cmd_ring);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001225}
1226
Sarah Sharp02386342010-05-24 13:25:28 -07001227static void handle_vendor_event(struct xhci_hcd *xhci,
1228 union xhci_trb *event)
1229{
1230 u32 trb_type;
1231
Matt Evans28ccd292011-03-29 13:40:46 +11001232 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
Sarah Sharp02386342010-05-24 13:25:28 -07001233 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1234 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1235 handle_cmd_completion(xhci, &event->event_cmd);
1236}
1237
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001238/* @port_id: the one-based port ID from the hardware (indexed from array of all
1239 * port registers -- USB 3.0 and USB 2.0).
1240 *
1241 * Returns a zero-based port number, which is suitable for indexing into each of
1242 * the split roothubs' port arrays and bus state arrays.
Sarah Sharpd0cd5d42011-11-14 17:51:39 -08001243 * Add one to it in order to call xhci_find_slot_id_by_port.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001244 */
1245static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1246 struct xhci_hcd *xhci, u32 port_id)
1247{
1248 unsigned int i;
1249 unsigned int num_similar_speed_ports = 0;
1250
1251 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1252 * and usb2_ports are 0-based indexes. Count the number of similar
1253 * speed ports, up to 1 port before this port.
1254 */
1255 for (i = 0; i < (port_id - 1); i++) {
1256 u8 port_speed = xhci->port_array[i];
1257
1258 /*
1259 * Skip ports that don't have known speeds, or have duplicate
1260 * Extended Capabilities port speed entries.
1261 */
Dan Carpenter22e04872011-03-17 22:39:49 +03001262 if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001263 continue;
1264
1265 /*
1266 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1267 * 1.1 ports are under the USB 2.0 hub. If the port speed
1268 * matches the device speed, it's a similar speed port.
1269 */
1270 if ((port_speed == 0x03) == (hcd->speed == HCD_USB3))
1271 num_similar_speed_ports++;
1272 }
1273 return num_similar_speed_ports;
1274}
1275
Sarah Sharp623bef92011-11-11 14:57:33 -08001276static void handle_device_notification(struct xhci_hcd *xhci,
1277 union xhci_trb *event)
1278{
1279 u32 slot_id;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001280 struct usb_device *udev;
Sarah Sharp623bef92011-11-11 14:57:33 -08001281
1282 slot_id = TRB_TO_SLOT_ID(event->generic.field[3]);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001283 if (!xhci->devs[slot_id]) {
Sarah Sharp623bef92011-11-11 14:57:33 -08001284 xhci_warn(xhci, "Device Notification event for "
1285 "unused slot %u\n", slot_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001286 return;
1287 }
1288
1289 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
1290 slot_id);
1291 udev = xhci->devs[slot_id]->udev;
1292 if (udev && udev->parent)
1293 usb_wakeup_notification(udev->parent, udev->portnum);
Sarah Sharp623bef92011-11-11 14:57:33 -08001294}
1295
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001296static void handle_port_status(struct xhci_hcd *xhci,
1297 union xhci_trb *event)
1298{
Neeti Desai350710e2012-11-20 15:30:34 -08001299 struct usb_hcd *hcd = NULL;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001300 u32 port_id;
Andiry Xu56192532010-10-14 07:23:00 -07001301 u32 temp, temp1;
Sarah Sharp518e8482010-12-15 11:56:29 -08001302 int max_ports;
Andiry Xu56192532010-10-14 07:23:00 -07001303 int slot_id;
Sarah Sharp5308a912010-12-01 11:34:59 -08001304 unsigned int faked_port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001305 u8 major_revision;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001306 struct xhci_bus_state *bus_state;
Matt Evans28ccd292011-03-29 13:40:46 +11001307 __le32 __iomem **port_array;
Sarah Sharp386139d2011-03-24 08:02:58 -07001308 bool bogus_port_status = false;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001309
1310 /* Port status change events always have a successful completion code */
Matt Evans28ccd292011-03-29 13:40:46 +11001311 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001312 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1313 xhci->error_bitmask |= 1 << 8;
1314 }
Matt Evans28ccd292011-03-29 13:40:46 +11001315 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001316 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1317
Sarah Sharp518e8482010-12-15 11:56:29 -08001318 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1319 if ((port_id <= 0) || (port_id > max_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001320 xhci_warn(xhci, "Invalid port id %d\n", port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001321 bogus_port_status = true;
Andiry Xu56192532010-10-14 07:23:00 -07001322 goto cleanup;
1323 }
1324
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001325 /* Figure out which usb_hcd this port is attached to:
1326 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1327 */
1328 major_revision = xhci->port_array[port_id - 1];
1329 if (major_revision == 0) {
1330 xhci_warn(xhci, "Event for port %u not in "
1331 "Extended Capabilities, ignoring.\n",
1332 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001333 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001334 goto cleanup;
1335 }
Dan Carpenter22e04872011-03-17 22:39:49 +03001336 if (major_revision == DUPLICATE_ENTRY) {
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001337 xhci_warn(xhci, "Event for port %u duplicated in"
1338 "Extended Capabilities, ignoring.\n",
1339 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001340 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001341 goto cleanup;
Sarah Sharp5308a912010-12-01 11:34:59 -08001342 }
1343
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001344 /*
1345 * Hardware port IDs reported by a Port Status Change Event include USB
1346 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1347 * resume event, but we first need to translate the hardware port ID
1348 * into the index into the ports on the correct split roothub, and the
1349 * correct bus_state structure.
1350 */
1351 /* Find the right roothub. */
1352 hcd = xhci_to_hcd(xhci);
Neeti Desai350710e2012-11-20 15:30:34 -08001353 if (!hcd)
1354 goto cleanup;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001355 if ((major_revision == 0x03) != (hcd->speed == HCD_USB3))
1356 hcd = xhci->shared_hcd;
1357 bus_state = &xhci->bus_state[hcd_index(hcd)];
1358 if (hcd->speed == HCD_USB3)
1359 port_array = xhci->usb3_ports;
1360 else
1361 port_array = xhci->usb2_ports;
1362 /* Find the faked port hub number */
1363 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1364 port_id);
1365
Sarah Sharp5308a912010-12-01 11:34:59 -08001366 temp = xhci_readl(xhci, port_array[faked_port_index]);
Sarah Sharp7111ebc2010-12-14 13:24:55 -08001367 if (hcd->state == HC_STATE_SUSPENDED) {
Andiry Xu56192532010-10-14 07:23:00 -07001368 xhci_dbg(xhci, "resume root hub\n");
1369 usb_hcd_resume_root_hub(hcd);
1370 }
1371
1372 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1373 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1374
1375 temp1 = xhci_readl(xhci, &xhci->op_regs->command);
1376 if (!(temp1 & CMD_RUN)) {
1377 xhci_warn(xhci, "xHC is not running.\n");
1378 goto cleanup;
1379 }
1380
1381 if (DEV_SUPERSPEED(temp)) {
Sarah Sharpd93814c2012-01-24 16:39:02 -08001382 xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001383 /* Set a flag to say the port signaled remote wakeup,
1384 * so we can tell the difference between the end of
1385 * device and host initiated resume.
1386 */
1387 bus_state->port_remote_wakeup |= 1 << faked_port_index;
Sarah Sharpd93814c2012-01-24 16:39:02 -08001388 xhci_test_and_clear_bit(xhci, port_array,
1389 faked_port_index, PORT_PLC);
Andiry Xuc9682df2011-09-23 14:19:48 -07001390 xhci_set_link_state(xhci, port_array, faked_port_index,
1391 XDEV_U0);
Sarah Sharpd93814c2012-01-24 16:39:02 -08001392 /* Need to wait until the next link state change
1393 * indicates the device is actually in U0.
1394 */
1395 bogus_port_status = true;
1396 goto cleanup;
Andiry Xu56192532010-10-14 07:23:00 -07001397 } else {
1398 xhci_dbg(xhci, "resume HS port %d\n", port_id);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001399 bus_state->resume_done[faked_port_index] = jiffies +
Andiry Xu56192532010-10-14 07:23:00 -07001400 msecs_to_jiffies(20);
1401 mod_timer(&hcd->rh_timer,
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001402 bus_state->resume_done[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001403 /* Do the rest in GetPortStatus */
1404 }
1405 }
1406
Sarah Sharpd93814c2012-01-24 16:39:02 -08001407 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_U0 &&
1408 DEV_SUPERSPEED(temp)) {
1409 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001410 /* We've just brought the device into U0 through either the
1411 * Resume state after a device remote wakeup, or through the
1412 * U3Exit state after a host-initiated resume. If it's a device
1413 * initiated remote wake, don't pass up the link state change,
1414 * so the roothub behavior is consistent with external
1415 * USB 3.0 hub behavior.
1416 */
Sarah Sharpd93814c2012-01-24 16:39:02 -08001417 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1418 faked_port_index + 1);
1419 if (slot_id && xhci->devs[slot_id])
1420 xhci_ring_device(xhci, slot_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001421 if (bus_state->port_remote_wakeup && (1 << faked_port_index)) {
1422 bus_state->port_remote_wakeup &=
1423 ~(1 << faked_port_index);
1424 xhci_test_and_clear_bit(xhci, port_array,
1425 faked_port_index, PORT_PLC);
1426 usb_wakeup_notification(hcd->self.root_hub,
1427 faked_port_index + 1);
1428 bogus_port_status = true;
1429 goto cleanup;
1430 }
Sarah Sharpd93814c2012-01-24 16:39:02 -08001431 }
1432
Andiry Xu6fd45622011-09-23 14:19:50 -07001433 if (hcd->speed != HCD_USB3)
1434 xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
1435 PORT_PLC);
1436
Andiry Xu56192532010-10-14 07:23:00 -07001437cleanup:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001438 /* Update event ring dequeue pointer before dropping the lock */
Andiry Xu3b72fca2012-03-05 17:49:32 +08001439 inc_deq(xhci, xhci->event_ring);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001440
Sarah Sharp386139d2011-03-24 08:02:58 -07001441 /* Don't make the USB core poll the roothub if we got a bad port status
1442 * change event. Besides, at that point we can't tell which roothub
1443 * (USB 2.0 or USB 3.0) to kick.
1444 */
1445 if (bogus_port_status)
1446 return;
1447
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001448 spin_unlock(&xhci->lock);
1449 /* Pass this up to the core */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001450 usb_hcd_poll_rh_status(hcd);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001451 spin_lock(&xhci->lock);
1452}
1453
1454/*
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001455 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1456 * at end_trb, which may be in another segment. If the suspect DMA address is a
1457 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1458 * returns 0.
1459 */
Sarah Sharp6648f292009-11-09 13:35:23 -08001460struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001461 union xhci_trb *start_trb,
1462 union xhci_trb *end_trb,
1463 dma_addr_t suspect_dma)
1464{
1465 dma_addr_t start_dma;
1466 dma_addr_t end_seg_dma;
1467 dma_addr_t end_trb_dma;
1468 struct xhci_segment *cur_seg;
1469
Sarah Sharp23e3be12009-04-29 19:05:20 -07001470 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001471 cur_seg = start_seg;
1472
1473 do {
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001474 if (start_dma == 0)
Randy Dunlap326b4812010-04-19 08:53:50 -07001475 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -07001476 /* We may get an event for a Link TRB in the middle of a TD */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001477 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001478 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001479 /* If the end TRB isn't in this segment, this is set to 0 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001480 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001481
1482 if (end_trb_dma > 0) {
1483 /* The end TRB is in this segment, so suspect should be here */
1484 if (start_dma <= end_trb_dma) {
1485 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1486 return cur_seg;
1487 } else {
1488 /* Case for one segment with
1489 * a TD wrapped around to the top
1490 */
1491 if ((suspect_dma >= start_dma &&
1492 suspect_dma <= end_seg_dma) ||
1493 (suspect_dma >= cur_seg->dma &&
1494 suspect_dma <= end_trb_dma))
1495 return cur_seg;
1496 }
Randy Dunlap326b4812010-04-19 08:53:50 -07001497 return NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001498 } else {
1499 /* Might still be somewhere in this segment */
1500 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1501 return cur_seg;
1502 }
1503 cur_seg = cur_seg->next;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001504 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001505 } while (cur_seg != start_seg);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001506
Randy Dunlap326b4812010-04-19 08:53:50 -07001507 return NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001508}
1509
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001510static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1511 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001512 unsigned int stream_id,
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001513 struct xhci_td *td, union xhci_trb *event_trb)
1514{
1515 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
1516 ep->ep_state |= EP_HALTED;
1517 ep->stopped_td = td;
1518 ep->stopped_trb = event_trb;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001519 ep->stopped_stream = stream_id;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001520
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001521 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1522 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
Sarah Sharp1624ae12010-05-06 13:40:08 -07001523
1524 ep->stopped_td = NULL;
1525 ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001526 ep->stopped_stream = 0;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001527
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001528 xhci_ring_cmd_db(xhci);
1529}
1530
1531/* Check if an error has halted the endpoint ring. The class driver will
1532 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1533 * However, a babble and other errors also halt the endpoint ring, and the class
1534 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1535 * Ring Dequeue Pointer command manually.
1536 */
1537static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1538 struct xhci_ep_ctx *ep_ctx,
1539 unsigned int trb_comp_code)
1540{
1541 /* TRB completion codes that may require a manual halt cleanup */
1542 if (trb_comp_code == COMP_TX_ERR ||
1543 trb_comp_code == COMP_BABBLE ||
1544 trb_comp_code == COMP_SPLIT_ERR)
1545 /* The 0.96 spec says a babbling control endpoint
1546 * is not halted. The 0.96 spec says it is. Some HW
1547 * claims to be 0.95 compliant, but it halts the control
1548 * endpoint anyway. Check if a babble halted the
1549 * endpoint.
1550 */
Matt Evansf5960b62011-06-01 10:22:55 +10001551 if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1552 cpu_to_le32(EP_STATE_HALTED))
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001553 return 1;
1554
1555 return 0;
1556}
1557
Sarah Sharpb45b5062009-12-09 15:59:06 -08001558int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1559{
1560 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1561 /* Vendor defined "informational" completion code,
1562 * treat as not-an-error.
1563 */
1564 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1565 trb_comp_code);
1566 xhci_dbg(xhci, "Treating code as success.\n");
1567 return 1;
1568 }
1569 return 0;
1570}
1571
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001572/*
Andiry Xu4422da62010-07-22 15:22:55 -07001573 * Finish the td processing, remove the td from td list;
1574 * Return 1 if the urb can be given back.
1575 */
1576static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1577 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1578 struct xhci_virt_ep *ep, int *status, bool skip)
1579{
1580 struct xhci_virt_device *xdev;
1581 struct xhci_ring *ep_ring;
1582 unsigned int slot_id;
1583 int ep_index;
1584 struct urb *urb = NULL;
1585 struct xhci_ep_ctx *ep_ctx;
1586 int ret = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001587 struct urb_priv *urb_priv;
Andiry Xu4422da62010-07-22 15:22:55 -07001588 u32 trb_comp_code;
1589
Matt Evans28ccd292011-03-29 13:40:46 +11001590 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu4422da62010-07-22 15:22:55 -07001591 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001592 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1593 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu4422da62010-07-22 15:22:55 -07001594 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001595 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu4422da62010-07-22 15:22:55 -07001596
1597 if (skip)
1598 goto td_cleanup;
1599
1600 if (trb_comp_code == COMP_STOP_INVAL ||
1601 trb_comp_code == COMP_STOP) {
1602 /* The Endpoint Stop Command completion will take care of any
1603 * stopped TDs. A stopped TD may be restarted, so don't update
1604 * the ring dequeue pointer or take this TD off any lists yet.
1605 */
1606 ep->stopped_td = td;
1607 ep->stopped_trb = event_trb;
1608 return 0;
1609 } else {
1610 if (trb_comp_code == COMP_STALL) {
1611 /* The transfer is completed from the driver's
1612 * perspective, but we need to issue a set dequeue
1613 * command for this stalled endpoint to move the dequeue
1614 * pointer past the TD. We can't do that here because
1615 * the halt condition must be cleared first. Let the
1616 * USB class driver clear the stall later.
1617 */
1618 ep->stopped_td = td;
1619 ep->stopped_trb = event_trb;
1620 ep->stopped_stream = ep_ring->stream_id;
1621 } else if (xhci_requires_manual_halt_cleanup(xhci,
1622 ep_ctx, trb_comp_code)) {
1623 /* Other types of errors halt the endpoint, but the
1624 * class driver doesn't call usb_reset_endpoint() unless
1625 * the error is -EPIPE. Clear the halted status in the
1626 * xHCI hardware manually.
1627 */
1628 xhci_cleanup_halted_endpoint(xhci,
1629 slot_id, ep_index, ep_ring->stream_id,
1630 td, event_trb);
1631 } else {
1632 /* Update ring dequeue pointer */
1633 while (ep_ring->dequeue != td->last_trb)
Andiry Xu3b72fca2012-03-05 17:49:32 +08001634 inc_deq(xhci, ep_ring);
1635 inc_deq(xhci, ep_ring);
Andiry Xu4422da62010-07-22 15:22:55 -07001636 }
1637
1638td_cleanup:
1639 /* Clean up the endpoint's TD list */
1640 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001641 urb_priv = urb->hcpriv;
Andiry Xu4422da62010-07-22 15:22:55 -07001642
1643 /* Do one last check of the actual transfer length.
1644 * If the host controller said we transferred more data than
1645 * the buffer length, urb->actual_length will be a very big
1646 * number (since it's unsigned). Play it safe and say we didn't
1647 * transfer anything.
1648 */
1649 if (urb->actual_length > urb->transfer_buffer_length) {
1650 xhci_warn(xhci, "URB transfer length is wrong, "
1651 "xHC issue? req. len = %u, "
1652 "act. len = %u\n",
1653 urb->transfer_buffer_length,
1654 urb->actual_length);
1655 urb->actual_length = 0;
1656 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1657 *status = -EREMOTEIO;
1658 else
1659 *status = 0;
1660 }
Sarah Sharp585df1d2011-08-02 15:43:40 -07001661 list_del_init(&td->td_list);
Andiry Xu4422da62010-07-22 15:22:55 -07001662 /* Was this TD slated to be cancelled but completed anyway? */
1663 if (!list_empty(&td->cancelled_td_list))
Sarah Sharp585df1d2011-08-02 15:43:40 -07001664 list_del_init(&td->cancelled_td_list);
Andiry Xu4422da62010-07-22 15:22:55 -07001665
Andiry Xu8e51adc2010-07-22 15:23:31 -07001666 urb_priv->td_cnt++;
1667 /* Giveback the urb when all the tds are completed */
Andiry Xuc41136b2011-03-22 17:08:14 +08001668 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001669 ret = 1;
Andiry Xuc41136b2011-03-22 17:08:14 +08001670 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1671 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
1672 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs
1673 == 0) {
1674 if (xhci->quirks & XHCI_AMD_PLL_FIX)
1675 usb_amd_quirk_pll_enable();
1676 }
1677 }
1678 }
Andiry Xu4422da62010-07-22 15:22:55 -07001679 }
1680
1681 return ret;
1682}
1683
1684/*
Andiry Xu8af56be2010-07-22 15:23:03 -07001685 * Process control tds, update urb status and actual_length.
1686 */
1687static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1688 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1689 struct xhci_virt_ep *ep, int *status)
1690{
1691 struct xhci_virt_device *xdev;
1692 struct xhci_ring *ep_ring;
1693 unsigned int slot_id;
1694 int ep_index;
1695 struct xhci_ep_ctx *ep_ctx;
1696 u32 trb_comp_code;
1697
Matt Evans28ccd292011-03-29 13:40:46 +11001698 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu8af56be2010-07-22 15:23:03 -07001699 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001700 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1701 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu8af56be2010-07-22 15:23:03 -07001702 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001703 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001704
Andiry Xu8af56be2010-07-22 15:23:03 -07001705 switch (trb_comp_code) {
1706 case COMP_SUCCESS:
1707 if (event_trb == ep_ring->dequeue) {
1708 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1709 "without IOC set??\n");
1710 *status = -ESHUTDOWN;
1711 } else if (event_trb != td->last_trb) {
1712 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1713 "without IOC set??\n");
1714 *status = -ESHUTDOWN;
1715 } else {
Andiry Xu8af56be2010-07-22 15:23:03 -07001716 *status = 0;
1717 }
1718 break;
1719 case COMP_SHORT_TX:
Andiry Xu8af56be2010-07-22 15:23:03 -07001720 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1721 *status = -EREMOTEIO;
1722 else
1723 *status = 0;
1724 break;
Sarah Sharp3abeca92011-05-05 19:08:09 -07001725 case COMP_STOP_INVAL:
1726 case COMP_STOP:
1727 return finish_td(xhci, td, event_trb, event, ep, status, false);
Andiry Xu8af56be2010-07-22 15:23:03 -07001728 default:
1729 if (!xhci_requires_manual_halt_cleanup(xhci,
1730 ep_ctx, trb_comp_code))
1731 break;
1732 xhci_dbg(xhci, "TRB error code %u, "
1733 "halted endpoint index = %u\n",
1734 trb_comp_code, ep_index);
1735 /* else fall through */
1736 case COMP_STALL:
1737 /* Did we transfer part of the data (middle) phase? */
1738 if (event_trb != ep_ring->dequeue &&
1739 event_trb != td->last_trb)
1740 td->urb->actual_length =
1741 td->urb->transfer_buffer_length
Matt Evans28ccd292011-03-29 13:40:46 +11001742 - TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001743 else
1744 td->urb->actual_length = 0;
1745
1746 xhci_cleanup_halted_endpoint(xhci,
1747 slot_id, ep_index, 0, td, event_trb);
1748 return finish_td(xhci, td, event_trb, event, ep, status, true);
1749 }
1750 /*
1751 * Did we transfer any data, despite the errors that might have
1752 * happened? I.e. did we get past the setup stage?
1753 */
1754 if (event_trb != ep_ring->dequeue) {
1755 /* The event was for the status stage */
1756 if (event_trb == td->last_trb) {
1757 if (td->urb->actual_length != 0) {
1758 /* Don't overwrite a previously set error code
1759 */
1760 if ((*status == -EINPROGRESS || *status == 0) &&
1761 (td->urb->transfer_flags
1762 & URB_SHORT_NOT_OK))
1763 /* Did we already see a short data
1764 * stage? */
1765 *status = -EREMOTEIO;
1766 } else {
1767 td->urb->actual_length =
1768 td->urb->transfer_buffer_length;
1769 }
1770 } else {
1771 /* Maybe the event was for the data stage? */
Sarah Sharp3abeca92011-05-05 19:08:09 -07001772 td->urb->actual_length =
1773 td->urb->transfer_buffer_length -
1774 TRB_LEN(le32_to_cpu(event->transfer_len));
1775 xhci_dbg(xhci, "Waiting for status "
1776 "stage event\n");
1777 return 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07001778 }
1779 }
1780
1781 return finish_td(xhci, td, event_trb, event, ep, status, false);
1782}
1783
1784/*
Andiry Xu04e51902010-07-22 15:23:39 -07001785 * Process isochronous tds, update urb packet status and actual_length.
1786 */
1787static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1788 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1789 struct xhci_virt_ep *ep, int *status)
1790{
1791 struct xhci_ring *ep_ring;
1792 struct urb_priv *urb_priv;
1793 int idx;
1794 int len = 0;
Andiry Xu04e51902010-07-22 15:23:39 -07001795 union xhci_trb *cur_trb;
1796 struct xhci_segment *cur_seg;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001797 struct usb_iso_packet_descriptor *frame;
Andiry Xu04e51902010-07-22 15:23:39 -07001798 u32 trb_comp_code;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001799 bool skip_td = false;
Andiry Xu04e51902010-07-22 15:23:39 -07001800
Matt Evans28ccd292011-03-29 13:40:46 +11001801 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1802 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07001803 urb_priv = td->urb->hcpriv;
1804 idx = urb_priv->td_cnt;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001805 frame = &td->urb->iso_frame_desc[idx];
Andiry Xu04e51902010-07-22 15:23:39 -07001806
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001807 /* handle completion code */
1808 switch (trb_comp_code) {
1809 case COMP_SUCCESS:
1810 frame->status = 0;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001811 break;
1812 case COMP_SHORT_TX:
1813 frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
1814 -EREMOTEIO : 0;
1815 break;
1816 case COMP_BW_OVER:
1817 frame->status = -ECOMM;
1818 skip_td = true;
1819 break;
1820 case COMP_BUFF_OVER:
1821 case COMP_BABBLE:
1822 frame->status = -EOVERFLOW;
1823 skip_td = true;
1824 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001825 case COMP_DEV_ERR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001826 case COMP_STALL:
1827 frame->status = -EPROTO;
1828 skip_td = true;
1829 break;
1830 case COMP_STOP:
1831 case COMP_STOP_INVAL:
1832 break;
1833 default:
1834 frame->status = -1;
1835 break;
Andiry Xu04e51902010-07-22 15:23:39 -07001836 }
1837
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001838 if (trb_comp_code == COMP_SUCCESS || skip_td) {
1839 frame->actual_length = frame->length;
1840 td->urb->actual_length += frame->length;
Andiry Xu04e51902010-07-22 15:23:39 -07001841 } else {
1842 for (cur_trb = ep_ring->dequeue,
1843 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
1844 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +10001845 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
1846 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
Matt Evans28ccd292011-03-29 13:40:46 +11001847 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu04e51902010-07-22 15:23:39 -07001848 }
Matt Evans28ccd292011-03-29 13:40:46 +11001849 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1850 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07001851
1852 if (trb_comp_code != COMP_STOP_INVAL) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001853 frame->actual_length = len;
Andiry Xu04e51902010-07-22 15:23:39 -07001854 td->urb->actual_length += len;
1855 }
1856 }
1857
Andiry Xu04e51902010-07-22 15:23:39 -07001858 return finish_td(xhci, td, event_trb, event, ep, status, false);
1859}
1860
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001861static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
1862 struct xhci_transfer_event *event,
1863 struct xhci_virt_ep *ep, int *status)
1864{
1865 struct xhci_ring *ep_ring;
1866 struct urb_priv *urb_priv;
1867 struct usb_iso_packet_descriptor *frame;
1868 int idx;
1869
Matt Evansf6975312011-06-01 13:01:01 +10001870 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001871 urb_priv = td->urb->hcpriv;
1872 idx = urb_priv->td_cnt;
1873 frame = &td->urb->iso_frame_desc[idx];
1874
Sarah Sharpb3df3f92011-06-15 19:57:46 -07001875 /* The transfer is partly done. */
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001876 frame->status = -EXDEV;
1877
1878 /* calc actual length */
1879 frame->actual_length = 0;
1880
1881 /* Update ring dequeue pointer */
1882 while (ep_ring->dequeue != td->last_trb)
Andiry Xu3b72fca2012-03-05 17:49:32 +08001883 inc_deq(xhci, ep_ring);
1884 inc_deq(xhci, ep_ring);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07001885
1886 return finish_td(xhci, td, NULL, event, ep, status, true);
1887}
1888
Andiry Xu04e51902010-07-22 15:23:39 -07001889/*
Andiry Xu22405ed2010-07-22 15:23:08 -07001890 * Process bulk and interrupt tds, update urb status and actual_length.
1891 */
1892static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
1893 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1894 struct xhci_virt_ep *ep, int *status)
1895{
1896 struct xhci_ring *ep_ring;
1897 union xhci_trb *cur_trb;
1898 struct xhci_segment *cur_seg;
1899 u32 trb_comp_code;
1900
Matt Evans28ccd292011-03-29 13:40:46 +11001901 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1902 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001903
1904 switch (trb_comp_code) {
1905 case COMP_SUCCESS:
1906 /* Double check that the HW transferred everything. */
1907 if (event_trb != td->last_trb) {
1908 xhci_warn(xhci, "WARN Successful completion "
1909 "on short TX\n");
1910 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1911 *status = -EREMOTEIO;
1912 else
1913 *status = 0;
1914 } else {
Andiry Xu22405ed2010-07-22 15:23:08 -07001915 *status = 0;
1916 }
1917 break;
1918 case COMP_SHORT_TX:
1919 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1920 *status = -EREMOTEIO;
1921 else
1922 *status = 0;
1923 break;
1924 default:
1925 /* Others already handled above */
1926 break;
1927 }
Sarah Sharpf444ff22011-04-05 15:53:47 -07001928 if (trb_comp_code == COMP_SHORT_TX)
1929 xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
1930 "%d bytes untransferred\n",
1931 td->urb->ep->desc.bEndpointAddress,
1932 td->urb->transfer_buffer_length,
1933 TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07001934 /* Fast path - was this the last TRB in the TD for this URB? */
1935 if (event_trb == td->last_trb) {
Matt Evans28ccd292011-03-29 13:40:46 +11001936 if (TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
Andiry Xu22405ed2010-07-22 15:23:08 -07001937 td->urb->actual_length =
1938 td->urb->transfer_buffer_length -
Matt Evans28ccd292011-03-29 13:40:46 +11001939 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001940 if (td->urb->transfer_buffer_length <
1941 td->urb->actual_length) {
1942 xhci_warn(xhci, "HC gave bad length "
1943 "of %d bytes left\n",
Matt Evans28ccd292011-03-29 13:40:46 +11001944 TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07001945 td->urb->actual_length = 0;
1946 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1947 *status = -EREMOTEIO;
1948 else
1949 *status = 0;
1950 }
1951 /* Don't overwrite a previously set error code */
1952 if (*status == -EINPROGRESS) {
1953 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1954 *status = -EREMOTEIO;
1955 else
1956 *status = 0;
1957 }
1958 } else {
1959 td->urb->actual_length =
1960 td->urb->transfer_buffer_length;
1961 /* Ignore a short packet completion if the
1962 * untransferred length was zero.
1963 */
1964 if (*status == -EREMOTEIO)
1965 *status = 0;
1966 }
1967 } else {
1968 /* Slow path - walk the list, starting from the dequeue
1969 * pointer, to get the actual length transferred.
1970 */
1971 td->urb->actual_length = 0;
1972 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
1973 cur_trb != event_trb;
1974 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +10001975 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
1976 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
Andiry Xu22405ed2010-07-22 15:23:08 -07001977 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11001978 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu22405ed2010-07-22 15:23:08 -07001979 }
1980 /* If the ring didn't stop on a Link or No-op TRB, add
1981 * in the actual bytes transferred from the Normal TRB
1982 */
1983 if (trb_comp_code != COMP_STOP_INVAL)
1984 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11001985 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
1986 TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07001987 }
1988
1989 return finish_td(xhci, td, event_trb, event, ep, status, false);
1990}
1991
1992/*
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001993 * If this function returns an error condition, it means it got a Transfer
1994 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
1995 * At this point, the host controller is probably hosed and should be reset.
1996 */
1997static int handle_tx_event(struct xhci_hcd *xhci,
1998 struct xhci_transfer_event *event)
1999{
2000 struct xhci_virt_device *xdev;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002001 struct xhci_virt_ep *ep;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002002 struct xhci_ring *ep_ring;
Sarah Sharp82d10092009-08-07 14:04:52 -07002003 unsigned int slot_id;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002004 int ep_index;
Randy Dunlap326b4812010-04-19 08:53:50 -07002005 struct xhci_td *td = NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002006 dma_addr_t event_dma;
2007 struct xhci_segment *event_seg;
2008 union xhci_trb *event_trb;
Randy Dunlap326b4812010-04-19 08:53:50 -07002009 struct urb *urb = NULL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002010 int status = -EINPROGRESS;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002011 struct urb_priv *urb_priv;
John Yound115b042009-07-27 12:05:15 -07002012 struct xhci_ep_ctx *ep_ctx;
Andiry Xuc2d7b492011-09-19 16:05:12 -07002013 struct list_head *tmp;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002014 u32 trb_comp_code;
Andiry Xu4422da62010-07-22 15:22:55 -07002015 int ret = 0;
Andiry Xuc2d7b492011-09-19 16:05:12 -07002016 int td_num = 0;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002017
Matt Evans28ccd292011-03-29 13:40:46 +11002018 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp82d10092009-08-07 14:04:52 -07002019 xdev = xhci->devs[slot_id];
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002020 if (!xdev) {
2021 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002022 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
Sarah Sharpe910b442012-01-04 16:54:12 -08002023 (unsigned long long) xhci_trb_virt_to_dma(
2024 xhci->event_ring->deq_seg,
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002025 xhci->event_ring->dequeue),
2026 lower_32_bits(le64_to_cpu(event->buffer)),
2027 upper_32_bits(le64_to_cpu(event->buffer)),
2028 le32_to_cpu(event->transfer_len),
2029 le32_to_cpu(event->flags));
2030 xhci_dbg(xhci, "Event ring:\n");
2031 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002032 return -ENODEV;
2033 }
2034
2035 /* Endpoint ID is 1 based, our index is zero based */
Matt Evans28ccd292011-03-29 13:40:46 +11002036 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002037 ep = &xdev->eps[ep_index];
Matt Evans28ccd292011-03-29 13:40:46 +11002038 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
John Yound115b042009-07-27 12:05:15 -07002039 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002040 if (!ep_ring ||
Matt Evans28ccd292011-03-29 13:40:46 +11002041 (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
2042 EP_STATE_DISABLED) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002043 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
2044 "or incorrect stream ring\n");
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002045 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
Sarah Sharpe910b442012-01-04 16:54:12 -08002046 (unsigned long long) xhci_trb_virt_to_dma(
2047 xhci->event_ring->deq_seg,
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002048 xhci->event_ring->dequeue),
2049 lower_32_bits(le64_to_cpu(event->buffer)),
2050 upper_32_bits(le64_to_cpu(event->buffer)),
2051 le32_to_cpu(event->transfer_len),
2052 le32_to_cpu(event->flags));
2053 xhci_dbg(xhci, "Event ring:\n");
2054 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002055 return -ENODEV;
2056 }
2057
Andiry Xuc2d7b492011-09-19 16:05:12 -07002058 /* Count current td numbers if ep->skip is set */
2059 if (ep->skip) {
2060 list_for_each(tmp, &ep_ring->td_list)
2061 td_num++;
2062 }
2063
Matt Evans28ccd292011-03-29 13:40:46 +11002064 event_dma = le64_to_cpu(event->buffer);
2065 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu986a92d2010-07-22 15:23:20 -07002066 /* Look for common error cases */
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002067 switch (trb_comp_code) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002068 /* Skip codes that require special handling depending on
2069 * transfer type
2070 */
2071 case COMP_SUCCESS:
2072 case COMP_SHORT_TX:
2073 break;
Sarah Sharpae636742009-04-29 19:02:31 -07002074 case COMP_STOP:
2075 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
2076 break;
2077 case COMP_STOP_INVAL:
2078 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
2079 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002080 case COMP_STALL:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002081 xhci_dbg(xhci, "Stalled endpoint\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002082 ep->ep_state |= EP_HALTED;
Sarah Sharpb10de142009-04-27 19:58:50 -07002083 status = -EPIPE;
2084 break;
2085 case COMP_TRB_ERR:
2086 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
2087 status = -EILSEQ;
2088 break;
Sarah Sharpec74e402009-11-11 10:28:36 -08002089 case COMP_SPLIT_ERR:
Sarah Sharpb10de142009-04-27 19:58:50 -07002090 case COMP_TX_ERR:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002091 xhci_dbg(xhci, "Transfer error on endpoint\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002092 status = -EPROTO;
2093 break;
Sarah Sharp4a731432009-07-27 12:04:32 -07002094 case COMP_BABBLE:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002095 xhci_dbg(xhci, "Babble error on endpoint\n");
Sarah Sharp4a731432009-07-27 12:04:32 -07002096 status = -EOVERFLOW;
2097 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002098 case COMP_DB_ERR:
2099 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
2100 status = -ENOSR;
2101 break;
Andiry Xu986a92d2010-07-22 15:23:20 -07002102 case COMP_BW_OVER:
2103 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
2104 break;
2105 case COMP_BUFF_OVER:
2106 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
2107 break;
2108 case COMP_UNDERRUN:
2109 /*
2110 * When the Isoch ring is empty, the xHC will generate
2111 * a Ring Overrun Event for IN Isoch endpoint or Ring
2112 * Underrun Event for OUT Isoch endpoint.
2113 */
2114 xhci_dbg(xhci, "underrun event on endpoint\n");
2115 if (!list_empty(&ep_ring->td_list))
2116 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2117 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002118 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2119 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002120 goto cleanup;
2121 case COMP_OVERRUN:
2122 xhci_dbg(xhci, "overrun event on endpoint\n");
2123 if (!list_empty(&ep_ring->td_list))
2124 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2125 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002126 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2127 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002128 goto cleanup;
Alex Hef6ba6fe2011-06-08 18:34:06 +08002129 case COMP_DEV_ERR:
2130 xhci_warn(xhci, "WARN: detect an incompatible device");
2131 status = -EPROTO;
2132 break;
Andiry Xud18240d2010-07-22 15:23:25 -07002133 case COMP_MISSED_INT:
2134 /*
2135 * When encounter missed service error, one or more isoc tds
2136 * may be missed by xHC.
2137 * Set skip flag of the ep_ring; Complete the missed tds as
2138 * short transfer when process the ep_ring next time.
2139 */
2140 ep->skip = true;
2141 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
2142 goto cleanup;
Sarah Sharpb10de142009-04-27 19:58:50 -07002143 default:
Sarah Sharpb45b5062009-12-09 15:59:06 -08002144 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
Sarah Sharp5ad6a522009-11-11 10:28:40 -08002145 status = 0;
2146 break;
2147 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002148 xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
2149 "busted\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002150 goto cleanup;
2151 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002152
Andiry Xud18240d2010-07-22 15:23:25 -07002153 do {
2154 /* This TRB should be in the TD at the head of this ring's
2155 * TD list.
2156 */
2157 if (list_empty(&ep_ring->td_list)) {
2158 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d "
2159 "with no TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002160 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2161 ep_index);
Andiry Xud18240d2010-07-22 15:23:25 -07002162 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
Matt Evansf5960b62011-06-01 10:22:55 +10002163 (le32_to_cpu(event->flags) &
2164 TRB_TYPE_BITMASK)>>10);
Andiry Xud18240d2010-07-22 15:23:25 -07002165 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2166 if (ep->skip) {
2167 ep->skip = false;
2168 xhci_dbg(xhci, "td_list is empty while skip "
2169 "flag set. Clear skip flag.\n");
2170 }
2171 ret = 0;
2172 goto cleanup;
2173 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002174
Andiry Xuc2d7b492011-09-19 16:05:12 -07002175 /* We've skipped all the TDs on the ep ring when ep->skip set */
2176 if (ep->skip && td_num == 0) {
2177 ep->skip = false;
2178 xhci_dbg(xhci, "All tds on the ep_ring skipped. "
2179 "Clear skip flag.\n");
2180 ret = 0;
2181 goto cleanup;
2182 }
2183
Andiry Xud18240d2010-07-22 15:23:25 -07002184 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
Andiry Xuc2d7b492011-09-19 16:05:12 -07002185 if (ep->skip)
2186 td_num--;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002187
Andiry Xud18240d2010-07-22 15:23:25 -07002188 /* Is this a TRB in the currently executing TD? */
2189 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
2190 td->last_trb, event_dma);
Alex Hee1cf4862011-06-03 15:58:25 +08002191
2192 /*
2193 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
2194 * is not in the current TD pointed by ep_ring->dequeue because
2195 * that the hardware dequeue pointer still at the previous TRB
2196 * of the current TD. The previous TRB maybe a Link TD or the
2197 * last TRB of the previous TD. The command completion handle
2198 * will take care the rest.
2199 */
2200 if (!event_seg && trb_comp_code == COMP_STOP_INVAL) {
2201 ret = 0;
2202 goto cleanup;
2203 }
2204
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002205 if (!event_seg) {
2206 if (!ep->skip ||
2207 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
Sarah Sharpad808332011-05-25 10:43:56 -07002208 /* Some host controllers give a spurious
2209 * successful event after a short transfer.
2210 * Ignore it.
2211 */
2212 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
2213 ep_ring->last_td_was_short) {
2214 ep_ring->last_td_was_short = false;
2215 ret = 0;
2216 goto cleanup;
2217 }
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002218 /* HC is busted, give up! */
2219 xhci_err(xhci,
2220 "ERROR Transfer event TRB DMA ptr not "
2221 "part of current TD\n");
2222 return -ESHUTDOWN;
2223 }
2224
2225 ret = skip_isoc_td(xhci, td, event, ep, &status);
2226 goto cleanup;
2227 }
Sarah Sharpad808332011-05-25 10:43:56 -07002228 if (trb_comp_code == COMP_SHORT_TX)
2229 ep_ring->last_td_was_short = true;
2230 else
2231 ep_ring->last_td_was_short = false;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002232
2233 if (ep->skip) {
Andiry Xud18240d2010-07-22 15:23:25 -07002234 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2235 ep->skip = false;
2236 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002237
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002238 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) /
2239 sizeof(*event_trb)];
2240 /*
2241 * No-op TRB should not trigger interrupts.
2242 * If event_trb is a no-op TRB, it means the
2243 * corresponding TD has been cancelled. Just ignore
2244 * the TD.
2245 */
Matt Evansf5960b62011-06-01 10:22:55 +10002246 if (TRB_TYPE_NOOP_LE32(event_trb->generic.field[3])) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002247 xhci_dbg(xhci,
2248 "event_trb is a no-op TRB. Skip it\n");
2249 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07002250 }
2251
2252 /* Now update the urb's actual_length and give back to
2253 * the core
2254 */
2255 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2256 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
2257 &status);
Andiry Xu04e51902010-07-22 15:23:39 -07002258 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2259 ret = process_isoc_td(xhci, td, event_trb, event, ep,
2260 &status);
Andiry Xud18240d2010-07-22 15:23:25 -07002261 else
2262 ret = process_bulk_intr_td(xhci, td, event_trb, event,
2263 ep, &status);
Andiry Xu4422da62010-07-22 15:22:55 -07002264
2265cleanup:
Andiry Xud18240d2010-07-22 15:23:25 -07002266 /*
2267 * Do not update event ring dequeue pointer if ep->skip is set.
2268 * Will roll back to continue process missed tds.
Sarah Sharp82d10092009-08-07 14:04:52 -07002269 */
Andiry Xud18240d2010-07-22 15:23:25 -07002270 if (trb_comp_code == COMP_MISSED_INT || !ep->skip) {
Andiry Xu3b72fca2012-03-05 17:49:32 +08002271 inc_deq(xhci, xhci->event_ring);
Andiry Xud18240d2010-07-22 15:23:25 -07002272 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002273
Andiry Xud18240d2010-07-22 15:23:25 -07002274 if (ret) {
2275 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002276 urb_priv = urb->hcpriv;
Andiry Xud18240d2010-07-22 15:23:25 -07002277 /* Leave the TD around for the reset endpoint function
2278 * to use(but only if it's not a control endpoint,
2279 * since we already queued the Set TR dequeue pointer
2280 * command for stalled control endpoints).
2281 */
2282 if (usb_endpoint_xfer_control(&urb->ep->desc) ||
2283 (trb_comp_code != COMP_STALL &&
2284 trb_comp_code != COMP_BABBLE))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002285 xhci_urb_free_priv(xhci, urb_priv);
Andiry Xud18240d2010-07-22 15:23:25 -07002286
Sarah Sharp214f76f2010-10-26 11:22:02 -07002287 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpf444ff22011-04-05 15:53:47 -07002288 if ((urb->actual_length != urb->transfer_buffer_length &&
2289 (urb->transfer_flags &
2290 URB_SHORT_NOT_OK)) ||
Sarah Sharpfd984d22011-09-02 11:05:56 -07002291 (status != 0 &&
2292 !usb_endpoint_xfer_isoc(&urb->ep->desc)))
Sarah Sharpf444ff22011-04-05 15:53:47 -07002293 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
2294 "expected = %x, status = %d\n",
2295 urb, urb->actual_length,
2296 urb->transfer_buffer_length,
2297 status);
Andiry Xud18240d2010-07-22 15:23:25 -07002298 spin_unlock(&xhci->lock);
Sarah Sharpb3df3f92011-06-15 19:57:46 -07002299 /* EHCI, UHCI, and OHCI always unconditionally set the
2300 * urb->status of an isochronous endpoint to 0.
2301 */
2302 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2303 status = 0;
Sarah Sharp214f76f2010-10-26 11:22:02 -07002304 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
Andiry Xud18240d2010-07-22 15:23:25 -07002305 spin_lock(&xhci->lock);
2306 }
2307
2308 /*
2309 * If ep->skip is set, it means there are missed tds on the
2310 * endpoint ring need to take care of.
2311 * Process them as short transfer until reach the td pointed by
2312 * the event.
2313 */
2314 } while (ep->skip && trb_comp_code != COMP_MISSED_INT);
2315
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002316 return 0;
2317}
2318
2319/*
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002320 * This function handles all OS-owned events on the event ring. It may drop
2321 * xhci->lock between event processing (e.g. to pass up port status changes).
Matt Evans9dee9a22011-03-29 13:41:02 +11002322 * Returns >0 for "possibly more events to process" (caller should call again),
2323 * otherwise 0 if done. In future, <0 returns should indicate error code.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002324 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002325static int xhci_handle_event(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002326{
2327 union xhci_trb *event;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002328 int update_ptrs = 1;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002329 int ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002330
2331 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2332 xhci->error_bitmask |= 1 << 1;
Matt Evans9dee9a22011-03-29 13:41:02 +11002333 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002334 }
2335
2336 event = xhci->event_ring->dequeue;
2337 /* Does the HC or OS own the TRB? */
Matt Evans28ccd292011-03-29 13:40:46 +11002338 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
2339 xhci->event_ring->cycle_state) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002340 xhci->error_bitmask |= 1 << 2;
Matt Evans9dee9a22011-03-29 13:41:02 +11002341 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002342 }
2343
Matt Evans92a3da42011-03-29 13:40:51 +11002344 /*
2345 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2346 * speculative reads of the event's flags/data below.
2347 */
2348 rmb();
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002349 /* FIXME: Handle more event types. */
Matt Evans28ccd292011-03-29 13:40:46 +11002350 switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002351 case TRB_TYPE(TRB_COMPLETION):
2352 handle_cmd_completion(xhci, &event->event_cmd);
2353 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002354 case TRB_TYPE(TRB_PORT_STATUS):
2355 handle_port_status(xhci, event);
2356 update_ptrs = 0;
2357 break;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002358 case TRB_TYPE(TRB_TRANSFER):
2359 ret = handle_tx_event(xhci, &event->trans_event);
2360 if (ret < 0)
2361 xhci->error_bitmask |= 1 << 9;
2362 else
2363 update_ptrs = 0;
2364 break;
Sarah Sharp623bef92011-11-11 14:57:33 -08002365 case TRB_TYPE(TRB_DEV_NOTE):
2366 handle_device_notification(xhci, event);
2367 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002368 default:
Matt Evans28ccd292011-03-29 13:40:46 +11002369 if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
2370 TRB_TYPE(48))
Sarah Sharp02386342010-05-24 13:25:28 -07002371 handle_vendor_event(xhci, event);
2372 else
2373 xhci->error_bitmask |= 1 << 3;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002374 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002375 /* Any of the above functions may drop and re-acquire the lock, so check
2376 * to make sure a watchdog timer didn't mark the host as non-responsive.
2377 */
2378 if (xhci->xhc_state & XHCI_STATE_DYING) {
2379 xhci_dbg(xhci, "xHCI host dying, returning from "
2380 "event handler.\n");
Matt Evans9dee9a22011-03-29 13:41:02 +11002381 return 0;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002382 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002383
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002384 if (update_ptrs)
2385 /* Update SW event ring dequeue pointer */
Andiry Xu3b72fca2012-03-05 17:49:32 +08002386 inc_deq(xhci, xhci->event_ring);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002387
Matt Evans9dee9a22011-03-29 13:41:02 +11002388 /* Are there more items on the event ring? Caller will call us again to
2389 * check.
2390 */
2391 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002392}
Sarah Sharp9032cd52010-07-29 22:12:29 -07002393
2394/*
2395 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2396 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2397 * indicators of an event TRB error, but we check the status *first* to be safe.
2398 */
2399irqreturn_t xhci_irq(struct usb_hcd *hcd)
2400{
2401 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002402 u32 status;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002403 union xhci_trb *trb;
Sarah Sharpbda53142010-07-29 22:12:38 -07002404 u64 temp_64;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002405 union xhci_trb *event_ring_deq;
2406 dma_addr_t deq;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002407
2408 spin_lock(&xhci->lock);
2409 trb = xhci->event_ring->dequeue;
2410 /* Check if the xHC generated the interrupt, or the irq is shared */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002411 status = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002412 if (status == 0xffffffff)
Sarah Sharp9032cd52010-07-29 22:12:29 -07002413 goto hw_died;
2414
Sarah Sharpc21599a2010-07-29 22:13:00 -07002415 if (!(status & STS_EINT)) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002416 spin_unlock(&xhci->lock);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002417 return IRQ_NONE;
2418 }
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002419 if (status & STS_FATAL) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002420 xhci_warn(xhci, "WARNING: Host System Error\n");
2421 xhci_halt(xhci);
2422hw_died:
Sarah Sharp9032cd52010-07-29 22:12:29 -07002423 spin_unlock(&xhci->lock);
2424 return -ESHUTDOWN;
2425 }
2426
Sarah Sharpbda53142010-07-29 22:12:38 -07002427 /*
2428 * Clear the op reg interrupt status first,
2429 * so we can receive interrupts from other MSI-X interrupters.
2430 * Write 1 to clear the interrupt status.
2431 */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002432 status |= STS_EINT;
2433 xhci_writel(xhci, status, &xhci->op_regs->status);
Sarah Sharpbda53142010-07-29 22:12:38 -07002434 /* FIXME when MSI-X is supported and there are multiple vectors */
2435 /* Clear the MSI-X event interrupt status */
2436
Felipe Balbicd704692012-02-29 16:46:23 +02002437 if (hcd->irq) {
Sarah Sharpc21599a2010-07-29 22:13:00 -07002438 u32 irq_pending;
2439 /* Acknowledge the PCI interrupt */
2440 irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Felipe Balbi4e833c02012-03-15 16:37:08 +02002441 irq_pending |= IMAN_IP;
Sarah Sharpc21599a2010-07-29 22:13:00 -07002442 xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
2443 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002444
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002445 if (xhci->xhc_state & XHCI_STATE_DYING) {
Sarah Sharpbda53142010-07-29 22:12:38 -07002446 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2447 "Shouldn't IRQs be disabled?\n");
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002448 /* Clear the event handler busy flag (RW1C);
2449 * the event ring should be empty.
Sarah Sharpbda53142010-07-29 22:12:38 -07002450 */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002451 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2452 xhci_write_64(xhci, temp_64 | ERST_EHB,
2453 &xhci->ir_set->erst_dequeue);
2454 spin_unlock(&xhci->lock);
2455
2456 return IRQ_HANDLED;
2457 }
2458
2459 event_ring_deq = xhci->event_ring->dequeue;
2460 /* FIXME this should be a delayed service routine
2461 * that clears the EHB.
2462 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002463 while (xhci_handle_event(xhci) > 0) {}
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002464
2465 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2466 /* If necessary, update the HW's version of the event ring deq ptr. */
2467 if (event_ring_deq != xhci->event_ring->dequeue) {
2468 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2469 xhci->event_ring->dequeue);
2470 if (deq == 0)
2471 xhci_warn(xhci, "WARN something wrong with SW event "
2472 "ring dequeue ptr.\n");
2473 /* Update HC event ring dequeue pointer */
2474 temp_64 &= ERST_PTR_MASK;
2475 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2476 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002477
2478 /* Clear the event handler busy flag (RW1C); event ring is empty. */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002479 temp_64 |= ERST_EHB;
2480 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
2481
Sarah Sharp9032cd52010-07-29 22:12:29 -07002482 spin_unlock(&xhci->lock);
2483
2484 return IRQ_HANDLED;
2485}
2486
2487irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
2488{
Alan Stern968b8222011-11-03 12:03:38 -04002489 return xhci_irq(hcd);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002490}
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002491
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002492/**** Endpoint Ring Operations ****/
2493
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002494/*
2495 * Generic function for queueing a TRB on a ring.
2496 * The caller must have checked to make sure there's room on the ring.
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002497 *
2498 * @more_trbs_coming: Will you enqueue more TRBs before calling
2499 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002500 */
2501static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002502 bool more_trbs_coming,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002503 u32 field1, u32 field2, u32 field3, u32 field4)
2504{
2505 struct xhci_generic_trb *trb;
2506
2507 trb = &ring->enqueue->generic;
Matt Evans28ccd292011-03-29 13:40:46 +11002508 trb->field[0] = cpu_to_le32(field1);
2509 trb->field[1] = cpu_to_le32(field2);
2510 trb->field[2] = cpu_to_le32(field3);
2511 trb->field[3] = cpu_to_le32(field4);
Andiry Xu3b72fca2012-03-05 17:49:32 +08002512 inc_enq(xhci, ring, more_trbs_coming);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002513}
2514
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002515/*
2516 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2517 * FIXME allocate segments if the ring is full.
2518 */
2519static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002520 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002521{
Andiry Xu8dfec612012-03-05 17:49:37 +08002522 unsigned int num_trbs_needed;
2523
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002524 /* Make sure the endpoint has been added to xHC schedule */
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002525 switch (ep_state) {
2526 case EP_STATE_DISABLED:
2527 /*
2528 * USB core changed config/interfaces without notifying us,
2529 * or hardware is reporting the wrong state.
2530 */
2531 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2532 return -ENOENT;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002533 case EP_STATE_ERROR:
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002534 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002535 /* FIXME event handling code for error needs to clear it */
2536 /* XXX not sure if this should be -ENOENT or not */
2537 return -EINVAL;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002538 case EP_STATE_HALTED:
2539 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002540 case EP_STATE_STOPPED:
2541 case EP_STATE_RUNNING:
2542 break;
2543 default:
2544 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2545 /*
2546 * FIXME issue Configure Endpoint command to try to get the HC
2547 * back into a known state.
2548 */
2549 return -EINVAL;
2550 }
Andiry Xu8dfec612012-03-05 17:49:37 +08002551
2552 while (1) {
2553 if (room_on_ring(xhci, ep_ring, num_trbs))
2554 break;
2555
2556 if (ep_ring == xhci->cmd_ring) {
2557 xhci_err(xhci, "Do not support expand command ring\n");
2558 return -ENOMEM;
2559 }
2560
Andiry Xu8dfec612012-03-05 17:49:37 +08002561 xhci_dbg(xhci, "ERROR no room on ep ring, "
2562 "try ring expansion\n");
2563 num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
2564 if (xhci_ring_expansion(xhci, ep_ring, num_trbs_needed,
2565 mem_flags)) {
2566 xhci_err(xhci, "Ring expansion failed\n");
2567 return -ENOMEM;
2568 }
2569 };
John Youn6c12db92010-05-10 15:33:00 -07002570
2571 if (enqueue_is_link_trb(ep_ring)) {
2572 struct xhci_ring *ring = ep_ring;
2573 union xhci_trb *next;
John Youn6c12db92010-05-10 15:33:00 -07002574
John Youn6c12db92010-05-10 15:33:00 -07002575 next = ring->enqueue;
2576
2577 while (last_trb(xhci, ring, ring->enq_seg, next)) {
Andiry Xu7e393a82011-09-23 14:19:54 -07002578 /* If we're not dealing with 0.95 hardware or isoc rings
2579 * on AMD 0.96 host, clear the chain bit.
John Youn6c12db92010-05-10 15:33:00 -07002580 */
Andiry Xu3b72fca2012-03-05 17:49:32 +08002581 if (!xhci_link_trb_quirk(xhci) &&
2582 !(ring->type == TYPE_ISOC &&
2583 (xhci->quirks & XHCI_AMD_0x96_HOST)))
Matt Evans28ccd292011-03-29 13:40:46 +11002584 next->link.control &= cpu_to_le32(~TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002585 else
Matt Evans28ccd292011-03-29 13:40:46 +11002586 next->link.control |= cpu_to_le32(TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002587
2588 wmb();
Matt Evansf5960b62011-06-01 10:22:55 +10002589 next->link.control ^= cpu_to_le32(TRB_CYCLE);
John Youn6c12db92010-05-10 15:33:00 -07002590
2591 /* Toggle the cycle bit after the last ring segment. */
2592 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
2593 ring->cycle_state = (ring->cycle_state ? 0 : 1);
John Youn6c12db92010-05-10 15:33:00 -07002594 }
2595 ring->enq_seg = ring->enq_seg->next;
2596 ring->enqueue = ring->enq_seg->trbs;
2597 next = ring->enqueue;
2598 }
2599 }
2600
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002601 return 0;
2602}
2603
Sarah Sharp23e3be12009-04-29 19:05:20 -07002604static int prepare_transfer(struct xhci_hcd *xhci,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002605 struct xhci_virt_device *xdev,
2606 unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002607 unsigned int stream_id,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002608 unsigned int num_trbs,
2609 struct urb *urb,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002610 unsigned int td_index,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002611 gfp_t mem_flags)
2612{
2613 int ret;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002614 struct urb_priv *urb_priv;
2615 struct xhci_td *td;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002616 struct xhci_ring *ep_ring;
John Yound115b042009-07-27 12:05:15 -07002617 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002618
2619 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2620 if (!ep_ring) {
2621 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2622 stream_id);
2623 return -EINVAL;
2624 }
2625
2626 ret = prepare_ring(xhci, ep_ring,
Matt Evans28ccd292011-03-29 13:40:46 +11002627 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002628 num_trbs, mem_flags);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002629 if (ret)
2630 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002631
Andiry Xu8e51adc2010-07-22 15:23:31 -07002632 urb_priv = urb->hcpriv;
2633 td = urb_priv->td[td_index];
2634
2635 INIT_LIST_HEAD(&td->td_list);
2636 INIT_LIST_HEAD(&td->cancelled_td_list);
2637
2638 if (td_index == 0) {
Sarah Sharp214f76f2010-10-26 11:22:02 -07002639 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07002640 if (unlikely(ret))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002641 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002642 }
2643
Andiry Xu8e51adc2010-07-22 15:23:31 -07002644 td->urb = urb;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002645 /* Add this TD to the tail of the endpoint ring's TD list */
Andiry Xu8e51adc2010-07-22 15:23:31 -07002646 list_add_tail(&td->td_list, &ep_ring->td_list);
2647 td->start_seg = ep_ring->enq_seg;
2648 td->first_trb = ep_ring->enqueue;
2649
2650 urb_priv->td[td_index] = td;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07002651
2652 return 0;
2653}
2654
Sarah Sharp23e3be12009-04-29 19:05:20 -07002655static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002656{
2657 int num_sgs, num_trbs, running_total, temp, i;
2658 struct scatterlist *sg;
2659
2660 sg = NULL;
Clemens Ladischbc677d52011-12-03 23:41:31 +01002661 num_sgs = urb->num_mapped_sgs;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002662 temp = urb->transfer_buffer_length;
2663
Sarah Sharp8a96c052009-04-27 19:59:19 -07002664 num_trbs = 0;
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002665 for_each_sg(urb->sg, sg, num_sgs, i) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002666 unsigned int len = sg_dma_len(sg);
2667
2668 /* Scatter gather list entries may cross 64KB boundaries */
2669 running_total = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002670 (sg_dma_address(sg) & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08002671 running_total &= TRB_MAX_BUFF_SIZE - 1;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002672 if (running_total != 0)
2673 num_trbs++;
2674
2675 /* How many more 64KB chunks to transfer, how many more TRBs? */
Paul Zimmermanbcd2fde2011-02-12 14:07:57 -08002676 while (running_total < sg_dma_len(sg) && running_total < temp) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002677 num_trbs++;
2678 running_total += TRB_MAX_BUFF_SIZE;
2679 }
Sarah Sharp8a96c052009-04-27 19:59:19 -07002680 len = min_t(int, len, temp);
2681 temp -= len;
2682 if (temp == 0)
2683 break;
2684 }
Sarah Sharp8a96c052009-04-27 19:59:19 -07002685 return num_trbs;
2686}
2687
Sarah Sharp23e3be12009-04-29 19:05:20 -07002688static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002689{
2690 if (num_trbs != 0)
Paul Zimmermana2490182011-02-12 14:06:44 -08002691 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
Sarah Sharp8a96c052009-04-27 19:59:19 -07002692 "TRBs, %d left\n", __func__,
2693 urb->ep->desc.bEndpointAddress, num_trbs);
2694 if (running_total != urb->transfer_buffer_length)
Paul Zimmermana2490182011-02-12 14:06:44 -08002695 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
Sarah Sharp8a96c052009-04-27 19:59:19 -07002696 "queued %#x (%d), asked for %#x (%d)\n",
2697 __func__,
2698 urb->ep->desc.bEndpointAddress,
2699 running_total, running_total,
2700 urb->transfer_buffer_length,
2701 urb->transfer_buffer_length);
2702}
2703
Sarah Sharp23e3be12009-04-29 19:05:20 -07002704static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002705 unsigned int ep_index, unsigned int stream_id, int start_cycle,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002706 struct xhci_generic_trb *start_trb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002707{
Sarah Sharp8a96c052009-04-27 19:59:19 -07002708 /*
2709 * Pass all the TRBs to the hardware at once and make sure this write
2710 * isn't reordered.
2711 */
2712 wmb();
Andiry Xu50f7b522010-12-20 15:09:34 +08002713 if (start_cycle)
Matt Evans28ccd292011-03-29 13:40:46 +11002714 start_trb->field[3] |= cpu_to_le32(start_cycle);
Andiry Xu50f7b522010-12-20 15:09:34 +08002715 else
Matt Evans28ccd292011-03-29 13:40:46 +11002716 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
Andiry Xube88fe42010-10-14 07:22:57 -07002717 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002718}
2719
Sarah Sharp624defa2009-09-02 12:14:28 -07002720/*
2721 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
2722 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
2723 * (comprised of sg list entries) can take several service intervals to
2724 * transmit.
2725 */
2726int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
2727 struct urb *urb, int slot_id, unsigned int ep_index)
2728{
2729 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
2730 xhci->devs[slot_id]->out_ctx, ep_index);
2731 int xhci_interval;
2732 int ep_interval;
2733
Matt Evans28ccd292011-03-29 13:40:46 +11002734 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Sarah Sharp624defa2009-09-02 12:14:28 -07002735 ep_interval = urb->interval;
2736 /* Convert to microframes */
2737 if (urb->dev->speed == USB_SPEED_LOW ||
2738 urb->dev->speed == USB_SPEED_FULL)
2739 ep_interval *= 8;
2740 /* FIXME change this to a warning and a suggestion to use the new API
2741 * to set the polling interval (once the API is added).
2742 */
2743 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08002744 if (printk_ratelimit())
Sarah Sharp624defa2009-09-02 12:14:28 -07002745 dev_dbg(&urb->dev->dev, "Driver uses different interval"
2746 " (%d microframe%s) than xHCI "
2747 "(%d microframe%s)\n",
2748 ep_interval,
2749 ep_interval == 1 ? "" : "s",
2750 xhci_interval,
2751 xhci_interval == 1 ? "" : "s");
2752 urb->interval = xhci_interval;
2753 /* Convert back to frames for LS/FS devices */
2754 if (urb->dev->speed == USB_SPEED_LOW ||
2755 urb->dev->speed == USB_SPEED_FULL)
2756 urb->interval /= 8;
2757 }
Dan Carpenter3fc82062012-03-28 10:30:26 +03002758 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
Sarah Sharp624defa2009-09-02 12:14:28 -07002759}
2760
Sarah Sharp04dd9502009-11-11 10:28:30 -08002761/*
2762 * The TD size is the number of bytes remaining in the TD (including this TRB),
2763 * right shifted by 10.
2764 * It must fit in bits 21:17, so it can't be bigger than 31.
2765 */
2766static u32 xhci_td_remainder(unsigned int remainder)
2767{
2768 u32 max = (1 << (21 - 17 + 1)) - 1;
2769
2770 if ((remainder >> 10) >= max)
2771 return max << 17;
2772 else
2773 return (remainder >> 10) << 17;
2774}
2775
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002776/*
2777 * For xHCI 1.0 host controllers, TD size is the number of packets remaining in
2778 * the TD (*not* including this TRB).
2779 *
2780 * Total TD packet count = total_packet_count =
2781 * roundup(TD size in bytes / wMaxPacketSize)
2782 *
2783 * Packets transferred up to and including this TRB = packets_transferred =
2784 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
2785 *
2786 * TD size = total_packet_count - packets_transferred
2787 *
2788 * It must fit in bits 21:17, so it can't be bigger than 31.
2789 */
2790
2791static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
2792 unsigned int total_packet_count, struct urb *urb)
2793{
2794 int packets_transferred;
2795
Sarah Sharp48df4a62011-08-12 10:23:01 -07002796 /* One TRB with a zero-length data packet. */
2797 if (running_total == 0 && trb_buff_len == 0)
2798 return 0;
2799
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002800 /* All the TRB queueing functions don't count the current TRB in
2801 * running_total.
2802 */
2803 packets_transferred = (running_total + trb_buff_len) /
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002804 usb_endpoint_maxp(&urb->ep->desc);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002805
2806 return xhci_td_remainder(total_packet_count - packets_transferred);
2807}
2808
Sarah Sharp23e3be12009-04-29 19:05:20 -07002809static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharp8a96c052009-04-27 19:59:19 -07002810 struct urb *urb, int slot_id, unsigned int ep_index)
2811{
2812 struct xhci_ring *ep_ring;
2813 unsigned int num_trbs;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002814 struct urb_priv *urb_priv;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002815 struct xhci_td *td;
2816 struct scatterlist *sg;
2817 int num_sgs;
2818 int trb_buff_len, this_sg_len, running_total;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002819 unsigned int total_packet_count;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002820 bool first_trb;
2821 u64 addr;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002822 bool more_trbs_coming;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002823
2824 struct xhci_generic_trb *start_trb;
2825 int start_cycle;
2826
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002827 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2828 if (!ep_ring)
2829 return -EINVAL;
2830
Sarah Sharp8a96c052009-04-27 19:59:19 -07002831 num_trbs = count_sg_trbs_needed(xhci, urb);
Clemens Ladischbc677d52011-12-03 23:41:31 +01002832 num_sgs = urb->num_mapped_sgs;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002833 total_packet_count = roundup(urb->transfer_buffer_length,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002834 usb_endpoint_maxp(&urb->ep->desc));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002835
Sarah Sharp23e3be12009-04-29 19:05:20 -07002836 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002837 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002838 num_trbs, urb, 0, mem_flags);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002839 if (trb_buff_len < 0)
2840 return trb_buff_len;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002841
2842 urb_priv = urb->hcpriv;
2843 td = urb_priv->td[0];
2844
Sarah Sharp8a96c052009-04-27 19:59:19 -07002845 /*
2846 * Don't give the first TRB to the hardware (by toggling the cycle bit)
2847 * until we've finished creating all the other TRBs. The ring's cycle
2848 * state may change as we enqueue the other TRBs, so save it too.
2849 */
2850 start_trb = &ep_ring->enqueue->generic;
2851 start_cycle = ep_ring->cycle_state;
2852
2853 running_total = 0;
2854 /*
2855 * How much data is in the first TRB?
2856 *
2857 * There are three forces at work for TRB buffer pointers and lengths:
2858 * 1. We don't want to walk off the end of this sg-list entry buffer.
2859 * 2. The transfer length that the driver requested may be smaller than
2860 * the amount of memory allocated for this scatter-gather list.
2861 * 3. TRBs buffers can't cross 64KB boundaries.
2862 */
Matthew Wilcox910f8d02010-05-01 12:20:01 -06002863 sg = urb->sg;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002864 addr = (u64) sg_dma_address(sg);
2865 this_sg_len = sg_dma_len(sg);
Paul Zimmermana2490182011-02-12 14:06:44 -08002866 trb_buff_len = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002867 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2868 if (trb_buff_len > urb->transfer_buffer_length)
2869 trb_buff_len = urb->transfer_buffer_length;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002870
2871 first_trb = true;
2872 /* Queue the first TRB, even if it's zero-length */
2873 do {
2874 u32 field = 0;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002875 u32 length_field = 0;
Sarah Sharp04dd9502009-11-11 10:28:30 -08002876 u32 remainder = 0;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002877
2878 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08002879 if (first_trb) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002880 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08002881 if (start_cycle == 0)
2882 field |= 0x1;
2883 } else
Sarah Sharp8a96c052009-04-27 19:59:19 -07002884 field |= ep_ring->cycle_state;
2885
2886 /* Chain all the TRBs together; clear the chain bit in the last
2887 * TRB to indicate it's the last TRB in the chain.
2888 */
2889 if (num_trbs > 1) {
2890 field |= TRB_CHAIN;
2891 } else {
2892 /* FIXME - add check for ZERO_PACKET flag before this */
2893 td->last_trb = ep_ring->enqueue;
2894 field |= TRB_IOC;
2895 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002896
2897 /* Only set interrupt on short packet for IN endpoints */
2898 if (usb_urb_dir_in(urb))
2899 field |= TRB_ISP;
2900
Sarah Sharp8a96c052009-04-27 19:59:19 -07002901 if (TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002902 (addr & (TRB_MAX_BUFF_SIZE - 1)) < trb_buff_len) {
Sarah Sharp8a96c052009-04-27 19:59:19 -07002903 xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
2904 xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
2905 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
2906 (unsigned int) addr + trb_buff_len);
2907 }
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002908
2909 /* Set the TRB length, TD size, and interrupter fields. */
2910 if (xhci->hci_version < 0x100) {
2911 remainder = xhci_td_remainder(
2912 urb->transfer_buffer_length -
2913 running_total);
2914 } else {
2915 remainder = xhci_v1_0_td_remainder(running_total,
2916 trb_buff_len, total_packet_count, urb);
2917 }
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002918 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08002919 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002920 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002921
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002922 if (num_trbs > 1)
2923 more_trbs_coming = true;
2924 else
2925 more_trbs_coming = false;
Andiry Xu3b72fca2012-03-05 17:49:32 +08002926 queue_trb(xhci, ep_ring, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002927 lower_32_bits(addr),
2928 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002929 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07002930 field | TRB_TYPE(TRB_NORMAL));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002931 --num_trbs;
2932 running_total += trb_buff_len;
2933
2934 /* Calculate length for next transfer --
2935 * Are we done queueing all the TRBs for this sg entry?
2936 */
2937 this_sg_len -= trb_buff_len;
2938 if (this_sg_len == 0) {
2939 --num_sgs;
2940 if (num_sgs == 0)
2941 break;
2942 sg = sg_next(sg);
2943 addr = (u64) sg_dma_address(sg);
2944 this_sg_len = sg_dma_len(sg);
2945 } else {
2946 addr += trb_buff_len;
2947 }
2948
2949 trb_buff_len = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002950 (addr & (TRB_MAX_BUFF_SIZE - 1));
Sarah Sharp8a96c052009-04-27 19:59:19 -07002951 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
2952 if (running_total + trb_buff_len > urb->transfer_buffer_length)
2953 trb_buff_len =
2954 urb->transfer_buffer_length - running_total;
2955 } while (running_total < urb->transfer_buffer_length);
2956
2957 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002958 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08002959 start_cycle, start_trb);
Sarah Sharp8a96c052009-04-27 19:59:19 -07002960 return 0;
2961}
2962
Sarah Sharpb10de142009-04-27 19:58:50 -07002963/* This is very similar to what ehci-q.c qtd_fill() does */
Sarah Sharp23e3be12009-04-29 19:05:20 -07002964int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpb10de142009-04-27 19:58:50 -07002965 struct urb *urb, int slot_id, unsigned int ep_index)
2966{
2967 struct xhci_ring *ep_ring;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002968 struct urb_priv *urb_priv;
Sarah Sharpb10de142009-04-27 19:58:50 -07002969 struct xhci_td *td;
2970 int num_trbs;
2971 struct xhci_generic_trb *start_trb;
2972 bool first_trb;
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002973 bool more_trbs_coming;
Sarah Sharpb10de142009-04-27 19:58:50 -07002974 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07002975 u32 field, length_field;
Sarah Sharpb10de142009-04-27 19:58:50 -07002976
2977 int running_total, trb_buff_len, ret;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07002978 unsigned int total_packet_count;
Sarah Sharpb10de142009-04-27 19:58:50 -07002979 u64 addr;
2980
Alan Sternff9c8952010-04-02 13:27:28 -04002981 if (urb->num_sgs)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002982 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
2983
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002984 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
2985 if (!ep_ring)
2986 return -EINVAL;
Sarah Sharpb10de142009-04-27 19:58:50 -07002987
2988 num_trbs = 0;
2989 /* How much data is (potentially) left before the 64KB boundary? */
2990 running_total = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08002991 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
Paul Zimmerman58077952011-02-12 14:07:20 -08002992 running_total &= TRB_MAX_BUFF_SIZE - 1;
Sarah Sharpb10de142009-04-27 19:58:50 -07002993
2994 /* If there's some data on this 64KB chunk, or we have to send a
2995 * zero-length transfer, we need at least one TRB
2996 */
2997 if (running_total != 0 || urb->transfer_buffer_length == 0)
2998 num_trbs++;
2999 /* How many more 64KB chunks to transfer, how many more TRBs? */
3000 while (running_total < urb->transfer_buffer_length) {
3001 num_trbs++;
3002 running_total += TRB_MAX_BUFF_SIZE;
3003 }
3004 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
3005
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003006 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3007 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003008 num_trbs, urb, 0, mem_flags);
Sarah Sharpb10de142009-04-27 19:58:50 -07003009 if (ret < 0)
3010 return ret;
3011
Andiry Xu8e51adc2010-07-22 15:23:31 -07003012 urb_priv = urb->hcpriv;
3013 td = urb_priv->td[0];
3014
Sarah Sharpb10de142009-04-27 19:58:50 -07003015 /*
3016 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3017 * until we've finished creating all the other TRBs. The ring's cycle
3018 * state may change as we enqueue the other TRBs, so save it too.
3019 */
3020 start_trb = &ep_ring->enqueue->generic;
3021 start_cycle = ep_ring->cycle_state;
3022
3023 running_total = 0;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003024 total_packet_count = roundup(urb->transfer_buffer_length,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003025 usb_endpoint_maxp(&urb->ep->desc));
Sarah Sharpb10de142009-04-27 19:58:50 -07003026 /* How much data is in the first TRB? */
3027 addr = (u64) urb->transfer_dma;
3028 trb_buff_len = TRB_MAX_BUFF_SIZE -
Paul Zimmermana2490182011-02-12 14:06:44 -08003029 (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
3030 if (trb_buff_len > urb->transfer_buffer_length)
Sarah Sharpb10de142009-04-27 19:58:50 -07003031 trb_buff_len = urb->transfer_buffer_length;
3032
3033 first_trb = true;
3034
3035 /* Queue the first TRB, even if it's zero-length */
3036 do {
Sarah Sharp04dd9502009-11-11 10:28:30 -08003037 u32 remainder = 0;
Sarah Sharpb10de142009-04-27 19:58:50 -07003038 field = 0;
3039
3040 /* Don't change the cycle bit of the first TRB until later */
Andiry Xu50f7b522010-12-20 15:09:34 +08003041 if (first_trb) {
Sarah Sharpb10de142009-04-27 19:58:50 -07003042 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08003043 if (start_cycle == 0)
3044 field |= 0x1;
3045 } else
Sarah Sharpb10de142009-04-27 19:58:50 -07003046 field |= ep_ring->cycle_state;
3047
3048 /* Chain all the TRBs together; clear the chain bit in the last
3049 * TRB to indicate it's the last TRB in the chain.
3050 */
3051 if (num_trbs > 1) {
3052 field |= TRB_CHAIN;
3053 } else {
3054 /* FIXME - add check for ZERO_PACKET flag before this */
3055 td->last_trb = ep_ring->enqueue;
3056 field |= TRB_IOC;
3057 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003058
3059 /* Only set interrupt on short packet for IN endpoints */
3060 if (usb_urb_dir_in(urb))
3061 field |= TRB_ISP;
3062
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003063 /* Set the TRB length, TD size, and interrupter fields. */
3064 if (xhci->hci_version < 0x100) {
3065 remainder = xhci_td_remainder(
3066 urb->transfer_buffer_length -
3067 running_total);
3068 } else {
3069 remainder = xhci_v1_0_td_remainder(running_total,
3070 trb_buff_len, total_packet_count, urb);
3071 }
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003072 length_field = TRB_LEN(trb_buff_len) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08003073 remainder |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003074 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003075
Sarah Sharp6cc30d82010-06-10 12:25:28 -07003076 if (num_trbs > 1)
3077 more_trbs_coming = true;
3078 else
3079 more_trbs_coming = false;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003080 queue_trb(xhci, ep_ring, more_trbs_coming,
Sarah Sharp8e595a52009-07-27 12:03:31 -07003081 lower_32_bits(addr),
3082 upper_32_bits(addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003083 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003084 field | TRB_TYPE(TRB_NORMAL));
Sarah Sharpb10de142009-04-27 19:58:50 -07003085 --num_trbs;
3086 running_total += trb_buff_len;
3087
3088 /* Calculate length for next transfer */
3089 addr += trb_buff_len;
3090 trb_buff_len = urb->transfer_buffer_length - running_total;
3091 if (trb_buff_len > TRB_MAX_BUFF_SIZE)
3092 trb_buff_len = TRB_MAX_BUFF_SIZE;
3093 } while (running_total < urb->transfer_buffer_length);
3094
Sarah Sharp8a96c052009-04-27 19:59:19 -07003095 check_trb_math(urb, num_trbs, running_total);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003096 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003097 start_cycle, start_trb);
Sarah Sharpb10de142009-04-27 19:58:50 -07003098 return 0;
3099}
3100
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003101/* Caller must have locked xhci->lock */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003102int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003103 struct urb *urb, int slot_id, unsigned int ep_index)
3104{
3105 struct xhci_ring *ep_ring;
3106 int num_trbs;
3107 int ret;
3108 struct usb_ctrlrequest *setup;
3109 struct xhci_generic_trb *start_trb;
3110 int start_cycle;
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003111 u32 field, length_field;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003112 struct urb_priv *urb_priv;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003113 struct xhci_td *td;
3114
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003115 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
3116 if (!ep_ring)
3117 return -EINVAL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003118
3119 /*
3120 * Need to copy setup packet into setup TRB, so we can't use the setup
3121 * DMA address.
3122 */
3123 if (!urb->setup_packet)
3124 return -EINVAL;
3125
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003126 /* 1 TRB for setup, 1 for status */
3127 num_trbs = 2;
3128 /*
3129 * Don't need to check if we need additional event data and normal TRBs,
3130 * since data in control transfers will never get bigger than 16MB
3131 * XXX: can we get a buffer that crosses 64KB boundaries?
3132 */
3133 if (urb->transfer_buffer_length > 0)
3134 num_trbs++;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003135 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3136 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003137 num_trbs, urb, 0, mem_flags);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003138 if (ret < 0)
3139 return ret;
3140
Andiry Xu8e51adc2010-07-22 15:23:31 -07003141 urb_priv = urb->hcpriv;
3142 td = urb_priv->td[0];
3143
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003144 /*
3145 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3146 * until we've finished creating all the other TRBs. The ring's cycle
3147 * state may change as we enqueue the other TRBs, so save it too.
3148 */
3149 start_trb = &ep_ring->enqueue->generic;
3150 start_cycle = ep_ring->cycle_state;
3151
3152 /* Queue setup TRB - see section 6.4.1.2.1 */
3153 /* FIXME better way to translate setup_packet into two u32 fields? */
3154 setup = (struct usb_ctrlrequest *) urb->setup_packet;
Andiry Xu50f7b522010-12-20 15:09:34 +08003155 field = 0;
3156 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3157 if (start_cycle == 0)
3158 field |= 0x1;
Andiry Xub83cdc82011-05-05 18:13:56 +08003159
3160 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
3161 if (xhci->hci_version == 0x100) {
3162 if (urb->transfer_buffer_length > 0) {
3163 if (setup->bRequestType & USB_DIR_IN)
3164 field |= TRB_TX_TYPE(TRB_DATA_IN);
3165 else
3166 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3167 }
3168 }
3169
Andiry Xu3b72fca2012-03-05 17:49:32 +08003170 queue_trb(xhci, ep_ring, true,
Matt Evans28ccd292011-03-29 13:40:46 +11003171 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3172 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3173 TRB_LEN(8) | TRB_INTR_TARGET(0),
3174 /* Immediate data in pointer */
3175 field);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003176
3177 /* If there's data, queue data TRBs */
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003178 /* Only set interrupt on short packet for IN endpoints */
3179 if (usb_urb_dir_in(urb))
3180 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3181 else
3182 field = TRB_TYPE(TRB_DATA);
3183
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003184 length_field = TRB_LEN(urb->transfer_buffer_length) |
Sarah Sharp04dd9502009-11-11 10:28:30 -08003185 xhci_td_remainder(urb->transfer_buffer_length) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003186 TRB_INTR_TARGET(0);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003187 if (urb->transfer_buffer_length > 0) {
3188 if (setup->bRequestType & USB_DIR_IN)
3189 field |= TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003190 queue_trb(xhci, ep_ring, true,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003191 lower_32_bits(urb->transfer_dma),
3192 upper_32_bits(urb->transfer_dma),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003193 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003194 field | ep_ring->cycle_state);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003195 }
3196
3197 /* Save the DMA address of the last TRB in the TD */
3198 td->last_trb = ep_ring->enqueue;
3199
3200 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3201 /* If the device sent data, the status stage is an OUT transfer */
3202 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3203 field = 0;
3204 else
3205 field = TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003206 queue_trb(xhci, ep_ring, false,
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003207 0,
3208 0,
3209 TRB_INTR_TARGET(0),
3210 /* Event on completion */
3211 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3212
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003213 giveback_first_trb(xhci, slot_id, ep_index, 0,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003214 start_cycle, start_trb);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003215 return 0;
3216}
3217
Andiry Xu04e51902010-07-22 15:23:39 -07003218static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
3219 struct urb *urb, int i)
3220{
3221 int num_trbs = 0;
Sarah Sharp48df4a62011-08-12 10:23:01 -07003222 u64 addr, td_len;
Andiry Xu04e51902010-07-22 15:23:39 -07003223
3224 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
3225 td_len = urb->iso_frame_desc[i].length;
3226
Sarah Sharp48df4a62011-08-12 10:23:01 -07003227 num_trbs = DIV_ROUND_UP(td_len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
3228 TRB_MAX_BUFF_SIZE);
3229 if (num_trbs == 0)
Andiry Xu04e51902010-07-22 15:23:39 -07003230 num_trbs++;
3231
Andiry Xu04e51902010-07-22 15:23:39 -07003232 return num_trbs;
3233}
3234
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003235/*
3236 * The transfer burst count field of the isochronous TRB defines the number of
3237 * bursts that are required to move all packets in this TD. Only SuperSpeed
3238 * devices can burst up to bMaxBurst number of packets per service interval.
3239 * This field is zero based, meaning a value of zero in the field means one
3240 * burst. Basically, for everything but SuperSpeed devices, this field will be
3241 * zero. Only xHCI 1.0 host controllers support this field.
3242 */
3243static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
3244 struct usb_device *udev,
3245 struct urb *urb, unsigned int total_packet_count)
3246{
3247 unsigned int max_burst;
3248
3249 if (xhci->hci_version < 0x100 || udev->speed != USB_SPEED_SUPER)
3250 return 0;
3251
3252 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3253 return roundup(total_packet_count, max_burst + 1) - 1;
3254}
3255
Sarah Sharpb61d3782011-04-19 17:43:33 -07003256/*
3257 * Returns the number of packets in the last "burst" of packets. This field is
3258 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3259 * the last burst packet count is equal to the total number of packets in the
3260 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3261 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3262 * contain 1 to (bMaxBurst + 1) packets.
3263 */
3264static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
3265 struct usb_device *udev,
3266 struct urb *urb, unsigned int total_packet_count)
3267{
3268 unsigned int max_burst;
3269 unsigned int residue;
3270
3271 if (xhci->hci_version < 0x100)
3272 return 0;
3273
3274 switch (udev->speed) {
3275 case USB_SPEED_SUPER:
3276 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3277 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3278 residue = total_packet_count % (max_burst + 1);
3279 /* If residue is zero, the last burst contains (max_burst + 1)
3280 * number of packets, but the TLBPC field is zero-based.
3281 */
3282 if (residue == 0)
3283 return max_burst;
3284 return residue - 1;
3285 default:
3286 if (total_packet_count == 0)
3287 return 0;
3288 return total_packet_count - 1;
3289 }
3290}
3291
Andiry Xu04e51902010-07-22 15:23:39 -07003292/* This is for isoc transfer */
3293static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3294 struct urb *urb, int slot_id, unsigned int ep_index)
3295{
3296 struct xhci_ring *ep_ring;
3297 struct urb_priv *urb_priv;
3298 struct xhci_td *td;
3299 int num_tds, trbs_per_td;
3300 struct xhci_generic_trb *start_trb;
3301 bool first_trb;
3302 int start_cycle;
3303 u32 field, length_field;
3304 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3305 u64 start_addr, addr;
3306 int i, j;
Andiry Xu47cbf692010-12-20 14:49:48 +08003307 bool more_trbs_coming;
Andiry Xu04e51902010-07-22 15:23:39 -07003308
3309 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3310
3311 num_tds = urb->number_of_packets;
3312 if (num_tds < 1) {
3313 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3314 return -EINVAL;
3315 }
3316
Andiry Xu04e51902010-07-22 15:23:39 -07003317 start_addr = (u64) urb->transfer_dma;
3318 start_trb = &ep_ring->enqueue->generic;
3319 start_cycle = ep_ring->cycle_state;
3320
Sarah Sharp522989a2011-07-29 12:44:32 -07003321 urb_priv = urb->hcpriv;
Andiry Xu04e51902010-07-22 15:23:39 -07003322 /* Queue the first TRB, even if it's zero-length */
3323 for (i = 0; i < num_tds; i++) {
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003324 unsigned int total_packet_count;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003325 unsigned int burst_count;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003326 unsigned int residue;
Andiry Xu04e51902010-07-22 15:23:39 -07003327
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003328 first_trb = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003329 running_total = 0;
3330 addr = start_addr + urb->iso_frame_desc[i].offset;
3331 td_len = urb->iso_frame_desc[i].length;
3332 td_remain_len = td_len;
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003333 total_packet_count = roundup(td_len,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003334 usb_endpoint_maxp(&urb->ep->desc));
Sarah Sharp48df4a62011-08-12 10:23:01 -07003335 /* A zero-length transfer still involves at least one packet. */
3336 if (total_packet_count == 0)
3337 total_packet_count++;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003338 burst_count = xhci_get_burst_count(xhci, urb->dev, urb,
3339 total_packet_count);
Sarah Sharpb61d3782011-04-19 17:43:33 -07003340 residue = xhci_get_last_burst_packet_count(xhci,
3341 urb->dev, urb, total_packet_count);
Andiry Xu04e51902010-07-22 15:23:39 -07003342
3343 trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
3344
3345 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003346 urb->stream_id, trbs_per_td, urb, i, mem_flags);
Sarah Sharp522989a2011-07-29 12:44:32 -07003347 if (ret < 0) {
3348 if (i == 0)
3349 return ret;
3350 goto cleanup;
3351 }
Andiry Xu04e51902010-07-22 15:23:39 -07003352
Andiry Xu04e51902010-07-22 15:23:39 -07003353 td = urb_priv->td[i];
Andiry Xu04e51902010-07-22 15:23:39 -07003354 for (j = 0; j < trbs_per_td; j++) {
3355 u32 remainder = 0;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003356 field = TRB_TBC(burst_count) | TRB_TLBPC(residue);
Andiry Xu04e51902010-07-22 15:23:39 -07003357
3358 if (first_trb) {
3359 /* Queue the isoc TRB */
3360 field |= TRB_TYPE(TRB_ISOC);
3361 /* Assume URB_ISO_ASAP is set */
3362 field |= TRB_SIA;
Andiry Xu50f7b522010-12-20 15:09:34 +08003363 if (i == 0) {
3364 if (start_cycle == 0)
3365 field |= 0x1;
3366 } else
Andiry Xu04e51902010-07-22 15:23:39 -07003367 field |= ep_ring->cycle_state;
3368 first_trb = false;
3369 } else {
3370 /* Queue other normal TRBs */
3371 field |= TRB_TYPE(TRB_NORMAL);
3372 field |= ep_ring->cycle_state;
3373 }
3374
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003375 /* Only set interrupt on short packet for IN EPs */
3376 if (usb_urb_dir_in(urb))
3377 field |= TRB_ISP;
3378
Andiry Xu04e51902010-07-22 15:23:39 -07003379 /* Chain all the TRBs together; clear the chain bit in
3380 * the last TRB to indicate it's the last TRB in the
3381 * chain.
3382 */
3383 if (j < trbs_per_td - 1) {
3384 field |= TRB_CHAIN;
Andiry Xu47cbf692010-12-20 14:49:48 +08003385 more_trbs_coming = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003386 } else {
3387 td->last_trb = ep_ring->enqueue;
3388 field |= TRB_IOC;
Andiry Xuad106f22011-05-05 18:14:02 +08003389 if (xhci->hci_version == 0x100) {
3390 /* Set BEI bit except for the last td */
3391 if (i < num_tds - 1)
3392 field |= TRB_BEI;
3393 }
Andiry Xu47cbf692010-12-20 14:49:48 +08003394 more_trbs_coming = false;
Andiry Xu04e51902010-07-22 15:23:39 -07003395 }
3396
3397 /* Calculate TRB length */
3398 trb_buff_len = TRB_MAX_BUFF_SIZE -
3399 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
3400 if (trb_buff_len > td_remain_len)
3401 trb_buff_len = td_remain_len;
3402
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003403 /* Set the TRB length, TD size, & interrupter fields. */
3404 if (xhci->hci_version < 0x100) {
3405 remainder = xhci_td_remainder(
3406 td_len - running_total);
3407 } else {
3408 remainder = xhci_v1_0_td_remainder(
3409 running_total, trb_buff_len,
3410 total_packet_count, urb);
3411 }
Andiry Xu04e51902010-07-22 15:23:39 -07003412 length_field = TRB_LEN(trb_buff_len) |
3413 remainder |
3414 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003415
Andiry Xu3b72fca2012-03-05 17:49:32 +08003416 queue_trb(xhci, ep_ring, more_trbs_coming,
Andiry Xu04e51902010-07-22 15:23:39 -07003417 lower_32_bits(addr),
3418 upper_32_bits(addr),
3419 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003420 field);
Andiry Xu04e51902010-07-22 15:23:39 -07003421 running_total += trb_buff_len;
3422
3423 addr += trb_buff_len;
3424 td_remain_len -= trb_buff_len;
3425 }
3426
3427 /* Check TD length */
3428 if (running_total != td_len) {
3429 xhci_err(xhci, "ISOC TD length unmatch\n");
Andiry Xucf840552012-01-18 17:47:12 +08003430 ret = -EINVAL;
3431 goto cleanup;
Andiry Xu04e51902010-07-22 15:23:39 -07003432 }
3433 }
3434
Andiry Xuc41136b2011-03-22 17:08:14 +08003435 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
3436 if (xhci->quirks & XHCI_AMD_PLL_FIX)
3437 usb_amd_quirk_pll_disable();
3438 }
3439 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
3440
Andiry Xue1eab2e2011-01-04 16:30:39 -08003441 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3442 start_cycle, start_trb);
Andiry Xu04e51902010-07-22 15:23:39 -07003443 return 0;
Sarah Sharp522989a2011-07-29 12:44:32 -07003444cleanup:
3445 /* Clean up a partially enqueued isoc transfer. */
3446
3447 for (i--; i >= 0; i--)
Sarah Sharp585df1d2011-08-02 15:43:40 -07003448 list_del_init(&urb_priv->td[i]->td_list);
Sarah Sharp522989a2011-07-29 12:44:32 -07003449
3450 /* Use the first TD as a temporary variable to turn the TDs we've queued
3451 * into No-ops with a software-owned cycle bit. That way the hardware
3452 * won't accidentally start executing bogus TDs when we partially
3453 * overwrite them. td->first_trb and td->start_seg are already set.
3454 */
3455 urb_priv->td[0]->last_trb = ep_ring->enqueue;
3456 /* Every TRB except the first & last will have its cycle bit flipped. */
3457 td_to_noop(xhci, ep_ring, urb_priv->td[0], true);
3458
3459 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
3460 ep_ring->enqueue = urb_priv->td[0]->first_trb;
3461 ep_ring->enq_seg = urb_priv->td[0]->start_seg;
3462 ep_ring->cycle_state = start_cycle;
Andiry Xub008df62012-03-05 17:49:34 +08003463 ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp;
Sarah Sharp522989a2011-07-29 12:44:32 -07003464 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
3465 return ret;
Andiry Xu04e51902010-07-22 15:23:39 -07003466}
3467
3468/*
3469 * Check transfer ring to guarantee there is enough room for the urb.
3470 * Update ISO URB start_frame and interval.
3471 * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
3472 * update the urb->start_frame by now.
3473 * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
3474 */
3475int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3476 struct urb *urb, int slot_id, unsigned int ep_index)
3477{
3478 struct xhci_virt_device *xdev;
3479 struct xhci_ring *ep_ring;
3480 struct xhci_ep_ctx *ep_ctx;
3481 int start_frame;
3482 int xhci_interval;
3483 int ep_interval;
3484 int num_tds, num_trbs, i;
3485 int ret;
3486
3487 xdev = xhci->devs[slot_id];
3488 ep_ring = xdev->eps[ep_index].ring;
3489 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3490
3491 num_trbs = 0;
3492 num_tds = urb->number_of_packets;
3493 for (i = 0; i < num_tds; i++)
3494 num_trbs += count_isoc_trbs_needed(xhci, urb, i);
3495
3496 /* Check the ring to guarantee there is enough room for the whole urb.
3497 * Do not insert any td of the urb to the ring if the check failed.
3498 */
Matt Evans28ccd292011-03-29 13:40:46 +11003499 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003500 num_trbs, mem_flags);
Andiry Xu04e51902010-07-22 15:23:39 -07003501 if (ret)
3502 return ret;
3503
3504 start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
3505 start_frame &= 0x3fff;
3506
3507 urb->start_frame = start_frame;
3508 if (urb->dev->speed == USB_SPEED_LOW ||
3509 urb->dev->speed == USB_SPEED_FULL)
3510 urb->start_frame >>= 3;
3511
Matt Evans28ccd292011-03-29 13:40:46 +11003512 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Andiry Xu04e51902010-07-22 15:23:39 -07003513 ep_interval = urb->interval;
3514 /* Convert to microframes */
3515 if (urb->dev->speed == USB_SPEED_LOW ||
3516 urb->dev->speed == USB_SPEED_FULL)
3517 ep_interval *= 8;
3518 /* FIXME change this to a warning and a suggestion to use the new API
3519 * to set the polling interval (once the API is added).
3520 */
3521 if (xhci_interval != ep_interval) {
Andiry Xu7961acd2010-12-20 17:14:20 +08003522 if (printk_ratelimit())
Andiry Xu04e51902010-07-22 15:23:39 -07003523 dev_dbg(&urb->dev->dev, "Driver uses different interval"
3524 " (%d microframe%s) than xHCI "
3525 "(%d microframe%s)\n",
3526 ep_interval,
3527 ep_interval == 1 ? "" : "s",
3528 xhci_interval,
3529 xhci_interval == 1 ? "" : "s");
3530 urb->interval = xhci_interval;
3531 /* Convert back to frames for LS/FS devices */
3532 if (urb->dev->speed == USB_SPEED_LOW ||
3533 urb->dev->speed == USB_SPEED_FULL)
3534 urb->interval /= 8;
3535 }
Andiry Xub008df62012-03-05 17:49:34 +08003536 ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free;
3537
Dan Carpenter3fc82062012-03-28 10:30:26 +03003538 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
Andiry Xu04e51902010-07-22 15:23:39 -07003539}
3540
Sarah Sharpd0e96f52009-04-27 19:58:01 -07003541/**** Command Ring Operations ****/
3542
Sarah Sharp913a8a32009-09-04 10:53:13 -07003543/* Generic function for queueing a command TRB on the command ring.
3544 * Check to make sure there's room on the command ring for one command TRB.
3545 * Also check that there's room reserved for commands that must not fail.
3546 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3547 * then only check for the number of reserved spots.
3548 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3549 * because the command event handler may want to resubmit a failed command.
3550 */
3551static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
3552 u32 field3, u32 field4, bool command_must_succeed)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003553{
Sarah Sharp913a8a32009-09-04 10:53:13 -07003554 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003555 int ret;
3556
Sarah Sharp913a8a32009-09-04 10:53:13 -07003557 if (!command_must_succeed)
3558 reserved_trbs++;
3559
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003560 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003561 reserved_trbs, GFP_ATOMIC);
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003562 if (ret < 0) {
3563 xhci_err(xhci, "ERR: No room for command on command ring\n");
Sarah Sharp913a8a32009-09-04 10:53:13 -07003564 if (command_must_succeed)
3565 xhci_err(xhci, "ERR: Reserved TRB counting for "
3566 "unfailable commands failed.\n");
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003567 return ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003568 }
Andiry Xu3b72fca2012-03-05 17:49:32 +08003569 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
3570 field4 | xhci->cmd_ring->cycle_state);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003571 return 0;
3572}
3573
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003574/* Queue a slot enable or disable request on the command ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003575int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003576{
3577 return queue_command(xhci, 0, 0, 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003578 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003579}
3580
3581/* Queue an address device command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003582int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3583 u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003584{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003585 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3586 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003587 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
3588 false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003589}
Sarah Sharpf94e01862009-04-27 19:58:38 -07003590
Sarah Sharp02386342010-05-24 13:25:28 -07003591int xhci_queue_vendor_command(struct xhci_hcd *xhci,
3592 u32 field1, u32 field2, u32 field3, u32 field4)
3593{
3594 return queue_command(xhci, field1, field2, field3, field4, false);
3595}
3596
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003597/* Queue a reset device command TRB */
3598int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
3599{
3600 return queue_command(xhci, 0, 0, 0,
3601 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
3602 false);
3603}
3604
Sarah Sharpf94e01862009-04-27 19:58:38 -07003605/* Queue a configure endpoint command TRB */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003606int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003607 u32 slot_id, bool command_must_succeed)
Sarah Sharpf94e01862009-04-27 19:58:38 -07003608{
Sarah Sharp8e595a52009-07-27 12:03:31 -07003609 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3610 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003611 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3612 command_must_succeed);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003613}
Sarah Sharpae636742009-04-29 19:02:31 -07003614
Sarah Sharpf2217e82009-08-07 14:04:43 -07003615/* Queue an evaluate context command TRB */
3616int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
3617 u32 slot_id)
3618{
3619 return queue_command(xhci, lower_32_bits(in_ctx_ptr),
3620 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003621 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
3622 false);
Sarah Sharpf2217e82009-08-07 14:04:43 -07003623}
3624
Andiry Xube88fe42010-10-14 07:22:57 -07003625/*
3626 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3627 * activity on an endpoint that is about to be suspended.
3628 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003629int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
Andiry Xube88fe42010-10-14 07:22:57 -07003630 unsigned int ep_index, int suspend)
Sarah Sharpae636742009-04-29 19:02:31 -07003631{
3632 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3633 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3634 u32 type = TRB_TYPE(TRB_STOP_RING);
Andiry Xube88fe42010-10-14 07:22:57 -07003635 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
Sarah Sharpae636742009-04-29 19:02:31 -07003636
3637 return queue_command(xhci, 0, 0, 0,
Andiry Xube88fe42010-10-14 07:22:57 -07003638 trb_slot_id | trb_ep_index | type | trb_suspend, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003639}
3640
3641/* Set Transfer Ring Dequeue Pointer command.
3642 * This should not be used for endpoints that have streams enabled.
3643 */
3644static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003645 unsigned int ep_index, unsigned int stream_id,
3646 struct xhci_segment *deq_seg,
Sarah Sharpae636742009-04-29 19:02:31 -07003647 union xhci_trb *deq_ptr, u32 cycle_state)
3648{
3649 dma_addr_t addr;
3650 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3651 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003652 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
Sarah Sharpae636742009-04-29 19:02:31 -07003653 u32 type = TRB_TYPE(TRB_SET_DEQ);
Sarah Sharpbf161e82011-02-23 15:46:42 -08003654 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07003655
Sarah Sharp23e3be12009-04-29 19:05:20 -07003656 addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003657 if (addr == 0) {
Sarah Sharpae636742009-04-29 19:02:31 -07003658 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003659 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
3660 deq_seg, deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003661 return 0;
3662 }
Sarah Sharpbf161e82011-02-23 15:46:42 -08003663 ep = &xhci->devs[slot_id]->eps[ep_index];
3664 if ((ep->ep_state & SET_DEQ_PENDING)) {
3665 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
3666 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
3667 return 0;
3668 }
3669 ep->queued_deq_seg = deq_seg;
3670 ep->queued_deq_ptr = deq_ptr;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003671 return queue_command(xhci, lower_32_bits(addr) | cycle_state,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003672 upper_32_bits(addr), trb_stream_id,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003673 trb_slot_id | trb_ep_index | type, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003674}
Sarah Sharpa1587d92009-07-27 12:03:15 -07003675
3676int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
3677 unsigned int ep_index)
3678{
3679 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3680 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3681 u32 type = TRB_TYPE(TRB_RESET_EP);
3682
Sarah Sharp913a8a32009-09-04 10:53:13 -07003683 return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
3684 false);
Sarah Sharpa1587d92009-07-27 12:03:15 -07003685}