blob: 764995fd59ef84b70b64c98c90e63a90c8604015 [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/irq.h>
24#include <linux/module.h>
25
26#include "xhci.h"
27
28#define DRIVER_AUTHOR "Sarah Sharp"
29#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
30
31/* TODO: copied from ehci-hcd.c - can this be refactored? */
32/*
33 * handshake - spin reading hc until handshake completes or fails
34 * @ptr: address of hc register to be read
35 * @mask: bits to look at in result of read
36 * @done: value of those bits when handshake succeeds
37 * @usec: timeout in microseconds
38 *
39 * Returns negative errno, or zero on success
40 *
41 * Success happens when the "mask" bits have the specified value (hardware
42 * handshake done). There are two failure modes: "usec" have passed (major
43 * hardware flakeout), or the register reads as all-ones (hardware removed).
44 */
45static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
46 u32 mask, u32 done, int usec)
47{
48 u32 result;
49
50 do {
51 result = xhci_readl(xhci, ptr);
52 if (result == ~(u32)0) /* card removed */
53 return -ENODEV;
54 result &= mask;
55 if (result == done)
56 return 0;
57 udelay(1);
58 usec--;
59 } while (usec > 0);
60 return -ETIMEDOUT;
61}
62
63/*
64 * Force HC into halt state.
65 *
66 * Disable any IRQs and clear the run/stop bit.
67 * HC will complete any current and actively pipelined transactions, and
68 * should halt within 16 microframes of the run/stop bit being cleared.
69 * Read HC Halted bit in the status register to see when the HC is finished.
70 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
71 */
72int xhci_halt(struct xhci_hcd *xhci)
73{
74 u32 halted;
75 u32 cmd;
76 u32 mask;
77
78 xhci_dbg(xhci, "// Halt the HC\n");
79 /* Disable all interrupts from the host controller */
80 mask = ~(XHCI_IRQS);
81 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
82 if (!halted)
83 mask &= ~CMD_RUN;
84
85 cmd = xhci_readl(xhci, &xhci->op_regs->command);
86 cmd &= mask;
87 xhci_writel(xhci, cmd, &xhci->op_regs->command);
88
89 return handshake(xhci, &xhci->op_regs->status,
90 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
91}
92
93/*
94 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
95 *
96 * This resets pipelines, timers, counters, state machines, etc.
97 * Transactions will be terminated immediately, and operational registers
98 * will be set to their defaults.
99 */
100int xhci_reset(struct xhci_hcd *xhci)
101{
102 u32 command;
103 u32 state;
104
105 state = xhci_readl(xhci, &xhci->op_regs->status);
106 BUG_ON((state & STS_HALT) == 0);
107
108 xhci_dbg(xhci, "// Reset the HC\n");
109 command = xhci_readl(xhci, &xhci->op_regs->command);
110 command |= CMD_RESET;
111 xhci_writel(xhci, command, &xhci->op_regs->command);
112 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
113 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
114
115 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
116}
117
118/*
119 * Stop the HC from processing the endpoint queues.
120 */
121static void xhci_quiesce(struct xhci_hcd *xhci)
122{
123 /*
124 * Queues are per endpoint, so we need to disable an endpoint or slot.
125 *
126 * To disable a slot, we need to insert a disable slot command on the
127 * command ring and ring the doorbell. This will also free any internal
128 * resources associated with the slot (which might not be what we want).
129 *
130 * A Release Endpoint command sounds better - doesn't free internal HC
131 * memory, but removes the endpoints from the schedule and releases the
132 * bandwidth, disables the doorbells, and clears the endpoint enable
133 * flag. Usually used prior to a set interface command.
134 *
135 * TODO: Implement after command ring code is done.
136 */
137 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
138 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
139}
140
141#if 0
142/* Set up MSI-X table for entry 0 (may claim other entries later) */
143static int xhci_setup_msix(struct xhci_hcd *xhci)
144{
145 int ret;
146 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
147
148 xhci->msix_count = 0;
149 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
150 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
151 if (!xhci->msix_entries) {
152 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
153 return -ENOMEM;
154 }
155 xhci->msix_entries[0].entry = 0;
156
157 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
158 if (ret) {
159 xhci_err(xhci, "Failed to enable MSI-X\n");
160 goto free_entries;
161 }
162
163 /*
164 * Pass the xhci pointer value as the request_irq "cookie".
165 * If more irqs are added, this will need to be unique for each one.
166 */
167 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
168 "xHCI", xhci_to_hcd(xhci));
169 if (ret) {
170 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
171 goto disable_msix;
172 }
173 xhci_dbg(xhci, "Finished setting up MSI-X\n");
174 return 0;
175
176disable_msix:
177 pci_disable_msix(pdev);
178free_entries:
179 kfree(xhci->msix_entries);
180 xhci->msix_entries = NULL;
181 return ret;
182}
183
184/* XXX: code duplication; can xhci_setup_msix call this? */
185/* Free any IRQs and disable MSI-X */
186static void xhci_cleanup_msix(struct xhci_hcd *xhci)
187{
188 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
189 if (!xhci->msix_entries)
190 return;
191
192 free_irq(xhci->msix_entries[0].vector, xhci);
193 pci_disable_msix(pdev);
194 kfree(xhci->msix_entries);
195 xhci->msix_entries = NULL;
196 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
197}
198#endif
199
200/*
201 * Initialize memory for HCD and xHC (one-time init).
202 *
203 * Program the PAGESIZE register, initialize the device context array, create
204 * device contexts (?), set up a command ring segment (or two?), create event
205 * ring (one for now).
206 */
207int xhci_init(struct usb_hcd *hcd)
208{
209 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
210 int retval = 0;
211
212 xhci_dbg(xhci, "xhci_init\n");
213 spin_lock_init(&xhci->lock);
214 retval = xhci_mem_init(xhci, GFP_KERNEL);
215 xhci_dbg(xhci, "Finished xhci_init\n");
216
217 return retval;
218}
219
220/*
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700221 * Called in interrupt context when there might be work
222 * queued on the event ring
223 *
224 * xhci->lock must be held by caller.
225 */
226static void xhci_work(struct xhci_hcd *xhci)
227{
228 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700229 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700230
231 /*
232 * Clear the op reg interrupt status first,
233 * so we can receive interrupts from other MSI-X interrupters.
234 * Write 1 to clear the interrupt status.
235 */
236 temp = xhci_readl(xhci, &xhci->op_regs->status);
237 temp |= STS_EINT;
238 xhci_writel(xhci, temp, &xhci->op_regs->status);
239 /* FIXME when MSI-X is supported and there are multiple vectors */
240 /* Clear the MSI-X event interrupt status */
241
242 /* Acknowledge the interrupt */
243 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
244 temp |= 0x3;
245 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
246 /* Flush posted writes */
247 xhci_readl(xhci, &xhci->ir_set->irq_pending);
248
249 /* FIXME this should be a delayed service routine that clears the EHB */
Stephen Rothwellb7258a42009-04-29 19:02:47 -0700250 xhci_handle_event(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700251
Sarah Sharp2d831092009-07-27 12:03:40 -0700252 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
Sarah Sharp8e595a52009-07-27 12:03:31 -0700253 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp2d831092009-07-27 12:03:40 -0700254 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700255 /* Flush posted writes -- FIXME is this necessary? */
256 xhci_readl(xhci, &xhci->ir_set->irq_pending);
257}
258
259/*-------------------------------------------------------------------------*/
260
261/*
262 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
263 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
264 * indicators of an event TRB error, but we check the status *first* to be safe.
265 */
266irqreturn_t xhci_irq(struct usb_hcd *hcd)
267{
268 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
269 u32 temp, temp2;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700270 union xhci_trb *trb;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700271
272 spin_lock(&xhci->lock);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700273 trb = xhci->event_ring->dequeue;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700274 /* Check if the xHC generated the interrupt, or the irq is shared */
275 temp = xhci_readl(xhci, &xhci->op_regs->status);
276 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
277 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
278 spin_unlock(&xhci->lock);
279 return IRQ_NONE;
280 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700281 xhci_dbg(xhci, "op reg status = %08x\n", temp);
282 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
283 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
284 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
285 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
286 lower_32_bits(trb->link.segment_ptr),
287 upper_32_bits(trb->link.segment_ptr),
288 (unsigned int) trb->link.intr_target,
289 (unsigned int) trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700290
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700291 if (temp & STS_FATAL) {
292 xhci_warn(xhci, "WARNING: Host System Error\n");
293 xhci_halt(xhci);
294 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
Sarah Sharpc96a2b82009-04-29 19:05:40 -0700295 spin_unlock(&xhci->lock);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700296 return -ESHUTDOWN;
297 }
298
299 xhci_work(xhci);
300 spin_unlock(&xhci->lock);
301
302 return IRQ_HANDLED;
303}
304
305#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700306void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700307{
308 unsigned long flags;
309 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700310 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700311 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
312 int i, j;
313
314 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
315
316 spin_lock_irqsave(&xhci->lock, flags);
317 temp = xhci_readl(xhci, &xhci->op_regs->status);
318 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
319 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
320 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
321 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
322 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
323 xhci->error_bitmask = 0;
324 xhci_dbg(xhci, "Event ring:\n");
325 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
326 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700327 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
328 temp_64 &= ~ERST_PTR_MASK;
329 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700330 xhci_dbg(xhci, "Command ring:\n");
331 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
332 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
333 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700334 for (i = 0; i < MAX_HC_SLOTS; ++i) {
335 if (xhci->devs[i]) {
336 for (j = 0; j < 31; ++j) {
337 if (xhci->devs[i]->ep_rings[j]) {
338 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
339 xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
340 }
341 }
342 }
343 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700344
345 if (xhci->noops_submitted != NUM_TEST_NOOPS)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700346 if (xhci_setup_one_noop(xhci))
347 xhci_ring_cmd_db(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700348 spin_unlock_irqrestore(&xhci->lock, flags);
349
350 if (!xhci->zombie)
351 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
352 else
353 xhci_dbg(xhci, "Quit polling the event ring.\n");
354}
355#endif
356
357/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700358 * Start the HC after it was halted.
359 *
360 * This function is called by the USB core when the HC driver is added.
361 * Its opposite is xhci_stop().
362 *
363 * xhci_init() must be called once before this function can be called.
364 * Reset the HC, enable device slot contexts, program DCBAAP, and
365 * set command ring pointer and event ring pointer.
366 *
367 * Setup MSI-X vectors and enable interrupts.
368 */
369int xhci_run(struct usb_hcd *hcd)
370{
371 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700372 u64 temp_64;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700373 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700374 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700375
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700376 hcd->uses_new_polling = 1;
377 hcd->poll_rh = 0;
378
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700379 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700380#if 0 /* FIXME: MSI not setup yet */
381 /* Do this at the very last minute */
382 ret = xhci_setup_msix(xhci);
383 if (!ret)
384 return ret;
385
386 return -ENOSYS;
387#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700388#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
389 init_timer(&xhci->event_ring_timer);
390 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700391 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700392 /* Poll the event ring */
393 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
394 xhci->zombie = 0;
395 xhci_dbg(xhci, "Setting event ring polling timer\n");
396 add_timer(&xhci->event_ring_timer);
397#endif
398
Sarah Sharp66e49d82009-07-27 12:03:46 -0700399 xhci_dbg(xhci, "Command ring memory map follows:\n");
400 xhci_debug_ring(xhci, xhci->cmd_ring);
401 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
402 xhci_dbg_cmd_ptrs(xhci);
403
404 xhci_dbg(xhci, "ERST memory map follows:\n");
405 xhci_dbg_erst(xhci, &xhci->erst);
406 xhci_dbg(xhci, "Event ring:\n");
407 xhci_debug_ring(xhci, xhci->event_ring);
408 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
409 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
410 temp_64 &= ~ERST_PTR_MASK;
411 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
412
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700413 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
414 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700415 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700416 temp |= (u32) 160;
417 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
418
419 /* Set the HCD state before we enable the irqs */
420 hcd->state = HC_STATE_RUNNING;
421 temp = xhci_readl(xhci, &xhci->op_regs->command);
422 temp |= (CMD_EIE);
423 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
424 temp);
425 xhci_writel(xhci, temp, &xhci->op_regs->command);
426
427 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700428 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
429 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700430 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
431 &xhci->ir_set->irq_pending);
432 xhci_print_ir_set(xhci, xhci->ir_set, 0);
433
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700434 if (NUM_TEST_NOOPS > 0)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700435 doorbell = xhci_setup_one_noop(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700436
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700437 temp = xhci_readl(xhci, &xhci->op_regs->command);
438 temp |= (CMD_RUN);
439 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
440 temp);
441 xhci_writel(xhci, temp, &xhci->op_regs->command);
442 /* Flush PCI posted writes */
443 temp = xhci_readl(xhci, &xhci->op_regs->command);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700444 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700445 if (doorbell)
446 (*doorbell)(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700447
448 xhci_dbg(xhci, "Finished xhci_run\n");
449 return 0;
450}
451
452/*
453 * Stop xHCI driver.
454 *
455 * This function is called by the USB core when the HC driver is removed.
456 * Its opposite is xhci_run().
457 *
458 * Disable device contexts, disable IRQs, and quiesce the HC.
459 * Reset the HC, finish any completed transactions, and cleanup memory.
460 */
461void xhci_stop(struct usb_hcd *hcd)
462{
463 u32 temp;
464 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
465
466 spin_lock_irq(&xhci->lock);
467 if (HC_IS_RUNNING(hcd->state))
468 xhci_quiesce(xhci);
469 xhci_halt(xhci);
470 xhci_reset(xhci);
471 spin_unlock_irq(&xhci->lock);
472
473#if 0 /* No MSI yet */
474 xhci_cleanup_msix(xhci);
475#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700476#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
477 /* Tell the event ring poll function not to reschedule */
478 xhci->zombie = 1;
479 del_timer_sync(&xhci->event_ring_timer);
480#endif
481
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700482 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
483 temp = xhci_readl(xhci, &xhci->op_regs->status);
484 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
485 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
486 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
487 &xhci->ir_set->irq_pending);
488 xhci_print_ir_set(xhci, xhci->ir_set, 0);
489
490 xhci_dbg(xhci, "cleaning up memory\n");
491 xhci_mem_cleanup(xhci);
492 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
493 xhci_readl(xhci, &xhci->op_regs->status));
494}
495
496/*
497 * Shutdown HC (not bus-specific)
498 *
499 * This is called when the machine is rebooting or halting. We assume that the
500 * machine will be powered off, and the HC's internal state will be reset.
501 * Don't bother to free memory.
502 */
503void xhci_shutdown(struct usb_hcd *hcd)
504{
505 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
506
507 spin_lock_irq(&xhci->lock);
508 xhci_halt(xhci);
509 spin_unlock_irq(&xhci->lock);
510
511#if 0
512 xhci_cleanup_msix(xhci);
513#endif
514
515 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
516 xhci_readl(xhci, &xhci->op_regs->status));
517}
518
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700519/*-------------------------------------------------------------------------*/
520
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700521/**
522 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
523 * HCDs. Find the index for an endpoint given its descriptor. Use the return
524 * value to right shift 1 for the bitmask.
525 *
526 * Index = (epnum * 2) + direction - 1,
527 * where direction = 0 for OUT, 1 for IN.
528 * For control endpoints, the IN index is used (OUT index is unused), so
529 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
530 */
531unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
532{
533 unsigned int index;
534 if (usb_endpoint_xfer_control(desc))
535 index = (unsigned int) (usb_endpoint_num(desc)*2);
536 else
537 index = (unsigned int) (usb_endpoint_num(desc)*2) +
538 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
539 return index;
540}
541
Sarah Sharpf94e01862009-04-27 19:58:38 -0700542/* Find the flag for this endpoint (for use in the control context). Use the
543 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
544 * bit 1, etc.
545 */
546unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
547{
548 return 1 << (xhci_get_endpoint_index(desc) + 1);
549}
550
551/* Compute the last valid endpoint context index. Basically, this is the
552 * endpoint index plus one. For slot contexts with more than valid endpoint,
553 * we find the most significant bit set in the added contexts flags.
554 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
555 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
556 */
557static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
558{
559 return fls(added_ctxs) - 1;
560}
561
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700562/* Returns 1 if the arguments are OK;
563 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
564 */
565int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
566 struct usb_host_endpoint *ep, int check_ep, const char *func) {
567 if (!hcd || (check_ep && !ep) || !udev) {
568 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
569 func);
570 return -EINVAL;
571 }
572 if (!udev->parent) {
573 printk(KERN_DEBUG "xHCI %s called for root hub\n",
574 func);
575 return 0;
576 }
577 if (!udev->slot_id) {
578 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
579 func);
580 return -EINVAL;
581 }
582 return 1;
583}
584
585/*
586 * non-error returns are a promise to giveback() the urb later
587 * we drop ownership so next owner (or urb unlink) can get it
588 */
589int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
590{
591 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
592 unsigned long flags;
593 int ret = 0;
594 unsigned int slot_id, ep_index;
595
596 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
597 return -EINVAL;
598
599 slot_id = urb->dev->slot_id;
600 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700601
602 spin_lock_irqsave(&xhci->lock, flags);
603 if (!xhci->devs || !xhci->devs[slot_id]) {
604 if (!in_interrupt())
605 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
Sarah Sharpc7959fb2009-04-29 19:06:36 -0700606 ret = -EINVAL;
607 goto exit;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700608 }
609 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
610 if (!in_interrupt())
611 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
612 ret = -ESHUTDOWN;
613 goto exit;
614 }
Sarah Sharpb10de142009-04-27 19:58:50 -0700615 if (usb_endpoint_xfer_control(&urb->ep->desc))
Sarah Sharpb11069f2009-07-27 12:03:23 -0700616 /* We have a spinlock and interrupts disabled, so we must pass
617 * atomic context to this function, which may allocate memory.
618 */
619 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700620 slot_id, ep_index);
Sarah Sharpb10de142009-04-27 19:58:50 -0700621 else if (usb_endpoint_xfer_bulk(&urb->ep->desc))
Sarah Sharpb11069f2009-07-27 12:03:23 -0700622 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700623 slot_id, ep_index);
Sarah Sharpb10de142009-04-27 19:58:50 -0700624 else
625 ret = -EINVAL;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700626exit:
627 spin_unlock_irqrestore(&xhci->lock, flags);
628 return ret;
629}
630
Sarah Sharpae636742009-04-29 19:02:31 -0700631/*
632 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
633 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
634 * should pick up where it left off in the TD, unless a Set Transfer Ring
635 * Dequeue Pointer is issued.
636 *
637 * The TRBs that make up the buffers for the canceled URB will be "removed" from
638 * the ring. Since the ring is a contiguous structure, they can't be physically
639 * removed. Instead, there are two options:
640 *
641 * 1) If the HC is in the middle of processing the URB to be canceled, we
642 * simply move the ring's dequeue pointer past those TRBs using the Set
643 * Transfer Ring Dequeue Pointer command. This will be the common case,
644 * when drivers timeout on the last submitted URB and attempt to cancel.
645 *
646 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
647 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
648 * HC will need to invalidate the any TRBs it has cached after the stop
649 * endpoint command, as noted in the xHCI 0.95 errata.
650 *
651 * 3) The TD may have completed by the time the Stop Endpoint Command
652 * completes, so software needs to handle that case too.
653 *
654 * This function should protect against the TD enqueueing code ringing the
655 * doorbell while this code is waiting for a Stop Endpoint command to complete.
656 * It also needs to account for multiple cancellations on happening at the same
657 * time for the same endpoint.
658 *
659 * Note that this function can be called in any context, or so says
660 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700661 */
662int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
663{
Sarah Sharpae636742009-04-29 19:02:31 -0700664 unsigned long flags;
665 int ret;
666 struct xhci_hcd *xhci;
667 struct xhci_td *td;
668 unsigned int ep_index;
669 struct xhci_ring *ep_ring;
670
671 xhci = hcd_to_xhci(hcd);
672 spin_lock_irqsave(&xhci->lock, flags);
673 /* Make sure the URB hasn't completed or been unlinked already */
674 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
675 if (ret || !urb->hcpriv)
676 goto done;
677
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700678 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700679 xhci_dbg(xhci, "Event ring:\n");
680 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700681 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
682 ep_ring = xhci->devs[urb->dev->slot_id]->ep_rings[ep_index];
Sarah Sharp66e49d82009-07-27 12:03:46 -0700683 xhci_dbg(xhci, "Endpoint ring:\n");
684 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700685 td = (struct xhci_td *) urb->hcpriv;
686
687 ep_ring->cancels_pending++;
688 list_add_tail(&td->cancelled_td_list, &ep_ring->cancelled_td_list);
689 /* Queue a stop endpoint command, but only if this is
690 * the first cancellation to be handled.
691 */
692 if (ep_ring->cancels_pending == 1) {
Sarah Sharp23e3be12009-04-29 19:05:20 -0700693 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
694 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700695 }
696done:
697 spin_unlock_irqrestore(&xhci->lock, flags);
698 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700699}
700
Sarah Sharpf94e01862009-04-27 19:58:38 -0700701/* Drop an endpoint from a new bandwidth configuration for this device.
702 * Only one call to this function is allowed per endpoint before
703 * check_bandwidth() or reset_bandwidth() must be called.
704 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
705 * add the endpoint to the schedule with possibly new parameters denoted by a
706 * different endpoint descriptor in usb_host_endpoint.
707 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
708 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700709 *
710 * The USB core will not allow URBs to be queued to an endpoint that is being
711 * disabled, so there's no need for mutual exclusion to protect
712 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700713 */
714int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
715 struct usb_host_endpoint *ep)
716{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700717 struct xhci_hcd *xhci;
718 struct xhci_device_control *in_ctx;
719 unsigned int last_ctx;
720 unsigned int ep_index;
721 struct xhci_ep_ctx *ep_ctx;
722 u32 drop_flag;
723 u32 new_add_flags, new_drop_flags, new_slot_info;
724 int ret;
725
726 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700727 if (ret <= 0)
728 return ret;
729 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700730 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700731
732 drop_flag = xhci_get_endpoint_flag(&ep->desc);
733 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
734 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
735 __func__, drop_flag);
736 return 0;
737 }
738
Sarah Sharpf94e01862009-04-27 19:58:38 -0700739 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
740 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
741 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700742 return -EINVAL;
743 }
744
745 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
746 ep_index = xhci_get_endpoint_index(&ep->desc);
747 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
748 /* If the HC already knows the endpoint is disabled,
749 * or the HCD has noted it is disabled, ignore this request
750 */
751 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
752 in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700753 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
754 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700755 return 0;
756 }
757
758 in_ctx->drop_flags |= drop_flag;
759 new_drop_flags = in_ctx->drop_flags;
760
761 in_ctx->add_flags = ~drop_flag;
762 new_add_flags = in_ctx->add_flags;
763
764 last_ctx = xhci_last_valid_endpoint(in_ctx->add_flags);
765 /* Update the last valid endpoint context, if we deleted the last one */
766 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
767 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
768 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
769 }
770 new_slot_info = in_ctx->slot.dev_info;
771
772 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
773
Sarah Sharpf94e01862009-04-27 19:58:38 -0700774 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
775 (unsigned int) ep->desc.bEndpointAddress,
776 udev->slot_id,
777 (unsigned int) new_drop_flags,
778 (unsigned int) new_add_flags,
779 (unsigned int) new_slot_info);
780 return 0;
781}
782
783/* Add an endpoint to a new possible bandwidth configuration for this device.
784 * Only one call to this function is allowed per endpoint before
785 * check_bandwidth() or reset_bandwidth() must be called.
786 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
787 * add the endpoint to the schedule with possibly new parameters denoted by a
788 * different endpoint descriptor in usb_host_endpoint.
789 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
790 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700791 *
792 * The USB core will not allow URBs to be queued to an endpoint until the
793 * configuration or alt setting is installed in the device, so there's no need
794 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700795 */
796int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
797 struct usb_host_endpoint *ep)
798{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700799 struct xhci_hcd *xhci;
800 struct xhci_device_control *in_ctx;
801 unsigned int ep_index;
802 struct xhci_ep_ctx *ep_ctx;
803 u32 added_ctxs;
804 unsigned int last_ctx;
805 u32 new_add_flags, new_drop_flags, new_slot_info;
806 int ret = 0;
807
808 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -0700809 if (ret <= 0) {
810 /* So we won't queue a reset ep command for a root hub */
811 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700812 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -0700813 }
Sarah Sharpf94e01862009-04-27 19:58:38 -0700814 xhci = hcd_to_xhci(hcd);
815
816 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
817 last_ctx = xhci_last_valid_endpoint(added_ctxs);
818 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
819 /* FIXME when we have to issue an evaluate endpoint command to
820 * deal with ep0 max packet size changing once we get the
821 * descriptors
822 */
823 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
824 __func__, added_ctxs);
825 return 0;
826 }
827
Sarah Sharpf94e01862009-04-27 19:58:38 -0700828 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
829 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
830 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700831 return -EINVAL;
832 }
833
834 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
835 ep_index = xhci_get_endpoint_index(&ep->desc);
836 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
837 /* If the HCD has already noted the endpoint is enabled,
838 * ignore this request.
839 */
840 if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700841 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
842 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700843 return 0;
844 }
845
Sarah Sharpf88ba782009-05-14 11:44:22 -0700846 /*
847 * Configuration and alternate setting changes must be done in
848 * process context, not interrupt context (or so documenation
849 * for usb_set_interface() and usb_set_configuration() claim).
850 */
851 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
852 udev, ep, GFP_KERNEL) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -0700853 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
854 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700855 return -ENOMEM;
856 }
857
858 in_ctx->add_flags |= added_ctxs;
859 new_add_flags = in_ctx->add_flags;
860
861 /* If xhci_endpoint_disable() was called for this endpoint, but the
862 * xHC hasn't been notified yet through the check_bandwidth() call,
863 * this re-adds a new state for the endpoint from the new endpoint
864 * descriptors. We must drop and re-add this endpoint, so we leave the
865 * drop flags alone.
866 */
867 new_drop_flags = in_ctx->drop_flags;
868
869 /* Update the last valid endpoint context, if we just added one past */
870 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
871 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
872 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
873 }
874 new_slot_info = in_ctx->slot.dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700875
Sarah Sharpa1587d92009-07-27 12:03:15 -0700876 /* Store the usb_device pointer for later use */
877 ep->hcpriv = udev;
878
Sarah Sharpf94e01862009-04-27 19:58:38 -0700879 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
880 (unsigned int) ep->desc.bEndpointAddress,
881 udev->slot_id,
882 (unsigned int) new_drop_flags,
883 (unsigned int) new_add_flags,
884 (unsigned int) new_slot_info);
885 return 0;
886}
887
888static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
889{
890 struct xhci_ep_ctx *ep_ctx;
891 int i;
892
893 /* When a device's add flag and drop flag are zero, any subsequent
894 * configure endpoint command will leave that endpoint's state
895 * untouched. Make sure we don't leave any old state in the input
896 * endpoint contexts.
897 */
898 virt_dev->in_ctx->drop_flags = 0;
899 virt_dev->in_ctx->add_flags = 0;
900 virt_dev->in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
901 /* Endpoint 0 is always valid */
902 virt_dev->in_ctx->slot.dev_info |= LAST_CTX(1);
903 for (i = 1; i < 31; ++i) {
904 ep_ctx = &virt_dev->in_ctx->ep[i];
905 ep_ctx->ep_info = 0;
906 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700907 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700908 ep_ctx->tx_info = 0;
909 }
910}
911
Sarah Sharpf88ba782009-05-14 11:44:22 -0700912/* Called after one or more calls to xhci_add_endpoint() or
913 * xhci_drop_endpoint(). If this call fails, the USB core is expected
914 * to call xhci_reset_bandwidth().
915 *
916 * Since we are in the middle of changing either configuration or
917 * installing a new alt setting, the USB core won't allow URBs to be
918 * enqueued for any endpoint on the old config or interface. Nothing
919 * else should be touching the xhci->devs[slot_id] structure, so we
920 * don't need to take the xhci->lock for manipulating that.
921 */
Sarah Sharpf94e01862009-04-27 19:58:38 -0700922int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
923{
924 int i;
925 int ret = 0;
926 int timeleft;
927 unsigned long flags;
928 struct xhci_hcd *xhci;
929 struct xhci_virt_device *virt_dev;
930
931 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
932 if (ret <= 0)
933 return ret;
934 xhci = hcd_to_xhci(hcd);
935
Sarah Sharpf94e01862009-04-27 19:58:38 -0700936 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
937 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
938 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700939 return -EINVAL;
940 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700941 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700942 virt_dev = xhci->devs[udev->slot_id];
943
944 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
945 virt_dev->in_ctx->add_flags |= SLOT_FLAG;
946 virt_dev->in_ctx->add_flags &= ~EP0_FLAG;
947 virt_dev->in_ctx->drop_flags &= ~SLOT_FLAG;
948 virt_dev->in_ctx->drop_flags &= ~EP0_FLAG;
949 xhci_dbg(xhci, "New Input Control Context:\n");
950 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
951 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
952
Sarah Sharpf88ba782009-05-14 11:44:22 -0700953 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -0700954 ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx_dma,
955 udev->slot_id);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700956 if (ret < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -0700957 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpf88ba782009-05-14 11:44:22 -0700958 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
Sarah Sharpf94e01862009-04-27 19:58:38 -0700959 return -ENOMEM;
960 }
Sarah Sharp23e3be12009-04-29 19:05:20 -0700961 xhci_ring_cmd_db(xhci);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700962 spin_unlock_irqrestore(&xhci->lock, flags);
963
964 /* Wait for the configure endpoint command to complete */
965 timeleft = wait_for_completion_interruptible_timeout(
966 &virt_dev->cmd_completion,
967 USB_CTRL_SET_TIMEOUT);
968 if (timeleft <= 0) {
969 xhci_warn(xhci, "%s while waiting for configure endpoint command\n",
970 timeleft == 0 ? "Timeout" : "Signal");
971 /* FIXME cancel the configure endpoint command */
972 return -ETIME;
973 }
974
Sarah Sharpf94e01862009-04-27 19:58:38 -0700975 switch (virt_dev->cmd_status) {
976 case COMP_ENOMEM:
977 dev_warn(&udev->dev, "Not enough host controller resources "
978 "for new device state.\n");
979 ret = -ENOMEM;
980 /* FIXME: can we allocate more resources for the HC? */
981 break;
982 case COMP_BW_ERR:
983 dev_warn(&udev->dev, "Not enough bandwidth "
984 "for new device state.\n");
985 ret = -ENOSPC;
986 /* FIXME: can we go back to the old state? */
987 break;
988 case COMP_TRB_ERR:
989 /* the HCD set up something wrong */
990 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, "
991 "and endpoint is not disabled.\n");
992 ret = -EINVAL;
993 break;
994 case COMP_SUCCESS:
995 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
996 break;
997 default:
998 xhci_err(xhci, "ERROR: unexpected command completion "
999 "code 0x%x.\n", virt_dev->cmd_status);
1000 ret = -EINVAL;
1001 break;
1002 }
1003 if (ret) {
1004 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001005 return ret;
1006 }
1007
1008 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
1009 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma,
1010 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
1011
1012 xhci_zero_in_ctx(virt_dev);
1013 /* Free any old rings */
1014 for (i = 1; i < 31; ++i) {
1015 if (virt_dev->new_ep_rings[i]) {
1016 xhci_ring_free(xhci, virt_dev->ep_rings[i]);
1017 virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
1018 virt_dev->new_ep_rings[i] = NULL;
1019 }
1020 }
1021
Sarah Sharpf94e01862009-04-27 19:58:38 -07001022 return ret;
1023}
1024
1025void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1026{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001027 struct xhci_hcd *xhci;
1028 struct xhci_virt_device *virt_dev;
1029 int i, ret;
1030
1031 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1032 if (ret <= 0)
1033 return;
1034 xhci = hcd_to_xhci(hcd);
1035
Sarah Sharpf94e01862009-04-27 19:58:38 -07001036 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1037 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1038 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001039 return;
1040 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001041 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001042 virt_dev = xhci->devs[udev->slot_id];
1043 /* Free any rings allocated for added endpoints */
1044 for (i = 0; i < 31; ++i) {
1045 if (virt_dev->new_ep_rings[i]) {
1046 xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
1047 virt_dev->new_ep_rings[i] = NULL;
1048 }
1049 }
1050 xhci_zero_in_ctx(virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001051}
1052
Sarah Sharpa1587d92009-07-27 12:03:15 -07001053/* Deal with stalled endpoints. The core should have sent the control message
1054 * to clear the halt condition. However, we need to make the xHCI hardware
1055 * reset its sequence number, since a device will expect a sequence number of
1056 * zero after the halt condition is cleared.
1057 * Context: in_interrupt
1058 */
1059void xhci_endpoint_reset(struct usb_hcd *hcd,
1060 struct usb_host_endpoint *ep)
1061{
1062 struct xhci_hcd *xhci;
1063 struct usb_device *udev;
1064 unsigned int ep_index;
1065 unsigned long flags;
1066 int ret;
1067
1068 xhci = hcd_to_xhci(hcd);
1069 udev = (struct usb_device *) ep->hcpriv;
1070 /* Called with a root hub endpoint (or an endpoint that wasn't added
1071 * with xhci_add_endpoint()
1072 */
1073 if (!ep->hcpriv)
1074 return;
1075 ep_index = xhci_get_endpoint_index(&ep->desc);
1076
1077 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1078 spin_lock_irqsave(&xhci->lock, flags);
1079 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
1080 if (!ret) {
1081 xhci_ring_cmd_db(xhci);
1082 }
1083 spin_unlock_irqrestore(&xhci->lock, flags);
1084
1085 if (ret)
1086 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1087}
1088
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001089/*
1090 * At this point, the struct usb_device is about to go away, the device has
1091 * disconnected, and all traffic has been stopped and the endpoints have been
1092 * disabled. Free any HC data structures associated with that device.
1093 */
1094void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1095{
1096 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1097 unsigned long flags;
1098
1099 if (udev->slot_id == 0)
1100 return;
1101
1102 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001103 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001104 spin_unlock_irqrestore(&xhci->lock, flags);
1105 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1106 return;
1107 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001108 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001109 spin_unlock_irqrestore(&xhci->lock, flags);
1110 /*
1111 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07001112 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001113 */
1114}
1115
1116/*
1117 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
1118 * timed out, or allocating memory failed. Returns 1 on success.
1119 */
1120int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
1121{
1122 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1123 unsigned long flags;
1124 int timeleft;
1125 int ret;
1126
1127 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001128 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001129 if (ret) {
1130 spin_unlock_irqrestore(&xhci->lock, flags);
1131 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1132 return 0;
1133 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001134 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001135 spin_unlock_irqrestore(&xhci->lock, flags);
1136
1137 /* XXX: how much time for xHC slot assignment? */
1138 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1139 USB_CTRL_SET_TIMEOUT);
1140 if (timeleft <= 0) {
1141 xhci_warn(xhci, "%s while waiting for a slot\n",
1142 timeleft == 0 ? "Timeout" : "Signal");
1143 /* FIXME cancel the enable slot request */
1144 return 0;
1145 }
1146
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001147 if (!xhci->slot_id) {
1148 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001149 return 0;
1150 }
Sarah Sharpf88ba782009-05-14 11:44:22 -07001151 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001152 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
1153 /* Disable slot, if we can do it without mem alloc */
1154 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07001155 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001156 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
1157 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001158 spin_unlock_irqrestore(&xhci->lock, flags);
1159 return 0;
1160 }
1161 udev->slot_id = xhci->slot_id;
1162 /* Is this a LS or FS device under a HS hub? */
1163 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001164 return 1;
1165}
1166
1167/*
1168 * Issue an Address Device command (which will issue a SetAddress request to
1169 * the device).
1170 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
1171 * we should only issue and wait on one address command at the same time.
1172 *
1173 * We add one to the device address issued by the hardware because the USB core
1174 * uses address 1 for the root hubs (even though they're not really devices).
1175 */
1176int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
1177{
1178 unsigned long flags;
1179 int timeleft;
1180 struct xhci_virt_device *virt_dev;
1181 int ret = 0;
1182 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp8e595a52009-07-27 12:03:31 -07001183 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001184
1185 if (!udev->slot_id) {
1186 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
1187 return -EINVAL;
1188 }
1189
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001190 virt_dev = xhci->devs[udev->slot_id];
1191
1192 /* If this is a Set Address to an unconfigured device, setup ep 0 */
1193 if (!udev->config)
1194 xhci_setup_addressable_virt_dev(xhci, udev);
1195 /* Otherwise, assume the core has the device configured how it wants */
Sarah Sharp66e49d82009-07-27 12:03:46 -07001196 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1197 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001198
Sarah Sharpf88ba782009-05-14 11:44:22 -07001199 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001200 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx_dma,
1201 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001202 if (ret) {
1203 spin_unlock_irqrestore(&xhci->lock, flags);
1204 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1205 return ret;
1206 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001207 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001208 spin_unlock_irqrestore(&xhci->lock, flags);
1209
1210 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
1211 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1212 USB_CTRL_SET_TIMEOUT);
1213 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
1214 * the SetAddress() "recovery interval" required by USB and aborting the
1215 * command on a timeout.
1216 */
1217 if (timeleft <= 0) {
1218 xhci_warn(xhci, "%s while waiting for a slot\n",
1219 timeleft == 0 ? "Timeout" : "Signal");
1220 /* FIXME cancel the address device command */
1221 return -ETIME;
1222 }
1223
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001224 switch (virt_dev->cmd_status) {
1225 case COMP_CTX_STATE:
1226 case COMP_EBADSLT:
1227 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
1228 udev->slot_id);
1229 ret = -EINVAL;
1230 break;
1231 case COMP_TX_ERR:
1232 dev_warn(&udev->dev, "Device not responding to set address.\n");
1233 ret = -EPROTO;
1234 break;
1235 case COMP_SUCCESS:
1236 xhci_dbg(xhci, "Successful Address Device command\n");
1237 break;
1238 default:
1239 xhci_err(xhci, "ERROR: unexpected command completion "
1240 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07001241 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1242 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001243 ret = -EINVAL;
1244 break;
1245 }
1246 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001247 return ret;
1248 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07001249 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
1250 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
1251 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001252 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07001253 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
1254 (unsigned long long)
1255 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001256 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
1257 (unsigned long long)virt_dev->out_ctx_dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001258 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1259 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
1260 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1261 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
1262 /*
1263 * USB core uses address 1 for the roothubs, so we add one to the
1264 * address given back to us by the HC.
1265 */
1266 udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001267 /* Zero the input context control for later use */
1268 virt_dev->in_ctx->add_flags = 0;
1269 virt_dev->in_ctx->drop_flags = 0;
1270 /* Mirror flags in the output context for future ep enable/disable */
1271 virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
1272 virt_dev->out_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001273
1274 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
1275 /* XXX Meh, not sure if anyone else but choose_address uses this. */
1276 set_bit(udev->devnum, udev->bus->devmap.devicemap);
1277
1278 return 0;
1279}
1280
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001281int xhci_get_frame(struct usb_hcd *hcd)
1282{
1283 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1284 /* EHCI mods by the periodic size. Why? */
1285 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
1286}
1287
1288MODULE_DESCRIPTION(DRIVER_DESC);
1289MODULE_AUTHOR(DRIVER_AUTHOR);
1290MODULE_LICENSE("GPL");
1291
1292static int __init xhci_hcd_init(void)
1293{
1294#ifdef CONFIG_PCI
1295 int retval = 0;
1296
1297 retval = xhci_register_pci();
1298
1299 if (retval < 0) {
1300 printk(KERN_DEBUG "Problem registering PCI driver.");
1301 return retval;
1302 }
1303#endif
Sarah Sharp98441972009-05-14 11:44:18 -07001304 /*
1305 * Check the compiler generated sizes of structures that must be laid
1306 * out in specific ways for hardware access.
1307 */
1308 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1309 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
1310 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
1311 /* xhci_device_control has eight fields, and also
1312 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
1313 */
1314 BUILD_BUG_ON(sizeof(struct xhci_device_control) != (8+8+8*31)*32/8);
1315 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
1316 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
1317 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
1318 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
1319 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
1320 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
1321 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
1322 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001323 return 0;
1324}
1325module_init(xhci_hcd_init);
1326
1327static void __exit xhci_hcd_cleanup(void)
1328{
1329#ifdef CONFIG_PCI
1330 xhci_unregister_pci();
1331#endif
1332}
1333module_exit(xhci_hcd_cleanup);