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