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