blob: 5e73386b389962f058ba709d8836a870253d54ae [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>
Sarah Sharp8df75f42010-04-02 15:34:16 -070024#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070025#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070026#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070028
29#include "xhci.h"
30
31#define DRIVER_AUTHOR "Sarah Sharp"
32#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
33
Sarah Sharpb0567b32009-08-07 14:04:36 -070034/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
35static int link_quirk;
36module_param(link_quirk, int, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
38
Sarah Sharp66d4ead2009-04-27 19:52:28 -070039/* TODO: copied from ehci-hcd.c - can this be refactored? */
40/*
41 * handshake - spin reading hc until handshake completes or fails
42 * @ptr: address of hc register to be read
43 * @mask: bits to look at in result of read
44 * @done: value of those bits when handshake succeeds
45 * @usec: timeout in microseconds
46 *
47 * Returns negative errno, or zero on success
48 *
49 * Success happens when the "mask" bits have the specified value (hardware
50 * handshake done). There are two failure modes: "usec" have passed (major
51 * hardware flakeout), or the register reads as all-ones (hardware removed).
52 */
53static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
54 u32 mask, u32 done, int usec)
55{
56 u32 result;
57
58 do {
59 result = xhci_readl(xhci, ptr);
60 if (result == ~(u32)0) /* card removed */
61 return -ENODEV;
62 result &= mask;
63 if (result == done)
64 return 0;
65 udelay(1);
66 usec--;
67 } while (usec > 0);
68 return -ETIMEDOUT;
69}
70
71/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070072 * Disable interrupts and begin the xHCI halting process.
73 */
74void xhci_quiesce(struct xhci_hcd *xhci)
75{
76 u32 halted;
77 u32 cmd;
78 u32 mask;
79
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
90/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070091 * Force HC into halt state.
92 *
93 * Disable any IRQs and clear the run/stop bit.
94 * HC will complete any current and actively pipelined transactions, and
95 * should halt within 16 microframes of the run/stop bit being cleared.
96 * Read HC Halted bit in the status register to see when the HC is finished.
97 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
98 */
99int xhci_halt(struct xhci_hcd *xhci)
100{
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700102 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700103
104 return handshake(xhci, &xhci->op_regs->status,
105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
106}
107
108/*
Sarah Sharped074532010-05-24 13:25:21 -0700109 * Set the run bit and wait for the host to be running.
110 */
111int xhci_start(struct xhci_hcd *xhci)
112{
113 u32 temp;
114 int ret;
115
116 temp = xhci_readl(xhci, &xhci->op_regs->command);
117 temp |= (CMD_RUN);
118 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
119 temp);
120 xhci_writel(xhci, temp, &xhci->op_regs->command);
121
122 /*
123 * Wait for the HCHalted Status bit to be 0 to indicate the host is
124 * running.
125 */
126 ret = handshake(xhci, &xhci->op_regs->status,
127 STS_HALT, 0, XHCI_MAX_HALT_USEC);
128 if (ret == -ETIMEDOUT)
129 xhci_err(xhci, "Host took too long to start, "
130 "waited %u microseconds.\n",
131 XHCI_MAX_HALT_USEC);
132 return ret;
133}
134
135/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700136 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
137 *
138 * This resets pipelines, timers, counters, state machines, etc.
139 * Transactions will be terminated immediately, and operational registers
140 * will be set to their defaults.
141 */
142int xhci_reset(struct xhci_hcd *xhci)
143{
144 u32 command;
145 u32 state;
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700146 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700147
148 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700149 if ((state & STS_HALT) == 0) {
150 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
151 return 0;
152 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700153
154 xhci_dbg(xhci, "// Reset the HC\n");
155 command = xhci_readl(xhci, &xhci->op_regs->command);
156 command |= CMD_RESET;
157 xhci_writel(xhci, command, &xhci->op_regs->command);
158 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
159 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
160
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700161 ret = handshake(xhci, &xhci->op_regs->command,
162 CMD_RESET, 0, 250 * 1000);
163 if (ret)
164 return ret;
165
166 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
167 /*
168 * xHCI cannot write to any doorbells or operational registers other
169 * than status until the "Controller Not Ready" flag is cleared.
170 */
171 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700172}
173
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700174
175#if 0
176/* Set up MSI-X table for entry 0 (may claim other entries later) */
177static int xhci_setup_msix(struct xhci_hcd *xhci)
178{
179 int ret;
180 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
181
182 xhci->msix_count = 0;
183 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
184 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
185 if (!xhci->msix_entries) {
186 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
187 return -ENOMEM;
188 }
189 xhci->msix_entries[0].entry = 0;
190
191 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
192 if (ret) {
193 xhci_err(xhci, "Failed to enable MSI-X\n");
194 goto free_entries;
195 }
196
197 /*
198 * Pass the xhci pointer value as the request_irq "cookie".
199 * If more irqs are added, this will need to be unique for each one.
200 */
201 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
202 "xHCI", xhci_to_hcd(xhci));
203 if (ret) {
204 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
205 goto disable_msix;
206 }
207 xhci_dbg(xhci, "Finished setting up MSI-X\n");
208 return 0;
209
210disable_msix:
211 pci_disable_msix(pdev);
212free_entries:
213 kfree(xhci->msix_entries);
214 xhci->msix_entries = NULL;
215 return ret;
216}
217
218/* XXX: code duplication; can xhci_setup_msix call this? */
219/* Free any IRQs and disable MSI-X */
220static void xhci_cleanup_msix(struct xhci_hcd *xhci)
221{
222 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
223 if (!xhci->msix_entries)
224 return;
225
226 free_irq(xhci->msix_entries[0].vector, xhci);
227 pci_disable_msix(pdev);
228 kfree(xhci->msix_entries);
229 xhci->msix_entries = NULL;
230 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
231}
232#endif
233
234/*
235 * Initialize memory for HCD and xHC (one-time init).
236 *
237 * Program the PAGESIZE register, initialize the device context array, create
238 * device contexts (?), set up a command ring segment (or two?), create event
239 * ring (one for now).
240 */
241int xhci_init(struct usb_hcd *hcd)
242{
243 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
244 int retval = 0;
245
246 xhci_dbg(xhci, "xhci_init\n");
247 spin_lock_init(&xhci->lock);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700248 if (link_quirk) {
249 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
250 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
251 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700252 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700253 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700254 retval = xhci_mem_init(xhci, GFP_KERNEL);
255 xhci_dbg(xhci, "Finished xhci_init\n");
256
257 return retval;
258}
259
260/*
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700261 * Called in interrupt context when there might be work
262 * queued on the event ring
263 *
264 * xhci->lock must be held by caller.
265 */
266static void xhci_work(struct xhci_hcd *xhci)
267{
268 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700269 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700270
271 /*
272 * Clear the op reg interrupt status first,
273 * so we can receive interrupts from other MSI-X interrupters.
274 * Write 1 to clear the interrupt status.
275 */
276 temp = xhci_readl(xhci, &xhci->op_regs->status);
277 temp |= STS_EINT;
278 xhci_writel(xhci, temp, &xhci->op_regs->status);
279 /* FIXME when MSI-X is supported and there are multiple vectors */
280 /* Clear the MSI-X event interrupt status */
281
282 /* Acknowledge the interrupt */
283 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
284 temp |= 0x3;
285 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
286 /* Flush posted writes */
287 xhci_readl(xhci, &xhci->ir_set->irq_pending);
288
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700289 if (xhci->xhc_state & XHCI_STATE_DYING)
290 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
291 "Shouldn't IRQs be disabled?\n");
292 else
293 /* FIXME this should be a delayed service routine
294 * that clears the EHB.
295 */
296 xhci_handle_event(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700297
Sarah Sharp2d831092009-07-27 12:03:40 -0700298 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
Sarah Sharp8e595a52009-07-27 12:03:31 -0700299 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp2d831092009-07-27 12:03:40 -0700300 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700301 /* Flush posted writes -- FIXME is this necessary? */
302 xhci_readl(xhci, &xhci->ir_set->irq_pending);
303}
304
305/*-------------------------------------------------------------------------*/
306
307/*
308 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
309 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
310 * indicators of an event TRB error, but we check the status *first* to be safe.
311 */
312irqreturn_t xhci_irq(struct usb_hcd *hcd)
313{
314 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
315 u32 temp, temp2;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700316 union xhci_trb *trb;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700317
318 spin_lock(&xhci->lock);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700319 trb = xhci->event_ring->dequeue;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700320 /* Check if the xHC generated the interrupt, or the irq is shared */
321 temp = xhci_readl(xhci, &xhci->op_regs->status);
322 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Sarah Sharpfcf8f572009-07-27 12:04:01 -0700323 if (temp == 0xffffffff && temp2 == 0xffffffff)
324 goto hw_died;
325
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700326 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
327 spin_unlock(&xhci->lock);
328 return IRQ_NONE;
329 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700330 xhci_dbg(xhci, "op reg status = %08x\n", temp);
331 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
332 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
333 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
334 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
335 lower_32_bits(trb->link.segment_ptr),
336 upper_32_bits(trb->link.segment_ptr),
337 (unsigned int) trb->link.intr_target,
338 (unsigned int) trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700339
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700340 if (temp & STS_FATAL) {
341 xhci_warn(xhci, "WARNING: Host System Error\n");
342 xhci_halt(xhci);
Sarah Sharpfcf8f572009-07-27 12:04:01 -0700343hw_died:
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700344 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
Sarah Sharpc96a2b82009-04-29 19:05:40 -0700345 spin_unlock(&xhci->lock);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700346 return -ESHUTDOWN;
347 }
348
349 xhci_work(xhci);
350 spin_unlock(&xhci->lock);
351
352 return IRQ_HANDLED;
353}
354
355#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700356void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700357{
358 unsigned long flags;
359 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700360 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700361 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
362 int i, j;
363
364 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
365
366 spin_lock_irqsave(&xhci->lock, flags);
367 temp = xhci_readl(xhci, &xhci->op_regs->status);
368 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700369 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700370 xhci_dbg(xhci, "HW died, polling stopped.\n");
371 spin_unlock_irqrestore(&xhci->lock, flags);
372 return;
373 }
374
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700375 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
376 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
377 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
378 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
379 xhci->error_bitmask = 0;
380 xhci_dbg(xhci, "Event ring:\n");
381 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
382 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700383 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
384 temp_64 &= ~ERST_PTR_MASK;
385 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700386 xhci_dbg(xhci, "Command ring:\n");
387 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
388 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
389 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700390 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700391 if (!xhci->devs[i])
392 continue;
393 for (j = 0; j < 31; ++j) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700394 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700395 }
396 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700397
398 if (xhci->noops_submitted != NUM_TEST_NOOPS)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700399 if (xhci_setup_one_noop(xhci))
400 xhci_ring_cmd_db(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700401 spin_unlock_irqrestore(&xhci->lock, flags);
402
403 if (!xhci->zombie)
404 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
405 else
406 xhci_dbg(xhci, "Quit polling the event ring.\n");
407}
408#endif
409
410/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700411 * Start the HC after it was halted.
412 *
413 * This function is called by the USB core when the HC driver is added.
414 * Its opposite is xhci_stop().
415 *
416 * xhci_init() must be called once before this function can be called.
417 * Reset the HC, enable device slot contexts, program DCBAAP, and
418 * set command ring pointer and event ring pointer.
419 *
420 * Setup MSI-X vectors and enable interrupts.
421 */
422int xhci_run(struct usb_hcd *hcd)
423{
424 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700425 u64 temp_64;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700426 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700427 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700428
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700429 hcd->uses_new_polling = 1;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700430
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700431 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700432#if 0 /* FIXME: MSI not setup yet */
433 /* Do this at the very last minute */
434 ret = xhci_setup_msix(xhci);
435 if (!ret)
436 return ret;
437
438 return -ENOSYS;
439#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700440#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
441 init_timer(&xhci->event_ring_timer);
442 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700443 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700444 /* Poll the event ring */
445 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
446 xhci->zombie = 0;
447 xhci_dbg(xhci, "Setting event ring polling timer\n");
448 add_timer(&xhci->event_ring_timer);
449#endif
450
Sarah Sharp66e49d82009-07-27 12:03:46 -0700451 xhci_dbg(xhci, "Command ring memory map follows:\n");
452 xhci_debug_ring(xhci, xhci->cmd_ring);
453 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
454 xhci_dbg_cmd_ptrs(xhci);
455
456 xhci_dbg(xhci, "ERST memory map follows:\n");
457 xhci_dbg_erst(xhci, &xhci->erst);
458 xhci_dbg(xhci, "Event ring:\n");
459 xhci_debug_ring(xhci, xhci->event_ring);
460 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
461 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
462 temp_64 &= ~ERST_PTR_MASK;
463 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
464
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700465 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
466 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700467 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700468 temp |= (u32) 160;
469 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
470
471 /* Set the HCD state before we enable the irqs */
472 hcd->state = HC_STATE_RUNNING;
473 temp = xhci_readl(xhci, &xhci->op_regs->command);
474 temp |= (CMD_EIE);
475 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
476 temp);
477 xhci_writel(xhci, temp, &xhci->op_regs->command);
478
479 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700480 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
481 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700482 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
483 &xhci->ir_set->irq_pending);
484 xhci_print_ir_set(xhci, xhci->ir_set, 0);
485
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700486 if (NUM_TEST_NOOPS > 0)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700487 doorbell = xhci_setup_one_noop(xhci);
Sarah Sharp02386342010-05-24 13:25:28 -0700488 if (xhci->quirks & XHCI_NEC_HOST)
489 xhci_queue_vendor_command(xhci, 0, 0, 0,
490 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700491
Sarah Sharped074532010-05-24 13:25:21 -0700492 if (xhci_start(xhci)) {
493 xhci_halt(xhci);
494 return -ENODEV;
495 }
496
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700497 if (doorbell)
498 (*doorbell)(xhci);
Sarah Sharp02386342010-05-24 13:25:28 -0700499 if (xhci->quirks & XHCI_NEC_HOST)
500 xhci_ring_cmd_db(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700501
502 xhci_dbg(xhci, "Finished xhci_run\n");
503 return 0;
504}
505
506/*
507 * Stop xHCI driver.
508 *
509 * This function is called by the USB core when the HC driver is removed.
510 * Its opposite is xhci_run().
511 *
512 * Disable device contexts, disable IRQs, and quiesce the HC.
513 * Reset the HC, finish any completed transactions, and cleanup memory.
514 */
515void xhci_stop(struct usb_hcd *hcd)
516{
517 u32 temp;
518 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
519
520 spin_lock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700521 xhci_halt(xhci);
522 xhci_reset(xhci);
523 spin_unlock_irq(&xhci->lock);
524
525#if 0 /* No MSI yet */
526 xhci_cleanup_msix(xhci);
527#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700528#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
529 /* Tell the event ring poll function not to reschedule */
530 xhci->zombie = 1;
531 del_timer_sync(&xhci->event_ring_timer);
532#endif
533
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700534 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
535 temp = xhci_readl(xhci, &xhci->op_regs->status);
536 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
537 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
538 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
539 &xhci->ir_set->irq_pending);
540 xhci_print_ir_set(xhci, xhci->ir_set, 0);
541
542 xhci_dbg(xhci, "cleaning up memory\n");
543 xhci_mem_cleanup(xhci);
544 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
545 xhci_readl(xhci, &xhci->op_regs->status));
546}
547
548/*
549 * Shutdown HC (not bus-specific)
550 *
551 * This is called when the machine is rebooting or halting. We assume that the
552 * machine will be powered off, and the HC's internal state will be reset.
553 * Don't bother to free memory.
554 */
555void xhci_shutdown(struct usb_hcd *hcd)
556{
557 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
558
559 spin_lock_irq(&xhci->lock);
560 xhci_halt(xhci);
561 spin_unlock_irq(&xhci->lock);
562
563#if 0
564 xhci_cleanup_msix(xhci);
565#endif
566
567 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
568 xhci_readl(xhci, &xhci->op_regs->status));
569}
570
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700571/*-------------------------------------------------------------------------*/
572
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700573/**
574 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
575 * HCDs. Find the index for an endpoint given its descriptor. Use the return
576 * value to right shift 1 for the bitmask.
577 *
578 * Index = (epnum * 2) + direction - 1,
579 * where direction = 0 for OUT, 1 for IN.
580 * For control endpoints, the IN index is used (OUT index is unused), so
581 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
582 */
583unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
584{
585 unsigned int index;
586 if (usb_endpoint_xfer_control(desc))
587 index = (unsigned int) (usb_endpoint_num(desc)*2);
588 else
589 index = (unsigned int) (usb_endpoint_num(desc)*2) +
590 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
591 return index;
592}
593
Sarah Sharpf94e01862009-04-27 19:58:38 -0700594/* Find the flag for this endpoint (for use in the control context). Use the
595 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
596 * bit 1, etc.
597 */
598unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
599{
600 return 1 << (xhci_get_endpoint_index(desc) + 1);
601}
602
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700603/* Find the flag for this endpoint (for use in the control context). Use the
604 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
605 * bit 1, etc.
606 */
607unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
608{
609 return 1 << (ep_index + 1);
610}
611
Sarah Sharpf94e01862009-04-27 19:58:38 -0700612/* Compute the last valid endpoint context index. Basically, this is the
613 * endpoint index plus one. For slot contexts with more than valid endpoint,
614 * we find the most significant bit set in the added contexts flags.
615 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
616 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
617 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700618unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700619{
620 return fls(added_ctxs) - 1;
621}
622
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700623/* Returns 1 if the arguments are OK;
624 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
625 */
626int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
627 struct usb_host_endpoint *ep, int check_ep, const char *func) {
628 if (!hcd || (check_ep && !ep) || !udev) {
629 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
630 func);
631 return -EINVAL;
632 }
633 if (!udev->parent) {
634 printk(KERN_DEBUG "xHCI %s called for root hub\n",
635 func);
636 return 0;
637 }
638 if (!udev->slot_id) {
639 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
640 func);
641 return -EINVAL;
642 }
643 return 1;
644}
645
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700646static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700647 struct usb_device *udev, struct xhci_command *command,
648 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700649
650/*
651 * Full speed devices may have a max packet size greater than 8 bytes, but the
652 * USB core doesn't know that until it reads the first 8 bytes of the
653 * descriptor. If the usb_device's max packet size changes after that point,
654 * we need to issue an evaluate context command and wait on it.
655 */
656static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
657 unsigned int ep_index, struct urb *urb)
658{
659 struct xhci_container_ctx *in_ctx;
660 struct xhci_container_ctx *out_ctx;
661 struct xhci_input_control_ctx *ctrl_ctx;
662 struct xhci_ep_ctx *ep_ctx;
663 int max_packet_size;
664 int hw_max_packet_size;
665 int ret = 0;
666
667 out_ctx = xhci->devs[slot_id]->out_ctx;
668 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
669 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
670 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
671 if (hw_max_packet_size != max_packet_size) {
672 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
673 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
674 max_packet_size);
675 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
676 hw_max_packet_size);
677 xhci_dbg(xhci, "Issuing evaluate context command.\n");
678
679 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700680 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
681 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700682 in_ctx = xhci->devs[slot_id]->in_ctx;
683 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
684 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
685 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
686
687 /* Set up the input context flags for the command */
688 /* FIXME: This won't work if a non-default control endpoint
689 * changes max packet sizes.
690 */
691 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
692 ctrl_ctx->add_flags = EP0_FLAG;
693 ctrl_ctx->drop_flags = 0;
694
695 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
696 xhci_dbg_ctx(xhci, in_ctx, ep_index);
697 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
698 xhci_dbg_ctx(xhci, out_ctx, ep_index);
699
Sarah Sharp913a8a32009-09-04 10:53:13 -0700700 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
701 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700702
703 /* Clean up the input context for later use by bandwidth
704 * functions.
705 */
706 ctrl_ctx->add_flags = SLOT_FLAG;
707 }
708 return ret;
709}
710
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700711/*
712 * non-error returns are a promise to giveback() the urb later
713 * we drop ownership so next owner (or urb unlink) can get it
714 */
715int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
716{
717 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
718 unsigned long flags;
719 int ret = 0;
720 unsigned int slot_id, ep_index;
721
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700722
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700723 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
724 return -EINVAL;
725
726 slot_id = urb->dev->slot_id;
727 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700728
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700729 if (!xhci->devs || !xhci->devs[slot_id]) {
730 if (!in_interrupt())
731 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
Sarah Sharpc7959fb2009-04-29 19:06:36 -0700732 ret = -EINVAL;
733 goto exit;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700734 }
Alan Stern541c7d42010-06-22 16:39:10 -0400735 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700736 if (!in_interrupt())
737 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
738 ret = -ESHUTDOWN;
739 goto exit;
740 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700741 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
742 /* Check to see if the max packet size for the default control
743 * endpoint changed during FS device enumeration
744 */
745 if (urb->dev->speed == USB_SPEED_FULL) {
746 ret = xhci_check_maxpacket(xhci, slot_id,
747 ep_index, urb);
748 if (ret < 0)
749 return ret;
750 }
751
Sarah Sharpb11069f2009-07-27 12:03:23 -0700752 /* We have a spinlock and interrupts disabled, so we must pass
753 * atomic context to this function, which may allocate memory.
754 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700755 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700756 if (xhci->xhc_state & XHCI_STATE_DYING)
757 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -0700758 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700759 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700760 spin_unlock_irqrestore(&xhci->lock, flags);
761 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
762 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700763 if (xhci->xhc_state & XHCI_STATE_DYING)
764 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -0700765 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
766 EP_GETTING_STREAMS) {
767 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
768 "is transitioning to using streams.\n");
769 ret = -EINVAL;
770 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
771 EP_GETTING_NO_STREAMS) {
772 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
773 "is transitioning to "
774 "not having streams.\n");
775 ret = -EINVAL;
776 } else {
777 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
778 slot_id, ep_index);
779 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700780 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -0700781 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
782 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700783 if (xhci->xhc_state & XHCI_STATE_DYING)
784 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -0700785 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
786 slot_id, ep_index);
787 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700788 } else {
Sarah Sharpb10de142009-04-27 19:58:50 -0700789 ret = -EINVAL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700790 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700791exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700792 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700793dying:
794 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
795 "non-responsive xHCI host.\n",
796 urb->ep->desc.bEndpointAddress, urb);
797 spin_unlock_irqrestore(&xhci->lock, flags);
798 return -ESHUTDOWN;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700799}
800
Sarah Sharpae636742009-04-29 19:02:31 -0700801/*
802 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
803 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
804 * should pick up where it left off in the TD, unless a Set Transfer Ring
805 * Dequeue Pointer is issued.
806 *
807 * The TRBs that make up the buffers for the canceled URB will be "removed" from
808 * the ring. Since the ring is a contiguous structure, they can't be physically
809 * removed. Instead, there are two options:
810 *
811 * 1) If the HC is in the middle of processing the URB to be canceled, we
812 * simply move the ring's dequeue pointer past those TRBs using the Set
813 * Transfer Ring Dequeue Pointer command. This will be the common case,
814 * when drivers timeout on the last submitted URB and attempt to cancel.
815 *
816 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
817 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
818 * HC will need to invalidate the any TRBs it has cached after the stop
819 * endpoint command, as noted in the xHCI 0.95 errata.
820 *
821 * 3) The TD may have completed by the time the Stop Endpoint Command
822 * completes, so software needs to handle that case too.
823 *
824 * This function should protect against the TD enqueueing code ringing the
825 * doorbell while this code is waiting for a Stop Endpoint command to complete.
826 * It also needs to account for multiple cancellations on happening at the same
827 * time for the same endpoint.
828 *
829 * Note that this function can be called in any context, or so says
830 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700831 */
832int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
833{
Sarah Sharpae636742009-04-29 19:02:31 -0700834 unsigned long flags;
835 int ret;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -0700836 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -0700837 struct xhci_hcd *xhci;
838 struct xhci_td *td;
839 unsigned int ep_index;
840 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700841 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700842
843 xhci = hcd_to_xhci(hcd);
844 spin_lock_irqsave(&xhci->lock, flags);
845 /* Make sure the URB hasn't completed or been unlinked already */
846 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
847 if (ret || !urb->hcpriv)
848 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -0700849 temp = xhci_readl(xhci, &xhci->op_regs->status);
850 if (temp == 0xffffffff) {
851 xhci_dbg(xhci, "HW died, freeing TD.\n");
852 td = (struct xhci_td *) urb->hcpriv;
853
854 usb_hcd_unlink_urb_from_ep(hcd, urb);
855 spin_unlock_irqrestore(&xhci->lock, flags);
856 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
857 kfree(td);
858 return ret;
859 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700860 if (xhci->xhc_state & XHCI_STATE_DYING) {
861 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
862 "non-responsive xHCI host.\n",
863 urb->ep->desc.bEndpointAddress, urb);
864 /* Let the stop endpoint command watchdog timer (which set this
865 * state) finish cleaning up the endpoint TD lists. We must
866 * have caught it in the middle of dropping a lock and giving
867 * back an URB.
868 */
869 goto done;
870 }
Sarah Sharpae636742009-04-29 19:02:31 -0700871
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700872 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700873 xhci_dbg(xhci, "Event ring:\n");
874 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700875 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700876 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700877 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
878 if (!ep_ring) {
879 ret = -EINVAL;
880 goto done;
881 }
882
Sarah Sharp66e49d82009-07-27 12:03:46 -0700883 xhci_dbg(xhci, "Endpoint ring:\n");
884 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700885 td = (struct xhci_td *) urb->hcpriv;
886
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700887 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700888 /* Queue a stop endpoint command, but only if this is
889 * the first cancellation to be handled.
890 */
Sarah Sharp678539c2009-10-27 10:55:52 -0700891 if (!(ep->ep_state & EP_HALT_PENDING)) {
892 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700893 ep->stop_cmds_pending++;
894 ep->stop_cmd_timer.expires = jiffies +
895 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
896 add_timer(&ep->stop_cmd_timer);
Sarah Sharp23e3be12009-04-29 19:05:20 -0700897 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
898 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700899 }
900done:
901 spin_unlock_irqrestore(&xhci->lock, flags);
902 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700903}
904
Sarah Sharpf94e01862009-04-27 19:58:38 -0700905/* Drop an endpoint from a new bandwidth configuration for this device.
906 * Only one call to this function is allowed per endpoint before
907 * check_bandwidth() or reset_bandwidth() must be called.
908 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
909 * add the endpoint to the schedule with possibly new parameters denoted by a
910 * different endpoint descriptor in usb_host_endpoint.
911 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
912 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700913 *
914 * The USB core will not allow URBs to be queued to an endpoint that is being
915 * disabled, so there's no need for mutual exclusion to protect
916 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700917 */
918int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
919 struct usb_host_endpoint *ep)
920{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700921 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -0700922 struct xhci_container_ctx *in_ctx, *out_ctx;
923 struct xhci_input_control_ctx *ctrl_ctx;
924 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700925 unsigned int last_ctx;
926 unsigned int ep_index;
927 struct xhci_ep_ctx *ep_ctx;
928 u32 drop_flag;
929 u32 new_add_flags, new_drop_flags, new_slot_info;
930 int ret;
931
932 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700933 if (ret <= 0)
934 return ret;
935 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700936 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700937
938 drop_flag = xhci_get_endpoint_flag(&ep->desc);
939 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
940 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
941 __func__, drop_flag);
942 return 0;
943 }
944
Sarah Sharpf94e01862009-04-27 19:58:38 -0700945 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
946 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
947 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700948 return -EINVAL;
949 }
950
951 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -0700952 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
953 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700954 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -0700955 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700956 /* If the HC already knows the endpoint is disabled,
957 * or the HCD has noted it is disabled, ignore this request
958 */
959 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
John Yound115b042009-07-27 12:05:15 -0700960 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700961 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
962 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700963 return 0;
964 }
965
John Yound115b042009-07-27 12:05:15 -0700966 ctrl_ctx->drop_flags |= drop_flag;
967 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700968
Sarah Sharp0a023c62009-09-18 08:55:12 -0700969 ctrl_ctx->add_flags &= ~drop_flag;
John Yound115b042009-07-27 12:05:15 -0700970 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700971
John Yound115b042009-07-27 12:05:15 -0700972 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
973 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700974 /* Update the last valid endpoint context, if we deleted the last one */
John Yound115b042009-07-27 12:05:15 -0700975 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
976 slot_ctx->dev_info &= ~LAST_CTX_MASK;
977 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700978 }
John Yound115b042009-07-27 12:05:15 -0700979 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700980
981 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
982
Sarah Sharpf94e01862009-04-27 19:58:38 -0700983 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
984 (unsigned int) ep->desc.bEndpointAddress,
985 udev->slot_id,
986 (unsigned int) new_drop_flags,
987 (unsigned int) new_add_flags,
988 (unsigned int) new_slot_info);
989 return 0;
990}
991
992/* Add an endpoint to a new possible bandwidth configuration for this device.
993 * Only one call to this function is allowed per endpoint before
994 * check_bandwidth() or reset_bandwidth() must be called.
995 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
996 * add the endpoint to the schedule with possibly new parameters denoted by a
997 * different endpoint descriptor in usb_host_endpoint.
998 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
999 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001000 *
1001 * The USB core will not allow URBs to be queued to an endpoint until the
1002 * configuration or alt setting is installed in the device, so there's no need
1003 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001004 */
1005int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1006 struct usb_host_endpoint *ep)
1007{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001008 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001009 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001010 unsigned int ep_index;
1011 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001012 struct xhci_slot_ctx *slot_ctx;
1013 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001014 u32 added_ctxs;
1015 unsigned int last_ctx;
1016 u32 new_add_flags, new_drop_flags, new_slot_info;
1017 int ret = 0;
1018
1019 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001020 if (ret <= 0) {
1021 /* So we won't queue a reset ep command for a root hub */
1022 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001023 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001024 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001025 xhci = hcd_to_xhci(hcd);
1026
1027 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1028 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1029 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1030 /* FIXME when we have to issue an evaluate endpoint command to
1031 * deal with ep0 max packet size changing once we get the
1032 * descriptors
1033 */
1034 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1035 __func__, added_ctxs);
1036 return 0;
1037 }
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 -EINVAL;
1043 }
1044
1045 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001046 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1047 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001048 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001049 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001050 /* If the HCD has already noted the endpoint is enabled,
1051 * ignore this request.
1052 */
John Yound115b042009-07-27 12:05:15 -07001053 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001054 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1055 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001056 return 0;
1057 }
1058
Sarah Sharpf88ba782009-05-14 11:44:22 -07001059 /*
1060 * Configuration and alternate setting changes must be done in
1061 * process context, not interrupt context (or so documenation
1062 * for usb_set_interface() and usb_set_configuration() claim).
1063 */
1064 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
Oliver Neukum319c3ea2009-12-16 19:43:59 +01001065 udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001066 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1067 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001068 return -ENOMEM;
1069 }
1070
John Yound115b042009-07-27 12:05:15 -07001071 ctrl_ctx->add_flags |= added_ctxs;
1072 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001073
1074 /* If xhci_endpoint_disable() was called for this endpoint, but the
1075 * xHC hasn't been notified yet through the check_bandwidth() call,
1076 * this re-adds a new state for the endpoint from the new endpoint
1077 * descriptors. We must drop and re-add this endpoint, so we leave the
1078 * drop flags alone.
1079 */
John Yound115b042009-07-27 12:05:15 -07001080 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001081
John Yound115b042009-07-27 12:05:15 -07001082 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001083 /* Update the last valid endpoint context, if we just added one past */
John Yound115b042009-07-27 12:05:15 -07001084 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1085 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1086 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001087 }
John Yound115b042009-07-27 12:05:15 -07001088 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001089
Sarah Sharpa1587d92009-07-27 12:03:15 -07001090 /* Store the usb_device pointer for later use */
1091 ep->hcpriv = udev;
1092
Sarah Sharpf94e01862009-04-27 19:58:38 -07001093 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1094 (unsigned int) ep->desc.bEndpointAddress,
1095 udev->slot_id,
1096 (unsigned int) new_drop_flags,
1097 (unsigned int) new_add_flags,
1098 (unsigned int) new_slot_info);
1099 return 0;
1100}
1101
John Yound115b042009-07-27 12:05:15 -07001102static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001103{
John Yound115b042009-07-27 12:05:15 -07001104 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001105 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001106 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001107 int i;
1108
1109 /* When a device's add flag and drop flag are zero, any subsequent
1110 * configure endpoint command will leave that endpoint's state
1111 * untouched. Make sure we don't leave any old state in the input
1112 * endpoint contexts.
1113 */
John Yound115b042009-07-27 12:05:15 -07001114 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1115 ctrl_ctx->drop_flags = 0;
1116 ctrl_ctx->add_flags = 0;
1117 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1118 slot_ctx->dev_info &= ~LAST_CTX_MASK;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001119 /* Endpoint 0 is always valid */
John Yound115b042009-07-27 12:05:15 -07001120 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001121 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001122 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001123 ep_ctx->ep_info = 0;
1124 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001125 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001126 ep_ctx->tx_info = 0;
1127 }
1128}
1129
Sarah Sharpf2217e82009-08-07 14:04:43 -07001130static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001131 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001132{
1133 int ret;
1134
Sarah Sharp913a8a32009-09-04 10:53:13 -07001135 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001136 case COMP_ENOMEM:
1137 dev_warn(&udev->dev, "Not enough host controller resources "
1138 "for new device state.\n");
1139 ret = -ENOMEM;
1140 /* FIXME: can we allocate more resources for the HC? */
1141 break;
1142 case COMP_BW_ERR:
1143 dev_warn(&udev->dev, "Not enough bandwidth "
1144 "for new device state.\n");
1145 ret = -ENOSPC;
1146 /* FIXME: can we go back to the old state? */
1147 break;
1148 case COMP_TRB_ERR:
1149 /* the HCD set up something wrong */
1150 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1151 "add flag = 1, "
1152 "and endpoint is not disabled.\n");
1153 ret = -EINVAL;
1154 break;
1155 case COMP_SUCCESS:
1156 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1157 ret = 0;
1158 break;
1159 default:
1160 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001161 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001162 ret = -EINVAL;
1163 break;
1164 }
1165 return ret;
1166}
1167
1168static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001169 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001170{
1171 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001172 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001173
Sarah Sharp913a8a32009-09-04 10:53:13 -07001174 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001175 case COMP_EINVAL:
1176 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1177 "context command.\n");
1178 ret = -EINVAL;
1179 break;
1180 case COMP_EBADSLT:
1181 dev_warn(&udev->dev, "WARN: slot not enabled for"
1182 "evaluate context command.\n");
1183 case COMP_CTX_STATE:
1184 dev_warn(&udev->dev, "WARN: invalid context state for "
1185 "evaluate context command.\n");
1186 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1187 ret = -EINVAL;
1188 break;
1189 case COMP_SUCCESS:
1190 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1191 ret = 0;
1192 break;
1193 default:
1194 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001195 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001196 ret = -EINVAL;
1197 break;
1198 }
1199 return ret;
1200}
1201
1202/* Issue a configure endpoint command or evaluate context command
1203 * and wait for it to finish.
1204 */
1205static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001206 struct usb_device *udev,
1207 struct xhci_command *command,
1208 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001209{
1210 int ret;
1211 int timeleft;
1212 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001213 struct xhci_container_ctx *in_ctx;
1214 struct completion *cmd_completion;
1215 int *cmd_status;
1216 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001217
1218 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001219 virt_dev = xhci->devs[udev->slot_id];
1220 if (command) {
1221 in_ctx = command->in_ctx;
1222 cmd_completion = command->completion;
1223 cmd_status = &command->status;
1224 command->command_trb = xhci->cmd_ring->enqueue;
1225 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1226 } else {
1227 in_ctx = virt_dev->in_ctx;
1228 cmd_completion = &virt_dev->cmd_completion;
1229 cmd_status = &virt_dev->cmd_status;
1230 }
Andiry Xu1d680642010-03-12 17:10:04 +08001231 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001232
Sarah Sharpf2217e82009-08-07 14:04:43 -07001233 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001234 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1235 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001236 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07001237 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001238 udev->slot_id);
1239 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08001240 if (command)
1241 list_del(&command->cmd_list);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001242 spin_unlock_irqrestore(&xhci->lock, flags);
1243 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1244 return -ENOMEM;
1245 }
1246 xhci_ring_cmd_db(xhci);
1247 spin_unlock_irqrestore(&xhci->lock, flags);
1248
1249 /* Wait for the configure endpoint command to complete */
1250 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07001251 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001252 USB_CTRL_SET_TIMEOUT);
1253 if (timeleft <= 0) {
1254 xhci_warn(xhci, "%s while waiting for %s command\n",
1255 timeleft == 0 ? "Timeout" : "Signal",
1256 ctx_change == 0 ?
1257 "configure endpoint" :
1258 "evaluate context");
1259 /* FIXME cancel the configure endpoint command */
1260 return -ETIME;
1261 }
1262
1263 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001264 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1265 return xhci_evaluate_context_result(xhci, udev, cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001266}
1267
Sarah Sharpf88ba782009-05-14 11:44:22 -07001268/* Called after one or more calls to xhci_add_endpoint() or
1269 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1270 * to call xhci_reset_bandwidth().
1271 *
1272 * Since we are in the middle of changing either configuration or
1273 * installing a new alt setting, the USB core won't allow URBs to be
1274 * enqueued for any endpoint on the old config or interface. Nothing
1275 * else should be touching the xhci->devs[slot_id] structure, so we
1276 * don't need to take the xhci->lock for manipulating that.
1277 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001278int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1279{
1280 int i;
1281 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001282 struct xhci_hcd *xhci;
1283 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07001284 struct xhci_input_control_ctx *ctrl_ctx;
1285 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001286
1287 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1288 if (ret <= 0)
1289 return ret;
1290 xhci = hcd_to_xhci(hcd);
1291
Sarah Sharpf94e01862009-04-27 19:58:38 -07001292 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1293 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1294 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001295 return -EINVAL;
1296 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001297 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001298 virt_dev = xhci->devs[udev->slot_id];
1299
1300 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07001301 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1302 ctrl_ctx->add_flags |= SLOT_FLAG;
1303 ctrl_ctx->add_flags &= ~EP0_FLAG;
1304 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1305 ctrl_ctx->drop_flags &= ~EP0_FLAG;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001306 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07001307 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1308 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1309 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001310
Sarah Sharp913a8a32009-09-04 10:53:13 -07001311 ret = xhci_configure_endpoint(xhci, udev, NULL,
1312 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001313 if (ret) {
1314 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001315 return ret;
1316 }
1317
1318 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07001319 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1320 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001321
John Yound115b042009-07-27 12:05:15 -07001322 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001323 /* Install new rings and free or cache any old rings */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001324 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001325 if (!virt_dev->eps[i].new_ring)
1326 continue;
1327 /* Only cache or free the old ring if it exists.
1328 * It may not if this is the first add of an endpoint.
1329 */
1330 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08001331 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001332 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001333 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1334 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001335 }
1336
Sarah Sharpf94e01862009-04-27 19:58:38 -07001337 return ret;
1338}
1339
1340void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1341{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001342 struct xhci_hcd *xhci;
1343 struct xhci_virt_device *virt_dev;
1344 int i, ret;
1345
1346 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1347 if (ret <= 0)
1348 return;
1349 xhci = hcd_to_xhci(hcd);
1350
Sarah Sharpf94e01862009-04-27 19:58:38 -07001351 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1352 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1353 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001354 return;
1355 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001356 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001357 virt_dev = xhci->devs[udev->slot_id];
1358 /* Free any rings allocated for added endpoints */
1359 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001360 if (virt_dev->eps[i].new_ring) {
1361 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1362 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001363 }
1364 }
John Yound115b042009-07-27 12:05:15 -07001365 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001366}
1367
Sarah Sharp5270b952009-09-04 10:53:11 -07001368static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001369 struct xhci_container_ctx *in_ctx,
1370 struct xhci_container_ctx *out_ctx,
1371 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07001372{
1373 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001374 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001375 ctrl_ctx->add_flags = add_flags;
1376 ctrl_ctx->drop_flags = drop_flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001377 xhci_slot_copy(xhci, in_ctx, out_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001378 ctrl_ctx->add_flags |= SLOT_FLAG;
1379
Sarah Sharp913a8a32009-09-04 10:53:13 -07001380 xhci_dbg(xhci, "Input Context:\n");
1381 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07001382}
1383
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001384void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1385 unsigned int slot_id, unsigned int ep_index,
1386 struct xhci_dequeue_state *deq_state)
1387{
1388 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001389 struct xhci_ep_ctx *ep_ctx;
1390 u32 added_ctxs;
1391 dma_addr_t addr;
1392
Sarah Sharp913a8a32009-09-04 10:53:13 -07001393 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1394 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001395 in_ctx = xhci->devs[slot_id]->in_ctx;
1396 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1397 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1398 deq_state->new_deq_ptr);
1399 if (addr == 0) {
1400 xhci_warn(xhci, "WARN Cannot submit config ep after "
1401 "reset ep command\n");
1402 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1403 deq_state->new_deq_seg,
1404 deq_state->new_deq_ptr);
1405 return;
1406 }
1407 ep_ctx->deq = addr | deq_state->new_cycle_state;
1408
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001409 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001410 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1411 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001412}
1413
Sarah Sharp82d10092009-08-07 14:04:52 -07001414void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001415 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07001416{
1417 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001418 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07001419
1420 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001421 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07001422 /* We need to move the HW's dequeue pointer past this TD,
1423 * or it will attempt to resend it on the next doorbell ring.
1424 */
1425 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001426 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001427 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07001428
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001429 /* HW with the reset endpoint quirk will use the saved dequeue state to
1430 * issue a configure endpoint command later.
1431 */
1432 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1433 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001434 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001435 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001436 } else {
1437 /* Better hope no one uses the input context between now and the
1438 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001439 * XXX: No idea how this hardware will react when stream rings
1440 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001441 */
1442 xhci_dbg(xhci, "Setting up input context for "
1443 "configure endpoint command\n");
1444 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1445 ep_index, &deq_state);
1446 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001447}
1448
Sarah Sharpa1587d92009-07-27 12:03:15 -07001449/* Deal with stalled endpoints. The core should have sent the control message
1450 * to clear the halt condition. However, we need to make the xHCI hardware
1451 * reset its sequence number, since a device will expect a sequence number of
1452 * zero after the halt condition is cleared.
1453 * Context: in_interrupt
1454 */
1455void xhci_endpoint_reset(struct usb_hcd *hcd,
1456 struct usb_host_endpoint *ep)
1457{
1458 struct xhci_hcd *xhci;
1459 struct usb_device *udev;
1460 unsigned int ep_index;
1461 unsigned long flags;
1462 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001463 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001464
1465 xhci = hcd_to_xhci(hcd);
1466 udev = (struct usb_device *) ep->hcpriv;
1467 /* Called with a root hub endpoint (or an endpoint that wasn't added
1468 * with xhci_add_endpoint()
1469 */
1470 if (!ep->hcpriv)
1471 return;
1472 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001473 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1474 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001475 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1476 ep->desc.bEndpointAddress);
1477 return;
1478 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001479 if (usb_endpoint_xfer_control(&ep->desc)) {
1480 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1481 return;
1482 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001483
1484 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1485 spin_lock_irqsave(&xhci->lock, flags);
1486 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001487 /*
1488 * Can't change the ring dequeue pointer until it's transitioned to the
1489 * stopped state, which is only upon a successful reset endpoint
1490 * command. Better hope that last command worked!
1491 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07001492 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001493 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1494 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001495 xhci_ring_cmd_db(xhci);
1496 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07001497 virt_ep->stopped_td = NULL;
1498 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001499 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001500 spin_unlock_irqrestore(&xhci->lock, flags);
1501
1502 if (ret)
1503 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1504}
1505
Sarah Sharp8df75f42010-04-02 15:34:16 -07001506static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1507 struct usb_device *udev, struct usb_host_endpoint *ep,
1508 unsigned int slot_id)
1509{
1510 int ret;
1511 unsigned int ep_index;
1512 unsigned int ep_state;
1513
1514 if (!ep)
1515 return -EINVAL;
1516 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__);
1517 if (ret <= 0)
1518 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04001519 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07001520 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1521 " descriptor for ep 0x%x does not support streams\n",
1522 ep->desc.bEndpointAddress);
1523 return -EINVAL;
1524 }
1525
1526 ep_index = xhci_get_endpoint_index(&ep->desc);
1527 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1528 if (ep_state & EP_HAS_STREAMS ||
1529 ep_state & EP_GETTING_STREAMS) {
1530 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1531 "already has streams set up.\n",
1532 ep->desc.bEndpointAddress);
1533 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1534 "dynamic stream context array reallocation.\n");
1535 return -EINVAL;
1536 }
1537 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1538 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1539 "endpoint 0x%x; URBs are pending.\n",
1540 ep->desc.bEndpointAddress);
1541 return -EINVAL;
1542 }
1543 return 0;
1544}
1545
1546static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1547 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1548{
1549 unsigned int max_streams;
1550
1551 /* The stream context array size must be a power of two */
1552 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1553 /*
1554 * Find out how many primary stream array entries the host controller
1555 * supports. Later we may use secondary stream arrays (similar to 2nd
1556 * level page entries), but that's an optional feature for xHCI host
1557 * controllers. xHCs must support at least 4 stream IDs.
1558 */
1559 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1560 if (*num_stream_ctxs > max_streams) {
1561 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1562 max_streams);
1563 *num_stream_ctxs = max_streams;
1564 *num_streams = max_streams;
1565 }
1566}
1567
1568/* Returns an error code if one of the endpoint already has streams.
1569 * This does not change any data structures, it only checks and gathers
1570 * information.
1571 */
1572static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1573 struct usb_device *udev,
1574 struct usb_host_endpoint **eps, unsigned int num_eps,
1575 unsigned int *num_streams, u32 *changed_ep_bitmask)
1576{
Sarah Sharp8df75f42010-04-02 15:34:16 -07001577 unsigned int max_streams;
1578 unsigned int endpoint_flag;
1579 int i;
1580 int ret;
1581
1582 for (i = 0; i < num_eps; i++) {
1583 ret = xhci_check_streams_endpoint(xhci, udev,
1584 eps[i], udev->slot_id);
1585 if (ret < 0)
1586 return ret;
1587
Alan Stern842f1692010-04-30 12:44:46 -04001588 max_streams = USB_SS_MAX_STREAMS(
1589 eps[i]->ss_ep_comp.bmAttributes);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001590 if (max_streams < (*num_streams - 1)) {
1591 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1592 eps[i]->desc.bEndpointAddress,
1593 max_streams);
1594 *num_streams = max_streams+1;
1595 }
1596
1597 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1598 if (*changed_ep_bitmask & endpoint_flag)
1599 return -EINVAL;
1600 *changed_ep_bitmask |= endpoint_flag;
1601 }
1602 return 0;
1603}
1604
1605static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1606 struct usb_device *udev,
1607 struct usb_host_endpoint **eps, unsigned int num_eps)
1608{
1609 u32 changed_ep_bitmask = 0;
1610 unsigned int slot_id;
1611 unsigned int ep_index;
1612 unsigned int ep_state;
1613 int i;
1614
1615 slot_id = udev->slot_id;
1616 if (!xhci->devs[slot_id])
1617 return 0;
1618
1619 for (i = 0; i < num_eps; i++) {
1620 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1621 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1622 /* Are streams already being freed for the endpoint? */
1623 if (ep_state & EP_GETTING_NO_STREAMS) {
1624 xhci_warn(xhci, "WARN Can't disable streams for "
1625 "endpoint 0x%x\n, "
1626 "streams are being disabled already.",
1627 eps[i]->desc.bEndpointAddress);
1628 return 0;
1629 }
1630 /* Are there actually any streams to free? */
1631 if (!(ep_state & EP_HAS_STREAMS) &&
1632 !(ep_state & EP_GETTING_STREAMS)) {
1633 xhci_warn(xhci, "WARN Can't disable streams for "
1634 "endpoint 0x%x\n, "
1635 "streams are already disabled!",
1636 eps[i]->desc.bEndpointAddress);
1637 xhci_warn(xhci, "WARN xhci_free_streams() called "
1638 "with non-streams endpoint\n");
1639 return 0;
1640 }
1641 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1642 }
1643 return changed_ep_bitmask;
1644}
1645
1646/*
1647 * The USB device drivers use this function (though the HCD interface in USB
1648 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1649 * coordinate mass storage command queueing across multiple endpoints (basically
1650 * a stream ID == a task ID).
1651 *
1652 * Setting up streams involves allocating the same size stream context array
1653 * for each endpoint and issuing a configure endpoint command for all endpoints.
1654 *
1655 * Don't allow the call to succeed if one endpoint only supports one stream
1656 * (which means it doesn't support streams at all).
1657 *
1658 * Drivers may get less stream IDs than they asked for, if the host controller
1659 * hardware or endpoints claim they can't support the number of requested
1660 * stream IDs.
1661 */
1662int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1663 struct usb_host_endpoint **eps, unsigned int num_eps,
1664 unsigned int num_streams, gfp_t mem_flags)
1665{
1666 int i, ret;
1667 struct xhci_hcd *xhci;
1668 struct xhci_virt_device *vdev;
1669 struct xhci_command *config_cmd;
1670 unsigned int ep_index;
1671 unsigned int num_stream_ctxs;
1672 unsigned long flags;
1673 u32 changed_ep_bitmask = 0;
1674
1675 if (!eps)
1676 return -EINVAL;
1677
1678 /* Add one to the number of streams requested to account for
1679 * stream 0 that is reserved for xHCI usage.
1680 */
1681 num_streams += 1;
1682 xhci = hcd_to_xhci(hcd);
1683 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
1684 num_streams);
1685
1686 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
1687 if (!config_cmd) {
1688 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1689 return -ENOMEM;
1690 }
1691
1692 /* Check to make sure all endpoints are not already configured for
1693 * streams. While we're at it, find the maximum number of streams that
1694 * all the endpoints will support and check for duplicate endpoints.
1695 */
1696 spin_lock_irqsave(&xhci->lock, flags);
1697 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
1698 num_eps, &num_streams, &changed_ep_bitmask);
1699 if (ret < 0) {
1700 xhci_free_command(xhci, config_cmd);
1701 spin_unlock_irqrestore(&xhci->lock, flags);
1702 return ret;
1703 }
1704 if (num_streams <= 1) {
1705 xhci_warn(xhci, "WARN: endpoints can't handle "
1706 "more than one stream.\n");
1707 xhci_free_command(xhci, config_cmd);
1708 spin_unlock_irqrestore(&xhci->lock, flags);
1709 return -EINVAL;
1710 }
1711 vdev = xhci->devs[udev->slot_id];
1712 /* Mark each endpoint as being in transistion, so
1713 * xhci_urb_enqueue() will reject all URBs.
1714 */
1715 for (i = 0; i < num_eps; i++) {
1716 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1717 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
1718 }
1719 spin_unlock_irqrestore(&xhci->lock, flags);
1720
1721 /* Setup internal data structures and allocate HW data structures for
1722 * streams (but don't install the HW structures in the input context
1723 * until we're sure all memory allocation succeeded).
1724 */
1725 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
1726 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
1727 num_stream_ctxs, num_streams);
1728
1729 for (i = 0; i < num_eps; i++) {
1730 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1731 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
1732 num_stream_ctxs,
1733 num_streams, mem_flags);
1734 if (!vdev->eps[ep_index].stream_info)
1735 goto cleanup;
1736 /* Set maxPstreams in endpoint context and update deq ptr to
1737 * point to stream context array. FIXME
1738 */
1739 }
1740
1741 /* Set up the input context for a configure endpoint command. */
1742 for (i = 0; i < num_eps; i++) {
1743 struct xhci_ep_ctx *ep_ctx;
1744
1745 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1746 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
1747
1748 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
1749 vdev->out_ctx, ep_index);
1750 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
1751 vdev->eps[ep_index].stream_info);
1752 }
1753 /* Tell the HW to drop its old copy of the endpoint context info
1754 * and add the updated copy from the input context.
1755 */
1756 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
1757 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1758
1759 /* Issue and wait for the configure endpoint command */
1760 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
1761 false, false);
1762
1763 /* xHC rejected the configure endpoint command for some reason, so we
1764 * leave the old ring intact and free our internal streams data
1765 * structure.
1766 */
1767 if (ret < 0)
1768 goto cleanup;
1769
1770 spin_lock_irqsave(&xhci->lock, flags);
1771 for (i = 0; i < num_eps; i++) {
1772 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1773 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1774 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
1775 udev->slot_id, ep_index);
1776 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
1777 }
1778 xhci_free_command(xhci, config_cmd);
1779 spin_unlock_irqrestore(&xhci->lock, flags);
1780
1781 /* Subtract 1 for stream 0, which drivers can't use */
1782 return num_streams - 1;
1783
1784cleanup:
1785 /* If it didn't work, free the streams! */
1786 for (i = 0; i < num_eps; i++) {
1787 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1788 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07001789 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001790 /* FIXME Unset maxPstreams in endpoint context and
1791 * update deq ptr to point to normal string ring.
1792 */
1793 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1794 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1795 xhci_endpoint_zero(xhci, vdev, eps[i]);
1796 }
1797 xhci_free_command(xhci, config_cmd);
1798 return -ENOMEM;
1799}
1800
1801/* Transition the endpoint from using streams to being a "normal" endpoint
1802 * without streams.
1803 *
1804 * Modify the endpoint context state, submit a configure endpoint command,
1805 * and free all endpoint rings for streams if that completes successfully.
1806 */
1807int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1808 struct usb_host_endpoint **eps, unsigned int num_eps,
1809 gfp_t mem_flags)
1810{
1811 int i, ret;
1812 struct xhci_hcd *xhci;
1813 struct xhci_virt_device *vdev;
1814 struct xhci_command *command;
1815 unsigned int ep_index;
1816 unsigned long flags;
1817 u32 changed_ep_bitmask;
1818
1819 xhci = hcd_to_xhci(hcd);
1820 vdev = xhci->devs[udev->slot_id];
1821
1822 /* Set up a configure endpoint command to remove the streams rings */
1823 spin_lock_irqsave(&xhci->lock, flags);
1824 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
1825 udev, eps, num_eps);
1826 if (changed_ep_bitmask == 0) {
1827 spin_unlock_irqrestore(&xhci->lock, flags);
1828 return -EINVAL;
1829 }
1830
1831 /* Use the xhci_command structure from the first endpoint. We may have
1832 * allocated too many, but the driver may call xhci_free_streams() for
1833 * each endpoint it grouped into one call to xhci_alloc_streams().
1834 */
1835 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
1836 command = vdev->eps[ep_index].stream_info->free_streams_command;
1837 for (i = 0; i < num_eps; i++) {
1838 struct xhci_ep_ctx *ep_ctx;
1839
1840 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1841 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
1842 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
1843 EP_GETTING_NO_STREAMS;
1844
1845 xhci_endpoint_copy(xhci, command->in_ctx,
1846 vdev->out_ctx, ep_index);
1847 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
1848 &vdev->eps[ep_index]);
1849 }
1850 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
1851 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1852 spin_unlock_irqrestore(&xhci->lock, flags);
1853
1854 /* Issue and wait for the configure endpoint command,
1855 * which must succeed.
1856 */
1857 ret = xhci_configure_endpoint(xhci, udev, command,
1858 false, true);
1859
1860 /* xHC rejected the configure endpoint command for some reason, so we
1861 * leave the streams rings intact.
1862 */
1863 if (ret < 0)
1864 return ret;
1865
1866 spin_lock_irqsave(&xhci->lock, flags);
1867 for (i = 0; i < num_eps; i++) {
1868 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1869 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07001870 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001871 /* FIXME Unset maxPstreams in endpoint context and
1872 * update deq ptr to point to normal string ring.
1873 */
1874 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
1875 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1876 }
1877 spin_unlock_irqrestore(&xhci->lock, flags);
1878
1879 return 0;
1880}
1881
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001882/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001883 * This submits a Reset Device Command, which will set the device state to 0,
1884 * set the device address to 0, and disable all the endpoints except the default
1885 * control endpoint. The USB core should come back and call
1886 * xhci_address_device(), and then re-set up the configuration. If this is
1887 * called because of a usb_reset_and_verify_device(), then the old alternate
1888 * settings will be re-installed through the normal bandwidth allocation
1889 * functions.
1890 *
1891 * Wait for the Reset Device command to finish. Remove all structures
1892 * associated with the endpoints that were disabled. Clear the input device
1893 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
1894 */
1895int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
1896{
1897 int ret, i;
1898 unsigned long flags;
1899 struct xhci_hcd *xhci;
1900 unsigned int slot_id;
1901 struct xhci_virt_device *virt_dev;
1902 struct xhci_command *reset_device_cmd;
1903 int timeleft;
1904 int last_freed_endpoint;
1905
1906 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1907 if (ret <= 0)
1908 return ret;
1909 xhci = hcd_to_xhci(hcd);
1910 slot_id = udev->slot_id;
1911 virt_dev = xhci->devs[slot_id];
1912 if (!virt_dev) {
1913 xhci_dbg(xhci, "%s called with invalid slot ID %u\n",
1914 __func__, slot_id);
1915 return -EINVAL;
1916 }
1917
1918 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
1919 /* Allocate the command structure that holds the struct completion.
1920 * Assume we're in process context, since the normal device reset
1921 * process has to wait for the device anyway. Storage devices are
1922 * reset as part of error handling, so use GFP_NOIO instead of
1923 * GFP_KERNEL.
1924 */
1925 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
1926 if (!reset_device_cmd) {
1927 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
1928 return -ENOMEM;
1929 }
1930
1931 /* Attempt to submit the Reset Device command to the command ring */
1932 spin_lock_irqsave(&xhci->lock, flags);
1933 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
1934 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
1935 ret = xhci_queue_reset_device(xhci, slot_id);
1936 if (ret) {
1937 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1938 list_del(&reset_device_cmd->cmd_list);
1939 spin_unlock_irqrestore(&xhci->lock, flags);
1940 goto command_cleanup;
1941 }
1942 xhci_ring_cmd_db(xhci);
1943 spin_unlock_irqrestore(&xhci->lock, flags);
1944
1945 /* Wait for the Reset Device command to finish */
1946 timeleft = wait_for_completion_interruptible_timeout(
1947 reset_device_cmd->completion,
1948 USB_CTRL_SET_TIMEOUT);
1949 if (timeleft <= 0) {
1950 xhci_warn(xhci, "%s while waiting for reset device command\n",
1951 timeleft == 0 ? "Timeout" : "Signal");
1952 spin_lock_irqsave(&xhci->lock, flags);
1953 /* The timeout might have raced with the event ring handler, so
1954 * only delete from the list if the item isn't poisoned.
1955 */
1956 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
1957 list_del(&reset_device_cmd->cmd_list);
1958 spin_unlock_irqrestore(&xhci->lock, flags);
1959 ret = -ETIME;
1960 goto command_cleanup;
1961 }
1962
1963 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
1964 * unless we tried to reset a slot ID that wasn't enabled,
1965 * or the device wasn't in the addressed or configured state.
1966 */
1967 ret = reset_device_cmd->status;
1968 switch (ret) {
1969 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
1970 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
1971 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
1972 slot_id,
1973 xhci_get_slot_state(xhci, virt_dev->out_ctx));
1974 xhci_info(xhci, "Not freeing device rings.\n");
1975 /* Don't treat this as an error. May change my mind later. */
1976 ret = 0;
1977 goto command_cleanup;
1978 case COMP_SUCCESS:
1979 xhci_dbg(xhci, "Successful reset device command.\n");
1980 break;
1981 default:
1982 if (xhci_is_vendor_info_code(xhci, ret))
1983 break;
1984 xhci_warn(xhci, "Unknown completion code %u for "
1985 "reset device command.\n", ret);
1986 ret = -EINVAL;
1987 goto command_cleanup;
1988 }
1989
1990 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
1991 last_freed_endpoint = 1;
1992 for (i = 1; i < 31; ++i) {
1993 if (!virt_dev->eps[i].ring)
1994 continue;
1995 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
1996 last_freed_endpoint = i;
1997 }
1998 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
1999 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2000 ret = 0;
2001
2002command_cleanup:
2003 xhci_free_command(xhci, reset_device_cmd);
2004 return ret;
2005}
2006
2007/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002008 * At this point, the struct usb_device is about to go away, the device has
2009 * disconnected, and all traffic has been stopped and the endpoints have been
2010 * disabled. Free any HC data structures associated with that device.
2011 */
2012void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2013{
2014 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002015 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002016 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002017 u32 state;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002018 int i;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002019
2020 if (udev->slot_id == 0)
2021 return;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002022 virt_dev = xhci->devs[udev->slot_id];
2023 if (!virt_dev)
2024 return;
2025
2026 /* Stop any wayward timer functions (which may grab the lock) */
2027 for (i = 0; i < 31; ++i) {
2028 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2029 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2030 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002031
2032 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002033 /* Don't disable the slot if the host controller is dead. */
2034 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002035 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002036 xhci_free_virt_device(xhci, udev->slot_id);
2037 spin_unlock_irqrestore(&xhci->lock, flags);
2038 return;
2039 }
2040
Sarah Sharp23e3be12009-04-29 19:05:20 -07002041 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002042 spin_unlock_irqrestore(&xhci->lock, flags);
2043 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2044 return;
2045 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002046 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002047 spin_unlock_irqrestore(&xhci->lock, flags);
2048 /*
2049 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07002050 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002051 */
2052}
2053
2054/*
2055 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2056 * timed out, or allocating memory failed. Returns 1 on success.
2057 */
2058int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2059{
2060 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2061 unsigned long flags;
2062 int timeleft;
2063 int ret;
2064
2065 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002066 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002067 if (ret) {
2068 spin_unlock_irqrestore(&xhci->lock, flags);
2069 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2070 return 0;
2071 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002072 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002073 spin_unlock_irqrestore(&xhci->lock, flags);
2074
2075 /* XXX: how much time for xHC slot assignment? */
2076 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2077 USB_CTRL_SET_TIMEOUT);
2078 if (timeleft <= 0) {
2079 xhci_warn(xhci, "%s while waiting for a slot\n",
2080 timeleft == 0 ? "Timeout" : "Signal");
2081 /* FIXME cancel the enable slot request */
2082 return 0;
2083 }
2084
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002085 if (!xhci->slot_id) {
2086 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002087 return 0;
2088 }
Sarah Sharpf88ba782009-05-14 11:44:22 -07002089 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002090 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
2091 /* Disable slot, if we can do it without mem alloc */
2092 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07002093 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002094 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2095 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002096 spin_unlock_irqrestore(&xhci->lock, flags);
2097 return 0;
2098 }
2099 udev->slot_id = xhci->slot_id;
2100 /* Is this a LS or FS device under a HS hub? */
2101 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002102 return 1;
2103}
2104
2105/*
2106 * Issue an Address Device command (which will issue a SetAddress request to
2107 * the device).
2108 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2109 * we should only issue and wait on one address command at the same time.
2110 *
2111 * We add one to the device address issued by the hardware because the USB core
2112 * uses address 1 for the root hubs (even though they're not really devices).
2113 */
2114int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2115{
2116 unsigned long flags;
2117 int timeleft;
2118 struct xhci_virt_device *virt_dev;
2119 int ret = 0;
2120 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07002121 struct xhci_slot_ctx *slot_ctx;
2122 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07002123 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002124
2125 if (!udev->slot_id) {
2126 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2127 return -EINVAL;
2128 }
2129
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002130 virt_dev = xhci->devs[udev->slot_id];
2131
2132 /* If this is a Set Address to an unconfigured device, setup ep 0 */
2133 if (!udev->config)
2134 xhci_setup_addressable_virt_dev(xhci, udev);
Sarah Sharp2d1ee592010-07-09 17:08:54 +02002135 else
2136 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002137 /* Otherwise, assume the core has the device configured how it wants */
Sarah Sharp66e49d82009-07-27 12:03:46 -07002138 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002139 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002140
Sarah Sharpf88ba782009-05-14 11:44:22 -07002141 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07002142 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2143 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002144 if (ret) {
2145 spin_unlock_irqrestore(&xhci->lock, flags);
2146 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2147 return ret;
2148 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002149 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002150 spin_unlock_irqrestore(&xhci->lock, flags);
2151
2152 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2153 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2154 USB_CTRL_SET_TIMEOUT);
2155 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2156 * the SetAddress() "recovery interval" required by USB and aborting the
2157 * command on a timeout.
2158 */
2159 if (timeleft <= 0) {
2160 xhci_warn(xhci, "%s while waiting for a slot\n",
2161 timeleft == 0 ? "Timeout" : "Signal");
2162 /* FIXME cancel the address device command */
2163 return -ETIME;
2164 }
2165
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002166 switch (virt_dev->cmd_status) {
2167 case COMP_CTX_STATE:
2168 case COMP_EBADSLT:
2169 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2170 udev->slot_id);
2171 ret = -EINVAL;
2172 break;
2173 case COMP_TX_ERR:
2174 dev_warn(&udev->dev, "Device not responding to set address.\n");
2175 ret = -EPROTO;
2176 break;
2177 case COMP_SUCCESS:
2178 xhci_dbg(xhci, "Successful Address Device command\n");
2179 break;
2180 default:
2181 xhci_err(xhci, "ERROR: unexpected command completion "
2182 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002183 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002184 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002185 ret = -EINVAL;
2186 break;
2187 }
2188 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002189 return ret;
2190 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07002191 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2192 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2193 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002194 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002195 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2196 (unsigned long long)
2197 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002198 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07002199 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002200 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002201 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002202 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002203 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002204 /*
2205 * USB core uses address 1 for the roothubs, so we add one to the
2206 * address given back to us by the HC.
2207 */
John Yound115b042009-07-27 12:05:15 -07002208 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
2209 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002210 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07002211 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2212 ctrl_ctx->add_flags = 0;
2213 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002214
2215 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
2216 /* XXX Meh, not sure if anyone else but choose_address uses this. */
2217 set_bit(udev->devnum, udev->bus->devmap.devicemap);
2218
2219 return 0;
2220}
2221
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002222/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2223 * internal data structures for the device.
2224 */
2225int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2226 struct usb_tt *tt, gfp_t mem_flags)
2227{
2228 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2229 struct xhci_virt_device *vdev;
2230 struct xhci_command *config_cmd;
2231 struct xhci_input_control_ctx *ctrl_ctx;
2232 struct xhci_slot_ctx *slot_ctx;
2233 unsigned long flags;
2234 unsigned think_time;
2235 int ret;
2236
2237 /* Ignore root hubs */
2238 if (!hdev->parent)
2239 return 0;
2240
2241 vdev = xhci->devs[hdev->slot_id];
2242 if (!vdev) {
2243 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2244 return -EINVAL;
2245 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08002246 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002247 if (!config_cmd) {
2248 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2249 return -ENOMEM;
2250 }
2251
2252 spin_lock_irqsave(&xhci->lock, flags);
2253 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2254 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2255 ctrl_ctx->add_flags |= SLOT_FLAG;
2256 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2257 slot_ctx->dev_info |= DEV_HUB;
2258 if (tt->multi)
2259 slot_ctx->dev_info |= DEV_MTT;
2260 if (xhci->hci_version > 0x95) {
2261 xhci_dbg(xhci, "xHCI version %x needs hub "
2262 "TT think time and number of ports\n",
2263 (unsigned int) xhci->hci_version);
2264 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2265 /* Set TT think time - convert from ns to FS bit times.
2266 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2267 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2268 */
2269 think_time = tt->think_time;
2270 if (think_time != 0)
2271 think_time = (think_time / 666) - 1;
2272 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2273 } else {
2274 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2275 "TT think time or number of ports\n",
2276 (unsigned int) xhci->hci_version);
2277 }
2278 slot_ctx->dev_state = 0;
2279 spin_unlock_irqrestore(&xhci->lock, flags);
2280
2281 xhci_dbg(xhci, "Set up %s for hub device.\n",
2282 (xhci->hci_version > 0x95) ?
2283 "configure endpoint" : "evaluate context");
2284 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2285 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2286
2287 /* Issue and wait for the configure endpoint or
2288 * evaluate context command.
2289 */
2290 if (xhci->hci_version > 0x95)
2291 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2292 false, false);
2293 else
2294 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2295 true, false);
2296
2297 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2298 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2299
2300 xhci_free_command(xhci, config_cmd);
2301 return ret;
2302}
2303
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002304int xhci_get_frame(struct usb_hcd *hcd)
2305{
2306 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2307 /* EHCI mods by the periodic size. Why? */
2308 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2309}
2310
2311MODULE_DESCRIPTION(DRIVER_DESC);
2312MODULE_AUTHOR(DRIVER_AUTHOR);
2313MODULE_LICENSE("GPL");
2314
2315static int __init xhci_hcd_init(void)
2316{
2317#ifdef CONFIG_PCI
2318 int retval = 0;
2319
2320 retval = xhci_register_pci();
2321
2322 if (retval < 0) {
2323 printk(KERN_DEBUG "Problem registering PCI driver.");
2324 return retval;
2325 }
2326#endif
Sarah Sharp98441972009-05-14 11:44:18 -07002327 /*
2328 * Check the compiler generated sizes of structures that must be laid
2329 * out in specific ways for hardware access.
2330 */
2331 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2332 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2333 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2334 /* xhci_device_control has eight fields, and also
2335 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2336 */
Sarah Sharp98441972009-05-14 11:44:18 -07002337 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2338 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2339 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2340 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2341 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2342 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2343 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2344 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002345 return 0;
2346}
2347module_init(xhci_hcd_init);
2348
2349static void __exit xhci_hcd_cleanup(void)
2350{
2351#ifdef CONFIG_PCI
2352 xhci_unregister_pci();
2353#endif
2354}
2355module_exit(xhci_hcd_cleanup);