blob: 21e1dd62ebf81917f66b78d315b11342f61c6d98 [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>
Mathias Nymanf9c589e2016-06-21 10:58:02 +030069#include <linux/dma-mapping.h>
Sarah Sharp7f84eef2009-04-27 19:53:56 -070070#include "xhci.h"
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +030071#include "xhci-trace.h"
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020072#include "xhci-mtk.h"
Sarah Sharp7f84eef2009-04-27 19:53:56 -070073
74/*
75 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
76 * address of the TRB.
77 */
Sarah Sharp23e3be12009-04-29 19:05:20 -070078dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
Sarah Sharp7f84eef2009-04-27 19:53:56 -070079 union xhci_trb *trb)
80{
Sarah Sharp6071d832009-05-14 11:44:14 -070081 unsigned long segment_offset;
Sarah Sharp7f84eef2009-04-27 19:53:56 -070082
Sarah Sharp6071d832009-05-14 11:44:14 -070083 if (!seg || !trb || trb < seg->trbs)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070084 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070085 /* offset in TRBs */
86 segment_offset = trb - seg->trbs;
Mathias Nyman78950862015-08-03 16:07:48 +030087 if (segment_offset >= TRBS_PER_SEGMENT)
Sarah Sharp7f84eef2009-04-27 19:53:56 -070088 return 0;
Sarah Sharp6071d832009-05-14 11:44:14 -070089 return seg->dma + (segment_offset * sizeof(*trb));
Sarah Sharp7f84eef2009-04-27 19:53:56 -070090}
91
Mathias Nyman2d98ef42016-06-21 10:58:04 +030092static bool trb_is_link(union xhci_trb *trb)
93{
94 return TRB_TYPE_LINK_LE32(trb->link.control);
95}
96
Mathias Nymanbd5e67f2016-06-21 10:58:05 +030097static bool last_trb_on_seg(struct xhci_segment *seg, union xhci_trb *trb)
98{
99 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1];
100}
101
102static bool last_trb_on_ring(struct xhci_ring *ring,
103 struct xhci_segment *seg, union xhci_trb *trb)
104{
105 return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg);
106}
107
Mathias Nymand0c77d82016-06-21 10:58:07 +0300108static bool link_trb_toggles_cycle(union xhci_trb *trb)
109{
110 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
111}
112
Sarah Sharpae636742009-04-29 19:02:31 -0700113/* Updates trb to point to the next TRB in the ring, and updates seg if the next
114 * TRB is in a new segment. This does not skip over link TRBs, and it does not
115 * effect the ring dequeue or enqueue pointers.
116 */
117static void next_trb(struct xhci_hcd *xhci,
118 struct xhci_ring *ring,
119 struct xhci_segment **seg,
120 union xhci_trb **trb)
121{
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300122 if (trb_is_link(*trb)) {
Sarah Sharpae636742009-04-29 19:02:31 -0700123 *seg = (*seg)->next;
124 *trb = ((*seg)->trbs);
125 } else {
John Youna1669b22010-08-09 13:56:11 -0700126 (*trb)++;
Sarah Sharpae636742009-04-29 19:02:31 -0700127 }
128}
129
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700130/*
131 * See Cycle bit rules. SW is the consumer for the event ring only.
132 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
133 */
Andiry Xu3b72fca2012-03-05 17:49:32 +0800134static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700135{
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700136 ring->deq_updates++;
Andiry Xub008df62012-03-05 17:49:34 +0800137
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300138 /* event ring doesn't have link trbs, check for last trb */
139 if (ring->type == TYPE_EVENT) {
140 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
Sarah Sharp50d02062012-07-26 12:03:59 -0700141 ring->dequeue++;
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300142 return;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700143 }
Mathias Nymanbd5e67f2016-06-21 10:58:05 +0300144 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue))
145 ring->cycle_state ^= 1;
146 ring->deq_seg = ring->deq_seg->next;
147 ring->dequeue = ring->deq_seg->trbs;
148 return;
149 }
150
151 /* All other rings have link trbs */
152 if (!trb_is_link(ring->dequeue)) {
153 ring->dequeue++;
154 ring->num_trbs_free++;
155 }
156 while (trb_is_link(ring->dequeue)) {
157 ring->deq_seg = ring->deq_seg->next;
158 ring->dequeue = ring->deq_seg->trbs;
159 }
160 return;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700161}
162
163/*
164 * See Cycle bit rules. SW is the consumer for the event ring only.
165 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
166 *
167 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
168 * chain bit is set), then set the chain bit in all the following link TRBs.
169 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
170 * have their chain bit cleared (so that each Link TRB is a separate TD).
171 *
172 * 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 -0700173 * set, but other sections talk about dealing with the chain bit set. This was
174 * fixed in the 0.96 specification errata, but we have to assume that all 0.95
175 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700176 *
177 * @more_trbs_coming: Will you enqueue more TRBs before calling
178 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700179 */
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700180static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +0800181 bool more_trbs_coming)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700182{
183 u32 chain;
184 union xhci_trb *next;
185
Matt Evans28ccd292011-03-29 13:40:46 +1100186 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
Andiry Xub008df62012-03-05 17:49:34 +0800187 /* If this is not event ring, there is one less usable TRB */
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300188 if (!trb_is_link(ring->enqueue))
Andiry Xub008df62012-03-05 17:49:34 +0800189 ring->num_trbs_free--;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700190 next = ++(ring->enqueue);
191
192 ring->enq_updates++;
Mathias Nyman22511982016-06-21 10:58:03 +0300193 /* Update the dequeue pointer further if that was a link TRB */
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300194 while (trb_is_link(next)) {
Sarah Sharp6cc30d82010-06-10 12:25:28 -0700195
Mathias Nyman22511982016-06-21 10:58:03 +0300196 /*
197 * If the caller doesn't plan on enqueueing more TDs before
198 * ringing the doorbell, then we don't want to give the link TRB
199 * to the hardware just yet. We'll give the link TRB back in
200 * prepare_ring() just before we enqueue the TD at the top of
201 * the ring.
202 */
203 if (!chain && !more_trbs_coming)
204 break;
Andiry Xu3b72fca2012-03-05 17:49:32 +0800205
Mathias Nyman22511982016-06-21 10:58:03 +0300206 /* If we're not dealing with 0.95 hardware or isoc rings on
207 * AMD 0.96 host, carry over the chain bit of the previous TRB
208 * (which may mean the chain bit is cleared).
209 */
210 if (!(ring->type == TYPE_ISOC &&
211 (xhci->quirks & XHCI_AMD_0x96_HOST)) &&
212 !xhci_link_trb_quirk(xhci)) {
213 next->link.control &= cpu_to_le32(~TRB_CHAIN);
214 next->link.control |= cpu_to_le32(chain);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700215 }
Mathias Nyman22511982016-06-21 10:58:03 +0300216 /* Give this link TRB to the hardware */
217 wmb();
218 next->link.control ^= cpu_to_le32(TRB_CYCLE);
219
220 /* Toggle the cycle bit after the last ring segment. */
Mathias Nymand0c77d82016-06-21 10:58:07 +0300221 if (link_trb_toggles_cycle(next))
Mathias Nyman22511982016-06-21 10:58:03 +0300222 ring->cycle_state ^= 1;
223
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700224 ring->enq_seg = ring->enq_seg->next;
225 ring->enqueue = ring->enq_seg->trbs;
226 next = ring->enqueue;
227 }
228}
229
230/*
Andiry Xu085deb12012-03-05 17:49:40 +0800231 * Check to see if there's room to enqueue num_trbs on the ring and make sure
232 * enqueue pointer will not advance into dequeue segment. See rules above.
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700233 */
Andiry Xub008df62012-03-05 17:49:34 +0800234static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700235 unsigned int num_trbs)
236{
Andiry Xu085deb12012-03-05 17:49:40 +0800237 int num_trbs_in_deq_seg;
Andiry Xub008df62012-03-05 17:49:34 +0800238
Andiry Xu085deb12012-03-05 17:49:40 +0800239 if (ring->num_trbs_free < num_trbs)
240 return 0;
241
242 if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
243 num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
244 if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
245 return 0;
246 }
247
248 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700249}
250
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700251/* Ring the host controller doorbell after placing a command on the ring */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700252void xhci_ring_cmd_db(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700253{
Elric Fuc181bc52012-06-27 16:30:57 +0800254 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING))
255 return;
256
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700257 xhci_dbg(xhci, "// Ding dong!\n");
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200258 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700259 /* Flush PCI posted writes */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200260 readl(&xhci->dba->doorbell[0]);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700261}
262
Elric Fub92cc662012-06-27 16:31:12 +0800263static int xhci_abort_cmd_ring(struct xhci_hcd *xhci)
264{
265 u64 temp_64;
266 int ret;
267
268 xhci_dbg(xhci, "Abort command ring\n");
269
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800270 temp_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Elric Fub92cc662012-06-27 16:31:12 +0800271 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED;
Mathias Nyman3425aa02016-06-01 18:09:08 +0300272
273 /*
274 * Writing the CMD_RING_ABORT bit should cause a cmd completion event,
275 * however on some host hw the CMD_RING_RUNNING bit is correctly cleared
276 * but the completion event in never sent. Use the cmd timeout timer to
277 * handle those cases. Use twice the time to cover the bit polling retry
278 */
279 mod_timer(&xhci->cmd_timer, jiffies + (2 * XHCI_CMD_DEFAULT_TIMEOUT));
Sarah Sharp477632d2014-01-29 14:02:00 -0800280 xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
281 &xhci->op_regs->cmd_ring);
Elric Fub92cc662012-06-27 16:31:12 +0800282
283 /* Section 4.6.1.2 of xHCI 1.0 spec says software should
284 * time the completion od all xHCI commands, including
285 * the Command Abort operation. If software doesn't see
286 * CRR negated in a timely manner (e.g. longer than 5
287 * seconds), then it should assume that the there are
288 * larger problems with the xHC and assert HCRST.
289 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200290 ret = xhci_handshake(&xhci->op_regs->cmd_ring,
Elric Fub92cc662012-06-27 16:31:12 +0800291 CMD_RING_RUNNING, 0, 5 * 1000 * 1000);
292 if (ret < 0) {
Mathias Nymana6809ff2015-09-21 17:46:10 +0300293 /* we are about to kill xhci, give it one more chance */
294 xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
295 &xhci->op_regs->cmd_ring);
296 udelay(1000);
297 ret = xhci_handshake(&xhci->op_regs->cmd_ring,
298 CMD_RING_RUNNING, 0, 3 * 1000 * 1000);
299 if (ret == 0)
300 return 0;
301
Elric Fub92cc662012-06-27 16:31:12 +0800302 xhci_err(xhci, "Stopped the command ring failed, "
303 "maybe the host is dead\n");
Mathias Nyman3425aa02016-06-01 18:09:08 +0300304 del_timer(&xhci->cmd_timer);
Elric Fub92cc662012-06-27 16:31:12 +0800305 xhci->xhc_state |= XHCI_STATE_DYING;
306 xhci_quiesce(xhci);
307 xhci_halt(xhci);
308 return -ESHUTDOWN;
309 }
310
311 return 0;
312}
313
Andiry Xube88fe42010-10-14 07:22:57 -0700314void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700315 unsigned int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700316 unsigned int ep_index,
317 unsigned int stream_id)
Sarah Sharpae636742009-04-29 19:02:31 -0700318{
Matt Evans28ccd292011-03-29 13:40:46 +1100319 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
Matthew Wilcox50d646762010-12-15 14:18:11 -0500320 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
321 unsigned int ep_state = ep->ep_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700322
Sarah Sharpae636742009-04-29 19:02:31 -0700323 /* Don't ring the doorbell for this endpoint if there are pending
Matthew Wilcox50d646762010-12-15 14:18:11 -0500324 * cancellations because we don't want to interrupt processing.
Sarah Sharp8df75f42010-04-02 15:34:16 -0700325 * We don't want to restart any stream rings if there's a set dequeue
326 * pointer command pending because the device can choose to start any
327 * stream once the endpoint is on the HW schedule.
Sarah Sharpae636742009-04-29 19:02:31 -0700328 */
Matthew Wilcox50d646762010-12-15 14:18:11 -0500329 if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
330 (ep_state & EP_HALTED))
331 return;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200332 writel(DB_VALUE(ep_index, stream_id), db_addr);
Matthew Wilcox50d646762010-12-15 14:18:11 -0500333 /* The CPU has better things to do at this point than wait for a
334 * write-posting flush. It'll get there soon enough.
335 */
Sarah Sharpae636742009-04-29 19:02:31 -0700336}
337
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700338/* Ring the doorbell for any rings with pending URBs */
339static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
340 unsigned int slot_id,
341 unsigned int ep_index)
342{
343 unsigned int stream_id;
344 struct xhci_virt_ep *ep;
345
346 ep = &xhci->devs[slot_id]->eps[ep_index];
347
348 /* A ring has pending URBs if its TD list is not empty */
349 if (!(ep->ep_state & EP_HAS_STREAMS)) {
Oleksij Rempeld66eaf92013-07-21 15:36:19 +0200350 if (ep->ring && !(list_empty(&ep->ring->td_list)))
Andiry Xube88fe42010-10-14 07:22:57 -0700351 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700352 return;
353 }
354
355 for (stream_id = 1; stream_id < ep->stream_info->num_streams;
356 stream_id++) {
357 struct xhci_stream_info *stream_info = ep->stream_info;
358 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
Andiry Xube88fe42010-10-14 07:22:57 -0700359 xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
360 stream_id);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700361 }
362}
363
Alexandr Ivanov75b040e2016-04-22 13:17:10 +0300364/* Get the right ring for the given slot_id, ep_index and stream_id.
365 * If the endpoint supports streams, boundary check the URB's stream ID.
366 * If the endpoint doesn't support streams, return the singular endpoint ring.
367 */
368struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
Sarah Sharp021bff92010-07-29 22:12:20 -0700369 unsigned int slot_id, unsigned int ep_index,
370 unsigned int stream_id)
371{
372 struct xhci_virt_ep *ep;
373
374 ep = &xhci->devs[slot_id]->eps[ep_index];
375 /* Common case: no streams */
376 if (!(ep->ep_state & EP_HAS_STREAMS))
377 return ep->ring;
378
379 if (stream_id == 0) {
380 xhci_warn(xhci,
381 "WARN: Slot ID %u, ep index %u has streams, "
382 "but URB has no stream ID.\n",
383 slot_id, ep_index);
384 return NULL;
385 }
386
387 if (stream_id < ep->stream_info->num_streams)
388 return ep->stream_info->stream_rings[stream_id];
389
390 xhci_warn(xhci,
391 "WARN: Slot ID %u, ep index %u has "
392 "stream IDs 1 to %u allocated, "
393 "but stream ID %u is requested.\n",
394 slot_id, ep_index,
395 ep->stream_info->num_streams - 1,
396 stream_id);
397 return NULL;
398}
399
Sarah Sharpae636742009-04-29 19:02:31 -0700400/*
401 * Move the xHC's endpoint ring dequeue pointer past cur_td.
402 * Record the new state of the xHC's endpoint ring dequeue segment,
403 * dequeue pointer, and new consumer cycle state in state.
404 * Update our internal representation of the ring's dequeue pointer.
405 *
406 * We do this in three jumps:
407 * - First we update our new ring state to be the same as when the xHC stopped.
408 * - Then we traverse the ring to find the segment that contains
409 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
410 * any link TRBs with the toggle cycle bit set.
411 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
412 * if we've moved it past a link TRB with the toggle cycle bit set.
Matt Evans28ccd292011-03-29 13:40:46 +1100413 *
414 * Some of the uses of xhci_generic_trb are grotty, but if they're done
415 * with correct __le32 accesses they should work fine. Only users of this are
416 * in here.
Sarah Sharpae636742009-04-29 19:02:31 -0700417 */
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700418void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
Sarah Sharpae636742009-04-29 19:02:31 -0700419 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700420 unsigned int stream_id, struct xhci_td *cur_td,
421 struct xhci_dequeue_state *state)
Sarah Sharpae636742009-04-29 19:02:31 -0700422{
423 struct xhci_virt_device *dev = xhci->devs[slot_id];
Hans de Goedec4bedb72013-10-04 00:29:47 +0200424 struct xhci_virt_ep *ep = &dev->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700425 struct xhci_ring *ep_ring;
Mathias Nyman365038d2014-08-19 15:17:58 +0300426 struct xhci_segment *new_seg;
427 union xhci_trb *new_deq;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700428 dma_addr_t addr;
Julius Werner1f81b6d2014-04-25 19:20:13 +0300429 u64 hw_dequeue;
Mathias Nyman365038d2014-08-19 15:17:58 +0300430 bool cycle_found = false;
431 bool td_last_trb_found = false;
Sarah Sharpae636742009-04-29 19:02:31 -0700432
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700433 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
434 ep_index, stream_id);
435 if (!ep_ring) {
436 xhci_warn(xhci, "WARN can't find new dequeue state "
437 "for invalid stream ID %u.\n",
438 stream_id);
439 return;
440 }
Paul Zimmerman68e41c52011-02-12 14:06:06 -0800441
Sarah Sharpae636742009-04-29 19:02:31 -0700442 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300443 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
444 "Finding endpoint context");
Hans de Goedec4bedb72013-10-04 00:29:47 +0200445 /* 4.6.9 the css flag is written to the stream context for streams */
446 if (ep->ep_state & EP_HAS_STREAMS) {
447 struct xhci_stream_ctx *ctx =
448 &ep->stream_info->stream_ctx_array[stream_id];
Julius Werner1f81b6d2014-04-25 19:20:13 +0300449 hw_dequeue = le64_to_cpu(ctx->stream_ring);
Hans de Goedec4bedb72013-10-04 00:29:47 +0200450 } else {
451 struct xhci_ep_ctx *ep_ctx
452 = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
Julius Werner1f81b6d2014-04-25 19:20:13 +0300453 hw_dequeue = le64_to_cpu(ep_ctx->deq);
Hans de Goedec4bedb72013-10-04 00:29:47 +0200454 }
Sarah Sharpae636742009-04-29 19:02:31 -0700455
Mathias Nyman365038d2014-08-19 15:17:58 +0300456 new_seg = ep_ring->deq_seg;
457 new_deq = ep_ring->dequeue;
458 state->new_cycle_state = hw_dequeue & 0x1;
459
460 /*
461 * We want to find the pointer, segment and cycle state of the new trb
462 * (the one after current TD's last_trb). We know the cycle state at
463 * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are
464 * found.
465 */
466 do {
467 if (!cycle_found && xhci_trb_virt_to_dma(new_seg, new_deq)
468 == (dma_addr_t)(hw_dequeue & ~0xf)) {
469 cycle_found = true;
470 if (td_last_trb_found)
471 break;
472 }
473 if (new_deq == cur_td->last_trb)
474 td_last_trb_found = true;
475
476 if (cycle_found &&
477 TRB_TYPE_LINK_LE32(new_deq->generic.field[3]) &&
478 new_deq->generic.field[3] & cpu_to_le32(LINK_TOGGLE))
479 state->new_cycle_state ^= 0x1;
480
481 next_trb(xhci, ep_ring, &new_seg, &new_deq);
482
483 /* Search wrapped around, bail out */
484 if (new_deq == ep->ring->dequeue) {
485 xhci_err(xhci, "Error: Failed finding new dequeue state\n");
486 state->new_deq_seg = NULL;
487 state->new_deq_ptr = NULL;
Julius Werner1f81b6d2014-04-25 19:20:13 +0300488 return;
489 }
Julius Werner1f81b6d2014-04-25 19:20:13 +0300490
Mathias Nyman365038d2014-08-19 15:17:58 +0300491 } while (!cycle_found || !td_last_trb_found);
Sarah Sharpae636742009-04-29 19:02:31 -0700492
Mathias Nyman365038d2014-08-19 15:17:58 +0300493 state->new_deq_seg = new_seg;
494 state->new_deq_ptr = new_deq;
Sarah Sharpae636742009-04-29 19:02:31 -0700495
Julius Werner1f81b6d2014-04-25 19:20:13 +0300496 /* Don't update the ring cycle state for the producer (us). */
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300497 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
498 "Cycle state = 0x%x", state->new_cycle_state);
Sarah Sharp01a1fdb2011-02-23 18:12:29 -0800499
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300500 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
501 "New dequeue segment = %p (virtual)",
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700502 state->new_deq_seg);
503 addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300504 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
505 "New dequeue pointer = 0x%llx (DMA)",
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700506 (unsigned long long) addr);
Sarah Sharpae636742009-04-29 19:02:31 -0700507}
508
Sarah Sharp522989a2011-07-29 12:44:32 -0700509/* flip_cycle means flip the cycle bit of all but the first and last TRB.
510 * (The last TRB actually points to the ring enqueue pointer, which is not part
511 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
512 */
Sarah Sharp23e3be12009-04-29 19:05:20 -0700513static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Sarah Sharp522989a2011-07-29 12:44:32 -0700514 struct xhci_td *cur_td, bool flip_cycle)
Sarah Sharpae636742009-04-29 19:02:31 -0700515{
516 struct xhci_segment *cur_seg;
517 union xhci_trb *cur_trb;
518
519 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
520 true;
521 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +1000522 if (TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) {
Sarah Sharpae636742009-04-29 19:02:31 -0700523 /* Unchain any chained Link TRBs, but
524 * leave the pointers intact.
525 */
Matt Evans28ccd292011-03-29 13:40:46 +1100526 cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN);
Sarah Sharp522989a2011-07-29 12:44:32 -0700527 /* Flip the cycle bit (link TRBs can't be the first
528 * or last TRB).
529 */
530 if (flip_cycle)
531 cur_trb->generic.field[3] ^=
532 cpu_to_le32(TRB_CYCLE);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300533 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
534 "Cancel (unchain) link TRB");
535 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
536 "Address = %p (0x%llx dma); "
537 "in seg %p (0x%llx dma)",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700538 cur_trb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700539 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700540 cur_seg,
541 (unsigned long long)cur_seg->dma);
Sarah Sharpae636742009-04-29 19:02:31 -0700542 } else {
543 cur_trb->generic.field[0] = 0;
544 cur_trb->generic.field[1] = 0;
545 cur_trb->generic.field[2] = 0;
546 /* Preserve only the cycle bit of this TRB */
Matt Evans28ccd292011-03-29 13:40:46 +1100547 cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
Sarah Sharp522989a2011-07-29 12:44:32 -0700548 /* Flip the cycle bit except on the first or last TRB */
549 if (flip_cycle && cur_trb != cur_td->first_trb &&
550 cur_trb != cur_td->last_trb)
551 cur_trb->generic.field[3] ^=
552 cpu_to_le32(TRB_CYCLE);
Matt Evans28ccd292011-03-29 13:40:46 +1100553 cur_trb->generic.field[3] |= cpu_to_le32(
554 TRB_TYPE(TRB_TR_NOOP));
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300555 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
556 "TRB to noop at offset 0x%llx",
Sarah Sharp79688ac2011-12-19 16:56:04 -0800557 (unsigned long long)
558 xhci_trb_virt_to_dma(cur_seg, cur_trb));
Sarah Sharpae636742009-04-29 19:02:31 -0700559 }
560 if (cur_trb == cur_td->last_trb)
561 break;
562 }
563}
564
Dmitry Torokhov575688e2011-03-20 02:15:16 -0700565static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700566 struct xhci_virt_ep *ep)
567{
568 ep->ep_state &= ~EP_HALT_PENDING;
569 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
570 * timer is running on another CPU, we don't decrement stop_cmds_pending
571 * (since we didn't successfully stop the watchdog timer).
572 */
573 if (del_timer(&ep->stop_cmd_timer))
574 ep->stop_cmds_pending--;
575}
576
577/* Must be called with xhci->lock held in interrupt context */
578static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
Xenia Ragiadakou07a37e92013-09-09 13:29:45 +0300579 struct xhci_td *cur_td, int status)
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700580{
Sarah Sharp214f76f2010-10-26 11:22:02 -0700581 struct usb_hcd *hcd;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700582 struct urb *urb;
583 struct urb_priv *urb_priv;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700584
Andiry Xu8e51adc2010-07-22 15:23:31 -0700585 urb = cur_td->urb;
586 urb_priv = urb->hcpriv;
587 urb_priv->td_cnt++;
Sarah Sharp214f76f2010-10-26 11:22:02 -0700588 hcd = bus_to_hcd(urb->dev->bus);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700589
Andiry Xu8e51adc2010-07-22 15:23:31 -0700590 /* Only giveback urb when this is the last td in urb */
591 if (urb_priv->td_cnt == urb_priv->length) {
Andiry Xuc41136b2011-03-22 17:08:14 +0800592 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
593 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
594 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
595 if (xhci->quirks & XHCI_AMD_PLL_FIX)
596 usb_amd_quirk_pll_enable();
597 }
598 }
Andiry Xu8e51adc2010-07-22 15:23:31 -0700599 usb_hcd_unlink_urb_from_ep(hcd, urb);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700600
601 spin_unlock(&xhci->lock);
602 usb_hcd_giveback_urb(hcd, urb, status);
Lin Wang4daf9df2015-01-09 16:06:31 +0200603 xhci_urb_free_priv(urb_priv);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700604 spin_lock(&xhci->lock);
Andiry Xu8e51adc2010-07-22 15:23:31 -0700605 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700606}
607
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300608void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci, struct xhci_ring *ring,
609 struct xhci_td *td)
610{
611 struct device *dev = xhci_to_hcd(xhci)->self.controller;
612 struct xhci_segment *seg = td->bounce_seg;
613 struct urb *urb = td->urb;
614
615 if (!seg || !urb)
616 return;
617
618 if (usb_urb_dir_out(urb)) {
619 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
620 DMA_TO_DEVICE);
621 return;
622 }
623
624 /* for in tranfers we need to copy the data from bounce to sg */
625 sg_pcopy_from_buffer(urb->sg, urb->num_mapped_sgs, seg->bounce_buf,
626 seg->bounce_len, seg->bounce_offs);
627 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
628 DMA_FROM_DEVICE);
629 seg->bounce_len = 0;
630 seg->bounce_offs = 0;
631}
632
Sarah Sharpae636742009-04-29 19:02:31 -0700633/*
634 * When we get a command completion for a Stop Endpoint Command, we need to
635 * unlink any cancelled TDs from the ring. There are two ways to do that:
636 *
637 * 1. If the HW was in the middle of processing the TD that needs to be
638 * cancelled, then we must move the ring's dequeue pointer past the last TRB
639 * in the TD with a Set Dequeue Pointer Command.
640 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
641 * bit cleared) so that the HW will skip over them.
642 */
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +0300643static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id,
Andiry Xube88fe42010-10-14 07:22:57 -0700644 union xhci_trb *trb, struct xhci_event_cmd *event)
Sarah Sharpae636742009-04-29 19:02:31 -0700645{
Sarah Sharpae636742009-04-29 19:02:31 -0700646 unsigned int ep_index;
647 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700648 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700649 struct list_head *entry;
Randy Dunlap326b4812010-04-19 08:53:50 -0700650 struct xhci_td *cur_td = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700651 struct xhci_td *last_unlinked_td;
652
Sarah Sharpc92bcfa2009-07-27 12:05:21 -0700653 struct xhci_dequeue_state deq_state;
Sarah Sharpae636742009-04-29 19:02:31 -0700654
Xenia Ragiadakoubc752bd2013-09-09 13:29:59 +0300655 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) {
Mathias Nyman9ea18332014-05-08 19:26:02 +0300656 if (!xhci->devs[slot_id])
Andiry Xube88fe42010-10-14 07:22:57 -0700657 xhci_warn(xhci, "Stop endpoint command "
658 "completion for disabled slot %u\n",
659 slot_id);
660 return;
661 }
662
Sarah Sharpae636742009-04-29 19:02:31 -0700663 memset(&deq_state, 0, sizeof(deq_state));
Matt Evans28ccd292011-03-29 13:40:46 +1100664 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700665 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharpae636742009-04-29 19:02:31 -0700666
Sarah Sharp678539c2009-10-27 10:55:52 -0700667 if (list_empty(&ep->cancelled_td_list)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700668 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharp0714a572011-05-24 11:53:29 -0700669 ep->stopped_td = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700670 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700671 return;
Sarah Sharp678539c2009-10-27 10:55:52 -0700672 }
Sarah Sharpae636742009-04-29 19:02:31 -0700673
674 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
675 * We have the xHCI lock, so nothing can modify this list until we drop
676 * it. We're also in the event handler, so we can't get re-interrupted
677 * if another Stop Endpoint command completes
678 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700679 list_for_each(entry, &ep->cancelled_td_list) {
Sarah Sharpae636742009-04-29 19:02:31 -0700680 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300681 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
682 "Removing canceled TD starting at 0x%llx (dma).",
Sarah Sharp79688ac2011-12-19 16:56:04 -0800683 (unsigned long long)xhci_trb_virt_to_dma(
684 cur_td->start_seg, cur_td->first_trb));
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700685 ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
686 if (!ep_ring) {
687 /* This shouldn't happen unless a driver is mucking
688 * with the stream ID after submission. This will
689 * leave the TD on the hardware ring, and the hardware
690 * will try to execute it, and may access a buffer
691 * that has already been freed. In the best case, the
692 * hardware will execute it, and the event handler will
693 * ignore the completion event for that TD, since it was
694 * removed from the td_list for that endpoint. In
695 * short, don't muck with the stream ID after
696 * submission.
697 */
698 xhci_warn(xhci, "WARN Cancelled URB %p "
699 "has invalid stream ID %u.\n",
700 cur_td->urb,
701 cur_td->urb->stream_id);
702 goto remove_finished_td;
703 }
Sarah Sharpae636742009-04-29 19:02:31 -0700704 /*
705 * If we stopped on the TD we need to cancel, then we have to
706 * move the xHC endpoint ring dequeue pointer past this TD.
707 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700708 if (cur_td == ep->stopped_td)
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700709 xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
710 cur_td->urb->stream_id,
711 cur_td, &deq_state);
Sarah Sharpae636742009-04-29 19:02:31 -0700712 else
Sarah Sharp522989a2011-07-29 12:44:32 -0700713 td_to_noop(xhci, ep_ring, cur_td, false);
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700714remove_finished_td:
Sarah Sharpae636742009-04-29 19:02:31 -0700715 /*
716 * The event handler won't see a completion for this TD anymore,
717 * so remove it from the endpoint ring's TD list. Keep it in
718 * the cancelled TD list for URB completion later.
719 */
Sarah Sharp585df1d2011-08-02 15:43:40 -0700720 list_del_init(&cur_td->td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700721 }
722 last_unlinked_td = cur_td;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700723 xhci_stop_watchdog_timer_in_irq(xhci, ep);
Sarah Sharpae636742009-04-29 19:02:31 -0700724
725 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
726 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
Hans de Goede1e3452e2014-08-20 16:41:52 +0300727 xhci_queue_new_dequeue_state(xhci, slot_id, ep_index,
728 ep->stopped_td->urb->stream_id, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700729 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700730 } else {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700731 /* Otherwise ring the doorbell(s) to restart queued transfers */
732 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -0700733 }
Florian Wolter526867c2013-08-14 10:33:16 +0200734
Mathias Nymand97b4f82014-11-27 18:19:16 +0200735 ep->stopped_td = NULL;
Sarah Sharpae636742009-04-29 19:02:31 -0700736
737 /*
738 * Drop the lock and complete the URBs in the cancelled TD list.
739 * New TDs to be cancelled might be added to the end of the list before
740 * we can complete all the URBs for the TDs we already unlinked.
741 * So stop when we've completed the URB for the last TD we unlinked.
742 */
743 do {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700744 cur_td = list_entry(ep->cancelled_td_list.next,
Sarah Sharpae636742009-04-29 19:02:31 -0700745 struct xhci_td, cancelled_td_list);
Sarah Sharp585df1d2011-08-02 15:43:40 -0700746 list_del_init(&cur_td->cancelled_td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700747
748 /* Clean up the cancelled URB */
Sarah Sharpae636742009-04-29 19:02:31 -0700749 /* Doesn't matter what we pass for status, since the core will
750 * just overwrite it (because the URB has been unlinked).
751 */
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300752 if (ep_ring && cur_td->bounce_seg)
753 xhci_unmap_td_bounce_buffer(xhci, ep_ring, cur_td);
Xenia Ragiadakou07a37e92013-09-09 13:29:45 +0300754 xhci_giveback_urb_in_irq(xhci, cur_td, 0);
Sarah Sharpae636742009-04-29 19:02:31 -0700755
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700756 /* Stop processing the cancelled list if the watchdog timer is
757 * running.
758 */
759 if (xhci->xhc_state & XHCI_STATE_DYING)
760 return;
Sarah Sharpae636742009-04-29 19:02:31 -0700761 } while (cur_td != last_unlinked_td);
762
763 /* Return to the event handler with xhci->lock re-acquired */
764}
765
Sarah Sharp50e87252014-02-21 09:27:30 -0800766static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring)
767{
768 struct xhci_td *cur_td;
769
770 while (!list_empty(&ring->td_list)) {
771 cur_td = list_first_entry(&ring->td_list,
772 struct xhci_td, td_list);
773 list_del_init(&cur_td->td_list);
774 if (!list_empty(&cur_td->cancelled_td_list))
775 list_del_init(&cur_td->cancelled_td_list);
Mathias Nymanf9c589e2016-06-21 10:58:02 +0300776
777 if (cur_td->bounce_seg)
778 xhci_unmap_td_bounce_buffer(xhci, ring, cur_td);
Sarah Sharp50e87252014-02-21 09:27:30 -0800779 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
780 }
781}
782
783static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci,
784 int slot_id, int ep_index)
785{
786 struct xhci_td *cur_td;
787 struct xhci_virt_ep *ep;
788 struct xhci_ring *ring;
789
790 ep = &xhci->devs[slot_id]->eps[ep_index];
Sarah Sharp21d0e512014-02-21 14:29:02 -0800791 if ((ep->ep_state & EP_HAS_STREAMS) ||
792 (ep->ep_state & EP_GETTING_NO_STREAMS)) {
793 int stream_id;
794
795 for (stream_id = 0; stream_id < ep->stream_info->num_streams;
796 stream_id++) {
797 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
798 "Killing URBs for slot ID %u, ep index %u, stream %u",
799 slot_id, ep_index, stream_id + 1);
800 xhci_kill_ring_urbs(xhci,
801 ep->stream_info->stream_rings[stream_id]);
802 }
803 } else {
804 ring = ep->ring;
805 if (!ring)
806 return;
807 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
808 "Killing URBs for slot ID %u, ep index %u",
809 slot_id, ep_index);
810 xhci_kill_ring_urbs(xhci, ring);
811 }
Sarah Sharp50e87252014-02-21 09:27:30 -0800812 while (!list_empty(&ep->cancelled_td_list)) {
813 cur_td = list_first_entry(&ep->cancelled_td_list,
814 struct xhci_td, cancelled_td_list);
815 list_del_init(&cur_td->cancelled_td_list);
816 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
817 }
818}
819
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700820/* Watchdog timer function for when a stop endpoint command fails to complete.
821 * In this case, we assume the host controller is broken or dying or dead. The
822 * host may still be completing some other events, so we have to be careful to
823 * let the event ring handler and the URB dequeueing/enqueueing functions know
824 * through xhci->state.
825 *
826 * The timer may also fire if the host takes a very long time to respond to the
827 * command, and the stop endpoint command completion handler cannot delete the
828 * timer before the timer function is called. Another endpoint cancellation may
829 * sneak in before the timer function can grab the lock, and that may queue
830 * another stop endpoint command and add the timer back. So we cannot use a
831 * simple flag to say whether there is a pending stop endpoint command for a
832 * particular endpoint.
833 *
834 * Instead we use a combination of that flag and a counter for the number of
835 * pending stop endpoint commands. If the timer is the tail end of the last
836 * stop endpoint command, and the endpoint's command is still pending, we assume
837 * the host is dying.
838 */
839void xhci_stop_endpoint_command_watchdog(unsigned long arg)
840{
841 struct xhci_hcd *xhci;
842 struct xhci_virt_ep *ep;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700843 int ret, i, j;
Don Zickusf43d6232011-10-20 23:52:14 -0400844 unsigned long flags;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700845
846 ep = (struct xhci_virt_ep *) arg;
847 xhci = ep->xhci;
848
Don Zickusf43d6232011-10-20 23:52:14 -0400849 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700850
851 ep->stop_cmds_pending--;
852 if (xhci->xhc_state & XHCI_STATE_DYING) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300853 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
854 "Stop EP timer ran, but another timer marked "
855 "xHCI as DYING, exiting.");
Don Zickusf43d6232011-10-20 23:52:14 -0400856 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700857 return;
858 }
859 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300860 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
861 "Stop EP timer ran, but no command pending, "
862 "exiting.");
Don Zickusf43d6232011-10-20 23:52:14 -0400863 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700864 return;
865 }
866
867 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
868 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
869 /* Oops, HC is dead or dying or at least not responding to the stop
870 * endpoint command.
871 */
872 xhci->xhc_state |= XHCI_STATE_DYING;
873 /* Disable interrupts from the host controller and start halting it */
874 xhci_quiesce(xhci);
Don Zickusf43d6232011-10-20 23:52:14 -0400875 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700876
877 ret = xhci_halt(xhci);
878
Don Zickusf43d6232011-10-20 23:52:14 -0400879 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700880 if (ret < 0) {
881 /* This is bad; the host is not responding to commands and it's
882 * not allowing itself to be halted. At least interrupts are
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800883 * disabled. If we call usb_hc_died(), it will attempt to
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700884 * disconnect all device drivers under this host. Those
885 * disconnect() methods will wait for all URBs to be unlinked,
886 * so we must complete them.
887 */
888 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
889 xhci_warn(xhci, "Completing active URBs anyway.\n");
890 /* We could turn all TDs on the rings to no-ops. This won't
891 * help if the host has cached part of the ring, and is slow if
892 * we want to preserve the cycle bit. Skip it and hope the host
893 * doesn't touch the memory.
894 */
895 }
896 for (i = 0; i < MAX_HC_SLOTS; i++) {
897 if (!xhci->devs[i])
898 continue;
Sarah Sharp50e87252014-02-21 09:27:30 -0800899 for (j = 0; j < 31; j++)
900 xhci_kill_endpoint_urbs(xhci, i, j);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700901 }
Don Zickusf43d6232011-10-20 23:52:14 -0400902 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300903 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
904 "Calling usb_hc_died()");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800905 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +0300906 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
907 "xHCI host controller is dead.");
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700908}
909
Andiry Xub008df62012-03-05 17:49:34 +0800910
911static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci,
912 struct xhci_virt_device *dev,
913 struct xhci_ring *ep_ring,
914 unsigned int ep_index)
915{
916 union xhci_trb *dequeue_temp;
917 int num_trbs_free_temp;
918 bool revert = false;
919
920 num_trbs_free_temp = ep_ring->num_trbs_free;
921 dequeue_temp = ep_ring->dequeue;
922
Sarah Sharp0d9f78a2012-06-21 16:28:30 -0700923 /* If we get two back-to-back stalls, and the first stalled transfer
924 * ends just before a link TRB, the dequeue pointer will be left on
925 * the link TRB by the code in the while loop. So we have to update
926 * the dequeue pointer one segment further, or we'll jump off
927 * the segment into la-la-land.
928 */
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300929 if (trb_is_link(ep_ring->dequeue)) {
Sarah Sharp0d9f78a2012-06-21 16:28:30 -0700930 ep_ring->deq_seg = ep_ring->deq_seg->next;
931 ep_ring->dequeue = ep_ring->deq_seg->trbs;
932 }
933
Andiry Xub008df62012-03-05 17:49:34 +0800934 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) {
935 /* We have more usable TRBs */
936 ep_ring->num_trbs_free++;
937 ep_ring->dequeue++;
Mathias Nyman2d98ef42016-06-21 10:58:04 +0300938 if (trb_is_link(ep_ring->dequeue)) {
Andiry Xub008df62012-03-05 17:49:34 +0800939 if (ep_ring->dequeue ==
940 dev->eps[ep_index].queued_deq_ptr)
941 break;
942 ep_ring->deq_seg = ep_ring->deq_seg->next;
943 ep_ring->dequeue = ep_ring->deq_seg->trbs;
944 }
945 if (ep_ring->dequeue == dequeue_temp) {
946 revert = true;
947 break;
948 }
949 }
950
951 if (revert) {
952 xhci_dbg(xhci, "Unable to find new dequeue pointer\n");
953 ep_ring->num_trbs_free = num_trbs_free_temp;
954 }
955}
956
Sarah Sharpae636742009-04-29 19:02:31 -0700957/*
958 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
959 * we need to clear the set deq pending flag in the endpoint ring state, so that
960 * the TD queueing code can ring the doorbell again. We also need to ring the
961 * endpoint doorbell to restart the ring, but only if there aren't more
962 * cancellations pending.
963 */
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +0300964static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +0300965 union xhci_trb *trb, u32 cmd_comp_code)
Sarah Sharpae636742009-04-29 19:02:31 -0700966{
Sarah Sharpae636742009-04-29 19:02:31 -0700967 unsigned int ep_index;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700968 unsigned int stream_id;
Sarah Sharpae636742009-04-29 19:02:31 -0700969 struct xhci_ring *ep_ring;
970 struct xhci_virt_device *dev;
Hans de Goede9aad95e2013-10-04 00:29:49 +0200971 struct xhci_virt_ep *ep;
John Yound115b042009-07-27 12:05:15 -0700972 struct xhci_ep_ctx *ep_ctx;
973 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpae636742009-04-29 19:02:31 -0700974
Matt Evans28ccd292011-03-29 13:40:46 +1100975 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
976 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
Sarah Sharpae636742009-04-29 19:02:31 -0700977 dev = xhci->devs[slot_id];
Hans de Goede9aad95e2013-10-04 00:29:49 +0200978 ep = &dev->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700979
980 ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
981 if (!ep_ring) {
Oliver Neukume587b8b2014-01-08 17:13:11 +0100982 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n",
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700983 stream_id);
984 /* XXX: Harmless??? */
Hans de Goede0d4976e2014-08-20 16:41:55 +0300985 goto cleanup;
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700986 }
987
John Yound115b042009-07-27 12:05:15 -0700988 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
989 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
Sarah Sharpae636742009-04-29 19:02:31 -0700990
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +0300991 if (cmd_comp_code != COMP_SUCCESS) {
Sarah Sharpae636742009-04-29 19:02:31 -0700992 unsigned int ep_state;
993 unsigned int slot_state;
994
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +0300995 switch (cmd_comp_code) {
Sarah Sharpae636742009-04-29 19:02:31 -0700996 case COMP_TRB_ERR:
Oliver Neukume587b8b2014-01-08 17:13:11 +0100997 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n");
Sarah Sharpae636742009-04-29 19:02:31 -0700998 break;
999 case COMP_CTX_STATE:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001000 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n");
Matt Evans28ccd292011-03-29 13:40:46 +11001001 ep_state = le32_to_cpu(ep_ctx->ep_info);
Sarah Sharpae636742009-04-29 19:02:31 -07001002 ep_state &= EP_STATE_MASK;
Matt Evans28ccd292011-03-29 13:40:46 +11001003 slot_state = le32_to_cpu(slot_ctx->dev_state);
Sarah Sharpae636742009-04-29 19:02:31 -07001004 slot_state = GET_SLOT_STATE(slot_state);
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001005 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1006 "Slot state = %u, EP state = %u",
Sarah Sharpae636742009-04-29 19:02:31 -07001007 slot_state, ep_state);
1008 break;
1009 case COMP_EBADSLT:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001010 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n",
1011 slot_id);
Sarah Sharpae636742009-04-29 19:02:31 -07001012 break;
1013 default:
Oliver Neukume587b8b2014-01-08 17:13:11 +01001014 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n",
1015 cmd_comp_code);
Sarah Sharpae636742009-04-29 19:02:31 -07001016 break;
1017 }
1018 /* OK what do we do now? The endpoint state is hosed, and we
1019 * should never get to this point if the synchronization between
1020 * queueing, and endpoint state are correct. This might happen
1021 * if the device gets disconnected after we've finished
1022 * cancelling URBs, which might not be an error...
1023 */
1024 } else {
Hans de Goede9aad95e2013-10-04 00:29:49 +02001025 u64 deq;
1026 /* 4.6.10 deq ptr is written to the stream ctx for streams */
1027 if (ep->ep_state & EP_HAS_STREAMS) {
1028 struct xhci_stream_ctx *ctx =
1029 &ep->stream_info->stream_ctx_array[stream_id];
1030 deq = le64_to_cpu(ctx->stream_ring) & SCTX_DEQ_MASK;
1031 } else {
1032 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK;
1033 }
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001034 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
Hans de Goede9aad95e2013-10-04 00:29:49 +02001035 "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
1036 if (xhci_trb_virt_to_dma(ep->queued_deq_seg,
1037 ep->queued_deq_ptr) == deq) {
Sarah Sharpbf161e82011-02-23 15:46:42 -08001038 /* Update the ring's dequeue segment and dequeue pointer
1039 * to reflect the new position.
1040 */
Andiry Xub008df62012-03-05 17:49:34 +08001041 update_ring_for_set_deq_completion(xhci, dev,
1042 ep_ring, ep_index);
Sarah Sharpbf161e82011-02-23 15:46:42 -08001043 } else {
Oliver Neukume587b8b2014-01-08 17:13:11 +01001044 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n");
Sarah Sharpbf161e82011-02-23 15:46:42 -08001045 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n",
Hans de Goede9aad95e2013-10-04 00:29:49 +02001046 ep->queued_deq_seg, ep->queued_deq_ptr);
Sarah Sharpbf161e82011-02-23 15:46:42 -08001047 }
Sarah Sharpae636742009-04-29 19:02:31 -07001048 }
1049
Hans de Goede0d4976e2014-08-20 16:41:55 +03001050cleanup:
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001051 dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
Sarah Sharpbf161e82011-02-23 15:46:42 -08001052 dev->eps[ep_index].queued_deq_seg = NULL;
1053 dev->eps[ep_index].queued_deq_ptr = NULL;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001054 /* Restart any rings with pending URBs */
1055 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
Sarah Sharpae636742009-04-29 19:02:31 -07001056}
1057
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001058static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id,
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001059 union xhci_trb *trb, u32 cmd_comp_code)
Sarah Sharpa1587d92009-07-27 12:03:15 -07001060{
Sarah Sharpa1587d92009-07-27 12:03:15 -07001061 unsigned int ep_index;
1062
Matt Evans28ccd292011-03-29 13:40:46 +11001063 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
Sarah Sharpa1587d92009-07-27 12:03:15 -07001064 /* This command will only fail if the endpoint wasn't halted,
1065 * but we don't care.
1066 */
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03001067 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001068 "Ignoring reset ep completion code of %u", cmd_comp_code);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001069
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001070 /* HW with the reset endpoint quirk needs to have a configure endpoint
1071 * command complete before the endpoint can be used. Queue that here
1072 * because the HW can't handle two commands being queued in a row.
1073 */
1074 if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001075 struct xhci_command *command;
1076 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
Hans de Goedea0ee6192014-07-25 22:01:21 +02001077 if (!command) {
1078 xhci_warn(xhci, "WARN Cannot submit cfg ep: ENOMEM\n");
1079 return;
1080 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001081 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1082 "Queueing configure endpoint command");
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001083 xhci_queue_configure_endpoint(xhci, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001084 xhci->devs[slot_id]->in_ctx->dma, slot_id,
1085 false);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001086 xhci_ring_cmd_db(xhci);
1087 } else {
Mathias Nymanc3492db2014-11-18 11:27:11 +02001088 /* Clear our internal halted state */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001089 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001090 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001091}
Sarah Sharpae636742009-04-29 19:02:31 -07001092
Xenia Ragiadakoub244b432013-09-09 13:29:47 +03001093static void xhci_handle_cmd_enable_slot(struct xhci_hcd *xhci, int slot_id,
1094 u32 cmd_comp_code)
1095{
1096 if (cmd_comp_code == COMP_SUCCESS)
1097 xhci->slot_id = slot_id;
1098 else
1099 xhci->slot_id = 0;
Xenia Ragiadakoub244b432013-09-09 13:29:47 +03001100}
1101
Xenia Ragiadakou6c02dd12013-09-09 13:29:48 +03001102static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id)
1103{
1104 struct xhci_virt_device *virt_dev;
1105
1106 virt_dev = xhci->devs[slot_id];
1107 if (!virt_dev)
1108 return;
1109 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
1110 /* Delete default control endpoint resources */
1111 xhci_free_device_endpoint_resources(xhci, virt_dev, true);
1112 xhci_free_virt_device(xhci, slot_id);
1113}
1114
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001115static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id,
1116 struct xhci_event_cmd *event, u32 cmd_comp_code)
1117{
1118 struct xhci_virt_device *virt_dev;
1119 struct xhci_input_control_ctx *ctrl_ctx;
1120 unsigned int ep_index;
1121 unsigned int ep_state;
1122 u32 add_flags, drop_flags;
1123
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001124 /*
1125 * Configure endpoint commands can come from the USB core
1126 * configuration or alt setting changes, or because the HW
1127 * needed an extra configure endpoint command after a reset
1128 * endpoint command or streams were being configured.
1129 * If the command was for a halted endpoint, the xHCI driver
1130 * is not waiting on the configure endpoint command.
1131 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03001132 virt_dev = xhci->devs[slot_id];
Lin Wang4daf9df2015-01-09 16:06:31 +02001133 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001134 if (!ctrl_ctx) {
1135 xhci_warn(xhci, "Could not get input context, bad type.\n");
1136 return;
1137 }
1138
1139 add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1140 drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1141 /* Input ctx add_flags are the endpoint index plus one */
1142 ep_index = xhci_last_valid_endpoint(add_flags) - 1;
1143
1144 /* A usb_set_interface() call directly after clearing a halted
1145 * condition may race on this quirky hardware. Not worth
1146 * worrying about, since this is prototype hardware. Not sure
1147 * if this will work for streams, but streams support was
1148 * untested on this prototype.
1149 */
1150 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
1151 ep_index != (unsigned int) -1 &&
1152 add_flags - SLOT_FLAG == drop_flags) {
1153 ep_state = virt_dev->eps[ep_index].ep_state;
1154 if (!(ep_state & EP_HALTED))
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001155 return;
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001156 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1157 "Completed config ep cmd - "
1158 "last ep index = %d, state = %d",
1159 ep_index, ep_state);
1160 /* Clear internal halted state and restart ring(s) */
1161 virt_dev->eps[ep_index].ep_state &= ~EP_HALTED;
1162 ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
1163 return;
1164 }
Xenia Ragiadakou6ed46d32013-09-09 13:29:55 +03001165 return;
1166}
1167
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001168static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id,
1169 struct xhci_event_cmd *event)
1170{
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001171 xhci_dbg(xhci, "Completed reset device command.\n");
Mathias Nyman9ea18332014-05-08 19:26:02 +03001172 if (!xhci->devs[slot_id])
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001173 xhci_warn(xhci, "Reset device command completion "
1174 "for disabled slot %u\n", slot_id);
1175}
1176
Xenia Ragiadakou2c070822013-09-09 13:29:52 +03001177static void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci,
1178 struct xhci_event_cmd *event)
1179{
1180 if (!(xhci->quirks & XHCI_NEC_HOST)) {
1181 xhci->error_bitmask |= 1 << 6;
1182 return;
1183 }
1184 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1185 "NEC firmware version %2x.%02x",
1186 NEC_FW_MAJOR(le32_to_cpu(event->status)),
1187 NEC_FW_MINOR(le32_to_cpu(event->status)));
1188}
1189
Mathias Nyman9ea18332014-05-08 19:26:02 +03001190static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 status)
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001191{
1192 list_del(&cmd->cmd_list);
Mathias Nyman9ea18332014-05-08 19:26:02 +03001193
1194 if (cmd->completion) {
1195 cmd->status = status;
1196 complete(cmd->completion);
1197 } else {
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001198 kfree(cmd);
Mathias Nyman9ea18332014-05-08 19:26:02 +03001199 }
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001200}
1201
1202void xhci_cleanup_command_queue(struct xhci_hcd *xhci)
1203{
1204 struct xhci_command *cur_cmd, *tmp_cmd;
1205 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list)
Mathias Nyman9ea18332014-05-08 19:26:02 +03001206 xhci_complete_del_and_free_cmd(cur_cmd, COMP_CMD_ABORT);
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001207}
1208
Mathias Nymanc311e392014-05-08 19:26:03 +03001209/*
1210 * Turn all commands on command ring with status set to "aborted" to no-op trbs.
1211 * If there are other commands waiting then restart the ring and kick the timer.
1212 * This must be called with command ring stopped and xhci->lock held.
1213 */
1214static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci,
1215 struct xhci_command *cur_cmd)
1216{
1217 struct xhci_command *i_cmd, *tmp_cmd;
1218 u32 cycle_state;
1219
1220 /* Turn all aborted commands in list to no-ops, then restart */
1221 list_for_each_entry_safe(i_cmd, tmp_cmd, &xhci->cmd_list,
1222 cmd_list) {
1223
1224 if (i_cmd->status != COMP_CMD_ABORT)
1225 continue;
1226
1227 i_cmd->status = COMP_CMD_STOP;
1228
1229 xhci_dbg(xhci, "Turn aborted command %p to no-op\n",
1230 i_cmd->command_trb);
1231 /* get cycle state from the original cmd trb */
1232 cycle_state = le32_to_cpu(
1233 i_cmd->command_trb->generic.field[3]) & TRB_CYCLE;
1234 /* modify the command trb to no-op command */
1235 i_cmd->command_trb->generic.field[0] = 0;
1236 i_cmd->command_trb->generic.field[1] = 0;
1237 i_cmd->command_trb->generic.field[2] = 0;
1238 i_cmd->command_trb->generic.field[3] = cpu_to_le32(
1239 TRB_TYPE(TRB_CMD_NOOP) | cycle_state);
1240
1241 /*
1242 * caller waiting for completion is called when command
1243 * completion event is received for these no-op commands
1244 */
1245 }
1246
1247 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
1248
1249 /* ring command ring doorbell to restart the command ring */
1250 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) &&
1251 !(xhci->xhc_state & XHCI_STATE_DYING)) {
1252 xhci->current_cmd = cur_cmd;
1253 mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
1254 xhci_ring_cmd_db(xhci);
1255 }
1256 return;
1257}
1258
1259
1260void xhci_handle_command_timeout(unsigned long data)
1261{
1262 struct xhci_hcd *xhci;
1263 int ret;
1264 unsigned long flags;
1265 u64 hw_ring_state;
Mathias Nyman3425aa02016-06-01 18:09:08 +03001266 bool second_timeout = false;
Mathias Nymanc311e392014-05-08 19:26:03 +03001267 xhci = (struct xhci_hcd *) data;
1268
1269 /* mark this command to be cancelled */
1270 spin_lock_irqsave(&xhci->lock, flags);
1271 if (xhci->current_cmd) {
Mathias Nyman3425aa02016-06-01 18:09:08 +03001272 if (xhci->current_cmd->status == COMP_CMD_ABORT)
1273 second_timeout = true;
1274 xhci->current_cmd->status = COMP_CMD_ABORT;
Mathias Nymanc311e392014-05-08 19:26:03 +03001275 }
1276
Mathias Nymanc311e392014-05-08 19:26:03 +03001277 /* Make sure command ring is running before aborting it */
1278 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
1279 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
1280 (hw_ring_state & CMD_RING_RUNNING)) {
Mathias Nymanc311e392014-05-08 19:26:03 +03001281 spin_unlock_irqrestore(&xhci->lock, flags);
1282 xhci_dbg(xhci, "Command timeout\n");
1283 ret = xhci_abort_cmd_ring(xhci);
1284 if (unlikely(ret == -ESHUTDOWN)) {
1285 xhci_err(xhci, "Abort command ring failed\n");
1286 xhci_cleanup_command_queue(xhci);
1287 usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
1288 xhci_dbg(xhci, "xHCI host controller is dead.\n");
1289 }
1290 return;
1291 }
Mathias Nyman3425aa02016-06-01 18:09:08 +03001292
1293 /* command ring failed to restart, or host removed. Bail out */
1294 if (second_timeout || xhci->xhc_state & XHCI_STATE_REMOVING) {
1295 spin_unlock_irqrestore(&xhci->lock, flags);
1296 xhci_dbg(xhci, "command timed out twice, ring start fail?\n");
1297 xhci_cleanup_command_queue(xhci);
1298 return;
1299 }
1300
Mathias Nymanc311e392014-05-08 19:26:03 +03001301 /* command timeout on stopped ring, ring can't be aborted */
1302 xhci_dbg(xhci, "Command timeout on stopped ring\n");
1303 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd);
1304 spin_unlock_irqrestore(&xhci->lock, flags);
1305 return;
1306}
1307
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001308static void handle_cmd_completion(struct xhci_hcd *xhci,
1309 struct xhci_event_cmd *event)
1310{
Matt Evans28ccd292011-03-29 13:40:46 +11001311 int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001312 u64 cmd_dma;
1313 dma_addr_t cmd_dequeue_dma;
Xenia Ragiadakoue7a79a12013-09-09 13:29:56 +03001314 u32 cmd_comp_code;
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001315 union xhci_trb *cmd_trb;
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001316 struct xhci_command *cmd;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001317 u32 cmd_type;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001318
Matt Evans28ccd292011-03-29 13:40:46 +11001319 cmd_dma = le64_to_cpu(event->cmd_trb);
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001320 cmd_trb = xhci->cmd_ring->dequeue;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001321 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001322 cmd_trb);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001323 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
1324 if (cmd_dequeue_dma == 0) {
1325 xhci->error_bitmask |= 1 << 4;
1326 return;
1327 }
1328 /* Does the DMA address match our internal dequeue pointer address? */
1329 if (cmd_dma != (u64) cmd_dequeue_dma) {
1330 xhci->error_bitmask |= 1 << 5;
1331 return;
1332 }
Elric Fub63f4052012-06-27 16:55:43 +08001333
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001334 cmd = list_entry(xhci->cmd_list.next, struct xhci_command, cmd_list);
1335
1336 if (cmd->command_trb != xhci->cmd_ring->dequeue) {
1337 xhci_err(xhci,
1338 "Command completion event does not match command\n");
1339 return;
1340 }
Mathias Nymanc311e392014-05-08 19:26:03 +03001341
1342 del_timer(&xhci->cmd_timer);
1343
Xenia Ragiadakou9124b122013-09-09 13:29:57 +03001344 trace_xhci_cmd_completion(cmd_trb, (struct xhci_generic_trb *) event);
Xenia Ragiadakou63a23b9a2013-08-06 07:52:48 +03001345
Xenia Ragiadakoue7a79a12013-09-09 13:29:56 +03001346 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status));
Mathias Nymanc311e392014-05-08 19:26:03 +03001347
1348 /* If CMD ring stopped we own the trbs between enqueue and dequeue */
1349 if (cmd_comp_code == COMP_CMD_STOP) {
1350 xhci_handle_stopped_cmd_ring(xhci, cmd);
1351 return;
1352 }
1353 /*
1354 * Host aborted the command ring, check if the current command was
1355 * supposed to be aborted, otherwise continue normally.
1356 * The command ring is stopped now, but the xHC will issue a Command
1357 * Ring Stopped event which will cause us to restart it.
1358 */
1359 if (cmd_comp_code == COMP_CMD_ABORT) {
1360 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
1361 if (cmd->status == COMP_CMD_ABORT)
1362 goto event_handled;
Elric Fub63f4052012-06-27 16:55:43 +08001363 }
1364
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001365 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3]));
1366 switch (cmd_type) {
1367 case TRB_ENABLE_SLOT:
Xenia Ragiadakoue7a79a12013-09-09 13:29:56 +03001368 xhci_handle_cmd_enable_slot(xhci, slot_id, cmd_comp_code);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001369 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001370 case TRB_DISABLE_SLOT:
Xenia Ragiadakou6c02dd12013-09-09 13:29:48 +03001371 xhci_handle_cmd_disable_slot(xhci, slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001372 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001373 case TRB_CONFIG_EP:
Mathias Nyman9ea18332014-05-08 19:26:02 +03001374 if (!cmd->completion)
1375 xhci_handle_cmd_config_ep(xhci, slot_id, event,
1376 cmd_comp_code);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001377 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001378 case TRB_EVAL_CONTEXT:
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001379 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001380 case TRB_ADDR_DEV:
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001381 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001382 case TRB_STOP_RING:
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001383 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1384 le32_to_cpu(cmd_trb->generic.field[3])));
1385 xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb, event);
Sarah Sharpae636742009-04-29 19:02:31 -07001386 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001387 case TRB_SET_DEQ:
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001388 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1389 le32_to_cpu(cmd_trb->generic.field[3])));
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001390 xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code);
Sarah Sharpae636742009-04-29 19:02:31 -07001391 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001392 case TRB_CMD_NOOP:
Mathias Nymanc311e392014-05-08 19:26:03 +03001393 /* Is this an aborted command turned to NO-OP? */
1394 if (cmd->status == COMP_CMD_STOP)
1395 cmd_comp_code = COMP_CMD_STOP;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001396 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001397 case TRB_RESET_EP:
Xenia Ragiadakoub8200c92013-09-09 13:30:00 +03001398 WARN_ON(slot_id != TRB_TO_SLOT_ID(
1399 le32_to_cpu(cmd_trb->generic.field[3])));
Xenia Ragiadakouc69a0592013-09-09 13:30:01 +03001400 xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001401 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001402 case TRB_RESET_DEV:
Mathias Nyman6fcfb0d2014-06-24 17:14:40 +03001403 /* SLOT_ID field in reset device cmd completion event TRB is 0.
1404 * Use the SLOT_ID from the command TRB instead (xhci 4.6.11)
1405 */
1406 slot_id = TRB_TO_SLOT_ID(
1407 le32_to_cpu(cmd_trb->generic.field[3]));
Xenia Ragiadakouf6813212013-09-09 13:29:51 +03001408 xhci_handle_cmd_reset_dev(xhci, slot_id, event);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001409 break;
Xenia Ragiadakoub54fc462013-09-09 13:29:58 +03001410 case TRB_NEC_GET_FW:
Xenia Ragiadakou2c070822013-09-09 13:29:52 +03001411 xhci_handle_cmd_nec_get_fw(xhci, event);
Sarah Sharp02386342010-05-24 13:25:28 -07001412 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001413 default:
1414 /* Skip over unknown commands on the event ring */
1415 xhci->error_bitmask |= 1 << 6;
1416 break;
1417 }
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001418
Mathias Nymanc311e392014-05-08 19:26:03 +03001419 /* restart timer if this wasn't the last command */
1420 if (cmd->cmd_list.next != &xhci->cmd_list) {
1421 xhci->current_cmd = list_entry(cmd->cmd_list.next,
1422 struct xhci_command, cmd_list);
1423 mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
1424 }
1425
1426event_handled:
Mathias Nyman9ea18332014-05-08 19:26:02 +03001427 xhci_complete_del_and_free_cmd(cmd, cmd_comp_code);
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03001428
Andiry Xu3b72fca2012-03-05 17:49:32 +08001429 inc_deq(xhci, xhci->cmd_ring);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001430}
1431
Sarah Sharp02386342010-05-24 13:25:28 -07001432static void handle_vendor_event(struct xhci_hcd *xhci,
1433 union xhci_trb *event)
1434{
1435 u32 trb_type;
1436
Matt Evans28ccd292011-03-29 13:40:46 +11001437 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
Sarah Sharp02386342010-05-24 13:25:28 -07001438 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
1439 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
1440 handle_cmd_completion(xhci, &event->event_cmd);
1441}
1442
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001443/* @port_id: the one-based port ID from the hardware (indexed from array of all
1444 * port registers -- USB 3.0 and USB 2.0).
1445 *
1446 * Returns a zero-based port number, which is suitable for indexing into each of
1447 * the split roothubs' port arrays and bus state arrays.
Sarah Sharpd0cd5d42011-11-14 17:51:39 -08001448 * Add one to it in order to call xhci_find_slot_id_by_port.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001449 */
1450static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
1451 struct xhci_hcd *xhci, u32 port_id)
1452{
1453 unsigned int i;
1454 unsigned int num_similar_speed_ports = 0;
1455
1456 /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
1457 * and usb2_ports are 0-based indexes. Count the number of similar
1458 * speed ports, up to 1 port before this port.
1459 */
1460 for (i = 0; i < (port_id - 1); i++) {
1461 u8 port_speed = xhci->port_array[i];
1462
1463 /*
1464 * Skip ports that don't have known speeds, or have duplicate
1465 * Extended Capabilities port speed entries.
1466 */
Dan Carpenter22e04872011-03-17 22:39:49 +03001467 if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001468 continue;
1469
1470 /*
1471 * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
1472 * 1.1 ports are under the USB 2.0 hub. If the port speed
1473 * matches the device speed, it's a similar speed port.
1474 */
Mathias Nymanb50107b2015-10-01 18:40:38 +03001475 if ((port_speed == 0x03) == (hcd->speed >= HCD_USB3))
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001476 num_similar_speed_ports++;
1477 }
1478 return num_similar_speed_ports;
1479}
1480
Sarah Sharp623bef92011-11-11 14:57:33 -08001481static void handle_device_notification(struct xhci_hcd *xhci,
1482 union xhci_trb *event)
1483{
1484 u32 slot_id;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001485 struct usb_device *udev;
Sarah Sharp623bef92011-11-11 14:57:33 -08001486
Xenia Ragiadakou7e76ad42013-09-09 21:03:10 +03001487 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3]));
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001488 if (!xhci->devs[slot_id]) {
Sarah Sharp623bef92011-11-11 14:57:33 -08001489 xhci_warn(xhci, "Device Notification event for "
1490 "unused slot %u\n", slot_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001491 return;
1492 }
1493
1494 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
1495 slot_id);
1496 udev = xhci->devs[slot_id]->udev;
1497 if (udev && udev->parent)
1498 usb_wakeup_notification(udev->parent, udev->portnum);
Sarah Sharp623bef92011-11-11 14:57:33 -08001499}
1500
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001501static void handle_port_status(struct xhci_hcd *xhci,
1502 union xhci_trb *event)
1503{
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001504 struct usb_hcd *hcd;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001505 u32 port_id;
Andiry Xu56192532010-10-14 07:23:00 -07001506 u32 temp, temp1;
Sarah Sharp518e8482010-12-15 11:56:29 -08001507 int max_ports;
Andiry Xu56192532010-10-14 07:23:00 -07001508 int slot_id;
Sarah Sharp5308a912010-12-01 11:34:59 -08001509 unsigned int faked_port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001510 u8 major_revision;
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001511 struct xhci_bus_state *bus_state;
Matt Evans28ccd292011-03-29 13:40:46 +11001512 __le32 __iomem **port_array;
Sarah Sharp386139d2011-03-24 08:02:58 -07001513 bool bogus_port_status = false;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001514
1515 /* Port status change events always have a successful completion code */
Matt Evans28ccd292011-03-29 13:40:46 +11001516 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001517 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
1518 xhci->error_bitmask |= 1 << 8;
1519 }
Matt Evans28ccd292011-03-29 13:40:46 +11001520 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001521 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
1522
Sarah Sharp518e8482010-12-15 11:56:29 -08001523 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1524 if ((port_id <= 0) || (port_id > max_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001525 xhci_warn(xhci, "Invalid port id %d\n", port_id);
Peter Chen09ce0c02013-03-20 09:30:00 +08001526 inc_deq(xhci, xhci->event_ring);
1527 return;
Andiry Xu56192532010-10-14 07:23:00 -07001528 }
1529
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001530 /* Figure out which usb_hcd this port is attached to:
1531 * is it a USB 3.0 port or a USB 2.0/1.1 port?
1532 */
1533 major_revision = xhci->port_array[port_id - 1];
Peter Chen09ce0c02013-03-20 09:30:00 +08001534
1535 /* Find the right roothub. */
1536 hcd = xhci_to_hcd(xhci);
Mathias Nymanb50107b2015-10-01 18:40:38 +03001537 if ((major_revision == 0x03) != (hcd->speed >= HCD_USB3))
Peter Chen09ce0c02013-03-20 09:30:00 +08001538 hcd = xhci->shared_hcd;
1539
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001540 if (major_revision == 0) {
1541 xhci_warn(xhci, "Event for port %u not in "
1542 "Extended Capabilities, ignoring.\n",
1543 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001544 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001545 goto cleanup;
1546 }
Dan Carpenter22e04872011-03-17 22:39:49 +03001547 if (major_revision == DUPLICATE_ENTRY) {
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001548 xhci_warn(xhci, "Event for port %u duplicated in"
1549 "Extended Capabilities, ignoring.\n",
1550 port_id);
Sarah Sharp386139d2011-03-24 08:02:58 -07001551 bogus_port_status = true;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001552 goto cleanup;
Sarah Sharp5308a912010-12-01 11:34:59 -08001553 }
1554
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001555 /*
1556 * Hardware port IDs reported by a Port Status Change Event include USB
1557 * 3.0 and USB 2.0 ports. We want to check if the port has reported a
1558 * resume event, but we first need to translate the hardware port ID
1559 * into the index into the ports on the correct split roothub, and the
1560 * correct bus_state structure.
1561 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001562 bus_state = &xhci->bus_state[hcd_index(hcd)];
Mathias Nymanb50107b2015-10-01 18:40:38 +03001563 if (hcd->speed >= HCD_USB3)
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001564 port_array = xhci->usb3_ports;
1565 else
1566 port_array = xhci->usb2_ports;
1567 /* Find the faked port hub number */
1568 faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
1569 port_id);
1570
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001571 temp = readl(port_array[faked_port_index]);
Sarah Sharp7111ebc2010-12-14 13:24:55 -08001572 if (hcd->state == HC_STATE_SUSPENDED) {
Andiry Xu56192532010-10-14 07:23:00 -07001573 xhci_dbg(xhci, "resume root hub\n");
1574 usb_hcd_resume_root_hub(hcd);
1575 }
1576
Mathias Nymanb50107b2015-10-01 18:40:38 +03001577 if (hcd->speed >= HCD_USB3 && (temp & PORT_PLS_MASK) == XDEV_INACTIVE)
Zhuang Jin Canfac42712015-07-21 17:20:30 +03001578 bus_state->port_remote_wakeup &= ~(1 << faked_port_index);
1579
Andiry Xu56192532010-10-14 07:23:00 -07001580 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
1581 xhci_dbg(xhci, "port resume event for port %d\n", port_id);
1582
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001583 temp1 = readl(&xhci->op_regs->command);
Andiry Xu56192532010-10-14 07:23:00 -07001584 if (!(temp1 & CMD_RUN)) {
1585 xhci_warn(xhci, "xHC is not running.\n");
1586 goto cleanup;
1587 }
1588
Mathias Nyman2338b9e2015-10-01 18:40:36 +03001589 if (DEV_SUPERSPEED_ANY(temp)) {
Sarah Sharpd93814c2012-01-24 16:39:02 -08001590 xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001591 /* Set a flag to say the port signaled remote wakeup,
1592 * so we can tell the difference between the end of
1593 * device and host initiated resume.
1594 */
1595 bus_state->port_remote_wakeup |= 1 << faked_port_index;
Sarah Sharpd93814c2012-01-24 16:39:02 -08001596 xhci_test_and_clear_bit(xhci, port_array,
1597 faked_port_index, PORT_PLC);
Andiry Xuc9682df2011-09-23 14:19:48 -07001598 xhci_set_link_state(xhci, port_array, faked_port_index,
1599 XDEV_U0);
Sarah Sharpd93814c2012-01-24 16:39:02 -08001600 /* Need to wait until the next link state change
1601 * indicates the device is actually in U0.
1602 */
1603 bogus_port_status = true;
1604 goto cleanup;
Mathias Nymanf69115f2015-12-11 14:38:06 +02001605 } else if (!test_bit(faked_port_index,
1606 &bus_state->resuming_ports)) {
Andiry Xu56192532010-10-14 07:23:00 -07001607 xhci_dbg(xhci, "resume HS port %d\n", port_id);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001608 bus_state->resume_done[faked_port_index] = jiffies +
Felipe Balbib9e45182015-02-13 14:39:13 -06001609 msecs_to_jiffies(USB_RESUME_TIMEOUT);
Andiry Xuf370b992012-04-14 02:54:30 +08001610 set_bit(faked_port_index, &bus_state->resuming_ports);
Andiry Xu56192532010-10-14 07:23:00 -07001611 mod_timer(&hcd->rh_timer,
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001612 bus_state->resume_done[faked_port_index]);
Andiry Xu56192532010-10-14 07:23:00 -07001613 /* Do the rest in GetPortStatus */
1614 }
1615 }
1616
Sarah Sharpd93814c2012-01-24 16:39:02 -08001617 if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_U0 &&
Mathias Nyman2338b9e2015-10-01 18:40:36 +03001618 DEV_SUPERSPEED_ANY(temp)) {
Sarah Sharpd93814c2012-01-24 16:39:02 -08001619 xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001620 /* We've just brought the device into U0 through either the
1621 * Resume state after a device remote wakeup, or through the
1622 * U3Exit state after a host-initiated resume. If it's a device
1623 * initiated remote wake, don't pass up the link state change,
1624 * so the roothub behavior is consistent with external
1625 * USB 3.0 hub behavior.
1626 */
Sarah Sharpd93814c2012-01-24 16:39:02 -08001627 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1628 faked_port_index + 1);
1629 if (slot_id && xhci->devs[slot_id])
1630 xhci_ring_device(xhci, slot_id);
Nickolai Zeldovichba7b5c22013-01-07 22:39:31 -05001631 if (bus_state->port_remote_wakeup & (1 << faked_port_index)) {
Sarah Sharp4ee823b2011-11-14 18:00:01 -08001632 bus_state->port_remote_wakeup &=
1633 ~(1 << faked_port_index);
1634 xhci_test_and_clear_bit(xhci, port_array,
1635 faked_port_index, PORT_PLC);
1636 usb_wakeup_notification(hcd->self.root_hub,
1637 faked_port_index + 1);
1638 bogus_port_status = true;
1639 goto cleanup;
1640 }
Sarah Sharpd93814c2012-01-24 16:39:02 -08001641 }
1642
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001643 /*
1644 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or
1645 * RExit to a disconnect state). If so, let the the driver know it's
1646 * out of the RExit state.
1647 */
Mathias Nyman2338b9e2015-10-01 18:40:36 +03001648 if (!DEV_SUPERSPEED_ANY(temp) &&
Sarah Sharp8b3d4572013-08-20 08:12:12 -07001649 test_and_clear_bit(faked_port_index,
1650 &bus_state->rexit_ports)) {
1651 complete(&bus_state->rexit_done[faked_port_index]);
1652 bogus_port_status = true;
1653 goto cleanup;
1654 }
1655
Mathias Nymanb50107b2015-10-01 18:40:38 +03001656 if (hcd->speed < HCD_USB3)
Andiry Xu6fd45622011-09-23 14:19:50 -07001657 xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
1658 PORT_PLC);
1659
Andiry Xu56192532010-10-14 07:23:00 -07001660cleanup:
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001661 /* Update event ring dequeue pointer before dropping the lock */
Andiry Xu3b72fca2012-03-05 17:49:32 +08001662 inc_deq(xhci, xhci->event_ring);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001663
Sarah Sharp386139d2011-03-24 08:02:58 -07001664 /* Don't make the USB core poll the roothub if we got a bad port status
1665 * change event. Besides, at that point we can't tell which roothub
1666 * (USB 2.0 or USB 3.0) to kick.
1667 */
1668 if (bogus_port_status)
1669 return;
1670
Sarah Sharpc52804a2012-11-27 12:30:23 -08001671 /*
1672 * xHCI port-status-change events occur when the "or" of all the
1673 * status-change bits in the portsc register changes from 0 to 1.
1674 * New status changes won't cause an event if any other change
1675 * bits are still set. When an event occurs, switch over to
1676 * polling to avoid losing status changes.
1677 */
1678 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1679 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001680 spin_unlock(&xhci->lock);
1681 /* Pass this up to the core */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001682 usb_hcd_poll_rh_status(hcd);
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001683 spin_lock(&xhci->lock);
1684}
1685
1686/*
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001687 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
1688 * at end_trb, which may be in another segment. If the suspect DMA address is a
1689 * TRB in this TD, this function returns that TRB's segment. Otherwise it
1690 * returns 0.
1691 */
Hans de Goedecffb9be2014-08-20 16:41:51 +03001692struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
1693 struct xhci_segment *start_seg,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001694 union xhci_trb *start_trb,
1695 union xhci_trb *end_trb,
Hans de Goedecffb9be2014-08-20 16:41:51 +03001696 dma_addr_t suspect_dma,
1697 bool debug)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001698{
1699 dma_addr_t start_dma;
1700 dma_addr_t end_seg_dma;
1701 dma_addr_t end_trb_dma;
1702 struct xhci_segment *cur_seg;
1703
Sarah Sharp23e3be12009-04-29 19:05:20 -07001704 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001705 cur_seg = start_seg;
1706
1707 do {
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001708 if (start_dma == 0)
Randy Dunlap326b4812010-04-19 08:53:50 -07001709 return NULL;
Sarah Sharpae636742009-04-29 19:02:31 -07001710 /* We may get an event for a Link TRB in the middle of a TD */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001711 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001712 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001713 /* If the end TRB isn't in this segment, this is set to 0 */
Sarah Sharp23e3be12009-04-29 19:05:20 -07001714 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001715
Hans de Goedecffb9be2014-08-20 16:41:51 +03001716 if (debug)
1717 xhci_warn(xhci,
1718 "Looking for event-dma %016llx trb-start %016llx trb-end %016llx seg-start %016llx seg-end %016llx\n",
1719 (unsigned long long)suspect_dma,
1720 (unsigned long long)start_dma,
1721 (unsigned long long)end_trb_dma,
1722 (unsigned long long)cur_seg->dma,
1723 (unsigned long long)end_seg_dma);
1724
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001725 if (end_trb_dma > 0) {
1726 /* The end TRB is in this segment, so suspect should be here */
1727 if (start_dma <= end_trb_dma) {
1728 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
1729 return cur_seg;
1730 } else {
1731 /* Case for one segment with
1732 * a TD wrapped around to the top
1733 */
1734 if ((suspect_dma >= start_dma &&
1735 suspect_dma <= end_seg_dma) ||
1736 (suspect_dma >= cur_seg->dma &&
1737 suspect_dma <= end_trb_dma))
1738 return cur_seg;
1739 }
Randy Dunlap326b4812010-04-19 08:53:50 -07001740 return NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001741 } else {
1742 /* Might still be somewhere in this segment */
1743 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
1744 return cur_seg;
1745 }
1746 cur_seg = cur_seg->next;
Sarah Sharp23e3be12009-04-29 19:05:20 -07001747 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
Sarah Sharp2fa88da2009-11-03 22:02:24 -08001748 } while (cur_seg != start_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001749
Randy Dunlap326b4812010-04-19 08:53:50 -07001750 return NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001751}
1752
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001753static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1754 unsigned int slot_id, unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001755 unsigned int stream_id,
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001756 struct xhci_td *td, union xhci_trb *event_trb)
1757{
1758 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001759 struct xhci_command *command;
1760 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
1761 if (!command)
1762 return;
1763
Mathias Nymand0167ad2015-03-10 19:49:00 +02001764 ep->ep_state |= EP_HALTED;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001765 ep->stopped_stream = stream_id;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001766
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001767 xhci_queue_reset_ep(xhci, command, slot_id, ep_index);
Mathias Nymand97b4f82014-11-27 18:19:16 +02001768 xhci_cleanup_stalled_ring(xhci, ep_index, td);
Sarah Sharp1624ae12010-05-06 13:40:08 -07001769
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001770 ep->stopped_stream = 0;
Sarah Sharp1624ae12010-05-06 13:40:08 -07001771
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001772 xhci_ring_cmd_db(xhci);
1773}
1774
1775/* Check if an error has halted the endpoint ring. The class driver will
1776 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1777 * However, a babble and other errors also halt the endpoint ring, and the class
1778 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1779 * Ring Dequeue Pointer command manually.
1780 */
1781static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1782 struct xhci_ep_ctx *ep_ctx,
1783 unsigned int trb_comp_code)
1784{
1785 /* TRB completion codes that may require a manual halt cleanup */
1786 if (trb_comp_code == COMP_TX_ERR ||
1787 trb_comp_code == COMP_BABBLE ||
1788 trb_comp_code == COMP_SPLIT_ERR)
Rajesh Bhagatd4fc8bf2016-03-11 10:27:49 +05301789 /* The 0.95 spec says a babbling control endpoint
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001790 * is not halted. The 0.96 spec says it is. Some HW
1791 * claims to be 0.95 compliant, but it halts the control
1792 * endpoint anyway. Check if a babble halted the
1793 * endpoint.
1794 */
Matt Evansf5960b62011-06-01 10:22:55 +10001795 if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1796 cpu_to_le32(EP_STATE_HALTED))
Sarah Sharpbcef3fd2009-11-11 10:28:44 -08001797 return 1;
1798
1799 return 0;
1800}
1801
Sarah Sharpb45b5062009-12-09 15:59:06 -08001802int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
1803{
1804 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1805 /* Vendor defined "informational" completion code,
1806 * treat as not-an-error.
1807 */
1808 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1809 trb_comp_code);
1810 xhci_dbg(xhci, "Treating code as success.\n");
1811 return 1;
1812 }
1813 return 0;
1814}
1815
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001816/*
Andiry Xu4422da62010-07-22 15:22:55 -07001817 * Finish the td processing, remove the td from td list;
1818 * Return 1 if the urb can be given back.
1819 */
1820static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
1821 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1822 struct xhci_virt_ep *ep, int *status, bool skip)
1823{
1824 struct xhci_virt_device *xdev;
1825 struct xhci_ring *ep_ring;
1826 unsigned int slot_id;
1827 int ep_index;
1828 struct urb *urb = NULL;
1829 struct xhci_ep_ctx *ep_ctx;
1830 int ret = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001831 struct urb_priv *urb_priv;
Andiry Xu4422da62010-07-22 15:22:55 -07001832 u32 trb_comp_code;
1833
Matt Evans28ccd292011-03-29 13:40:46 +11001834 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu4422da62010-07-22 15:22:55 -07001835 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001836 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1837 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu4422da62010-07-22 15:22:55 -07001838 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001839 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu4422da62010-07-22 15:22:55 -07001840
1841 if (skip)
1842 goto td_cleanup;
1843
Lu Baolu40a3b772015-08-06 19:24:01 +03001844 if (trb_comp_code == COMP_STOP_INVAL ||
1845 trb_comp_code == COMP_STOP ||
1846 trb_comp_code == COMP_STOP_SHORT) {
Andiry Xu4422da62010-07-22 15:22:55 -07001847 /* The Endpoint Stop Command completion will take care of any
1848 * stopped TDs. A stopped TD may be restarted, so don't update
1849 * the ring dequeue pointer or take this TD off any lists yet.
1850 */
1851 ep->stopped_td = td;
Andiry Xu4422da62010-07-22 15:22:55 -07001852 return 0;
Mathias Nyman69defe02014-11-27 18:19:14 +02001853 }
1854 if (trb_comp_code == COMP_STALL ||
1855 xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
1856 trb_comp_code)) {
1857 /* Issue a reset endpoint command to clear the host side
1858 * halt, followed by a set dequeue command to move the
1859 * dequeue pointer past the TD.
1860 * The class driver clears the device side halt later.
1861 */
1862 xhci_cleanup_halted_endpoint(xhci, slot_id, ep_index,
1863 ep_ring->stream_id, td, event_trb);
Andiry Xu4422da62010-07-22 15:22:55 -07001864 } else {
Mathias Nyman69defe02014-11-27 18:19:14 +02001865 /* Update ring dequeue pointer */
1866 while (ep_ring->dequeue != td->last_trb)
Andiry Xu3b72fca2012-03-05 17:49:32 +08001867 inc_deq(xhci, ep_ring);
Mathias Nyman69defe02014-11-27 18:19:14 +02001868 inc_deq(xhci, ep_ring);
1869 }
Andiry Xu4422da62010-07-22 15:22:55 -07001870
1871td_cleanup:
Mathias Nyman69defe02014-11-27 18:19:14 +02001872 /* Clean up the endpoint's TD list */
1873 urb = td->urb;
1874 urb_priv = urb->hcpriv;
Andiry Xu4422da62010-07-22 15:22:55 -07001875
Mathias Nymanf9c589e2016-06-21 10:58:02 +03001876 /* if a bounce buffer was used to align this td then unmap it */
1877 if (td->bounce_seg)
1878 xhci_unmap_td_bounce_buffer(xhci, ep_ring, td);
1879
Mathias Nyman69defe02014-11-27 18:19:14 +02001880 /* Do one last check of the actual transfer length.
1881 * If the host controller said we transferred more data than the buffer
1882 * length, urb->actual_length will be a very big number (since it's
1883 * unsigned). Play it safe and say we didn't transfer anything.
1884 */
1885 if (urb->actual_length > urb->transfer_buffer_length) {
1886 xhci_warn(xhci, "URB transfer length is wrong, xHC issue? req. len = %u, act. len = %u\n",
1887 urb->transfer_buffer_length,
1888 urb->actual_length);
1889 urb->actual_length = 0;
1890 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1891 *status = -EREMOTEIO;
1892 else
1893 *status = 0;
1894 }
1895 list_del_init(&td->td_list);
1896 /* Was this TD slated to be cancelled but completed anyway? */
1897 if (!list_empty(&td->cancelled_td_list))
1898 list_del_init(&td->cancelled_td_list);
Andiry Xu4422da62010-07-22 15:22:55 -07001899
Mathias Nyman69defe02014-11-27 18:19:14 +02001900 urb_priv->td_cnt++;
1901 /* Giveback the urb when all the tds are completed */
1902 if (urb_priv->td_cnt == urb_priv->length) {
1903 ret = 1;
1904 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1905 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
1906 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
1907 if (xhci->quirks & XHCI_AMD_PLL_FIX)
1908 usb_amd_quirk_pll_enable();
Andiry Xuc41136b2011-03-22 17:08:14 +08001909 }
1910 }
Andiry Xu4422da62010-07-22 15:22:55 -07001911 }
1912
1913 return ret;
1914}
1915
1916/*
Andiry Xu8af56be2010-07-22 15:23:03 -07001917 * Process control tds, update urb status and actual_length.
1918 */
1919static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
1920 union xhci_trb *event_trb, struct xhci_transfer_event *event,
1921 struct xhci_virt_ep *ep, int *status)
1922{
1923 struct xhci_virt_device *xdev;
1924 struct xhci_ring *ep_ring;
1925 unsigned int slot_id;
1926 int ep_index;
1927 struct xhci_ep_ctx *ep_ctx;
1928 u32 trb_comp_code;
1929
Matt Evans28ccd292011-03-29 13:40:46 +11001930 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Andiry Xu8af56be2010-07-22 15:23:03 -07001931 xdev = xhci->devs[slot_id];
Matt Evans28ccd292011-03-29 13:40:46 +11001932 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1933 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Andiry Xu8af56be2010-07-22 15:23:03 -07001934 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001935 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu8af56be2010-07-22 15:23:03 -07001936
Andiry Xu8af56be2010-07-22 15:23:03 -07001937 switch (trb_comp_code) {
1938 case COMP_SUCCESS:
1939 if (event_trb == ep_ring->dequeue) {
1940 xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
1941 "without IOC set??\n");
1942 *status = -ESHUTDOWN;
1943 } else if (event_trb != td->last_trb) {
1944 xhci_warn(xhci, "WARN: Success on ctrl data TRB "
1945 "without IOC set??\n");
1946 *status = -ESHUTDOWN;
1947 } else {
Andiry Xu8af56be2010-07-22 15:23:03 -07001948 *status = 0;
1949 }
1950 break;
1951 case COMP_SHORT_TX:
Andiry Xu8af56be2010-07-22 15:23:03 -07001952 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
1953 *status = -EREMOTEIO;
1954 else
1955 *status = 0;
1956 break;
Lu Baolu40a3b772015-08-06 19:24:01 +03001957 case COMP_STOP_SHORT:
1958 if (event_trb == ep_ring->dequeue || event_trb == td->last_trb)
1959 xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n");
1960 else
1961 td->urb->actual_length =
1962 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
1963
1964 return finish_td(xhci, td, event_trb, event, ep, status, false);
Sarah Sharp3abeca92011-05-05 19:08:09 -07001965 case COMP_STOP:
Lu Baolu40a3b772015-08-06 19:24:01 +03001966 /* Did we stop at data stage? */
1967 if (event_trb != ep_ring->dequeue && event_trb != td->last_trb)
1968 td->urb->actual_length =
1969 td->urb->transfer_buffer_length -
1970 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
1971 /* fall through */
1972 case COMP_STOP_INVAL:
Sarah Sharp3abeca92011-05-05 19:08:09 -07001973 return finish_td(xhci, td, event_trb, event, ep, status, false);
Andiry Xu8af56be2010-07-22 15:23:03 -07001974 default:
1975 if (!xhci_requires_manual_halt_cleanup(xhci,
1976 ep_ctx, trb_comp_code))
1977 break;
1978 xhci_dbg(xhci, "TRB error code %u, "
1979 "halted endpoint index = %u\n",
1980 trb_comp_code, ep_index);
1981 /* else fall through */
1982 case COMP_STALL:
1983 /* Did we transfer part of the data (middle) phase? */
1984 if (event_trb != ep_ring->dequeue &&
1985 event_trb != td->last_trb)
1986 td->urb->actual_length =
Vivek Gautam1c11a172013-03-21 12:06:48 +05301987 td->urb->transfer_buffer_length -
1988 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
Mathias Nyman22ae47e2015-05-29 17:01:53 +03001989 else if (!td->urb_length_set)
Andiry Xu8af56be2010-07-22 15:23:03 -07001990 td->urb->actual_length = 0;
1991
Mathias Nyman8e71a322014-11-18 11:27:12 +02001992 return finish_td(xhci, td, event_trb, event, ep, status, false);
Andiry Xu8af56be2010-07-22 15:23:03 -07001993 }
1994 /*
1995 * Did we transfer any data, despite the errors that might have
1996 * happened? I.e. did we get past the setup stage?
1997 */
1998 if (event_trb != ep_ring->dequeue) {
1999 /* The event was for the status stage */
2000 if (event_trb == td->last_trb) {
Aleksander Morgado45ba2152015-03-06 17:14:21 +02002001 if (td->urb_length_set) {
Andiry Xu8af56be2010-07-22 15:23:03 -07002002 /* Don't overwrite a previously set error code
2003 */
2004 if ((*status == -EINPROGRESS || *status == 0) &&
2005 (td->urb->transfer_flags
2006 & URB_SHORT_NOT_OK))
2007 /* Did we already see a short data
2008 * stage? */
2009 *status = -EREMOTEIO;
2010 } else {
2011 td->urb->actual_length =
2012 td->urb->transfer_buffer_length;
2013 }
2014 } else {
Aleksander Morgado45ba2152015-03-06 17:14:21 +02002015 /*
2016 * Maybe the event was for the data stage? If so, update
2017 * already the actual_length of the URB and flag it as
2018 * set, so that it is not overwritten in the event for
2019 * the last TRB.
2020 */
2021 td->urb_length_set = true;
Sarah Sharp3abeca92011-05-05 19:08:09 -07002022 td->urb->actual_length =
2023 td->urb->transfer_buffer_length -
Vivek Gautam1c11a172013-03-21 12:06:48 +05302024 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
Sarah Sharp3abeca92011-05-05 19:08:09 -07002025 xhci_dbg(xhci, "Waiting for status "
2026 "stage event\n");
2027 return 0;
Andiry Xu8af56be2010-07-22 15:23:03 -07002028 }
2029 }
2030
2031 return finish_td(xhci, td, event_trb, event, ep, status, false);
2032}
2033
2034/*
Andiry Xu04e51902010-07-22 15:23:39 -07002035 * Process isochronous tds, update urb packet status and actual_length.
2036 */
2037static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
2038 union xhci_trb *event_trb, struct xhci_transfer_event *event,
2039 struct xhci_virt_ep *ep, int *status)
2040{
2041 struct xhci_ring *ep_ring;
2042 struct urb_priv *urb_priv;
2043 int idx;
2044 int len = 0;
Andiry Xu04e51902010-07-22 15:23:39 -07002045 union xhci_trb *cur_trb;
2046 struct xhci_segment *cur_seg;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002047 struct usb_iso_packet_descriptor *frame;
Andiry Xu04e51902010-07-22 15:23:39 -07002048 u32 trb_comp_code;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002049 bool skip_td = false;
Andiry Xu04e51902010-07-22 15:23:39 -07002050
Matt Evans28ccd292011-03-29 13:40:46 +11002051 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
2052 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07002053 urb_priv = td->urb->hcpriv;
2054 idx = urb_priv->td_cnt;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002055 frame = &td->urb->iso_frame_desc[idx];
Andiry Xu04e51902010-07-22 15:23:39 -07002056
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002057 /* handle completion code */
2058 switch (trb_comp_code) {
2059 case COMP_SUCCESS:
Vivek Gautam1c11a172013-03-21 12:06:48 +05302060 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0) {
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002061 frame->status = 0;
2062 break;
2063 }
2064 if ((xhci->quirks & XHCI_TRUST_TX_LENGTH))
2065 trb_comp_code = COMP_SHORT_TX;
Lu Baolu40a3b772015-08-06 19:24:01 +03002066 /* fallthrough */
2067 case COMP_STOP_SHORT:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002068 case COMP_SHORT_TX:
2069 frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
2070 -EREMOTEIO : 0;
2071 break;
2072 case COMP_BW_OVER:
2073 frame->status = -ECOMM;
2074 skip_td = true;
2075 break;
2076 case COMP_BUFF_OVER:
2077 case COMP_BABBLE:
2078 frame->status = -EOVERFLOW;
2079 skip_td = true;
2080 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08002081 case COMP_DEV_ERR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002082 case COMP_STALL:
Mathias Nymand104d012015-04-30 17:16:02 +03002083 frame->status = -EPROTO;
2084 skip_td = true;
2085 break;
Hans de Goede9c745992012-04-23 15:06:09 +02002086 case COMP_TX_ERR:
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002087 frame->status = -EPROTO;
Mathias Nymand104d012015-04-30 17:16:02 +03002088 if (event_trb != td->last_trb)
2089 return 0;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002090 skip_td = true;
2091 break;
2092 case COMP_STOP:
2093 case COMP_STOP_INVAL:
2094 break;
2095 default:
2096 frame->status = -1;
2097 break;
Andiry Xu04e51902010-07-22 15:23:39 -07002098 }
2099
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002100 if (trb_comp_code == COMP_SUCCESS || skip_td) {
2101 frame->actual_length = frame->length;
2102 td->urb->actual_length += frame->length;
Lu Baolu40a3b772015-08-06 19:24:01 +03002103 } else if (trb_comp_code == COMP_STOP_SHORT) {
2104 frame->actual_length =
2105 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2106 td->urb->actual_length += frame->actual_length;
Andiry Xu04e51902010-07-22 15:23:39 -07002107 } else {
2108 for (cur_trb = ep_ring->dequeue,
2109 cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
2110 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +10002111 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
2112 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
Matt Evans28ccd292011-03-29 13:40:46 +11002113 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu04e51902010-07-22 15:23:39 -07002114 }
Matt Evans28ccd292011-03-29 13:40:46 +11002115 len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
Vivek Gautam1c11a172013-03-21 12:06:48 +05302116 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu04e51902010-07-22 15:23:39 -07002117
2118 if (trb_comp_code != COMP_STOP_INVAL) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002119 frame->actual_length = len;
Andiry Xu04e51902010-07-22 15:23:39 -07002120 td->urb->actual_length += len;
2121 }
2122 }
2123
Andiry Xu04e51902010-07-22 15:23:39 -07002124 return finish_td(xhci, td, event_trb, event, ep, status, false);
2125}
2126
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002127static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
2128 struct xhci_transfer_event *event,
2129 struct xhci_virt_ep *ep, int *status)
2130{
2131 struct xhci_ring *ep_ring;
2132 struct urb_priv *urb_priv;
2133 struct usb_iso_packet_descriptor *frame;
2134 int idx;
2135
Matt Evansf6975312011-06-01 13:01:01 +10002136 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002137 urb_priv = td->urb->hcpriv;
2138 idx = urb_priv->td_cnt;
2139 frame = &td->urb->iso_frame_desc[idx];
2140
Sarah Sharpb3df3f92011-06-15 19:57:46 -07002141 /* The transfer is partly done. */
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002142 frame->status = -EXDEV;
2143
2144 /* calc actual length */
2145 frame->actual_length = 0;
2146
2147 /* Update ring dequeue pointer */
2148 while (ep_ring->dequeue != td->last_trb)
Andiry Xu3b72fca2012-03-05 17:49:32 +08002149 inc_deq(xhci, ep_ring);
2150 inc_deq(xhci, ep_ring);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002151
2152 return finish_td(xhci, td, NULL, event, ep, status, true);
2153}
2154
Andiry Xu04e51902010-07-22 15:23:39 -07002155/*
Andiry Xu22405ed2010-07-22 15:23:08 -07002156 * Process bulk and interrupt tds, update urb status and actual_length.
2157 */
2158static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
2159 union xhci_trb *event_trb, struct xhci_transfer_event *event,
2160 struct xhci_virt_ep *ep, int *status)
2161{
2162 struct xhci_ring *ep_ring;
2163 union xhci_trb *cur_trb;
2164 struct xhci_segment *cur_seg;
2165 u32 trb_comp_code;
2166
Matt Evans28ccd292011-03-29 13:40:46 +11002167 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
2168 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07002169
2170 switch (trb_comp_code) {
2171 case COMP_SUCCESS:
2172 /* Double check that the HW transferred everything. */
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002173 if (event_trb != td->last_trb ||
Vivek Gautam1c11a172013-03-21 12:06:48 +05302174 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
Andiry Xu22405ed2010-07-22 15:23:08 -07002175 xhci_warn(xhci, "WARN Successful completion "
2176 "on short TX\n");
2177 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
2178 *status = -EREMOTEIO;
2179 else
2180 *status = 0;
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002181 if ((xhci->quirks & XHCI_TRUST_TX_LENGTH))
2182 trb_comp_code = COMP_SHORT_TX;
Andiry Xu22405ed2010-07-22 15:23:08 -07002183 } else {
Andiry Xu22405ed2010-07-22 15:23:08 -07002184 *status = 0;
2185 }
2186 break;
Lu Baolu40a3b772015-08-06 19:24:01 +03002187 case COMP_STOP_SHORT:
Andiry Xu22405ed2010-07-22 15:23:08 -07002188 case COMP_SHORT_TX:
2189 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
2190 *status = -EREMOTEIO;
2191 else
2192 *status = 0;
2193 break;
2194 default:
2195 /* Others already handled above */
2196 break;
2197 }
Sarah Sharpf444ff22011-04-05 15:53:47 -07002198 if (trb_comp_code == COMP_SHORT_TX)
2199 xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
2200 "%d bytes untransferred\n",
2201 td->urb->ep->desc.bEndpointAddress,
2202 td->urb->transfer_buffer_length,
Vivek Gautam1c11a172013-03-21 12:06:48 +05302203 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
Lu Baolu40a3b772015-08-06 19:24:01 +03002204 /* Stopped - short packet completion */
2205 if (trb_comp_code == COMP_STOP_SHORT) {
2206 td->urb->actual_length =
2207 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
2208
2209 if (td->urb->transfer_buffer_length <
2210 td->urb->actual_length) {
2211 xhci_warn(xhci, "HC gave bad length of %d bytes txed\n",
2212 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
2213 td->urb->actual_length = 0;
2214 /* status will be set by usb core for canceled urbs */
2215 }
Andiry Xu22405ed2010-07-22 15:23:08 -07002216 /* Fast path - was this the last TRB in the TD for this URB? */
Lu Baolu40a3b772015-08-06 19:24:01 +03002217 } else if (event_trb == td->last_trb) {
Vivek Gautam1c11a172013-03-21 12:06:48 +05302218 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
Andiry Xu22405ed2010-07-22 15:23:08 -07002219 td->urb->actual_length =
2220 td->urb->transfer_buffer_length -
Vivek Gautam1c11a172013-03-21 12:06:48 +05302221 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07002222 if (td->urb->transfer_buffer_length <
2223 td->urb->actual_length) {
2224 xhci_warn(xhci, "HC gave bad length "
2225 "of %d bytes left\n",
Vivek Gautam1c11a172013-03-21 12:06:48 +05302226 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
Andiry Xu22405ed2010-07-22 15:23:08 -07002227 td->urb->actual_length = 0;
2228 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
2229 *status = -EREMOTEIO;
2230 else
2231 *status = 0;
2232 }
2233 /* Don't overwrite a previously set error code */
2234 if (*status == -EINPROGRESS) {
2235 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
2236 *status = -EREMOTEIO;
2237 else
2238 *status = 0;
2239 }
2240 } else {
2241 td->urb->actual_length =
2242 td->urb->transfer_buffer_length;
2243 /* Ignore a short packet completion if the
2244 * untransferred length was zero.
2245 */
2246 if (*status == -EREMOTEIO)
2247 *status = 0;
2248 }
2249 } else {
2250 /* Slow path - walk the list, starting from the dequeue
2251 * pointer, to get the actual length transferred.
2252 */
2253 td->urb->actual_length = 0;
2254 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
2255 cur_trb != event_trb;
2256 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
Matt Evansf5960b62011-06-01 10:22:55 +10002257 if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
2258 !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
Andiry Xu22405ed2010-07-22 15:23:08 -07002259 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11002260 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
Andiry Xu22405ed2010-07-22 15:23:08 -07002261 }
2262 /* If the ring didn't stop on a Link or No-op TRB, add
2263 * in the actual bytes transferred from the Normal TRB
2264 */
2265 if (trb_comp_code != COMP_STOP_INVAL)
2266 td->urb->actual_length +=
Matt Evans28ccd292011-03-29 13:40:46 +11002267 TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
Vivek Gautam1c11a172013-03-21 12:06:48 +05302268 EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
Andiry Xu22405ed2010-07-22 15:23:08 -07002269 }
2270
2271 return finish_td(xhci, td, event_trb, event, ep, status, false);
2272}
2273
2274/*
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002275 * If this function returns an error condition, it means it got a Transfer
2276 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
2277 * At this point, the host controller is probably hosed and should be reset.
2278 */
2279static int handle_tx_event(struct xhci_hcd *xhci,
2280 struct xhci_transfer_event *event)
Felipe Balbied384bd2012-08-07 14:10:03 +03002281 __releases(&xhci->lock)
2282 __acquires(&xhci->lock)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002283{
2284 struct xhci_virt_device *xdev;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002285 struct xhci_virt_ep *ep;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002286 struct xhci_ring *ep_ring;
Sarah Sharp82d10092009-08-07 14:04:52 -07002287 unsigned int slot_id;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002288 int ep_index;
Randy Dunlap326b4812010-04-19 08:53:50 -07002289 struct xhci_td *td = NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002290 dma_addr_t event_dma;
2291 struct xhci_segment *event_seg;
2292 union xhci_trb *event_trb;
Randy Dunlap326b4812010-04-19 08:53:50 -07002293 struct urb *urb = NULL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002294 int status = -EINPROGRESS;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002295 struct urb_priv *urb_priv;
John Yound115b042009-07-27 12:05:15 -07002296 struct xhci_ep_ctx *ep_ctx;
Andiry Xuc2d7b492011-09-19 16:05:12 -07002297 struct list_head *tmp;
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002298 u32 trb_comp_code;
Andiry Xu4422da62010-07-22 15:22:55 -07002299 int ret = 0;
Andiry Xuc2d7b492011-09-19 16:05:12 -07002300 int td_num = 0;
Mathias Nyman3b4739b2015-10-12 11:30:12 +03002301 bool handling_skipped_tds = false;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002302
Matt Evans28ccd292011-03-29 13:40:46 +11002303 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
Sarah Sharp82d10092009-08-07 14:04:52 -07002304 xdev = xhci->devs[slot_id];
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002305 if (!xdev) {
2306 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002307 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
Sarah Sharpe910b442012-01-04 16:54:12 -08002308 (unsigned long long) xhci_trb_virt_to_dma(
2309 xhci->event_ring->deq_seg,
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002310 xhci->event_ring->dequeue),
2311 lower_32_bits(le64_to_cpu(event->buffer)),
2312 upper_32_bits(le64_to_cpu(event->buffer)),
2313 le32_to_cpu(event->transfer_len),
2314 le32_to_cpu(event->flags));
2315 xhci_dbg(xhci, "Event ring:\n");
2316 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002317 return -ENODEV;
2318 }
2319
2320 /* Endpoint ID is 1 based, our index is zero based */
Matt Evans28ccd292011-03-29 13:40:46 +11002321 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002322 ep = &xdev->eps[ep_index];
Matt Evans28ccd292011-03-29 13:40:46 +11002323 ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
John Yound115b042009-07-27 12:05:15 -07002324 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002325 if (!ep_ring ||
Matt Evans28ccd292011-03-29 13:40:46 +11002326 (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
2327 EP_STATE_DISABLED) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002328 xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
2329 "or incorrect stream ring\n");
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002330 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
Sarah Sharpe910b442012-01-04 16:54:12 -08002331 (unsigned long long) xhci_trb_virt_to_dma(
2332 xhci->event_ring->deq_seg,
Sarah Sharp9258c0b2011-12-01 14:50:30 -08002333 xhci->event_ring->dequeue),
2334 lower_32_bits(le64_to_cpu(event->buffer)),
2335 upper_32_bits(le64_to_cpu(event->buffer)),
2336 le32_to_cpu(event->transfer_len),
2337 le32_to_cpu(event->flags));
2338 xhci_dbg(xhci, "Event ring:\n");
2339 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002340 return -ENODEV;
2341 }
2342
Andiry Xuc2d7b492011-09-19 16:05:12 -07002343 /* Count current td numbers if ep->skip is set */
2344 if (ep->skip) {
2345 list_for_each(tmp, &ep_ring->td_list)
2346 td_num++;
2347 }
2348
Matt Evans28ccd292011-03-29 13:40:46 +11002349 event_dma = le64_to_cpu(event->buffer);
2350 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
Andiry Xu986a92d2010-07-22 15:23:20 -07002351 /* Look for common error cases */
Sarah Sharp66d1eeb2009-08-27 14:35:53 -07002352 switch (trb_comp_code) {
Sarah Sharpb10de142009-04-27 19:58:50 -07002353 /* Skip codes that require special handling depending on
2354 * transfer type
2355 */
2356 case COMP_SUCCESS:
Vivek Gautam1c11a172013-03-21 12:06:48 +05302357 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0)
Sarah Sharp1530bbc62012-05-08 09:22:49 -07002358 break;
2359 if (xhci->quirks & XHCI_TRUST_TX_LENGTH)
2360 trb_comp_code = COMP_SHORT_TX;
2361 else
Sarah Sharp8202ce22012-07-25 10:52:45 -07002362 xhci_warn_ratelimited(xhci,
2363 "WARN Successful completion on short TX: needs XHCI_TRUST_TX_LENGTH quirk?\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002364 case COMP_SHORT_TX:
2365 break;
Sarah Sharpae636742009-04-29 19:02:31 -07002366 case COMP_STOP:
2367 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
2368 break;
2369 case COMP_STOP_INVAL:
2370 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
2371 break;
Lu Baolu40a3b772015-08-06 19:24:01 +03002372 case COMP_STOP_SHORT:
2373 xhci_dbg(xhci, "Stopped with short packet transfer detected\n");
2374 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002375 case COMP_STALL:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002376 xhci_dbg(xhci, "Stalled endpoint\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002377 ep->ep_state |= EP_HALTED;
Sarah Sharpb10de142009-04-27 19:58:50 -07002378 status = -EPIPE;
2379 break;
2380 case COMP_TRB_ERR:
2381 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
2382 status = -EILSEQ;
2383 break;
Sarah Sharpec74e402009-11-11 10:28:36 -08002384 case COMP_SPLIT_ERR:
Sarah Sharpb10de142009-04-27 19:58:50 -07002385 case COMP_TX_ERR:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002386 xhci_dbg(xhci, "Transfer error on endpoint\n");
Sarah Sharpb10de142009-04-27 19:58:50 -07002387 status = -EPROTO;
2388 break;
Sarah Sharp4a731432009-07-27 12:04:32 -07002389 case COMP_BABBLE:
Sarah Sharp2a9227a2011-10-25 13:55:30 +02002390 xhci_dbg(xhci, "Babble error on endpoint\n");
Sarah Sharp4a731432009-07-27 12:04:32 -07002391 status = -EOVERFLOW;
2392 break;
Sarah Sharpb10de142009-04-27 19:58:50 -07002393 case COMP_DB_ERR:
2394 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
2395 status = -ENOSR;
2396 break;
Andiry Xu986a92d2010-07-22 15:23:20 -07002397 case COMP_BW_OVER:
2398 xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
2399 break;
2400 case COMP_BUFF_OVER:
2401 xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
2402 break;
2403 case COMP_UNDERRUN:
2404 /*
2405 * When the Isoch ring is empty, the xHC will generate
2406 * a Ring Overrun Event for IN Isoch endpoint or Ring
2407 * Underrun Event for OUT Isoch endpoint.
2408 */
2409 xhci_dbg(xhci, "underrun event on endpoint\n");
2410 if (!list_empty(&ep_ring->td_list))
2411 xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
2412 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002413 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2414 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002415 goto cleanup;
2416 case COMP_OVERRUN:
2417 xhci_dbg(xhci, "overrun event on endpoint\n");
2418 if (!list_empty(&ep_ring->td_list))
2419 xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
2420 "still with TDs queued?\n",
Matt Evans28ccd292011-03-29 13:40:46 +11002421 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2422 ep_index);
Andiry Xu986a92d2010-07-22 15:23:20 -07002423 goto cleanup;
Alex Hef6ba6fe2011-06-08 18:34:06 +08002424 case COMP_DEV_ERR:
2425 xhci_warn(xhci, "WARN: detect an incompatible device");
2426 status = -EPROTO;
2427 break;
Andiry Xud18240d2010-07-22 15:23:25 -07002428 case COMP_MISSED_INT:
2429 /*
2430 * When encounter missed service error, one or more isoc tds
2431 * may be missed by xHC.
2432 * Set skip flag of the ep_ring; Complete the missed tds as
2433 * short transfer when process the ep_ring next time.
2434 */
2435 ep->skip = true;
2436 xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
2437 goto cleanup;
Mathias Nyman3b4739b2015-10-12 11:30:12 +03002438 case COMP_PING_ERR:
2439 ep->skip = true;
2440 xhci_dbg(xhci, "No Ping response error, Skip one Isoc TD\n");
2441 goto cleanup;
Sarah Sharpb10de142009-04-27 19:58:50 -07002442 default:
Sarah Sharpb45b5062009-12-09 15:59:06 -08002443 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
Sarah Sharp5ad6a522009-11-11 10:28:40 -08002444 status = 0;
2445 break;
2446 }
Mathias Nyman86cd7402015-01-09 16:06:32 +02002447 xhci_warn(xhci, "ERROR Unknown event condition %u, HC probably busted\n",
2448 trb_comp_code);
Sarah Sharpb10de142009-04-27 19:58:50 -07002449 goto cleanup;
2450 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002451
Andiry Xud18240d2010-07-22 15:23:25 -07002452 do {
2453 /* This TRB should be in the TD at the head of this ring's
2454 * TD list.
2455 */
2456 if (list_empty(&ep_ring->td_list)) {
Sarah Sharpa83d6752013-03-18 10:19:51 -07002457 /*
2458 * A stopped endpoint may generate an extra completion
2459 * event if the device was suspended. Don't print
2460 * warnings.
2461 */
2462 if (!(trb_comp_code == COMP_STOP ||
2463 trb_comp_code == COMP_STOP_INVAL)) {
2464 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
2465 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
2466 ep_index);
2467 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
2468 (le32_to_cpu(event->flags) &
2469 TRB_TYPE_BITMASK)>>10);
2470 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
2471 }
Andiry Xud18240d2010-07-22 15:23:25 -07002472 if (ep->skip) {
2473 ep->skip = false;
2474 xhci_dbg(xhci, "td_list is empty while skip "
2475 "flag set. Clear skip flag.\n");
2476 }
2477 ret = 0;
2478 goto cleanup;
2479 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002480
Andiry Xuc2d7b492011-09-19 16:05:12 -07002481 /* We've skipped all the TDs on the ep ring when ep->skip set */
2482 if (ep->skip && td_num == 0) {
2483 ep->skip = false;
2484 xhci_dbg(xhci, "All tds on the ep_ring skipped. "
2485 "Clear skip flag.\n");
2486 ret = 0;
2487 goto cleanup;
2488 }
2489
Andiry Xud18240d2010-07-22 15:23:25 -07002490 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
Andiry Xuc2d7b492011-09-19 16:05:12 -07002491 if (ep->skip)
2492 td_num--;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002493
Andiry Xud18240d2010-07-22 15:23:25 -07002494 /* Is this a TRB in the currently executing TD? */
Hans de Goedecffb9be2014-08-20 16:41:51 +03002495 event_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue,
2496 td->last_trb, event_dma, false);
Alex Hee1cf4862011-06-03 15:58:25 +08002497
2498 /*
2499 * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
2500 * is not in the current TD pointed by ep_ring->dequeue because
2501 * that the hardware dequeue pointer still at the previous TRB
2502 * of the current TD. The previous TRB maybe a Link TD or the
2503 * last TRB of the previous TD. The command completion handle
2504 * will take care the rest.
2505 */
Hans de Goede9a548862014-08-19 15:17:56 +03002506 if (!event_seg && (trb_comp_code == COMP_STOP ||
2507 trb_comp_code == COMP_STOP_INVAL)) {
Alex Hee1cf4862011-06-03 15:58:25 +08002508 ret = 0;
2509 goto cleanup;
2510 }
2511
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002512 if (!event_seg) {
2513 if (!ep->skip ||
2514 !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
Sarah Sharpad808332011-05-25 10:43:56 -07002515 /* Some host controllers give a spurious
2516 * successful event after a short transfer.
2517 * Ignore it.
2518 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002519 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
Sarah Sharpad808332011-05-25 10:43:56 -07002520 ep_ring->last_td_was_short) {
2521 ep_ring->last_td_was_short = false;
2522 ret = 0;
2523 goto cleanup;
2524 }
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002525 /* HC is busted, give up! */
2526 xhci_err(xhci,
2527 "ERROR Transfer event TRB DMA ptr not "
Hans de Goedecffb9be2014-08-20 16:41:51 +03002528 "part of current TD ep_index %d "
2529 "comp_code %u\n", ep_index,
2530 trb_comp_code);
2531 trb_in_td(xhci, ep_ring->deq_seg,
2532 ep_ring->dequeue, td->last_trb,
2533 event_dma, true);
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002534 return -ESHUTDOWN;
2535 }
2536
2537 ret = skip_isoc_td(xhci, td, event, ep, &status);
2538 goto cleanup;
2539 }
Sarah Sharpad808332011-05-25 10:43:56 -07002540 if (trb_comp_code == COMP_SHORT_TX)
2541 ep_ring->last_td_was_short = true;
2542 else
2543 ep_ring->last_td_was_short = false;
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002544
2545 if (ep->skip) {
Andiry Xud18240d2010-07-22 15:23:25 -07002546 xhci_dbg(xhci, "Found td. Clear skip flag.\n");
2547 ep->skip = false;
2548 }
Andiry Xu986a92d2010-07-22 15:23:20 -07002549
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002550 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) /
2551 sizeof(*event_trb)];
2552 /*
2553 * No-op TRB should not trigger interrupts.
2554 * If event_trb is a no-op TRB, it means the
2555 * corresponding TD has been cancelled. Just ignore
2556 * the TD.
2557 */
Matt Evansf5960b62011-06-01 10:22:55 +10002558 if (TRB_TYPE_NOOP_LE32(event_trb->generic.field[3])) {
Dmitry Torokhov926008c2011-03-23 20:47:05 -07002559 xhci_dbg(xhci,
2560 "event_trb is a no-op TRB. Skip it\n");
2561 goto cleanup;
Andiry Xud18240d2010-07-22 15:23:25 -07002562 }
2563
2564 /* Now update the urb's actual_length and give back to
2565 * the core
2566 */
2567 if (usb_endpoint_xfer_control(&td->urb->ep->desc))
2568 ret = process_ctrl_td(xhci, td, event_trb, event, ep,
2569 &status);
Andiry Xu04e51902010-07-22 15:23:39 -07002570 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
2571 ret = process_isoc_td(xhci, td, event_trb, event, ep,
2572 &status);
Andiry Xud18240d2010-07-22 15:23:25 -07002573 else
2574 ret = process_bulk_intr_td(xhci, td, event_trb, event,
2575 ep, &status);
Andiry Xu4422da62010-07-22 15:22:55 -07002576
2577cleanup:
Mathias Nyman3b4739b2015-10-12 11:30:12 +03002578
2579
2580 handling_skipped_tds = ep->skip &&
2581 trb_comp_code != COMP_MISSED_INT &&
2582 trb_comp_code != COMP_PING_ERR;
2583
Andiry Xud18240d2010-07-22 15:23:25 -07002584 /*
Mathias Nyman3b4739b2015-10-12 11:30:12 +03002585 * Do not update event ring dequeue pointer if we're in a loop
2586 * processing missed tds.
Sarah Sharp82d10092009-08-07 14:04:52 -07002587 */
Mathias Nyman3b4739b2015-10-12 11:30:12 +03002588 if (!handling_skipped_tds)
Andiry Xu3b72fca2012-03-05 17:49:32 +08002589 inc_deq(xhci, xhci->event_ring);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002590
Andiry Xud18240d2010-07-22 15:23:25 -07002591 if (ret) {
2592 urb = td->urb;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002593 urb_priv = urb->hcpriv;
Mathias Nyman8e71a322014-11-18 11:27:12 +02002594
Lin Wang4daf9df2015-01-09 16:06:31 +02002595 xhci_urb_free_priv(urb_priv);
Andiry Xud18240d2010-07-22 15:23:25 -07002596
Sarah Sharp214f76f2010-10-26 11:22:02 -07002597 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpf444ff22011-04-05 15:53:47 -07002598 if ((urb->actual_length != urb->transfer_buffer_length &&
2599 (urb->transfer_flags &
2600 URB_SHORT_NOT_OK)) ||
Sarah Sharpfd984d22011-09-02 11:05:56 -07002601 (status != 0 &&
2602 !usb_endpoint_xfer_isoc(&urb->ep->desc)))
Sarah Sharpf444ff22011-04-05 15:53:47 -07002603 xhci_dbg(xhci, "Giveback URB %p, len = %d, "
Alan Stern1949f9e2012-05-07 13:22:52 -04002604 "expected = %d, status = %d\n",
Sarah Sharpf444ff22011-04-05 15:53:47 -07002605 urb, urb->actual_length,
2606 urb->transfer_buffer_length,
2607 status);
Andiry Xud18240d2010-07-22 15:23:25 -07002608 spin_unlock(&xhci->lock);
Sarah Sharpb3df3f92011-06-15 19:57:46 -07002609 /* EHCI, UHCI, and OHCI always unconditionally set the
2610 * urb->status of an isochronous endpoint to 0.
2611 */
2612 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2613 status = 0;
Sarah Sharp214f76f2010-10-26 11:22:02 -07002614 usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
Andiry Xud18240d2010-07-22 15:23:25 -07002615 spin_lock(&xhci->lock);
2616 }
2617
2618 /*
2619 * If ep->skip is set, it means there are missed tds on the
2620 * endpoint ring need to take care of.
2621 * Process them as short transfer until reach the td pointed by
2622 * the event.
2623 */
Mathias Nyman3b4739b2015-10-12 11:30:12 +03002624 } while (handling_skipped_tds);
Andiry Xud18240d2010-07-22 15:23:25 -07002625
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002626 return 0;
2627}
2628
2629/*
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002630 * This function handles all OS-owned events on the event ring. It may drop
2631 * xhci->lock between event processing (e.g. to pass up port status changes).
Matt Evans9dee9a22011-03-29 13:41:02 +11002632 * Returns >0 for "possibly more events to process" (caller should call again),
2633 * otherwise 0 if done. In future, <0 returns should indicate error code.
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002634 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002635static int xhci_handle_event(struct xhci_hcd *xhci)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002636{
2637 union xhci_trb *event;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002638 int update_ptrs = 1;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002639 int ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002640
2641 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
2642 xhci->error_bitmask |= 1 << 1;
Matt Evans9dee9a22011-03-29 13:41:02 +11002643 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002644 }
2645
2646 event = xhci->event_ring->dequeue;
2647 /* Does the HC or OS own the TRB? */
Matt Evans28ccd292011-03-29 13:40:46 +11002648 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
2649 xhci->event_ring->cycle_state) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002650 xhci->error_bitmask |= 1 << 2;
Matt Evans9dee9a22011-03-29 13:41:02 +11002651 return 0;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002652 }
2653
Matt Evans92a3da42011-03-29 13:40:51 +11002654 /*
2655 * Barrier between reading the TRB_CYCLE (valid) flag above and any
2656 * speculative reads of the event's flags/data below.
2657 */
2658 rmb();
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002659 /* FIXME: Handle more event types. */
Matt Evans28ccd292011-03-29 13:40:46 +11002660 switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002661 case TRB_TYPE(TRB_COMPLETION):
2662 handle_cmd_completion(xhci, &event->event_cmd);
2663 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -07002664 case TRB_TYPE(TRB_PORT_STATUS):
2665 handle_port_status(xhci, event);
2666 update_ptrs = 0;
2667 break;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002668 case TRB_TYPE(TRB_TRANSFER):
2669 ret = handle_tx_event(xhci, &event->trans_event);
2670 if (ret < 0)
2671 xhci->error_bitmask |= 1 << 9;
2672 else
2673 update_ptrs = 0;
2674 break;
Sarah Sharp623bef92011-11-11 14:57:33 -08002675 case TRB_TYPE(TRB_DEV_NOTE):
2676 handle_device_notification(xhci, event);
2677 break;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002678 default:
Matt Evans28ccd292011-03-29 13:40:46 +11002679 if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
2680 TRB_TYPE(48))
Sarah Sharp02386342010-05-24 13:25:28 -07002681 handle_vendor_event(xhci, event);
2682 else
2683 xhci->error_bitmask |= 1 << 3;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002684 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002685 /* Any of the above functions may drop and re-acquire the lock, so check
2686 * to make sure a watchdog timer didn't mark the host as non-responsive.
2687 */
2688 if (xhci->xhc_state & XHCI_STATE_DYING) {
2689 xhci_dbg(xhci, "xHCI host dying, returning from "
2690 "event handler.\n");
Matt Evans9dee9a22011-03-29 13:41:02 +11002691 return 0;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002692 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002693
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002694 if (update_ptrs)
2695 /* Update SW event ring dequeue pointer */
Andiry Xu3b72fca2012-03-05 17:49:32 +08002696 inc_deq(xhci, xhci->event_ring);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002697
Matt Evans9dee9a22011-03-29 13:41:02 +11002698 /* Are there more items on the event ring? Caller will call us again to
2699 * check.
2700 */
2701 return 1;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002702}
Sarah Sharp9032cd52010-07-29 22:12:29 -07002703
2704/*
2705 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
2706 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
2707 * indicators of an event TRB error, but we check the status *first* to be safe.
2708 */
2709irqreturn_t xhci_irq(struct usb_hcd *hcd)
2710{
2711 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002712 u32 status;
Sarah Sharpbda53142010-07-29 22:12:38 -07002713 u64 temp_64;
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002714 union xhci_trb *event_ring_deq;
2715 dma_addr_t deq;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002716
2717 spin_lock(&xhci->lock);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002718 /* Check if the xHC generated the interrupt, or the irq is shared */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02002719 status = readl(&xhci->op_regs->status);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002720 if (status == 0xffffffff)
Sarah Sharp9032cd52010-07-29 22:12:29 -07002721 goto hw_died;
2722
Sarah Sharpc21599a2010-07-29 22:13:00 -07002723 if (!(status & STS_EINT)) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002724 spin_unlock(&xhci->lock);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002725 return IRQ_NONE;
2726 }
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002727 if (status & STS_FATAL) {
Sarah Sharp9032cd52010-07-29 22:12:29 -07002728 xhci_warn(xhci, "WARNING: Host System Error\n");
2729 xhci_halt(xhci);
2730hw_died:
Sarah Sharp9032cd52010-07-29 22:12:29 -07002731 spin_unlock(&xhci->lock);
Joe Lawrence948fa132015-04-30 17:16:04 +03002732 return IRQ_HANDLED;
Sarah Sharp9032cd52010-07-29 22:12:29 -07002733 }
2734
Sarah Sharpbda53142010-07-29 22:12:38 -07002735 /*
2736 * Clear the op reg interrupt status first,
2737 * so we can receive interrupts from other MSI-X interrupters.
2738 * Write 1 to clear the interrupt status.
2739 */
Sarah Sharp27e0dd42010-07-29 22:12:43 -07002740 status |= STS_EINT;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02002741 writel(status, &xhci->op_regs->status);
Sarah Sharpbda53142010-07-29 22:12:38 -07002742 /* FIXME when MSI-X is supported and there are multiple vectors */
2743 /* Clear the MSI-X event interrupt status */
2744
Felipe Balbicd704692012-02-29 16:46:23 +02002745 if (hcd->irq) {
Sarah Sharpc21599a2010-07-29 22:13:00 -07002746 u32 irq_pending;
2747 /* Acknowledge the PCI interrupt */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02002748 irq_pending = readl(&xhci->ir_set->irq_pending);
Felipe Balbi4e833c02012-03-15 16:37:08 +02002749 irq_pending |= IMAN_IP;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02002750 writel(irq_pending, &xhci->ir_set->irq_pending);
Sarah Sharpc21599a2010-07-29 22:13:00 -07002751 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002752
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +03002753 if (xhci->xhc_state & XHCI_STATE_DYING ||
2754 xhci->xhc_state & XHCI_STATE_HALTED) {
Sarah Sharpbda53142010-07-29 22:12:38 -07002755 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
2756 "Shouldn't IRQs be disabled?\n");
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002757 /* Clear the event handler busy flag (RW1C);
2758 * the event ring should be empty.
Sarah Sharpbda53142010-07-29 22:12:38 -07002759 */
Sarah Sharpf7b2e402014-01-30 13:27:49 -08002760 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp477632d2014-01-29 14:02:00 -08002761 xhci_write_64(xhci, temp_64 | ERST_EHB,
2762 &xhci->ir_set->erst_dequeue);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002763 spin_unlock(&xhci->lock);
2764
2765 return IRQ_HANDLED;
2766 }
2767
2768 event_ring_deq = xhci->event_ring->dequeue;
2769 /* FIXME this should be a delayed service routine
2770 * that clears the EHB.
2771 */
Matt Evans9dee9a22011-03-29 13:41:02 +11002772 while (xhci_handle_event(xhci) > 0) {}
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002773
Sarah Sharpf7b2e402014-01-30 13:27:49 -08002774 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002775 /* If necessary, update the HW's version of the event ring deq ptr. */
2776 if (event_ring_deq != xhci->event_ring->dequeue) {
2777 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2778 xhci->event_ring->dequeue);
2779 if (deq == 0)
2780 xhci_warn(xhci, "WARN something wrong with SW event "
2781 "ring dequeue ptr.\n");
2782 /* Update HC event ring dequeue pointer */
2783 temp_64 &= ERST_PTR_MASK;
2784 temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
2785 }
Sarah Sharpbda53142010-07-29 22:12:38 -07002786
2787 /* Clear the event handler busy flag (RW1C); event ring is empty. */
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002788 temp_64 |= ERST_EHB;
Sarah Sharp477632d2014-01-29 14:02:00 -08002789 xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
Sarah Sharpc06d68b2010-07-29 22:12:49 -07002790
Sarah Sharp9032cd52010-07-29 22:12:29 -07002791 spin_unlock(&xhci->lock);
2792
2793 return IRQ_HANDLED;
2794}
2795
Alex Shi851ec162013-05-24 10:54:19 +08002796irqreturn_t xhci_msi_irq(int irq, void *hcd)
Sarah Sharp9032cd52010-07-29 22:12:29 -07002797{
Alan Stern968b8222011-11-03 12:03:38 -04002798 return xhci_irq(hcd);
Sarah Sharp9032cd52010-07-29 22:12:29 -07002799}
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002800
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002801/**** Endpoint Ring Operations ****/
2802
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002803/*
2804 * Generic function for queueing a TRB on a ring.
2805 * The caller must have checked to make sure there's room on the ring.
Sarah Sharp6cc30d82010-06-10 12:25:28 -07002806 *
2807 * @more_trbs_coming: Will you enqueue more TRBs before calling
2808 * prepare_transfer()?
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002809 */
2810static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002811 bool more_trbs_coming,
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002812 u32 field1, u32 field2, u32 field3, u32 field4)
2813{
2814 struct xhci_generic_trb *trb;
2815
2816 trb = &ring->enqueue->generic;
Matt Evans28ccd292011-03-29 13:40:46 +11002817 trb->field[0] = cpu_to_le32(field1);
2818 trb->field[1] = cpu_to_le32(field2);
2819 trb->field[2] = cpu_to_le32(field3);
2820 trb->field[3] = cpu_to_le32(field4);
Andiry Xu3b72fca2012-03-05 17:49:32 +08002821 inc_enq(xhci, ring, more_trbs_coming);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07002822}
2823
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002824/*
2825 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
2826 * FIXME allocate segments if the ring is full.
2827 */
2828static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002829 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002830{
Andiry Xu8dfec612012-03-05 17:49:37 +08002831 unsigned int num_trbs_needed;
2832
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002833 /* Make sure the endpoint has been added to xHC schedule */
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002834 switch (ep_state) {
2835 case EP_STATE_DISABLED:
2836 /*
2837 * USB core changed config/interfaces without notifying us,
2838 * or hardware is reporting the wrong state.
2839 */
2840 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
2841 return -ENOENT;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002842 case EP_STATE_ERROR:
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002843 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002844 /* FIXME event handling code for error needs to clear it */
2845 /* XXX not sure if this should be -ENOENT or not */
2846 return -EINVAL;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002847 case EP_STATE_HALTED:
2848 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002849 case EP_STATE_STOPPED:
2850 case EP_STATE_RUNNING:
2851 break;
2852 default:
2853 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
2854 /*
2855 * FIXME issue Configure Endpoint command to try to get the HC
2856 * back into a known state.
2857 */
2858 return -EINVAL;
2859 }
Andiry Xu8dfec612012-03-05 17:49:37 +08002860
2861 while (1) {
Sarah Sharp3d4b81e2014-01-31 11:52:57 -08002862 if (room_on_ring(xhci, ep_ring, num_trbs))
2863 break;
Andiry Xu8dfec612012-03-05 17:49:37 +08002864
2865 if (ep_ring == xhci->cmd_ring) {
2866 xhci_err(xhci, "Do not support expand command ring\n");
2867 return -ENOMEM;
2868 }
2869
Xenia Ragiadakou68ffb012013-08-14 06:33:56 +03002870 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
2871 "ERROR no room on ep ring, try ring expansion");
Andiry Xu8dfec612012-03-05 17:49:37 +08002872 num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
2873 if (xhci_ring_expansion(xhci, ep_ring, num_trbs_needed,
2874 mem_flags)) {
2875 xhci_err(xhci, "Ring expansion failed\n");
2876 return -ENOMEM;
2877 }
Peter Senna Tschudin261fa122012-09-12 19:03:17 +02002878 }
John Youn6c12db92010-05-10 15:33:00 -07002879
Mathias Nymand0c77d82016-06-21 10:58:07 +03002880 while (trb_is_link(ep_ring->enqueue)) {
2881 /* If we're not dealing with 0.95 hardware or isoc rings
2882 * on AMD 0.96 host, clear the chain bit.
2883 */
2884 if (!xhci_link_trb_quirk(xhci) &&
2885 !(ep_ring->type == TYPE_ISOC &&
2886 (xhci->quirks & XHCI_AMD_0x96_HOST)))
2887 ep_ring->enqueue->link.control &=
2888 cpu_to_le32(~TRB_CHAIN);
2889 else
2890 ep_ring->enqueue->link.control |=
2891 cpu_to_le32(TRB_CHAIN);
John Youn6c12db92010-05-10 15:33:00 -07002892
Mathias Nymand0c77d82016-06-21 10:58:07 +03002893 wmb();
2894 ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE);
John Youn6c12db92010-05-10 15:33:00 -07002895
Mathias Nymand0c77d82016-06-21 10:58:07 +03002896 /* Toggle the cycle bit after the last ring segment. */
2897 if (link_trb_toggles_cycle(ep_ring->enqueue))
2898 ep_ring->cycle_state ^= 1;
John Youn6c12db92010-05-10 15:33:00 -07002899
Mathias Nymand0c77d82016-06-21 10:58:07 +03002900 ep_ring->enq_seg = ep_ring->enq_seg->next;
2901 ep_ring->enqueue = ep_ring->enq_seg->trbs;
John Youn6c12db92010-05-10 15:33:00 -07002902 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002903 return 0;
2904}
2905
Sarah Sharp23e3be12009-04-29 19:05:20 -07002906static int prepare_transfer(struct xhci_hcd *xhci,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002907 struct xhci_virt_device *xdev,
2908 unsigned int ep_index,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002909 unsigned int stream_id,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002910 unsigned int num_trbs,
2911 struct urb *urb,
Andiry Xu8e51adc2010-07-22 15:23:31 -07002912 unsigned int td_index,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002913 gfp_t mem_flags)
2914{
2915 int ret;
Andiry Xu8e51adc2010-07-22 15:23:31 -07002916 struct urb_priv *urb_priv;
2917 struct xhci_td *td;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002918 struct xhci_ring *ep_ring;
John Yound115b042009-07-27 12:05:15 -07002919 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002920
2921 ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
2922 if (!ep_ring) {
2923 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
2924 stream_id);
2925 return -EINVAL;
2926 }
2927
2928 ret = prepare_ring(xhci, ep_ring,
Matt Evans28ccd292011-03-29 13:40:46 +11002929 le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
Andiry Xu3b72fca2012-03-05 17:49:32 +08002930 num_trbs, mem_flags);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002931 if (ret)
2932 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002933
Andiry Xu8e51adc2010-07-22 15:23:31 -07002934 urb_priv = urb->hcpriv;
2935 td = urb_priv->td[td_index];
2936
2937 INIT_LIST_HEAD(&td->td_list);
2938 INIT_LIST_HEAD(&td->cancelled_td_list);
2939
2940 if (td_index == 0) {
Sarah Sharp214f76f2010-10-26 11:22:02 -07002941 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07002942 if (unlikely(ret))
Andiry Xu8e51adc2010-07-22 15:23:31 -07002943 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002944 }
2945
Andiry Xu8e51adc2010-07-22 15:23:31 -07002946 td->urb = urb;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002947 /* Add this TD to the tail of the endpoint ring's TD list */
Andiry Xu8e51adc2010-07-22 15:23:31 -07002948 list_add_tail(&td->td_list, &ep_ring->td_list);
2949 td->start_seg = ep_ring->enq_seg;
2950 td->first_trb = ep_ring->enqueue;
2951
2952 urb_priv->td[td_index] = td;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07002953
2954 return 0;
2955}
2956
Alexandr Ivanovd2510342016-04-22 13:17:09 +03002957static unsigned int count_trbs(u64 addr, u64 len)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002958{
Alexandr Ivanovd2510342016-04-22 13:17:09 +03002959 unsigned int num_trbs;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002960
Alexandr Ivanovd2510342016-04-22 13:17:09 +03002961 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
2962 TRB_MAX_BUFF_SIZE);
2963 if (num_trbs == 0)
2964 num_trbs++;
Sarah Sharp8a96c052009-04-27 19:59:19 -07002965
Sarah Sharp8a96c052009-04-27 19:59:19 -07002966 return num_trbs;
2967}
2968
Alexandr Ivanovd2510342016-04-22 13:17:09 +03002969static inline unsigned int count_trbs_needed(struct urb *urb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07002970{
Alexandr Ivanovd2510342016-04-22 13:17:09 +03002971 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length);
2972}
2973
2974static unsigned int count_sg_trbs_needed(struct urb *urb)
2975{
2976 struct scatterlist *sg;
2977 unsigned int i, len, full_len, num_trbs = 0;
2978
2979 full_len = urb->transfer_buffer_length;
2980
2981 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) {
2982 len = sg_dma_len(sg);
2983 num_trbs += count_trbs(sg_dma_address(sg), len);
2984 len = min_t(unsigned int, len, full_len);
2985 full_len -= len;
2986 if (full_len == 0)
2987 break;
2988 }
2989
2990 return num_trbs;
2991}
2992
2993static unsigned int count_isoc_trbs_needed(struct urb *urb, int i)
2994{
2995 u64 addr, len;
2996
2997 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
2998 len = urb->iso_frame_desc[i].length;
2999
3000 return count_trbs(addr, len);
3001}
3002
3003static void check_trb_math(struct urb *urb, int running_total)
3004{
3005 if (unlikely(running_total != urb->transfer_buffer_length))
Paul Zimmermana2490182011-02-12 14:06:44 -08003006 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
Sarah Sharp8a96c052009-04-27 19:59:19 -07003007 "queued %#x (%d), asked for %#x (%d)\n",
3008 __func__,
3009 urb->ep->desc.bEndpointAddress,
3010 running_total, running_total,
3011 urb->transfer_buffer_length,
3012 urb->transfer_buffer_length);
3013}
3014
Sarah Sharp23e3be12009-04-29 19:05:20 -07003015static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003016 unsigned int ep_index, unsigned int stream_id, int start_cycle,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003017 struct xhci_generic_trb *start_trb)
Sarah Sharp8a96c052009-04-27 19:59:19 -07003018{
Sarah Sharp8a96c052009-04-27 19:59:19 -07003019 /*
3020 * Pass all the TRBs to the hardware at once and make sure this write
3021 * isn't reordered.
3022 */
3023 wmb();
Andiry Xu50f7b522010-12-20 15:09:34 +08003024 if (start_cycle)
Matt Evans28ccd292011-03-29 13:40:46 +11003025 start_trb->field[3] |= cpu_to_le32(start_cycle);
Andiry Xu50f7b522010-12-20 15:09:34 +08003026 else
Matt Evans28ccd292011-03-29 13:40:46 +11003027 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
Andiry Xube88fe42010-10-14 07:22:57 -07003028 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
Sarah Sharp8a96c052009-04-27 19:59:19 -07003029}
3030
Alexandr Ivanov78140152016-04-22 13:17:11 +03003031static void check_interval(struct xhci_hcd *xhci, struct urb *urb,
3032 struct xhci_ep_ctx *ep_ctx)
Sarah Sharp624defa2009-09-02 12:14:28 -07003033{
Sarah Sharp624defa2009-09-02 12:14:28 -07003034 int xhci_interval;
3035 int ep_interval;
3036
Matt Evans28ccd292011-03-29 13:40:46 +11003037 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
Sarah Sharp624defa2009-09-02 12:14:28 -07003038 ep_interval = urb->interval;
Alexandr Ivanov78140152016-04-22 13:17:11 +03003039
Sarah Sharp624defa2009-09-02 12:14:28 -07003040 /* Convert to microframes */
3041 if (urb->dev->speed == USB_SPEED_LOW ||
3042 urb->dev->speed == USB_SPEED_FULL)
3043 ep_interval *= 8;
Alexandr Ivanov78140152016-04-22 13:17:11 +03003044
Sarah Sharp624defa2009-09-02 12:14:28 -07003045 /* FIXME change this to a warning and a suggestion to use the new API
3046 * to set the polling interval (once the API is added).
3047 */
3048 if (xhci_interval != ep_interval) {
Dmitry Kasatkin0730d522013-08-27 17:47:35 +03003049 dev_dbg_ratelimited(&urb->dev->dev,
3050 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n",
3051 ep_interval, ep_interval == 1 ? "" : "s",
3052 xhci_interval, xhci_interval == 1 ? "" : "s");
Sarah Sharp624defa2009-09-02 12:14:28 -07003053 urb->interval = xhci_interval;
3054 /* Convert back to frames for LS/FS devices */
3055 if (urb->dev->speed == USB_SPEED_LOW ||
3056 urb->dev->speed == USB_SPEED_FULL)
3057 urb->interval /= 8;
3058 }
Alexandr Ivanov78140152016-04-22 13:17:11 +03003059}
3060
3061/*
3062 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
3063 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
3064 * (comprised of sg list entries) can take several service intervals to
3065 * transmit.
3066 */
3067int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3068 struct urb *urb, int slot_id, unsigned int ep_index)
3069{
3070 struct xhci_ep_ctx *ep_ctx;
3071
3072 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index);
3073 check_interval(xhci, urb, ep_ctx);
3074
Dan Carpenter3fc82062012-03-28 10:30:26 +03003075 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
Sarah Sharp624defa2009-09-02 12:14:28 -07003076}
3077
Sarah Sharp04dd9502009-11-11 10:28:30 -08003078/*
Sarah Sharp4525c0a2012-10-25 15:56:40 -07003079 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
3080 * packets remaining in the TD (*not* including this TRB).
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003081 *
3082 * Total TD packet count = total_packet_count =
Sarah Sharp4525c0a2012-10-25 15:56:40 -07003083 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003084 *
3085 * Packets transferred up to and including this TRB = packets_transferred =
3086 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
3087 *
3088 * TD size = total_packet_count - packets_transferred
3089 *
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003090 * For xHCI 0.96 and older, TD size field should be the remaining bytes
3091 * including this TRB, right shifted by 10
3092 *
3093 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31.
3094 * This is taken care of in the TRB_TD_SIZE() macro
3095 *
Sarah Sharp4525c0a2012-10-25 15:56:40 -07003096 * The last TRB in a TD must have the TD size set to zero.
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003097 */
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003098static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred,
3099 int trb_buff_len, unsigned int td_total_len,
Mathias Nyman124c3932016-06-21 10:57:59 +03003100 struct urb *urb, bool more_trbs_coming)
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003101{
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003102 u32 maxp, total_packet_count;
3103
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003104 /* MTK xHCI is mostly 0.97 but contains some features from 1.0 */
3105 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST))
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003106 return ((td_total_len - transferred) >> 10);
3107
Sarah Sharp48df4a62011-08-12 10:23:01 -07003108 /* One TRB with a zero-length data packet. */
Mathias Nyman124c3932016-06-21 10:57:59 +03003109 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003110 trb_buff_len == td_total_len)
Sarah Sharp48df4a62011-08-12 10:23:01 -07003111 return 0;
3112
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003113 /* for MTK xHCI, TD size doesn't include this TRB */
3114 if (xhci->quirks & XHCI_MTK_HOST)
3115 trb_buff_len = 0;
3116
3117 maxp = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
3118 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
3119
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003120 /* Queueing functions don't count the current TRB into transferred */
3121 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003122}
3123
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003124
Mathias Nyman474ed232016-06-21 10:58:01 +03003125static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003126 u32 *trb_buff_len, struct xhci_segment *seg)
Mathias Nyman474ed232016-06-21 10:58:01 +03003127{
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003128 struct device *dev = xhci_to_hcd(xhci)->self.controller;
Mathias Nyman474ed232016-06-21 10:58:01 +03003129 unsigned int unalign;
3130 unsigned int max_pkt;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003131 u32 new_buff_len;
Mathias Nyman474ed232016-06-21 10:58:01 +03003132
3133 max_pkt = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
3134 unalign = (enqd_len + *trb_buff_len) % max_pkt;
3135
3136 /* we got lucky, last normal TRB data on segment is packet aligned */
3137 if (unalign == 0)
3138 return 0;
3139
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003140 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n",
3141 unalign, *trb_buff_len);
3142
Mathias Nyman474ed232016-06-21 10:58:01 +03003143 /* is the last nornal TRB alignable by splitting it */
3144 if (*trb_buff_len > unalign) {
3145 *trb_buff_len -= unalign;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003146 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len);
Mathias Nyman474ed232016-06-21 10:58:01 +03003147 return 0;
3148 }
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003149
3150 /*
3151 * We want enqd_len + trb_buff_len to sum up to a number aligned to
3152 * number which is divisible by the endpoint's wMaxPacketSize. IOW:
3153 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0.
3154 */
3155 new_buff_len = max_pkt - (enqd_len % max_pkt);
3156
3157 if (new_buff_len > (urb->transfer_buffer_length - enqd_len))
3158 new_buff_len = (urb->transfer_buffer_length - enqd_len);
3159
3160 /* create a max max_pkt sized bounce buffer pointed to by last trb */
3161 if (usb_urb_dir_out(urb)) {
3162 sg_pcopy_to_buffer(urb->sg, urb->num_mapped_sgs,
3163 seg->bounce_buf, new_buff_len, enqd_len);
3164 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3165 max_pkt, DMA_TO_DEVICE);
3166 } else {
3167 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
3168 max_pkt, DMA_FROM_DEVICE);
3169 }
3170
3171 if (dma_mapping_error(dev, seg->bounce_dma)) {
3172 /* try without aligning. Some host controllers survive */
3173 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n");
3174 return 0;
3175 }
3176 *trb_buff_len = new_buff_len;
3177 seg->bounce_len = new_buff_len;
3178 seg->bounce_offs = enqd_len;
3179
3180 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len);
3181
Mathias Nyman474ed232016-06-21 10:58:01 +03003182 return 1;
3183}
3184
Sarah Sharpb10de142009-04-27 19:58:50 -07003185/* This is very similar to what ehci-q.c qtd_fill() does */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003186int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpb10de142009-04-27 19:58:50 -07003187 struct urb *urb, int slot_id, unsigned int ep_index)
3188{
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003189 struct xhci_ring *ring;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003190 struct urb_priv *urb_priv;
Sarah Sharpb10de142009-04-27 19:58:50 -07003191 struct xhci_td *td;
Sarah Sharpb10de142009-04-27 19:58:50 -07003192 struct xhci_generic_trb *start_trb;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003193 struct scatterlist *sg = NULL;
Mathias Nyman5a83f042016-06-21 10:57:58 +03003194 bool more_trbs_coming = true;
3195 bool need_zero_pkt = false;
Mathias Nyman86065c22016-06-21 10:58:00 +03003196 bool first_trb = true;
3197 unsigned int num_trbs;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003198 unsigned int start_cycle, num_sgs = 0;
Mathias Nyman86065c22016-06-21 10:58:00 +03003199 unsigned int enqd_len, block_len, trb_buff_len, full_len;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003200 int sent_len, ret;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003201 u32 field, length_field, remainder;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003202 u64 addr, send_addr;
Sarah Sharpb10de142009-04-27 19:58:50 -07003203
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003204 ring = xhci_urb_to_transfer_ring(xhci, urb);
3205 if (!ring)
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003206 return -EINVAL;
Sarah Sharpb10de142009-04-27 19:58:50 -07003207
Mathias Nyman86065c22016-06-21 10:58:00 +03003208 full_len = urb->transfer_buffer_length;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003209 /* If we have scatter/gather list, we use it. */
3210 if (urb->num_sgs) {
3211 num_sgs = urb->num_mapped_sgs;
3212 sg = urb->sg;
Mathias Nyman86065c22016-06-21 10:58:00 +03003213 addr = (u64) sg_dma_address(sg);
3214 block_len = sg_dma_len(sg);
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003215 num_trbs = count_sg_trbs_needed(urb);
Mathias Nyman86065c22016-06-21 10:58:00 +03003216 } else {
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003217 num_trbs = count_trbs_needed(urb);
Mathias Nyman86065c22016-06-21 10:58:00 +03003218 addr = (u64) urb->transfer_dma;
3219 block_len = full_len;
3220 }
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003221 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3222 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003223 num_trbs, urb, 0, mem_flags);
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003224 if (unlikely(ret < 0))
Sarah Sharpb10de142009-04-27 19:58:50 -07003225 return ret;
3226
Andiry Xu8e51adc2010-07-22 15:23:31 -07003227 urb_priv = urb->hcpriv;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03003228
3229 /* Deal with URB_ZERO_PACKET - need one more td/trb */
Mathias Nyman5a83f042016-06-21 10:57:58 +03003230 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->length > 1)
3231 need_zero_pkt = true;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03003232
Andiry Xu8e51adc2010-07-22 15:23:31 -07003233 td = urb_priv->td[0];
3234
Sarah Sharpb10de142009-04-27 19:58:50 -07003235 /*
3236 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3237 * until we've finished creating all the other TRBs. The ring's cycle
3238 * state may change as we enqueue the other TRBs, so save it too.
3239 */
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003240 start_trb = &ring->enqueue->generic;
3241 start_cycle = ring->cycle_state;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003242 send_addr = addr;
Sarah Sharpb10de142009-04-27 19:58:50 -07003243
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003244 /* Queue the TRBs, even if they are zero-length */
Mathias Nyman86065c22016-06-21 10:58:00 +03003245 for (enqd_len = 0; enqd_len < full_len; enqd_len += trb_buff_len) {
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003246 field = TRB_TYPE(TRB_NORMAL);
3247
Mathias Nyman86065c22016-06-21 10:58:00 +03003248 /* TRB buffer should not cross 64KB boundaries */
3249 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
3250 trb_buff_len = min_t(unsigned int, trb_buff_len, block_len);
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003251
Mathias Nyman86065c22016-06-21 10:58:00 +03003252 if (enqd_len + trb_buff_len > full_len)
3253 trb_buff_len = full_len - enqd_len;
Sarah Sharpb10de142009-04-27 19:58:50 -07003254
3255 /* Don't change the cycle bit of the first TRB until later */
Mathias Nyman86065c22016-06-21 10:58:00 +03003256 if (first_trb) {
3257 first_trb = false;
Andiry Xu50f7b522010-12-20 15:09:34 +08003258 if (start_cycle == 0)
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003259 field |= TRB_CYCLE;
Andiry Xu50f7b522010-12-20 15:09:34 +08003260 } else
Mathias Nyman5a5a0b12016-06-21 10:57:57 +03003261 field |= ring->cycle_state;
Sarah Sharpb10de142009-04-27 19:58:50 -07003262
3263 /* Chain all the TRBs together; clear the chain bit in the last
3264 * TRB to indicate it's the last TRB in the chain.
3265 */
Mathias Nyman86065c22016-06-21 10:58:00 +03003266 if (enqd_len + trb_buff_len < full_len) {
Sarah Sharpb10de142009-04-27 19:58:50 -07003267 field |= TRB_CHAIN;
Mathias Nyman2d98ef42016-06-21 10:58:04 +03003268 if (trb_is_link(ring->enqueue + 1)) {
Mathias Nyman474ed232016-06-21 10:58:01 +03003269 if (xhci_align_td(xhci, urb, enqd_len,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003270 &trb_buff_len,
3271 ring->enq_seg)) {
3272 send_addr = ring->enq_seg->bounce_dma;
3273 /* assuming TD won't span 2 segs */
3274 td->bounce_seg = ring->enq_seg;
3275 }
Mathias Nyman474ed232016-06-21 10:58:01 +03003276 }
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003277 }
3278 if (enqd_len + trb_buff_len >= full_len) {
3279 field &= ~TRB_CHAIN;
Sarah Sharpb10de142009-04-27 19:58:50 -07003280 field |= TRB_IOC;
Mathias Nyman124c3932016-06-21 10:57:59 +03003281 more_trbs_coming = false;
Mathias Nyman5a83f042016-06-21 10:57:58 +03003282 td->last_trb = ring->enqueue;
Sarah Sharpb10de142009-04-27 19:58:50 -07003283 }
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003284
3285 /* Only set interrupt on short packet for IN endpoints */
3286 if (usb_urb_dir_in(urb))
3287 field |= TRB_ISP;
3288
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003289 /* Set the TRB length, TD size, and interrupter fields. */
Mathias Nyman86065c22016-06-21 10:58:00 +03003290 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len,
3291 full_len, urb, more_trbs_coming);
3292
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003293 length_field = TRB_LEN(trb_buff_len) |
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003294 TRB_TD_SIZE(remainder) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003295 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003296
Mathias Nyman124c3932016-06-21 10:57:59 +03003297 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003298 lower_32_bits(send_addr),
3299 upper_32_bits(send_addr),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003300 length_field,
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003301 field);
3302
Sarah Sharpb10de142009-04-27 19:58:50 -07003303 addr += trb_buff_len;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003304 sent_len = trb_buff_len;
Sarah Sharpb10de142009-04-27 19:58:50 -07003305
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003306 while (sg && sent_len >= block_len) {
Mathias Nyman86065c22016-06-21 10:58:00 +03003307 /* New sg entry */
3308 --num_sgs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003309 sent_len -= block_len;
Mathias Nyman86065c22016-06-21 10:58:00 +03003310 if (num_sgs != 0) {
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003311 sg = sg_next(sg);
Mathias Nyman86065c22016-06-21 10:58:00 +03003312 block_len = sg_dma_len(sg);
3313 addr = (u64) sg_dma_address(sg);
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003314 addr += sent_len;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003315 }
3316 }
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003317 block_len -= sent_len;
3318 send_addr = addr;
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003319 }
3320
Mathias Nyman5a83f042016-06-21 10:57:58 +03003321 if (need_zero_pkt) {
3322 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3323 ep_index, urb->stream_id,
3324 1, urb, 1, mem_flags);
3325 urb_priv->td[1]->last_trb = ring->enqueue;
3326 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC;
3327 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field);
3328 }
3329
Mathias Nyman86065c22016-06-21 10:58:00 +03003330 check_trb_math(urb, enqd_len);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003331 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003332 start_cycle, start_trb);
Sarah Sharpb10de142009-04-27 19:58:50 -07003333 return 0;
3334}
3335
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003336/* Caller must have locked xhci->lock */
Sarah Sharp23e3be12009-04-29 19:05:20 -07003337int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003338 struct urb *urb, int slot_id, unsigned int ep_index)
3339{
3340 struct xhci_ring *ep_ring;
3341 int num_trbs;
3342 int ret;
3343 struct usb_ctrlrequest *setup;
3344 struct xhci_generic_trb *start_trb;
3345 int start_cycle;
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003346 u32 field, length_field, remainder;
Andiry Xu8e51adc2010-07-22 15:23:31 -07003347 struct urb_priv *urb_priv;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003348 struct xhci_td *td;
3349
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003350 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
3351 if (!ep_ring)
3352 return -EINVAL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003353
3354 /*
3355 * Need to copy setup packet into setup TRB, so we can't use the setup
3356 * DMA address.
3357 */
3358 if (!urb->setup_packet)
3359 return -EINVAL;
3360
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003361 /* 1 TRB for setup, 1 for status */
3362 num_trbs = 2;
3363 /*
3364 * Don't need to check if we need additional event data and normal TRBs,
3365 * since data in control transfers will never get bigger than 16MB
3366 * XXX: can we get a buffer that crosses 64KB boundaries?
3367 */
3368 if (urb->transfer_buffer_length > 0)
3369 num_trbs++;
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003370 ret = prepare_transfer(xhci, xhci->devs[slot_id],
3371 ep_index, urb->stream_id,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003372 num_trbs, urb, 0, mem_flags);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003373 if (ret < 0)
3374 return ret;
3375
Andiry Xu8e51adc2010-07-22 15:23:31 -07003376 urb_priv = urb->hcpriv;
3377 td = urb_priv->td[0];
3378
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003379 /*
3380 * Don't give the first TRB to the hardware (by toggling the cycle bit)
3381 * until we've finished creating all the other TRBs. The ring's cycle
3382 * state may change as we enqueue the other TRBs, so save it too.
3383 */
3384 start_trb = &ep_ring->enqueue->generic;
3385 start_cycle = ep_ring->cycle_state;
3386
3387 /* Queue setup TRB - see section 6.4.1.2.1 */
3388 /* FIXME better way to translate setup_packet into two u32 fields? */
3389 setup = (struct usb_ctrlrequest *) urb->setup_packet;
Andiry Xu50f7b522010-12-20 15:09:34 +08003390 field = 0;
3391 field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
3392 if (start_cycle == 0)
3393 field |= 0x1;
Andiry Xub83cdc82011-05-05 18:13:56 +08003394
Mathias Nymandca77942015-09-21 17:46:16 +03003395 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02003396 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) {
Andiry Xub83cdc82011-05-05 18:13:56 +08003397 if (urb->transfer_buffer_length > 0) {
3398 if (setup->bRequestType & USB_DIR_IN)
3399 field |= TRB_TX_TYPE(TRB_DATA_IN);
3400 else
3401 field |= TRB_TX_TYPE(TRB_DATA_OUT);
3402 }
3403 }
3404
Andiry Xu3b72fca2012-03-05 17:49:32 +08003405 queue_trb(xhci, ep_ring, true,
Matt Evans28ccd292011-03-29 13:40:46 +11003406 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
3407 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
3408 TRB_LEN(8) | TRB_INTR_TARGET(0),
3409 /* Immediate data in pointer */
3410 field);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003411
3412 /* If there's data, queue data TRBs */
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003413 /* Only set interrupt on short packet for IN endpoints */
3414 if (usb_urb_dir_in(urb))
3415 field = TRB_ISP | TRB_TYPE(TRB_DATA);
3416 else
3417 field = TRB_TYPE(TRB_DATA);
3418
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003419 remainder = xhci_td_remainder(xhci, 0,
3420 urb->transfer_buffer_length,
3421 urb->transfer_buffer_length,
3422 urb, 1);
3423
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003424 length_field = TRB_LEN(urb->transfer_buffer_length) |
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003425 TRB_TD_SIZE(remainder) |
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003426 TRB_INTR_TARGET(0);
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003427
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003428 if (urb->transfer_buffer_length > 0) {
3429 if (setup->bRequestType & USB_DIR_IN)
3430 field |= TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003431 queue_trb(xhci, ep_ring, true,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003432 lower_32_bits(urb->transfer_dma),
3433 upper_32_bits(urb->transfer_dma),
Sarah Sharpf9dc68f2009-07-27 12:03:07 -07003434 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003435 field | ep_ring->cycle_state);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003436 }
3437
3438 /* Save the DMA address of the last TRB in the TD */
3439 td->last_trb = ep_ring->enqueue;
3440
3441 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
3442 /* If the device sent data, the status stage is an OUT transfer */
3443 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
3444 field = 0;
3445 else
3446 field = TRB_DIR_IN;
Andiry Xu3b72fca2012-03-05 17:49:32 +08003447 queue_trb(xhci, ep_ring, false,
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003448 0,
3449 0,
3450 TRB_INTR_TARGET(0),
3451 /* Event on completion */
3452 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
3453
Sarah Sharpe9df17e2010-04-02 15:34:43 -07003454 giveback_first_trb(xhci, slot_id, ep_index, 0,
Andiry Xue1eab2e2011-01-04 16:30:39 -08003455 start_cycle, start_trb);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003456 return 0;
3457}
3458
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003459/*
3460 * The transfer burst count field of the isochronous TRB defines the number of
3461 * bursts that are required to move all packets in this TD. Only SuperSpeed
3462 * devices can burst up to bMaxBurst number of packets per service interval.
3463 * This field is zero based, meaning a value of zero in the field means one
3464 * burst. Basically, for everything but SuperSpeed devices, this field will be
3465 * zero. Only xHCI 1.0 host controllers support this field.
3466 */
3467static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003468 struct urb *urb, unsigned int total_packet_count)
3469{
3470 unsigned int max_burst;
3471
Mathias Nyman09c352e2016-02-12 16:40:17 +02003472 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER)
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003473 return 0;
3474
3475 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
Mathias Nyman3213b152014-06-24 17:14:41 +03003476 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
Sarah Sharp5cd43e32011-04-08 09:37:29 -07003477}
3478
Sarah Sharpb61d3782011-04-19 17:43:33 -07003479/*
3480 * Returns the number of packets in the last "burst" of packets. This field is
3481 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
3482 * the last burst packet count is equal to the total number of packets in the
3483 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3484 * must contain (bMaxBurst + 1) number of packets, but the last burst can
3485 * contain 1 to (bMaxBurst + 1) packets.
3486 */
3487static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
Sarah Sharpb61d3782011-04-19 17:43:33 -07003488 struct urb *urb, unsigned int total_packet_count)
3489{
3490 unsigned int max_burst;
3491 unsigned int residue;
3492
3493 if (xhci->hci_version < 0x100)
3494 return 0;
3495
Mathias Nyman09c352e2016-02-12 16:40:17 +02003496 if (urb->dev->speed >= USB_SPEED_SUPER) {
Sarah Sharpb61d3782011-04-19 17:43:33 -07003497 /* bMaxBurst is zero based: 0 means 1 packet per burst */
3498 max_burst = urb->ep->ss_ep_comp.bMaxBurst;
3499 residue = total_packet_count % (max_burst + 1);
3500 /* If residue is zero, the last burst contains (max_burst + 1)
3501 * number of packets, but the TLBPC field is zero-based.
3502 */
3503 if (residue == 0)
3504 return max_burst;
3505 return residue - 1;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003506 }
Mathias Nyman09c352e2016-02-12 16:40:17 +02003507 if (total_packet_count == 0)
3508 return 0;
3509 return total_packet_count - 1;
Sarah Sharpb61d3782011-04-19 17:43:33 -07003510}
3511
Lu Baolu79b80942015-08-06 19:24:00 +03003512/*
3513 * Calculates Frame ID field of the isochronous TRB identifies the
3514 * target frame that the Interval associated with this Isochronous
3515 * Transfer Descriptor will start on. Refer to 4.11.2.5 in 1.1 spec.
3516 *
3517 * Returns actual frame id on success, negative value on error.
3518 */
3519static int xhci_get_isoc_frame_id(struct xhci_hcd *xhci,
3520 struct urb *urb, int index)
3521{
3522 int start_frame, ist, ret = 0;
3523 int start_frame_id, end_frame_id, current_frame_id;
3524
3525 if (urb->dev->speed == USB_SPEED_LOW ||
3526 urb->dev->speed == USB_SPEED_FULL)
3527 start_frame = urb->start_frame + index * urb->interval;
3528 else
3529 start_frame = (urb->start_frame + index * urb->interval) >> 3;
3530
3531 /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2):
3532 *
3533 * If bit [3] of IST is cleared to '0', software can add a TRB no
3534 * later than IST[2:0] Microframes before that TRB is scheduled to
3535 * be executed.
3536 * If bit [3] of IST is set to '1', software can add a TRB no later
3537 * than IST[2:0] Frames before that TRB is scheduled to be executed.
3538 */
3539 ist = HCS_IST(xhci->hcs_params2) & 0x7;
3540 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
3541 ist <<= 3;
3542
3543 /* Software shall not schedule an Isoch TD with a Frame ID value that
3544 * is less than the Start Frame ID or greater than the End Frame ID,
3545 * where:
3546 *
3547 * End Frame ID = (Current MFINDEX register value + 895 ms.) MOD 2048
3548 * Start Frame ID = (Current MFINDEX register value + IST + 1) MOD 2048
3549 *
3550 * Both the End Frame ID and Start Frame ID values are calculated
3551 * in microframes. When software determines the valid Frame ID value;
3552 * The End Frame ID value should be rounded down to the nearest Frame
3553 * boundary, and the Start Frame ID value should be rounded up to the
3554 * nearest Frame boundary.
3555 */
3556 current_frame_id = readl(&xhci->run_regs->microframe_index);
3557 start_frame_id = roundup(current_frame_id + ist + 1, 8);
3558 end_frame_id = rounddown(current_frame_id + 895 * 8, 8);
3559
3560 start_frame &= 0x7ff;
3561 start_frame_id = (start_frame_id >> 3) & 0x7ff;
3562 end_frame_id = (end_frame_id >> 3) & 0x7ff;
3563
3564 xhci_dbg(xhci, "%s: index %d, reg 0x%x start_frame_id 0x%x, end_frame_id 0x%x, start_frame 0x%x\n",
3565 __func__, index, readl(&xhci->run_regs->microframe_index),
3566 start_frame_id, end_frame_id, start_frame);
3567
3568 if (start_frame_id < end_frame_id) {
3569 if (start_frame > end_frame_id ||
3570 start_frame < start_frame_id)
3571 ret = -EINVAL;
3572 } else if (start_frame_id > end_frame_id) {
3573 if ((start_frame > end_frame_id &&
3574 start_frame < start_frame_id))
3575 ret = -EINVAL;
3576 } else {
3577 ret = -EINVAL;
3578 }
3579
3580 if (index == 0) {
3581 if (ret == -EINVAL || start_frame == start_frame_id) {
3582 start_frame = start_frame_id + 1;
3583 if (urb->dev->speed == USB_SPEED_LOW ||
3584 urb->dev->speed == USB_SPEED_FULL)
3585 urb->start_frame = start_frame;
3586 else
3587 urb->start_frame = start_frame << 3;
3588 ret = 0;
3589 }
3590 }
3591
3592 if (ret) {
3593 xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n",
3594 start_frame, current_frame_id, index,
3595 start_frame_id, end_frame_id);
3596 xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n");
3597 return ret;
3598 }
3599
3600 return start_frame;
3601}
3602
Andiry Xu04e51902010-07-22 15:23:39 -07003603/* This is for isoc transfer */
3604static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
3605 struct urb *urb, int slot_id, unsigned int ep_index)
3606{
3607 struct xhci_ring *ep_ring;
3608 struct urb_priv *urb_priv;
3609 struct xhci_td *td;
3610 int num_tds, trbs_per_td;
3611 struct xhci_generic_trb *start_trb;
3612 bool first_trb;
3613 int start_cycle;
3614 u32 field, length_field;
3615 int running_total, trb_buff_len, td_len, td_remain_len, ret;
3616 u64 start_addr, addr;
3617 int i, j;
Andiry Xu47cbf692010-12-20 14:49:48 +08003618 bool more_trbs_coming;
Lu Baolu79b80942015-08-06 19:24:00 +03003619 struct xhci_virt_ep *xep;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003620 int frame_id;
Andiry Xu04e51902010-07-22 15:23:39 -07003621
Lu Baolu79b80942015-08-06 19:24:00 +03003622 xep = &xhci->devs[slot_id]->eps[ep_index];
Andiry Xu04e51902010-07-22 15:23:39 -07003623 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
3624
3625 num_tds = urb->number_of_packets;
3626 if (num_tds < 1) {
3627 xhci_dbg(xhci, "Isoc URB with zero packets?\n");
3628 return -EINVAL;
3629 }
Andiry Xu04e51902010-07-22 15:23:39 -07003630 start_addr = (u64) urb->transfer_dma;
3631 start_trb = &ep_ring->enqueue->generic;
3632 start_cycle = ep_ring->cycle_state;
3633
Sarah Sharp522989a2011-07-29 12:44:32 -07003634 urb_priv = urb->hcpriv;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003635 /* Queue the TRBs for each TD, even if they are zero-length */
Andiry Xu04e51902010-07-22 15:23:39 -07003636 for (i = 0; i < num_tds; i++) {
Mathias Nyman09c352e2016-02-12 16:40:17 +02003637 unsigned int total_pkt_count, max_pkt;
3638 unsigned int burst_count, last_burst_pkt_count;
3639 u32 sia_frame_id;
Andiry Xu04e51902010-07-22 15:23:39 -07003640
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003641 first_trb = true;
Andiry Xu04e51902010-07-22 15:23:39 -07003642 running_total = 0;
3643 addr = start_addr + urb->iso_frame_desc[i].offset;
3644 td_len = urb->iso_frame_desc[i].length;
3645 td_remain_len = td_len;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003646 max_pkt = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
3647 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt);
3648
Sarah Sharp48df4a62011-08-12 10:23:01 -07003649 /* A zero-length transfer still involves at least one packet. */
Mathias Nyman09c352e2016-02-12 16:40:17 +02003650 if (total_pkt_count == 0)
3651 total_pkt_count++;
3652 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count);
3653 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci,
3654 urb, total_pkt_count);
Andiry Xu04e51902010-07-22 15:23:39 -07003655
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003656 trbs_per_td = count_isoc_trbs_needed(urb, i);
Andiry Xu04e51902010-07-22 15:23:39 -07003657
3658 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003659 urb->stream_id, trbs_per_td, urb, i, mem_flags);
Sarah Sharp522989a2011-07-29 12:44:32 -07003660 if (ret < 0) {
3661 if (i == 0)
3662 return ret;
3663 goto cleanup;
3664 }
Andiry Xu04e51902010-07-22 15:23:39 -07003665 td = urb_priv->td[i];
Mathias Nyman09c352e2016-02-12 16:40:17 +02003666
3667 /* use SIA as default, if frame id is used overwrite it */
3668 sia_frame_id = TRB_SIA;
3669 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3670 HCC_CFC(xhci->hcc_params)) {
3671 frame_id = xhci_get_isoc_frame_id(xhci, urb, i);
3672 if (frame_id >= 0)
3673 sia_frame_id = TRB_FRAME_ID(frame_id);
3674 }
3675 /*
3676 * Set isoc specific data for the first TRB in a TD.
3677 * Prevent HW from getting the TRBs by keeping the cycle state
3678 * inverted in the first TDs isoc TRB.
3679 */
Mathias Nyman2f6d3b62016-02-12 16:40:18 +02003680 field = TRB_TYPE(TRB_ISOC) |
Mathias Nyman09c352e2016-02-12 16:40:17 +02003681 TRB_TLBPC(last_burst_pkt_count) |
3682 sia_frame_id |
3683 (i ? ep_ring->cycle_state : !start_cycle);
3684
Mathias Nyman2f6d3b62016-02-12 16:40:18 +02003685 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */
3686 if (!xep->use_extended_tbc)
3687 field |= TRB_TBC(burst_count);
3688
Mathias Nyman09c352e2016-02-12 16:40:17 +02003689 /* fill the rest of the TRB fields, and remaining normal TRBs */
Andiry Xu04e51902010-07-22 15:23:39 -07003690 for (j = 0; j < trbs_per_td; j++) {
3691 u32 remainder = 0;
Andiry Xu04e51902010-07-22 15:23:39 -07003692
Mathias Nyman09c352e2016-02-12 16:40:17 +02003693 /* only first TRB is isoc, overwrite otherwise */
3694 if (!first_trb)
3695 field = TRB_TYPE(TRB_NORMAL) |
3696 ep_ring->cycle_state;
Andiry Xu04e51902010-07-22 15:23:39 -07003697
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003698 /* Only set interrupt on short packet for IN EPs */
3699 if (usb_urb_dir_in(urb))
3700 field |= TRB_ISP;
3701
Mathias Nyman09c352e2016-02-12 16:40:17 +02003702 /* Set the chain bit for all except the last TRB */
Andiry Xu04e51902010-07-22 15:23:39 -07003703 if (j < trbs_per_td - 1) {
Andiry Xu47cbf692010-12-20 14:49:48 +08003704 more_trbs_coming = true;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003705 field |= TRB_CHAIN;
Andiry Xu04e51902010-07-22 15:23:39 -07003706 } else {
Mathias Nyman09c352e2016-02-12 16:40:17 +02003707 more_trbs_coming = false;
Andiry Xu04e51902010-07-22 15:23:39 -07003708 td->last_trb = ep_ring->enqueue;
3709 field |= TRB_IOC;
Mathias Nyman09c352e2016-02-12 16:40:17 +02003710 /* set BEI, except for the last TD */
3711 if (xhci->hci_version >= 0x100 &&
3712 !(xhci->quirks & XHCI_AVOID_BEI) &&
3713 i < num_tds - 1)
3714 field |= TRB_BEI;
Andiry Xu04e51902010-07-22 15:23:39 -07003715 }
Andiry Xu04e51902010-07-22 15:23:39 -07003716 /* Calculate TRB length */
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003717 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
Andiry Xu04e51902010-07-22 15:23:39 -07003718 if (trb_buff_len > td_remain_len)
3719 trb_buff_len = td_remain_len;
3720
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003721 /* Set the TRB length, TD size, & interrupter fields. */
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003722 remainder = xhci_td_remainder(xhci, running_total,
3723 trb_buff_len, td_len,
Mathias Nyman124c3932016-06-21 10:57:59 +03003724 urb, more_trbs_coming);
Mathias Nymanc840d6c2015-10-09 13:30:08 +03003725
Andiry Xu04e51902010-07-22 15:23:39 -07003726 length_field = TRB_LEN(trb_buff_len) |
Andiry Xu04e51902010-07-22 15:23:39 -07003727 TRB_INTR_TARGET(0);
Sarah Sharp4da6e6f2011-04-01 14:01:30 -07003728
Mathias Nyman2f6d3b62016-02-12 16:40:18 +02003729 /* xhci 1.1 with ETE uses TD Size field for TBC */
3730 if (first_trb && xep->use_extended_tbc)
3731 length_field |= TRB_TD_SIZE_TBC(burst_count);
3732 else
3733 length_field |= TRB_TD_SIZE(remainder);
3734 first_trb = false;
3735
Andiry Xu3b72fca2012-03-05 17:49:32 +08003736 queue_trb(xhci, ep_ring, more_trbs_coming,
Andiry Xu04e51902010-07-22 15:23:39 -07003737 lower_32_bits(addr),
3738 upper_32_bits(addr),
3739 length_field,
Sarah Sharpaf8b9e62011-03-23 16:26:26 -07003740 field);
Andiry Xu04e51902010-07-22 15:23:39 -07003741 running_total += trb_buff_len;
3742
3743 addr += trb_buff_len;
3744 td_remain_len -= trb_buff_len;
3745 }
3746
3747 /* Check TD length */
3748 if (running_total != td_len) {
3749 xhci_err(xhci, "ISOC TD length unmatch\n");
Andiry Xucf840552012-01-18 17:47:12 +08003750 ret = -EINVAL;
3751 goto cleanup;
Andiry Xu04e51902010-07-22 15:23:39 -07003752 }
3753 }
3754
Lu Baolu79b80942015-08-06 19:24:00 +03003755 /* store the next frame id */
3756 if (HCC_CFC(xhci->hcc_params))
3757 xep->next_frame_id = urb->start_frame + num_tds * urb->interval;
3758
Andiry Xuc41136b2011-03-22 17:08:14 +08003759 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
3760 if (xhci->quirks & XHCI_AMD_PLL_FIX)
3761 usb_amd_quirk_pll_disable();
3762 }
3763 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
3764
Andiry Xue1eab2e2011-01-04 16:30:39 -08003765 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
3766 start_cycle, start_trb);
Andiry Xu04e51902010-07-22 15:23:39 -07003767 return 0;
Sarah Sharp522989a2011-07-29 12:44:32 -07003768cleanup:
3769 /* Clean up a partially enqueued isoc transfer. */
3770
3771 for (i--; i >= 0; i--)
Sarah Sharp585df1d2011-08-02 15:43:40 -07003772 list_del_init(&urb_priv->td[i]->td_list);
Sarah Sharp522989a2011-07-29 12:44:32 -07003773
3774 /* Use the first TD as a temporary variable to turn the TDs we've queued
3775 * into No-ops with a software-owned cycle bit. That way the hardware
3776 * won't accidentally start executing bogus TDs when we partially
3777 * overwrite them. td->first_trb and td->start_seg are already set.
3778 */
3779 urb_priv->td[0]->last_trb = ep_ring->enqueue;
3780 /* Every TRB except the first & last will have its cycle bit flipped. */
3781 td_to_noop(xhci, ep_ring, urb_priv->td[0], true);
3782
3783 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
3784 ep_ring->enqueue = urb_priv->td[0]->first_trb;
3785 ep_ring->enq_seg = urb_priv->td[0]->start_seg;
3786 ep_ring->cycle_state = start_cycle;
Andiry Xub008df62012-03-05 17:49:34 +08003787 ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp;
Sarah Sharp522989a2011-07-29 12:44:32 -07003788 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
3789 return ret;
Andiry Xu04e51902010-07-22 15:23:39 -07003790}
3791
3792/*
3793 * Check transfer ring to guarantee there is enough room for the urb.
3794 * Update ISO URB start_frame and interval.
Lu Baolu79b80942015-08-06 19:24:00 +03003795 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to
3796 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or
3797 * Contiguous Frame ID is not supported by HC.
Andiry Xu04e51902010-07-22 15:23:39 -07003798 */
3799int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
3800 struct urb *urb, int slot_id, unsigned int ep_index)
3801{
3802 struct xhci_virt_device *xdev;
3803 struct xhci_ring *ep_ring;
3804 struct xhci_ep_ctx *ep_ctx;
3805 int start_frame;
Andiry Xu04e51902010-07-22 15:23:39 -07003806 int num_tds, num_trbs, i;
3807 int ret;
Lu Baolu79b80942015-08-06 19:24:00 +03003808 struct xhci_virt_ep *xep;
3809 int ist;
Andiry Xu04e51902010-07-22 15:23:39 -07003810
3811 xdev = xhci->devs[slot_id];
Lu Baolu79b80942015-08-06 19:24:00 +03003812 xep = &xhci->devs[slot_id]->eps[ep_index];
Andiry Xu04e51902010-07-22 15:23:39 -07003813 ep_ring = xdev->eps[ep_index].ring;
3814 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
3815
3816 num_trbs = 0;
3817 num_tds = urb->number_of_packets;
3818 for (i = 0; i < num_tds; i++)
Alexandr Ivanovd2510342016-04-22 13:17:09 +03003819 num_trbs += count_isoc_trbs_needed(urb, i);
Andiry Xu04e51902010-07-22 15:23:39 -07003820
3821 /* Check the ring to guarantee there is enough room for the whole urb.
3822 * Do not insert any td of the urb to the ring if the check failed.
3823 */
Matt Evans28ccd292011-03-29 13:40:46 +11003824 ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003825 num_trbs, mem_flags);
Andiry Xu04e51902010-07-22 15:23:39 -07003826 if (ret)
3827 return ret;
3828
Lu Baolu79b80942015-08-06 19:24:00 +03003829 /*
3830 * Check interval value. This should be done before we start to
3831 * calculate the start frame value.
3832 */
Alexandr Ivanov78140152016-04-22 13:17:11 +03003833 check_interval(xhci, urb, ep_ctx);
Lu Baolu79b80942015-08-06 19:24:00 +03003834
3835 /* Calculate the start frame and put it in urb->start_frame. */
Lu Baolu42df7212015-11-18 10:48:21 +02003836 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) {
3837 if ((le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
3838 EP_STATE_RUNNING) {
3839 urb->start_frame = xep->next_frame_id;
3840 goto skip_start_over;
3841 }
Lu Baolu79b80942015-08-06 19:24:00 +03003842 }
3843
3844 start_frame = readl(&xhci->run_regs->microframe_index);
3845 start_frame &= 0x3fff;
3846 /*
3847 * Round up to the next frame and consider the time before trb really
3848 * gets scheduled by hardare.
3849 */
3850 ist = HCS_IST(xhci->hcs_params2) & 0x7;
3851 if (HCS_IST(xhci->hcs_params2) & (1 << 3))
3852 ist <<= 3;
3853 start_frame += ist + XHCI_CFC_DELAY;
3854 start_frame = roundup(start_frame, 8);
3855
3856 /*
3857 * Round up to the next ESIT (Endpoint Service Interval Time) if ESIT
3858 * is greate than 8 microframes.
3859 */
3860 if (urb->dev->speed == USB_SPEED_LOW ||
3861 urb->dev->speed == USB_SPEED_FULL) {
3862 start_frame = roundup(start_frame, urb->interval << 3);
3863 urb->start_frame = start_frame >> 3;
3864 } else {
3865 start_frame = roundup(start_frame, urb->interval);
3866 urb->start_frame = start_frame;
3867 }
3868
3869skip_start_over:
Andiry Xub008df62012-03-05 17:49:34 +08003870 ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free;
3871
Dan Carpenter3fc82062012-03-28 10:30:26 +03003872 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
Andiry Xu04e51902010-07-22 15:23:39 -07003873}
3874
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07003875/**** Command Ring Operations ****/
3876
Sarah Sharp913a8a32009-09-04 10:53:13 -07003877/* Generic function for queueing a command TRB on the command ring.
3878 * Check to make sure there's room on the command ring for one command TRB.
3879 * Also check that there's room reserved for commands that must not fail.
3880 * If this is a command that must not fail, meaning command_must_succeed = TRUE,
3881 * then only check for the number of reserved spots.
3882 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
3883 * because the command event handler may want to resubmit a failed command.
3884 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003885static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
3886 u32 field1, u32 field2,
3887 u32 field3, u32 field4, bool command_must_succeed)
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003888{
Sarah Sharp913a8a32009-09-04 10:53:13 -07003889 int reserved_trbs = xhci->cmd_ring_reserved_trbs;
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003890 int ret;
Roger Quadrosad6b1d92015-05-29 17:01:49 +03003891
Mathias Nyman98d74f92016-04-08 16:25:10 +03003892 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
3893 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Roger Quadrosad6b1d92015-05-29 17:01:49 +03003894 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n");
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03003895 return -ESHUTDOWN;
Roger Quadrosad6b1d92015-05-29 17:01:49 +03003896 }
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003897
Sarah Sharp913a8a32009-09-04 10:53:13 -07003898 if (!command_must_succeed)
3899 reserved_trbs++;
3900
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003901 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
Andiry Xu3b72fca2012-03-05 17:49:32 +08003902 reserved_trbs, GFP_ATOMIC);
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003903 if (ret < 0) {
3904 xhci_err(xhci, "ERR: No room for command on command ring\n");
Sarah Sharp913a8a32009-09-04 10:53:13 -07003905 if (command_must_succeed)
3906 xhci_err(xhci, "ERR: Reserved TRB counting for "
3907 "unfailable commands failed.\n");
Sarah Sharpd1dc9082010-07-09 17:08:38 +02003908 return ret;
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003909 }
Mathias Nymanc9aa1a22014-05-08 19:26:01 +03003910
3911 cmd->command_trb = xhci->cmd_ring->enqueue;
3912 list_add_tail(&cmd->cmd_list, &xhci->cmd_list);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003913
Mathias Nymanc311e392014-05-08 19:26:03 +03003914 /* if there are no other commands queued we start the timeout timer */
3915 if (xhci->cmd_list.next == &cmd->cmd_list &&
3916 !timer_pending(&xhci->cmd_timer)) {
3917 xhci->current_cmd = cmd;
3918 mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
3919 }
3920
Andiry Xu3b72fca2012-03-05 17:49:32 +08003921 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
3922 field4 | xhci->cmd_ring->cycle_state);
Sarah Sharp7f84eef2009-04-27 19:53:56 -07003923 return 0;
3924}
3925
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003926/* Queue a slot enable or disable request on the command ring */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003927int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
3928 u32 trb_type, u32 slot_id)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003929{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003930 return queue_command(xhci, cmd, 0, 0, 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003931 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003932}
3933
3934/* Queue an address device command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003935int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
3936 dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003937{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003938 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
Sarah Sharp8e595a52009-07-27 12:03:31 -07003939 upper_32_bits(in_ctx_ptr), 0,
Dan Williams48fc7db2013-12-05 17:07:27 -08003940 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id)
3941 | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003942}
Sarah Sharpf94e01862009-04-27 19:58:38 -07003943
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003944int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
Sarah Sharp02386342010-05-24 13:25:28 -07003945 u32 field1, u32 field2, u32 field3, u32 field4)
3946{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003947 return queue_command(xhci, cmd, field1, field2, field3, field4, false);
Sarah Sharp02386342010-05-24 13:25:28 -07003948}
3949
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003950/* Queue a reset device command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003951int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
3952 u32 slot_id)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003953{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003954 return queue_command(xhci, cmd, 0, 0, 0,
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003955 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
3956 false);
3957}
3958
Sarah Sharpf94e01862009-04-27 19:58:38 -07003959/* Queue a configure endpoint command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003960int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
3961 struct xhci_command *cmd, dma_addr_t in_ctx_ptr,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003962 u32 slot_id, bool command_must_succeed)
Sarah Sharpf94e01862009-04-27 19:58:38 -07003963{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003964 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
Sarah Sharp8e595a52009-07-27 12:03:31 -07003965 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003966 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
3967 command_must_succeed);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003968}
Sarah Sharpae636742009-04-29 19:02:31 -07003969
Sarah Sharpf2217e82009-08-07 14:04:43 -07003970/* Queue an evaluate context command TRB */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003971int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
3972 dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07003973{
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003974 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr),
Sarah Sharpf2217e82009-08-07 14:04:43 -07003975 upper_32_bits(in_ctx_ptr), 0,
Sarah Sharp913a8a32009-09-04 10:53:13 -07003976 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
Sarah Sharp4b266542012-05-07 15:34:26 -07003977 command_must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07003978}
3979
Andiry Xube88fe42010-10-14 07:22:57 -07003980/*
3981 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
3982 * activity on an endpoint that is about to be suspended.
3983 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003984int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
3985 int slot_id, unsigned int ep_index, int suspend)
Sarah Sharpae636742009-04-29 19:02:31 -07003986{
3987 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
3988 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
3989 u32 type = TRB_TYPE(TRB_STOP_RING);
Andiry Xube88fe42010-10-14 07:22:57 -07003990 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
Sarah Sharpae636742009-04-29 19:02:31 -07003991
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003992 return queue_command(xhci, cmd, 0, 0, 0,
Andiry Xube88fe42010-10-14 07:22:57 -07003993 trb_slot_id | trb_ep_index | type | trb_suspend, false);
Sarah Sharpae636742009-04-29 19:02:31 -07003994}
3995
Hans de Goeded3a43e62014-08-20 16:41:53 +03003996/* Set Transfer Ring Dequeue Pointer command */
3997void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
3998 unsigned int slot_id, unsigned int ep_index,
3999 unsigned int stream_id,
4000 struct xhci_dequeue_state *deq_state)
Sarah Sharpae636742009-04-29 19:02:31 -07004001{
4002 dma_addr_t addr;
4003 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4004 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
Sarah Sharpe9df17e2010-04-02 15:34:43 -07004005 u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
Hans de Goede95241db2013-10-04 00:29:48 +02004006 u32 trb_sct = 0;
Sarah Sharpae636742009-04-29 19:02:31 -07004007 u32 type = TRB_TYPE(TRB_SET_DEQ);
Sarah Sharpbf161e82011-02-23 15:46:42 -08004008 struct xhci_virt_ep *ep;
Hans de Goede1e3452e2014-08-20 16:41:52 +03004009 struct xhci_command *cmd;
4010 int ret;
Sarah Sharpae636742009-04-29 19:02:31 -07004011
Hans de Goeded3a43e62014-08-20 16:41:53 +03004012 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
4013 "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), new deq ptr = %p (0x%llx dma), new cycle = %u",
4014 deq_state->new_deq_seg,
4015 (unsigned long long)deq_state->new_deq_seg->dma,
4016 deq_state->new_deq_ptr,
4017 (unsigned long long)xhci_trb_virt_to_dma(
4018 deq_state->new_deq_seg, deq_state->new_deq_ptr),
4019 deq_state->new_cycle_state);
4020
4021 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
4022 deq_state->new_deq_ptr);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07004023 if (addr == 0) {
Sarah Sharpae636742009-04-29 19:02:31 -07004024 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07004025 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
Hans de Goeded3a43e62014-08-20 16:41:53 +03004026 deq_state->new_deq_seg, deq_state->new_deq_ptr);
4027 return;
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07004028 }
Sarah Sharpbf161e82011-02-23 15:46:42 -08004029 ep = &xhci->devs[slot_id]->eps[ep_index];
4030 if ((ep->ep_state & SET_DEQ_PENDING)) {
4031 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
4032 xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
Hans de Goeded3a43e62014-08-20 16:41:53 +03004033 return;
Sarah Sharpbf161e82011-02-23 15:46:42 -08004034 }
Hans de Goede1e3452e2014-08-20 16:41:52 +03004035
4036 /* This function gets called from contexts where it cannot sleep */
4037 cmd = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
4038 if (!cmd) {
4039 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr: ENOMEM\n");
Hans de Goeded3a43e62014-08-20 16:41:53 +03004040 return;
Hans de Goede1e3452e2014-08-20 16:41:52 +03004041 }
4042
Hans de Goeded3a43e62014-08-20 16:41:53 +03004043 ep->queued_deq_seg = deq_state->new_deq_seg;
4044 ep->queued_deq_ptr = deq_state->new_deq_ptr;
Hans de Goede95241db2013-10-04 00:29:48 +02004045 if (stream_id)
4046 trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
Hans de Goede1e3452e2014-08-20 16:41:52 +03004047 ret = queue_command(xhci, cmd,
Hans de Goeded3a43e62014-08-20 16:41:53 +03004048 lower_32_bits(addr) | trb_sct | deq_state->new_cycle_state,
4049 upper_32_bits(addr), trb_stream_id,
4050 trb_slot_id | trb_ep_index | type, false);
Hans de Goede1e3452e2014-08-20 16:41:52 +03004051 if (ret < 0) {
4052 xhci_free_command(xhci, cmd);
Hans de Goeded3a43e62014-08-20 16:41:53 +03004053 return;
Hans de Goede1e3452e2014-08-20 16:41:52 +03004054 }
4055
Hans de Goeded3a43e62014-08-20 16:41:53 +03004056 /* Stop the TD queueing code from ringing the doorbell until
4057 * this command completes. The HC won't set the dequeue pointer
4058 * if the ring is running, and ringing the doorbell starts the
4059 * ring running.
4060 */
4061 ep->ep_state |= SET_DEQ_PENDING;
Sarah Sharpae636742009-04-29 19:02:31 -07004062}
Sarah Sharpa1587d92009-07-27 12:03:15 -07004063
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004064int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
4065 int slot_id, unsigned int ep_index)
Sarah Sharpa1587d92009-07-27 12:03:15 -07004066{
4067 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
4068 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
4069 u32 type = TRB_TYPE(TRB_RESET_EP);
4070
Mathias Nymanddba5cd2014-05-08 19:26:00 +03004071 return queue_command(xhci, cmd, 0, 0, 0,
4072 trb_slot_id | trb_ep_index | type, false);
Sarah Sharpa1587d92009-07-27 12:03:15 -07004073}