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