USB: xhci: Wait for controller to be ready after reset.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / host / xhci.c
CommitLineData
66d4eadd
SS
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/irq.h>
8df75f42 24#include <linux/log2.h>
66d4eadd 25#include <linux/module.h>
b0567b3f 26#include <linux/moduleparam.h>
5a0e3ad6 27#include <linux/slab.h>
66d4eadd
SS
28
29#include "xhci.h"
30
31#define DRIVER_AUTHOR "Sarah Sharp"
32#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
33
b0567b3f
SS
34/* 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
66d4eadd
SS
39/* 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/*
4f0f0bae 72 * Disable interrupts and begin the xHCI halting process.
66d4eadd 73 */
4f0f0bae 74void xhci_quiesce(struct xhci_hcd *xhci)
66d4eadd
SS
75{
76 u32 halted;
77 u32 cmd;
78 u32 mask;
79
66d4eadd
SS
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);
4f0f0bae
SS
88}
89
90/*
91 * 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{
101 xhci_dbg(xhci, "// Halt the HC\n");
102 xhci_quiesce(xhci);
66d4eadd
SS
103
104 return handshake(xhci, &xhci->op_regs->status,
105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
106}
107
108/*
109 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
110 *
111 * This resets pipelines, timers, counters, state machines, etc.
112 * Transactions will be terminated immediately, and operational registers
113 * will be set to their defaults.
114 */
115int xhci_reset(struct xhci_hcd *xhci)
116{
117 u32 command;
118 u32 state;
2d62f3ee 119 int ret;
66d4eadd
SS
120
121 state = xhci_readl(xhci, &xhci->op_regs->status);
d3512f63
SS
122 if ((state & STS_HALT) == 0) {
123 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
124 return 0;
125 }
66d4eadd
SS
126
127 xhci_dbg(xhci, "// Reset the HC\n");
128 command = xhci_readl(xhci, &xhci->op_regs->command);
129 command |= CMD_RESET;
130 xhci_writel(xhci, command, &xhci->op_regs->command);
131 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
132 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
133
2d62f3ee
SS
134 ret = handshake(xhci, &xhci->op_regs->command,
135 CMD_RESET, 0, 250 * 1000);
136 if (ret)
137 return ret;
138
139 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
140 /*
141 * xHCI cannot write to any doorbells or operational registers other
142 * than status until the "Controller Not Ready" flag is cleared.
143 */
144 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
66d4eadd
SS
145}
146
66d4eadd
SS
147
148#if 0
149/* Set up MSI-X table for entry 0 (may claim other entries later) */
150static int xhci_setup_msix(struct xhci_hcd *xhci)
151{
152 int ret;
153 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
154
155 xhci->msix_count = 0;
156 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
157 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
158 if (!xhci->msix_entries) {
159 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
160 return -ENOMEM;
161 }
162 xhci->msix_entries[0].entry = 0;
163
164 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
165 if (ret) {
166 xhci_err(xhci, "Failed to enable MSI-X\n");
167 goto free_entries;
168 }
169
170 /*
171 * Pass the xhci pointer value as the request_irq "cookie".
172 * If more irqs are added, this will need to be unique for each one.
173 */
174 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
175 "xHCI", xhci_to_hcd(xhci));
176 if (ret) {
177 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
178 goto disable_msix;
179 }
180 xhci_dbg(xhci, "Finished setting up MSI-X\n");
181 return 0;
182
183disable_msix:
184 pci_disable_msix(pdev);
185free_entries:
186 kfree(xhci->msix_entries);
187 xhci->msix_entries = NULL;
188 return ret;
189}
190
191/* XXX: code duplication; can xhci_setup_msix call this? */
192/* Free any IRQs and disable MSI-X */
193static void xhci_cleanup_msix(struct xhci_hcd *xhci)
194{
195 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
196 if (!xhci->msix_entries)
197 return;
198
199 free_irq(xhci->msix_entries[0].vector, xhci);
200 pci_disable_msix(pdev);
201 kfree(xhci->msix_entries);
202 xhci->msix_entries = NULL;
203 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
204}
205#endif
206
207/*
208 * Initialize memory for HCD and xHC (one-time init).
209 *
210 * Program the PAGESIZE register, initialize the device context array, create
211 * device contexts (?), set up a command ring segment (or two?), create event
212 * ring (one for now).
213 */
214int xhci_init(struct usb_hcd *hcd)
215{
216 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
217 int retval = 0;
218
219 xhci_dbg(xhci, "xhci_init\n");
220 spin_lock_init(&xhci->lock);
b0567b3f
SS
221 if (link_quirk) {
222 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
223 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
224 } else {
ac9d8fe7 225 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
b0567b3f 226 }
66d4eadd
SS
227 retval = xhci_mem_init(xhci, GFP_KERNEL);
228 xhci_dbg(xhci, "Finished xhci_init\n");
229
230 return retval;
231}
232
7f84eef0
SS
233/*
234 * Called in interrupt context when there might be work
235 * queued on the event ring
236 *
237 * xhci->lock must be held by caller.
238 */
239static void xhci_work(struct xhci_hcd *xhci)
240{
241 u32 temp;
8e595a5d 242 u64 temp_64;
7f84eef0
SS
243
244 /*
245 * Clear the op reg interrupt status first,
246 * so we can receive interrupts from other MSI-X interrupters.
247 * Write 1 to clear the interrupt status.
248 */
249 temp = xhci_readl(xhci, &xhci->op_regs->status);
250 temp |= STS_EINT;
251 xhci_writel(xhci, temp, &xhci->op_regs->status);
252 /* FIXME when MSI-X is supported and there are multiple vectors */
253 /* Clear the MSI-X event interrupt status */
254
255 /* Acknowledge the interrupt */
256 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
257 temp |= 0x3;
258 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
259 /* Flush posted writes */
260 xhci_readl(xhci, &xhci->ir_set->irq_pending);
261
6f5165cf
SS
262 if (xhci->xhc_state & XHCI_STATE_DYING)
263 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
264 "Shouldn't IRQs be disabled?\n");
265 else
266 /* FIXME this should be a delayed service routine
267 * that clears the EHB.
268 */
269 xhci_handle_event(xhci);
7f84eef0 270
2d83109b 271 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
8e595a5d 272 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2d83109b 273 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
7f84eef0
SS
274 /* Flush posted writes -- FIXME is this necessary? */
275 xhci_readl(xhci, &xhci->ir_set->irq_pending);
276}
277
278/*-------------------------------------------------------------------------*/
279
280/*
281 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
282 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
283 * indicators of an event TRB error, but we check the status *first* to be safe.
284 */
285irqreturn_t xhci_irq(struct usb_hcd *hcd)
286{
287 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
288 u32 temp, temp2;
66e49d87 289 union xhci_trb *trb;
7f84eef0
SS
290
291 spin_lock(&xhci->lock);
66e49d87 292 trb = xhci->event_ring->dequeue;
7f84eef0
SS
293 /* Check if the xHC generated the interrupt, or the irq is shared */
294 temp = xhci_readl(xhci, &xhci->op_regs->status);
295 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
fcf8f576
SS
296 if (temp == 0xffffffff && temp2 == 0xffffffff)
297 goto hw_died;
298
7f84eef0
SS
299 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
300 spin_unlock(&xhci->lock);
301 return IRQ_NONE;
302 }
66e49d87
SS
303 xhci_dbg(xhci, "op reg status = %08x\n", temp);
304 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
305 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
306 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
307 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
308 lower_32_bits(trb->link.segment_ptr),
309 upper_32_bits(trb->link.segment_ptr),
310 (unsigned int) trb->link.intr_target,
311 (unsigned int) trb->link.control);
7f84eef0 312
7f84eef0
SS
313 if (temp & STS_FATAL) {
314 xhci_warn(xhci, "WARNING: Host System Error\n");
315 xhci_halt(xhci);
fcf8f576 316hw_died:
7f84eef0 317 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
c96a2b81 318 spin_unlock(&xhci->lock);
7f84eef0
SS
319 return -ESHUTDOWN;
320 }
321
322 xhci_work(xhci);
323 spin_unlock(&xhci->lock);
324
325 return IRQ_HANDLED;
326}
327
328#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
23e3be11 329void xhci_event_ring_work(unsigned long arg)
7f84eef0
SS
330{
331 unsigned long flags;
332 int temp;
8e595a5d 333 u64 temp_64;
7f84eef0
SS
334 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
335 int i, j;
336
337 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
338
339 spin_lock_irqsave(&xhci->lock, flags);
340 temp = xhci_readl(xhci, &xhci->op_regs->status);
341 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
6f5165cf 342 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
e4ab05df
SS
343 xhci_dbg(xhci, "HW died, polling stopped.\n");
344 spin_unlock_irqrestore(&xhci->lock, flags);
345 return;
346 }
347
7f84eef0
SS
348 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
349 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
350 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
351 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
352 xhci->error_bitmask = 0;
353 xhci_dbg(xhci, "Event ring:\n");
354 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
355 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
8e595a5d
SS
356 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
357 temp_64 &= ~ERST_PTR_MASK;
358 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
7f84eef0
SS
359 xhci_dbg(xhci, "Command ring:\n");
360 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
361 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
362 xhci_dbg_cmd_ptrs(xhci);
3ffbba95 363 for (i = 0; i < MAX_HC_SLOTS; ++i) {
63a0d9ab
SS
364 if (!xhci->devs[i])
365 continue;
366 for (j = 0; j < 31; ++j) {
e9df17eb 367 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
3ffbba95
SS
368 }
369 }
7f84eef0
SS
370
371 if (xhci->noops_submitted != NUM_TEST_NOOPS)
23e3be11
SS
372 if (xhci_setup_one_noop(xhci))
373 xhci_ring_cmd_db(xhci);
7f84eef0
SS
374 spin_unlock_irqrestore(&xhci->lock, flags);
375
376 if (!xhci->zombie)
377 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
378 else
379 xhci_dbg(xhci, "Quit polling the event ring.\n");
380}
381#endif
382
66d4eadd
SS
383/*
384 * Start the HC after it was halted.
385 *
386 * This function is called by the USB core when the HC driver is added.
387 * Its opposite is xhci_stop().
388 *
389 * xhci_init() must be called once before this function can be called.
390 * Reset the HC, enable device slot contexts, program DCBAAP, and
391 * set command ring pointer and event ring pointer.
392 *
393 * Setup MSI-X vectors and enable interrupts.
394 */
395int xhci_run(struct usb_hcd *hcd)
396{
397 u32 temp;
8e595a5d 398 u64 temp_64;
66d4eadd 399 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
7f84eef0 400 void (*doorbell)(struct xhci_hcd *) = NULL;
66d4eadd 401
0f2a7930
SS
402 hcd->uses_new_polling = 1;
403 hcd->poll_rh = 0;
404
7f84eef0 405 xhci_dbg(xhci, "xhci_run\n");
66d4eadd
SS
406#if 0 /* FIXME: MSI not setup yet */
407 /* Do this at the very last minute */
408 ret = xhci_setup_msix(xhci);
409 if (!ret)
410 return ret;
411
412 return -ENOSYS;
413#endif
7f84eef0
SS
414#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
415 init_timer(&xhci->event_ring_timer);
416 xhci->event_ring_timer.data = (unsigned long) xhci;
23e3be11 417 xhci->event_ring_timer.function = xhci_event_ring_work;
7f84eef0
SS
418 /* Poll the event ring */
419 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
420 xhci->zombie = 0;
421 xhci_dbg(xhci, "Setting event ring polling timer\n");
422 add_timer(&xhci->event_ring_timer);
423#endif
424
66e49d87
SS
425 xhci_dbg(xhci, "Command ring memory map follows:\n");
426 xhci_debug_ring(xhci, xhci->cmd_ring);
427 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
428 xhci_dbg_cmd_ptrs(xhci);
429
430 xhci_dbg(xhci, "ERST memory map follows:\n");
431 xhci_dbg_erst(xhci, &xhci->erst);
432 xhci_dbg(xhci, "Event ring:\n");
433 xhci_debug_ring(xhci, xhci->event_ring);
434 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
435 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
436 temp_64 &= ~ERST_PTR_MASK;
437 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
438
66d4eadd
SS
439 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
440 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
a4d88302 441 temp &= ~ER_IRQ_INTERVAL_MASK;
66d4eadd
SS
442 temp |= (u32) 160;
443 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
444
445 /* Set the HCD state before we enable the irqs */
446 hcd->state = HC_STATE_RUNNING;
447 temp = xhci_readl(xhci, &xhci->op_regs->command);
448 temp |= (CMD_EIE);
449 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
450 temp);
451 xhci_writel(xhci, temp, &xhci->op_regs->command);
452
453 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
700e2052
GKH
454 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
455 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
66d4eadd
SS
456 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
457 &xhci->ir_set->irq_pending);
458 xhci_print_ir_set(xhci, xhci->ir_set, 0);
459
7f84eef0 460 if (NUM_TEST_NOOPS > 0)
23e3be11 461 doorbell = xhci_setup_one_noop(xhci);
7f84eef0 462
66d4eadd
SS
463 temp = xhci_readl(xhci, &xhci->op_regs->command);
464 temp |= (CMD_RUN);
465 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
466 temp);
467 xhci_writel(xhci, temp, &xhci->op_regs->command);
468 /* Flush PCI posted writes */
469 temp = xhci_readl(xhci, &xhci->op_regs->command);
700e2052 470 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
7f84eef0
SS
471 if (doorbell)
472 (*doorbell)(xhci);
66d4eadd
SS
473
474 xhci_dbg(xhci, "Finished xhci_run\n");
475 return 0;
476}
477
478/*
479 * Stop xHCI driver.
480 *
481 * This function is called by the USB core when the HC driver is removed.
482 * Its opposite is xhci_run().
483 *
484 * Disable device contexts, disable IRQs, and quiesce the HC.
485 * Reset the HC, finish any completed transactions, and cleanup memory.
486 */
487void xhci_stop(struct usb_hcd *hcd)
488{
489 u32 temp;
490 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
491
492 spin_lock_irq(&xhci->lock);
66d4eadd
SS
493 xhci_halt(xhci);
494 xhci_reset(xhci);
495 spin_unlock_irq(&xhci->lock);
496
497#if 0 /* No MSI yet */
498 xhci_cleanup_msix(xhci);
499#endif
7f84eef0
SS
500#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
501 /* Tell the event ring poll function not to reschedule */
502 xhci->zombie = 1;
503 del_timer_sync(&xhci->event_ring_timer);
504#endif
505
66d4eadd
SS
506 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
507 temp = xhci_readl(xhci, &xhci->op_regs->status);
508 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
509 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
510 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
511 &xhci->ir_set->irq_pending);
512 xhci_print_ir_set(xhci, xhci->ir_set, 0);
513
514 xhci_dbg(xhci, "cleaning up memory\n");
515 xhci_mem_cleanup(xhci);
516 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
517 xhci_readl(xhci, &xhci->op_regs->status));
518}
519
520/*
521 * Shutdown HC (not bus-specific)
522 *
523 * This is called when the machine is rebooting or halting. We assume that the
524 * machine will be powered off, and the HC's internal state will be reset.
525 * Don't bother to free memory.
526 */
527void xhci_shutdown(struct usb_hcd *hcd)
528{
529 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
530
531 spin_lock_irq(&xhci->lock);
532 xhci_halt(xhci);
533 spin_unlock_irq(&xhci->lock);
534
535#if 0
536 xhci_cleanup_msix(xhci);
537#endif
538
539 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
540 xhci_readl(xhci, &xhci->op_regs->status));
541}
542
7f84eef0
SS
543/*-------------------------------------------------------------------------*/
544
d0e96f5a
SS
545/**
546 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
547 * HCDs. Find the index for an endpoint given its descriptor. Use the return
548 * value to right shift 1 for the bitmask.
549 *
550 * Index = (epnum * 2) + direction - 1,
551 * where direction = 0 for OUT, 1 for IN.
552 * For control endpoints, the IN index is used (OUT index is unused), so
553 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
554 */
555unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
556{
557 unsigned int index;
558 if (usb_endpoint_xfer_control(desc))
559 index = (unsigned int) (usb_endpoint_num(desc)*2);
560 else
561 index = (unsigned int) (usb_endpoint_num(desc)*2) +
562 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
563 return index;
564}
565
f94e0186
SS
566/* Find the flag for this endpoint (for use in the control context). Use the
567 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
568 * bit 1, etc.
569 */
570unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
571{
572 return 1 << (xhci_get_endpoint_index(desc) + 1);
573}
574
ac9d8fe7
SS
575/* Find the flag for this endpoint (for use in the control context). Use the
576 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
577 * bit 1, etc.
578 */
579unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
580{
581 return 1 << (ep_index + 1);
582}
583
f94e0186
SS
584/* Compute the last valid endpoint context index. Basically, this is the
585 * endpoint index plus one. For slot contexts with more than valid endpoint,
586 * we find the most significant bit set in the added contexts flags.
587 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
588 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
589 */
ac9d8fe7 590unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
f94e0186
SS
591{
592 return fls(added_ctxs) - 1;
593}
594
d0e96f5a
SS
595/* Returns 1 if the arguments are OK;
596 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
597 */
598int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
599 struct usb_host_endpoint *ep, int check_ep, const char *func) {
600 if (!hcd || (check_ep && !ep) || !udev) {
601 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
602 func);
603 return -EINVAL;
604 }
605 if (!udev->parent) {
606 printk(KERN_DEBUG "xHCI %s called for root hub\n",
607 func);
608 return 0;
609 }
610 if (!udev->slot_id) {
611 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
612 func);
613 return -EINVAL;
614 }
615 return 1;
616}
617
2d3f1fac 618static int xhci_configure_endpoint(struct xhci_hcd *xhci,
913a8a34
SS
619 struct usb_device *udev, struct xhci_command *command,
620 bool ctx_change, bool must_succeed);
2d3f1fac
SS
621
622/*
623 * Full speed devices may have a max packet size greater than 8 bytes, but the
624 * USB core doesn't know that until it reads the first 8 bytes of the
625 * descriptor. If the usb_device's max packet size changes after that point,
626 * we need to issue an evaluate context command and wait on it.
627 */
628static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
629 unsigned int ep_index, struct urb *urb)
630{
631 struct xhci_container_ctx *in_ctx;
632 struct xhci_container_ctx *out_ctx;
633 struct xhci_input_control_ctx *ctrl_ctx;
634 struct xhci_ep_ctx *ep_ctx;
635 int max_packet_size;
636 int hw_max_packet_size;
637 int ret = 0;
638
639 out_ctx = xhci->devs[slot_id]->out_ctx;
640 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
641 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
642 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
643 if (hw_max_packet_size != max_packet_size) {
644 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
645 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
646 max_packet_size);
647 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
648 hw_max_packet_size);
649 xhci_dbg(xhci, "Issuing evaluate context command.\n");
650
651 /* Set up the modified control endpoint 0 */
913a8a34
SS
652 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
653 xhci->devs[slot_id]->out_ctx, ep_index);
2d3f1fac
SS
654 in_ctx = xhci->devs[slot_id]->in_ctx;
655 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
656 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
657 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
658
659 /* Set up the input context flags for the command */
660 /* FIXME: This won't work if a non-default control endpoint
661 * changes max packet sizes.
662 */
663 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
664 ctrl_ctx->add_flags = EP0_FLAG;
665 ctrl_ctx->drop_flags = 0;
666
667 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
668 xhci_dbg_ctx(xhci, in_ctx, ep_index);
669 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
670 xhci_dbg_ctx(xhci, out_ctx, ep_index);
671
913a8a34
SS
672 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
673 true, false);
2d3f1fac
SS
674
675 /* Clean up the input context for later use by bandwidth
676 * functions.
677 */
678 ctrl_ctx->add_flags = SLOT_FLAG;
679 }
680 return ret;
681}
682
d0e96f5a
SS
683/*
684 * non-error returns are a promise to giveback() the urb later
685 * we drop ownership so next owner (or urb unlink) can get it
686 */
687int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
688{
689 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
690 unsigned long flags;
691 int ret = 0;
692 unsigned int slot_id, ep_index;
693
2d3f1fac 694
d0e96f5a
SS
695 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
696 return -EINVAL;
697
698 slot_id = urb->dev->slot_id;
699 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
d0e96f5a 700
d0e96f5a
SS
701 if (!xhci->devs || !xhci->devs[slot_id]) {
702 if (!in_interrupt())
703 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
c7959fb2
SS
704 ret = -EINVAL;
705 goto exit;
d0e96f5a
SS
706 }
707 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
708 if (!in_interrupt())
709 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
710 ret = -ESHUTDOWN;
711 goto exit;
712 }
2d3f1fac
SS
713 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
714 /* Check to see if the max packet size for the default control
715 * endpoint changed during FS device enumeration
716 */
717 if (urb->dev->speed == USB_SPEED_FULL) {
718 ret = xhci_check_maxpacket(xhci, slot_id,
719 ep_index, urb);
720 if (ret < 0)
721 return ret;
722 }
723
b11069f5
SS
724 /* We have a spinlock and interrupts disabled, so we must pass
725 * atomic context to this function, which may allocate memory.
726 */
2d3f1fac 727 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
728 if (xhci->xhc_state & XHCI_STATE_DYING)
729 goto dying;
b11069f5 730 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
23e3be11 731 slot_id, ep_index);
2d3f1fac
SS
732 spin_unlock_irqrestore(&xhci->lock, flags);
733 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
734 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
735 if (xhci->xhc_state & XHCI_STATE_DYING)
736 goto dying;
8df75f42
SS
737 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
738 EP_GETTING_STREAMS) {
739 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
740 "is transitioning to using streams.\n");
741 ret = -EINVAL;
742 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
743 EP_GETTING_NO_STREAMS) {
744 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
745 "is transitioning to "
746 "not having streams.\n");
747 ret = -EINVAL;
748 } else {
749 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
750 slot_id, ep_index);
751 }
2d3f1fac 752 spin_unlock_irqrestore(&xhci->lock, flags);
624defa1
SS
753 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
754 spin_lock_irqsave(&xhci->lock, flags);
6f5165cf
SS
755 if (xhci->xhc_state & XHCI_STATE_DYING)
756 goto dying;
624defa1
SS
757 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
758 slot_id, ep_index);
759 spin_unlock_irqrestore(&xhci->lock, flags);
2d3f1fac 760 } else {
b10de142 761 ret = -EINVAL;
2d3f1fac 762 }
d0e96f5a 763exit:
d0e96f5a 764 return ret;
6f5165cf
SS
765dying:
766 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
767 "non-responsive xHCI host.\n",
768 urb->ep->desc.bEndpointAddress, urb);
769 spin_unlock_irqrestore(&xhci->lock, flags);
770 return -ESHUTDOWN;
d0e96f5a
SS
771}
772
ae636747
SS
773/*
774 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
775 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
776 * should pick up where it left off in the TD, unless a Set Transfer Ring
777 * Dequeue Pointer is issued.
778 *
779 * The TRBs that make up the buffers for the canceled URB will be "removed" from
780 * the ring. Since the ring is a contiguous structure, they can't be physically
781 * removed. Instead, there are two options:
782 *
783 * 1) If the HC is in the middle of processing the URB to be canceled, we
784 * simply move the ring's dequeue pointer past those TRBs using the Set
785 * Transfer Ring Dequeue Pointer command. This will be the common case,
786 * when drivers timeout on the last submitted URB and attempt to cancel.
787 *
788 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
789 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
790 * HC will need to invalidate the any TRBs it has cached after the stop
791 * endpoint command, as noted in the xHCI 0.95 errata.
792 *
793 * 3) The TD may have completed by the time the Stop Endpoint Command
794 * completes, so software needs to handle that case too.
795 *
796 * This function should protect against the TD enqueueing code ringing the
797 * doorbell while this code is waiting for a Stop Endpoint command to complete.
798 * It also needs to account for multiple cancellations on happening at the same
799 * time for the same endpoint.
800 *
801 * Note that this function can be called in any context, or so says
802 * usb_hcd_unlink_urb()
d0e96f5a
SS
803 */
804int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
805{
ae636747
SS
806 unsigned long flags;
807 int ret;
e34b2fbf 808 u32 temp;
ae636747
SS
809 struct xhci_hcd *xhci;
810 struct xhci_td *td;
811 unsigned int ep_index;
812 struct xhci_ring *ep_ring;
63a0d9ab 813 struct xhci_virt_ep *ep;
ae636747
SS
814
815 xhci = hcd_to_xhci(hcd);
816 spin_lock_irqsave(&xhci->lock, flags);
817 /* Make sure the URB hasn't completed or been unlinked already */
818 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
819 if (ret || !urb->hcpriv)
820 goto done;
e34b2fbf
SS
821 temp = xhci_readl(xhci, &xhci->op_regs->status);
822 if (temp == 0xffffffff) {
823 xhci_dbg(xhci, "HW died, freeing TD.\n");
824 td = (struct xhci_td *) urb->hcpriv;
825
826 usb_hcd_unlink_urb_from_ep(hcd, urb);
827 spin_unlock_irqrestore(&xhci->lock, flags);
828 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
829 kfree(td);
830 return ret;
831 }
6f5165cf
SS
832 if (xhci->xhc_state & XHCI_STATE_DYING) {
833 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
834 "non-responsive xHCI host.\n",
835 urb->ep->desc.bEndpointAddress, urb);
836 /* Let the stop endpoint command watchdog timer (which set this
837 * state) finish cleaning up the endpoint TD lists. We must
838 * have caught it in the middle of dropping a lock and giving
839 * back an URB.
840 */
841 goto done;
842 }
ae636747 843
700e2052 844 xhci_dbg(xhci, "Cancel URB %p\n", urb);
66e49d87
SS
845 xhci_dbg(xhci, "Event ring:\n");
846 xhci_debug_ring(xhci, xhci->event_ring);
ae636747 847 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
63a0d9ab 848 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
e9df17eb
SS
849 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
850 if (!ep_ring) {
851 ret = -EINVAL;
852 goto done;
853 }
854
66e49d87
SS
855 xhci_dbg(xhci, "Endpoint ring:\n");
856 xhci_debug_ring(xhci, ep_ring);
ae636747
SS
857 td = (struct xhci_td *) urb->hcpriv;
858
63a0d9ab 859 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
ae636747
SS
860 /* Queue a stop endpoint command, but only if this is
861 * the first cancellation to be handled.
862 */
678539cf
SS
863 if (!(ep->ep_state & EP_HALT_PENDING)) {
864 ep->ep_state |= EP_HALT_PENDING;
6f5165cf
SS
865 ep->stop_cmds_pending++;
866 ep->stop_cmd_timer.expires = jiffies +
867 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
868 add_timer(&ep->stop_cmd_timer);
23e3be11
SS
869 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
870 xhci_ring_cmd_db(xhci);
ae636747
SS
871 }
872done:
873 spin_unlock_irqrestore(&xhci->lock, flags);
874 return ret;
d0e96f5a
SS
875}
876
f94e0186
SS
877/* Drop an endpoint from a new bandwidth configuration for this device.
878 * Only one call to this function is allowed per endpoint before
879 * check_bandwidth() or reset_bandwidth() must be called.
880 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
881 * add the endpoint to the schedule with possibly new parameters denoted by a
882 * different endpoint descriptor in usb_host_endpoint.
883 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
884 * not allowed.
f88ba78d
SS
885 *
886 * The USB core will not allow URBs to be queued to an endpoint that is being
887 * disabled, so there's no need for mutual exclusion to protect
888 * the xhci->devs[slot_id] structure.
f94e0186
SS
889 */
890int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
891 struct usb_host_endpoint *ep)
892{
f94e0186 893 struct xhci_hcd *xhci;
d115b048
JY
894 struct xhci_container_ctx *in_ctx, *out_ctx;
895 struct xhci_input_control_ctx *ctrl_ctx;
896 struct xhci_slot_ctx *slot_ctx;
f94e0186
SS
897 unsigned int last_ctx;
898 unsigned int ep_index;
899 struct xhci_ep_ctx *ep_ctx;
900 u32 drop_flag;
901 u32 new_add_flags, new_drop_flags, new_slot_info;
902 int ret;
903
904 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
f94e0186
SS
905 if (ret <= 0)
906 return ret;
907 xhci = hcd_to_xhci(hcd);
700e2052 908 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
f94e0186
SS
909
910 drop_flag = xhci_get_endpoint_flag(&ep->desc);
911 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
912 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
913 __func__, drop_flag);
914 return 0;
915 }
916
f94e0186
SS
917 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
918 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
919 __func__);
f94e0186
SS
920 return -EINVAL;
921 }
922
923 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
d115b048
JY
924 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
925 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
f94e0186 926 ep_index = xhci_get_endpoint_index(&ep->desc);
d115b048 927 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
f94e0186
SS
928 /* If the HC already knows the endpoint is disabled,
929 * or the HCD has noted it is disabled, ignore this request
930 */
931 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
d115b048 932 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
700e2052
GKH
933 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
934 __func__, ep);
f94e0186
SS
935 return 0;
936 }
937
d115b048
JY
938 ctrl_ctx->drop_flags |= drop_flag;
939 new_drop_flags = ctrl_ctx->drop_flags;
f94e0186 940
0a023c6c 941 ctrl_ctx->add_flags &= ~drop_flag;
d115b048 942 new_add_flags = ctrl_ctx->add_flags;
f94e0186 943
d115b048
JY
944 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
945 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
f94e0186 946 /* Update the last valid endpoint context, if we deleted the last one */
d115b048
JY
947 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
948 slot_ctx->dev_info &= ~LAST_CTX_MASK;
949 slot_ctx->dev_info |= LAST_CTX(last_ctx);
f94e0186 950 }
d115b048 951 new_slot_info = slot_ctx->dev_info;
f94e0186
SS
952
953 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
954
f94e0186
SS
955 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
956 (unsigned int) ep->desc.bEndpointAddress,
957 udev->slot_id,
958 (unsigned int) new_drop_flags,
959 (unsigned int) new_add_flags,
960 (unsigned int) new_slot_info);
961 return 0;
962}
963
964/* Add an endpoint to a new possible bandwidth configuration for this device.
965 * Only one call to this function is allowed per endpoint before
966 * check_bandwidth() or reset_bandwidth() must be called.
967 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
968 * add the endpoint to the schedule with possibly new parameters denoted by a
969 * different endpoint descriptor in usb_host_endpoint.
970 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
971 * not allowed.
f88ba78d
SS
972 *
973 * The USB core will not allow URBs to be queued to an endpoint until the
974 * configuration or alt setting is installed in the device, so there's no need
975 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
f94e0186
SS
976 */
977int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
978 struct usb_host_endpoint *ep)
979{
f94e0186 980 struct xhci_hcd *xhci;
d115b048 981 struct xhci_container_ctx *in_ctx, *out_ctx;
f94e0186
SS
982 unsigned int ep_index;
983 struct xhci_ep_ctx *ep_ctx;
d115b048
JY
984 struct xhci_slot_ctx *slot_ctx;
985 struct xhci_input_control_ctx *ctrl_ctx;
f94e0186
SS
986 u32 added_ctxs;
987 unsigned int last_ctx;
988 u32 new_add_flags, new_drop_flags, new_slot_info;
989 int ret = 0;
990
991 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
a1587d97
SS
992 if (ret <= 0) {
993 /* So we won't queue a reset ep command for a root hub */
994 ep->hcpriv = NULL;
f94e0186 995 return ret;
a1587d97 996 }
f94e0186
SS
997 xhci = hcd_to_xhci(hcd);
998
999 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1000 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1001 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1002 /* FIXME when we have to issue an evaluate endpoint command to
1003 * deal with ep0 max packet size changing once we get the
1004 * descriptors
1005 */
1006 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1007 __func__, added_ctxs);
1008 return 0;
1009 }
1010
f94e0186
SS
1011 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1012 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1013 __func__);
f94e0186
SS
1014 return -EINVAL;
1015 }
1016
1017 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
d115b048
JY
1018 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1019 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
f94e0186 1020 ep_index = xhci_get_endpoint_index(&ep->desc);
d115b048 1021 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
f94e0186
SS
1022 /* If the HCD has already noted the endpoint is enabled,
1023 * ignore this request.
1024 */
d115b048 1025 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
700e2052
GKH
1026 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1027 __func__, ep);
f94e0186
SS
1028 return 0;
1029 }
1030
f88ba78d
SS
1031 /*
1032 * Configuration and alternate setting changes must be done in
1033 * process context, not interrupt context (or so documenation
1034 * for usb_set_interface() and usb_set_configuration() claim).
1035 */
1036 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
319c3ea4 1037 udev, ep, GFP_NOIO) < 0) {
f94e0186
SS
1038 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1039 __func__, ep->desc.bEndpointAddress);
f94e0186
SS
1040 return -ENOMEM;
1041 }
1042
d115b048
JY
1043 ctrl_ctx->add_flags |= added_ctxs;
1044 new_add_flags = ctrl_ctx->add_flags;
f94e0186
SS
1045
1046 /* If xhci_endpoint_disable() was called for this endpoint, but the
1047 * xHC hasn't been notified yet through the check_bandwidth() call,
1048 * this re-adds a new state for the endpoint from the new endpoint
1049 * descriptors. We must drop and re-add this endpoint, so we leave the
1050 * drop flags alone.
1051 */
d115b048 1052 new_drop_flags = ctrl_ctx->drop_flags;
f94e0186 1053
d115b048 1054 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
f94e0186 1055 /* Update the last valid endpoint context, if we just added one past */
d115b048
JY
1056 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1057 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1058 slot_ctx->dev_info |= LAST_CTX(last_ctx);
f94e0186 1059 }
d115b048 1060 new_slot_info = slot_ctx->dev_info;
f94e0186 1061
a1587d97
SS
1062 /* Store the usb_device pointer for later use */
1063 ep->hcpriv = udev;
1064
f94e0186
SS
1065 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1066 (unsigned int) ep->desc.bEndpointAddress,
1067 udev->slot_id,
1068 (unsigned int) new_drop_flags,
1069 (unsigned int) new_add_flags,
1070 (unsigned int) new_slot_info);
1071 return 0;
1072}
1073
d115b048 1074static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
f94e0186 1075{
d115b048 1076 struct xhci_input_control_ctx *ctrl_ctx;
f94e0186 1077 struct xhci_ep_ctx *ep_ctx;
d115b048 1078 struct xhci_slot_ctx *slot_ctx;
f94e0186
SS
1079 int i;
1080
1081 /* When a device's add flag and drop flag are zero, any subsequent
1082 * configure endpoint command will leave that endpoint's state
1083 * untouched. Make sure we don't leave any old state in the input
1084 * endpoint contexts.
1085 */
d115b048
JY
1086 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1087 ctrl_ctx->drop_flags = 0;
1088 ctrl_ctx->add_flags = 0;
1089 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1090 slot_ctx->dev_info &= ~LAST_CTX_MASK;
f94e0186 1091 /* Endpoint 0 is always valid */
d115b048 1092 slot_ctx->dev_info |= LAST_CTX(1);
f94e0186 1093 for (i = 1; i < 31; ++i) {
d115b048 1094 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
f94e0186
SS
1095 ep_ctx->ep_info = 0;
1096 ep_ctx->ep_info2 = 0;
8e595a5d 1097 ep_ctx->deq = 0;
f94e0186
SS
1098 ep_ctx->tx_info = 0;
1099 }
1100}
1101
f2217e8e 1102static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
913a8a34 1103 struct usb_device *udev, int *cmd_status)
f2217e8e
SS
1104{
1105 int ret;
1106
913a8a34 1107 switch (*cmd_status) {
f2217e8e
SS
1108 case COMP_ENOMEM:
1109 dev_warn(&udev->dev, "Not enough host controller resources "
1110 "for new device state.\n");
1111 ret = -ENOMEM;
1112 /* FIXME: can we allocate more resources for the HC? */
1113 break;
1114 case COMP_BW_ERR:
1115 dev_warn(&udev->dev, "Not enough bandwidth "
1116 "for new device state.\n");
1117 ret = -ENOSPC;
1118 /* FIXME: can we go back to the old state? */
1119 break;
1120 case COMP_TRB_ERR:
1121 /* the HCD set up something wrong */
1122 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1123 "add flag = 1, "
1124 "and endpoint is not disabled.\n");
1125 ret = -EINVAL;
1126 break;
1127 case COMP_SUCCESS:
1128 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1129 ret = 0;
1130 break;
1131 default:
1132 xhci_err(xhci, "ERROR: unexpected command completion "
913a8a34 1133 "code 0x%x.\n", *cmd_status);
f2217e8e
SS
1134 ret = -EINVAL;
1135 break;
1136 }
1137 return ret;
1138}
1139
1140static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
913a8a34 1141 struct usb_device *udev, int *cmd_status)
f2217e8e
SS
1142{
1143 int ret;
913a8a34 1144 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
f2217e8e 1145
913a8a34 1146 switch (*cmd_status) {
f2217e8e
SS
1147 case COMP_EINVAL:
1148 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1149 "context command.\n");
1150 ret = -EINVAL;
1151 break;
1152 case COMP_EBADSLT:
1153 dev_warn(&udev->dev, "WARN: slot not enabled for"
1154 "evaluate context command.\n");
1155 case COMP_CTX_STATE:
1156 dev_warn(&udev->dev, "WARN: invalid context state for "
1157 "evaluate context command.\n");
1158 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1159 ret = -EINVAL;
1160 break;
1161 case COMP_SUCCESS:
1162 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1163 ret = 0;
1164 break;
1165 default:
1166 xhci_err(xhci, "ERROR: unexpected command completion "
913a8a34 1167 "code 0x%x.\n", *cmd_status);
f2217e8e
SS
1168 ret = -EINVAL;
1169 break;
1170 }
1171 return ret;
1172}
1173
1174/* Issue a configure endpoint command or evaluate context command
1175 * and wait for it to finish.
1176 */
1177static int xhci_configure_endpoint(struct xhci_hcd *xhci,
913a8a34
SS
1178 struct usb_device *udev,
1179 struct xhci_command *command,
1180 bool ctx_change, bool must_succeed)
f2217e8e
SS
1181{
1182 int ret;
1183 int timeleft;
1184 unsigned long flags;
913a8a34
SS
1185 struct xhci_container_ctx *in_ctx;
1186 struct completion *cmd_completion;
1187 int *cmd_status;
1188 struct xhci_virt_device *virt_dev;
f2217e8e
SS
1189
1190 spin_lock_irqsave(&xhci->lock, flags);
913a8a34
SS
1191 virt_dev = xhci->devs[udev->slot_id];
1192 if (command) {
1193 in_ctx = command->in_ctx;
1194 cmd_completion = command->completion;
1195 cmd_status = &command->status;
1196 command->command_trb = xhci->cmd_ring->enqueue;
1197 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1198 } else {
1199 in_ctx = virt_dev->in_ctx;
1200 cmd_completion = &virt_dev->cmd_completion;
1201 cmd_status = &virt_dev->cmd_status;
1202 }
1d68064a 1203 init_completion(cmd_completion);
913a8a34 1204
f2217e8e 1205 if (!ctx_change)
913a8a34
SS
1206 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1207 udev->slot_id, must_succeed);
f2217e8e 1208 else
913a8a34 1209 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
f2217e8e
SS
1210 udev->slot_id);
1211 if (ret < 0) {
c01591bd
SS
1212 if (command)
1213 list_del(&command->cmd_list);
f2217e8e
SS
1214 spin_unlock_irqrestore(&xhci->lock, flags);
1215 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1216 return -ENOMEM;
1217 }
1218 xhci_ring_cmd_db(xhci);
1219 spin_unlock_irqrestore(&xhci->lock, flags);
1220
1221 /* Wait for the configure endpoint command to complete */
1222 timeleft = wait_for_completion_interruptible_timeout(
913a8a34 1223 cmd_completion,
f2217e8e
SS
1224 USB_CTRL_SET_TIMEOUT);
1225 if (timeleft <= 0) {
1226 xhci_warn(xhci, "%s while waiting for %s command\n",
1227 timeleft == 0 ? "Timeout" : "Signal",
1228 ctx_change == 0 ?
1229 "configure endpoint" :
1230 "evaluate context");
1231 /* FIXME cancel the configure endpoint command */
1232 return -ETIME;
1233 }
1234
1235 if (!ctx_change)
913a8a34
SS
1236 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1237 return xhci_evaluate_context_result(xhci, udev, cmd_status);
f2217e8e
SS
1238}
1239
f88ba78d
SS
1240/* Called after one or more calls to xhci_add_endpoint() or
1241 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1242 * to call xhci_reset_bandwidth().
1243 *
1244 * Since we are in the middle of changing either configuration or
1245 * installing a new alt setting, the USB core won't allow URBs to be
1246 * enqueued for any endpoint on the old config or interface. Nothing
1247 * else should be touching the xhci->devs[slot_id] structure, so we
1248 * don't need to take the xhci->lock for manipulating that.
1249 */
f94e0186
SS
1250int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1251{
1252 int i;
1253 int ret = 0;
f94e0186
SS
1254 struct xhci_hcd *xhci;
1255 struct xhci_virt_device *virt_dev;
d115b048
JY
1256 struct xhci_input_control_ctx *ctrl_ctx;
1257 struct xhci_slot_ctx *slot_ctx;
f94e0186
SS
1258
1259 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1260 if (ret <= 0)
1261 return ret;
1262 xhci = hcd_to_xhci(hcd);
1263
f94e0186
SS
1264 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1265 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1266 __func__);
f94e0186
SS
1267 return -EINVAL;
1268 }
700e2052 1269 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
f94e0186
SS
1270 virt_dev = xhci->devs[udev->slot_id];
1271
1272 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
d115b048
JY
1273 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1274 ctrl_ctx->add_flags |= SLOT_FLAG;
1275 ctrl_ctx->add_flags &= ~EP0_FLAG;
1276 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1277 ctrl_ctx->drop_flags &= ~EP0_FLAG;
f94e0186 1278 xhci_dbg(xhci, "New Input Control Context:\n");
d115b048
JY
1279 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1280 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1281 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
f94e0186 1282
913a8a34
SS
1283 ret = xhci_configure_endpoint(xhci, udev, NULL,
1284 false, false);
f94e0186
SS
1285 if (ret) {
1286 /* Callee should call reset_bandwidth() */
f94e0186
SS
1287 return ret;
1288 }
1289
1290 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
d115b048
JY
1291 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1292 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
f94e0186 1293
d115b048 1294 xhci_zero_in_ctx(xhci, virt_dev);
74f9fe21 1295 /* Install new rings and free or cache any old rings */
f94e0186 1296 for (i = 1; i < 31; ++i) {
74f9fe21
SS
1297 if (!virt_dev->eps[i].new_ring)
1298 continue;
1299 /* Only cache or free the old ring if it exists.
1300 * It may not if this is the first add of an endpoint.
1301 */
1302 if (virt_dev->eps[i].ring) {
412566bd 1303 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
f94e0186 1304 }
74f9fe21
SS
1305 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1306 virt_dev->eps[i].new_ring = NULL;
f94e0186
SS
1307 }
1308
f94e0186
SS
1309 return ret;
1310}
1311
1312void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1313{
f94e0186
SS
1314 struct xhci_hcd *xhci;
1315 struct xhci_virt_device *virt_dev;
1316 int i, ret;
1317
1318 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1319 if (ret <= 0)
1320 return;
1321 xhci = hcd_to_xhci(hcd);
1322
f94e0186
SS
1323 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1324 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1325 __func__);
f94e0186
SS
1326 return;
1327 }
700e2052 1328 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
f94e0186
SS
1329 virt_dev = xhci->devs[udev->slot_id];
1330 /* Free any rings allocated for added endpoints */
1331 for (i = 0; i < 31; ++i) {
63a0d9ab
SS
1332 if (virt_dev->eps[i].new_ring) {
1333 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1334 virt_dev->eps[i].new_ring = NULL;
f94e0186
SS
1335 }
1336 }
d115b048 1337 xhci_zero_in_ctx(xhci, virt_dev);
f94e0186
SS
1338}
1339
5270b951 1340static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
913a8a34
SS
1341 struct xhci_container_ctx *in_ctx,
1342 struct xhci_container_ctx *out_ctx,
1343 u32 add_flags, u32 drop_flags)
5270b951
SS
1344{
1345 struct xhci_input_control_ctx *ctrl_ctx;
913a8a34 1346 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
5270b951
SS
1347 ctrl_ctx->add_flags = add_flags;
1348 ctrl_ctx->drop_flags = drop_flags;
913a8a34 1349 xhci_slot_copy(xhci, in_ctx, out_ctx);
5270b951
SS
1350 ctrl_ctx->add_flags |= SLOT_FLAG;
1351
913a8a34
SS
1352 xhci_dbg(xhci, "Input Context:\n");
1353 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
5270b951
SS
1354}
1355
ac9d8fe7
SS
1356void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1357 unsigned int slot_id, unsigned int ep_index,
1358 struct xhci_dequeue_state *deq_state)
1359{
1360 struct xhci_container_ctx *in_ctx;
ac9d8fe7
SS
1361 struct xhci_ep_ctx *ep_ctx;
1362 u32 added_ctxs;
1363 dma_addr_t addr;
1364
913a8a34
SS
1365 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1366 xhci->devs[slot_id]->out_ctx, ep_index);
ac9d8fe7
SS
1367 in_ctx = xhci->devs[slot_id]->in_ctx;
1368 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1369 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1370 deq_state->new_deq_ptr);
1371 if (addr == 0) {
1372 xhci_warn(xhci, "WARN Cannot submit config ep after "
1373 "reset ep command\n");
1374 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1375 deq_state->new_deq_seg,
1376 deq_state->new_deq_ptr);
1377 return;
1378 }
1379 ep_ctx->deq = addr | deq_state->new_cycle_state;
1380
ac9d8fe7 1381 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
913a8a34
SS
1382 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1383 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
ac9d8fe7
SS
1384}
1385
82d1009f 1386void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
63a0d9ab 1387 struct usb_device *udev, unsigned int ep_index)
82d1009f
SS
1388{
1389 struct xhci_dequeue_state deq_state;
63a0d9ab 1390 struct xhci_virt_ep *ep;
82d1009f
SS
1391
1392 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
63a0d9ab 1393 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
82d1009f
SS
1394 /* We need to move the HW's dequeue pointer past this TD,
1395 * or it will attempt to resend it on the next doorbell ring.
1396 */
1397 xhci_find_new_dequeue_state(xhci, udev->slot_id,
e9df17eb 1398 ep_index, ep->stopped_stream, ep->stopped_td,
ac9d8fe7 1399 &deq_state);
82d1009f 1400
ac9d8fe7
SS
1401 /* HW with the reset endpoint quirk will use the saved dequeue state to
1402 * issue a configure endpoint command later.
1403 */
1404 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1405 xhci_dbg(xhci, "Queueing new dequeue state\n");
63a0d9ab 1406 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
e9df17eb 1407 ep_index, ep->stopped_stream, &deq_state);
ac9d8fe7
SS
1408 } else {
1409 /* Better hope no one uses the input context between now and the
1410 * reset endpoint completion!
e9df17eb
SS
1411 * XXX: No idea how this hardware will react when stream rings
1412 * are enabled.
ac9d8fe7
SS
1413 */
1414 xhci_dbg(xhci, "Setting up input context for "
1415 "configure endpoint command\n");
1416 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1417 ep_index, &deq_state);
1418 }
82d1009f
SS
1419}
1420
a1587d97
SS
1421/* Deal with stalled endpoints. The core should have sent the control message
1422 * to clear the halt condition. However, we need to make the xHCI hardware
1423 * reset its sequence number, since a device will expect a sequence number of
1424 * zero after the halt condition is cleared.
1425 * Context: in_interrupt
1426 */
1427void xhci_endpoint_reset(struct usb_hcd *hcd,
1428 struct usb_host_endpoint *ep)
1429{
1430 struct xhci_hcd *xhci;
1431 struct usb_device *udev;
1432 unsigned int ep_index;
1433 unsigned long flags;
1434 int ret;
63a0d9ab 1435 struct xhci_virt_ep *virt_ep;
a1587d97
SS
1436
1437 xhci = hcd_to_xhci(hcd);
1438 udev = (struct usb_device *) ep->hcpriv;
1439 /* Called with a root hub endpoint (or an endpoint that wasn't added
1440 * with xhci_add_endpoint()
1441 */
1442 if (!ep->hcpriv)
1443 return;
1444 ep_index = xhci_get_endpoint_index(&ep->desc);
63a0d9ab
SS
1445 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1446 if (!virt_ep->stopped_td) {
c92bcfa7
SS
1447 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1448 ep->desc.bEndpointAddress);
1449 return;
1450 }
82d1009f
SS
1451 if (usb_endpoint_xfer_control(&ep->desc)) {
1452 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1453 return;
1454 }
a1587d97
SS
1455
1456 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1457 spin_lock_irqsave(&xhci->lock, flags);
1458 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
c92bcfa7
SS
1459 /*
1460 * Can't change the ring dequeue pointer until it's transitioned to the
1461 * stopped state, which is only upon a successful reset endpoint
1462 * command. Better hope that last command worked!
1463 */
a1587d97 1464 if (!ret) {
63a0d9ab
SS
1465 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1466 kfree(virt_ep->stopped_td);
a1587d97
SS
1467 xhci_ring_cmd_db(xhci);
1468 }
1624ae1c
SS
1469 virt_ep->stopped_td = NULL;
1470 virt_ep->stopped_trb = NULL;
5e5cf6fc 1471 virt_ep->stopped_stream = 0;
a1587d97
SS
1472 spin_unlock_irqrestore(&xhci->lock, flags);
1473
1474 if (ret)
1475 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1476}
1477
8df75f42
SS
1478static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1479 struct usb_device *udev, struct usb_host_endpoint *ep,
1480 unsigned int slot_id)
1481{
1482 int ret;
1483 unsigned int ep_index;
1484 unsigned int ep_state;
1485
1486 if (!ep)
1487 return -EINVAL;
1488 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__);
1489 if (ret <= 0)
1490 return -EINVAL;
842f1690 1491 if (ep->ss_ep_comp.bmAttributes == 0) {
8df75f42
SS
1492 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1493 " descriptor for ep 0x%x does not support streams\n",
1494 ep->desc.bEndpointAddress);
1495 return -EINVAL;
1496 }
1497
1498 ep_index = xhci_get_endpoint_index(&ep->desc);
1499 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1500 if (ep_state & EP_HAS_STREAMS ||
1501 ep_state & EP_GETTING_STREAMS) {
1502 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1503 "already has streams set up.\n",
1504 ep->desc.bEndpointAddress);
1505 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1506 "dynamic stream context array reallocation.\n");
1507 return -EINVAL;
1508 }
1509 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1510 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1511 "endpoint 0x%x; URBs are pending.\n",
1512 ep->desc.bEndpointAddress);
1513 return -EINVAL;
1514 }
1515 return 0;
1516}
1517
1518static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1519 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1520{
1521 unsigned int max_streams;
1522
1523 /* The stream context array size must be a power of two */
1524 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1525 /*
1526 * Find out how many primary stream array entries the host controller
1527 * supports. Later we may use secondary stream arrays (similar to 2nd
1528 * level page entries), but that's an optional feature for xHCI host
1529 * controllers. xHCs must support at least 4 stream IDs.
1530 */
1531 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1532 if (*num_stream_ctxs > max_streams) {
1533 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1534 max_streams);
1535 *num_stream_ctxs = max_streams;
1536 *num_streams = max_streams;
1537 }
1538}
1539
1540/* Returns an error code if one of the endpoint already has streams.
1541 * This does not change any data structures, it only checks and gathers
1542 * information.
1543 */
1544static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1545 struct usb_device *udev,
1546 struct usb_host_endpoint **eps, unsigned int num_eps,
1547 unsigned int *num_streams, u32 *changed_ep_bitmask)
1548{
8df75f42
SS
1549 unsigned int max_streams;
1550 unsigned int endpoint_flag;
1551 int i;
1552 int ret;
1553
1554 for (i = 0; i < num_eps; i++) {
1555 ret = xhci_check_streams_endpoint(xhci, udev,
1556 eps[i], udev->slot_id);
1557 if (ret < 0)
1558 return ret;
1559
842f1690
AS
1560 max_streams = USB_SS_MAX_STREAMS(
1561 eps[i]->ss_ep_comp.bmAttributes);
8df75f42
SS
1562 if (max_streams < (*num_streams - 1)) {
1563 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1564 eps[i]->desc.bEndpointAddress,
1565 max_streams);
1566 *num_streams = max_streams+1;
1567 }
1568
1569 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1570 if (*changed_ep_bitmask & endpoint_flag)
1571 return -EINVAL;
1572 *changed_ep_bitmask |= endpoint_flag;
1573 }
1574 return 0;
1575}
1576
1577static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1578 struct usb_device *udev,
1579 struct usb_host_endpoint **eps, unsigned int num_eps)
1580{
1581 u32 changed_ep_bitmask = 0;
1582 unsigned int slot_id;
1583 unsigned int ep_index;
1584 unsigned int ep_state;
1585 int i;
1586
1587 slot_id = udev->slot_id;
1588 if (!xhci->devs[slot_id])
1589 return 0;
1590
1591 for (i = 0; i < num_eps; i++) {
1592 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1593 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1594 /* Are streams already being freed for the endpoint? */
1595 if (ep_state & EP_GETTING_NO_STREAMS) {
1596 xhci_warn(xhci, "WARN Can't disable streams for "
1597 "endpoint 0x%x\n, "
1598 "streams are being disabled already.",
1599 eps[i]->desc.bEndpointAddress);
1600 return 0;
1601 }
1602 /* Are there actually any streams to free? */
1603 if (!(ep_state & EP_HAS_STREAMS) &&
1604 !(ep_state & EP_GETTING_STREAMS)) {
1605 xhci_warn(xhci, "WARN Can't disable streams for "
1606 "endpoint 0x%x\n, "
1607 "streams are already disabled!",
1608 eps[i]->desc.bEndpointAddress);
1609 xhci_warn(xhci, "WARN xhci_free_streams() called "
1610 "with non-streams endpoint\n");
1611 return 0;
1612 }
1613 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1614 }
1615 return changed_ep_bitmask;
1616}
1617
1618/*
1619 * The USB device drivers use this function (though the HCD interface in USB
1620 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1621 * coordinate mass storage command queueing across multiple endpoints (basically
1622 * a stream ID == a task ID).
1623 *
1624 * Setting up streams involves allocating the same size stream context array
1625 * for each endpoint and issuing a configure endpoint command for all endpoints.
1626 *
1627 * Don't allow the call to succeed if one endpoint only supports one stream
1628 * (which means it doesn't support streams at all).
1629 *
1630 * Drivers may get less stream IDs than they asked for, if the host controller
1631 * hardware or endpoints claim they can't support the number of requested
1632 * stream IDs.
1633 */
1634int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1635 struct usb_host_endpoint **eps, unsigned int num_eps,
1636 unsigned int num_streams, gfp_t mem_flags)
1637{
1638 int i, ret;
1639 struct xhci_hcd *xhci;
1640 struct xhci_virt_device *vdev;
1641 struct xhci_command *config_cmd;
1642 unsigned int ep_index;
1643 unsigned int num_stream_ctxs;
1644 unsigned long flags;
1645 u32 changed_ep_bitmask = 0;
1646
1647 if (!eps)
1648 return -EINVAL;
1649
1650 /* Add one to the number of streams requested to account for
1651 * stream 0 that is reserved for xHCI usage.
1652 */
1653 num_streams += 1;
1654 xhci = hcd_to_xhci(hcd);
1655 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
1656 num_streams);
1657
1658 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
1659 if (!config_cmd) {
1660 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1661 return -ENOMEM;
1662 }
1663
1664 /* Check to make sure all endpoints are not already configured for
1665 * streams. While we're at it, find the maximum number of streams that
1666 * all the endpoints will support and check for duplicate endpoints.
1667 */
1668 spin_lock_irqsave(&xhci->lock, flags);
1669 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
1670 num_eps, &num_streams, &changed_ep_bitmask);
1671 if (ret < 0) {
1672 xhci_free_command(xhci, config_cmd);
1673 spin_unlock_irqrestore(&xhci->lock, flags);
1674 return ret;
1675 }
1676 if (num_streams <= 1) {
1677 xhci_warn(xhci, "WARN: endpoints can't handle "
1678 "more than one stream.\n");
1679 xhci_free_command(xhci, config_cmd);
1680 spin_unlock_irqrestore(&xhci->lock, flags);
1681 return -EINVAL;
1682 }
1683 vdev = xhci->devs[udev->slot_id];
1684 /* Mark each endpoint as being in transistion, so
1685 * xhci_urb_enqueue() will reject all URBs.
1686 */
1687 for (i = 0; i < num_eps; i++) {
1688 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1689 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
1690 }
1691 spin_unlock_irqrestore(&xhci->lock, flags);
1692
1693 /* Setup internal data structures and allocate HW data structures for
1694 * streams (but don't install the HW structures in the input context
1695 * until we're sure all memory allocation succeeded).
1696 */
1697 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
1698 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
1699 num_stream_ctxs, num_streams);
1700
1701 for (i = 0; i < num_eps; i++) {
1702 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1703 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
1704 num_stream_ctxs,
1705 num_streams, mem_flags);
1706 if (!vdev->eps[ep_index].stream_info)
1707 goto cleanup;
1708 /* Set maxPstreams in endpoint context and update deq ptr to
1709 * point to stream context array. FIXME
1710 */
1711 }
1712
1713 /* Set up the input context for a configure endpoint command. */
1714 for (i = 0; i < num_eps; i++) {
1715 struct xhci_ep_ctx *ep_ctx;
1716
1717 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1718 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
1719
1720 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
1721 vdev->out_ctx, ep_index);
1722 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
1723 vdev->eps[ep_index].stream_info);
1724 }
1725 /* Tell the HW to drop its old copy of the endpoint context info
1726 * and add the updated copy from the input context.
1727 */
1728 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
1729 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1730
1731 /* Issue and wait for the configure endpoint command */
1732 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
1733 false, false);
1734
1735 /* xHC rejected the configure endpoint command for some reason, so we
1736 * leave the old ring intact and free our internal streams data
1737 * structure.
1738 */
1739 if (ret < 0)
1740 goto cleanup;
1741
1742 spin_lock_irqsave(&xhci->lock, flags);
1743 for (i = 0; i < num_eps; i++) {
1744 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1745 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1746 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
1747 udev->slot_id, ep_index);
1748 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
1749 }
1750 xhci_free_command(xhci, config_cmd);
1751 spin_unlock_irqrestore(&xhci->lock, flags);
1752
1753 /* Subtract 1 for stream 0, which drivers can't use */
1754 return num_streams - 1;
1755
1756cleanup:
1757 /* If it didn't work, free the streams! */
1758 for (i = 0; i < num_eps; i++) {
1759 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1760 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
8a007748 1761 vdev->eps[ep_index].stream_info = NULL;
8df75f42
SS
1762 /* FIXME Unset maxPstreams in endpoint context and
1763 * update deq ptr to point to normal string ring.
1764 */
1765 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1766 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1767 xhci_endpoint_zero(xhci, vdev, eps[i]);
1768 }
1769 xhci_free_command(xhci, config_cmd);
1770 return -ENOMEM;
1771}
1772
1773/* Transition the endpoint from using streams to being a "normal" endpoint
1774 * without streams.
1775 *
1776 * Modify the endpoint context state, submit a configure endpoint command,
1777 * and free all endpoint rings for streams if that completes successfully.
1778 */
1779int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1780 struct usb_host_endpoint **eps, unsigned int num_eps,
1781 gfp_t mem_flags)
1782{
1783 int i, ret;
1784 struct xhci_hcd *xhci;
1785 struct xhci_virt_device *vdev;
1786 struct xhci_command *command;
1787 unsigned int ep_index;
1788 unsigned long flags;
1789 u32 changed_ep_bitmask;
1790
1791 xhci = hcd_to_xhci(hcd);
1792 vdev = xhci->devs[udev->slot_id];
1793
1794 /* Set up a configure endpoint command to remove the streams rings */
1795 spin_lock_irqsave(&xhci->lock, flags);
1796 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
1797 udev, eps, num_eps);
1798 if (changed_ep_bitmask == 0) {
1799 spin_unlock_irqrestore(&xhci->lock, flags);
1800 return -EINVAL;
1801 }
1802
1803 /* Use the xhci_command structure from the first endpoint. We may have
1804 * allocated too many, but the driver may call xhci_free_streams() for
1805 * each endpoint it grouped into one call to xhci_alloc_streams().
1806 */
1807 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
1808 command = vdev->eps[ep_index].stream_info->free_streams_command;
1809 for (i = 0; i < num_eps; i++) {
1810 struct xhci_ep_ctx *ep_ctx;
1811
1812 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1813 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
1814 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
1815 EP_GETTING_NO_STREAMS;
1816
1817 xhci_endpoint_copy(xhci, command->in_ctx,
1818 vdev->out_ctx, ep_index);
1819 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
1820 &vdev->eps[ep_index]);
1821 }
1822 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
1823 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1824 spin_unlock_irqrestore(&xhci->lock, flags);
1825
1826 /* Issue and wait for the configure endpoint command,
1827 * which must succeed.
1828 */
1829 ret = xhci_configure_endpoint(xhci, udev, command,
1830 false, true);
1831
1832 /* xHC rejected the configure endpoint command for some reason, so we
1833 * leave the streams rings intact.
1834 */
1835 if (ret < 0)
1836 return ret;
1837
1838 spin_lock_irqsave(&xhci->lock, flags);
1839 for (i = 0; i < num_eps; i++) {
1840 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1841 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
8a007748 1842 vdev->eps[ep_index].stream_info = NULL;
8df75f42
SS
1843 /* FIXME Unset maxPstreams in endpoint context and
1844 * update deq ptr to point to normal string ring.
1845 */
1846 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
1847 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1848 }
1849 spin_unlock_irqrestore(&xhci->lock, flags);
1850
1851 return 0;
1852}
1853
2a8f82c4
SS
1854/*
1855 * This submits a Reset Device Command, which will set the device state to 0,
1856 * set the device address to 0, and disable all the endpoints except the default
1857 * control endpoint. The USB core should come back and call
1858 * xhci_address_device(), and then re-set up the configuration. If this is
1859 * called because of a usb_reset_and_verify_device(), then the old alternate
1860 * settings will be re-installed through the normal bandwidth allocation
1861 * functions.
1862 *
1863 * Wait for the Reset Device command to finish. Remove all structures
1864 * associated with the endpoints that were disabled. Clear the input device
1865 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
1866 */
1867int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
1868{
1869 int ret, i;
1870 unsigned long flags;
1871 struct xhci_hcd *xhci;
1872 unsigned int slot_id;
1873 struct xhci_virt_device *virt_dev;
1874 struct xhci_command *reset_device_cmd;
1875 int timeleft;
1876 int last_freed_endpoint;
1877
1878 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1879 if (ret <= 0)
1880 return ret;
1881 xhci = hcd_to_xhci(hcd);
1882 slot_id = udev->slot_id;
1883 virt_dev = xhci->devs[slot_id];
1884 if (!virt_dev) {
1885 xhci_dbg(xhci, "%s called with invalid slot ID %u\n",
1886 __func__, slot_id);
1887 return -EINVAL;
1888 }
1889
1890 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
1891 /* Allocate the command structure that holds the struct completion.
1892 * Assume we're in process context, since the normal device reset
1893 * process has to wait for the device anyway. Storage devices are
1894 * reset as part of error handling, so use GFP_NOIO instead of
1895 * GFP_KERNEL.
1896 */
1897 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
1898 if (!reset_device_cmd) {
1899 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
1900 return -ENOMEM;
1901 }
1902
1903 /* Attempt to submit the Reset Device command to the command ring */
1904 spin_lock_irqsave(&xhci->lock, flags);
1905 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
1906 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
1907 ret = xhci_queue_reset_device(xhci, slot_id);
1908 if (ret) {
1909 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1910 list_del(&reset_device_cmd->cmd_list);
1911 spin_unlock_irqrestore(&xhci->lock, flags);
1912 goto command_cleanup;
1913 }
1914 xhci_ring_cmd_db(xhci);
1915 spin_unlock_irqrestore(&xhci->lock, flags);
1916
1917 /* Wait for the Reset Device command to finish */
1918 timeleft = wait_for_completion_interruptible_timeout(
1919 reset_device_cmd->completion,
1920 USB_CTRL_SET_TIMEOUT);
1921 if (timeleft <= 0) {
1922 xhci_warn(xhci, "%s while waiting for reset device command\n",
1923 timeleft == 0 ? "Timeout" : "Signal");
1924 spin_lock_irqsave(&xhci->lock, flags);
1925 /* The timeout might have raced with the event ring handler, so
1926 * only delete from the list if the item isn't poisoned.
1927 */
1928 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
1929 list_del(&reset_device_cmd->cmd_list);
1930 spin_unlock_irqrestore(&xhci->lock, flags);
1931 ret = -ETIME;
1932 goto command_cleanup;
1933 }
1934
1935 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
1936 * unless we tried to reset a slot ID that wasn't enabled,
1937 * or the device wasn't in the addressed or configured state.
1938 */
1939 ret = reset_device_cmd->status;
1940 switch (ret) {
1941 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
1942 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
1943 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
1944 slot_id,
1945 xhci_get_slot_state(xhci, virt_dev->out_ctx));
1946 xhci_info(xhci, "Not freeing device rings.\n");
1947 /* Don't treat this as an error. May change my mind later. */
1948 ret = 0;
1949 goto command_cleanup;
1950 case COMP_SUCCESS:
1951 xhci_dbg(xhci, "Successful reset device command.\n");
1952 break;
1953 default:
1954 if (xhci_is_vendor_info_code(xhci, ret))
1955 break;
1956 xhci_warn(xhci, "Unknown completion code %u for "
1957 "reset device command.\n", ret);
1958 ret = -EINVAL;
1959 goto command_cleanup;
1960 }
1961
1962 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
1963 last_freed_endpoint = 1;
1964 for (i = 1; i < 31; ++i) {
1965 if (!virt_dev->eps[i].ring)
1966 continue;
1967 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
1968 last_freed_endpoint = i;
1969 }
1970 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
1971 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
1972 ret = 0;
1973
1974command_cleanup:
1975 xhci_free_command(xhci, reset_device_cmd);
1976 return ret;
1977}
1978
3ffbba95
SS
1979/*
1980 * At this point, the struct usb_device is about to go away, the device has
1981 * disconnected, and all traffic has been stopped and the endpoints have been
1982 * disabled. Free any HC data structures associated with that device.
1983 */
1984void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1985{
1986 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
6f5165cf 1987 struct xhci_virt_device *virt_dev;
3ffbba95 1988 unsigned long flags;
c526d0d4 1989 u32 state;
6f5165cf 1990 int i;
3ffbba95
SS
1991
1992 if (udev->slot_id == 0)
1993 return;
6f5165cf
SS
1994 virt_dev = xhci->devs[udev->slot_id];
1995 if (!virt_dev)
1996 return;
1997
1998 /* Stop any wayward timer functions (which may grab the lock) */
1999 for (i = 0; i < 31; ++i) {
2000 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2001 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2002 }
3ffbba95
SS
2003
2004 spin_lock_irqsave(&xhci->lock, flags);
c526d0d4
SS
2005 /* Don't disable the slot if the host controller is dead. */
2006 state = xhci_readl(xhci, &xhci->op_regs->status);
6f5165cf 2007 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
c526d0d4
SS
2008 xhci_free_virt_device(xhci, udev->slot_id);
2009 spin_unlock_irqrestore(&xhci->lock, flags);
2010 return;
2011 }
2012
23e3be11 2013 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
3ffbba95
SS
2014 spin_unlock_irqrestore(&xhci->lock, flags);
2015 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2016 return;
2017 }
23e3be11 2018 xhci_ring_cmd_db(xhci);
3ffbba95
SS
2019 spin_unlock_irqrestore(&xhci->lock, flags);
2020 /*
2021 * Event command completion handler will free any data structures
f88ba78d 2022 * associated with the slot. XXX Can free sleep?
3ffbba95
SS
2023 */
2024}
2025
2026/*
2027 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2028 * timed out, or allocating memory failed. Returns 1 on success.
2029 */
2030int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2031{
2032 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2033 unsigned long flags;
2034 int timeleft;
2035 int ret;
2036
2037 spin_lock_irqsave(&xhci->lock, flags);
23e3be11 2038 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
3ffbba95
SS
2039 if (ret) {
2040 spin_unlock_irqrestore(&xhci->lock, flags);
2041 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2042 return 0;
2043 }
23e3be11 2044 xhci_ring_cmd_db(xhci);
3ffbba95
SS
2045 spin_unlock_irqrestore(&xhci->lock, flags);
2046
2047 /* XXX: how much time for xHC slot assignment? */
2048 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2049 USB_CTRL_SET_TIMEOUT);
2050 if (timeleft <= 0) {
2051 xhci_warn(xhci, "%s while waiting for a slot\n",
2052 timeleft == 0 ? "Timeout" : "Signal");
2053 /* FIXME cancel the enable slot request */
2054 return 0;
2055 }
2056
3ffbba95
SS
2057 if (!xhci->slot_id) {
2058 xhci_err(xhci, "Error while assigning device slot ID\n");
3ffbba95
SS
2059 return 0;
2060 }
f88ba78d 2061 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
3ffbba95
SS
2062 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
2063 /* Disable slot, if we can do it without mem alloc */
2064 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
f88ba78d 2065 spin_lock_irqsave(&xhci->lock, flags);
23e3be11
SS
2066 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2067 xhci_ring_cmd_db(xhci);
3ffbba95
SS
2068 spin_unlock_irqrestore(&xhci->lock, flags);
2069 return 0;
2070 }
2071 udev->slot_id = xhci->slot_id;
2072 /* Is this a LS or FS device under a HS hub? */
2073 /* Hub or peripherial? */
3ffbba95
SS
2074 return 1;
2075}
2076
2077/*
2078 * Issue an Address Device command (which will issue a SetAddress request to
2079 * the device).
2080 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2081 * we should only issue and wait on one address command at the same time.
2082 *
2083 * We add one to the device address issued by the hardware because the USB core
2084 * uses address 1 for the root hubs (even though they're not really devices).
2085 */
2086int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2087{
2088 unsigned long flags;
2089 int timeleft;
2090 struct xhci_virt_device *virt_dev;
2091 int ret = 0;
2092 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
d115b048
JY
2093 struct xhci_slot_ctx *slot_ctx;
2094 struct xhci_input_control_ctx *ctrl_ctx;
8e595a5d 2095 u64 temp_64;
3ffbba95
SS
2096
2097 if (!udev->slot_id) {
2098 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2099 return -EINVAL;
2100 }
2101
3ffbba95
SS
2102 virt_dev = xhci->devs[udev->slot_id];
2103
2104 /* If this is a Set Address to an unconfigured device, setup ep 0 */
2105 if (!udev->config)
2106 xhci_setup_addressable_virt_dev(xhci, udev);
2107 /* Otherwise, assume the core has the device configured how it wants */
66e49d87 2108 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
d115b048 2109 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3ffbba95 2110
f88ba78d 2111 spin_lock_irqsave(&xhci->lock, flags);
d115b048
JY
2112 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2113 udev->slot_id);
3ffbba95
SS
2114 if (ret) {
2115 spin_unlock_irqrestore(&xhci->lock, flags);
2116 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2117 return ret;
2118 }
23e3be11 2119 xhci_ring_cmd_db(xhci);
3ffbba95
SS
2120 spin_unlock_irqrestore(&xhci->lock, flags);
2121
2122 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2123 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2124 USB_CTRL_SET_TIMEOUT);
2125 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2126 * the SetAddress() "recovery interval" required by USB and aborting the
2127 * command on a timeout.
2128 */
2129 if (timeleft <= 0) {
2130 xhci_warn(xhci, "%s while waiting for a slot\n",
2131 timeleft == 0 ? "Timeout" : "Signal");
2132 /* FIXME cancel the address device command */
2133 return -ETIME;
2134 }
2135
3ffbba95
SS
2136 switch (virt_dev->cmd_status) {
2137 case COMP_CTX_STATE:
2138 case COMP_EBADSLT:
2139 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2140 udev->slot_id);
2141 ret = -EINVAL;
2142 break;
2143 case COMP_TX_ERR:
2144 dev_warn(&udev->dev, "Device not responding to set address.\n");
2145 ret = -EPROTO;
2146 break;
2147 case COMP_SUCCESS:
2148 xhci_dbg(xhci, "Successful Address Device command\n");
2149 break;
2150 default:
2151 xhci_err(xhci, "ERROR: unexpected command completion "
2152 "code 0x%x.\n", virt_dev->cmd_status);
66e49d87 2153 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
d115b048 2154 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3ffbba95
SS
2155 ret = -EINVAL;
2156 break;
2157 }
2158 if (ret) {
3ffbba95
SS
2159 return ret;
2160 }
8e595a5d
SS
2161 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2162 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2163 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
3ffbba95 2164 udev->slot_id,
8e595a5d
SS
2165 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2166 (unsigned long long)
2167 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
700e2052 2168 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
d115b048 2169 (unsigned long long)virt_dev->out_ctx->dma);
3ffbba95 2170 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
d115b048 2171 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3ffbba95 2172 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
d115b048 2173 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3ffbba95
SS
2174 /*
2175 * USB core uses address 1 for the roothubs, so we add one to the
2176 * address given back to us by the HC.
2177 */
d115b048
JY
2178 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
2179 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
f94e0186 2180 /* Zero the input context control for later use */
d115b048
JY
2181 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2182 ctrl_ctx->add_flags = 0;
2183 ctrl_ctx->drop_flags = 0;
3ffbba95
SS
2184
2185 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
2186 /* XXX Meh, not sure if anyone else but choose_address uses this. */
2187 set_bit(udev->devnum, udev->bus->devmap.devicemap);
2188
2189 return 0;
2190}
2191
ac1c1b7f
SS
2192/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2193 * internal data structures for the device.
2194 */
2195int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2196 struct usb_tt *tt, gfp_t mem_flags)
2197{
2198 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2199 struct xhci_virt_device *vdev;
2200 struct xhci_command *config_cmd;
2201 struct xhci_input_control_ctx *ctrl_ctx;
2202 struct xhci_slot_ctx *slot_ctx;
2203 unsigned long flags;
2204 unsigned think_time;
2205 int ret;
2206
2207 /* Ignore root hubs */
2208 if (!hdev->parent)
2209 return 0;
2210
2211 vdev = xhci->devs[hdev->slot_id];
2212 if (!vdev) {
2213 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2214 return -EINVAL;
2215 }
a1d78c16 2216 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
ac1c1b7f
SS
2217 if (!config_cmd) {
2218 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2219 return -ENOMEM;
2220 }
2221
2222 spin_lock_irqsave(&xhci->lock, flags);
2223 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2224 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2225 ctrl_ctx->add_flags |= SLOT_FLAG;
2226 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2227 slot_ctx->dev_info |= DEV_HUB;
2228 if (tt->multi)
2229 slot_ctx->dev_info |= DEV_MTT;
2230 if (xhci->hci_version > 0x95) {
2231 xhci_dbg(xhci, "xHCI version %x needs hub "
2232 "TT think time and number of ports\n",
2233 (unsigned int) xhci->hci_version);
2234 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2235 /* Set TT think time - convert from ns to FS bit times.
2236 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2237 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2238 */
2239 think_time = tt->think_time;
2240 if (think_time != 0)
2241 think_time = (think_time / 666) - 1;
2242 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2243 } else {
2244 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2245 "TT think time or number of ports\n",
2246 (unsigned int) xhci->hci_version);
2247 }
2248 slot_ctx->dev_state = 0;
2249 spin_unlock_irqrestore(&xhci->lock, flags);
2250
2251 xhci_dbg(xhci, "Set up %s for hub device.\n",
2252 (xhci->hci_version > 0x95) ?
2253 "configure endpoint" : "evaluate context");
2254 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2255 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2256
2257 /* Issue and wait for the configure endpoint or
2258 * evaluate context command.
2259 */
2260 if (xhci->hci_version > 0x95)
2261 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2262 false, false);
2263 else
2264 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2265 true, false);
2266
2267 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2268 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2269
2270 xhci_free_command(xhci, config_cmd);
2271 return ret;
2272}
2273
66d4eadd
SS
2274int xhci_get_frame(struct usb_hcd *hcd)
2275{
2276 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2277 /* EHCI mods by the periodic size. Why? */
2278 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2279}
2280
2281MODULE_DESCRIPTION(DRIVER_DESC);
2282MODULE_AUTHOR(DRIVER_AUTHOR);
2283MODULE_LICENSE("GPL");
2284
2285static int __init xhci_hcd_init(void)
2286{
2287#ifdef CONFIG_PCI
2288 int retval = 0;
2289
2290 retval = xhci_register_pci();
2291
2292 if (retval < 0) {
2293 printk(KERN_DEBUG "Problem registering PCI driver.");
2294 return retval;
2295 }
2296#endif
98441973
SS
2297 /*
2298 * Check the compiler generated sizes of structures that must be laid
2299 * out in specific ways for hardware access.
2300 */
2301 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2302 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2303 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2304 /* xhci_device_control has eight fields, and also
2305 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2306 */
98441973
SS
2307 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2308 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2309 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2310 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2311 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2312 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2313 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2314 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
66d4eadd
SS
2315 return 0;
2316}
2317module_init(xhci_hcd_init);
2318
2319static void __exit xhci_hcd_cleanup(void)
2320{
2321#ifdef CONFIG_PCI
2322 xhci_unregister_pci();
2323#endif
2324}
2325module_exit(xhci_hcd_cleanup);