Commit | Line | Data |
---|---|---|
7f84eef0 SS |
1 | /* |
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 | ||
8a96c052 | 67 | #include <linux/scatterlist.h> |
5a0e3ad6 | 68 | #include <linux/slab.h> |
7f84eef0 SS |
69 | #include "xhci.h" |
70 | ||
be88fe4f AX |
71 | static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci, |
72 | struct xhci_virt_device *virt_dev, | |
73 | struct xhci_event_cmd *event); | |
74 | ||
7f84eef0 SS |
75 | /* |
76 | * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA | |
77 | * address of the TRB. | |
78 | */ | |
23e3be11 | 79 | dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, |
7f84eef0 SS |
80 | union xhci_trb *trb) |
81 | { | |
6071d836 | 82 | unsigned long segment_offset; |
7f84eef0 | 83 | |
6071d836 | 84 | if (!seg || !trb || trb < seg->trbs) |
7f84eef0 | 85 | return 0; |
6071d836 SS |
86 | /* offset in TRBs */ |
87 | segment_offset = trb - seg->trbs; | |
88 | if (segment_offset > TRBS_PER_SEGMENT) | |
7f84eef0 | 89 | return 0; |
6071d836 | 90 | return seg->dma + (segment_offset * sizeof(*trb)); |
7f84eef0 SS |
91 | } |
92 | ||
93 | /* Does this link TRB point to the first segment in a ring, | |
94 | * or was the previous TRB the last TRB on the last segment in the ERST? | |
95 | */ | |
96 | static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
97 | struct xhci_segment *seg, union xhci_trb *trb) | |
98 | { | |
99 | if (ring == xhci->event_ring) | |
100 | return (trb == &seg->trbs[TRBS_PER_SEGMENT]) && | |
101 | (seg->next == xhci->event_ring->first_seg); | |
102 | else | |
103 | return trb->link.control & LINK_TOGGLE; | |
104 | } | |
105 | ||
106 | /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring | |
107 | * segment? I.e. would the updated event TRB pointer step off the end of the | |
108 | * event seg? | |
109 | */ | |
110 | static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
111 | struct xhci_segment *seg, union xhci_trb *trb) | |
112 | { | |
113 | if (ring == xhci->event_ring) | |
114 | return trb == &seg->trbs[TRBS_PER_SEGMENT]; | |
115 | else | |
116 | return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK); | |
117 | } | |
118 | ||
6c12db90 JY |
119 | static inline int enqueue_is_link_trb(struct xhci_ring *ring) |
120 | { | |
121 | struct xhci_link_trb *link = &ring->enqueue->link; | |
122 | return ((link->control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK)); | |
123 | } | |
124 | ||
ae636747 SS |
125 | /* Updates trb to point to the next TRB in the ring, and updates seg if the next |
126 | * TRB is in a new segment. This does not skip over link TRBs, and it does not | |
127 | * effect the ring dequeue or enqueue pointers. | |
128 | */ | |
129 | static void next_trb(struct xhci_hcd *xhci, | |
130 | struct xhci_ring *ring, | |
131 | struct xhci_segment **seg, | |
132 | union xhci_trb **trb) | |
133 | { | |
134 | if (last_trb(xhci, ring, *seg, *trb)) { | |
135 | *seg = (*seg)->next; | |
136 | *trb = ((*seg)->trbs); | |
137 | } else { | |
a1669b2c | 138 | (*trb)++; |
ae636747 SS |
139 | } |
140 | } | |
141 | ||
7f84eef0 SS |
142 | /* |
143 | * See Cycle bit rules. SW is the consumer for the event ring only. | |
144 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. | |
145 | */ | |
146 | static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer) | |
147 | { | |
148 | union xhci_trb *next = ++(ring->dequeue); | |
66e49d87 | 149 | unsigned long long addr; |
7f84eef0 SS |
150 | |
151 | ring->deq_updates++; | |
152 | /* Update the dequeue pointer further if that was a link TRB or we're at | |
153 | * the end of an event ring segment (which doesn't have link TRBS) | |
154 | */ | |
155 | while (last_trb(xhci, ring, ring->deq_seg, next)) { | |
156 | if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) { | |
157 | ring->cycle_state = (ring->cycle_state ? 0 : 1); | |
158 | if (!in_interrupt()) | |
700e2052 GKH |
159 | xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n", |
160 | ring, | |
7f84eef0 SS |
161 | (unsigned int) ring->cycle_state); |
162 | } | |
163 | ring->deq_seg = ring->deq_seg->next; | |
164 | ring->dequeue = ring->deq_seg->trbs; | |
165 | next = ring->dequeue; | |
166 | } | |
66e49d87 SS |
167 | addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue); |
168 | if (ring == xhci->event_ring) | |
169 | xhci_dbg(xhci, "Event ring deq = 0x%llx (DMA)\n", addr); | |
170 | else if (ring == xhci->cmd_ring) | |
171 | xhci_dbg(xhci, "Command ring deq = 0x%llx (DMA)\n", addr); | |
172 | else | |
173 | xhci_dbg(xhci, "Ring deq = 0x%llx (DMA)\n", addr); | |
7f84eef0 SS |
174 | } |
175 | ||
176 | /* | |
177 | * See Cycle bit rules. SW is the consumer for the event ring only. | |
178 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. | |
179 | * | |
180 | * If we've just enqueued a TRB that is in the middle of a TD (meaning the | |
181 | * chain bit is set), then set the chain bit in all the following link TRBs. | |
182 | * If we've enqueued the last TRB in a TD, make sure the following link TRBs | |
183 | * have their chain bit cleared (so that each Link TRB is a separate TD). | |
184 | * | |
185 | * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit | |
b0567b3f SS |
186 | * set, but other sections talk about dealing with the chain bit set. This was |
187 | * fixed in the 0.96 specification errata, but we have to assume that all 0.95 | |
188 | * xHCI hardware can't handle the chain bit being cleared on a link TRB. | |
6cc30d85 SS |
189 | * |
190 | * @more_trbs_coming: Will you enqueue more TRBs before calling | |
191 | * prepare_transfer()? | |
7f84eef0 | 192 | */ |
6cc30d85 SS |
193 | static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, |
194 | bool consumer, bool more_trbs_coming) | |
7f84eef0 SS |
195 | { |
196 | u32 chain; | |
197 | union xhci_trb *next; | |
66e49d87 | 198 | unsigned long long addr; |
7f84eef0 SS |
199 | |
200 | chain = ring->enqueue->generic.field[3] & TRB_CHAIN; | |
201 | next = ++(ring->enqueue); | |
202 | ||
203 | ring->enq_updates++; | |
204 | /* Update the dequeue pointer further if that was a link TRB or we're at | |
205 | * the end of an event ring segment (which doesn't have link TRBS) | |
206 | */ | |
207 | while (last_trb(xhci, ring, ring->enq_seg, next)) { | |
208 | if (!consumer) { | |
209 | if (ring != xhci->event_ring) { | |
6cc30d85 SS |
210 | /* |
211 | * If the caller doesn't plan on enqueueing more | |
212 | * TDs before ringing the doorbell, then we | |
213 | * don't want to give the link TRB to the | |
214 | * hardware just yet. We'll give the link TRB | |
215 | * back in prepare_ring() just before we enqueue | |
216 | * the TD at the top of the ring. | |
217 | */ | |
218 | if (!chain && !more_trbs_coming) | |
6c12db90 | 219 | break; |
6cc30d85 SS |
220 | |
221 | /* If we're not dealing with 0.95 hardware, | |
222 | * carry over the chain bit of the previous TRB | |
223 | * (which may mean the chain bit is cleared). | |
224 | */ | |
225 | if (!xhci_link_trb_quirk(xhci)) { | |
226 | next->link.control &= ~TRB_CHAIN; | |
227 | next->link.control |= chain; | |
b0567b3f | 228 | } |
6cc30d85 SS |
229 | /* Give this link TRB to the hardware */ |
230 | wmb(); | |
231 | next->link.control ^= TRB_CYCLE; | |
7f84eef0 SS |
232 | } |
233 | /* Toggle the cycle bit after the last ring segment. */ | |
234 | if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { | |
235 | ring->cycle_state = (ring->cycle_state ? 0 : 1); | |
236 | if (!in_interrupt()) | |
700e2052 GKH |
237 | xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n", |
238 | ring, | |
7f84eef0 SS |
239 | (unsigned int) ring->cycle_state); |
240 | } | |
241 | } | |
242 | ring->enq_seg = ring->enq_seg->next; | |
243 | ring->enqueue = ring->enq_seg->trbs; | |
244 | next = ring->enqueue; | |
245 | } | |
66e49d87 SS |
246 | addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue); |
247 | if (ring == xhci->event_ring) | |
248 | xhci_dbg(xhci, "Event ring enq = 0x%llx (DMA)\n", addr); | |
249 | else if (ring == xhci->cmd_ring) | |
250 | xhci_dbg(xhci, "Command ring enq = 0x%llx (DMA)\n", addr); | |
251 | else | |
252 | xhci_dbg(xhci, "Ring enq = 0x%llx (DMA)\n", addr); | |
7f84eef0 SS |
253 | } |
254 | ||
255 | /* | |
256 | * Check to see if there's room to enqueue num_trbs on the ring. See rules | |
257 | * above. | |
258 | * FIXME: this would be simpler and faster if we just kept track of the number | |
259 | * of free TRBs in a ring. | |
260 | */ | |
261 | static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
262 | unsigned int num_trbs) | |
263 | { | |
264 | int i; | |
265 | union xhci_trb *enq = ring->enqueue; | |
266 | struct xhci_segment *enq_seg = ring->enq_seg; | |
44ebd037 SS |
267 | struct xhci_segment *cur_seg; |
268 | unsigned int left_on_ring; | |
7f84eef0 | 269 | |
6c12db90 JY |
270 | /* If we are currently pointing to a link TRB, advance the |
271 | * enqueue pointer before checking for space */ | |
272 | while (last_trb(xhci, ring, enq_seg, enq)) { | |
273 | enq_seg = enq_seg->next; | |
274 | enq = enq_seg->trbs; | |
275 | } | |
276 | ||
7f84eef0 | 277 | /* Check if ring is empty */ |
44ebd037 SS |
278 | if (enq == ring->dequeue) { |
279 | /* Can't use link trbs */ | |
280 | left_on_ring = TRBS_PER_SEGMENT - 1; | |
281 | for (cur_seg = enq_seg->next; cur_seg != enq_seg; | |
282 | cur_seg = cur_seg->next) | |
283 | left_on_ring += TRBS_PER_SEGMENT - 1; | |
284 | ||
285 | /* Always need one TRB free in the ring. */ | |
286 | left_on_ring -= 1; | |
287 | if (num_trbs > left_on_ring) { | |
288 | xhci_warn(xhci, "Not enough room on ring; " | |
289 | "need %u TRBs, %u TRBs left\n", | |
290 | num_trbs, left_on_ring); | |
291 | return 0; | |
292 | } | |
7f84eef0 | 293 | return 1; |
44ebd037 | 294 | } |
7f84eef0 SS |
295 | /* Make sure there's an extra empty TRB available */ |
296 | for (i = 0; i <= num_trbs; ++i) { | |
297 | if (enq == ring->dequeue) | |
298 | return 0; | |
299 | enq++; | |
300 | while (last_trb(xhci, ring, enq_seg, enq)) { | |
301 | enq_seg = enq_seg->next; | |
302 | enq = enq_seg->trbs; | |
303 | } | |
304 | } | |
305 | return 1; | |
306 | } | |
307 | ||
7f84eef0 | 308 | /* Ring the host controller doorbell after placing a command on the ring */ |
23e3be11 | 309 | void xhci_ring_cmd_db(struct xhci_hcd *xhci) |
7f84eef0 | 310 | { |
7f84eef0 | 311 | xhci_dbg(xhci, "// Ding dong!\n"); |
50d64676 | 312 | xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]); |
7f84eef0 SS |
313 | /* Flush PCI posted writes */ |
314 | xhci_readl(xhci, &xhci->dba->doorbell[0]); | |
315 | } | |
316 | ||
be88fe4f | 317 | void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, |
ae636747 | 318 | unsigned int slot_id, |
e9df17eb SS |
319 | unsigned int ep_index, |
320 | unsigned int stream_id) | |
ae636747 | 321 | { |
ae636747 | 322 | __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; |
50d64676 MW |
323 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; |
324 | unsigned int ep_state = ep->ep_state; | |
ae636747 | 325 | |
ae636747 | 326 | /* Don't ring the doorbell for this endpoint if there are pending |
50d64676 | 327 | * cancellations because we don't want to interrupt processing. |
8df75f42 SS |
328 | * We don't want to restart any stream rings if there's a set dequeue |
329 | * pointer command pending because the device can choose to start any | |
330 | * stream once the endpoint is on the HW schedule. | |
331 | * FIXME - check all the stream rings for pending cancellations. | |
ae636747 | 332 | */ |
50d64676 MW |
333 | if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) || |
334 | (ep_state & EP_HALTED)) | |
335 | return; | |
336 | xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr); | |
337 | /* The CPU has better things to do at this point than wait for a | |
338 | * write-posting flush. It'll get there soon enough. | |
339 | */ | |
ae636747 SS |
340 | } |
341 | ||
e9df17eb SS |
342 | /* Ring the doorbell for any rings with pending URBs */ |
343 | static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci, | |
344 | unsigned int slot_id, | |
345 | unsigned int ep_index) | |
346 | { | |
347 | unsigned int stream_id; | |
348 | struct xhci_virt_ep *ep; | |
349 | ||
350 | ep = &xhci->devs[slot_id]->eps[ep_index]; | |
351 | ||
352 | /* A ring has pending URBs if its TD list is not empty */ | |
353 | if (!(ep->ep_state & EP_HAS_STREAMS)) { | |
354 | if (!(list_empty(&ep->ring->td_list))) | |
be88fe4f | 355 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0); |
e9df17eb SS |
356 | return; |
357 | } | |
358 | ||
359 | for (stream_id = 1; stream_id < ep->stream_info->num_streams; | |
360 | stream_id++) { | |
361 | struct xhci_stream_info *stream_info = ep->stream_info; | |
362 | if (!list_empty(&stream_info->stream_rings[stream_id]->td_list)) | |
be88fe4f AX |
363 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, |
364 | stream_id); | |
e9df17eb SS |
365 | } |
366 | } | |
367 | ||
ae636747 SS |
368 | /* |
369 | * Find the segment that trb is in. Start searching in start_seg. | |
370 | * If we must move past a segment that has a link TRB with a toggle cycle state | |
371 | * bit set, then we will toggle the value pointed at by cycle_state. | |
372 | */ | |
373 | static struct xhci_segment *find_trb_seg( | |
374 | struct xhci_segment *start_seg, | |
375 | union xhci_trb *trb, int *cycle_state) | |
376 | { | |
377 | struct xhci_segment *cur_seg = start_seg; | |
378 | struct xhci_generic_trb *generic_trb; | |
379 | ||
380 | while (cur_seg->trbs > trb || | |
381 | &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) { | |
382 | generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic; | |
54b5acf3 AX |
383 | if ((generic_trb->field[3] & TRB_TYPE_BITMASK) == |
384 | TRB_TYPE(TRB_LINK) && | |
ae636747 SS |
385 | (generic_trb->field[3] & LINK_TOGGLE)) |
386 | *cycle_state = ~(*cycle_state) & 0x1; | |
387 | cur_seg = cur_seg->next; | |
388 | if (cur_seg == start_seg) | |
389 | /* Looped over the entire list. Oops! */ | |
326b4810 | 390 | return NULL; |
ae636747 SS |
391 | } |
392 | return cur_seg; | |
393 | } | |
394 | ||
021bff91 SS |
395 | |
396 | static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci, | |
397 | unsigned int slot_id, unsigned int ep_index, | |
398 | unsigned int stream_id) | |
399 | { | |
400 | struct xhci_virt_ep *ep; | |
401 | ||
402 | ep = &xhci->devs[slot_id]->eps[ep_index]; | |
403 | /* Common case: no streams */ | |
404 | if (!(ep->ep_state & EP_HAS_STREAMS)) | |
405 | return ep->ring; | |
406 | ||
407 | if (stream_id == 0) { | |
408 | xhci_warn(xhci, | |
409 | "WARN: Slot ID %u, ep index %u has streams, " | |
410 | "but URB has no stream ID.\n", | |
411 | slot_id, ep_index); | |
412 | return NULL; | |
413 | } | |
414 | ||
415 | if (stream_id < ep->stream_info->num_streams) | |
416 | return ep->stream_info->stream_rings[stream_id]; | |
417 | ||
418 | xhci_warn(xhci, | |
419 | "WARN: Slot ID %u, ep index %u has " | |
420 | "stream IDs 1 to %u allocated, " | |
421 | "but stream ID %u is requested.\n", | |
422 | slot_id, ep_index, | |
423 | ep->stream_info->num_streams - 1, | |
424 | stream_id); | |
425 | return NULL; | |
426 | } | |
427 | ||
428 | /* Get the right ring for the given URB. | |
429 | * If the endpoint supports streams, boundary check the URB's stream ID. | |
430 | * If the endpoint doesn't support streams, return the singular endpoint ring. | |
431 | */ | |
432 | static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci, | |
433 | struct urb *urb) | |
434 | { | |
435 | return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id, | |
436 | xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id); | |
437 | } | |
438 | ||
ae636747 SS |
439 | /* |
440 | * Move the xHC's endpoint ring dequeue pointer past cur_td. | |
441 | * Record the new state of the xHC's endpoint ring dequeue segment, | |
442 | * dequeue pointer, and new consumer cycle state in state. | |
443 | * Update our internal representation of the ring's dequeue pointer. | |
444 | * | |
445 | * We do this in three jumps: | |
446 | * - First we update our new ring state to be the same as when the xHC stopped. | |
447 | * - Then we traverse the ring to find the segment that contains | |
448 | * the last TRB in the TD. We toggle the xHC's new cycle state when we pass | |
449 | * any link TRBs with the toggle cycle bit set. | |
450 | * - Finally we move the dequeue state one TRB further, toggling the cycle bit | |
451 | * if we've moved it past a link TRB with the toggle cycle bit set. | |
452 | */ | |
c92bcfa7 | 453 | void xhci_find_new_dequeue_state(struct xhci_hcd *xhci, |
ae636747 | 454 | unsigned int slot_id, unsigned int ep_index, |
e9df17eb SS |
455 | unsigned int stream_id, struct xhci_td *cur_td, |
456 | struct xhci_dequeue_state *state) | |
ae636747 SS |
457 | { |
458 | struct xhci_virt_device *dev = xhci->devs[slot_id]; | |
e9df17eb | 459 | struct xhci_ring *ep_ring; |
ae636747 | 460 | struct xhci_generic_trb *trb; |
d115b048 | 461 | struct xhci_ep_ctx *ep_ctx; |
c92bcfa7 | 462 | dma_addr_t addr; |
ae636747 | 463 | |
e9df17eb SS |
464 | ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id, |
465 | ep_index, stream_id); | |
466 | if (!ep_ring) { | |
467 | xhci_warn(xhci, "WARN can't find new dequeue state " | |
468 | "for invalid stream ID %u.\n", | |
469 | stream_id); | |
470 | return; | |
471 | } | |
ae636747 | 472 | state->new_cycle_state = 0; |
c92bcfa7 | 473 | xhci_dbg(xhci, "Finding segment containing stopped TRB.\n"); |
ae636747 | 474 | state->new_deq_seg = find_trb_seg(cur_td->start_seg, |
63a0d9ab | 475 | dev->eps[ep_index].stopped_trb, |
ae636747 SS |
476 | &state->new_cycle_state); |
477 | if (!state->new_deq_seg) | |
478 | BUG(); | |
479 | /* Dig out the cycle state saved by the xHC during the stop ep cmd */ | |
c92bcfa7 | 480 | xhci_dbg(xhci, "Finding endpoint context\n"); |
d115b048 JY |
481 | ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index); |
482 | state->new_cycle_state = 0x1 & ep_ctx->deq; | |
ae636747 SS |
483 | |
484 | state->new_deq_ptr = cur_td->last_trb; | |
c92bcfa7 | 485 | xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n"); |
ae636747 SS |
486 | state->new_deq_seg = find_trb_seg(state->new_deq_seg, |
487 | state->new_deq_ptr, | |
488 | &state->new_cycle_state); | |
489 | if (!state->new_deq_seg) | |
490 | BUG(); | |
491 | ||
492 | trb = &state->new_deq_ptr->generic; | |
54b5acf3 | 493 | if ((trb->field[3] & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK) && |
ae636747 SS |
494 | (trb->field[3] & LINK_TOGGLE)) |
495 | state->new_cycle_state = ~(state->new_cycle_state) & 0x1; | |
496 | next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr); | |
497 | ||
01a1fdb9 SS |
498 | /* |
499 | * If there is only one segment in a ring, find_trb_seg()'s while loop | |
500 | * will not run, and it will return before it has a chance to see if it | |
501 | * needs to toggle the cycle bit. It can't tell if the stalled transfer | |
502 | * ended just before the link TRB on a one-segment ring, or if the TD | |
503 | * wrapped around the top of the ring, because it doesn't have the TD in | |
504 | * question. Look for the one-segment case where stalled TRB's address | |
505 | * is greater than the new dequeue pointer address. | |
506 | */ | |
507 | if (ep_ring->first_seg == ep_ring->first_seg->next && | |
508 | state->new_deq_ptr < dev->eps[ep_index].stopped_trb) | |
509 | state->new_cycle_state ^= 0x1; | |
510 | xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state); | |
511 | ||
ae636747 | 512 | /* Don't update the ring cycle state for the producer (us). */ |
c92bcfa7 SS |
513 | xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n", |
514 | state->new_deq_seg); | |
515 | addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr); | |
516 | xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n", | |
517 | (unsigned long long) addr); | |
ae636747 SS |
518 | } |
519 | ||
23e3be11 | 520 | static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, |
ae636747 SS |
521 | struct xhci_td *cur_td) |
522 | { | |
523 | struct xhci_segment *cur_seg; | |
524 | union xhci_trb *cur_trb; | |
525 | ||
526 | for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb; | |
527 | true; | |
528 | next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { | |
529 | if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) == | |
530 | TRB_TYPE(TRB_LINK)) { | |
531 | /* Unchain any chained Link TRBs, but | |
532 | * leave the pointers intact. | |
533 | */ | |
534 | cur_trb->generic.field[3] &= ~TRB_CHAIN; | |
535 | xhci_dbg(xhci, "Cancel (unchain) link TRB\n"); | |
700e2052 GKH |
536 | xhci_dbg(xhci, "Address = %p (0x%llx dma); " |
537 | "in seg %p (0x%llx dma)\n", | |
538 | cur_trb, | |
23e3be11 | 539 | (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb), |
700e2052 GKH |
540 | cur_seg, |
541 | (unsigned long long)cur_seg->dma); | |
ae636747 SS |
542 | } 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 */ | |
547 | cur_trb->generic.field[3] &= TRB_CYCLE; | |
548 | cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP); | |
700e2052 GKH |
549 | xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) " |
550 | "in seg %p (0x%llx dma)\n", | |
551 | cur_trb, | |
23e3be11 | 552 | (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb), |
700e2052 GKH |
553 | cur_seg, |
554 | (unsigned long long)cur_seg->dma); | |
ae636747 SS |
555 | } |
556 | if (cur_trb == cur_td->last_trb) | |
557 | break; | |
558 | } | |
559 | } | |
560 | ||
561 | static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id, | |
e9df17eb SS |
562 | unsigned int ep_index, unsigned int stream_id, |
563 | struct xhci_segment *deq_seg, | |
ae636747 SS |
564 | union xhci_trb *deq_ptr, u32 cycle_state); |
565 | ||
c92bcfa7 | 566 | void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci, |
63a0d9ab | 567 | unsigned int slot_id, unsigned int ep_index, |
e9df17eb | 568 | unsigned int stream_id, |
63a0d9ab | 569 | struct xhci_dequeue_state *deq_state) |
c92bcfa7 | 570 | { |
63a0d9ab SS |
571 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; |
572 | ||
c92bcfa7 SS |
573 | xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), " |
574 | "new deq ptr = %p (0x%llx dma), new cycle = %u\n", | |
575 | deq_state->new_deq_seg, | |
576 | (unsigned long long)deq_state->new_deq_seg->dma, | |
577 | deq_state->new_deq_ptr, | |
578 | (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr), | |
579 | deq_state->new_cycle_state); | |
e9df17eb | 580 | queue_set_tr_deq(xhci, slot_id, ep_index, stream_id, |
c92bcfa7 SS |
581 | deq_state->new_deq_seg, |
582 | deq_state->new_deq_ptr, | |
583 | (u32) deq_state->new_cycle_state); | |
584 | /* Stop the TD queueing code from ringing the doorbell until | |
585 | * this command completes. The HC won't set the dequeue pointer | |
586 | * if the ring is running, and ringing the doorbell starts the | |
587 | * ring running. | |
588 | */ | |
63a0d9ab | 589 | ep->ep_state |= SET_DEQ_PENDING; |
c92bcfa7 SS |
590 | } |
591 | ||
6f5165cf SS |
592 | static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci, |
593 | struct xhci_virt_ep *ep) | |
594 | { | |
595 | ep->ep_state &= ~EP_HALT_PENDING; | |
596 | /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the | |
597 | * timer is running on another CPU, we don't decrement stop_cmds_pending | |
598 | * (since we didn't successfully stop the watchdog timer). | |
599 | */ | |
600 | if (del_timer(&ep->stop_cmd_timer)) | |
601 | ep->stop_cmds_pending--; | |
602 | } | |
603 | ||
604 | /* Must be called with xhci->lock held in interrupt context */ | |
605 | static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, | |
606 | struct xhci_td *cur_td, int status, char *adjective) | |
607 | { | |
214f76f7 | 608 | struct usb_hcd *hcd; |
8e51adcc AX |
609 | struct urb *urb; |
610 | struct urb_priv *urb_priv; | |
6f5165cf | 611 | |
8e51adcc AX |
612 | urb = cur_td->urb; |
613 | urb_priv = urb->hcpriv; | |
614 | urb_priv->td_cnt++; | |
214f76f7 | 615 | hcd = bus_to_hcd(urb->dev->bus); |
6f5165cf | 616 | |
8e51adcc AX |
617 | /* Only giveback urb when this is the last td in urb */ |
618 | if (urb_priv->td_cnt == urb_priv->length) { | |
619 | usb_hcd_unlink_urb_from_ep(hcd, urb); | |
620 | xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, urb); | |
621 | ||
622 | spin_unlock(&xhci->lock); | |
623 | usb_hcd_giveback_urb(hcd, urb, status); | |
624 | xhci_urb_free_priv(xhci, urb_priv); | |
625 | spin_lock(&xhci->lock); | |
626 | xhci_dbg(xhci, "%s URB given back\n", adjective); | |
627 | } | |
6f5165cf SS |
628 | } |
629 | ||
ae636747 SS |
630 | /* |
631 | * When we get a command completion for a Stop Endpoint Command, we need to | |
632 | * unlink any cancelled TDs from the ring. There are two ways to do that: | |
633 | * | |
634 | * 1. If the HW was in the middle of processing the TD that needs to be | |
635 | * cancelled, then we must move the ring's dequeue pointer past the last TRB | |
636 | * in the TD with a Set Dequeue Pointer Command. | |
637 | * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain | |
638 | * bit cleared) so that the HW will skip over them. | |
639 | */ | |
640 | static void handle_stopped_endpoint(struct xhci_hcd *xhci, | |
be88fe4f | 641 | union xhci_trb *trb, struct xhci_event_cmd *event) |
ae636747 SS |
642 | { |
643 | unsigned int slot_id; | |
644 | unsigned int ep_index; | |
be88fe4f | 645 | struct xhci_virt_device *virt_dev; |
ae636747 | 646 | struct xhci_ring *ep_ring; |
63a0d9ab | 647 | struct xhci_virt_ep *ep; |
ae636747 | 648 | struct list_head *entry; |
326b4810 | 649 | struct xhci_td *cur_td = NULL; |
ae636747 SS |
650 | struct xhci_td *last_unlinked_td; |
651 | ||
c92bcfa7 | 652 | struct xhci_dequeue_state deq_state; |
ae636747 | 653 | |
be88fe4f AX |
654 | if (unlikely(TRB_TO_SUSPEND_PORT( |
655 | xhci->cmd_ring->dequeue->generic.field[3]))) { | |
656 | slot_id = TRB_TO_SLOT_ID( | |
657 | xhci->cmd_ring->dequeue->generic.field[3]); | |
658 | virt_dev = xhci->devs[slot_id]; | |
659 | if (virt_dev) | |
660 | handle_cmd_in_cmd_wait_list(xhci, virt_dev, | |
661 | event); | |
662 | else | |
663 | xhci_warn(xhci, "Stop endpoint command " | |
664 | "completion for disabled slot %u\n", | |
665 | slot_id); | |
666 | return; | |
667 | } | |
668 | ||
ae636747 SS |
669 | memset(&deq_state, 0, sizeof(deq_state)); |
670 | slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]); | |
671 | ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]); | |
63a0d9ab | 672 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
ae636747 | 673 | |
678539cf | 674 | if (list_empty(&ep->cancelled_td_list)) { |
6f5165cf | 675 | xhci_stop_watchdog_timer_in_irq(xhci, ep); |
e9df17eb | 676 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
ae636747 | 677 | return; |
678539cf | 678 | } |
ae636747 SS |
679 | |
680 | /* Fix up the ep ring first, so HW stops executing cancelled TDs. | |
681 | * We have the xHCI lock, so nothing can modify this list until we drop | |
682 | * it. We're also in the event handler, so we can't get re-interrupted | |
683 | * if another Stop Endpoint command completes | |
684 | */ | |
63a0d9ab | 685 | list_for_each(entry, &ep->cancelled_td_list) { |
ae636747 | 686 | cur_td = list_entry(entry, struct xhci_td, cancelled_td_list); |
700e2052 GKH |
687 | xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n", |
688 | cur_td->first_trb, | |
23e3be11 | 689 | (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb)); |
e9df17eb SS |
690 | ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb); |
691 | if (!ep_ring) { | |
692 | /* This shouldn't happen unless a driver is mucking | |
693 | * with the stream ID after submission. This will | |
694 | * leave the TD on the hardware ring, and the hardware | |
695 | * will try to execute it, and may access a buffer | |
696 | * that has already been freed. In the best case, the | |
697 | * hardware will execute it, and the event handler will | |
698 | * ignore the completion event for that TD, since it was | |
699 | * removed from the td_list for that endpoint. In | |
700 | * short, don't muck with the stream ID after | |
701 | * submission. | |
702 | */ | |
703 | xhci_warn(xhci, "WARN Cancelled URB %p " | |
704 | "has invalid stream ID %u.\n", | |
705 | cur_td->urb, | |
706 | cur_td->urb->stream_id); | |
707 | goto remove_finished_td; | |
708 | } | |
ae636747 SS |
709 | /* |
710 | * If we stopped on the TD we need to cancel, then we have to | |
711 | * move the xHC endpoint ring dequeue pointer past this TD. | |
712 | */ | |
63a0d9ab | 713 | if (cur_td == ep->stopped_td) |
e9df17eb SS |
714 | xhci_find_new_dequeue_state(xhci, slot_id, ep_index, |
715 | cur_td->urb->stream_id, | |
716 | cur_td, &deq_state); | |
ae636747 SS |
717 | else |
718 | td_to_noop(xhci, ep_ring, cur_td); | |
e9df17eb | 719 | remove_finished_td: |
ae636747 SS |
720 | /* |
721 | * The event handler won't see a completion for this TD anymore, | |
722 | * so remove it from the endpoint ring's TD list. Keep it in | |
723 | * the cancelled TD list for URB completion later. | |
724 | */ | |
725 | list_del(&cur_td->td_list); | |
ae636747 SS |
726 | } |
727 | last_unlinked_td = cur_td; | |
6f5165cf | 728 | xhci_stop_watchdog_timer_in_irq(xhci, ep); |
ae636747 SS |
729 | |
730 | /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */ | |
731 | if (deq_state.new_deq_ptr && deq_state.new_deq_seg) { | |
63a0d9ab | 732 | xhci_queue_new_dequeue_state(xhci, |
e9df17eb SS |
733 | slot_id, ep_index, |
734 | ep->stopped_td->urb->stream_id, | |
735 | &deq_state); | |
ac9d8fe7 | 736 | xhci_ring_cmd_db(xhci); |
ae636747 | 737 | } else { |
e9df17eb SS |
738 | /* Otherwise ring the doorbell(s) to restart queued transfers */ |
739 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); | |
ae636747 | 740 | } |
1624ae1c SS |
741 | ep->stopped_td = NULL; |
742 | ep->stopped_trb = NULL; | |
ae636747 SS |
743 | |
744 | /* | |
745 | * Drop the lock and complete the URBs in the cancelled TD list. | |
746 | * New TDs to be cancelled might be added to the end of the list before | |
747 | * we can complete all the URBs for the TDs we already unlinked. | |
748 | * So stop when we've completed the URB for the last TD we unlinked. | |
749 | */ | |
750 | do { | |
63a0d9ab | 751 | cur_td = list_entry(ep->cancelled_td_list.next, |
ae636747 SS |
752 | struct xhci_td, cancelled_td_list); |
753 | list_del(&cur_td->cancelled_td_list); | |
754 | ||
755 | /* Clean up the cancelled URB */ | |
ae636747 SS |
756 | /* Doesn't matter what we pass for status, since the core will |
757 | * just overwrite it (because the URB has been unlinked). | |
758 | */ | |
6f5165cf | 759 | xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled"); |
ae636747 | 760 | |
6f5165cf SS |
761 | /* Stop processing the cancelled list if the watchdog timer is |
762 | * running. | |
763 | */ | |
764 | if (xhci->xhc_state & XHCI_STATE_DYING) | |
765 | return; | |
ae636747 SS |
766 | } while (cur_td != last_unlinked_td); |
767 | ||
768 | /* Return to the event handler with xhci->lock re-acquired */ | |
769 | } | |
770 | ||
6f5165cf SS |
771 | /* Watchdog timer function for when a stop endpoint command fails to complete. |
772 | * In this case, we assume the host controller is broken or dying or dead. The | |
773 | * host may still be completing some other events, so we have to be careful to | |
774 | * let the event ring handler and the URB dequeueing/enqueueing functions know | |
775 | * through xhci->state. | |
776 | * | |
777 | * The timer may also fire if the host takes a very long time to respond to the | |
778 | * command, and the stop endpoint command completion handler cannot delete the | |
779 | * timer before the timer function is called. Another endpoint cancellation may | |
780 | * sneak in before the timer function can grab the lock, and that may queue | |
781 | * another stop endpoint command and add the timer back. So we cannot use a | |
782 | * simple flag to say whether there is a pending stop endpoint command for a | |
783 | * particular endpoint. | |
784 | * | |
785 | * Instead we use a combination of that flag and a counter for the number of | |
786 | * pending stop endpoint commands. If the timer is the tail end of the last | |
787 | * stop endpoint command, and the endpoint's command is still pending, we assume | |
788 | * the host is dying. | |
789 | */ | |
790 | void xhci_stop_endpoint_command_watchdog(unsigned long arg) | |
791 | { | |
792 | struct xhci_hcd *xhci; | |
793 | struct xhci_virt_ep *ep; | |
794 | struct xhci_virt_ep *temp_ep; | |
795 | struct xhci_ring *ring; | |
796 | struct xhci_td *cur_td; | |
797 | int ret, i, j; | |
798 | ||
799 | ep = (struct xhci_virt_ep *) arg; | |
800 | xhci = ep->xhci; | |
801 | ||
802 | spin_lock(&xhci->lock); | |
803 | ||
804 | ep->stop_cmds_pending--; | |
805 | if (xhci->xhc_state & XHCI_STATE_DYING) { | |
806 | xhci_dbg(xhci, "Stop EP timer ran, but another timer marked " | |
807 | "xHCI as DYING, exiting.\n"); | |
808 | spin_unlock(&xhci->lock); | |
809 | return; | |
810 | } | |
811 | if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) { | |
812 | xhci_dbg(xhci, "Stop EP timer ran, but no command pending, " | |
813 | "exiting.\n"); | |
814 | spin_unlock(&xhci->lock); | |
815 | return; | |
816 | } | |
817 | ||
818 | xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n"); | |
819 | xhci_warn(xhci, "Assuming host is dying, halting host.\n"); | |
820 | /* Oops, HC is dead or dying or at least not responding to the stop | |
821 | * endpoint command. | |
822 | */ | |
823 | xhci->xhc_state |= XHCI_STATE_DYING; | |
824 | /* Disable interrupts from the host controller and start halting it */ | |
825 | xhci_quiesce(xhci); | |
826 | spin_unlock(&xhci->lock); | |
827 | ||
828 | ret = xhci_halt(xhci); | |
829 | ||
830 | spin_lock(&xhci->lock); | |
831 | if (ret < 0) { | |
832 | /* This is bad; the host is not responding to commands and it's | |
833 | * not allowing itself to be halted. At least interrupts are | |
ac04e6ff | 834 | * disabled. If we call usb_hc_died(), it will attempt to |
6f5165cf SS |
835 | * disconnect all device drivers under this host. Those |
836 | * disconnect() methods will wait for all URBs to be unlinked, | |
837 | * so we must complete them. | |
838 | */ | |
839 | xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n"); | |
840 | xhci_warn(xhci, "Completing active URBs anyway.\n"); | |
841 | /* We could turn all TDs on the rings to no-ops. This won't | |
842 | * help if the host has cached part of the ring, and is slow if | |
843 | * we want to preserve the cycle bit. Skip it and hope the host | |
844 | * doesn't touch the memory. | |
845 | */ | |
846 | } | |
847 | for (i = 0; i < MAX_HC_SLOTS; i++) { | |
848 | if (!xhci->devs[i]) | |
849 | continue; | |
850 | for (j = 0; j < 31; j++) { | |
851 | temp_ep = &xhci->devs[i]->eps[j]; | |
852 | ring = temp_ep->ring; | |
853 | if (!ring) | |
854 | continue; | |
855 | xhci_dbg(xhci, "Killing URBs for slot ID %u, " | |
856 | "ep index %u\n", i, j); | |
857 | while (!list_empty(&ring->td_list)) { | |
858 | cur_td = list_first_entry(&ring->td_list, | |
859 | struct xhci_td, | |
860 | td_list); | |
861 | list_del(&cur_td->td_list); | |
862 | if (!list_empty(&cur_td->cancelled_td_list)) | |
863 | list_del(&cur_td->cancelled_td_list); | |
864 | xhci_giveback_urb_in_irq(xhci, cur_td, | |
865 | -ESHUTDOWN, "killed"); | |
866 | } | |
867 | while (!list_empty(&temp_ep->cancelled_td_list)) { | |
868 | cur_td = list_first_entry( | |
869 | &temp_ep->cancelled_td_list, | |
870 | struct xhci_td, | |
871 | cancelled_td_list); | |
872 | list_del(&cur_td->cancelled_td_list); | |
873 | xhci_giveback_urb_in_irq(xhci, cur_td, | |
874 | -ESHUTDOWN, "killed"); | |
875 | } | |
876 | } | |
877 | } | |
878 | spin_unlock(&xhci->lock); | |
6f5165cf | 879 | xhci_dbg(xhci, "Calling usb_hc_died()\n"); |
f6ff0ac8 | 880 | usb_hc_died(xhci_to_hcd(xhci)->primary_hcd); |
6f5165cf SS |
881 | xhci_dbg(xhci, "xHCI host controller is dead.\n"); |
882 | } | |
883 | ||
ae636747 SS |
884 | /* |
885 | * When we get a completion for a Set Transfer Ring Dequeue Pointer command, | |
886 | * we need to clear the set deq pending flag in the endpoint ring state, so that | |
887 | * the TD queueing code can ring the doorbell again. We also need to ring the | |
888 | * endpoint doorbell to restart the ring, but only if there aren't more | |
889 | * cancellations pending. | |
890 | */ | |
891 | static void handle_set_deq_completion(struct xhci_hcd *xhci, | |
892 | struct xhci_event_cmd *event, | |
893 | union xhci_trb *trb) | |
894 | { | |
895 | unsigned int slot_id; | |
896 | unsigned int ep_index; | |
e9df17eb | 897 | unsigned int stream_id; |
ae636747 SS |
898 | struct xhci_ring *ep_ring; |
899 | struct xhci_virt_device *dev; | |
d115b048 JY |
900 | struct xhci_ep_ctx *ep_ctx; |
901 | struct xhci_slot_ctx *slot_ctx; | |
ae636747 SS |
902 | |
903 | slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]); | |
904 | ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]); | |
e9df17eb | 905 | stream_id = TRB_TO_STREAM_ID(trb->generic.field[2]); |
ae636747 | 906 | dev = xhci->devs[slot_id]; |
e9df17eb SS |
907 | |
908 | ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id); | |
909 | if (!ep_ring) { | |
910 | xhci_warn(xhci, "WARN Set TR deq ptr command for " | |
911 | "freed stream ID %u\n", | |
912 | stream_id); | |
913 | /* XXX: Harmless??? */ | |
914 | dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING; | |
915 | return; | |
916 | } | |
917 | ||
d115b048 JY |
918 | ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index); |
919 | slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx); | |
ae636747 SS |
920 | |
921 | if (GET_COMP_CODE(event->status) != COMP_SUCCESS) { | |
922 | unsigned int ep_state; | |
923 | unsigned int slot_state; | |
924 | ||
925 | switch (GET_COMP_CODE(event->status)) { | |
926 | case COMP_TRB_ERR: | |
927 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because " | |
928 | "of stream ID configuration\n"); | |
929 | break; | |
930 | case COMP_CTX_STATE: | |
931 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due " | |
932 | "to incorrect slot or ep state.\n"); | |
d115b048 | 933 | ep_state = ep_ctx->ep_info; |
ae636747 | 934 | ep_state &= EP_STATE_MASK; |
d115b048 | 935 | slot_state = slot_ctx->dev_state; |
ae636747 SS |
936 | slot_state = GET_SLOT_STATE(slot_state); |
937 | xhci_dbg(xhci, "Slot state = %u, EP state = %u\n", | |
938 | slot_state, ep_state); | |
939 | break; | |
940 | case COMP_EBADSLT: | |
941 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because " | |
942 | "slot %u was not enabled.\n", slot_id); | |
943 | break; | |
944 | default: | |
945 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown " | |
946 | "completion code of %u.\n", | |
947 | GET_COMP_CODE(event->status)); | |
948 | break; | |
949 | } | |
950 | /* OK what do we do now? The endpoint state is hosed, and we | |
951 | * should never get to this point if the synchronization between | |
952 | * queueing, and endpoint state are correct. This might happen | |
953 | * if the device gets disconnected after we've finished | |
954 | * cancelling URBs, which might not be an error... | |
955 | */ | |
956 | } else { | |
8e595a5d | 957 | xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n", |
d115b048 | 958 | ep_ctx->deq); |
bf161e85 SS |
959 | if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg, |
960 | dev->eps[ep_index].queued_deq_ptr) == | |
961 | (ep_ctx->deq & ~(EP_CTX_CYCLE_MASK))) { | |
962 | /* Update the ring's dequeue segment and dequeue pointer | |
963 | * to reflect the new position. | |
964 | */ | |
965 | ep_ring->deq_seg = dev->eps[ep_index].queued_deq_seg; | |
966 | ep_ring->dequeue = dev->eps[ep_index].queued_deq_ptr; | |
967 | } else { | |
968 | xhci_warn(xhci, "Mismatch between completed Set TR Deq " | |
969 | "Ptr command & xHCI internal state.\n"); | |
970 | xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", | |
971 | dev->eps[ep_index].queued_deq_seg, | |
972 | dev->eps[ep_index].queued_deq_ptr); | |
973 | } | |
ae636747 SS |
974 | } |
975 | ||
63a0d9ab | 976 | dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING; |
bf161e85 SS |
977 | dev->eps[ep_index].queued_deq_seg = NULL; |
978 | dev->eps[ep_index].queued_deq_ptr = NULL; | |
e9df17eb SS |
979 | /* Restart any rings with pending URBs */ |
980 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); | |
ae636747 SS |
981 | } |
982 | ||
a1587d97 SS |
983 | static void handle_reset_ep_completion(struct xhci_hcd *xhci, |
984 | struct xhci_event_cmd *event, | |
985 | union xhci_trb *trb) | |
986 | { | |
987 | int slot_id; | |
988 | unsigned int ep_index; | |
989 | ||
990 | slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]); | |
991 | ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]); | |
992 | /* This command will only fail if the endpoint wasn't halted, | |
993 | * but we don't care. | |
994 | */ | |
995 | xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n", | |
996 | (unsigned int) GET_COMP_CODE(event->status)); | |
997 | ||
ac9d8fe7 SS |
998 | /* HW with the reset endpoint quirk needs to have a configure endpoint |
999 | * command complete before the endpoint can be used. Queue that here | |
1000 | * because the HW can't handle two commands being queued in a row. | |
1001 | */ | |
1002 | if (xhci->quirks & XHCI_RESET_EP_QUIRK) { | |
1003 | xhci_dbg(xhci, "Queueing configure endpoint command\n"); | |
1004 | xhci_queue_configure_endpoint(xhci, | |
913a8a34 SS |
1005 | xhci->devs[slot_id]->in_ctx->dma, slot_id, |
1006 | false); | |
ac9d8fe7 SS |
1007 | xhci_ring_cmd_db(xhci); |
1008 | } else { | |
e9df17eb | 1009 | /* Clear our internal halted state and restart the ring(s) */ |
63a0d9ab | 1010 | xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED; |
e9df17eb | 1011 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
ac9d8fe7 | 1012 | } |
a1587d97 | 1013 | } |
ae636747 | 1014 | |
a50c8aa9 SS |
1015 | /* Check to see if a command in the device's command queue matches this one. |
1016 | * Signal the completion or free the command, and return 1. Return 0 if the | |
1017 | * completed command isn't at the head of the command list. | |
1018 | */ | |
1019 | static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci, | |
1020 | struct xhci_virt_device *virt_dev, | |
1021 | struct xhci_event_cmd *event) | |
1022 | { | |
1023 | struct xhci_command *command; | |
1024 | ||
1025 | if (list_empty(&virt_dev->cmd_list)) | |
1026 | return 0; | |
1027 | ||
1028 | command = list_entry(virt_dev->cmd_list.next, | |
1029 | struct xhci_command, cmd_list); | |
1030 | if (xhci->cmd_ring->dequeue != command->command_trb) | |
1031 | return 0; | |
1032 | ||
1033 | command->status = | |
1034 | GET_COMP_CODE(event->status); | |
1035 | list_del(&command->cmd_list); | |
1036 | if (command->completion) | |
1037 | complete(command->completion); | |
1038 | else | |
1039 | xhci_free_command(xhci, command); | |
1040 | return 1; | |
1041 | } | |
1042 | ||
7f84eef0 SS |
1043 | static void handle_cmd_completion(struct xhci_hcd *xhci, |
1044 | struct xhci_event_cmd *event) | |
1045 | { | |
3ffbba95 | 1046 | int slot_id = TRB_TO_SLOT_ID(event->flags); |
7f84eef0 SS |
1047 | u64 cmd_dma; |
1048 | dma_addr_t cmd_dequeue_dma; | |
ac9d8fe7 | 1049 | struct xhci_input_control_ctx *ctrl_ctx; |
913a8a34 | 1050 | struct xhci_virt_device *virt_dev; |
ac9d8fe7 SS |
1051 | unsigned int ep_index; |
1052 | struct xhci_ring *ep_ring; | |
1053 | unsigned int ep_state; | |
7f84eef0 | 1054 | |
8e595a5d | 1055 | cmd_dma = event->cmd_trb; |
23e3be11 | 1056 | cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, |
7f84eef0 SS |
1057 | xhci->cmd_ring->dequeue); |
1058 | /* Is the command ring deq ptr out of sync with the deq seg ptr? */ | |
1059 | if (cmd_dequeue_dma == 0) { | |
1060 | xhci->error_bitmask |= 1 << 4; | |
1061 | return; | |
1062 | } | |
1063 | /* Does the DMA address match our internal dequeue pointer address? */ | |
1064 | if (cmd_dma != (u64) cmd_dequeue_dma) { | |
1065 | xhci->error_bitmask |= 1 << 5; | |
1066 | return; | |
1067 | } | |
1068 | switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) { | |
3ffbba95 SS |
1069 | case TRB_TYPE(TRB_ENABLE_SLOT): |
1070 | if (GET_COMP_CODE(event->status) == COMP_SUCCESS) | |
1071 | xhci->slot_id = slot_id; | |
1072 | else | |
1073 | xhci->slot_id = 0; | |
1074 | complete(&xhci->addr_dev); | |
1075 | break; | |
1076 | case TRB_TYPE(TRB_DISABLE_SLOT): | |
1077 | if (xhci->devs[slot_id]) | |
1078 | xhci_free_virt_device(xhci, slot_id); | |
1079 | break; | |
f94e0186 | 1080 | case TRB_TYPE(TRB_CONFIG_EP): |
913a8a34 | 1081 | virt_dev = xhci->devs[slot_id]; |
a50c8aa9 | 1082 | if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event)) |
913a8a34 | 1083 | break; |
ac9d8fe7 SS |
1084 | /* |
1085 | * Configure endpoint commands can come from the USB core | |
1086 | * configuration or alt setting changes, or because the HW | |
1087 | * needed an extra configure endpoint command after a reset | |
8df75f42 SS |
1088 | * endpoint command or streams were being configured. |
1089 | * If the command was for a halted endpoint, the xHCI driver | |
1090 | * is not waiting on the configure endpoint command. | |
ac9d8fe7 SS |
1091 | */ |
1092 | ctrl_ctx = xhci_get_input_control_ctx(xhci, | |
913a8a34 | 1093 | virt_dev->in_ctx); |
ac9d8fe7 SS |
1094 | /* Input ctx add_flags are the endpoint index plus one */ |
1095 | ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1; | |
06df5729 | 1096 | /* A usb_set_interface() call directly after clearing a halted |
e9df17eb SS |
1097 | * condition may race on this quirky hardware. Not worth |
1098 | * worrying about, since this is prototype hardware. Not sure | |
1099 | * if this will work for streams, but streams support was | |
1100 | * untested on this prototype. | |
06df5729 | 1101 | */ |
ac9d8fe7 | 1102 | if (xhci->quirks & XHCI_RESET_EP_QUIRK && |
06df5729 SS |
1103 | ep_index != (unsigned int) -1 && |
1104 | ctrl_ctx->add_flags - SLOT_FLAG == | |
1105 | ctrl_ctx->drop_flags) { | |
1106 | ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; | |
1107 | ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; | |
1108 | if (!(ep_state & EP_HALTED)) | |
1109 | goto bandwidth_change; | |
1110 | xhci_dbg(xhci, "Completed config ep cmd - " | |
1111 | "last ep index = %d, state = %d\n", | |
1112 | ep_index, ep_state); | |
e9df17eb | 1113 | /* Clear internal halted state and restart ring(s) */ |
63a0d9ab | 1114 | xhci->devs[slot_id]->eps[ep_index].ep_state &= |
ac9d8fe7 | 1115 | ~EP_HALTED; |
e9df17eb | 1116 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
06df5729 | 1117 | break; |
ac9d8fe7 | 1118 | } |
06df5729 SS |
1119 | bandwidth_change: |
1120 | xhci_dbg(xhci, "Completed config ep cmd\n"); | |
1121 | xhci->devs[slot_id]->cmd_status = | |
1122 | GET_COMP_CODE(event->status); | |
1123 | complete(&xhci->devs[slot_id]->cmd_completion); | |
f94e0186 | 1124 | break; |
2d3f1fac | 1125 | case TRB_TYPE(TRB_EVAL_CONTEXT): |
ac1c1b7f SS |
1126 | virt_dev = xhci->devs[slot_id]; |
1127 | if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event)) | |
1128 | break; | |
2d3f1fac SS |
1129 | xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status); |
1130 | complete(&xhci->devs[slot_id]->cmd_completion); | |
1131 | break; | |
3ffbba95 SS |
1132 | case TRB_TYPE(TRB_ADDR_DEV): |
1133 | xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status); | |
1134 | complete(&xhci->addr_dev); | |
1135 | break; | |
ae636747 | 1136 | case TRB_TYPE(TRB_STOP_RING): |
be88fe4f | 1137 | handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event); |
ae636747 SS |
1138 | break; |
1139 | case TRB_TYPE(TRB_SET_DEQ): | |
1140 | handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue); | |
1141 | break; | |
7f84eef0 | 1142 | case TRB_TYPE(TRB_CMD_NOOP): |
7f84eef0 | 1143 | break; |
a1587d97 SS |
1144 | case TRB_TYPE(TRB_RESET_EP): |
1145 | handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue); | |
1146 | break; | |
2a8f82c4 SS |
1147 | case TRB_TYPE(TRB_RESET_DEV): |
1148 | xhci_dbg(xhci, "Completed reset device command.\n"); | |
1149 | slot_id = TRB_TO_SLOT_ID( | |
1150 | xhci->cmd_ring->dequeue->generic.field[3]); | |
1151 | virt_dev = xhci->devs[slot_id]; | |
1152 | if (virt_dev) | |
1153 | handle_cmd_in_cmd_wait_list(xhci, virt_dev, event); | |
1154 | else | |
1155 | xhci_warn(xhci, "Reset device command completion " | |
1156 | "for disabled slot %u\n", slot_id); | |
1157 | break; | |
0238634d SS |
1158 | case TRB_TYPE(TRB_NEC_GET_FW): |
1159 | if (!(xhci->quirks & XHCI_NEC_HOST)) { | |
1160 | xhci->error_bitmask |= 1 << 6; | |
1161 | break; | |
1162 | } | |
1163 | xhci_dbg(xhci, "NEC firmware version %2x.%02x\n", | |
1164 | NEC_FW_MAJOR(event->status), | |
1165 | NEC_FW_MINOR(event->status)); | |
1166 | break; | |
7f84eef0 SS |
1167 | default: |
1168 | /* Skip over unknown commands on the event ring */ | |
1169 | xhci->error_bitmask |= 1 << 6; | |
1170 | break; | |
1171 | } | |
1172 | inc_deq(xhci, xhci->cmd_ring, false); | |
1173 | } | |
1174 | ||
0238634d SS |
1175 | static void handle_vendor_event(struct xhci_hcd *xhci, |
1176 | union xhci_trb *event) | |
1177 | { | |
1178 | u32 trb_type; | |
1179 | ||
1180 | trb_type = TRB_FIELD_TO_TYPE(event->generic.field[3]); | |
1181 | xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type); | |
1182 | if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST)) | |
1183 | handle_cmd_completion(xhci, &event->event_cmd); | |
1184 | } | |
1185 | ||
f6ff0ac8 SS |
1186 | /* @port_id: the one-based port ID from the hardware (indexed from array of all |
1187 | * port registers -- USB 3.0 and USB 2.0). | |
1188 | * | |
1189 | * Returns a zero-based port number, which is suitable for indexing into each of | |
1190 | * the split roothubs' port arrays and bus state arrays. | |
1191 | */ | |
1192 | static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd, | |
1193 | struct xhci_hcd *xhci, u32 port_id) | |
1194 | { | |
1195 | unsigned int i; | |
1196 | unsigned int num_similar_speed_ports = 0; | |
1197 | ||
1198 | /* port_id from the hardware is 1-based, but port_array[], usb3_ports[], | |
1199 | * and usb2_ports are 0-based indexes. Count the number of similar | |
1200 | * speed ports, up to 1 port before this port. | |
1201 | */ | |
1202 | for (i = 0; i < (port_id - 1); i++) { | |
1203 | u8 port_speed = xhci->port_array[i]; | |
1204 | ||
1205 | /* | |
1206 | * Skip ports that don't have known speeds, or have duplicate | |
1207 | * Extended Capabilities port speed entries. | |
1208 | */ | |
1209 | if (port_speed == 0 || port_speed == -1) | |
1210 | continue; | |
1211 | ||
1212 | /* | |
1213 | * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and | |
1214 | * 1.1 ports are under the USB 2.0 hub. If the port speed | |
1215 | * matches the device speed, it's a similar speed port. | |
1216 | */ | |
1217 | if ((port_speed == 0x03) == (hcd->speed == HCD_USB3)) | |
1218 | num_similar_speed_ports++; | |
1219 | } | |
1220 | return num_similar_speed_ports; | |
1221 | } | |
1222 | ||
0f2a7930 SS |
1223 | static void handle_port_status(struct xhci_hcd *xhci, |
1224 | union xhci_trb *event) | |
1225 | { | |
f6ff0ac8 | 1226 | struct usb_hcd *hcd; |
0f2a7930 | 1227 | u32 port_id; |
56192531 | 1228 | u32 temp, temp1; |
518e848e | 1229 | int max_ports; |
56192531 | 1230 | int slot_id; |
5308a91b | 1231 | unsigned int faked_port_index; |
f6ff0ac8 | 1232 | u8 major_revision; |
20b67cf5 | 1233 | struct xhci_bus_state *bus_state; |
f6ff0ac8 | 1234 | u32 __iomem **port_array; |
0f2a7930 SS |
1235 | |
1236 | /* Port status change events always have a successful completion code */ | |
1237 | if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) { | |
1238 | xhci_warn(xhci, "WARN: xHC returned failed port status event\n"); | |
1239 | xhci->error_bitmask |= 1 << 8; | |
1240 | } | |
0f2a7930 SS |
1241 | port_id = GET_PORT_ID(event->generic.field[0]); |
1242 | xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id); | |
1243 | ||
518e848e SS |
1244 | max_ports = HCS_MAX_PORTS(xhci->hcs_params1); |
1245 | if ((port_id <= 0) || (port_id > max_ports)) { | |
56192531 AX |
1246 | xhci_warn(xhci, "Invalid port id %d\n", port_id); |
1247 | goto cleanup; | |
1248 | } | |
1249 | ||
f6ff0ac8 SS |
1250 | /* Figure out which usb_hcd this port is attached to: |
1251 | * is it a USB 3.0 port or a USB 2.0/1.1 port? | |
1252 | */ | |
1253 | major_revision = xhci->port_array[port_id - 1]; | |
1254 | if (major_revision == 0) { | |
1255 | xhci_warn(xhci, "Event for port %u not in " | |
1256 | "Extended Capabilities, ignoring.\n", | |
1257 | port_id); | |
1258 | goto cleanup; | |
5308a91b | 1259 | } |
f6ff0ac8 SS |
1260 | if (major_revision == (u8) -1) { |
1261 | xhci_warn(xhci, "Event for port %u duplicated in" | |
1262 | "Extended Capabilities, ignoring.\n", | |
1263 | port_id); | |
1264 | goto cleanup; | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * Hardware port IDs reported by a Port Status Change Event include USB | |
1269 | * 3.0 and USB 2.0 ports. We want to check if the port has reported a | |
1270 | * resume event, but we first need to translate the hardware port ID | |
1271 | * into the index into the ports on the correct split roothub, and the | |
1272 | * correct bus_state structure. | |
1273 | */ | |
1274 | /* Find the right roothub. */ | |
1275 | hcd = xhci_to_hcd(xhci); | |
1276 | if ((major_revision == 0x03) != (hcd->speed == HCD_USB3)) | |
1277 | hcd = xhci->shared_hcd; | |
1278 | bus_state = &xhci->bus_state[hcd_index(hcd)]; | |
1279 | if (hcd->speed == HCD_USB3) | |
1280 | port_array = xhci->usb3_ports; | |
1281 | else | |
1282 | port_array = xhci->usb2_ports; | |
1283 | /* Find the faked port hub number */ | |
1284 | faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci, | |
1285 | port_id); | |
5308a91b | 1286 | |
5308a91b | 1287 | temp = xhci_readl(xhci, port_array[faked_port_index]); |
7111ebc9 | 1288 | if (hcd->state == HC_STATE_SUSPENDED) { |
56192531 AX |
1289 | xhci_dbg(xhci, "resume root hub\n"); |
1290 | usb_hcd_resume_root_hub(hcd); | |
1291 | } | |
1292 | ||
1293 | if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) { | |
1294 | xhci_dbg(xhci, "port resume event for port %d\n", port_id); | |
1295 | ||
1296 | temp1 = xhci_readl(xhci, &xhci->op_regs->command); | |
1297 | if (!(temp1 & CMD_RUN)) { | |
1298 | xhci_warn(xhci, "xHC is not running.\n"); | |
1299 | goto cleanup; | |
1300 | } | |
1301 | ||
1302 | if (DEV_SUPERSPEED(temp)) { | |
1303 | xhci_dbg(xhci, "resume SS port %d\n", port_id); | |
1304 | temp = xhci_port_state_to_neutral(temp); | |
1305 | temp &= ~PORT_PLS_MASK; | |
1306 | temp |= PORT_LINK_STROBE | XDEV_U0; | |
5308a91b | 1307 | xhci_writel(xhci, temp, port_array[faked_port_index]); |
5233630f SS |
1308 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
1309 | faked_port_index); | |
56192531 AX |
1310 | if (!slot_id) { |
1311 | xhci_dbg(xhci, "slot_id is zero\n"); | |
1312 | goto cleanup; | |
1313 | } | |
1314 | xhci_ring_device(xhci, slot_id); | |
1315 | xhci_dbg(xhci, "resume SS port %d finished\n", port_id); | |
1316 | /* Clear PORT_PLC */ | |
5308a91b | 1317 | temp = xhci_readl(xhci, port_array[faked_port_index]); |
56192531 AX |
1318 | temp = xhci_port_state_to_neutral(temp); |
1319 | temp |= PORT_PLC; | |
5308a91b | 1320 | xhci_writel(xhci, temp, port_array[faked_port_index]); |
56192531 AX |
1321 | } else { |
1322 | xhci_dbg(xhci, "resume HS port %d\n", port_id); | |
f6ff0ac8 | 1323 | bus_state->resume_done[faked_port_index] = jiffies + |
56192531 AX |
1324 | msecs_to_jiffies(20); |
1325 | mod_timer(&hcd->rh_timer, | |
f6ff0ac8 | 1326 | bus_state->resume_done[faked_port_index]); |
56192531 AX |
1327 | /* Do the rest in GetPortStatus */ |
1328 | } | |
1329 | } | |
1330 | ||
1331 | cleanup: | |
0f2a7930 SS |
1332 | /* Update event ring dequeue pointer before dropping the lock */ |
1333 | inc_deq(xhci, xhci->event_ring, true); | |
0f2a7930 SS |
1334 | |
1335 | spin_unlock(&xhci->lock); | |
1336 | /* Pass this up to the core */ | |
f6ff0ac8 | 1337 | usb_hcd_poll_rh_status(hcd); |
0f2a7930 SS |
1338 | spin_lock(&xhci->lock); |
1339 | } | |
1340 | ||
d0e96f5a SS |
1341 | /* |
1342 | * This TD is defined by the TRBs starting at start_trb in start_seg and ending | |
1343 | * at end_trb, which may be in another segment. If the suspect DMA address is a | |
1344 | * TRB in this TD, this function returns that TRB's segment. Otherwise it | |
1345 | * returns 0. | |
1346 | */ | |
6648f29d | 1347 | struct xhci_segment *trb_in_td(struct xhci_segment *start_seg, |
d0e96f5a SS |
1348 | union xhci_trb *start_trb, |
1349 | union xhci_trb *end_trb, | |
1350 | dma_addr_t suspect_dma) | |
1351 | { | |
1352 | dma_addr_t start_dma; | |
1353 | dma_addr_t end_seg_dma; | |
1354 | dma_addr_t end_trb_dma; | |
1355 | struct xhci_segment *cur_seg; | |
1356 | ||
23e3be11 | 1357 | start_dma = xhci_trb_virt_to_dma(start_seg, start_trb); |
d0e96f5a SS |
1358 | cur_seg = start_seg; |
1359 | ||
1360 | do { | |
2fa88daa | 1361 | if (start_dma == 0) |
326b4810 | 1362 | return NULL; |
ae636747 | 1363 | /* We may get an event for a Link TRB in the middle of a TD */ |
23e3be11 | 1364 | end_seg_dma = xhci_trb_virt_to_dma(cur_seg, |
2fa88daa | 1365 | &cur_seg->trbs[TRBS_PER_SEGMENT - 1]); |
d0e96f5a | 1366 | /* If the end TRB isn't in this segment, this is set to 0 */ |
23e3be11 | 1367 | end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb); |
d0e96f5a SS |
1368 | |
1369 | if (end_trb_dma > 0) { | |
1370 | /* The end TRB is in this segment, so suspect should be here */ | |
1371 | if (start_dma <= end_trb_dma) { | |
1372 | if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma) | |
1373 | return cur_seg; | |
1374 | } else { | |
1375 | /* Case for one segment with | |
1376 | * a TD wrapped around to the top | |
1377 | */ | |
1378 | if ((suspect_dma >= start_dma && | |
1379 | suspect_dma <= end_seg_dma) || | |
1380 | (suspect_dma >= cur_seg->dma && | |
1381 | suspect_dma <= end_trb_dma)) | |
1382 | return cur_seg; | |
1383 | } | |
326b4810 | 1384 | return NULL; |
d0e96f5a SS |
1385 | } else { |
1386 | /* Might still be somewhere in this segment */ | |
1387 | if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma) | |
1388 | return cur_seg; | |
1389 | } | |
1390 | cur_seg = cur_seg->next; | |
23e3be11 | 1391 | start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); |
2fa88daa | 1392 | } while (cur_seg != start_seg); |
d0e96f5a | 1393 | |
326b4810 | 1394 | return NULL; |
d0e96f5a SS |
1395 | } |
1396 | ||
bcef3fd5 SS |
1397 | static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci, |
1398 | unsigned int slot_id, unsigned int ep_index, | |
e9df17eb | 1399 | unsigned int stream_id, |
bcef3fd5 SS |
1400 | struct xhci_td *td, union xhci_trb *event_trb) |
1401 | { | |
1402 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; | |
1403 | ep->ep_state |= EP_HALTED; | |
1404 | ep->stopped_td = td; | |
1405 | ep->stopped_trb = event_trb; | |
e9df17eb | 1406 | ep->stopped_stream = stream_id; |
1624ae1c | 1407 | |
bcef3fd5 SS |
1408 | xhci_queue_reset_ep(xhci, slot_id, ep_index); |
1409 | xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index); | |
1624ae1c SS |
1410 | |
1411 | ep->stopped_td = NULL; | |
1412 | ep->stopped_trb = NULL; | |
5e5cf6fc | 1413 | ep->stopped_stream = 0; |
1624ae1c | 1414 | |
bcef3fd5 SS |
1415 | xhci_ring_cmd_db(xhci); |
1416 | } | |
1417 | ||
1418 | /* Check if an error has halted the endpoint ring. The class driver will | |
1419 | * cleanup the halt for a non-default control endpoint if we indicate a stall. | |
1420 | * However, a babble and other errors also halt the endpoint ring, and the class | |
1421 | * driver won't clear the halt in that case, so we need to issue a Set Transfer | |
1422 | * Ring Dequeue Pointer command manually. | |
1423 | */ | |
1424 | static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci, | |
1425 | struct xhci_ep_ctx *ep_ctx, | |
1426 | unsigned int trb_comp_code) | |
1427 | { | |
1428 | /* TRB completion codes that may require a manual halt cleanup */ | |
1429 | if (trb_comp_code == COMP_TX_ERR || | |
1430 | trb_comp_code == COMP_BABBLE || | |
1431 | trb_comp_code == COMP_SPLIT_ERR) | |
1432 | /* The 0.96 spec says a babbling control endpoint | |
1433 | * is not halted. The 0.96 spec says it is. Some HW | |
1434 | * claims to be 0.95 compliant, but it halts the control | |
1435 | * endpoint anyway. Check if a babble halted the | |
1436 | * endpoint. | |
1437 | */ | |
1438 | if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED) | |
1439 | return 1; | |
1440 | ||
1441 | return 0; | |
1442 | } | |
1443 | ||
b45b5069 SS |
1444 | int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code) |
1445 | { | |
1446 | if (trb_comp_code >= 224 && trb_comp_code <= 255) { | |
1447 | /* Vendor defined "informational" completion code, | |
1448 | * treat as not-an-error. | |
1449 | */ | |
1450 | xhci_dbg(xhci, "Vendor defined info completion code %u\n", | |
1451 | trb_comp_code); | |
1452 | xhci_dbg(xhci, "Treating code as success.\n"); | |
1453 | return 1; | |
1454 | } | |
1455 | return 0; | |
1456 | } | |
1457 | ||
4422da61 AX |
1458 | /* |
1459 | * Finish the td processing, remove the td from td list; | |
1460 | * Return 1 if the urb can be given back. | |
1461 | */ | |
1462 | static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td, | |
1463 | union xhci_trb *event_trb, struct xhci_transfer_event *event, | |
1464 | struct xhci_virt_ep *ep, int *status, bool skip) | |
1465 | { | |
1466 | struct xhci_virt_device *xdev; | |
1467 | struct xhci_ring *ep_ring; | |
1468 | unsigned int slot_id; | |
1469 | int ep_index; | |
1470 | struct urb *urb = NULL; | |
1471 | struct xhci_ep_ctx *ep_ctx; | |
1472 | int ret = 0; | |
8e51adcc | 1473 | struct urb_priv *urb_priv; |
4422da61 AX |
1474 | u32 trb_comp_code; |
1475 | ||
1476 | slot_id = TRB_TO_SLOT_ID(event->flags); | |
1477 | xdev = xhci->devs[slot_id]; | |
1478 | ep_index = TRB_TO_EP_ID(event->flags) - 1; | |
1479 | ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer); | |
1480 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); | |
1481 | trb_comp_code = GET_COMP_CODE(event->transfer_len); | |
1482 | ||
1483 | if (skip) | |
1484 | goto td_cleanup; | |
1485 | ||
1486 | if (trb_comp_code == COMP_STOP_INVAL || | |
1487 | trb_comp_code == COMP_STOP) { | |
1488 | /* The Endpoint Stop Command completion will take care of any | |
1489 | * stopped TDs. A stopped TD may be restarted, so don't update | |
1490 | * the ring dequeue pointer or take this TD off any lists yet. | |
1491 | */ | |
1492 | ep->stopped_td = td; | |
1493 | ep->stopped_trb = event_trb; | |
1494 | return 0; | |
1495 | } else { | |
1496 | if (trb_comp_code == COMP_STALL) { | |
1497 | /* The transfer is completed from the driver's | |
1498 | * perspective, but we need to issue a set dequeue | |
1499 | * command for this stalled endpoint to move the dequeue | |
1500 | * pointer past the TD. We can't do that here because | |
1501 | * the halt condition must be cleared first. Let the | |
1502 | * USB class driver clear the stall later. | |
1503 | */ | |
1504 | ep->stopped_td = td; | |
1505 | ep->stopped_trb = event_trb; | |
1506 | ep->stopped_stream = ep_ring->stream_id; | |
1507 | } else if (xhci_requires_manual_halt_cleanup(xhci, | |
1508 | ep_ctx, trb_comp_code)) { | |
1509 | /* Other types of errors halt the endpoint, but the | |
1510 | * class driver doesn't call usb_reset_endpoint() unless | |
1511 | * the error is -EPIPE. Clear the halted status in the | |
1512 | * xHCI hardware manually. | |
1513 | */ | |
1514 | xhci_cleanup_halted_endpoint(xhci, | |
1515 | slot_id, ep_index, ep_ring->stream_id, | |
1516 | td, event_trb); | |
1517 | } else { | |
1518 | /* Update ring dequeue pointer */ | |
1519 | while (ep_ring->dequeue != td->last_trb) | |
1520 | inc_deq(xhci, ep_ring, false); | |
1521 | inc_deq(xhci, ep_ring, false); | |
1522 | } | |
1523 | ||
1524 | td_cleanup: | |
1525 | /* Clean up the endpoint's TD list */ | |
1526 | urb = td->urb; | |
8e51adcc | 1527 | urb_priv = urb->hcpriv; |
4422da61 AX |
1528 | |
1529 | /* Do one last check of the actual transfer length. | |
1530 | * If the host controller said we transferred more data than | |
1531 | * the buffer length, urb->actual_length will be a very big | |
1532 | * number (since it's unsigned). Play it safe and say we didn't | |
1533 | * transfer anything. | |
1534 | */ | |
1535 | if (urb->actual_length > urb->transfer_buffer_length) { | |
1536 | xhci_warn(xhci, "URB transfer length is wrong, " | |
1537 | "xHC issue? req. len = %u, " | |
1538 | "act. len = %u\n", | |
1539 | urb->transfer_buffer_length, | |
1540 | urb->actual_length); | |
1541 | urb->actual_length = 0; | |
1542 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1543 | *status = -EREMOTEIO; | |
1544 | else | |
1545 | *status = 0; | |
1546 | } | |
1547 | list_del(&td->td_list); | |
1548 | /* Was this TD slated to be cancelled but completed anyway? */ | |
1549 | if (!list_empty(&td->cancelled_td_list)) | |
1550 | list_del(&td->cancelled_td_list); | |
1551 | ||
8e51adcc AX |
1552 | urb_priv->td_cnt++; |
1553 | /* Giveback the urb when all the tds are completed */ | |
1554 | if (urb_priv->td_cnt == urb_priv->length) | |
1555 | ret = 1; | |
4422da61 AX |
1556 | } |
1557 | ||
1558 | return ret; | |
1559 | } | |
1560 | ||
8af56be1 AX |
1561 | /* |
1562 | * Process control tds, update urb status and actual_length. | |
1563 | */ | |
1564 | static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td, | |
1565 | union xhci_trb *event_trb, struct xhci_transfer_event *event, | |
1566 | struct xhci_virt_ep *ep, int *status) | |
1567 | { | |
1568 | struct xhci_virt_device *xdev; | |
1569 | struct xhci_ring *ep_ring; | |
1570 | unsigned int slot_id; | |
1571 | int ep_index; | |
1572 | struct xhci_ep_ctx *ep_ctx; | |
1573 | u32 trb_comp_code; | |
1574 | ||
1575 | slot_id = TRB_TO_SLOT_ID(event->flags); | |
1576 | xdev = xhci->devs[slot_id]; | |
1577 | ep_index = TRB_TO_EP_ID(event->flags) - 1; | |
1578 | ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer); | |
1579 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); | |
1580 | trb_comp_code = GET_COMP_CODE(event->transfer_len); | |
1581 | ||
1582 | xhci_debug_trb(xhci, xhci->event_ring->dequeue); | |
1583 | switch (trb_comp_code) { | |
1584 | case COMP_SUCCESS: | |
1585 | if (event_trb == ep_ring->dequeue) { | |
1586 | xhci_warn(xhci, "WARN: Success on ctrl setup TRB " | |
1587 | "without IOC set??\n"); | |
1588 | *status = -ESHUTDOWN; | |
1589 | } else if (event_trb != td->last_trb) { | |
1590 | xhci_warn(xhci, "WARN: Success on ctrl data TRB " | |
1591 | "without IOC set??\n"); | |
1592 | *status = -ESHUTDOWN; | |
1593 | } else { | |
1594 | xhci_dbg(xhci, "Successful control transfer!\n"); | |
1595 | *status = 0; | |
1596 | } | |
1597 | break; | |
1598 | case COMP_SHORT_TX: | |
1599 | xhci_warn(xhci, "WARN: short transfer on control ep\n"); | |
1600 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1601 | *status = -EREMOTEIO; | |
1602 | else | |
1603 | *status = 0; | |
1604 | break; | |
1605 | default: | |
1606 | if (!xhci_requires_manual_halt_cleanup(xhci, | |
1607 | ep_ctx, trb_comp_code)) | |
1608 | break; | |
1609 | xhci_dbg(xhci, "TRB error code %u, " | |
1610 | "halted endpoint index = %u\n", | |
1611 | trb_comp_code, ep_index); | |
1612 | /* else fall through */ | |
1613 | case COMP_STALL: | |
1614 | /* Did we transfer part of the data (middle) phase? */ | |
1615 | if (event_trb != ep_ring->dequeue && | |
1616 | event_trb != td->last_trb) | |
1617 | td->urb->actual_length = | |
1618 | td->urb->transfer_buffer_length | |
1619 | - TRB_LEN(event->transfer_len); | |
1620 | else | |
1621 | td->urb->actual_length = 0; | |
1622 | ||
1623 | xhci_cleanup_halted_endpoint(xhci, | |
1624 | slot_id, ep_index, 0, td, event_trb); | |
1625 | return finish_td(xhci, td, event_trb, event, ep, status, true); | |
1626 | } | |
1627 | /* | |
1628 | * Did we transfer any data, despite the errors that might have | |
1629 | * happened? I.e. did we get past the setup stage? | |
1630 | */ | |
1631 | if (event_trb != ep_ring->dequeue) { | |
1632 | /* The event was for the status stage */ | |
1633 | if (event_trb == td->last_trb) { | |
1634 | if (td->urb->actual_length != 0) { | |
1635 | /* Don't overwrite a previously set error code | |
1636 | */ | |
1637 | if ((*status == -EINPROGRESS || *status == 0) && | |
1638 | (td->urb->transfer_flags | |
1639 | & URB_SHORT_NOT_OK)) | |
1640 | /* Did we already see a short data | |
1641 | * stage? */ | |
1642 | *status = -EREMOTEIO; | |
1643 | } else { | |
1644 | td->urb->actual_length = | |
1645 | td->urb->transfer_buffer_length; | |
1646 | } | |
1647 | } else { | |
1648 | /* Maybe the event was for the data stage? */ | |
1649 | if (trb_comp_code != COMP_STOP_INVAL) { | |
1650 | /* We didn't stop on a link TRB in the middle */ | |
1651 | td->urb->actual_length = | |
1652 | td->urb->transfer_buffer_length - | |
1653 | TRB_LEN(event->transfer_len); | |
1654 | xhci_dbg(xhci, "Waiting for status " | |
1655 | "stage event\n"); | |
1656 | return 0; | |
1657 | } | |
1658 | } | |
1659 | } | |
1660 | ||
1661 | return finish_td(xhci, td, event_trb, event, ep, status, false); | |
1662 | } | |
1663 | ||
04e51901 AX |
1664 | /* |
1665 | * Process isochronous tds, update urb packet status and actual_length. | |
1666 | */ | |
1667 | static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, | |
1668 | union xhci_trb *event_trb, struct xhci_transfer_event *event, | |
1669 | struct xhci_virt_ep *ep, int *status) | |
1670 | { | |
1671 | struct xhci_ring *ep_ring; | |
1672 | struct urb_priv *urb_priv; | |
1673 | int idx; | |
1674 | int len = 0; | |
1675 | int skip_td = 0; | |
1676 | union xhci_trb *cur_trb; | |
1677 | struct xhci_segment *cur_seg; | |
1678 | u32 trb_comp_code; | |
1679 | ||
1680 | ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer); | |
1681 | trb_comp_code = GET_COMP_CODE(event->transfer_len); | |
1682 | urb_priv = td->urb->hcpriv; | |
1683 | idx = urb_priv->td_cnt; | |
1684 | ||
1685 | if (ep->skip) { | |
1686 | /* The transfer is partly done */ | |
1687 | *status = -EXDEV; | |
1688 | td->urb->iso_frame_desc[idx].status = -EXDEV; | |
1689 | } else { | |
1690 | /* handle completion code */ | |
1691 | switch (trb_comp_code) { | |
1692 | case COMP_SUCCESS: | |
1693 | td->urb->iso_frame_desc[idx].status = 0; | |
1694 | xhci_dbg(xhci, "Successful isoc transfer!\n"); | |
1695 | break; | |
1696 | case COMP_SHORT_TX: | |
1697 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1698 | td->urb->iso_frame_desc[idx].status = | |
1699 | -EREMOTEIO; | |
1700 | else | |
1701 | td->urb->iso_frame_desc[idx].status = 0; | |
1702 | break; | |
1703 | case COMP_BW_OVER: | |
1704 | td->urb->iso_frame_desc[idx].status = -ECOMM; | |
1705 | skip_td = 1; | |
1706 | break; | |
1707 | case COMP_BUFF_OVER: | |
1708 | case COMP_BABBLE: | |
1709 | td->urb->iso_frame_desc[idx].status = -EOVERFLOW; | |
1710 | skip_td = 1; | |
1711 | break; | |
1712 | case COMP_STALL: | |
1713 | td->urb->iso_frame_desc[idx].status = -EPROTO; | |
1714 | skip_td = 1; | |
1715 | break; | |
1716 | case COMP_STOP: | |
1717 | case COMP_STOP_INVAL: | |
1718 | break; | |
1719 | default: | |
1720 | td->urb->iso_frame_desc[idx].status = -1; | |
1721 | break; | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | /* calc actual length */ | |
1726 | if (ep->skip) { | |
1727 | td->urb->iso_frame_desc[idx].actual_length = 0; | |
14184f9b AX |
1728 | /* Update ring dequeue pointer */ |
1729 | while (ep_ring->dequeue != td->last_trb) | |
1730 | inc_deq(xhci, ep_ring, false); | |
1731 | inc_deq(xhci, ep_ring, false); | |
04e51901 AX |
1732 | return finish_td(xhci, td, event_trb, event, ep, status, true); |
1733 | } | |
1734 | ||
1735 | if (trb_comp_code == COMP_SUCCESS || skip_td == 1) { | |
1736 | td->urb->iso_frame_desc[idx].actual_length = | |
1737 | td->urb->iso_frame_desc[idx].length; | |
1738 | td->urb->actual_length += | |
1739 | td->urb->iso_frame_desc[idx].length; | |
1740 | } else { | |
1741 | for (cur_trb = ep_ring->dequeue, | |
1742 | cur_seg = ep_ring->deq_seg; cur_trb != event_trb; | |
1743 | next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { | |
1744 | if ((cur_trb->generic.field[3] & | |
1745 | TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) && | |
1746 | (cur_trb->generic.field[3] & | |
1747 | TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK)) | |
1748 | len += | |
1749 | TRB_LEN(cur_trb->generic.field[2]); | |
1750 | } | |
1751 | len += TRB_LEN(cur_trb->generic.field[2]) - | |
1752 | TRB_LEN(event->transfer_len); | |
1753 | ||
1754 | if (trb_comp_code != COMP_STOP_INVAL) { | |
1755 | td->urb->iso_frame_desc[idx].actual_length = len; | |
1756 | td->urb->actual_length += len; | |
1757 | } | |
1758 | } | |
1759 | ||
1760 | if ((idx == urb_priv->length - 1) && *status == -EINPROGRESS) | |
1761 | *status = 0; | |
1762 | ||
1763 | return finish_td(xhci, td, event_trb, event, ep, status, false); | |
1764 | } | |
1765 | ||
22405ed2 AX |
1766 | /* |
1767 | * Process bulk and interrupt tds, update urb status and actual_length. | |
1768 | */ | |
1769 | static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td, | |
1770 | union xhci_trb *event_trb, struct xhci_transfer_event *event, | |
1771 | struct xhci_virt_ep *ep, int *status) | |
1772 | { | |
1773 | struct xhci_ring *ep_ring; | |
1774 | union xhci_trb *cur_trb; | |
1775 | struct xhci_segment *cur_seg; | |
1776 | u32 trb_comp_code; | |
1777 | ||
1778 | ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer); | |
1779 | trb_comp_code = GET_COMP_CODE(event->transfer_len); | |
1780 | ||
1781 | switch (trb_comp_code) { | |
1782 | case COMP_SUCCESS: | |
1783 | /* Double check that the HW transferred everything. */ | |
1784 | if (event_trb != td->last_trb) { | |
1785 | xhci_warn(xhci, "WARN Successful completion " | |
1786 | "on short TX\n"); | |
1787 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1788 | *status = -EREMOTEIO; | |
1789 | else | |
1790 | *status = 0; | |
1791 | } else { | |
1792 | if (usb_endpoint_xfer_bulk(&td->urb->ep->desc)) | |
1793 | xhci_dbg(xhci, "Successful bulk " | |
1794 | "transfer!\n"); | |
1795 | else | |
1796 | xhci_dbg(xhci, "Successful interrupt " | |
1797 | "transfer!\n"); | |
1798 | *status = 0; | |
1799 | } | |
1800 | break; | |
1801 | case COMP_SHORT_TX: | |
1802 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1803 | *status = -EREMOTEIO; | |
1804 | else | |
1805 | *status = 0; | |
1806 | break; | |
1807 | default: | |
1808 | /* Others already handled above */ | |
1809 | break; | |
1810 | } | |
f2c565e2 | 1811 | xhci_dbg(xhci, "ep %#x - asked for %d bytes, " |
22405ed2 AX |
1812 | "%d bytes untransferred\n", |
1813 | td->urb->ep->desc.bEndpointAddress, | |
1814 | td->urb->transfer_buffer_length, | |
1815 | TRB_LEN(event->transfer_len)); | |
1816 | /* Fast path - was this the last TRB in the TD for this URB? */ | |
1817 | if (event_trb == td->last_trb) { | |
1818 | if (TRB_LEN(event->transfer_len) != 0) { | |
1819 | td->urb->actual_length = | |
1820 | td->urb->transfer_buffer_length - | |
1821 | TRB_LEN(event->transfer_len); | |
1822 | if (td->urb->transfer_buffer_length < | |
1823 | td->urb->actual_length) { | |
1824 | xhci_warn(xhci, "HC gave bad length " | |
1825 | "of %d bytes left\n", | |
1826 | TRB_LEN(event->transfer_len)); | |
1827 | td->urb->actual_length = 0; | |
1828 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1829 | *status = -EREMOTEIO; | |
1830 | else | |
1831 | *status = 0; | |
1832 | } | |
1833 | /* Don't overwrite a previously set error code */ | |
1834 | if (*status == -EINPROGRESS) { | |
1835 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) | |
1836 | *status = -EREMOTEIO; | |
1837 | else | |
1838 | *status = 0; | |
1839 | } | |
1840 | } else { | |
1841 | td->urb->actual_length = | |
1842 | td->urb->transfer_buffer_length; | |
1843 | /* Ignore a short packet completion if the | |
1844 | * untransferred length was zero. | |
1845 | */ | |
1846 | if (*status == -EREMOTEIO) | |
1847 | *status = 0; | |
1848 | } | |
1849 | } else { | |
1850 | /* Slow path - walk the list, starting from the dequeue | |
1851 | * pointer, to get the actual length transferred. | |
1852 | */ | |
1853 | td->urb->actual_length = 0; | |
1854 | for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg; | |
1855 | cur_trb != event_trb; | |
1856 | next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { | |
1857 | if ((cur_trb->generic.field[3] & | |
1858 | TRB_TYPE_BITMASK) != TRB_TYPE(TRB_TR_NOOP) && | |
1859 | (cur_trb->generic.field[3] & | |
1860 | TRB_TYPE_BITMASK) != TRB_TYPE(TRB_LINK)) | |
1861 | td->urb->actual_length += | |
1862 | TRB_LEN(cur_trb->generic.field[2]); | |
1863 | } | |
1864 | /* If the ring didn't stop on a Link or No-op TRB, add | |
1865 | * in the actual bytes transferred from the Normal TRB | |
1866 | */ | |
1867 | if (trb_comp_code != COMP_STOP_INVAL) | |
1868 | td->urb->actual_length += | |
1869 | TRB_LEN(cur_trb->generic.field[2]) - | |
1870 | TRB_LEN(event->transfer_len); | |
1871 | } | |
1872 | ||
1873 | return finish_td(xhci, td, event_trb, event, ep, status, false); | |
1874 | } | |
1875 | ||
d0e96f5a SS |
1876 | /* |
1877 | * If this function returns an error condition, it means it got a Transfer | |
1878 | * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. | |
1879 | * At this point, the host controller is probably hosed and should be reset. | |
1880 | */ | |
1881 | static int handle_tx_event(struct xhci_hcd *xhci, | |
1882 | struct xhci_transfer_event *event) | |
1883 | { | |
1884 | struct xhci_virt_device *xdev; | |
63a0d9ab | 1885 | struct xhci_virt_ep *ep; |
d0e96f5a | 1886 | struct xhci_ring *ep_ring; |
82d1009f | 1887 | unsigned int slot_id; |
d0e96f5a | 1888 | int ep_index; |
326b4810 | 1889 | struct xhci_td *td = NULL; |
d0e96f5a SS |
1890 | dma_addr_t event_dma; |
1891 | struct xhci_segment *event_seg; | |
1892 | union xhci_trb *event_trb; | |
326b4810 | 1893 | struct urb *urb = NULL; |
d0e96f5a | 1894 | int status = -EINPROGRESS; |
8e51adcc | 1895 | struct urb_priv *urb_priv; |
d115b048 | 1896 | struct xhci_ep_ctx *ep_ctx; |
66d1eebc | 1897 | u32 trb_comp_code; |
4422da61 | 1898 | int ret = 0; |
d0e96f5a | 1899 | |
82d1009f SS |
1900 | slot_id = TRB_TO_SLOT_ID(event->flags); |
1901 | xdev = xhci->devs[slot_id]; | |
d0e96f5a SS |
1902 | if (!xdev) { |
1903 | xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n"); | |
1904 | return -ENODEV; | |
1905 | } | |
1906 | ||
1907 | /* Endpoint ID is 1 based, our index is zero based */ | |
1908 | ep_index = TRB_TO_EP_ID(event->flags) - 1; | |
66e49d87 | 1909 | xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index); |
63a0d9ab | 1910 | ep = &xdev->eps[ep_index]; |
e9df17eb | 1911 | ep_ring = xhci_dma_to_transfer_ring(ep, event->buffer); |
d115b048 | 1912 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
986a92d4 AX |
1913 | if (!ep_ring || |
1914 | (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) { | |
e9df17eb SS |
1915 | xhci_err(xhci, "ERROR Transfer event for disabled endpoint " |
1916 | "or incorrect stream ring\n"); | |
d0e96f5a SS |
1917 | return -ENODEV; |
1918 | } | |
1919 | ||
8e595a5d | 1920 | event_dma = event->buffer; |
66d1eebc | 1921 | trb_comp_code = GET_COMP_CODE(event->transfer_len); |
986a92d4 | 1922 | /* Look for common error cases */ |
66d1eebc | 1923 | switch (trb_comp_code) { |
b10de142 SS |
1924 | /* Skip codes that require special handling depending on |
1925 | * transfer type | |
1926 | */ | |
1927 | case COMP_SUCCESS: | |
1928 | case COMP_SHORT_TX: | |
1929 | break; | |
ae636747 SS |
1930 | case COMP_STOP: |
1931 | xhci_dbg(xhci, "Stopped on Transfer TRB\n"); | |
1932 | break; | |
1933 | case COMP_STOP_INVAL: | |
1934 | xhci_dbg(xhci, "Stopped on No-op or Link TRB\n"); | |
1935 | break; | |
b10de142 SS |
1936 | case COMP_STALL: |
1937 | xhci_warn(xhci, "WARN: Stalled endpoint\n"); | |
63a0d9ab | 1938 | ep->ep_state |= EP_HALTED; |
b10de142 SS |
1939 | status = -EPIPE; |
1940 | break; | |
1941 | case COMP_TRB_ERR: | |
1942 | xhci_warn(xhci, "WARN: TRB error on endpoint\n"); | |
1943 | status = -EILSEQ; | |
1944 | break; | |
ec74e403 | 1945 | case COMP_SPLIT_ERR: |
b10de142 SS |
1946 | case COMP_TX_ERR: |
1947 | xhci_warn(xhci, "WARN: transfer error on endpoint\n"); | |
1948 | status = -EPROTO; | |
1949 | break; | |
4a73143c SS |
1950 | case COMP_BABBLE: |
1951 | xhci_warn(xhci, "WARN: babble error on endpoint\n"); | |
1952 | status = -EOVERFLOW; | |
1953 | break; | |
b10de142 SS |
1954 | case COMP_DB_ERR: |
1955 | xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n"); | |
1956 | status = -ENOSR; | |
1957 | break; | |
986a92d4 AX |
1958 | case COMP_BW_OVER: |
1959 | xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n"); | |
1960 | break; | |
1961 | case COMP_BUFF_OVER: | |
1962 | xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n"); | |
1963 | break; | |
1964 | case COMP_UNDERRUN: | |
1965 | /* | |
1966 | * When the Isoch ring is empty, the xHC will generate | |
1967 | * a Ring Overrun Event for IN Isoch endpoint or Ring | |
1968 | * Underrun Event for OUT Isoch endpoint. | |
1969 | */ | |
1970 | xhci_dbg(xhci, "underrun event on endpoint\n"); | |
1971 | if (!list_empty(&ep_ring->td_list)) | |
1972 | xhci_dbg(xhci, "Underrun Event for slot %d ep %d " | |
1973 | "still with TDs queued?\n", | |
1974 | TRB_TO_SLOT_ID(event->flags), ep_index); | |
1975 | goto cleanup; | |
1976 | case COMP_OVERRUN: | |
1977 | xhci_dbg(xhci, "overrun event on endpoint\n"); | |
1978 | if (!list_empty(&ep_ring->td_list)) | |
1979 | xhci_dbg(xhci, "Overrun Event for slot %d ep %d " | |
1980 | "still with TDs queued?\n", | |
1981 | TRB_TO_SLOT_ID(event->flags), ep_index); | |
1982 | goto cleanup; | |
d18240db AX |
1983 | case COMP_MISSED_INT: |
1984 | /* | |
1985 | * When encounter missed service error, one or more isoc tds | |
1986 | * may be missed by xHC. | |
1987 | * Set skip flag of the ep_ring; Complete the missed tds as | |
1988 | * short transfer when process the ep_ring next time. | |
1989 | */ | |
1990 | ep->skip = true; | |
1991 | xhci_dbg(xhci, "Miss service interval error, set skip flag\n"); | |
1992 | goto cleanup; | |
b10de142 | 1993 | default: |
b45b5069 | 1994 | if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { |
5ad6a529 SS |
1995 | status = 0; |
1996 | break; | |
1997 | } | |
986a92d4 AX |
1998 | xhci_warn(xhci, "ERROR Unknown event condition, HC probably " |
1999 | "busted\n"); | |
2000 | goto cleanup; | |
2001 | } | |
2002 | ||
d18240db AX |
2003 | do { |
2004 | /* This TRB should be in the TD at the head of this ring's | |
2005 | * TD list. | |
2006 | */ | |
2007 | if (list_empty(&ep_ring->td_list)) { | |
2008 | xhci_warn(xhci, "WARN Event TRB for slot %d ep %d " | |
2009 | "with no TDs queued?\n", | |
2010 | TRB_TO_SLOT_ID(event->flags), ep_index); | |
2011 | xhci_dbg(xhci, "Event TRB with TRB type ID %u\n", | |
2012 | (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10); | |
2013 | xhci_print_trb_offsets(xhci, (union xhci_trb *) event); | |
2014 | if (ep->skip) { | |
2015 | ep->skip = false; | |
2016 | xhci_dbg(xhci, "td_list is empty while skip " | |
2017 | "flag set. Clear skip flag.\n"); | |
2018 | } | |
2019 | ret = 0; | |
2020 | goto cleanup; | |
2021 | } | |
986a92d4 | 2022 | |
d18240db AX |
2023 | td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list); |
2024 | /* Is this a TRB in the currently executing TD? */ | |
2025 | event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue, | |
2026 | td->last_trb, event_dma); | |
2027 | if (event_seg && ep->skip) { | |
2028 | xhci_dbg(xhci, "Found td. Clear skip flag.\n"); | |
2029 | ep->skip = false; | |
2030 | } | |
2031 | if (!event_seg && | |
2032 | (!ep->skip || !usb_endpoint_xfer_isoc(&td->urb->ep->desc))) { | |
2033 | /* HC is busted, give up! */ | |
2034 | xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not " | |
2035 | "part of current TD\n"); | |
2036 | return -ESHUTDOWN; | |
2037 | } | |
678539cf | 2038 | |
d18240db AX |
2039 | if (event_seg) { |
2040 | event_trb = &event_seg->trbs[(event_dma - | |
2041 | event_seg->dma) / sizeof(*event_trb)]; | |
2042 | /* | |
2043 | * No-op TRB should not trigger interrupts. | |
2044 | * If event_trb is a no-op TRB, it means the | |
2045 | * corresponding TD has been cancelled. Just ignore | |
2046 | * the TD. | |
2047 | */ | |
2048 | if ((event_trb->generic.field[3] & TRB_TYPE_BITMASK) | |
2049 | == TRB_TYPE(TRB_TR_NOOP)) { | |
2050 | xhci_dbg(xhci, "event_trb is a no-op TRB. " | |
2051 | "Skip it\n"); | |
2052 | goto cleanup; | |
2053 | } | |
2054 | } | |
4422da61 | 2055 | |
d18240db AX |
2056 | /* Now update the urb's actual_length and give back to |
2057 | * the core | |
82d1009f | 2058 | */ |
d18240db AX |
2059 | if (usb_endpoint_xfer_control(&td->urb->ep->desc)) |
2060 | ret = process_ctrl_td(xhci, td, event_trb, event, ep, | |
2061 | &status); | |
04e51901 AX |
2062 | else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) |
2063 | ret = process_isoc_td(xhci, td, event_trb, event, ep, | |
2064 | &status); | |
d18240db AX |
2065 | else |
2066 | ret = process_bulk_intr_td(xhci, td, event_trb, event, | |
2067 | ep, &status); | |
2068 | ||
2069 | cleanup: | |
2070 | /* | |
2071 | * Do not update event ring dequeue pointer if ep->skip is set. | |
2072 | * Will roll back to continue process missed tds. | |
2073 | */ | |
2074 | if (trb_comp_code == COMP_MISSED_INT || !ep->skip) { | |
2075 | inc_deq(xhci, xhci->event_ring, true); | |
d18240db AX |
2076 | } |
2077 | ||
2078 | if (ret) { | |
2079 | urb = td->urb; | |
8e51adcc | 2080 | urb_priv = urb->hcpriv; |
d18240db AX |
2081 | /* Leave the TD around for the reset endpoint function |
2082 | * to use(but only if it's not a control endpoint, | |
2083 | * since we already queued the Set TR dequeue pointer | |
2084 | * command for stalled control endpoints). | |
2085 | */ | |
2086 | if (usb_endpoint_xfer_control(&urb->ep->desc) || | |
2087 | (trb_comp_code != COMP_STALL && | |
2088 | trb_comp_code != COMP_BABBLE)) | |
8e51adcc | 2089 | xhci_urb_free_priv(xhci, urb_priv); |
d18240db | 2090 | |
214f76f7 | 2091 | usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); |
d18240db AX |
2092 | xhci_dbg(xhci, "Giveback URB %p, len = %d, " |
2093 | "status = %d\n", | |
2094 | urb, urb->actual_length, status); | |
2095 | spin_unlock(&xhci->lock); | |
214f76f7 | 2096 | usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status); |
d18240db AX |
2097 | spin_lock(&xhci->lock); |
2098 | } | |
2099 | ||
2100 | /* | |
2101 | * If ep->skip is set, it means there are missed tds on the | |
2102 | * endpoint ring need to take care of. | |
2103 | * Process them as short transfer until reach the td pointed by | |
2104 | * the event. | |
2105 | */ | |
2106 | } while (ep->skip && trb_comp_code != COMP_MISSED_INT); | |
2107 | ||
d0e96f5a SS |
2108 | return 0; |
2109 | } | |
2110 | ||
0f2a7930 SS |
2111 | /* |
2112 | * This function handles all OS-owned events on the event ring. It may drop | |
2113 | * xhci->lock between event processing (e.g. to pass up port status changes). | |
2114 | */ | |
d6d98a4d | 2115 | static void xhci_handle_event(struct xhci_hcd *xhci) |
7f84eef0 SS |
2116 | { |
2117 | union xhci_trb *event; | |
0f2a7930 | 2118 | int update_ptrs = 1; |
d0e96f5a | 2119 | int ret; |
7f84eef0 | 2120 | |
66e49d87 | 2121 | xhci_dbg(xhci, "In %s\n", __func__); |
7f84eef0 SS |
2122 | if (!xhci->event_ring || !xhci->event_ring->dequeue) { |
2123 | xhci->error_bitmask |= 1 << 1; | |
2124 | return; | |
2125 | } | |
2126 | ||
2127 | event = xhci->event_ring->dequeue; | |
2128 | /* Does the HC or OS own the TRB? */ | |
2129 | if ((event->event_cmd.flags & TRB_CYCLE) != | |
2130 | xhci->event_ring->cycle_state) { | |
2131 | xhci->error_bitmask |= 1 << 2; | |
2132 | return; | |
2133 | } | |
66e49d87 | 2134 | xhci_dbg(xhci, "%s - OS owns TRB\n", __func__); |
7f84eef0 | 2135 | |
0f2a7930 | 2136 | /* FIXME: Handle more event types. */ |
7f84eef0 SS |
2137 | switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) { |
2138 | case TRB_TYPE(TRB_COMPLETION): | |
66e49d87 | 2139 | xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__); |
7f84eef0 | 2140 | handle_cmd_completion(xhci, &event->event_cmd); |
66e49d87 | 2141 | xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__); |
7f84eef0 | 2142 | break; |
0f2a7930 | 2143 | case TRB_TYPE(TRB_PORT_STATUS): |
66e49d87 | 2144 | xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__); |
0f2a7930 | 2145 | handle_port_status(xhci, event); |
66e49d87 | 2146 | xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__); |
0f2a7930 SS |
2147 | update_ptrs = 0; |
2148 | break; | |
d0e96f5a | 2149 | case TRB_TYPE(TRB_TRANSFER): |
66e49d87 | 2150 | xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__); |
d0e96f5a | 2151 | ret = handle_tx_event(xhci, &event->trans_event); |
66e49d87 | 2152 | xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__); |
d0e96f5a SS |
2153 | if (ret < 0) |
2154 | xhci->error_bitmask |= 1 << 9; | |
2155 | else | |
2156 | update_ptrs = 0; | |
2157 | break; | |
7f84eef0 | 2158 | default: |
0238634d SS |
2159 | if ((event->event_cmd.flags & TRB_TYPE_BITMASK) >= TRB_TYPE(48)) |
2160 | handle_vendor_event(xhci, event); | |
2161 | else | |
2162 | xhci->error_bitmask |= 1 << 3; | |
7f84eef0 | 2163 | } |
6f5165cf SS |
2164 | /* Any of the above functions may drop and re-acquire the lock, so check |
2165 | * to make sure a watchdog timer didn't mark the host as non-responsive. | |
2166 | */ | |
2167 | if (xhci->xhc_state & XHCI_STATE_DYING) { | |
2168 | xhci_dbg(xhci, "xHCI host dying, returning from " | |
2169 | "event handler.\n"); | |
2170 | return; | |
2171 | } | |
7f84eef0 | 2172 | |
c06d68b8 SS |
2173 | if (update_ptrs) |
2174 | /* Update SW event ring dequeue pointer */ | |
0f2a7930 | 2175 | inc_deq(xhci, xhci->event_ring, true); |
c06d68b8 | 2176 | |
7f84eef0 | 2177 | /* Are there more items on the event ring? */ |
b7258a4a | 2178 | xhci_handle_event(xhci); |
7f84eef0 | 2179 | } |
9032cd52 SS |
2180 | |
2181 | /* | |
2182 | * xHCI spec says we can get an interrupt, and if the HC has an error condition, | |
2183 | * we might get bad data out of the event ring. Section 4.10.2.7 has a list of | |
2184 | * indicators of an event TRB error, but we check the status *first* to be safe. | |
2185 | */ | |
2186 | irqreturn_t xhci_irq(struct usb_hcd *hcd) | |
2187 | { | |
2188 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); | |
c21599a3 | 2189 | u32 status; |
9032cd52 | 2190 | union xhci_trb *trb; |
bda53145 | 2191 | u64 temp_64; |
c06d68b8 SS |
2192 | union xhci_trb *event_ring_deq; |
2193 | dma_addr_t deq; | |
9032cd52 SS |
2194 | |
2195 | spin_lock(&xhci->lock); | |
2196 | trb = xhci->event_ring->dequeue; | |
2197 | /* Check if the xHC generated the interrupt, or the irq is shared */ | |
27e0dd4d | 2198 | status = xhci_readl(xhci, &xhci->op_regs->status); |
c21599a3 | 2199 | if (status == 0xffffffff) |
9032cd52 SS |
2200 | goto hw_died; |
2201 | ||
c21599a3 | 2202 | if (!(status & STS_EINT)) { |
9032cd52 | 2203 | spin_unlock(&xhci->lock); |
9032cd52 SS |
2204 | return IRQ_NONE; |
2205 | } | |
27e0dd4d | 2206 | xhci_dbg(xhci, "op reg status = %08x\n", status); |
9032cd52 SS |
2207 | xhci_dbg(xhci, "Event ring dequeue ptr:\n"); |
2208 | xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n", | |
2209 | (unsigned long long) | |
2210 | xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb), | |
2211 | lower_32_bits(trb->link.segment_ptr), | |
2212 | upper_32_bits(trb->link.segment_ptr), | |
2213 | (unsigned int) trb->link.intr_target, | |
2214 | (unsigned int) trb->link.control); | |
2215 | ||
27e0dd4d | 2216 | if (status & STS_FATAL) { |
9032cd52 SS |
2217 | xhci_warn(xhci, "WARNING: Host System Error\n"); |
2218 | xhci_halt(xhci); | |
2219 | hw_died: | |
9032cd52 SS |
2220 | spin_unlock(&xhci->lock); |
2221 | return -ESHUTDOWN; | |
2222 | } | |
2223 | ||
bda53145 SS |
2224 | /* |
2225 | * Clear the op reg interrupt status first, | |
2226 | * so we can receive interrupts from other MSI-X interrupters. | |
2227 | * Write 1 to clear the interrupt status. | |
2228 | */ | |
27e0dd4d SS |
2229 | status |= STS_EINT; |
2230 | xhci_writel(xhci, status, &xhci->op_regs->status); | |
bda53145 SS |
2231 | /* FIXME when MSI-X is supported and there are multiple vectors */ |
2232 | /* Clear the MSI-X event interrupt status */ | |
2233 | ||
c21599a3 SS |
2234 | if (hcd->irq != -1) { |
2235 | u32 irq_pending; | |
2236 | /* Acknowledge the PCI interrupt */ | |
2237 | irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending); | |
2238 | irq_pending |= 0x3; | |
2239 | xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending); | |
2240 | } | |
bda53145 | 2241 | |
c06d68b8 | 2242 | if (xhci->xhc_state & XHCI_STATE_DYING) { |
bda53145 SS |
2243 | xhci_dbg(xhci, "xHCI dying, ignoring interrupt. " |
2244 | "Shouldn't IRQs be disabled?\n"); | |
c06d68b8 SS |
2245 | /* Clear the event handler busy flag (RW1C); |
2246 | * the event ring should be empty. | |
bda53145 | 2247 | */ |
c06d68b8 SS |
2248 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
2249 | xhci_write_64(xhci, temp_64 | ERST_EHB, | |
2250 | &xhci->ir_set->erst_dequeue); | |
2251 | spin_unlock(&xhci->lock); | |
2252 | ||
2253 | return IRQ_HANDLED; | |
2254 | } | |
2255 | ||
2256 | event_ring_deq = xhci->event_ring->dequeue; | |
2257 | /* FIXME this should be a delayed service routine | |
2258 | * that clears the EHB. | |
2259 | */ | |
2260 | xhci_handle_event(xhci); | |
bda53145 | 2261 | |
bda53145 | 2262 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
c06d68b8 SS |
2263 | /* If necessary, update the HW's version of the event ring deq ptr. */ |
2264 | if (event_ring_deq != xhci->event_ring->dequeue) { | |
2265 | deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, | |
2266 | xhci->event_ring->dequeue); | |
2267 | if (deq == 0) | |
2268 | xhci_warn(xhci, "WARN something wrong with SW event " | |
2269 | "ring dequeue ptr.\n"); | |
2270 | /* Update HC event ring dequeue pointer */ | |
2271 | temp_64 &= ERST_PTR_MASK; | |
2272 | temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK); | |
2273 | } | |
2274 | ||
2275 | /* Clear the event handler busy flag (RW1C); event ring is empty. */ | |
2276 | temp_64 |= ERST_EHB; | |
2277 | xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue); | |
2278 | ||
9032cd52 SS |
2279 | spin_unlock(&xhci->lock); |
2280 | ||
2281 | return IRQ_HANDLED; | |
2282 | } | |
2283 | ||
2284 | irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd) | |
2285 | { | |
2286 | irqreturn_t ret; | |
b3209379 | 2287 | struct xhci_hcd *xhci; |
9032cd52 | 2288 | |
b3209379 | 2289 | xhci = hcd_to_xhci(hcd); |
9032cd52 | 2290 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); |
b3209379 SS |
2291 | if (xhci->shared_hcd) |
2292 | set_bit(HCD_FLAG_SAW_IRQ, &xhci->shared_hcd->flags); | |
9032cd52 SS |
2293 | |
2294 | ret = xhci_irq(hcd); | |
2295 | ||
2296 | return ret; | |
2297 | } | |
7f84eef0 | 2298 | |
d0e96f5a SS |
2299 | /**** Endpoint Ring Operations ****/ |
2300 | ||
7f84eef0 SS |
2301 | /* |
2302 | * Generic function for queueing a TRB on a ring. | |
2303 | * The caller must have checked to make sure there's room on the ring. | |
6cc30d85 SS |
2304 | * |
2305 | * @more_trbs_coming: Will you enqueue more TRBs before calling | |
2306 | * prepare_transfer()? | |
7f84eef0 SS |
2307 | */ |
2308 | static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
6cc30d85 | 2309 | bool consumer, bool more_trbs_coming, |
7f84eef0 SS |
2310 | u32 field1, u32 field2, u32 field3, u32 field4) |
2311 | { | |
2312 | struct xhci_generic_trb *trb; | |
2313 | ||
2314 | trb = &ring->enqueue->generic; | |
2315 | trb->field[0] = field1; | |
2316 | trb->field[1] = field2; | |
2317 | trb->field[2] = field3; | |
2318 | trb->field[3] = field4; | |
6cc30d85 | 2319 | inc_enq(xhci, ring, consumer, more_trbs_coming); |
7f84eef0 SS |
2320 | } |
2321 | ||
d0e96f5a SS |
2322 | /* |
2323 | * Does various checks on the endpoint ring, and makes it ready to queue num_trbs. | |
2324 | * FIXME allocate segments if the ring is full. | |
2325 | */ | |
2326 | static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, | |
2327 | u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) | |
2328 | { | |
2329 | /* Make sure the endpoint has been added to xHC schedule */ | |
2330 | xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state); | |
2331 | switch (ep_state) { | |
2332 | case EP_STATE_DISABLED: | |
2333 | /* | |
2334 | * USB core changed config/interfaces without notifying us, | |
2335 | * or hardware is reporting the wrong state. | |
2336 | */ | |
2337 | xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); | |
2338 | return -ENOENT; | |
d0e96f5a | 2339 | case EP_STATE_ERROR: |
c92bcfa7 | 2340 | xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); |
d0e96f5a SS |
2341 | /* FIXME event handling code for error needs to clear it */ |
2342 | /* XXX not sure if this should be -ENOENT or not */ | |
2343 | return -EINVAL; | |
c92bcfa7 SS |
2344 | case EP_STATE_HALTED: |
2345 | xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); | |
d0e96f5a SS |
2346 | case EP_STATE_STOPPED: |
2347 | case EP_STATE_RUNNING: | |
2348 | break; | |
2349 | default: | |
2350 | xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); | |
2351 | /* | |
2352 | * FIXME issue Configure Endpoint command to try to get the HC | |
2353 | * back into a known state. | |
2354 | */ | |
2355 | return -EINVAL; | |
2356 | } | |
2357 | if (!room_on_ring(xhci, ep_ring, num_trbs)) { | |
2358 | /* FIXME allocate more room */ | |
2359 | xhci_err(xhci, "ERROR no room on ep ring\n"); | |
2360 | return -ENOMEM; | |
2361 | } | |
6c12db90 JY |
2362 | |
2363 | if (enqueue_is_link_trb(ep_ring)) { | |
2364 | struct xhci_ring *ring = ep_ring; | |
2365 | union xhci_trb *next; | |
6c12db90 JY |
2366 | |
2367 | xhci_dbg(xhci, "prepare_ring: pointing to link trb\n"); | |
2368 | next = ring->enqueue; | |
2369 | ||
2370 | while (last_trb(xhci, ring, ring->enq_seg, next)) { | |
2371 | ||
2372 | /* If we're not dealing with 0.95 hardware, | |
2373 | * clear the chain bit. | |
2374 | */ | |
2375 | if (!xhci_link_trb_quirk(xhci)) | |
2376 | next->link.control &= ~TRB_CHAIN; | |
2377 | else | |
2378 | next->link.control |= TRB_CHAIN; | |
2379 | ||
2380 | wmb(); | |
2381 | next->link.control ^= (u32) TRB_CYCLE; | |
2382 | ||
2383 | /* Toggle the cycle bit after the last ring segment. */ | |
2384 | if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { | |
2385 | ring->cycle_state = (ring->cycle_state ? 0 : 1); | |
2386 | if (!in_interrupt()) { | |
2387 | xhci_dbg(xhci, "queue_trb: Toggle cycle " | |
2388 | "state for ring %p = %i\n", | |
2389 | ring, (unsigned int)ring->cycle_state); | |
2390 | } | |
2391 | } | |
2392 | ring->enq_seg = ring->enq_seg->next; | |
2393 | ring->enqueue = ring->enq_seg->trbs; | |
2394 | next = ring->enqueue; | |
2395 | } | |
2396 | } | |
2397 | ||
d0e96f5a SS |
2398 | return 0; |
2399 | } | |
2400 | ||
23e3be11 | 2401 | static int prepare_transfer(struct xhci_hcd *xhci, |
d0e96f5a SS |
2402 | struct xhci_virt_device *xdev, |
2403 | unsigned int ep_index, | |
e9df17eb | 2404 | unsigned int stream_id, |
d0e96f5a SS |
2405 | unsigned int num_trbs, |
2406 | struct urb *urb, | |
8e51adcc | 2407 | unsigned int td_index, |
d0e96f5a SS |
2408 | gfp_t mem_flags) |
2409 | { | |
2410 | int ret; | |
8e51adcc AX |
2411 | struct urb_priv *urb_priv; |
2412 | struct xhci_td *td; | |
e9df17eb | 2413 | struct xhci_ring *ep_ring; |
d115b048 | 2414 | struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
e9df17eb SS |
2415 | |
2416 | ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id); | |
2417 | if (!ep_ring) { | |
2418 | xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n", | |
2419 | stream_id); | |
2420 | return -EINVAL; | |
2421 | } | |
2422 | ||
2423 | ret = prepare_ring(xhci, ep_ring, | |
d115b048 | 2424 | ep_ctx->ep_info & EP_STATE_MASK, |
d0e96f5a SS |
2425 | num_trbs, mem_flags); |
2426 | if (ret) | |
2427 | return ret; | |
d0e96f5a | 2428 | |
8e51adcc AX |
2429 | urb_priv = urb->hcpriv; |
2430 | td = urb_priv->td[td_index]; | |
2431 | ||
2432 | INIT_LIST_HEAD(&td->td_list); | |
2433 | INIT_LIST_HEAD(&td->cancelled_td_list); | |
2434 | ||
2435 | if (td_index == 0) { | |
214f76f7 | 2436 | ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); |
8e51adcc AX |
2437 | if (unlikely(ret)) { |
2438 | xhci_urb_free_priv(xhci, urb_priv); | |
2439 | urb->hcpriv = NULL; | |
2440 | return ret; | |
2441 | } | |
d0e96f5a SS |
2442 | } |
2443 | ||
8e51adcc | 2444 | td->urb = urb; |
d0e96f5a | 2445 | /* Add this TD to the tail of the endpoint ring's TD list */ |
8e51adcc AX |
2446 | list_add_tail(&td->td_list, &ep_ring->td_list); |
2447 | td->start_seg = ep_ring->enq_seg; | |
2448 | td->first_trb = ep_ring->enqueue; | |
2449 | ||
2450 | urb_priv->td[td_index] = td; | |
d0e96f5a SS |
2451 | |
2452 | return 0; | |
2453 | } | |
2454 | ||
23e3be11 | 2455 | static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb) |
8a96c052 SS |
2456 | { |
2457 | int num_sgs, num_trbs, running_total, temp, i; | |
2458 | struct scatterlist *sg; | |
2459 | ||
2460 | sg = NULL; | |
2461 | num_sgs = urb->num_sgs; | |
2462 | temp = urb->transfer_buffer_length; | |
2463 | ||
2464 | xhci_dbg(xhci, "count sg list trbs: \n"); | |
2465 | num_trbs = 0; | |
910f8d0c | 2466 | for_each_sg(urb->sg, sg, num_sgs, i) { |
8a96c052 SS |
2467 | unsigned int previous_total_trbs = num_trbs; |
2468 | unsigned int len = sg_dma_len(sg); | |
2469 | ||
2470 | /* Scatter gather list entries may cross 64KB boundaries */ | |
2471 | running_total = TRB_MAX_BUFF_SIZE - | |
2472 | (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
2473 | if (running_total != 0) | |
2474 | num_trbs++; | |
2475 | ||
2476 | /* How many more 64KB chunks to transfer, how many more TRBs? */ | |
2477 | while (running_total < sg_dma_len(sg)) { | |
2478 | num_trbs++; | |
2479 | running_total += TRB_MAX_BUFF_SIZE; | |
2480 | } | |
700e2052 GKH |
2481 | xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n", |
2482 | i, (unsigned long long)sg_dma_address(sg), | |
2483 | len, len, num_trbs - previous_total_trbs); | |
8a96c052 SS |
2484 | |
2485 | len = min_t(int, len, temp); | |
2486 | temp -= len; | |
2487 | if (temp == 0) | |
2488 | break; | |
2489 | } | |
2490 | xhci_dbg(xhci, "\n"); | |
2491 | if (!in_interrupt()) | |
f2c565e2 AX |
2492 | xhci_dbg(xhci, "ep %#x - urb len = %d, sglist used, " |
2493 | "num_trbs = %d\n", | |
8a96c052 SS |
2494 | urb->ep->desc.bEndpointAddress, |
2495 | urb->transfer_buffer_length, | |
2496 | num_trbs); | |
2497 | return num_trbs; | |
2498 | } | |
2499 | ||
23e3be11 | 2500 | static void check_trb_math(struct urb *urb, int num_trbs, int running_total) |
8a96c052 SS |
2501 | { |
2502 | if (num_trbs != 0) | |
2503 | dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of " | |
2504 | "TRBs, %d left\n", __func__, | |
2505 | urb->ep->desc.bEndpointAddress, num_trbs); | |
2506 | if (running_total != urb->transfer_buffer_length) | |
2507 | dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " | |
2508 | "queued %#x (%d), asked for %#x (%d)\n", | |
2509 | __func__, | |
2510 | urb->ep->desc.bEndpointAddress, | |
2511 | running_total, running_total, | |
2512 | urb->transfer_buffer_length, | |
2513 | urb->transfer_buffer_length); | |
2514 | } | |
2515 | ||
23e3be11 | 2516 | static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, |
e9df17eb | 2517 | unsigned int ep_index, unsigned int stream_id, int start_cycle, |
e1eab2e0 | 2518 | struct xhci_generic_trb *start_trb) |
8a96c052 | 2519 | { |
8a96c052 SS |
2520 | /* |
2521 | * Pass all the TRBs to the hardware at once and make sure this write | |
2522 | * isn't reordered. | |
2523 | */ | |
2524 | wmb(); | |
50f7b52a AX |
2525 | if (start_cycle) |
2526 | start_trb->field[3] |= start_cycle; | |
2527 | else | |
2528 | start_trb->field[3] &= ~0x1; | |
be88fe4f | 2529 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); |
8a96c052 SS |
2530 | } |
2531 | ||
624defa1 SS |
2532 | /* |
2533 | * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt | |
2534 | * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD | |
2535 | * (comprised of sg list entries) can take several service intervals to | |
2536 | * transmit. | |
2537 | */ | |
2538 | int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, | |
2539 | struct urb *urb, int slot_id, unsigned int ep_index) | |
2540 | { | |
2541 | struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, | |
2542 | xhci->devs[slot_id]->out_ctx, ep_index); | |
2543 | int xhci_interval; | |
2544 | int ep_interval; | |
2545 | ||
2546 | xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info); | |
2547 | ep_interval = urb->interval; | |
2548 | /* Convert to microframes */ | |
2549 | if (urb->dev->speed == USB_SPEED_LOW || | |
2550 | urb->dev->speed == USB_SPEED_FULL) | |
2551 | ep_interval *= 8; | |
2552 | /* FIXME change this to a warning and a suggestion to use the new API | |
2553 | * to set the polling interval (once the API is added). | |
2554 | */ | |
2555 | if (xhci_interval != ep_interval) { | |
7961acd7 | 2556 | if (printk_ratelimit()) |
624defa1 SS |
2557 | dev_dbg(&urb->dev->dev, "Driver uses different interval" |
2558 | " (%d microframe%s) than xHCI " | |
2559 | "(%d microframe%s)\n", | |
2560 | ep_interval, | |
2561 | ep_interval == 1 ? "" : "s", | |
2562 | xhci_interval, | |
2563 | xhci_interval == 1 ? "" : "s"); | |
2564 | urb->interval = xhci_interval; | |
2565 | /* Convert back to frames for LS/FS devices */ | |
2566 | if (urb->dev->speed == USB_SPEED_LOW || | |
2567 | urb->dev->speed == USB_SPEED_FULL) | |
2568 | urb->interval /= 8; | |
2569 | } | |
2570 | return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index); | |
2571 | } | |
2572 | ||
04dd950d SS |
2573 | /* |
2574 | * The TD size is the number of bytes remaining in the TD (including this TRB), | |
2575 | * right shifted by 10. | |
2576 | * It must fit in bits 21:17, so it can't be bigger than 31. | |
2577 | */ | |
2578 | static u32 xhci_td_remainder(unsigned int remainder) | |
2579 | { | |
2580 | u32 max = (1 << (21 - 17 + 1)) - 1; | |
2581 | ||
2582 | if ((remainder >> 10) >= max) | |
2583 | return max << 17; | |
2584 | else | |
2585 | return (remainder >> 10) << 17; | |
2586 | } | |
2587 | ||
23e3be11 | 2588 | static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
8a96c052 SS |
2589 | struct urb *urb, int slot_id, unsigned int ep_index) |
2590 | { | |
2591 | struct xhci_ring *ep_ring; | |
2592 | unsigned int num_trbs; | |
8e51adcc | 2593 | struct urb_priv *urb_priv; |
8a96c052 SS |
2594 | struct xhci_td *td; |
2595 | struct scatterlist *sg; | |
2596 | int num_sgs; | |
2597 | int trb_buff_len, this_sg_len, running_total; | |
2598 | bool first_trb; | |
2599 | u64 addr; | |
6cc30d85 | 2600 | bool more_trbs_coming; |
8a96c052 SS |
2601 | |
2602 | struct xhci_generic_trb *start_trb; | |
2603 | int start_cycle; | |
2604 | ||
e9df17eb SS |
2605 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
2606 | if (!ep_ring) | |
2607 | return -EINVAL; | |
2608 | ||
8a96c052 SS |
2609 | num_trbs = count_sg_trbs_needed(xhci, urb); |
2610 | num_sgs = urb->num_sgs; | |
2611 | ||
23e3be11 | 2612 | trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id], |
e9df17eb | 2613 | ep_index, urb->stream_id, |
8e51adcc | 2614 | num_trbs, urb, 0, mem_flags); |
8a96c052 SS |
2615 | if (trb_buff_len < 0) |
2616 | return trb_buff_len; | |
8e51adcc AX |
2617 | |
2618 | urb_priv = urb->hcpriv; | |
2619 | td = urb_priv->td[0]; | |
2620 | ||
8a96c052 SS |
2621 | /* |
2622 | * Don't give the first TRB to the hardware (by toggling the cycle bit) | |
2623 | * until we've finished creating all the other TRBs. The ring's cycle | |
2624 | * state may change as we enqueue the other TRBs, so save it too. | |
2625 | */ | |
2626 | start_trb = &ep_ring->enqueue->generic; | |
2627 | start_cycle = ep_ring->cycle_state; | |
2628 | ||
2629 | running_total = 0; | |
2630 | /* | |
2631 | * How much data is in the first TRB? | |
2632 | * | |
2633 | * There are three forces at work for TRB buffer pointers and lengths: | |
2634 | * 1. We don't want to walk off the end of this sg-list entry buffer. | |
2635 | * 2. The transfer length that the driver requested may be smaller than | |
2636 | * the amount of memory allocated for this scatter-gather list. | |
2637 | * 3. TRBs buffers can't cross 64KB boundaries. | |
2638 | */ | |
910f8d0c | 2639 | sg = urb->sg; |
8a96c052 SS |
2640 | addr = (u64) sg_dma_address(sg); |
2641 | this_sg_len = sg_dma_len(sg); | |
2642 | trb_buff_len = TRB_MAX_BUFF_SIZE - | |
2643 | (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
2644 | trb_buff_len = min_t(int, trb_buff_len, this_sg_len); | |
2645 | if (trb_buff_len > urb->transfer_buffer_length) | |
2646 | trb_buff_len = urb->transfer_buffer_length; | |
2647 | xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n", | |
2648 | trb_buff_len); | |
2649 | ||
2650 | first_trb = true; | |
2651 | /* Queue the first TRB, even if it's zero-length */ | |
2652 | do { | |
2653 | u32 field = 0; | |
f9dc68fe | 2654 | u32 length_field = 0; |
04dd950d | 2655 | u32 remainder = 0; |
8a96c052 SS |
2656 | |
2657 | /* Don't change the cycle bit of the first TRB until later */ | |
50f7b52a | 2658 | if (first_trb) { |
8a96c052 | 2659 | first_trb = false; |
50f7b52a AX |
2660 | if (start_cycle == 0) |
2661 | field |= 0x1; | |
2662 | } else | |
8a96c052 SS |
2663 | field |= ep_ring->cycle_state; |
2664 | ||
2665 | /* Chain all the TRBs together; clear the chain bit in the last | |
2666 | * TRB to indicate it's the last TRB in the chain. | |
2667 | */ | |
2668 | if (num_trbs > 1) { | |
2669 | field |= TRB_CHAIN; | |
2670 | } else { | |
2671 | /* FIXME - add check for ZERO_PACKET flag before this */ | |
2672 | td->last_trb = ep_ring->enqueue; | |
2673 | field |= TRB_IOC; | |
2674 | } | |
2675 | xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), " | |
2676 | "64KB boundary at %#x, end dma = %#x\n", | |
2677 | (unsigned int) addr, trb_buff_len, trb_buff_len, | |
2678 | (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1), | |
2679 | (unsigned int) addr + trb_buff_len); | |
2680 | if (TRB_MAX_BUFF_SIZE - | |
2681 | (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) { | |
2682 | xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n"); | |
2683 | xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n", | |
2684 | (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1), | |
2685 | (unsigned int) addr + trb_buff_len); | |
2686 | } | |
04dd950d SS |
2687 | remainder = xhci_td_remainder(urb->transfer_buffer_length - |
2688 | running_total) ; | |
f9dc68fe | 2689 | length_field = TRB_LEN(trb_buff_len) | |
04dd950d | 2690 | remainder | |
f9dc68fe | 2691 | TRB_INTR_TARGET(0); |
6cc30d85 SS |
2692 | if (num_trbs > 1) |
2693 | more_trbs_coming = true; | |
2694 | else | |
2695 | more_trbs_coming = false; | |
2696 | queue_trb(xhci, ep_ring, false, more_trbs_coming, | |
8e595a5d SS |
2697 | lower_32_bits(addr), |
2698 | upper_32_bits(addr), | |
f9dc68fe | 2699 | length_field, |
8a96c052 SS |
2700 | /* We always want to know if the TRB was short, |
2701 | * or we won't get an event when it completes. | |
2702 | * (Unless we use event data TRBs, which are a | |
2703 | * waste of space and HC resources.) | |
2704 | */ | |
2705 | field | TRB_ISP | TRB_TYPE(TRB_NORMAL)); | |
2706 | --num_trbs; | |
2707 | running_total += trb_buff_len; | |
2708 | ||
2709 | /* Calculate length for next transfer -- | |
2710 | * Are we done queueing all the TRBs for this sg entry? | |
2711 | */ | |
2712 | this_sg_len -= trb_buff_len; | |
2713 | if (this_sg_len == 0) { | |
2714 | --num_sgs; | |
2715 | if (num_sgs == 0) | |
2716 | break; | |
2717 | sg = sg_next(sg); | |
2718 | addr = (u64) sg_dma_address(sg); | |
2719 | this_sg_len = sg_dma_len(sg); | |
2720 | } else { | |
2721 | addr += trb_buff_len; | |
2722 | } | |
2723 | ||
2724 | trb_buff_len = TRB_MAX_BUFF_SIZE - | |
2725 | (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
2726 | trb_buff_len = min_t(int, trb_buff_len, this_sg_len); | |
2727 | if (running_total + trb_buff_len > urb->transfer_buffer_length) | |
2728 | trb_buff_len = | |
2729 | urb->transfer_buffer_length - running_total; | |
2730 | } while (running_total < urb->transfer_buffer_length); | |
2731 | ||
2732 | check_trb_math(urb, num_trbs, running_total); | |
e9df17eb | 2733 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
e1eab2e0 | 2734 | start_cycle, start_trb); |
8a96c052 SS |
2735 | return 0; |
2736 | } | |
2737 | ||
b10de142 | 2738 | /* This is very similar to what ehci-q.c qtd_fill() does */ |
23e3be11 | 2739 | int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
b10de142 SS |
2740 | struct urb *urb, int slot_id, unsigned int ep_index) |
2741 | { | |
2742 | struct xhci_ring *ep_ring; | |
8e51adcc | 2743 | struct urb_priv *urb_priv; |
b10de142 SS |
2744 | struct xhci_td *td; |
2745 | int num_trbs; | |
2746 | struct xhci_generic_trb *start_trb; | |
2747 | bool first_trb; | |
6cc30d85 | 2748 | bool more_trbs_coming; |
b10de142 | 2749 | int start_cycle; |
f9dc68fe | 2750 | u32 field, length_field; |
b10de142 SS |
2751 | |
2752 | int running_total, trb_buff_len, ret; | |
2753 | u64 addr; | |
2754 | ||
ff9c895f | 2755 | if (urb->num_sgs) |
8a96c052 SS |
2756 | return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index); |
2757 | ||
e9df17eb SS |
2758 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
2759 | if (!ep_ring) | |
2760 | return -EINVAL; | |
b10de142 SS |
2761 | |
2762 | num_trbs = 0; | |
2763 | /* How much data is (potentially) left before the 64KB boundary? */ | |
2764 | running_total = TRB_MAX_BUFF_SIZE - | |
2765 | (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
2766 | ||
2767 | /* If there's some data on this 64KB chunk, or we have to send a | |
2768 | * zero-length transfer, we need at least one TRB | |
2769 | */ | |
2770 | if (running_total != 0 || urb->transfer_buffer_length == 0) | |
2771 | num_trbs++; | |
2772 | /* How many more 64KB chunks to transfer, how many more TRBs? */ | |
2773 | while (running_total < urb->transfer_buffer_length) { | |
2774 | num_trbs++; | |
2775 | running_total += TRB_MAX_BUFF_SIZE; | |
2776 | } | |
2777 | /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */ | |
2778 | ||
2779 | if (!in_interrupt()) | |
f2c565e2 AX |
2780 | xhci_dbg(xhci, "ep %#x - urb len = %#x (%d), " |
2781 | "addr = %#llx, num_trbs = %d\n", | |
b10de142 | 2782 | urb->ep->desc.bEndpointAddress, |
8a96c052 SS |
2783 | urb->transfer_buffer_length, |
2784 | urb->transfer_buffer_length, | |
700e2052 | 2785 | (unsigned long long)urb->transfer_dma, |
b10de142 | 2786 | num_trbs); |
8a96c052 | 2787 | |
e9df17eb SS |
2788 | ret = prepare_transfer(xhci, xhci->devs[slot_id], |
2789 | ep_index, urb->stream_id, | |
8e51adcc | 2790 | num_trbs, urb, 0, mem_flags); |
b10de142 SS |
2791 | if (ret < 0) |
2792 | return ret; | |
2793 | ||
8e51adcc AX |
2794 | urb_priv = urb->hcpriv; |
2795 | td = urb_priv->td[0]; | |
2796 | ||
b10de142 SS |
2797 | /* |
2798 | * Don't give the first TRB to the hardware (by toggling the cycle bit) | |
2799 | * until we've finished creating all the other TRBs. The ring's cycle | |
2800 | * state may change as we enqueue the other TRBs, so save it too. | |
2801 | */ | |
2802 | start_trb = &ep_ring->enqueue->generic; | |
2803 | start_cycle = ep_ring->cycle_state; | |
2804 | ||
2805 | running_total = 0; | |
2806 | /* How much data is in the first TRB? */ | |
2807 | addr = (u64) urb->transfer_dma; | |
2808 | trb_buff_len = TRB_MAX_BUFF_SIZE - | |
2809 | (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
2810 | if (urb->transfer_buffer_length < trb_buff_len) | |
2811 | trb_buff_len = urb->transfer_buffer_length; | |
2812 | ||
2813 | first_trb = true; | |
2814 | ||
2815 | /* Queue the first TRB, even if it's zero-length */ | |
2816 | do { | |
04dd950d | 2817 | u32 remainder = 0; |
b10de142 SS |
2818 | field = 0; |
2819 | ||
2820 | /* Don't change the cycle bit of the first TRB until later */ | |
50f7b52a | 2821 | if (first_trb) { |
b10de142 | 2822 | first_trb = false; |
50f7b52a AX |
2823 | if (start_cycle == 0) |
2824 | field |= 0x1; | |
2825 | } else | |
b10de142 SS |
2826 | field |= ep_ring->cycle_state; |
2827 | ||
2828 | /* Chain all the TRBs together; clear the chain bit in the last | |
2829 | * TRB to indicate it's the last TRB in the chain. | |
2830 | */ | |
2831 | if (num_trbs > 1) { | |
2832 | field |= TRB_CHAIN; | |
2833 | } else { | |
2834 | /* FIXME - add check for ZERO_PACKET flag before this */ | |
2835 | td->last_trb = ep_ring->enqueue; | |
2836 | field |= TRB_IOC; | |
2837 | } | |
04dd950d SS |
2838 | remainder = xhci_td_remainder(urb->transfer_buffer_length - |
2839 | running_total); | |
f9dc68fe | 2840 | length_field = TRB_LEN(trb_buff_len) | |
04dd950d | 2841 | remainder | |
f9dc68fe | 2842 | TRB_INTR_TARGET(0); |
6cc30d85 SS |
2843 | if (num_trbs > 1) |
2844 | more_trbs_coming = true; | |
2845 | else | |
2846 | more_trbs_coming = false; | |
2847 | queue_trb(xhci, ep_ring, false, more_trbs_coming, | |
8e595a5d SS |
2848 | lower_32_bits(addr), |
2849 | upper_32_bits(addr), | |
f9dc68fe | 2850 | length_field, |
b10de142 SS |
2851 | /* We always want to know if the TRB was short, |
2852 | * or we won't get an event when it completes. | |
2853 | * (Unless we use event data TRBs, which are a | |
2854 | * waste of space and HC resources.) | |
2855 | */ | |
2856 | field | TRB_ISP | TRB_TYPE(TRB_NORMAL)); | |
2857 | --num_trbs; | |
2858 | running_total += trb_buff_len; | |
2859 | ||
2860 | /* Calculate length for next transfer */ | |
2861 | addr += trb_buff_len; | |
2862 | trb_buff_len = urb->transfer_buffer_length - running_total; | |
2863 | if (trb_buff_len > TRB_MAX_BUFF_SIZE) | |
2864 | trb_buff_len = TRB_MAX_BUFF_SIZE; | |
2865 | } while (running_total < urb->transfer_buffer_length); | |
2866 | ||
8a96c052 | 2867 | check_trb_math(urb, num_trbs, running_total); |
e9df17eb | 2868 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
e1eab2e0 | 2869 | start_cycle, start_trb); |
b10de142 SS |
2870 | return 0; |
2871 | } | |
2872 | ||
d0e96f5a | 2873 | /* Caller must have locked xhci->lock */ |
23e3be11 | 2874 | int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
d0e96f5a SS |
2875 | struct urb *urb, int slot_id, unsigned int ep_index) |
2876 | { | |
2877 | struct xhci_ring *ep_ring; | |
2878 | int num_trbs; | |
2879 | int ret; | |
2880 | struct usb_ctrlrequest *setup; | |
2881 | struct xhci_generic_trb *start_trb; | |
2882 | int start_cycle; | |
f9dc68fe | 2883 | u32 field, length_field; |
8e51adcc | 2884 | struct urb_priv *urb_priv; |
d0e96f5a SS |
2885 | struct xhci_td *td; |
2886 | ||
e9df17eb SS |
2887 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
2888 | if (!ep_ring) | |
2889 | return -EINVAL; | |
d0e96f5a SS |
2890 | |
2891 | /* | |
2892 | * Need to copy setup packet into setup TRB, so we can't use the setup | |
2893 | * DMA address. | |
2894 | */ | |
2895 | if (!urb->setup_packet) | |
2896 | return -EINVAL; | |
2897 | ||
2898 | if (!in_interrupt()) | |
2899 | xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n", | |
2900 | slot_id, ep_index); | |
2901 | /* 1 TRB for setup, 1 for status */ | |
2902 | num_trbs = 2; | |
2903 | /* | |
2904 | * Don't need to check if we need additional event data and normal TRBs, | |
2905 | * since data in control transfers will never get bigger than 16MB | |
2906 | * XXX: can we get a buffer that crosses 64KB boundaries? | |
2907 | */ | |
2908 | if (urb->transfer_buffer_length > 0) | |
2909 | num_trbs++; | |
e9df17eb SS |
2910 | ret = prepare_transfer(xhci, xhci->devs[slot_id], |
2911 | ep_index, urb->stream_id, | |
8e51adcc | 2912 | num_trbs, urb, 0, mem_flags); |
d0e96f5a SS |
2913 | if (ret < 0) |
2914 | return ret; | |
2915 | ||
8e51adcc AX |
2916 | urb_priv = urb->hcpriv; |
2917 | td = urb_priv->td[0]; | |
2918 | ||
d0e96f5a SS |
2919 | /* |
2920 | * Don't give the first TRB to the hardware (by toggling the cycle bit) | |
2921 | * until we've finished creating all the other TRBs. The ring's cycle | |
2922 | * state may change as we enqueue the other TRBs, so save it too. | |
2923 | */ | |
2924 | start_trb = &ep_ring->enqueue->generic; | |
2925 | start_cycle = ep_ring->cycle_state; | |
2926 | ||
2927 | /* Queue setup TRB - see section 6.4.1.2.1 */ | |
2928 | /* FIXME better way to translate setup_packet into two u32 fields? */ | |
2929 | setup = (struct usb_ctrlrequest *) urb->setup_packet; | |
50f7b52a AX |
2930 | field = 0; |
2931 | field |= TRB_IDT | TRB_TYPE(TRB_SETUP); | |
2932 | if (start_cycle == 0) | |
2933 | field |= 0x1; | |
6cc30d85 | 2934 | queue_trb(xhci, ep_ring, false, true, |
d0e96f5a SS |
2935 | /* FIXME endianness is probably going to bite my ass here. */ |
2936 | setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16, | |
2937 | setup->wIndex | setup->wLength << 16, | |
2938 | TRB_LEN(8) | TRB_INTR_TARGET(0), | |
2939 | /* Immediate data in pointer */ | |
50f7b52a | 2940 | field); |
d0e96f5a SS |
2941 | |
2942 | /* If there's data, queue data TRBs */ | |
2943 | field = 0; | |
f9dc68fe | 2944 | length_field = TRB_LEN(urb->transfer_buffer_length) | |
04dd950d | 2945 | xhci_td_remainder(urb->transfer_buffer_length) | |
f9dc68fe | 2946 | TRB_INTR_TARGET(0); |
d0e96f5a SS |
2947 | if (urb->transfer_buffer_length > 0) { |
2948 | if (setup->bRequestType & USB_DIR_IN) | |
2949 | field |= TRB_DIR_IN; | |
6cc30d85 | 2950 | queue_trb(xhci, ep_ring, false, true, |
d0e96f5a SS |
2951 | lower_32_bits(urb->transfer_dma), |
2952 | upper_32_bits(urb->transfer_dma), | |
f9dc68fe | 2953 | length_field, |
d0e96f5a SS |
2954 | /* Event on short tx */ |
2955 | field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state); | |
2956 | } | |
2957 | ||
2958 | /* Save the DMA address of the last TRB in the TD */ | |
2959 | td->last_trb = ep_ring->enqueue; | |
2960 | ||
2961 | /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ | |
2962 | /* If the device sent data, the status stage is an OUT transfer */ | |
2963 | if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) | |
2964 | field = 0; | |
2965 | else | |
2966 | field = TRB_DIR_IN; | |
6cc30d85 | 2967 | queue_trb(xhci, ep_ring, false, false, |
d0e96f5a SS |
2968 | 0, |
2969 | 0, | |
2970 | TRB_INTR_TARGET(0), | |
2971 | /* Event on completion */ | |
2972 | field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); | |
2973 | ||
e9df17eb | 2974 | giveback_first_trb(xhci, slot_id, ep_index, 0, |
e1eab2e0 | 2975 | start_cycle, start_trb); |
d0e96f5a SS |
2976 | return 0; |
2977 | } | |
2978 | ||
04e51901 AX |
2979 | static int count_isoc_trbs_needed(struct xhci_hcd *xhci, |
2980 | struct urb *urb, int i) | |
2981 | { | |
2982 | int num_trbs = 0; | |
2983 | u64 addr, td_len, running_total; | |
2984 | ||
2985 | addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); | |
2986 | td_len = urb->iso_frame_desc[i].length; | |
2987 | ||
2988 | running_total = TRB_MAX_BUFF_SIZE - | |
2989 | (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
2990 | if (running_total != 0) | |
2991 | num_trbs++; | |
2992 | ||
2993 | while (running_total < td_len) { | |
2994 | num_trbs++; | |
2995 | running_total += TRB_MAX_BUFF_SIZE; | |
2996 | } | |
2997 | ||
2998 | return num_trbs; | |
2999 | } | |
3000 | ||
3001 | /* This is for isoc transfer */ | |
3002 | static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags, | |
3003 | struct urb *urb, int slot_id, unsigned int ep_index) | |
3004 | { | |
3005 | struct xhci_ring *ep_ring; | |
3006 | struct urb_priv *urb_priv; | |
3007 | struct xhci_td *td; | |
3008 | int num_tds, trbs_per_td; | |
3009 | struct xhci_generic_trb *start_trb; | |
3010 | bool first_trb; | |
3011 | int start_cycle; | |
3012 | u32 field, length_field; | |
3013 | int running_total, trb_buff_len, td_len, td_remain_len, ret; | |
3014 | u64 start_addr, addr; | |
3015 | int i, j; | |
47cbf692 | 3016 | bool more_trbs_coming; |
04e51901 AX |
3017 | |
3018 | ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; | |
3019 | ||
3020 | num_tds = urb->number_of_packets; | |
3021 | if (num_tds < 1) { | |
3022 | xhci_dbg(xhci, "Isoc URB with zero packets?\n"); | |
3023 | return -EINVAL; | |
3024 | } | |
3025 | ||
3026 | if (!in_interrupt()) | |
f2c565e2 | 3027 | xhci_dbg(xhci, "ep %#x - urb len = %#x (%d)," |
04e51901 AX |
3028 | " addr = %#llx, num_tds = %d\n", |
3029 | urb->ep->desc.bEndpointAddress, | |
3030 | urb->transfer_buffer_length, | |
3031 | urb->transfer_buffer_length, | |
3032 | (unsigned long long)urb->transfer_dma, | |
3033 | num_tds); | |
3034 | ||
3035 | start_addr = (u64) urb->transfer_dma; | |
3036 | start_trb = &ep_ring->enqueue->generic; | |
3037 | start_cycle = ep_ring->cycle_state; | |
3038 | ||
3039 | /* Queue the first TRB, even if it's zero-length */ | |
3040 | for (i = 0; i < num_tds; i++) { | |
3041 | first_trb = true; | |
3042 | ||
3043 | running_total = 0; | |
3044 | addr = start_addr + urb->iso_frame_desc[i].offset; | |
3045 | td_len = urb->iso_frame_desc[i].length; | |
3046 | td_remain_len = td_len; | |
3047 | ||
3048 | trbs_per_td = count_isoc_trbs_needed(xhci, urb, i); | |
3049 | ||
3050 | ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, | |
3051 | urb->stream_id, trbs_per_td, urb, i, mem_flags); | |
3052 | if (ret < 0) | |
3053 | return ret; | |
3054 | ||
3055 | urb_priv = urb->hcpriv; | |
3056 | td = urb_priv->td[i]; | |
3057 | ||
3058 | for (j = 0; j < trbs_per_td; j++) { | |
3059 | u32 remainder = 0; | |
3060 | field = 0; | |
3061 | ||
3062 | if (first_trb) { | |
3063 | /* Queue the isoc TRB */ | |
3064 | field |= TRB_TYPE(TRB_ISOC); | |
3065 | /* Assume URB_ISO_ASAP is set */ | |
3066 | field |= TRB_SIA; | |
50f7b52a AX |
3067 | if (i == 0) { |
3068 | if (start_cycle == 0) | |
3069 | field |= 0x1; | |
3070 | } else | |
04e51901 AX |
3071 | field |= ep_ring->cycle_state; |
3072 | first_trb = false; | |
3073 | } else { | |
3074 | /* Queue other normal TRBs */ | |
3075 | field |= TRB_TYPE(TRB_NORMAL); | |
3076 | field |= ep_ring->cycle_state; | |
3077 | } | |
3078 | ||
3079 | /* Chain all the TRBs together; clear the chain bit in | |
3080 | * the last TRB to indicate it's the last TRB in the | |
3081 | * chain. | |
3082 | */ | |
3083 | if (j < trbs_per_td - 1) { | |
3084 | field |= TRB_CHAIN; | |
47cbf692 | 3085 | more_trbs_coming = true; |
04e51901 AX |
3086 | } else { |
3087 | td->last_trb = ep_ring->enqueue; | |
3088 | field |= TRB_IOC; | |
47cbf692 | 3089 | more_trbs_coming = false; |
04e51901 AX |
3090 | } |
3091 | ||
3092 | /* Calculate TRB length */ | |
3093 | trb_buff_len = TRB_MAX_BUFF_SIZE - | |
3094 | (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); | |
3095 | if (trb_buff_len > td_remain_len) | |
3096 | trb_buff_len = td_remain_len; | |
3097 | ||
3098 | remainder = xhci_td_remainder(td_len - running_total); | |
3099 | length_field = TRB_LEN(trb_buff_len) | | |
3100 | remainder | | |
3101 | TRB_INTR_TARGET(0); | |
47cbf692 | 3102 | queue_trb(xhci, ep_ring, false, more_trbs_coming, |
04e51901 AX |
3103 | lower_32_bits(addr), |
3104 | upper_32_bits(addr), | |
3105 | length_field, | |
3106 | /* We always want to know if the TRB was short, | |
3107 | * or we won't get an event when it completes. | |
3108 | * (Unless we use event data TRBs, which are a | |
3109 | * waste of space and HC resources.) | |
3110 | */ | |
3111 | field | TRB_ISP); | |
3112 | running_total += trb_buff_len; | |
3113 | ||
3114 | addr += trb_buff_len; | |
3115 | td_remain_len -= trb_buff_len; | |
3116 | } | |
3117 | ||
3118 | /* Check TD length */ | |
3119 | if (running_total != td_len) { | |
3120 | xhci_err(xhci, "ISOC TD length unmatch\n"); | |
3121 | return -EINVAL; | |
3122 | } | |
3123 | } | |
3124 | ||
e1eab2e0 AX |
3125 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
3126 | start_cycle, start_trb); | |
04e51901 AX |
3127 | return 0; |
3128 | } | |
3129 | ||
3130 | /* | |
3131 | * Check transfer ring to guarantee there is enough room for the urb. | |
3132 | * Update ISO URB start_frame and interval. | |
3133 | * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to | |
3134 | * update the urb->start_frame by now. | |
3135 | * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input. | |
3136 | */ | |
3137 | int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, | |
3138 | struct urb *urb, int slot_id, unsigned int ep_index) | |
3139 | { | |
3140 | struct xhci_virt_device *xdev; | |
3141 | struct xhci_ring *ep_ring; | |
3142 | struct xhci_ep_ctx *ep_ctx; | |
3143 | int start_frame; | |
3144 | int xhci_interval; | |
3145 | int ep_interval; | |
3146 | int num_tds, num_trbs, i; | |
3147 | int ret; | |
3148 | ||
3149 | xdev = xhci->devs[slot_id]; | |
3150 | ep_ring = xdev->eps[ep_index].ring; | |
3151 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); | |
3152 | ||
3153 | num_trbs = 0; | |
3154 | num_tds = urb->number_of_packets; | |
3155 | for (i = 0; i < num_tds; i++) | |
3156 | num_trbs += count_isoc_trbs_needed(xhci, urb, i); | |
3157 | ||
3158 | /* Check the ring to guarantee there is enough room for the whole urb. | |
3159 | * Do not insert any td of the urb to the ring if the check failed. | |
3160 | */ | |
3161 | ret = prepare_ring(xhci, ep_ring, ep_ctx->ep_info & EP_STATE_MASK, | |
3162 | num_trbs, mem_flags); | |
3163 | if (ret) | |
3164 | return ret; | |
3165 | ||
3166 | start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index); | |
3167 | start_frame &= 0x3fff; | |
3168 | ||
3169 | urb->start_frame = start_frame; | |
3170 | if (urb->dev->speed == USB_SPEED_LOW || | |
3171 | urb->dev->speed == USB_SPEED_FULL) | |
3172 | urb->start_frame >>= 3; | |
3173 | ||
3174 | xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info); | |
3175 | ep_interval = urb->interval; | |
3176 | /* Convert to microframes */ | |
3177 | if (urb->dev->speed == USB_SPEED_LOW || | |
3178 | urb->dev->speed == USB_SPEED_FULL) | |
3179 | ep_interval *= 8; | |
3180 | /* FIXME change this to a warning and a suggestion to use the new API | |
3181 | * to set the polling interval (once the API is added). | |
3182 | */ | |
3183 | if (xhci_interval != ep_interval) { | |
7961acd7 | 3184 | if (printk_ratelimit()) |
04e51901 AX |
3185 | dev_dbg(&urb->dev->dev, "Driver uses different interval" |
3186 | " (%d microframe%s) than xHCI " | |
3187 | "(%d microframe%s)\n", | |
3188 | ep_interval, | |
3189 | ep_interval == 1 ? "" : "s", | |
3190 | xhci_interval, | |
3191 | xhci_interval == 1 ? "" : "s"); | |
3192 | urb->interval = xhci_interval; | |
3193 | /* Convert back to frames for LS/FS devices */ | |
3194 | if (urb->dev->speed == USB_SPEED_LOW || | |
3195 | urb->dev->speed == USB_SPEED_FULL) | |
3196 | urb->interval /= 8; | |
3197 | } | |
3198 | return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index); | |
3199 | } | |
3200 | ||
d0e96f5a SS |
3201 | /**** Command Ring Operations ****/ |
3202 | ||
913a8a34 SS |
3203 | /* Generic function for queueing a command TRB on the command ring. |
3204 | * Check to make sure there's room on the command ring for one command TRB. | |
3205 | * Also check that there's room reserved for commands that must not fail. | |
3206 | * If this is a command that must not fail, meaning command_must_succeed = TRUE, | |
3207 | * then only check for the number of reserved spots. | |
3208 | * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB | |
3209 | * because the command event handler may want to resubmit a failed command. | |
3210 | */ | |
3211 | static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, | |
3212 | u32 field3, u32 field4, bool command_must_succeed) | |
7f84eef0 | 3213 | { |
913a8a34 | 3214 | int reserved_trbs = xhci->cmd_ring_reserved_trbs; |
d1dc908a SS |
3215 | int ret; |
3216 | ||
913a8a34 SS |
3217 | if (!command_must_succeed) |
3218 | reserved_trbs++; | |
3219 | ||
d1dc908a SS |
3220 | ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, |
3221 | reserved_trbs, GFP_ATOMIC); | |
3222 | if (ret < 0) { | |
3223 | xhci_err(xhci, "ERR: No room for command on command ring\n"); | |
913a8a34 SS |
3224 | if (command_must_succeed) |
3225 | xhci_err(xhci, "ERR: Reserved TRB counting for " | |
3226 | "unfailable commands failed.\n"); | |
d1dc908a | 3227 | return ret; |
7f84eef0 | 3228 | } |
6cc30d85 | 3229 | queue_trb(xhci, xhci->cmd_ring, false, false, field1, field2, field3, |
7f84eef0 SS |
3230 | field4 | xhci->cmd_ring->cycle_state); |
3231 | return 0; | |
3232 | } | |
3233 | ||
3ffbba95 | 3234 | /* Queue a slot enable or disable request on the command ring */ |
23e3be11 | 3235 | int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id) |
3ffbba95 SS |
3236 | { |
3237 | return queue_command(xhci, 0, 0, 0, | |
913a8a34 | 3238 | TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false); |
3ffbba95 SS |
3239 | } |
3240 | ||
3241 | /* Queue an address device command TRB */ | |
23e3be11 SS |
3242 | int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, |
3243 | u32 slot_id) | |
3ffbba95 | 3244 | { |
8e595a5d SS |
3245 | return queue_command(xhci, lower_32_bits(in_ctx_ptr), |
3246 | upper_32_bits(in_ctx_ptr), 0, | |
913a8a34 | 3247 | TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id), |
2a8f82c4 SS |
3248 | false); |
3249 | } | |
3250 | ||
0238634d SS |
3251 | int xhci_queue_vendor_command(struct xhci_hcd *xhci, |
3252 | u32 field1, u32 field2, u32 field3, u32 field4) | |
3253 | { | |
3254 | return queue_command(xhci, field1, field2, field3, field4, false); | |
3255 | } | |
3256 | ||
2a8f82c4 SS |
3257 | /* Queue a reset device command TRB */ |
3258 | int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id) | |
3259 | { | |
3260 | return queue_command(xhci, 0, 0, 0, | |
3261 | TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id), | |
913a8a34 | 3262 | false); |
3ffbba95 | 3263 | } |
f94e0186 SS |
3264 | |
3265 | /* Queue a configure endpoint command TRB */ | |
23e3be11 | 3266 | int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, |
913a8a34 | 3267 | u32 slot_id, bool command_must_succeed) |
f94e0186 | 3268 | { |
8e595a5d SS |
3269 | return queue_command(xhci, lower_32_bits(in_ctx_ptr), |
3270 | upper_32_bits(in_ctx_ptr), 0, | |
913a8a34 SS |
3271 | TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id), |
3272 | command_must_succeed); | |
f94e0186 | 3273 | } |
ae636747 | 3274 | |
f2217e8e SS |
3275 | /* Queue an evaluate context command TRB */ |
3276 | int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, | |
3277 | u32 slot_id) | |
3278 | { | |
3279 | return queue_command(xhci, lower_32_bits(in_ctx_ptr), | |
3280 | upper_32_bits(in_ctx_ptr), 0, | |
913a8a34 SS |
3281 | TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id), |
3282 | false); | |
f2217e8e SS |
3283 | } |
3284 | ||
be88fe4f AX |
3285 | /* |
3286 | * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop | |
3287 | * activity on an endpoint that is about to be suspended. | |
3288 | */ | |
23e3be11 | 3289 | int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id, |
be88fe4f | 3290 | unsigned int ep_index, int suspend) |
ae636747 SS |
3291 | { |
3292 | u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); | |
3293 | u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); | |
3294 | u32 type = TRB_TYPE(TRB_STOP_RING); | |
be88fe4f | 3295 | u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend); |
ae636747 SS |
3296 | |
3297 | return queue_command(xhci, 0, 0, 0, | |
be88fe4f | 3298 | trb_slot_id | trb_ep_index | type | trb_suspend, false); |
ae636747 SS |
3299 | } |
3300 | ||
3301 | /* Set Transfer Ring Dequeue Pointer command. | |
3302 | * This should not be used for endpoints that have streams enabled. | |
3303 | */ | |
3304 | static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id, | |
e9df17eb SS |
3305 | unsigned int ep_index, unsigned int stream_id, |
3306 | struct xhci_segment *deq_seg, | |
ae636747 SS |
3307 | union xhci_trb *deq_ptr, u32 cycle_state) |
3308 | { | |
3309 | dma_addr_t addr; | |
3310 | u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); | |
3311 | u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); | |
e9df17eb | 3312 | u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id); |
ae636747 | 3313 | u32 type = TRB_TYPE(TRB_SET_DEQ); |
bf161e85 | 3314 | struct xhci_virt_ep *ep; |
ae636747 | 3315 | |
23e3be11 | 3316 | addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr); |
c92bcfa7 | 3317 | if (addr == 0) { |
ae636747 | 3318 | xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n"); |
700e2052 GKH |
3319 | xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n", |
3320 | deq_seg, deq_ptr); | |
c92bcfa7 SS |
3321 | return 0; |
3322 | } | |
bf161e85 SS |
3323 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
3324 | if ((ep->ep_state & SET_DEQ_PENDING)) { | |
3325 | xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n"); | |
3326 | xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n"); | |
3327 | return 0; | |
3328 | } | |
3329 | ep->queued_deq_seg = deq_seg; | |
3330 | ep->queued_deq_ptr = deq_ptr; | |
8e595a5d | 3331 | return queue_command(xhci, lower_32_bits(addr) | cycle_state, |
e9df17eb | 3332 | upper_32_bits(addr), trb_stream_id, |
913a8a34 | 3333 | trb_slot_id | trb_ep_index | type, false); |
ae636747 | 3334 | } |
a1587d97 SS |
3335 | |
3336 | int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id, | |
3337 | unsigned int ep_index) | |
3338 | { | |
3339 | u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); | |
3340 | u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); | |
3341 | u32 type = TRB_TYPE(TRB_RESET_EP); | |
3342 | ||
913a8a34 SS |
3343 | return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type, |
3344 | false); | |
a1587d97 | 3345 | } |