blob: 8c3a074a95cdf6f14e4d0427c27d16384ed46031 [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);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700106 if ((state & STS_HALT) == 0) {
107 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
108 return 0;
109 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700110
111 xhci_dbg(xhci, "// Reset the HC\n");
112 command = xhci_readl(xhci, &xhci->op_regs->command);
113 command |= CMD_RESET;
114 xhci_writel(xhci, command, &xhci->op_regs->command);
115 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
116 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
117
118 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
119}
120
121/*
122 * Stop the HC from processing the endpoint queues.
123 */
124static void xhci_quiesce(struct xhci_hcd *xhci)
125{
126 /*
127 * Queues are per endpoint, so we need to disable an endpoint or slot.
128 *
129 * To disable a slot, we need to insert a disable slot command on the
130 * command ring and ring the doorbell. This will also free any internal
131 * resources associated with the slot (which might not be what we want).
132 *
133 * A Release Endpoint command sounds better - doesn't free internal HC
134 * memory, but removes the endpoints from the schedule and releases the
135 * bandwidth, disables the doorbells, and clears the endpoint enable
136 * flag. Usually used prior to a set interface command.
137 *
138 * TODO: Implement after command ring code is done.
139 */
140 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
141 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
142}
143
144#if 0
145/* Set up MSI-X table for entry 0 (may claim other entries later) */
146static int xhci_setup_msix(struct xhci_hcd *xhci)
147{
148 int ret;
149 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
150
151 xhci->msix_count = 0;
152 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
153 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
154 if (!xhci->msix_entries) {
155 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
156 return -ENOMEM;
157 }
158 xhci->msix_entries[0].entry = 0;
159
160 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
161 if (ret) {
162 xhci_err(xhci, "Failed to enable MSI-X\n");
163 goto free_entries;
164 }
165
166 /*
167 * Pass the xhci pointer value as the request_irq "cookie".
168 * If more irqs are added, this will need to be unique for each one.
169 */
170 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
171 "xHCI", xhci_to_hcd(xhci));
172 if (ret) {
173 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
174 goto disable_msix;
175 }
176 xhci_dbg(xhci, "Finished setting up MSI-X\n");
177 return 0;
178
179disable_msix:
180 pci_disable_msix(pdev);
181free_entries:
182 kfree(xhci->msix_entries);
183 xhci->msix_entries = NULL;
184 return ret;
185}
186
187/* XXX: code duplication; can xhci_setup_msix call this? */
188/* Free any IRQs and disable MSI-X */
189static void xhci_cleanup_msix(struct xhci_hcd *xhci)
190{
191 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
192 if (!xhci->msix_entries)
193 return;
194
195 free_irq(xhci->msix_entries[0].vector, xhci);
196 pci_disable_msix(pdev);
197 kfree(xhci->msix_entries);
198 xhci->msix_entries = NULL;
199 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
200}
201#endif
202
203/*
204 * Initialize memory for HCD and xHC (one-time init).
205 *
206 * Program the PAGESIZE register, initialize the device context array, create
207 * device contexts (?), set up a command ring segment (or two?), create event
208 * ring (one for now).
209 */
210int xhci_init(struct usb_hcd *hcd)
211{
212 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
213 int retval = 0;
214
215 xhci_dbg(xhci, "xhci_init\n");
216 spin_lock_init(&xhci->lock);
217 retval = xhci_mem_init(xhci, GFP_KERNEL);
218 xhci_dbg(xhci, "Finished xhci_init\n");
219
220 return retval;
221}
222
223/*
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700224 * Called in interrupt context when there might be work
225 * queued on the event ring
226 *
227 * xhci->lock must be held by caller.
228 */
229static void xhci_work(struct xhci_hcd *xhci)
230{
231 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700232 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700233
234 /*
235 * Clear the op reg interrupt status first,
236 * so we can receive interrupts from other MSI-X interrupters.
237 * Write 1 to clear the interrupt status.
238 */
239 temp = xhci_readl(xhci, &xhci->op_regs->status);
240 temp |= STS_EINT;
241 xhci_writel(xhci, temp, &xhci->op_regs->status);
242 /* FIXME when MSI-X is supported and there are multiple vectors */
243 /* Clear the MSI-X event interrupt status */
244
245 /* Acknowledge the interrupt */
246 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
247 temp |= 0x3;
248 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
249 /* Flush posted writes */
250 xhci_readl(xhci, &xhci->ir_set->irq_pending);
251
252 /* FIXME this should be a delayed service routine that clears the EHB */
Stephen Rothwellb7258a42009-04-29 19:02:47 -0700253 xhci_handle_event(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700254
Sarah Sharp2d831092009-07-27 12:03:40 -0700255 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
Sarah Sharp8e595a52009-07-27 12:03:31 -0700256 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp2d831092009-07-27 12:03:40 -0700257 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700258 /* Flush posted writes -- FIXME is this necessary? */
259 xhci_readl(xhci, &xhci->ir_set->irq_pending);
260}
261
262/*-------------------------------------------------------------------------*/
263
264/*
265 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
266 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
267 * indicators of an event TRB error, but we check the status *first* to be safe.
268 */
269irqreturn_t xhci_irq(struct usb_hcd *hcd)
270{
271 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
272 u32 temp, temp2;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700273 union xhci_trb *trb;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700274
275 spin_lock(&xhci->lock);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700276 trb = xhci->event_ring->dequeue;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700277 /* Check if the xHC generated the interrupt, or the irq is shared */
278 temp = xhci_readl(xhci, &xhci->op_regs->status);
279 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
280 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
281 spin_unlock(&xhci->lock);
282 return IRQ_NONE;
283 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700284 xhci_dbg(xhci, "op reg status = %08x\n", temp);
285 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
286 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
287 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
288 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
289 lower_32_bits(trb->link.segment_ptr),
290 upper_32_bits(trb->link.segment_ptr),
291 (unsigned int) trb->link.intr_target,
292 (unsigned int) trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700293
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700294 if (temp & STS_FATAL) {
295 xhci_warn(xhci, "WARNING: Host System Error\n");
296 xhci_halt(xhci);
297 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
Sarah Sharpc96a2b82009-04-29 19:05:40 -0700298 spin_unlock(&xhci->lock);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700299 return -ESHUTDOWN;
300 }
301
302 xhci_work(xhci);
303 spin_unlock(&xhci->lock);
304
305 return IRQ_HANDLED;
306}
307
308#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700309void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700310{
311 unsigned long flags;
312 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700313 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700314 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
315 int i, j;
316
317 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
318
319 spin_lock_irqsave(&xhci->lock, flags);
320 temp = xhci_readl(xhci, &xhci->op_regs->status);
321 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
322 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
323 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
324 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
325 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
326 xhci->error_bitmask = 0;
327 xhci_dbg(xhci, "Event ring:\n");
328 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
329 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700330 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
331 temp_64 &= ~ERST_PTR_MASK;
332 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700333 xhci_dbg(xhci, "Command ring:\n");
334 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
335 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
336 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700337 for (i = 0; i < MAX_HC_SLOTS; ++i) {
338 if (xhci->devs[i]) {
339 for (j = 0; j < 31; ++j) {
340 if (xhci->devs[i]->ep_rings[j]) {
341 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
342 xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
343 }
344 }
345 }
346 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700347
348 if (xhci->noops_submitted != NUM_TEST_NOOPS)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700349 if (xhci_setup_one_noop(xhci))
350 xhci_ring_cmd_db(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700351 spin_unlock_irqrestore(&xhci->lock, flags);
352
353 if (!xhci->zombie)
354 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
355 else
356 xhci_dbg(xhci, "Quit polling the event ring.\n");
357}
358#endif
359
360/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700361 * Start the HC after it was halted.
362 *
363 * This function is called by the USB core when the HC driver is added.
364 * Its opposite is xhci_stop().
365 *
366 * xhci_init() must be called once before this function can be called.
367 * Reset the HC, enable device slot contexts, program DCBAAP, and
368 * set command ring pointer and event ring pointer.
369 *
370 * Setup MSI-X vectors and enable interrupts.
371 */
372int xhci_run(struct usb_hcd *hcd)
373{
374 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700375 u64 temp_64;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700376 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700377 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700378
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700379 hcd->uses_new_polling = 1;
380 hcd->poll_rh = 0;
381
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700382 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700383#if 0 /* FIXME: MSI not setup yet */
384 /* Do this at the very last minute */
385 ret = xhci_setup_msix(xhci);
386 if (!ret)
387 return ret;
388
389 return -ENOSYS;
390#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700391#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
392 init_timer(&xhci->event_ring_timer);
393 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700394 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700395 /* Poll the event ring */
396 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
397 xhci->zombie = 0;
398 xhci_dbg(xhci, "Setting event ring polling timer\n");
399 add_timer(&xhci->event_ring_timer);
400#endif
401
Sarah Sharp66e49d82009-07-27 12:03:46 -0700402 xhci_dbg(xhci, "Command ring memory map follows:\n");
403 xhci_debug_ring(xhci, xhci->cmd_ring);
404 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
405 xhci_dbg_cmd_ptrs(xhci);
406
407 xhci_dbg(xhci, "ERST memory map follows:\n");
408 xhci_dbg_erst(xhci, &xhci->erst);
409 xhci_dbg(xhci, "Event ring:\n");
410 xhci_debug_ring(xhci, xhci->event_ring);
411 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
412 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
413 temp_64 &= ~ERST_PTR_MASK;
414 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
415
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700416 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
417 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700418 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700419 temp |= (u32) 160;
420 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
421
422 /* Set the HCD state before we enable the irqs */
423 hcd->state = HC_STATE_RUNNING;
424 temp = xhci_readl(xhci, &xhci->op_regs->command);
425 temp |= (CMD_EIE);
426 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
427 temp);
428 xhci_writel(xhci, temp, &xhci->op_regs->command);
429
430 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700431 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
432 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700433 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
434 &xhci->ir_set->irq_pending);
435 xhci_print_ir_set(xhci, xhci->ir_set, 0);
436
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700437 if (NUM_TEST_NOOPS > 0)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700438 doorbell = xhci_setup_one_noop(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700439
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700440 temp = xhci_readl(xhci, &xhci->op_regs->command);
441 temp |= (CMD_RUN);
442 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
443 temp);
444 xhci_writel(xhci, temp, &xhci->op_regs->command);
445 /* Flush PCI posted writes */
446 temp = xhci_readl(xhci, &xhci->op_regs->command);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700447 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700448 if (doorbell)
449 (*doorbell)(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700450
451 xhci_dbg(xhci, "Finished xhci_run\n");
452 return 0;
453}
454
455/*
456 * Stop xHCI driver.
457 *
458 * This function is called by the USB core when the HC driver is removed.
459 * Its opposite is xhci_run().
460 *
461 * Disable device contexts, disable IRQs, and quiesce the HC.
462 * Reset the HC, finish any completed transactions, and cleanup memory.
463 */
464void xhci_stop(struct usb_hcd *hcd)
465{
466 u32 temp;
467 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
468
469 spin_lock_irq(&xhci->lock);
470 if (HC_IS_RUNNING(hcd->state))
471 xhci_quiesce(xhci);
472 xhci_halt(xhci);
473 xhci_reset(xhci);
474 spin_unlock_irq(&xhci->lock);
475
476#if 0 /* No MSI yet */
477 xhci_cleanup_msix(xhci);
478#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700479#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
480 /* Tell the event ring poll function not to reschedule */
481 xhci->zombie = 1;
482 del_timer_sync(&xhci->event_ring_timer);
483#endif
484
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700485 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
486 temp = xhci_readl(xhci, &xhci->op_regs->status);
487 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
488 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
489 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
490 &xhci->ir_set->irq_pending);
491 xhci_print_ir_set(xhci, xhci->ir_set, 0);
492
493 xhci_dbg(xhci, "cleaning up memory\n");
494 xhci_mem_cleanup(xhci);
495 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
496 xhci_readl(xhci, &xhci->op_regs->status));
497}
498
499/*
500 * Shutdown HC (not bus-specific)
501 *
502 * This is called when the machine is rebooting or halting. We assume that the
503 * machine will be powered off, and the HC's internal state will be reset.
504 * Don't bother to free memory.
505 */
506void xhci_shutdown(struct usb_hcd *hcd)
507{
508 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
509
510 spin_lock_irq(&xhci->lock);
511 xhci_halt(xhci);
512 spin_unlock_irq(&xhci->lock);
513
514#if 0
515 xhci_cleanup_msix(xhci);
516#endif
517
518 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
519 xhci_readl(xhci, &xhci->op_regs->status));
520}
521
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700522/*-------------------------------------------------------------------------*/
523
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700524/**
525 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
526 * HCDs. Find the index for an endpoint given its descriptor. Use the return
527 * value to right shift 1 for the bitmask.
528 *
529 * Index = (epnum * 2) + direction - 1,
530 * where direction = 0 for OUT, 1 for IN.
531 * For control endpoints, the IN index is used (OUT index is unused), so
532 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
533 */
534unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
535{
536 unsigned int index;
537 if (usb_endpoint_xfer_control(desc))
538 index = (unsigned int) (usb_endpoint_num(desc)*2);
539 else
540 index = (unsigned int) (usb_endpoint_num(desc)*2) +
541 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
542 return index;
543}
544
Sarah Sharpf94e01862009-04-27 19:58:38 -0700545/* Find the flag for this endpoint (for use in the control context). Use the
546 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
547 * bit 1, etc.
548 */
549unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
550{
551 return 1 << (xhci_get_endpoint_index(desc) + 1);
552}
553
554/* Compute the last valid endpoint context index. Basically, this is the
555 * endpoint index plus one. For slot contexts with more than valid endpoint,
556 * we find the most significant bit set in the added contexts flags.
557 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
558 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
559 */
560static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
561{
562 return fls(added_ctxs) - 1;
563}
564
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700565/* Returns 1 if the arguments are OK;
566 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
567 */
568int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
569 struct usb_host_endpoint *ep, int check_ep, const char *func) {
570 if (!hcd || (check_ep && !ep) || !udev) {
571 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
572 func);
573 return -EINVAL;
574 }
575 if (!udev->parent) {
576 printk(KERN_DEBUG "xHCI %s called for root hub\n",
577 func);
578 return 0;
579 }
580 if (!udev->slot_id) {
581 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
582 func);
583 return -EINVAL;
584 }
585 return 1;
586}
587
588/*
589 * non-error returns are a promise to giveback() the urb later
590 * we drop ownership so next owner (or urb unlink) can get it
591 */
592int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
593{
594 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
595 unsigned long flags;
596 int ret = 0;
597 unsigned int slot_id, ep_index;
598
599 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
600 return -EINVAL;
601
602 slot_id = urb->dev->slot_id;
603 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700604
605 spin_lock_irqsave(&xhci->lock, flags);
606 if (!xhci->devs || !xhci->devs[slot_id]) {
607 if (!in_interrupt())
608 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
Sarah Sharpc7959fb2009-04-29 19:06:36 -0700609 ret = -EINVAL;
610 goto exit;
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700611 }
612 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
613 if (!in_interrupt())
614 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
615 ret = -ESHUTDOWN;
616 goto exit;
617 }
Sarah Sharpb10de142009-04-27 19:58:50 -0700618 if (usb_endpoint_xfer_control(&urb->ep->desc))
Sarah Sharpb11069f2009-07-27 12:03:23 -0700619 /* We have a spinlock and interrupts disabled, so we must pass
620 * atomic context to this function, which may allocate memory.
621 */
622 ret = xhci_queue_ctrl_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 if (usb_endpoint_xfer_bulk(&urb->ep->desc))
Sarah Sharpb11069f2009-07-27 12:03:23 -0700625 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700626 slot_id, ep_index);
Sarah Sharpb10de142009-04-27 19:58:50 -0700627 else
628 ret = -EINVAL;
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700629exit:
630 spin_unlock_irqrestore(&xhci->lock, flags);
631 return ret;
632}
633
Sarah Sharpae636742009-04-29 19:02:31 -0700634/*
635 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
636 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
637 * should pick up where it left off in the TD, unless a Set Transfer Ring
638 * Dequeue Pointer is issued.
639 *
640 * The TRBs that make up the buffers for the canceled URB will be "removed" from
641 * the ring. Since the ring is a contiguous structure, they can't be physically
642 * removed. Instead, there are two options:
643 *
644 * 1) If the HC is in the middle of processing the URB to be canceled, we
645 * simply move the ring's dequeue pointer past those TRBs using the Set
646 * Transfer Ring Dequeue Pointer command. This will be the common case,
647 * when drivers timeout on the last submitted URB and attempt to cancel.
648 *
649 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
650 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
651 * HC will need to invalidate the any TRBs it has cached after the stop
652 * endpoint command, as noted in the xHCI 0.95 errata.
653 *
654 * 3) The TD may have completed by the time the Stop Endpoint Command
655 * completes, so software needs to handle that case too.
656 *
657 * This function should protect against the TD enqueueing code ringing the
658 * doorbell while this code is waiting for a Stop Endpoint command to complete.
659 * It also needs to account for multiple cancellations on happening at the same
660 * time for the same endpoint.
661 *
662 * Note that this function can be called in any context, or so says
663 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700664 */
665int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
666{
Sarah Sharpae636742009-04-29 19:02:31 -0700667 unsigned long flags;
668 int ret;
669 struct xhci_hcd *xhci;
670 struct xhci_td *td;
671 unsigned int ep_index;
672 struct xhci_ring *ep_ring;
673
674 xhci = hcd_to_xhci(hcd);
675 spin_lock_irqsave(&xhci->lock, flags);
676 /* Make sure the URB hasn't completed or been unlinked already */
677 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
678 if (ret || !urb->hcpriv)
679 goto done;
680
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700681 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700682 xhci_dbg(xhci, "Event ring:\n");
683 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700684 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
685 ep_ring = xhci->devs[urb->dev->slot_id]->ep_rings[ep_index];
Sarah Sharp66e49d82009-07-27 12:03:46 -0700686 xhci_dbg(xhci, "Endpoint ring:\n");
687 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700688 td = (struct xhci_td *) urb->hcpriv;
689
690 ep_ring->cancels_pending++;
691 list_add_tail(&td->cancelled_td_list, &ep_ring->cancelled_td_list);
692 /* Queue a stop endpoint command, but only if this is
693 * the first cancellation to be handled.
694 */
695 if (ep_ring->cancels_pending == 1) {
Sarah Sharp23e3be12009-04-29 19:05:20 -0700696 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
697 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700698 }
699done:
700 spin_unlock_irqrestore(&xhci->lock, flags);
701 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700702}
703
Sarah Sharpf94e01862009-04-27 19:58:38 -0700704/* Drop an endpoint from a new bandwidth configuration for this device.
705 * Only one call to this function is allowed per endpoint before
706 * check_bandwidth() or reset_bandwidth() must be called.
707 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
708 * add the endpoint to the schedule with possibly new parameters denoted by a
709 * different endpoint descriptor in usb_host_endpoint.
710 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
711 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700712 *
713 * The USB core will not allow URBs to be queued to an endpoint that is being
714 * disabled, so there's no need for mutual exclusion to protect
715 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700716 */
717int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
718 struct usb_host_endpoint *ep)
719{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700720 struct xhci_hcd *xhci;
721 struct xhci_device_control *in_ctx;
722 unsigned int last_ctx;
723 unsigned int ep_index;
724 struct xhci_ep_ctx *ep_ctx;
725 u32 drop_flag;
726 u32 new_add_flags, new_drop_flags, new_slot_info;
727 int ret;
728
729 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700730 if (ret <= 0)
731 return ret;
732 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700733 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700734
735 drop_flag = xhci_get_endpoint_flag(&ep->desc);
736 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
737 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
738 __func__, drop_flag);
739 return 0;
740 }
741
Sarah Sharpf94e01862009-04-27 19:58:38 -0700742 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
743 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
744 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700745 return -EINVAL;
746 }
747
748 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
749 ep_index = xhci_get_endpoint_index(&ep->desc);
750 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
751 /* If the HC already knows the endpoint is disabled,
752 * or the HCD has noted it is disabled, ignore this request
753 */
754 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
755 in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700756 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
757 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700758 return 0;
759 }
760
761 in_ctx->drop_flags |= drop_flag;
762 new_drop_flags = in_ctx->drop_flags;
763
764 in_ctx->add_flags = ~drop_flag;
765 new_add_flags = in_ctx->add_flags;
766
767 last_ctx = xhci_last_valid_endpoint(in_ctx->add_flags);
768 /* Update the last valid endpoint context, if we deleted the last one */
769 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
770 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
771 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
772 }
773 new_slot_info = in_ctx->slot.dev_info;
774
775 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
776
Sarah Sharpf94e01862009-04-27 19:58:38 -0700777 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
778 (unsigned int) ep->desc.bEndpointAddress,
779 udev->slot_id,
780 (unsigned int) new_drop_flags,
781 (unsigned int) new_add_flags,
782 (unsigned int) new_slot_info);
783 return 0;
784}
785
786/* Add an endpoint to a new possible bandwidth configuration for this device.
787 * Only one call to this function is allowed per endpoint before
788 * check_bandwidth() or reset_bandwidth() must be called.
789 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
790 * add the endpoint to the schedule with possibly new parameters denoted by a
791 * different endpoint descriptor in usb_host_endpoint.
792 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
793 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700794 *
795 * The USB core will not allow URBs to be queued to an endpoint until the
796 * configuration or alt setting is installed in the device, so there's no need
797 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700798 */
799int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
800 struct usb_host_endpoint *ep)
801{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700802 struct xhci_hcd *xhci;
803 struct xhci_device_control *in_ctx;
804 unsigned int ep_index;
805 struct xhci_ep_ctx *ep_ctx;
806 u32 added_ctxs;
807 unsigned int last_ctx;
808 u32 new_add_flags, new_drop_flags, new_slot_info;
809 int ret = 0;
810
811 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -0700812 if (ret <= 0) {
813 /* So we won't queue a reset ep command for a root hub */
814 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700815 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -0700816 }
Sarah Sharpf94e01862009-04-27 19:58:38 -0700817 xhci = hcd_to_xhci(hcd);
818
819 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
820 last_ctx = xhci_last_valid_endpoint(added_ctxs);
821 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
822 /* FIXME when we have to issue an evaluate endpoint command to
823 * deal with ep0 max packet size changing once we get the
824 * descriptors
825 */
826 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
827 __func__, added_ctxs);
828 return 0;
829 }
830
Sarah Sharpf94e01862009-04-27 19:58:38 -0700831 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
832 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
833 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700834 return -EINVAL;
835 }
836
837 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
838 ep_index = xhci_get_endpoint_index(&ep->desc);
839 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
840 /* If the HCD has already noted the endpoint is enabled,
841 * ignore this request.
842 */
843 if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700844 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
845 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700846 return 0;
847 }
848
Sarah Sharpf88ba782009-05-14 11:44:22 -0700849 /*
850 * Configuration and alternate setting changes must be done in
851 * process context, not interrupt context (or so documenation
852 * for usb_set_interface() and usb_set_configuration() claim).
853 */
854 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
855 udev, ep, GFP_KERNEL) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -0700856 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
857 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700858 return -ENOMEM;
859 }
860
861 in_ctx->add_flags |= added_ctxs;
862 new_add_flags = in_ctx->add_flags;
863
864 /* If xhci_endpoint_disable() was called for this endpoint, but the
865 * xHC hasn't been notified yet through the check_bandwidth() call,
866 * this re-adds a new state for the endpoint from the new endpoint
867 * descriptors. We must drop and re-add this endpoint, so we leave the
868 * drop flags alone.
869 */
870 new_drop_flags = in_ctx->drop_flags;
871
872 /* Update the last valid endpoint context, if we just added one past */
873 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
874 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
875 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
876 }
877 new_slot_info = in_ctx->slot.dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700878
Sarah Sharpa1587d92009-07-27 12:03:15 -0700879 /* Store the usb_device pointer for later use */
880 ep->hcpriv = udev;
881
Sarah Sharpf94e01862009-04-27 19:58:38 -0700882 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
883 (unsigned int) ep->desc.bEndpointAddress,
884 udev->slot_id,
885 (unsigned int) new_drop_flags,
886 (unsigned int) new_add_flags,
887 (unsigned int) new_slot_info);
888 return 0;
889}
890
891static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
892{
893 struct xhci_ep_ctx *ep_ctx;
894 int i;
895
896 /* When a device's add flag and drop flag are zero, any subsequent
897 * configure endpoint command will leave that endpoint's state
898 * untouched. Make sure we don't leave any old state in the input
899 * endpoint contexts.
900 */
901 virt_dev->in_ctx->drop_flags = 0;
902 virt_dev->in_ctx->add_flags = 0;
903 virt_dev->in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
904 /* Endpoint 0 is always valid */
905 virt_dev->in_ctx->slot.dev_info |= LAST_CTX(1);
906 for (i = 1; i < 31; ++i) {
907 ep_ctx = &virt_dev->in_ctx->ep[i];
908 ep_ctx->ep_info = 0;
909 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700910 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700911 ep_ctx->tx_info = 0;
912 }
913}
914
Sarah Sharpf88ba782009-05-14 11:44:22 -0700915/* Called after one or more calls to xhci_add_endpoint() or
916 * xhci_drop_endpoint(). If this call fails, the USB core is expected
917 * to call xhci_reset_bandwidth().
918 *
919 * Since we are in the middle of changing either configuration or
920 * installing a new alt setting, the USB core won't allow URBs to be
921 * enqueued for any endpoint on the old config or interface. Nothing
922 * else should be touching the xhci->devs[slot_id] structure, so we
923 * don't need to take the xhci->lock for manipulating that.
924 */
Sarah Sharpf94e01862009-04-27 19:58:38 -0700925int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
926{
927 int i;
928 int ret = 0;
929 int timeleft;
930 unsigned long flags;
931 struct xhci_hcd *xhci;
932 struct xhci_virt_device *virt_dev;
933
934 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
935 if (ret <= 0)
936 return ret;
937 xhci = hcd_to_xhci(hcd);
938
Sarah Sharpf94e01862009-04-27 19:58:38 -0700939 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
940 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
941 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700942 return -EINVAL;
943 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700944 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700945 virt_dev = xhci->devs[udev->slot_id];
946
947 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
948 virt_dev->in_ctx->add_flags |= SLOT_FLAG;
949 virt_dev->in_ctx->add_flags &= ~EP0_FLAG;
950 virt_dev->in_ctx->drop_flags &= ~SLOT_FLAG;
951 virt_dev->in_ctx->drop_flags &= ~EP0_FLAG;
952 xhci_dbg(xhci, "New Input Control Context:\n");
953 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
954 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
955
Sarah Sharpf88ba782009-05-14 11:44:22 -0700956 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -0700957 ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx_dma,
958 udev->slot_id);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700959 if (ret < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -0700960 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpf88ba782009-05-14 11:44:22 -0700961 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
Sarah Sharpf94e01862009-04-27 19:58:38 -0700962 return -ENOMEM;
963 }
Sarah Sharp23e3be12009-04-29 19:05:20 -0700964 xhci_ring_cmd_db(xhci);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700965 spin_unlock_irqrestore(&xhci->lock, flags);
966
967 /* Wait for the configure endpoint command to complete */
968 timeleft = wait_for_completion_interruptible_timeout(
969 &virt_dev->cmd_completion,
970 USB_CTRL_SET_TIMEOUT);
971 if (timeleft <= 0) {
972 xhci_warn(xhci, "%s while waiting for configure endpoint command\n",
973 timeleft == 0 ? "Timeout" : "Signal");
974 /* FIXME cancel the configure endpoint command */
975 return -ETIME;
976 }
977
Sarah Sharpf94e01862009-04-27 19:58:38 -0700978 switch (virt_dev->cmd_status) {
979 case COMP_ENOMEM:
980 dev_warn(&udev->dev, "Not enough host controller resources "
981 "for new device state.\n");
982 ret = -ENOMEM;
983 /* FIXME: can we allocate more resources for the HC? */
984 break;
985 case COMP_BW_ERR:
986 dev_warn(&udev->dev, "Not enough bandwidth "
987 "for new device state.\n");
988 ret = -ENOSPC;
989 /* FIXME: can we go back to the old state? */
990 break;
991 case COMP_TRB_ERR:
992 /* the HCD set up something wrong */
993 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, "
994 "and endpoint is not disabled.\n");
995 ret = -EINVAL;
996 break;
997 case COMP_SUCCESS:
998 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
999 break;
1000 default:
1001 xhci_err(xhci, "ERROR: unexpected command completion "
1002 "code 0x%x.\n", virt_dev->cmd_status);
1003 ret = -EINVAL;
1004 break;
1005 }
1006 if (ret) {
1007 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001008 return ret;
1009 }
1010
1011 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
1012 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma,
1013 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
1014
1015 xhci_zero_in_ctx(virt_dev);
1016 /* Free any old rings */
1017 for (i = 1; i < 31; ++i) {
1018 if (virt_dev->new_ep_rings[i]) {
1019 xhci_ring_free(xhci, virt_dev->ep_rings[i]);
1020 virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
1021 virt_dev->new_ep_rings[i] = NULL;
1022 }
1023 }
1024
Sarah Sharpf94e01862009-04-27 19:58:38 -07001025 return ret;
1026}
1027
1028void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1029{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001030 struct xhci_hcd *xhci;
1031 struct xhci_virt_device *virt_dev;
1032 int i, ret;
1033
1034 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1035 if (ret <= 0)
1036 return;
1037 xhci = hcd_to_xhci(hcd);
1038
Sarah Sharpf94e01862009-04-27 19:58:38 -07001039 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1040 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1041 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001042 return;
1043 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001044 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001045 virt_dev = xhci->devs[udev->slot_id];
1046 /* Free any rings allocated for added endpoints */
1047 for (i = 0; i < 31; ++i) {
1048 if (virt_dev->new_ep_rings[i]) {
1049 xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
1050 virt_dev->new_ep_rings[i] = NULL;
1051 }
1052 }
1053 xhci_zero_in_ctx(virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001054}
1055
Sarah Sharpa1587d92009-07-27 12:03:15 -07001056/* Deal with stalled endpoints. The core should have sent the control message
1057 * to clear the halt condition. However, we need to make the xHCI hardware
1058 * reset its sequence number, since a device will expect a sequence number of
1059 * zero after the halt condition is cleared.
1060 * Context: in_interrupt
1061 */
1062void xhci_endpoint_reset(struct usb_hcd *hcd,
1063 struct usb_host_endpoint *ep)
1064{
1065 struct xhci_hcd *xhci;
1066 struct usb_device *udev;
1067 unsigned int ep_index;
1068 unsigned long flags;
1069 int ret;
1070
1071 xhci = hcd_to_xhci(hcd);
1072 udev = (struct usb_device *) ep->hcpriv;
1073 /* Called with a root hub endpoint (or an endpoint that wasn't added
1074 * with xhci_add_endpoint()
1075 */
1076 if (!ep->hcpriv)
1077 return;
1078 ep_index = xhci_get_endpoint_index(&ep->desc);
1079
1080 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1081 spin_lock_irqsave(&xhci->lock, flags);
1082 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
1083 if (!ret) {
1084 xhci_ring_cmd_db(xhci);
1085 }
1086 spin_unlock_irqrestore(&xhci->lock, flags);
1087
1088 if (ret)
1089 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1090}
1091
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001092/*
1093 * At this point, the struct usb_device is about to go away, the device has
1094 * disconnected, and all traffic has been stopped and the endpoints have been
1095 * disabled. Free any HC data structures associated with that device.
1096 */
1097void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1098{
1099 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1100 unsigned long flags;
1101
1102 if (udev->slot_id == 0)
1103 return;
1104
1105 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001106 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001107 spin_unlock_irqrestore(&xhci->lock, flags);
1108 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1109 return;
1110 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001111 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001112 spin_unlock_irqrestore(&xhci->lock, flags);
1113 /*
1114 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07001115 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001116 */
1117}
1118
1119/*
1120 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
1121 * timed out, or allocating memory failed. Returns 1 on success.
1122 */
1123int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
1124{
1125 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1126 unsigned long flags;
1127 int timeleft;
1128 int ret;
1129
1130 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001131 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001132 if (ret) {
1133 spin_unlock_irqrestore(&xhci->lock, flags);
1134 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1135 return 0;
1136 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001137 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001138 spin_unlock_irqrestore(&xhci->lock, flags);
1139
1140 /* XXX: how much time for xHC slot assignment? */
1141 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1142 USB_CTRL_SET_TIMEOUT);
1143 if (timeleft <= 0) {
1144 xhci_warn(xhci, "%s while waiting for a slot\n",
1145 timeleft == 0 ? "Timeout" : "Signal");
1146 /* FIXME cancel the enable slot request */
1147 return 0;
1148 }
1149
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001150 if (!xhci->slot_id) {
1151 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001152 return 0;
1153 }
Sarah Sharpf88ba782009-05-14 11:44:22 -07001154 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001155 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
1156 /* Disable slot, if we can do it without mem alloc */
1157 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07001158 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001159 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
1160 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001161 spin_unlock_irqrestore(&xhci->lock, flags);
1162 return 0;
1163 }
1164 udev->slot_id = xhci->slot_id;
1165 /* Is this a LS or FS device under a HS hub? */
1166 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001167 return 1;
1168}
1169
1170/*
1171 * Issue an Address Device command (which will issue a SetAddress request to
1172 * the device).
1173 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
1174 * we should only issue and wait on one address command at the same time.
1175 *
1176 * We add one to the device address issued by the hardware because the USB core
1177 * uses address 1 for the root hubs (even though they're not really devices).
1178 */
1179int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
1180{
1181 unsigned long flags;
1182 int timeleft;
1183 struct xhci_virt_device *virt_dev;
1184 int ret = 0;
1185 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp8e595a52009-07-27 12:03:31 -07001186 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001187
1188 if (!udev->slot_id) {
1189 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
1190 return -EINVAL;
1191 }
1192
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001193 virt_dev = xhci->devs[udev->slot_id];
1194
1195 /* If this is a Set Address to an unconfigured device, setup ep 0 */
1196 if (!udev->config)
1197 xhci_setup_addressable_virt_dev(xhci, udev);
1198 /* Otherwise, assume the core has the device configured how it wants */
Sarah Sharp66e49d82009-07-27 12:03:46 -07001199 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1200 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001201
Sarah Sharpf88ba782009-05-14 11:44:22 -07001202 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001203 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx_dma,
1204 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001205 if (ret) {
1206 spin_unlock_irqrestore(&xhci->lock, flags);
1207 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1208 return ret;
1209 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001210 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001211 spin_unlock_irqrestore(&xhci->lock, flags);
1212
1213 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
1214 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1215 USB_CTRL_SET_TIMEOUT);
1216 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
1217 * the SetAddress() "recovery interval" required by USB and aborting the
1218 * command on a timeout.
1219 */
1220 if (timeleft <= 0) {
1221 xhci_warn(xhci, "%s while waiting for a slot\n",
1222 timeleft == 0 ? "Timeout" : "Signal");
1223 /* FIXME cancel the address device command */
1224 return -ETIME;
1225 }
1226
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001227 switch (virt_dev->cmd_status) {
1228 case COMP_CTX_STATE:
1229 case COMP_EBADSLT:
1230 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
1231 udev->slot_id);
1232 ret = -EINVAL;
1233 break;
1234 case COMP_TX_ERR:
1235 dev_warn(&udev->dev, "Device not responding to set address.\n");
1236 ret = -EPROTO;
1237 break;
1238 case COMP_SUCCESS:
1239 xhci_dbg(xhci, "Successful Address Device command\n");
1240 break;
1241 default:
1242 xhci_err(xhci, "ERROR: unexpected command completion "
1243 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07001244 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1245 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001246 ret = -EINVAL;
1247 break;
1248 }
1249 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001250 return ret;
1251 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07001252 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
1253 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
1254 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001255 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07001256 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
1257 (unsigned long long)
1258 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001259 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
1260 (unsigned long long)virt_dev->out_ctx_dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001261 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1262 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
1263 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1264 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
1265 /*
1266 * USB core uses address 1 for the roothubs, so we add one to the
1267 * address given back to us by the HC.
1268 */
1269 udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001270 /* Zero the input context control for later use */
1271 virt_dev->in_ctx->add_flags = 0;
1272 virt_dev->in_ctx->drop_flags = 0;
1273 /* Mirror flags in the output context for future ep enable/disable */
1274 virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
1275 virt_dev->out_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001276
1277 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
1278 /* XXX Meh, not sure if anyone else but choose_address uses this. */
1279 set_bit(udev->devnum, udev->bus->devmap.devicemap);
1280
1281 return 0;
1282}
1283
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001284int xhci_get_frame(struct usb_hcd *hcd)
1285{
1286 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1287 /* EHCI mods by the periodic size. Why? */
1288 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
1289}
1290
1291MODULE_DESCRIPTION(DRIVER_DESC);
1292MODULE_AUTHOR(DRIVER_AUTHOR);
1293MODULE_LICENSE("GPL");
1294
1295static int __init xhci_hcd_init(void)
1296{
1297#ifdef CONFIG_PCI
1298 int retval = 0;
1299
1300 retval = xhci_register_pci();
1301
1302 if (retval < 0) {
1303 printk(KERN_DEBUG "Problem registering PCI driver.");
1304 return retval;
1305 }
1306#endif
Sarah Sharp98441972009-05-14 11:44:18 -07001307 /*
1308 * Check the compiler generated sizes of structures that must be laid
1309 * out in specific ways for hardware access.
1310 */
1311 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1312 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
1313 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
1314 /* xhci_device_control has eight fields, and also
1315 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
1316 */
1317 BUILD_BUG_ON(sizeof(struct xhci_device_control) != (8+8+8*31)*32/8);
1318 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
1319 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
1320 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
1321 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
1322 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
1323 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
1324 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
1325 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001326 return 0;
1327}
1328module_init(xhci_hcd_init);
1329
1330static void __exit xhci_hcd_cleanup(void)
1331{
1332#ifdef CONFIG_PCI
1333 xhci_unregister_pci();
1334#endif
1335}
1336module_exit(xhci_hcd_cleanup);