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 | ||
67 | #include "xhci.h" | |
68 | ||
69 | /* | |
70 | * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA | |
71 | * address of the TRB. | |
72 | */ | |
73 | dma_addr_t trb_virt_to_dma(struct xhci_segment *seg, | |
74 | union xhci_trb *trb) | |
75 | { | |
76 | unsigned int offset; | |
77 | ||
78 | if (!seg || !trb || (void *) trb < (void *) seg->trbs) | |
79 | return 0; | |
80 | /* offset in bytes, since these are byte-addressable */ | |
81 | offset = (unsigned int) trb - (unsigned int) seg->trbs; | |
82 | /* SEGMENT_SIZE in bytes, trbs are 16-byte aligned */ | |
83 | if (offset > SEGMENT_SIZE || (offset % sizeof(*trb)) != 0) | |
84 | return 0; | |
85 | return seg->dma + offset; | |
86 | } | |
87 | ||
88 | /* Does this link TRB point to the first segment in a ring, | |
89 | * or was the previous TRB the last TRB on the last segment in the ERST? | |
90 | */ | |
91 | static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
92 | struct xhci_segment *seg, union xhci_trb *trb) | |
93 | { | |
94 | if (ring == xhci->event_ring) | |
95 | return (trb == &seg->trbs[TRBS_PER_SEGMENT]) && | |
96 | (seg->next == xhci->event_ring->first_seg); | |
97 | else | |
98 | return trb->link.control & LINK_TOGGLE; | |
99 | } | |
100 | ||
101 | /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring | |
102 | * segment? I.e. would the updated event TRB pointer step off the end of the | |
103 | * event seg? | |
104 | */ | |
105 | static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
106 | struct xhci_segment *seg, union xhci_trb *trb) | |
107 | { | |
108 | if (ring == xhci->event_ring) | |
109 | return trb == &seg->trbs[TRBS_PER_SEGMENT]; | |
110 | else | |
111 | return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK); | |
112 | } | |
113 | ||
114 | /* | |
115 | * See Cycle bit rules. SW is the consumer for the event ring only. | |
116 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. | |
117 | */ | |
118 | static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer) | |
119 | { | |
120 | union xhci_trb *next = ++(ring->dequeue); | |
121 | ||
122 | ring->deq_updates++; | |
123 | /* Update the dequeue pointer further if that was a link TRB or we're at | |
124 | * the end of an event ring segment (which doesn't have link TRBS) | |
125 | */ | |
126 | while (last_trb(xhci, ring, ring->deq_seg, next)) { | |
127 | if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) { | |
128 | ring->cycle_state = (ring->cycle_state ? 0 : 1); | |
129 | if (!in_interrupt()) | |
130 | xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n", | |
131 | (unsigned int) ring, | |
132 | (unsigned int) ring->cycle_state); | |
133 | } | |
134 | ring->deq_seg = ring->deq_seg->next; | |
135 | ring->dequeue = ring->deq_seg->trbs; | |
136 | next = ring->dequeue; | |
137 | } | |
138 | } | |
139 | ||
140 | /* | |
141 | * See Cycle bit rules. SW is the consumer for the event ring only. | |
142 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. | |
143 | * | |
144 | * If we've just enqueued a TRB that is in the middle of a TD (meaning the | |
145 | * chain bit is set), then set the chain bit in all the following link TRBs. | |
146 | * If we've enqueued the last TRB in a TD, make sure the following link TRBs | |
147 | * have their chain bit cleared (so that each Link TRB is a separate TD). | |
148 | * | |
149 | * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit | |
150 | * set, but other sections talk about dealing with the chain bit set. | |
151 | * Assume section 6.4.4.1 is wrong, and the chain bit can be set in a Link TRB. | |
152 | */ | |
153 | static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer) | |
154 | { | |
155 | u32 chain; | |
156 | union xhci_trb *next; | |
157 | ||
158 | chain = ring->enqueue->generic.field[3] & TRB_CHAIN; | |
159 | next = ++(ring->enqueue); | |
160 | ||
161 | ring->enq_updates++; | |
162 | /* Update the dequeue pointer further if that was a link TRB or we're at | |
163 | * the end of an event ring segment (which doesn't have link TRBS) | |
164 | */ | |
165 | while (last_trb(xhci, ring, ring->enq_seg, next)) { | |
166 | if (!consumer) { | |
167 | if (ring != xhci->event_ring) { | |
168 | /* Give this link TRB to the hardware */ | |
169 | if (next->link.control & TRB_CYCLE) | |
170 | next->link.control &= (u32) ~TRB_CYCLE; | |
171 | else | |
172 | next->link.control |= (u32) TRB_CYCLE; | |
173 | next->link.control &= TRB_CHAIN; | |
174 | next->link.control |= chain; | |
175 | } | |
176 | /* Toggle the cycle bit after the last ring segment. */ | |
177 | if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { | |
178 | ring->cycle_state = (ring->cycle_state ? 0 : 1); | |
179 | if (!in_interrupt()) | |
180 | xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n", | |
181 | (unsigned int) ring, | |
182 | (unsigned int) ring->cycle_state); | |
183 | } | |
184 | } | |
185 | ring->enq_seg = ring->enq_seg->next; | |
186 | ring->enqueue = ring->enq_seg->trbs; | |
187 | next = ring->enqueue; | |
188 | } | |
189 | } | |
190 | ||
191 | /* | |
192 | * Check to see if there's room to enqueue num_trbs on the ring. See rules | |
193 | * above. | |
194 | * FIXME: this would be simpler and faster if we just kept track of the number | |
195 | * of free TRBs in a ring. | |
196 | */ | |
197 | static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
198 | unsigned int num_trbs) | |
199 | { | |
200 | int i; | |
201 | union xhci_trb *enq = ring->enqueue; | |
202 | struct xhci_segment *enq_seg = ring->enq_seg; | |
203 | ||
204 | /* Check if ring is empty */ | |
205 | if (enq == ring->dequeue) | |
206 | return 1; | |
207 | /* Make sure there's an extra empty TRB available */ | |
208 | for (i = 0; i <= num_trbs; ++i) { | |
209 | if (enq == ring->dequeue) | |
210 | return 0; | |
211 | enq++; | |
212 | while (last_trb(xhci, ring, enq_seg, enq)) { | |
213 | enq_seg = enq_seg->next; | |
214 | enq = enq_seg->trbs; | |
215 | } | |
216 | } | |
217 | return 1; | |
218 | } | |
219 | ||
220 | void set_hc_event_deq(struct xhci_hcd *xhci) | |
221 | { | |
222 | u32 temp; | |
223 | dma_addr_t deq; | |
224 | ||
225 | deq = trb_virt_to_dma(xhci->event_ring->deq_seg, | |
226 | xhci->event_ring->dequeue); | |
227 | if (deq == 0 && !in_interrupt()) | |
228 | xhci_warn(xhci, "WARN something wrong with SW event ring " | |
229 | "dequeue ptr.\n"); | |
230 | /* Update HC event ring dequeue pointer */ | |
231 | temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]); | |
232 | temp &= ERST_PTR_MASK; | |
233 | if (!in_interrupt()) | |
234 | xhci_dbg(xhci, "// Write event ring dequeue pointer\n"); | |
235 | xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]); | |
236 | xhci_writel(xhci, (deq & ~ERST_PTR_MASK) | temp, | |
237 | &xhci->ir_set->erst_dequeue[0]); | |
238 | } | |
239 | ||
240 | /* Ring the host controller doorbell after placing a command on the ring */ | |
241 | void ring_cmd_db(struct xhci_hcd *xhci) | |
242 | { | |
243 | u32 temp; | |
244 | ||
245 | xhci_dbg(xhci, "// Ding dong!\n"); | |
246 | temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK; | |
247 | xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]); | |
248 | /* Flush PCI posted writes */ | |
249 | xhci_readl(xhci, &xhci->dba->doorbell[0]); | |
250 | } | |
251 | ||
252 | static void handle_cmd_completion(struct xhci_hcd *xhci, | |
253 | struct xhci_event_cmd *event) | |
254 | { | |
3ffbba95 | 255 | int slot_id = TRB_TO_SLOT_ID(event->flags); |
7f84eef0 SS |
256 | u64 cmd_dma; |
257 | dma_addr_t cmd_dequeue_dma; | |
258 | ||
7f84eef0 SS |
259 | cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0]; |
260 | cmd_dequeue_dma = trb_virt_to_dma(xhci->cmd_ring->deq_seg, | |
261 | xhci->cmd_ring->dequeue); | |
262 | /* Is the command ring deq ptr out of sync with the deq seg ptr? */ | |
263 | if (cmd_dequeue_dma == 0) { | |
264 | xhci->error_bitmask |= 1 << 4; | |
265 | return; | |
266 | } | |
267 | /* Does the DMA address match our internal dequeue pointer address? */ | |
268 | if (cmd_dma != (u64) cmd_dequeue_dma) { | |
269 | xhci->error_bitmask |= 1 << 5; | |
270 | return; | |
271 | } | |
272 | switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) { | |
3ffbba95 SS |
273 | case TRB_TYPE(TRB_ENABLE_SLOT): |
274 | if (GET_COMP_CODE(event->status) == COMP_SUCCESS) | |
275 | xhci->slot_id = slot_id; | |
276 | else | |
277 | xhci->slot_id = 0; | |
278 | complete(&xhci->addr_dev); | |
279 | break; | |
280 | case TRB_TYPE(TRB_DISABLE_SLOT): | |
281 | if (xhci->devs[slot_id]) | |
282 | xhci_free_virt_device(xhci, slot_id); | |
283 | break; | |
f94e0186 SS |
284 | case TRB_TYPE(TRB_CONFIG_EP): |
285 | xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status); | |
286 | complete(&xhci->devs[slot_id]->cmd_completion); | |
287 | break; | |
3ffbba95 SS |
288 | case TRB_TYPE(TRB_ADDR_DEV): |
289 | xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status); | |
290 | complete(&xhci->addr_dev); | |
291 | break; | |
7f84eef0 SS |
292 | case TRB_TYPE(TRB_CMD_NOOP): |
293 | ++xhci->noops_handled; | |
294 | break; | |
295 | default: | |
296 | /* Skip over unknown commands on the event ring */ | |
297 | xhci->error_bitmask |= 1 << 6; | |
298 | break; | |
299 | } | |
300 | inc_deq(xhci, xhci->cmd_ring, false); | |
301 | } | |
302 | ||
0f2a7930 SS |
303 | static void handle_port_status(struct xhci_hcd *xhci, |
304 | union xhci_trb *event) | |
305 | { | |
306 | u32 port_id; | |
307 | ||
308 | /* Port status change events always have a successful completion code */ | |
309 | if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) { | |
310 | xhci_warn(xhci, "WARN: xHC returned failed port status event\n"); | |
311 | xhci->error_bitmask |= 1 << 8; | |
312 | } | |
313 | /* FIXME: core doesn't care about all port link state changes yet */ | |
314 | port_id = GET_PORT_ID(event->generic.field[0]); | |
315 | xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id); | |
316 | ||
317 | /* Update event ring dequeue pointer before dropping the lock */ | |
318 | inc_deq(xhci, xhci->event_ring, true); | |
319 | set_hc_event_deq(xhci); | |
320 | ||
321 | spin_unlock(&xhci->lock); | |
322 | /* Pass this up to the core */ | |
323 | usb_hcd_poll_rh_status(xhci_to_hcd(xhci)); | |
324 | spin_lock(&xhci->lock); | |
325 | } | |
326 | ||
d0e96f5a SS |
327 | /* |
328 | * This TD is defined by the TRBs starting at start_trb in start_seg and ending | |
329 | * at end_trb, which may be in another segment. If the suspect DMA address is a | |
330 | * TRB in this TD, this function returns that TRB's segment. Otherwise it | |
331 | * returns 0. | |
332 | */ | |
333 | static struct xhci_segment *trb_in_td( | |
334 | struct xhci_segment *start_seg, | |
335 | union xhci_trb *start_trb, | |
336 | union xhci_trb *end_trb, | |
337 | dma_addr_t suspect_dma) | |
338 | { | |
339 | dma_addr_t start_dma; | |
340 | dma_addr_t end_seg_dma; | |
341 | dma_addr_t end_trb_dma; | |
342 | struct xhci_segment *cur_seg; | |
343 | ||
344 | start_dma = trb_virt_to_dma(start_seg, start_trb); | |
345 | cur_seg = start_seg; | |
346 | ||
347 | do { | |
348 | /* | |
349 | * Last TRB is a link TRB (unless we start inserting links in | |
350 | * the middle, FIXME if you do) | |
351 | */ | |
352 | end_seg_dma = trb_virt_to_dma(cur_seg, &start_seg->trbs[TRBS_PER_SEGMENT - 2]); | |
353 | /* If the end TRB isn't in this segment, this is set to 0 */ | |
354 | end_trb_dma = trb_virt_to_dma(cur_seg, end_trb); | |
355 | ||
356 | if (end_trb_dma > 0) { | |
357 | /* The end TRB is in this segment, so suspect should be here */ | |
358 | if (start_dma <= end_trb_dma) { | |
359 | if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma) | |
360 | return cur_seg; | |
361 | } else { | |
362 | /* Case for one segment with | |
363 | * a TD wrapped around to the top | |
364 | */ | |
365 | if ((suspect_dma >= start_dma && | |
366 | suspect_dma <= end_seg_dma) || | |
367 | (suspect_dma >= cur_seg->dma && | |
368 | suspect_dma <= end_trb_dma)) | |
369 | return cur_seg; | |
370 | } | |
371 | return 0; | |
372 | } else { | |
373 | /* Might still be somewhere in this segment */ | |
374 | if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma) | |
375 | return cur_seg; | |
376 | } | |
377 | cur_seg = cur_seg->next; | |
378 | start_dma = trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); | |
379 | } while (1); | |
380 | ||
381 | } | |
382 | ||
383 | /* | |
384 | * If this function returns an error condition, it means it got a Transfer | |
385 | * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. | |
386 | * At this point, the host controller is probably hosed and should be reset. | |
387 | */ | |
388 | static int handle_tx_event(struct xhci_hcd *xhci, | |
389 | struct xhci_transfer_event *event) | |
390 | { | |
391 | struct xhci_virt_device *xdev; | |
392 | struct xhci_ring *ep_ring; | |
393 | int ep_index; | |
394 | struct xhci_td *td = 0; | |
395 | dma_addr_t event_dma; | |
396 | struct xhci_segment *event_seg; | |
397 | union xhci_trb *event_trb; | |
398 | struct urb *urb = NULL; | |
399 | int status = -EINPROGRESS; | |
400 | ||
401 | xdev = xhci->devs[TRB_TO_SLOT_ID(event->flags)]; | |
402 | if (!xdev) { | |
403 | xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n"); | |
404 | return -ENODEV; | |
405 | } | |
406 | ||
407 | /* Endpoint ID is 1 based, our index is zero based */ | |
408 | ep_index = TRB_TO_EP_ID(event->flags) - 1; | |
409 | ep_ring = xdev->ep_rings[ep_index]; | |
410 | if (!ep_ring || (xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) { | |
411 | xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n"); | |
412 | return -ENODEV; | |
413 | } | |
414 | ||
415 | event_dma = event->buffer[0]; | |
416 | if (event->buffer[1] != 0) | |
417 | xhci_warn(xhci, "WARN ignoring upper 32-bits of 64-bit TRB dma address\n"); | |
418 | ||
419 | /* This TRB should be in the TD at the head of this ring's TD list */ | |
420 | if (list_empty(&ep_ring->td_list)) { | |
421 | xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n", | |
422 | TRB_TO_SLOT_ID(event->flags), ep_index); | |
423 | xhci_dbg(xhci, "Event TRB with TRB type ID %u\n", | |
424 | (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10); | |
425 | xhci_print_trb_offsets(xhci, (union xhci_trb *) event); | |
426 | urb = NULL; | |
427 | goto cleanup; | |
428 | } | |
429 | td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list); | |
430 | ||
431 | /* Is this a TRB in the currently executing TD? */ | |
432 | event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue, | |
433 | td->last_trb, event_dma); | |
434 | if (!event_seg) { | |
435 | /* HC is busted, give up! */ | |
436 | xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n"); | |
437 | return -ESHUTDOWN; | |
438 | } | |
439 | event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)]; | |
440 | ||
441 | /* Now update the urb's actual_length and give back to the core */ | |
442 | /* Was this a control transfer? */ | |
443 | if (usb_endpoint_xfer_control(&td->urb->ep->desc)) { | |
444 | xhci_debug_trb(xhci, xhci->event_ring->dequeue); | |
445 | switch (GET_COMP_CODE(event->transfer_len)) { | |
446 | case COMP_SUCCESS: | |
447 | if (event_trb == ep_ring->dequeue) { | |
448 | xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n"); | |
449 | status = -ESHUTDOWN; | |
450 | } else if (event_trb != td->last_trb) { | |
451 | xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n"); | |
452 | status = -ESHUTDOWN; | |
453 | } else { | |
454 | xhci_dbg(xhci, "Successful control transfer!\n"); | |
455 | status = 0; | |
456 | } | |
457 | break; | |
458 | case COMP_SHORT_TX: | |
459 | xhci_warn(xhci, "WARN: short transfer on control ep\n"); | |
460 | status = -EREMOTEIO; | |
461 | break; | |
462 | case COMP_STALL: | |
463 | xhci_warn(xhci, "WARN: Stalled control ep\n"); | |
464 | status = -EPIPE; | |
465 | break; | |
466 | case COMP_TRB_ERR: | |
467 | xhci_warn(xhci, "WARN: TRB error on control ep\n"); | |
468 | status = -EILSEQ; | |
469 | break; | |
470 | case COMP_TX_ERR: | |
471 | xhci_warn(xhci, "WARN: transfer error on control ep\n"); | |
472 | status = -EPROTO; | |
473 | break; | |
474 | case COMP_DB_ERR: | |
475 | xhci_warn(xhci, "WARN: HC couldn't access mem fast enough on control TX\n"); | |
476 | status = -ENOSR; | |
477 | break; | |
478 | default: | |
479 | xhci_dbg(xhci, "ERROR Unknown event condition, HC probably busted\n"); | |
480 | goto cleanup; | |
481 | } | |
482 | /* | |
483 | * Did we transfer any data, despite the errors that might have | |
484 | * happened? I.e. did we get past the setup stage? | |
485 | */ | |
486 | if (event_trb != ep_ring->dequeue) { | |
487 | /* The event was for the status stage */ | |
488 | if (event_trb == td->last_trb) { | |
489 | td->urb->actual_length = td->urb->transfer_buffer_length; | |
490 | } else { | |
491 | /* The event was for the data stage */ | |
492 | td->urb->actual_length = td->urb->transfer_buffer_length - | |
493 | TRB_LEN(event->transfer_len); | |
494 | } | |
495 | } | |
496 | while (ep_ring->dequeue != td->last_trb) | |
497 | inc_deq(xhci, ep_ring, false); | |
498 | inc_deq(xhci, ep_ring, false); | |
499 | ||
500 | /* Clean up the endpoint's TD list */ | |
501 | urb = td->urb; | |
502 | list_del(&td->td_list); | |
503 | kfree(td); | |
504 | } else { | |
505 | xhci_dbg(xhci, "FIXME do something for non-control transfers\n"); | |
506 | } | |
507 | cleanup: | |
508 | inc_deq(xhci, xhci->event_ring, true); | |
509 | set_hc_event_deq(xhci); | |
510 | ||
511 | if (urb) { | |
512 | usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb); | |
513 | spin_unlock(&xhci->lock); | |
514 | usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status); | |
515 | spin_lock(&xhci->lock); | |
516 | } | |
517 | return 0; | |
518 | } | |
519 | ||
0f2a7930 SS |
520 | /* |
521 | * This function handles all OS-owned events on the event ring. It may drop | |
522 | * xhci->lock between event processing (e.g. to pass up port status changes). | |
523 | */ | |
7f84eef0 SS |
524 | void handle_event(struct xhci_hcd *xhci) |
525 | { | |
526 | union xhci_trb *event; | |
0f2a7930 | 527 | int update_ptrs = 1; |
d0e96f5a | 528 | int ret; |
7f84eef0 SS |
529 | |
530 | if (!xhci->event_ring || !xhci->event_ring->dequeue) { | |
531 | xhci->error_bitmask |= 1 << 1; | |
532 | return; | |
533 | } | |
534 | ||
535 | event = xhci->event_ring->dequeue; | |
536 | /* Does the HC or OS own the TRB? */ | |
537 | if ((event->event_cmd.flags & TRB_CYCLE) != | |
538 | xhci->event_ring->cycle_state) { | |
539 | xhci->error_bitmask |= 1 << 2; | |
540 | return; | |
541 | } | |
542 | ||
0f2a7930 | 543 | /* FIXME: Handle more event types. */ |
7f84eef0 SS |
544 | switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) { |
545 | case TRB_TYPE(TRB_COMPLETION): | |
546 | handle_cmd_completion(xhci, &event->event_cmd); | |
547 | break; | |
0f2a7930 SS |
548 | case TRB_TYPE(TRB_PORT_STATUS): |
549 | handle_port_status(xhci, event); | |
550 | update_ptrs = 0; | |
551 | break; | |
d0e96f5a SS |
552 | case TRB_TYPE(TRB_TRANSFER): |
553 | ret = handle_tx_event(xhci, &event->trans_event); | |
554 | if (ret < 0) | |
555 | xhci->error_bitmask |= 1 << 9; | |
556 | else | |
557 | update_ptrs = 0; | |
558 | break; | |
7f84eef0 SS |
559 | default: |
560 | xhci->error_bitmask |= 1 << 3; | |
561 | } | |
562 | ||
0f2a7930 SS |
563 | if (update_ptrs) { |
564 | /* Update SW and HC event ring dequeue pointer */ | |
565 | inc_deq(xhci, xhci->event_ring, true); | |
566 | set_hc_event_deq(xhci); | |
567 | } | |
7f84eef0 SS |
568 | /* Are there more items on the event ring? */ |
569 | handle_event(xhci); | |
570 | } | |
571 | ||
d0e96f5a SS |
572 | /**** Endpoint Ring Operations ****/ |
573 | ||
7f84eef0 SS |
574 | /* |
575 | * Generic function for queueing a TRB on a ring. | |
576 | * The caller must have checked to make sure there's room on the ring. | |
577 | */ | |
578 | static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, | |
579 | bool consumer, | |
580 | u32 field1, u32 field2, u32 field3, u32 field4) | |
581 | { | |
582 | struct xhci_generic_trb *trb; | |
583 | ||
584 | trb = &ring->enqueue->generic; | |
585 | trb->field[0] = field1; | |
586 | trb->field[1] = field2; | |
587 | trb->field[2] = field3; | |
588 | trb->field[3] = field4; | |
589 | inc_enq(xhci, ring, consumer); | |
590 | } | |
591 | ||
d0e96f5a SS |
592 | /* |
593 | * Does various checks on the endpoint ring, and makes it ready to queue num_trbs. | |
594 | * FIXME allocate segments if the ring is full. | |
595 | */ | |
596 | static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, | |
597 | u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) | |
598 | { | |
599 | /* Make sure the endpoint has been added to xHC schedule */ | |
600 | xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state); | |
601 | switch (ep_state) { | |
602 | case EP_STATE_DISABLED: | |
603 | /* | |
604 | * USB core changed config/interfaces without notifying us, | |
605 | * or hardware is reporting the wrong state. | |
606 | */ | |
607 | xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); | |
608 | return -ENOENT; | |
609 | case EP_STATE_HALTED: | |
610 | case EP_STATE_ERROR: | |
611 | xhci_warn(xhci, "WARN waiting for halt or error on ep " | |
612 | "to be cleared\n"); | |
613 | /* FIXME event handling code for error needs to clear it */ | |
614 | /* XXX not sure if this should be -ENOENT or not */ | |
615 | return -EINVAL; | |
616 | case EP_STATE_STOPPED: | |
617 | case EP_STATE_RUNNING: | |
618 | break; | |
619 | default: | |
620 | xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); | |
621 | /* | |
622 | * FIXME issue Configure Endpoint command to try to get the HC | |
623 | * back into a known state. | |
624 | */ | |
625 | return -EINVAL; | |
626 | } | |
627 | if (!room_on_ring(xhci, ep_ring, num_trbs)) { | |
628 | /* FIXME allocate more room */ | |
629 | xhci_err(xhci, "ERROR no room on ep ring\n"); | |
630 | return -ENOMEM; | |
631 | } | |
632 | return 0; | |
633 | } | |
634 | ||
635 | int xhci_prepare_transfer(struct xhci_hcd *xhci, | |
636 | struct xhci_virt_device *xdev, | |
637 | unsigned int ep_index, | |
638 | unsigned int num_trbs, | |
639 | struct urb *urb, | |
640 | struct xhci_td **td, | |
641 | gfp_t mem_flags) | |
642 | { | |
643 | int ret; | |
644 | ||
645 | ret = prepare_ring(xhci, xdev->ep_rings[ep_index], | |
646 | xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK, | |
647 | num_trbs, mem_flags); | |
648 | if (ret) | |
649 | return ret; | |
650 | *td = kzalloc(sizeof(struct xhci_td), mem_flags); | |
651 | if (!*td) | |
652 | return -ENOMEM; | |
653 | INIT_LIST_HEAD(&(*td)->td_list); | |
654 | ||
655 | ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb); | |
656 | if (unlikely(ret)) { | |
657 | kfree(*td); | |
658 | return ret; | |
659 | } | |
660 | ||
661 | (*td)->urb = urb; | |
662 | urb->hcpriv = (void *) (*td); | |
663 | /* Add this TD to the tail of the endpoint ring's TD list */ | |
664 | list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list); | |
665 | ||
666 | return 0; | |
667 | } | |
668 | ||
669 | /* Caller must have locked xhci->lock */ | |
670 | int queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, | |
671 | struct urb *urb, int slot_id, unsigned int ep_index) | |
672 | { | |
673 | struct xhci_ring *ep_ring; | |
674 | int num_trbs; | |
675 | int ret; | |
676 | struct usb_ctrlrequest *setup; | |
677 | struct xhci_generic_trb *start_trb; | |
678 | int start_cycle; | |
679 | u32 field; | |
680 | struct xhci_td *td; | |
681 | ||
682 | ep_ring = xhci->devs[slot_id]->ep_rings[ep_index]; | |
683 | ||
684 | /* | |
685 | * Need to copy setup packet into setup TRB, so we can't use the setup | |
686 | * DMA address. | |
687 | */ | |
688 | if (!urb->setup_packet) | |
689 | return -EINVAL; | |
690 | ||
691 | if (!in_interrupt()) | |
692 | xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n", | |
693 | slot_id, ep_index); | |
694 | /* 1 TRB for setup, 1 for status */ | |
695 | num_trbs = 2; | |
696 | /* | |
697 | * Don't need to check if we need additional event data and normal TRBs, | |
698 | * since data in control transfers will never get bigger than 16MB | |
699 | * XXX: can we get a buffer that crosses 64KB boundaries? | |
700 | */ | |
701 | if (urb->transfer_buffer_length > 0) | |
702 | num_trbs++; | |
703 | ret = xhci_prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs, | |
704 | urb, &td, mem_flags); | |
705 | if (ret < 0) | |
706 | return ret; | |
707 | ||
708 | /* | |
709 | * Don't give the first TRB to the hardware (by toggling the cycle bit) | |
710 | * until we've finished creating all the other TRBs. The ring's cycle | |
711 | * state may change as we enqueue the other TRBs, so save it too. | |
712 | */ | |
713 | start_trb = &ep_ring->enqueue->generic; | |
714 | start_cycle = ep_ring->cycle_state; | |
715 | ||
716 | /* Queue setup TRB - see section 6.4.1.2.1 */ | |
717 | /* FIXME better way to translate setup_packet into two u32 fields? */ | |
718 | setup = (struct usb_ctrlrequest *) urb->setup_packet; | |
719 | queue_trb(xhci, ep_ring, false, | |
720 | /* FIXME endianness is probably going to bite my ass here. */ | |
721 | setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16, | |
722 | setup->wIndex | setup->wLength << 16, | |
723 | TRB_LEN(8) | TRB_INTR_TARGET(0), | |
724 | /* Immediate data in pointer */ | |
725 | TRB_IDT | TRB_TYPE(TRB_SETUP)); | |
726 | ||
727 | /* If there's data, queue data TRBs */ | |
728 | field = 0; | |
729 | if (urb->transfer_buffer_length > 0) { | |
730 | if (setup->bRequestType & USB_DIR_IN) | |
731 | field |= TRB_DIR_IN; | |
732 | queue_trb(xhci, ep_ring, false, | |
733 | lower_32_bits(urb->transfer_dma), | |
734 | upper_32_bits(urb->transfer_dma), | |
735 | TRB_LEN(urb->transfer_buffer_length) | TRB_INTR_TARGET(0), | |
736 | /* Event on short tx */ | |
737 | field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state); | |
738 | } | |
739 | ||
740 | /* Save the DMA address of the last TRB in the TD */ | |
741 | td->last_trb = ep_ring->enqueue; | |
742 | ||
743 | /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ | |
744 | /* If the device sent data, the status stage is an OUT transfer */ | |
745 | if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) | |
746 | field = 0; | |
747 | else | |
748 | field = TRB_DIR_IN; | |
749 | queue_trb(xhci, ep_ring, false, | |
750 | 0, | |
751 | 0, | |
752 | TRB_INTR_TARGET(0), | |
753 | /* Event on completion */ | |
754 | field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); | |
755 | ||
756 | /* | |
757 | * Pass all the TRBs to the hardware at once and make sure this write | |
758 | * isn't reordered. | |
759 | */ | |
760 | wmb(); | |
761 | start_trb->field[3] |= start_cycle; | |
762 | field = xhci_readl(xhci, &xhci->dba->doorbell[slot_id]) & DB_MASK; | |
763 | xhci_writel(xhci, field | EPI_TO_DB(ep_index), &xhci->dba->doorbell[slot_id]); | |
764 | /* Flush PCI posted writes */ | |
765 | xhci_readl(xhci, &xhci->dba->doorbell[slot_id]); | |
766 | ||
767 | return 0; | |
768 | } | |
769 | ||
770 | /**** Command Ring Operations ****/ | |
771 | ||
7f84eef0 SS |
772 | /* Generic function for queueing a command TRB on the command ring */ |
773 | static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4) | |
774 | { | |
775 | if (!room_on_ring(xhci, xhci->cmd_ring, 1)) { | |
776 | if (!in_interrupt()) | |
777 | xhci_err(xhci, "ERR: No room for command on command ring\n"); | |
778 | return -ENOMEM; | |
779 | } | |
780 | queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, | |
781 | field4 | xhci->cmd_ring->cycle_state); | |
782 | return 0; | |
783 | } | |
784 | ||
785 | /* Queue a no-op command on the command ring */ | |
786 | static int queue_cmd_noop(struct xhci_hcd *xhci) | |
787 | { | |
788 | return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP)); | |
789 | } | |
790 | ||
791 | /* | |
792 | * Place a no-op command on the command ring to test the command and | |
793 | * event ring. | |
794 | */ | |
795 | void *setup_one_noop(struct xhci_hcd *xhci) | |
796 | { | |
797 | if (queue_cmd_noop(xhci) < 0) | |
798 | return NULL; | |
799 | xhci->noops_submitted++; | |
800 | return ring_cmd_db; | |
801 | } | |
3ffbba95 SS |
802 | |
803 | /* Queue a slot enable or disable request on the command ring */ | |
804 | int queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id) | |
805 | { | |
806 | return queue_command(xhci, 0, 0, 0, | |
807 | TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id)); | |
808 | } | |
809 | ||
810 | /* Queue an address device command TRB */ | |
811 | int queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id) | |
812 | { | |
813 | return queue_command(xhci, in_ctx_ptr, 0, 0, | |
814 | TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id)); | |
815 | } | |
f94e0186 SS |
816 | |
817 | /* Queue a configure endpoint command TRB */ | |
818 | int queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id) | |
819 | { | |
820 | return queue_command(xhci, in_ctx_ptr, 0, 0, | |
821 | TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id)); | |
822 | } |