blob: e23b5188a7956f7e8bc397787d2c12ccd28c6a82 [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Dong Nguyen43b86af2010-07-21 16:56:08 -070023#include <linux/pci.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070024#include <linux/irq.h>
Sarah Sharp8df75f42010-04-02 15:34:16 -070025#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070026#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070027#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -050029#include <linux/dmi.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070030
31#include "xhci.h"
32
33#define DRIVER_AUTHOR "Sarah Sharp"
34#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
35
Sarah Sharpb0567b32009-08-07 14:04:36 -070036/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
37static int link_quirk;
38module_param(link_quirk, int, S_IRUGO | S_IWUSR);
39MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
40
Sarah Sharp66d4ead2009-04-27 19:52:28 -070041/* TODO: copied from ehci-hcd.c - can this be refactored? */
42/*
43 * handshake - spin reading hc until handshake completes or fails
44 * @ptr: address of hc register to be read
45 * @mask: bits to look at in result of read
46 * @done: value of those bits when handshake succeeds
47 * @usec: timeout in microseconds
48 *
49 * Returns negative errno, or zero on success
50 *
51 * Success happens when the "mask" bits have the specified value (hardware
52 * handshake done). There are two failure modes: "usec" have passed (major
53 * hardware flakeout), or the register reads as all-ones (hardware removed).
54 */
Elric Fu28182472012-06-27 16:31:12 +080055int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
Sarah Sharp66d4ead2009-04-27 19:52:28 -070056 u32 mask, u32 done, int usec)
57{
58 u32 result;
59
60 do {
61 result = xhci_readl(xhci, ptr);
62 if (result == ~(u32)0) /* card removed */
63 return -ENODEV;
64 result &= mask;
65 if (result == done)
66 return 0;
67 udelay(1);
68 usec--;
69 } while (usec > 0);
70 return -ETIMEDOUT;
71}
72
73/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070074 * Disable interrupts and begin the xHCI halting process.
75 */
76void xhci_quiesce(struct xhci_hcd *xhci)
77{
78 u32 halted;
79 u32 cmd;
80 u32 mask;
81
82 mask = ~(XHCI_IRQS);
83 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
84 if (!halted)
85 mask &= ~CMD_RUN;
86
87 cmd = xhci_readl(xhci, &xhci->op_regs->command);
88 cmd &= mask;
89 xhci_writel(xhci, cmd, &xhci->op_regs->command);
90}
91
92/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070093 * Force HC into halt state.
94 *
95 * Disable any IRQs and clear the run/stop bit.
96 * HC will complete any current and actively pipelined transactions, and
Andiry Xubdfca502011-01-06 15:43:39 +080097 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070098 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070099 */
100int xhci_halt(struct xhci_hcd *xhci)
101{
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800102 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700103 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700104 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700105
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800106 ret = handshake(xhci, &xhci->op_regs->status,
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700107 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
Elric Fu1976fff2012-06-27 16:30:57 +0800108 if (!ret) {
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800109 xhci->xhc_state |= XHCI_STATE_HALTED;
Elric Fu1976fff2012-06-27 16:30:57 +0800110 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
111 } else
Sarah Sharp5af98bb2012-03-16 12:58:20 -0700112 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
113 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800114 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700115}
116
117/*
Sarah Sharped074532010-05-24 13:25:21 -0700118 * Set the run bit and wait for the host to be running.
119 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800120static int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700121{
122 u32 temp;
123 int ret;
124
125 temp = xhci_readl(xhci, &xhci->op_regs->command);
126 temp |= (CMD_RUN);
127 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
128 temp);
129 xhci_writel(xhci, temp, &xhci->op_regs->command);
130
131 /*
132 * Wait for the HCHalted Status bit to be 0 to indicate the host is
133 * running.
134 */
135 ret = handshake(xhci, &xhci->op_regs->status,
136 STS_HALT, 0, XHCI_MAX_HALT_USEC);
137 if (ret == -ETIMEDOUT)
138 xhci_err(xhci, "Host took too long to start, "
139 "waited %u microseconds.\n",
140 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800141 if (!ret)
142 xhci->xhc_state &= ~XHCI_STATE_HALTED;
Sarah Sharped074532010-05-24 13:25:21 -0700143 return ret;
144}
145
146/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800147 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700148 *
149 * This resets pipelines, timers, counters, state machines, etc.
150 * Transactions will be terminated immediately, and operational registers
151 * will be set to their defaults.
152 */
153int xhci_reset(struct xhci_hcd *xhci)
154{
155 u32 command;
156 u32 state;
Andiry Xu296b8ce2012-04-14 02:54:30 +0800157 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700158
159 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700160 if ((state & STS_HALT) == 0) {
161 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
162 return 0;
163 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700164
165 xhci_dbg(xhci, "// Reset the HC\n");
166 command = xhci_readl(xhci, &xhci->op_regs->command);
167 command |= CMD_RESET;
168 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700169
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700170 ret = handshake(xhci, &xhci->op_regs->command,
Sarah Sharpebd311e2012-07-23 16:06:08 -0700171 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700172 if (ret)
173 return ret;
174
175 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
176 /*
177 * xHCI cannot write to any doorbells or operational registers other
178 * than status until the "Controller Not Ready" flag is cleared.
179 */
Sarah Sharpebd311e2012-07-23 16:06:08 -0700180 ret = handshake(xhci, &xhci->op_regs->status,
181 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xu296b8ce2012-04-14 02:54:30 +0800182
183 for (i = 0; i < 2; ++i) {
184 xhci->bus_state[i].port_c_suspend = 0;
185 xhci->bus_state[i].suspended_ports = 0;
186 xhci->bus_state[i].resuming_ports = 0;
187 }
188
189 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700190}
191
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700192#ifdef CONFIG_PCI
193static int xhci_free_msi(struct xhci_hcd *xhci)
Dong Nguyen43b86af2010-07-21 16:56:08 -0700194{
195 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700196
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700197 if (!xhci->msix_entries)
198 return -EINVAL;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700199
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700200 for (i = 0; i < xhci->msix_count; i++)
201 if (xhci->msix_entries[i].vector)
202 free_irq(xhci->msix_entries[i].vector,
203 xhci_to_hcd(xhci));
204 return 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700205}
206
207/*
208 * Set up MSI
209 */
210static int xhci_setup_msi(struct xhci_hcd *xhci)
211{
212 int ret;
213 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
214
215 ret = pci_enable_msi(pdev);
216 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800217 xhci_dbg(xhci, "failed to allocate MSI entry\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700218 return ret;
219 }
220
221 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
222 0, "xhci_hcd", xhci_to_hcd(xhci));
223 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800224 xhci_dbg(xhci, "disable MSI interrupt\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700225 pci_disable_msi(pdev);
226 }
227
228 return ret;
229}
230
231/*
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700232 * Free IRQs
233 * free all IRQs request
234 */
235static void xhci_free_irq(struct xhci_hcd *xhci)
236{
237 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
238 int ret;
239
240 /* return if using legacy interrupt */
Felipe Balbicd704692012-02-29 16:46:23 +0200241 if (xhci_to_hcd(xhci)->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700242 return;
243
244 ret = xhci_free_msi(xhci);
245 if (!ret)
246 return;
Felipe Balbicd704692012-02-29 16:46:23 +0200247 if (pdev->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700248 free_irq(pdev->irq, xhci_to_hcd(xhci));
249
250 return;
251}
252
253/*
Dong Nguyen43b86af2010-07-21 16:56:08 -0700254 * Set up MSI-X
255 */
256static int xhci_setup_msix(struct xhci_hcd *xhci)
257{
258 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800259 struct usb_hcd *hcd = xhci_to_hcd(xhci);
260 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700261
262 /*
263 * calculate number of msi-x vectors supported.
264 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
265 * with max number of interrupters based on the xhci HCSPARAMS1.
266 * - num_online_cpus: maximum msi-x vectors per CPUs core.
267 * Add additional 1 vector to ensure always available interrupt.
268 */
269 xhci->msix_count = min(num_online_cpus() + 1,
270 HCS_MAX_INTRS(xhci->hcs_params1));
271
272 xhci->msix_entries =
273 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800274 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700275 if (!xhci->msix_entries) {
276 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
277 return -ENOMEM;
278 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700279
280 for (i = 0; i < xhci->msix_count; i++) {
281 xhci->msix_entries[i].entry = i;
282 xhci->msix_entries[i].vector = 0;
283 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700284
285 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
286 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800287 xhci_dbg(xhci, "Failed to enable MSI-X\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700288 goto free_entries;
289 }
290
Dong Nguyen43b86af2010-07-21 16:56:08 -0700291 for (i = 0; i < xhci->msix_count; i++) {
292 ret = request_irq(xhci->msix_entries[i].vector,
293 (irq_handler_t)xhci_msi_irq,
294 0, "xhci_hcd", xhci_to_hcd(xhci));
295 if (ret)
296 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700297 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700298
Andiry Xu00292272010-12-27 17:39:02 +0800299 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700300 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700301
302disable_msix:
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800303 xhci_dbg(xhci, "disable MSI-X interrupt\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700304 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700305 pci_disable_msix(pdev);
306free_entries:
307 kfree(xhci->msix_entries);
308 xhci->msix_entries = NULL;
309 return ret;
310}
311
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700312/* Free any IRQs and disable MSI-X */
313static void xhci_cleanup_msix(struct xhci_hcd *xhci)
314{
Andiry Xu00292272010-12-27 17:39:02 +0800315 struct usb_hcd *hcd = xhci_to_hcd(xhci);
316 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700317
Jack Pham23faa152013-11-15 14:53:14 -0800318 if (xhci->quirks & XHCI_PLAT)
319 return;
320
Dong Nguyen43b86af2010-07-21 16:56:08 -0700321 xhci_free_irq(xhci);
322
323 if (xhci->msix_entries) {
324 pci_disable_msix(pdev);
325 kfree(xhci->msix_entries);
326 xhci->msix_entries = NULL;
327 } else {
328 pci_disable_msi(pdev);
329 }
330
Andiry Xu00292272010-12-27 17:39:02 +0800331 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700332 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700333}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700334
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700335static int xhci_try_enable_msi(struct usb_hcd *hcd)
336{
337 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharpdf5831d2013-08-08 10:08:34 -0700338 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700339 int ret;
340
Sarah Sharpdf5831d2013-08-08 10:08:34 -0700341 /* The xhci platform device has set up IRQs through usb_add_hcd. */
342 if (xhci->quirks & XHCI_PLAT)
343 return 0;
344
345 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700346 /*
347 * Some Fresco Logic host controllers advertise MSI, but fail to
348 * generate interrupts. Don't even try to enable MSI.
349 */
350 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecked581bb32013-03-04 17:14:43 +0100351 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700352
353 /* unregister the legacy interrupt */
354 if (hcd->irq)
355 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200356 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700357
358 ret = xhci_setup_msix(xhci);
359 if (ret)
360 /* fall back to msi*/
361 ret = xhci_setup_msi(xhci);
362
363 if (!ret)
Felipe Balbicd704692012-02-29 16:46:23 +0200364 /* hcd->irq is 0, we have MSI */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700365 return 0;
366
Sarah Sharp68d07f62012-02-13 16:25:57 -0800367 if (!pdev->irq) {
368 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
369 return -EINVAL;
370 }
371
Hannes Reinecked581bb32013-03-04 17:14:43 +0100372 legacy_irq:
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700373 /* fall back to legacy interrupt*/
374 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
375 hcd->irq_descr, hcd);
376 if (ret) {
377 xhci_err(xhci, "request interrupt %d failed\n",
378 pdev->irq);
379 return ret;
380 }
381 hcd->irq = pdev->irq;
382 return 0;
383}
384
385#else
386
David Cohenf97f28f2014-04-25 19:20:16 +0300387static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700388{
389 return 0;
390}
391
David Cohenf97f28f2014-04-25 19:20:16 +0300392static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700393{
394}
395
David Cohenf97f28f2014-04-25 19:20:16 +0300396static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700397{
398}
399
400#endif
401
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500402static void compliance_mode_recovery(unsigned long arg)
403{
404 struct xhci_hcd *xhci;
405 struct usb_hcd *hcd;
406 u32 temp;
407 int i;
408
409 xhci = (struct xhci_hcd *)arg;
410
411 for (i = 0; i < xhci->num_usb3_ports; i++) {
412 temp = xhci_readl(xhci, xhci->usb3_ports[i]);
413 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
414 /*
415 * Compliance Mode Detected. Letting USB Core
416 * handle the Warm Reset
417 */
418 xhci_dbg(xhci, "Compliance Mode Detected->Port %d!\n",
419 i + 1);
420 xhci_dbg(xhci, "Attempting Recovery routine!\n");
421 hcd = xhci->shared_hcd;
422
423 if (hcd->state == HC_STATE_SUSPENDED)
424 usb_hcd_resume_root_hub(hcd);
425
426 usb_hcd_poll_rh_status(hcd);
427 }
428 }
429
430 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
431 mod_timer(&xhci->comp_mode_recovery_timer,
432 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
433}
434
435/*
436 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
437 * that causes ports behind that hardware to enter compliance mode sometimes.
438 * The quirk creates a timer that polls every 2 seconds the link state of
439 * each host controller's port and recovers it by issuing a Warm reset
440 * if Compliance mode is detected, otherwise the port will become "dead" (no
441 * device connections or disconnections will be detected anymore). Becasue no
442 * status event is generated when entering compliance mode (per xhci spec),
443 * this quirk is needed on systems that have the failing hardware installed.
444 */
445static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
446{
447 xhci->port_status_u0 = 0;
448 init_timer(&xhci->comp_mode_recovery_timer);
449
450 xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
451 xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
452 xhci->comp_mode_recovery_timer.expires = jiffies +
453 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
454
455 set_timer_slack(&xhci->comp_mode_recovery_timer,
456 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
457 add_timer(&xhci->comp_mode_recovery_timer);
458 xhci_dbg(xhci, "Compliance Mode Recovery Timer Initialized.\n");
459}
460
461/*
462 * This function identifies the systems that have installed the SN65LVPE502CP
463 * USB3.0 re-driver and that need the Compliance Mode Quirk.
464 * Systems:
465 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
466 */
467static bool compliance_mode_recovery_timer_quirk_check(void)
468{
469 const char *dmi_product_name, *dmi_sys_vendor;
470
471 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
472 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam1d645602012-09-22 18:11:19 +0530473 if (!dmi_product_name || !dmi_sys_vendor)
474 return false;
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500475
476 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
477 return false;
478
479 if (strstr(dmi_product_name, "Z420") ||
480 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes045b3612012-10-17 14:09:12 -0500481 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortes4b2e6102012-11-08 16:59:27 -0600482 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500483 return true;
484
485 return false;
486}
487
488static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
489{
490 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
491}
492
493
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700494/*
495 * Initialize memory for HCD and xHC (one-time init).
496 *
497 * Program the PAGESIZE register, initialize the device context array, create
498 * device contexts (?), set up a command ring segment (or two?), create event
499 * ring (one for now).
500 */
501int xhci_init(struct usb_hcd *hcd)
502{
503 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
504 int retval = 0;
505
506 xhci_dbg(xhci, "xhci_init\n");
507 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700508 if (xhci->hci_version == 0x95 && link_quirk) {
Sarah Sharpb0567b32009-08-07 14:04:36 -0700509 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
510 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
511 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700512 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700513 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700514 retval = xhci_mem_init(xhci, GFP_KERNEL);
515 xhci_dbg(xhci, "Finished xhci_init\n");
516
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500517 /* Initializing Compliance Mode Recovery Data If Needed */
518 if (compliance_mode_recovery_timer_quirk_check()) {
519 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
520 compliance_mode_recovery_timer_init(xhci);
521 }
522
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700523 return retval;
524}
525
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700526/*-------------------------------------------------------------------------*/
527
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700528
529#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800530static void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700531{
532 unsigned long flags;
533 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700534 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700535 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
536 int i, j;
537
538 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
539
540 spin_lock_irqsave(&xhci->lock, flags);
541 temp = xhci_readl(xhci, &xhci->op_regs->status);
542 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp7bd89b42011-07-01 13:35:40 -0700543 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
544 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700545 xhci_dbg(xhci, "HW died, polling stopped.\n");
546 spin_unlock_irqrestore(&xhci->lock, flags);
547 return;
548 }
549
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700550 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
551 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700552 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
553 xhci->error_bitmask = 0;
554 xhci_dbg(xhci, "Event ring:\n");
555 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
556 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700557 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
558 temp_64 &= ~ERST_PTR_MASK;
559 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700560 xhci_dbg(xhci, "Command ring:\n");
561 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
562 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
563 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700564 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700565 if (!xhci->devs[i])
566 continue;
567 for (j = 0; j < 31; ++j) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700568 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700569 }
570 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700571 spin_unlock_irqrestore(&xhci->lock, flags);
572
573 if (!xhci->zombie)
574 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
575 else
576 xhci_dbg(xhci, "Quit polling the event ring.\n");
577}
578#endif
579
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800580static int xhci_run_finished(struct xhci_hcd *xhci)
581{
582 if (xhci_start(xhci)) {
583 xhci_halt(xhci);
584 return -ENODEV;
585 }
586 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fu1976fff2012-06-27 16:30:57 +0800587 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800588
589 if (xhci->quirks & XHCI_NEC_HOST)
590 xhci_ring_cmd_db(xhci);
591
592 xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
593 return 0;
594}
595
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700596/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700597 * Start the HC after it was halted.
598 *
599 * This function is called by the USB core when the HC driver is added.
600 * Its opposite is xhci_stop().
601 *
602 * xhci_init() must be called once before this function can be called.
603 * Reset the HC, enable device slot contexts, program DCBAAP, and
604 * set command ring pointer and event ring pointer.
605 *
606 * Setup MSI-X vectors and enable interrupts.
607 */
608int xhci_run(struct usb_hcd *hcd)
609{
610 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700611 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700612 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700613 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700614
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800615 /* Start the xHCI host controller running only after the USB 2.0 roothub
616 * is setup.
617 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700618
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700619 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800620 if (!usb_hcd_is_primary_hcd(hcd))
621 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700622
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700623 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700624
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200625 xhci_dbg(xhci, "Calling HCD init\n");
626 /* Initialize HCD and host controller data structures. */
627 ret = xhci_init(hcd);
628 if (ret)
629 return ret;
630 xhci_dbg(xhci, "Called HCD init\n");
631
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700632 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700633 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700634 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700635
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700636#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
637 init_timer(&xhci->event_ring_timer);
638 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700639 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700640 /* Poll the event ring */
641 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
642 xhci->zombie = 0;
643 xhci_dbg(xhci, "Setting event ring polling timer\n");
644 add_timer(&xhci->event_ring_timer);
645#endif
646
Sarah Sharp66e49d82009-07-27 12:03:46 -0700647 xhci_dbg(xhci, "Command ring memory map follows:\n");
648 xhci_debug_ring(xhci, xhci->cmd_ring);
649 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
650 xhci_dbg_cmd_ptrs(xhci);
651
652 xhci_dbg(xhci, "ERST memory map follows:\n");
653 xhci_dbg_erst(xhci, &xhci->erst);
654 xhci_dbg(xhci, "Event ring:\n");
655 xhci_debug_ring(xhci, xhci->event_ring);
656 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
657 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
658 temp_64 &= ~ERST_PTR_MASK;
659 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
660
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700661 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
662 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700663 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700664 temp |= (u32) 160;
665 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
666
667 /* Set the HCD state before we enable the irqs */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700668 temp = xhci_readl(xhci, &xhci->op_regs->command);
669 temp |= (CMD_EIE);
670 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
671 temp);
672 xhci_writel(xhci, temp, &xhci->op_regs->command);
673
674 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700675 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
676 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700677 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
678 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800679 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700680
Sarah Sharp02386342010-05-24 13:25:28 -0700681 if (xhci->quirks & XHCI_NEC_HOST)
682 xhci_queue_vendor_command(xhci, 0, 0, 0,
683 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700684
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800685 xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700686 return 0;
687}
688
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800689static void xhci_only_stop_hcd(struct usb_hcd *hcd)
690{
691 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
692
693 spin_lock_irq(&xhci->lock);
694 xhci_halt(xhci);
695
696 /* The shared_hcd is going to be deallocated shortly (the USB core only
697 * calls this function when allocation fails in usb_add_hcd(), or
698 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
699 */
700 xhci->shared_hcd = NULL;
701 spin_unlock_irq(&xhci->lock);
702}
703
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700704/*
705 * Stop xHCI driver.
706 *
707 * This function is called by the USB core when the HC driver is removed.
708 * Its opposite is xhci_run().
709 *
710 * Disable device contexts, disable IRQs, and quiesce the HC.
711 * Reset the HC, finish any completed transactions, and cleanup memory.
712 */
713void xhci_stop(struct usb_hcd *hcd)
714{
715 u32 temp;
716 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
717
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800718 if (!usb_hcd_is_primary_hcd(hcd)) {
719 xhci_only_stop_hcd(xhci->shared_hcd);
720 return;
721 }
722
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700723 spin_lock_irq(&xhci->lock);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800724 /* Make sure the xHC is halted for a USB3 roothub
725 * (xhci_stop() could be called as part of failed init).
726 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700727 xhci_halt(xhci);
728 xhci_reset(xhci);
729 spin_unlock_irq(&xhci->lock);
730
Zhang Rui40a9fb12010-12-17 13:17:04 -0800731 xhci_cleanup_msix(xhci);
732
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700733#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
734 /* Tell the event ring poll function not to reschedule */
735 xhci->zombie = 1;
736 del_timer_sync(&xhci->event_ring_timer);
737#endif
738
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500739 /* Deleting Compliance Mode Recovery Timer */
740 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
741 (!(xhci_all_ports_seen_u0(xhci))))
742 del_timer_sync(&xhci->comp_mode_recovery_timer);
743
Andiry Xuc41136b2011-03-22 17:08:14 +0800744 if (xhci->quirks & XHCI_AMD_PLL_FIX)
745 usb_amd_dev_put();
746
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700747 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
748 temp = xhci_readl(xhci, &xhci->op_regs->status);
749 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
750 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
751 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
752 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800753 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700754
755 xhci_dbg(xhci, "cleaning up memory\n");
756 xhci_mem_cleanup(xhci);
757 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
758 xhci_readl(xhci, &xhci->op_regs->status));
759}
760
761/*
762 * Shutdown HC (not bus-specific)
763 *
764 * This is called when the machine is rebooting or halting. We assume that the
765 * machine will be powered off, and the HC's internal state will be reset.
766 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800767 *
768 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700769 */
770void xhci_shutdown(struct usb_hcd *hcd)
771{
772 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
773
Dan Carpenter3dd2f0b2012-08-13 19:57:03 +0300774 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Sarah Sharp0adf7a02012-07-23 18:59:30 +0300775 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
776
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700777 spin_lock_irq(&xhci->lock);
778 xhci_halt(xhci);
Takashi Iwai630b5e02013-09-12 08:11:06 +0200779 /* Workaround for spurious wakeups at shutdown with HSW */
780 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
781 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700782 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700783
Zhang Rui40a9fb12010-12-17 13:17:04 -0800784 xhci_cleanup_msix(xhci);
785
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700786 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
787 xhci_readl(xhci, &xhci->op_regs->status));
Takashi Iwai630b5e02013-09-12 08:11:06 +0200788
789 /* Yet another workaround for spurious wakeups at shutdown with HSW */
790 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
791 pci_set_power_state(to_pci_dev(hcd->self.controller), PCI_D3hot);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700792}
793
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700794#ifdef CONFIG_PM
Ido Shayevitzb546ed72012-06-06 20:27:45 +0300795
796#ifdef CONFIG_PCI
797static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
798{
799 int i;
800
801 if (xhci->msix_entries) {
802 for (i = 0; i < xhci->msix_count; i++)
803 synchronize_irq(xhci->msix_entries[i].vector);
804 }
805}
806#else
807static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
808{
809}
810#endif /* CONFIG_PCI */
811
Andiry Xu5535b1d2010-10-14 07:23:06 -0700812static void xhci_save_registers(struct xhci_hcd *xhci)
813{
814 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
815 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
816 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
817 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700818 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
819 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
820 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700821 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
822 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700823}
824
825static void xhci_restore_registers(struct xhci_hcd *xhci)
826{
827 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
828 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
829 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
830 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700831 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
832 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
Sarah Sharpfb3d85b2012-03-16 13:27:39 -0700833 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700834 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
835 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700836}
837
Sarah Sharp89821322010-11-12 11:59:31 -0800838static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
839{
840 u64 val_64;
841
842 /* step 2: initialize command ring buffer */
843 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
844 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
845 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
846 xhci->cmd_ring->dequeue) &
847 (u64) ~CMD_RING_RSVD_BITS) |
848 xhci->cmd_ring->cycle_state;
849 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
850 (long unsigned long) val_64);
851 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
852}
853
854/*
855 * The whole command ring must be cleared to zero when we suspend the host.
856 *
857 * The host doesn't save the command ring pointer in the suspend well, so we
858 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
859 * aligned, because of the reserved bits in the command ring dequeue pointer
860 * register. Therefore, we can't just set the dequeue pointer back in the
861 * middle of the ring (TRBs are 16-byte aligned).
862 */
863static void xhci_clear_command_ring(struct xhci_hcd *xhci)
864{
865 struct xhci_ring *ring;
866 struct xhci_segment *seg;
867
868 ring = xhci->cmd_ring;
869 seg = ring->deq_seg;
870 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800871 memset(seg->trbs, 0,
872 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
873 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
874 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800875 seg = seg->next;
876 } while (seg != ring->deq_seg);
877
878 /* Reset the software enqueue and dequeue pointers */
879 ring->deq_seg = ring->first_seg;
880 ring->dequeue = ring->first_seg->trbs;
881 ring->enq_seg = ring->deq_seg;
882 ring->enqueue = ring->dequeue;
883
Andiry Xub008df62012-03-05 17:49:34 +0800884 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800885 /*
886 * Ring is now zeroed, so the HW should look for change of ownership
887 * when the cycle bit is set to 1.
888 */
889 ring->cycle_state = 1;
890
891 /*
892 * Reset the hardware dequeue pointer.
893 * Yes, this will need to be re-written after resume, but we're paranoid
894 * and want to make sure the hardware doesn't access bogus memory
895 * because, say, the BIOS or an SMI started the host without changing
896 * the command ring pointers.
897 */
898 xhci_set_cmd_ring_deq(xhci);
899}
900
Andiry Xu5535b1d2010-10-14 07:23:06 -0700901/*
902 * Stop HC (not bus-specific)
903 *
904 * This is called when the machine transition into S3/S4 mode.
905 *
906 */
907int xhci_suspend(struct xhci_hcd *xhci)
908{
909 int rc = 0;
Oliver Neukume4330c72013-09-30 15:50:54 +0200910 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700911 struct usb_hcd *hcd = xhci_to_hcd(xhci);
912 u32 command;
913
Sarah Sharp4ceac472012-11-27 12:30:23 -0800914 /* Don't poll the roothubs on bus suspend. */
915 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
916 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
917 del_timer_sync(&hcd->rh_timer);
918
Andiry Xu5535b1d2010-10-14 07:23:06 -0700919 spin_lock_irq(&xhci->lock);
920 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -0800921 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700922 /* step 1: stop endpoint */
923 /* skipped assuming that port suspend has done */
924
925 /* step 2: clear Run/Stop bit */
926 command = xhci_readl(xhci, &xhci->op_regs->command);
927 command &= ~CMD_RUN;
928 xhci_writel(xhci, command, &xhci->op_regs->command);
Oliver Neukume4330c72013-09-30 15:50:54 +0200929
930 /* Some chips from Fresco Logic need an extraordinary delay */
931 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
932
Andiry Xu5535b1d2010-10-14 07:23:06 -0700933 if (handshake(xhci, &xhci->op_regs->status,
Oliver Neukume4330c72013-09-30 15:50:54 +0200934 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d2010-10-14 07:23:06 -0700935 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
936 spin_unlock_irq(&xhci->lock);
937 return -ETIMEDOUT;
938 }
Sarah Sharp89821322010-11-12 11:59:31 -0800939 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700940
941 /* step 3: save registers */
942 xhci_save_registers(xhci);
943
944 /* step 4: set CSS flag */
945 command = xhci_readl(xhci, &xhci->op_regs->command);
946 command |= CMD_CSS;
947 xhci_writel(xhci, command, &xhci->op_regs->command);
Andiry Xu5dc6fed2012-06-13 10:51:57 +0800948 if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10 * 1000)) {
949 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700950 spin_unlock_irq(&xhci->lock);
951 return -ETIMEDOUT;
952 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700953 spin_unlock_irq(&xhci->lock);
954
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500955 /*
956 * Deleting Compliance Mode Recovery Timer because the xHCI Host
957 * is about to be suspended.
958 */
959 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
960 (!(xhci_all_ports_seen_u0(xhci)))) {
961 del_timer_sync(&xhci->comp_mode_recovery_timer);
962 xhci_dbg(xhci, "Compliance Mode Recovery Timer Deleted!\n");
963 }
964
Andiry Xu00292272010-12-27 17:39:02 +0800965 /* step 5: remove core well power */
966 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700967 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800968
Andiry Xu5535b1d2010-10-14 07:23:06 -0700969 return rc;
970}
971
972/*
973 * start xHC (not bus-specific)
974 *
975 * This is called when the machine transition from S3/S4 mode.
976 *
977 */
978int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
979{
Wang, Yu939a3a42014-06-24 17:14:44 +0300980 u32 command, temp = 0, status;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700981 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -0800982 struct usb_hcd *secondary_hcd;
Alan Sternf69e3122011-11-03 11:37:10 -0400983 int retval = 0;
Tony Camuso6eb953e2013-02-21 16:11:27 -0500984 bool comp_timer_running = false;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700985
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800986 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300987 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800988 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800989 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
990 time_before(jiffies,
991 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -0700992 msleep(100);
993
Alan Sternf69e3122011-11-03 11:37:10 -0400994 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
995 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
996
Andiry Xu5535b1d2010-10-14 07:23:06 -0700997 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +0200998 if (xhci->quirks & XHCI_RESET_ON_RESUME)
999 hibernated = true;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001000
1001 if (!hibernated) {
1002 /* step 1: restore register */
1003 xhci_restore_registers(xhci);
1004 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -08001005 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001006 /* step 3: restore state and start state*/
1007 /* step 3: set CRS flag */
1008 command = xhci_readl(xhci, &xhci->op_regs->command);
1009 command |= CMD_CRS;
1010 xhci_writel(xhci, command, &xhci->op_regs->command);
1011 if (handshake(xhci, &xhci->op_regs->status,
Andiry Xu5dc6fed2012-06-13 10:51:57 +08001012 STS_RESTORE, 0, 10 * 1000)) {
1013 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -07001014 spin_unlock_irq(&xhci->lock);
1015 return -ETIMEDOUT;
1016 }
1017 temp = xhci_readl(xhci, &xhci->op_regs->status);
1018 }
1019
1020 /* If restore operation fails, re-initialize the HC during resume */
1021 if ((temp & STS_SRE) || hibernated) {
Tony Camuso6eb953e2013-02-21 16:11:27 -05001022
1023 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1024 !(xhci_all_ports_seen_u0(xhci))) {
1025 del_timer_sync(&xhci->comp_mode_recovery_timer);
1026 xhci_dbg(xhci, "Compliance Mode Recovery Timer deleted!\n");
1027 }
1028
Sarah Sharpfedd3832011-04-12 17:43:19 -07001029 /* Let the USB core know _both_ roothubs lost power. */
1030 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1031 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001032
1033 xhci_dbg(xhci, "Stop HCD\n");
1034 xhci_halt(xhci);
1035 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001036 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001037 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001038
1039#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
1040 /* Tell the event ring poll function not to reschedule */
1041 xhci->zombie = 1;
1042 del_timer_sync(&xhci->event_ring_timer);
1043#endif
1044
1045 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
1046 temp = xhci_readl(xhci, &xhci->op_regs->status);
1047 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
1048 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
1049 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
1050 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -08001051 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001052
1053 xhci_dbg(xhci, "cleaning up memory\n");
1054 xhci_mem_cleanup(xhci);
1055 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1056 xhci_readl(xhci, &xhci->op_regs->status));
1057
Sarah Sharp65b22f92010-12-17 12:35:05 -08001058 /* USB core calls the PCI reinit and start functions twice:
1059 * first with the primary HCD, and then with the secondary HCD.
1060 * If we don't do the same, the host will never be started.
1061 */
1062 if (!usb_hcd_is_primary_hcd(hcd))
1063 secondary_hcd = hcd;
1064 else
1065 secondary_hcd = xhci->shared_hcd;
1066
1067 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1068 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001069 if (retval)
1070 return retval;
Tony Camuso6eb953e2013-02-21 16:11:27 -05001071 comp_timer_running = true;
1072
Sarah Sharp65b22f92010-12-17 12:35:05 -08001073 xhci_dbg(xhci, "Start the primary HCD\n");
1074 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001075 if (!retval) {
Alan Sternf69e3122011-11-03 11:37:10 -04001076 xhci_dbg(xhci, "Start the secondary HCD\n");
1077 retval = xhci_run(secondary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001078 }
Andiry Xu5535b1d2010-10-14 07:23:06 -07001079 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb3209372011-03-07 11:24:07 -08001080 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e3122011-11-03 11:37:10 -04001081 goto done;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001082 }
1083
Andiry Xu5535b1d2010-10-14 07:23:06 -07001084 /* step 4: set Run/Stop bit */
1085 command = xhci_readl(xhci, &xhci->op_regs->command);
1086 command |= CMD_RUN;
1087 xhci_writel(xhci, command, &xhci->op_regs->command);
1088 handshake(xhci, &xhci->op_regs->status, STS_HALT,
1089 0, 250 * 1000);
1090
1091 /* step 5: walk topology and initialize portsc,
1092 * portpmsc and portli
1093 */
1094 /* this is done in bus_resume */
1095
1096 /* step 6: restart each of the previously
1097 * Running endpoints by ringing their doorbells
1098 */
1099
Andiry Xu5535b1d2010-10-14 07:23:06 -07001100 spin_unlock_irq(&xhci->lock);
Alan Sternf69e3122011-11-03 11:37:10 -04001101
1102 done:
1103 if (retval == 0) {
Wang, Yu939a3a42014-06-24 17:14:44 +03001104 /* Resume root hubs only when have pending events. */
1105 status = readl(&xhci->op_regs->status);
1106 if (status & STS_EINT) {
1107 usb_hcd_resume_root_hub(hcd);
1108 usb_hcd_resume_root_hub(xhci->shared_hcd);
1109 }
Alan Sternf69e3122011-11-03 11:37:10 -04001110 }
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -05001111
1112 /*
1113 * If system is subject to the Quirk, Compliance Mode Timer needs to
1114 * be re-initialized Always after a system resume. Ports are subject
1115 * to suffer the Compliance Mode issue again. It doesn't matter if
1116 * ports have entered previously to U0 before system's suspension.
1117 */
Tony Camuso6eb953e2013-02-21 16:11:27 -05001118 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -05001119 compliance_mode_recovery_timer_init(xhci);
1120
Sarah Sharp4ceac472012-11-27 12:30:23 -08001121 /* Re-enable port polling. */
1122 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1123 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1124 usb_hcd_poll_rh_status(hcd);
1125
Alan Sternf69e3122011-11-03 11:37:10 -04001126 return retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001127}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001128#endif /* CONFIG_PM */
1129
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001130/*-------------------------------------------------------------------------*/
1131
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001132/**
1133 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1134 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1135 * value to right shift 1 for the bitmask.
1136 *
1137 * Index = (epnum * 2) + direction - 1,
1138 * where direction = 0 for OUT, 1 for IN.
1139 * For control endpoints, the IN index is used (OUT index is unused), so
1140 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1141 */
1142unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1143{
1144 unsigned int index;
1145 if (usb_endpoint_xfer_control(desc))
1146 index = (unsigned int) (usb_endpoint_num(desc)*2);
1147 else
1148 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1149 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1150 return index;
1151}
1152
Sarah Sharpf94e01862009-04-27 19:58:38 -07001153/* Find the flag for this endpoint (for use in the control context). Use the
1154 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1155 * bit 1, etc.
1156 */
1157unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1158{
1159 return 1 << (xhci_get_endpoint_index(desc) + 1);
1160}
1161
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001162/* Find the flag for this endpoint (for use in the control context). Use the
1163 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1164 * bit 1, etc.
1165 */
1166unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1167{
1168 return 1 << (ep_index + 1);
1169}
1170
Sarah Sharpf94e01862009-04-27 19:58:38 -07001171/* Compute the last valid endpoint context index. Basically, this is the
1172 * endpoint index plus one. For slot contexts with more than valid endpoint,
1173 * we find the most significant bit set in the added contexts flags.
1174 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1175 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1176 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001177unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001178{
1179 return fls(added_ctxs) - 1;
1180}
1181
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001182/* Returns 1 if the arguments are OK;
1183 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1184 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001185static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001186 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1187 const char *func) {
1188 struct xhci_hcd *xhci;
1189 struct xhci_virt_device *virt_dev;
1190
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001191 if (!hcd || (check_ep && !ep) || !udev) {
1192 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
1193 func);
1194 return -EINVAL;
1195 }
1196 if (!udev->parent) {
1197 printk(KERN_DEBUG "xHCI %s called for root hub\n",
1198 func);
1199 return 0;
1200 }
Andiry Xu64927732010-10-14 07:22:45 -07001201
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001202 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001203 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001204 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Andiry Xu64927732010-10-14 07:22:45 -07001205 printk(KERN_DEBUG "xHCI %s called with unaddressed "
1206 "device\n", func);
1207 return -EINVAL;
1208 }
1209
1210 virt_dev = xhci->devs[udev->slot_id];
1211 if (virt_dev->udev != udev) {
1212 printk(KERN_DEBUG "xHCI %s called with udev and "
1213 "virt_dev does not match\n", func);
1214 return -EINVAL;
1215 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001216 }
Andiry Xu64927732010-10-14 07:22:45 -07001217
Sarah Sharp79bc1752013-07-24 10:27:13 -07001218 if (xhci->xhc_state & XHCI_STATE_HALTED)
1219 return -ENODEV;
1220
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001221 return 1;
1222}
1223
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001224static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001225 struct usb_device *udev, struct xhci_command *command,
1226 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001227
1228/*
1229 * Full speed devices may have a max packet size greater than 8 bytes, but the
1230 * USB core doesn't know that until it reads the first 8 bytes of the
1231 * descriptor. If the usb_device's max packet size changes after that point,
1232 * we need to issue an evaluate context command and wait on it.
1233 */
1234static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1235 unsigned int ep_index, struct urb *urb)
1236{
1237 struct xhci_container_ctx *in_ctx;
1238 struct xhci_container_ctx *out_ctx;
1239 struct xhci_input_control_ctx *ctrl_ctx;
1240 struct xhci_ep_ctx *ep_ctx;
1241 int max_packet_size;
1242 int hw_max_packet_size;
1243 int ret = 0;
1244
1245 out_ctx = xhci->devs[slot_id]->out_ctx;
1246 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001247 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001248 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001249 if (hw_max_packet_size != max_packet_size) {
1250 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
1251 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
1252 max_packet_size);
1253 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
1254 hw_max_packet_size);
1255 xhci_dbg(xhci, "Issuing evaluate context command.\n");
1256
1257 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -07001258 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1259 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001260 in_ctx = xhci->devs[slot_id]->in_ctx;
1261 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001262 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1263 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001264
1265 /* Set up the input context flags for the command */
1266 /* FIXME: This won't work if a non-default control endpoint
1267 * changes max packet sizes.
1268 */
1269 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001270 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001271 ctrl_ctx->drop_flags = 0;
1272
1273 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1274 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1275 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1276 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1277
Sarah Sharp913a8a32009-09-04 10:53:13 -07001278 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1279 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001280
1281 /* Clean up the input context for later use by bandwidth
1282 * functions.
1283 */
Matt Evans28ccd292011-03-29 13:40:46 +11001284 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001285 }
1286 return ret;
1287}
1288
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001289/*
1290 * non-error returns are a promise to giveback() the urb later
1291 * we drop ownership so next owner (or urb unlink) can get it
1292 */
1293int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1294{
1295 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Andiry Xu2ffdea22011-09-02 11:05:57 -07001296 struct xhci_td *buffer;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001297 unsigned long flags;
1298 int ret = 0;
1299 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001300 struct urb_priv *urb_priv;
1301 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001302
Andiry Xu64927732010-10-14 07:22:45 -07001303 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1304 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001305 return -EINVAL;
1306
1307 slot_id = urb->dev->slot_id;
1308 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001309
Alan Stern541c7d42010-06-22 16:39:10 -04001310 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001311 if (!in_interrupt())
1312 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1313 ret = -ESHUTDOWN;
1314 goto exit;
1315 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001316
1317 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1318 size = urb->number_of_packets;
1319 else
1320 size = 1;
1321
1322 urb_priv = kzalloc(sizeof(struct urb_priv) +
1323 size * sizeof(struct xhci_td *), mem_flags);
1324 if (!urb_priv)
1325 return -ENOMEM;
1326
Andiry Xu2ffdea22011-09-02 11:05:57 -07001327 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1328 if (!buffer) {
1329 kfree(urb_priv);
1330 return -ENOMEM;
1331 }
1332
Andiry Xu8e51adc2010-07-22 15:23:31 -07001333 for (i = 0; i < size; i++) {
Andiry Xu2ffdea22011-09-02 11:05:57 -07001334 urb_priv->td[i] = buffer;
1335 buffer++;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001336 }
1337
1338 urb_priv->length = size;
1339 urb_priv->td_cnt = 0;
1340 urb->hcpriv = urb_priv;
1341
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001342 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1343 /* Check to see if the max packet size for the default control
1344 * endpoint changed during FS device enumeration
1345 */
1346 if (urb->dev->speed == USB_SPEED_FULL) {
1347 ret = xhci_check_maxpacket(xhci, slot_id,
1348 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001349 if (ret < 0) {
1350 xhci_urb_free_priv(xhci, urb_priv);
1351 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001352 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001353 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001354 }
1355
Sarah Sharpb11069f2009-07-27 12:03:23 -07001356 /* We have a spinlock and interrupts disabled, so we must pass
1357 * atomic context to this function, which may allocate memory.
1358 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001359 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001360 if (xhci->xhc_state & XHCI_STATE_DYING)
1361 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001362 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001363 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001364 if (ret)
1365 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001366 spin_unlock_irqrestore(&xhci->lock, flags);
1367 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1368 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001369 if (xhci->xhc_state & XHCI_STATE_DYING)
1370 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001371 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1372 EP_GETTING_STREAMS) {
1373 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1374 "is transitioning to using streams.\n");
1375 ret = -EINVAL;
1376 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1377 EP_GETTING_NO_STREAMS) {
1378 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1379 "is transitioning to "
1380 "not having streams.\n");
1381 ret = -EINVAL;
1382 } else {
1383 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1384 slot_id, ep_index);
1385 }
Sarah Sharpd13565c2011-07-22 14:34:34 -07001386 if (ret)
1387 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001388 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001389 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1390 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001391 if (xhci->xhc_state & XHCI_STATE_DYING)
1392 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001393 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1394 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001395 if (ret)
1396 goto free_priv;
Sarah Sharp624defa2009-09-02 12:14:28 -07001397 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001398 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001399 spin_lock_irqsave(&xhci->lock, flags);
1400 if (xhci->xhc_state & XHCI_STATE_DYING)
1401 goto dying;
1402 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1403 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001404 if (ret)
1405 goto free_priv;
Andiry Xu787f4e52010-07-22 15:23:52 -07001406 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001407 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001408exit:
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001409 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001410dying:
1411 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1412 "non-responsive xHCI host.\n",
1413 urb->ep->desc.bEndpointAddress, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001414 ret = -ESHUTDOWN;
1415free_priv:
1416 xhci_urb_free_priv(xhci, urb_priv);
1417 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001418 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001419 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001420}
1421
Sarah Sharp021bff92010-07-29 22:12:20 -07001422/* Get the right ring for the given URB.
1423 * If the endpoint supports streams, boundary check the URB's stream ID.
1424 * If the endpoint doesn't support streams, return the singular endpoint ring.
1425 */
1426static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1427 struct urb *urb)
1428{
1429 unsigned int slot_id;
1430 unsigned int ep_index;
1431 unsigned int stream_id;
1432 struct xhci_virt_ep *ep;
1433
1434 slot_id = urb->dev->slot_id;
1435 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1436 stream_id = urb->stream_id;
1437 ep = &xhci->devs[slot_id]->eps[ep_index];
1438 /* Common case: no streams */
1439 if (!(ep->ep_state & EP_HAS_STREAMS))
1440 return ep->ring;
1441
1442 if (stream_id == 0) {
1443 xhci_warn(xhci,
1444 "WARN: Slot ID %u, ep index %u has streams, "
1445 "but URB has no stream ID.\n",
1446 slot_id, ep_index);
1447 return NULL;
1448 }
1449
1450 if (stream_id < ep->stream_info->num_streams)
1451 return ep->stream_info->stream_rings[stream_id];
1452
1453 xhci_warn(xhci,
1454 "WARN: Slot ID %u, ep index %u has "
1455 "stream IDs 1 to %u allocated, "
1456 "but stream ID %u is requested.\n",
1457 slot_id, ep_index,
1458 ep->stream_info->num_streams - 1,
1459 stream_id);
1460 return NULL;
1461}
1462
Sarah Sharpae636742009-04-29 19:02:31 -07001463/*
1464 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1465 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1466 * should pick up where it left off in the TD, unless a Set Transfer Ring
1467 * Dequeue Pointer is issued.
1468 *
1469 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1470 * the ring. Since the ring is a contiguous structure, they can't be physically
1471 * removed. Instead, there are two options:
1472 *
1473 * 1) If the HC is in the middle of processing the URB to be canceled, we
1474 * simply move the ring's dequeue pointer past those TRBs using the Set
1475 * Transfer Ring Dequeue Pointer command. This will be the common case,
1476 * when drivers timeout on the last submitted URB and attempt to cancel.
1477 *
1478 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1479 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1480 * HC will need to invalidate the any TRBs it has cached after the stop
1481 * endpoint command, as noted in the xHCI 0.95 errata.
1482 *
1483 * 3) The TD may have completed by the time the Stop Endpoint Command
1484 * completes, so software needs to handle that case too.
1485 *
1486 * This function should protect against the TD enqueueing code ringing the
1487 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1488 * It also needs to account for multiple cancellations on happening at the same
1489 * time for the same endpoint.
1490 *
1491 * Note that this function can be called in any context, or so says
1492 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001493 */
1494int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1495{
Sarah Sharpae636742009-04-29 19:02:31 -07001496 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001497 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001498 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001499 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001500 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001501 struct xhci_td *td;
1502 unsigned int ep_index;
1503 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001504 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001505
1506 xhci = hcd_to_xhci(hcd);
1507 spin_lock_irqsave(&xhci->lock, flags);
1508 /* Make sure the URB hasn't completed or been unlinked already */
1509 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1510 if (ret || !urb->hcpriv)
1511 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001512 temp = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -08001513 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001514 xhci_dbg(xhci, "HW died, freeing TD.\n");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001515 urb_priv = urb->hcpriv;
Sarah Sharp585df1d2011-08-02 15:43:40 -07001516 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1517 td = urb_priv->td[i];
1518 if (!list_empty(&td->td_list))
1519 list_del_init(&td->td_list);
1520 if (!list_empty(&td->cancelled_td_list))
1521 list_del_init(&td->cancelled_td_list);
1522 }
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001523
1524 usb_hcd_unlink_urb_from_ep(hcd, urb);
1525 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001526 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001527 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001528 return ret;
1529 }
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001530 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1531 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001532 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1533 "non-responsive xHCI host.\n",
1534 urb->ep->desc.bEndpointAddress, urb);
1535 /* Let the stop endpoint command watchdog timer (which set this
1536 * state) finish cleaning up the endpoint TD lists. We must
1537 * have caught it in the middle of dropping a lock and giving
1538 * back an URB.
1539 */
1540 goto done;
1541 }
Sarah Sharpae636742009-04-29 19:02:31 -07001542
Sarah Sharpae636742009-04-29 19:02:31 -07001543 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001544 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001545 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1546 if (!ep_ring) {
1547 ret = -EINVAL;
1548 goto done;
1549 }
1550
Andiry Xu8e51adc2010-07-22 15:23:31 -07001551 urb_priv = urb->hcpriv;
Sarah Sharp79688ac2011-12-19 16:56:04 -08001552 i = urb_priv->td_cnt;
1553 if (i < urb_priv->length)
1554 xhci_dbg(xhci, "Cancel URB %p, dev %s, ep 0x%x, "
1555 "starting at offset 0x%llx\n",
1556 urb, urb->dev->devpath,
1557 urb->ep->desc.bEndpointAddress,
1558 (unsigned long long) xhci_trb_virt_to_dma(
1559 urb_priv->td[i]->start_seg,
1560 urb_priv->td[i]->first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001561
Sarah Sharp79688ac2011-12-19 16:56:04 -08001562 for (; i < urb_priv->length; i++) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001563 td = urb_priv->td[i];
1564 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1565 }
1566
Sarah Sharpae636742009-04-29 19:02:31 -07001567 /* Queue a stop endpoint command, but only if this is
1568 * the first cancellation to be handled.
1569 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001570 if (!(ep->ep_state & EP_HALT_PENDING)) {
1571 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001572 ep->stop_cmds_pending++;
1573 ep->stop_cmd_timer.expires = jiffies +
1574 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1575 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001576 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001577 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001578 }
1579done:
1580 spin_unlock_irqrestore(&xhci->lock, flags);
1581 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001582}
1583
Sarah Sharpf94e01862009-04-27 19:58:38 -07001584/* Drop an endpoint from a new bandwidth configuration for this device.
1585 * Only one call to this function is allowed per endpoint before
1586 * check_bandwidth() or reset_bandwidth() must be called.
1587 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1588 * add the endpoint to the schedule with possibly new parameters denoted by a
1589 * different endpoint descriptor in usb_host_endpoint.
1590 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1591 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001592 *
1593 * The USB core will not allow URBs to be queued to an endpoint that is being
1594 * disabled, so there's no need for mutual exclusion to protect
1595 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001596 */
1597int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1598 struct usb_host_endpoint *ep)
1599{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001600 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001601 struct xhci_container_ctx *in_ctx, *out_ctx;
1602 struct xhci_input_control_ctx *ctrl_ctx;
1603 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001604 unsigned int last_ctx;
1605 unsigned int ep_index;
1606 struct xhci_ep_ctx *ep_ctx;
1607 u32 drop_flag;
1608 u32 new_add_flags, new_drop_flags, new_slot_info;
1609 int ret;
1610
Andiry Xu64927732010-10-14 07:22:45 -07001611 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001612 if (ret <= 0)
1613 return ret;
1614 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001615 if (xhci->xhc_state & XHCI_STATE_DYING)
1616 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001617
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001618 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001619 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1620 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1621 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1622 __func__, drop_flag);
1623 return 0;
1624 }
1625
Sarah Sharpf94e01862009-04-27 19:58:38 -07001626 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001627 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1628 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001629 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001630 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001631 /* If the HC already knows the endpoint is disabled,
1632 * or the HCD has noted it is disabled, ignore this request
1633 */
Matt Evansf5960b62011-06-01 10:22:55 +10001634 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1635 cpu_to_le32(EP_STATE_DISABLED)) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001636 le32_to_cpu(ctrl_ctx->drop_flags) &
1637 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001638 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1639 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001640 return 0;
1641 }
1642
Matt Evans28ccd292011-03-29 13:40:46 +11001643 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1644 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001645
Matt Evans28ccd292011-03-29 13:40:46 +11001646 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1647 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001648
Matt Evans28ccd292011-03-29 13:40:46 +11001649 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
John Yound115b042009-07-27 12:05:15 -07001650 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001651 /* Update the last valid endpoint context, if we deleted the last one */
Matt Evans28ccd292011-03-29 13:40:46 +11001652 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1653 LAST_CTX(last_ctx)) {
1654 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1655 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001656 }
Matt Evans28ccd292011-03-29 13:40:46 +11001657 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001658
1659 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1660
Sarah Sharpf94e01862009-04-27 19:58:38 -07001661 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1662 (unsigned int) ep->desc.bEndpointAddress,
1663 udev->slot_id,
1664 (unsigned int) new_drop_flags,
1665 (unsigned int) new_add_flags,
1666 (unsigned int) new_slot_info);
1667 return 0;
1668}
1669
1670/* Add an endpoint to a new possible bandwidth configuration for this device.
1671 * Only one call to this function is allowed per endpoint before
1672 * check_bandwidth() or reset_bandwidth() must be called.
1673 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1674 * add the endpoint to the schedule with possibly new parameters denoted by a
1675 * different endpoint descriptor in usb_host_endpoint.
1676 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1677 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001678 *
1679 * The USB core will not allow URBs to be queued to an endpoint until the
1680 * configuration or alt setting is installed in the device, so there's no need
1681 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001682 */
1683int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1684 struct usb_host_endpoint *ep)
1685{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001686 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001687 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001688 unsigned int ep_index;
1689 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001690 struct xhci_slot_ctx *slot_ctx;
1691 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001692 u32 added_ctxs;
1693 unsigned int last_ctx;
1694 u32 new_add_flags, new_drop_flags, new_slot_info;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001695 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001696 int ret = 0;
1697
Andiry Xu64927732010-10-14 07:22:45 -07001698 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001699 if (ret <= 0) {
1700 /* So we won't queue a reset ep command for a root hub */
1701 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001702 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001703 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001704 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001705 if (xhci->xhc_state & XHCI_STATE_DYING)
1706 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001707
1708 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1709 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1710 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1711 /* FIXME when we have to issue an evaluate endpoint command to
1712 * deal with ep0 max packet size changing once we get the
1713 * descriptors
1714 */
1715 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1716 __func__, added_ctxs);
1717 return 0;
1718 }
1719
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001720 virt_dev = xhci->devs[udev->slot_id];
1721 in_ctx = virt_dev->in_ctx;
1722 out_ctx = virt_dev->out_ctx;
John Yound115b042009-07-27 12:05:15 -07001723 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001724 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001725 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001726
1727 /* If this endpoint is already in use, and the upper layers are trying
1728 * to add it again without dropping it, reject the addition.
1729 */
1730 if (virt_dev->eps[ep_index].ring &&
1731 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1732 xhci_get_endpoint_flag(&ep->desc))) {
1733 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1734 "without dropping it.\n",
1735 (unsigned int) ep->desc.bEndpointAddress);
1736 return -EINVAL;
1737 }
1738
Sarah Sharpf94e01862009-04-27 19:58:38 -07001739 /* If the HCD has already noted the endpoint is enabled,
1740 * ignore this request.
1741 */
Matt Evans28ccd292011-03-29 13:40:46 +11001742 if (le32_to_cpu(ctrl_ctx->add_flags) &
1743 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001744 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1745 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001746 return 0;
1747 }
1748
Sarah Sharpf88ba782009-05-14 11:44:22 -07001749 /*
1750 * Configuration and alternate setting changes must be done in
1751 * process context, not interrupt context (or so documenation
1752 * for usb_set_interface() and usb_set_configuration() claim).
1753 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001754 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001755 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1756 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001757 return -ENOMEM;
1758 }
1759
Matt Evans28ccd292011-03-29 13:40:46 +11001760 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1761 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001762
1763 /* If xhci_endpoint_disable() was called for this endpoint, but the
1764 * xHC hasn't been notified yet through the check_bandwidth() call,
1765 * this re-adds a new state for the endpoint from the new endpoint
1766 * descriptors. We must drop and re-add this endpoint, so we leave the
1767 * drop flags alone.
1768 */
Matt Evans28ccd292011-03-29 13:40:46 +11001769 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001770
John Yound115b042009-07-27 12:05:15 -07001771 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001772 /* Update the last valid endpoint context, if we just added one past */
Matt Evans28ccd292011-03-29 13:40:46 +11001773 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1774 LAST_CTX(last_ctx)) {
1775 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1776 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001777 }
Matt Evans28ccd292011-03-29 13:40:46 +11001778 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001779
Sarah Sharpa1587d92009-07-27 12:03:15 -07001780 /* Store the usb_device pointer for later use */
1781 ep->hcpriv = udev;
1782
Sarah Sharpf94e01862009-04-27 19:58:38 -07001783 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1784 (unsigned int) ep->desc.bEndpointAddress,
1785 udev->slot_id,
1786 (unsigned int) new_drop_flags,
1787 (unsigned int) new_add_flags,
1788 (unsigned int) new_slot_info);
1789 return 0;
1790}
1791
John Yound115b042009-07-27 12:05:15 -07001792static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001793{
John Yound115b042009-07-27 12:05:15 -07001794 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001795 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001796 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001797 int i;
1798
1799 /* When a device's add flag and drop flag are zero, any subsequent
1800 * configure endpoint command will leave that endpoint's state
1801 * untouched. Make sure we don't leave any old state in the input
1802 * endpoint contexts.
1803 */
John Yound115b042009-07-27 12:05:15 -07001804 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1805 ctrl_ctx->drop_flags = 0;
1806 ctrl_ctx->add_flags = 0;
1807 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001808 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001809 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001810 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001811 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001812 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001813 ep_ctx->ep_info = 0;
1814 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001815 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001816 ep_ctx->tx_info = 0;
1817 }
1818}
1819
Sarah Sharpf2217e82009-08-07 14:04:43 -07001820static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001821 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001822{
1823 int ret;
1824
Sarah Sharp913a8a32009-09-04 10:53:13 -07001825 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001826 case COMP_ENOMEM:
1827 dev_warn(&udev->dev, "Not enough host controller resources "
1828 "for new device state.\n");
1829 ret = -ENOMEM;
1830 /* FIXME: can we allocate more resources for the HC? */
1831 break;
1832 case COMP_BW_ERR:
Hans de Goede71d85722012-01-04 23:29:18 +01001833 case COMP_2ND_BW_ERR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001834 dev_warn(&udev->dev, "Not enough bandwidth "
1835 "for new device state.\n");
1836 ret = -ENOSPC;
1837 /* FIXME: can we go back to the old state? */
1838 break;
1839 case COMP_TRB_ERR:
1840 /* the HCD set up something wrong */
1841 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1842 "add flag = 1, "
1843 "and endpoint is not disabled.\n");
1844 ret = -EINVAL;
1845 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001846 case COMP_DEV_ERR:
1847 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1848 "configure command.\n");
1849 ret = -ENODEV;
1850 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001851 case COMP_SUCCESS:
1852 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1853 ret = 0;
1854 break;
1855 default:
1856 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001857 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001858 ret = -EINVAL;
1859 break;
1860 }
1861 return ret;
1862}
1863
1864static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001865 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001866{
1867 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001868 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001869
Sarah Sharp913a8a32009-09-04 10:53:13 -07001870 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001871 case COMP_EINVAL:
1872 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1873 "context command.\n");
1874 ret = -EINVAL;
1875 break;
1876 case COMP_EBADSLT:
1877 dev_warn(&udev->dev, "WARN: slot not enabled for"
1878 "evaluate context command.\n");
1879 case COMP_CTX_STATE:
1880 dev_warn(&udev->dev, "WARN: invalid context state for "
1881 "evaluate context command.\n");
1882 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1883 ret = -EINVAL;
1884 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001885 case COMP_DEV_ERR:
1886 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1887 "context command.\n");
1888 ret = -ENODEV;
1889 break;
Alex He1bb73a82011-05-05 18:14:12 +08001890 case COMP_MEL_ERR:
1891 /* Max Exit Latency too large error */
1892 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1893 ret = -EINVAL;
1894 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001895 case COMP_SUCCESS:
1896 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1897 ret = 0;
1898 break;
1899 default:
1900 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001901 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001902 ret = -EINVAL;
1903 break;
1904 }
1905 return ret;
1906}
1907
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001908static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
1909 struct xhci_container_ctx *in_ctx)
1910{
1911 struct xhci_input_control_ctx *ctrl_ctx;
1912 u32 valid_add_flags;
1913 u32 valid_drop_flags;
1914
1915 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1916 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1917 * (bit 1). The default control endpoint is added during the Address
1918 * Device command and is never removed until the slot is disabled.
1919 */
1920 valid_add_flags = ctrl_ctx->add_flags >> 2;
1921 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1922
1923 /* Use hweight32 to count the number of ones in the add flags, or
1924 * number of endpoints added. Don't count endpoints that are changed
1925 * (both added and dropped).
1926 */
1927 return hweight32(valid_add_flags) -
1928 hweight32(valid_add_flags & valid_drop_flags);
1929}
1930
1931static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
1932 struct xhci_container_ctx *in_ctx)
1933{
1934 struct xhci_input_control_ctx *ctrl_ctx;
1935 u32 valid_add_flags;
1936 u32 valid_drop_flags;
1937
1938 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1939 valid_add_flags = ctrl_ctx->add_flags >> 2;
1940 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1941
1942 return hweight32(valid_drop_flags) -
1943 hweight32(valid_add_flags & valid_drop_flags);
1944}
1945
1946/*
1947 * We need to reserve the new number of endpoints before the configure endpoint
1948 * command completes. We can't subtract the dropped endpoints from the number
1949 * of active endpoints until the command completes because we can oversubscribe
1950 * the host in this case:
1951 *
1952 * - the first configure endpoint command drops more endpoints than it adds
1953 * - a second configure endpoint command that adds more endpoints is queued
1954 * - the first configure endpoint command fails, so the config is unchanged
1955 * - the second command may succeed, even though there isn't enough resources
1956 *
1957 * Must be called with xhci->lock held.
1958 */
1959static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
1960 struct xhci_container_ctx *in_ctx)
1961{
1962 u32 added_eps;
1963
1964 added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
1965 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
1966 xhci_dbg(xhci, "Not enough ep ctxs: "
1967 "%u active, need to add %u, limit is %u.\n",
1968 xhci->num_active_eps, added_eps,
1969 xhci->limit_active_eps);
1970 return -ENOMEM;
1971 }
1972 xhci->num_active_eps += added_eps;
1973 xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
1974 xhci->num_active_eps);
1975 return 0;
1976}
1977
1978/*
1979 * The configure endpoint was failed by the xHC for some other reason, so we
1980 * need to revert the resources that failed configuration would have used.
1981 *
1982 * Must be called with xhci->lock held.
1983 */
1984static void xhci_free_host_resources(struct xhci_hcd *xhci,
1985 struct xhci_container_ctx *in_ctx)
1986{
1987 u32 num_failed_eps;
1988
1989 num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
1990 xhci->num_active_eps -= num_failed_eps;
1991 xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
1992 num_failed_eps,
1993 xhci->num_active_eps);
1994}
1995
1996/*
1997 * Now that the command has completed, clean up the active endpoint count by
1998 * subtracting out the endpoints that were dropped (but not changed).
1999 *
2000 * Must be called with xhci->lock held.
2001 */
2002static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
2003 struct xhci_container_ctx *in_ctx)
2004{
2005 u32 num_dropped_eps;
2006
2007 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
2008 xhci->num_active_eps -= num_dropped_eps;
2009 if (num_dropped_eps)
2010 xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
2011 num_dropped_eps,
2012 xhci->num_active_eps);
2013}
2014
Sarah Sharpc29eea62011-09-02 11:05:52 -07002015unsigned int xhci_get_block_size(struct usb_device *udev)
2016{
2017 switch (udev->speed) {
2018 case USB_SPEED_LOW:
2019 case USB_SPEED_FULL:
2020 return FS_BLOCK;
2021 case USB_SPEED_HIGH:
2022 return HS_BLOCK;
2023 case USB_SPEED_SUPER:
2024 return SS_BLOCK;
2025 case USB_SPEED_UNKNOWN:
2026 case USB_SPEED_WIRELESS:
2027 default:
2028 /* Should never happen */
2029 return 1;
2030 }
2031}
2032
2033unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
2034{
2035 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2036 return LS_OVERHEAD;
2037 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2038 return FS_OVERHEAD;
2039 return HS_OVERHEAD;
2040}
2041
2042/* If we are changing a LS/FS device under a HS hub,
2043 * make sure (if we are activating a new TT) that the HS bus has enough
2044 * bandwidth for this new TT.
2045 */
2046static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2047 struct xhci_virt_device *virt_dev,
2048 int old_active_eps)
2049{
2050 struct xhci_interval_bw_table *bw_table;
2051 struct xhci_tt_bw_info *tt_info;
2052
2053 /* Find the bandwidth table for the root port this TT is attached to. */
2054 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2055 tt_info = virt_dev->tt_info;
2056 /* If this TT already had active endpoints, the bandwidth for this TT
2057 * has already been added. Removing all periodic endpoints (and thus
2058 * making the TT enactive) will only decrease the bandwidth used.
2059 */
2060 if (old_active_eps)
2061 return 0;
2062 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2063 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2064 return -ENOMEM;
2065 return 0;
2066 }
2067 /* Not sure why we would have no new active endpoints...
2068 *
2069 * Maybe because of an Evaluate Context change for a hub update or a
2070 * control endpoint 0 max packet size change?
2071 * FIXME: skip the bandwidth calculation in that case.
2072 */
2073 return 0;
2074}
2075
Sarah Sharp2b698992011-09-13 16:41:13 -07002076static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2077 struct xhci_virt_device *virt_dev)
2078{
2079 unsigned int bw_reserved;
2080
2081 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2082 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2083 return -ENOMEM;
2084
2085 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2086 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2087 return -ENOMEM;
2088
2089 return 0;
2090}
2091
Sarah Sharpc29eea62011-09-02 11:05:52 -07002092/*
2093 * This algorithm is a very conservative estimate of the worst-case scheduling
2094 * scenario for any one interval. The hardware dynamically schedules the
2095 * packets, so we can't tell which microframe could be the limiting factor in
2096 * the bandwidth scheduling. This only takes into account periodic endpoints.
2097 *
2098 * Obviously, we can't solve an NP complete problem to find the minimum worst
2099 * case scenario. Instead, we come up with an estimate that is no less than
2100 * the worst case bandwidth used for any one microframe, but may be an
2101 * over-estimate.
2102 *
2103 * We walk the requirements for each endpoint by interval, starting with the
2104 * smallest interval, and place packets in the schedule where there is only one
2105 * possible way to schedule packets for that interval. In order to simplify
2106 * this algorithm, we record the largest max packet size for each interval, and
2107 * assume all packets will be that size.
2108 *
2109 * For interval 0, we obviously must schedule all packets for each interval.
2110 * The bandwidth for interval 0 is just the amount of data to be transmitted
2111 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2112 * the number of packets).
2113 *
2114 * For interval 1, we have two possible microframes to schedule those packets
2115 * in. For this algorithm, if we can schedule the same number of packets for
2116 * each possible scheduling opportunity (each microframe), we will do so. The
2117 * remaining number of packets will be saved to be transmitted in the gaps in
2118 * the next interval's scheduling sequence.
2119 *
2120 * As we move those remaining packets to be scheduled with interval 2 packets,
2121 * we have to double the number of remaining packets to transmit. This is
2122 * because the intervals are actually powers of 2, and we would be transmitting
2123 * the previous interval's packets twice in this interval. We also have to be
2124 * sure that when we look at the largest max packet size for this interval, we
2125 * also look at the largest max packet size for the remaining packets and take
2126 * the greater of the two.
2127 *
2128 * The algorithm continues to evenly distribute packets in each scheduling
2129 * opportunity, and push the remaining packets out, until we get to the last
2130 * interval. Then those packets and their associated overhead are just added
2131 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002132 */
2133static int xhci_check_bw_table(struct xhci_hcd *xhci,
2134 struct xhci_virt_device *virt_dev,
2135 int old_active_eps)
2136{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002137 unsigned int bw_reserved;
2138 unsigned int max_bandwidth;
2139 unsigned int bw_used;
2140 unsigned int block_size;
2141 struct xhci_interval_bw_table *bw_table;
2142 unsigned int packet_size = 0;
2143 unsigned int overhead = 0;
2144 unsigned int packets_transmitted = 0;
2145 unsigned int packets_remaining = 0;
2146 unsigned int i;
2147
Sarah Sharp2b698992011-09-13 16:41:13 -07002148 if (virt_dev->udev->speed == USB_SPEED_SUPER)
2149 return xhci_check_ss_bw(xhci, virt_dev);
2150
Sarah Sharpc29eea62011-09-02 11:05:52 -07002151 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2152 max_bandwidth = HS_BW_LIMIT;
2153 /* Convert percent of bus BW reserved to blocks reserved */
2154 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2155 } else {
2156 max_bandwidth = FS_BW_LIMIT;
2157 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2158 }
2159
2160 bw_table = virt_dev->bw_table;
2161 /* We need to translate the max packet size and max ESIT payloads into
2162 * the units the hardware uses.
2163 */
2164 block_size = xhci_get_block_size(virt_dev->udev);
2165
2166 /* If we are manipulating a LS/FS device under a HS hub, double check
2167 * that the HS bus has enough bandwidth if we are activing a new TT.
2168 */
2169 if (virt_dev->tt_info) {
2170 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2171 virt_dev->real_port);
2172 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2173 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2174 "newly activated TT.\n");
2175 return -ENOMEM;
2176 }
2177 xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
2178 virt_dev->tt_info->slot_id,
2179 virt_dev->tt_info->ttport);
2180 } else {
2181 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2182 virt_dev->real_port);
2183 }
2184
2185 /* Add in how much bandwidth will be used for interval zero, or the
2186 * rounded max ESIT payload + number of packets * largest overhead.
2187 */
2188 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2189 bw_table->interval_bw[0].num_packets *
2190 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2191
2192 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2193 unsigned int bw_added;
2194 unsigned int largest_mps;
2195 unsigned int interval_overhead;
2196
2197 /*
2198 * How many packets could we transmit in this interval?
2199 * If packets didn't fit in the previous interval, we will need
2200 * to transmit that many packets twice within this interval.
2201 */
2202 packets_remaining = 2 * packets_remaining +
2203 bw_table->interval_bw[i].num_packets;
2204
2205 /* Find the largest max packet size of this or the previous
2206 * interval.
2207 */
2208 if (list_empty(&bw_table->interval_bw[i].endpoints))
2209 largest_mps = 0;
2210 else {
2211 struct xhci_virt_ep *virt_ep;
2212 struct list_head *ep_entry;
2213
2214 ep_entry = bw_table->interval_bw[i].endpoints.next;
2215 virt_ep = list_entry(ep_entry,
2216 struct xhci_virt_ep, bw_endpoint_list);
2217 /* Convert to blocks, rounding up */
2218 largest_mps = DIV_ROUND_UP(
2219 virt_ep->bw_info.max_packet_size,
2220 block_size);
2221 }
2222 if (largest_mps > packet_size)
2223 packet_size = largest_mps;
2224
2225 /* Use the larger overhead of this or the previous interval. */
2226 interval_overhead = xhci_get_largest_overhead(
2227 &bw_table->interval_bw[i]);
2228 if (interval_overhead > overhead)
2229 overhead = interval_overhead;
2230
2231 /* How many packets can we evenly distribute across
2232 * (1 << (i + 1)) possible scheduling opportunities?
2233 */
2234 packets_transmitted = packets_remaining >> (i + 1);
2235
2236 /* Add in the bandwidth used for those scheduled packets */
2237 bw_added = packets_transmitted * (overhead + packet_size);
2238
2239 /* How many packets do we have remaining to transmit? */
2240 packets_remaining = packets_remaining % (1 << (i + 1));
2241
2242 /* What largest max packet size should those packets have? */
2243 /* If we've transmitted all packets, don't carry over the
2244 * largest packet size.
2245 */
2246 if (packets_remaining == 0) {
2247 packet_size = 0;
2248 overhead = 0;
2249 } else if (packets_transmitted > 0) {
2250 /* Otherwise if we do have remaining packets, and we've
2251 * scheduled some packets in this interval, take the
2252 * largest max packet size from endpoints with this
2253 * interval.
2254 */
2255 packet_size = largest_mps;
2256 overhead = interval_overhead;
2257 }
2258 /* Otherwise carry over packet_size and overhead from the last
2259 * time we had a remainder.
2260 */
2261 bw_used += bw_added;
2262 if (bw_used > max_bandwidth) {
2263 xhci_warn(xhci, "Not enough bandwidth. "
2264 "Proposed: %u, Max: %u\n",
2265 bw_used, max_bandwidth);
2266 return -ENOMEM;
2267 }
2268 }
2269 /*
2270 * Ok, we know we have some packets left over after even-handedly
2271 * scheduling interval 15. We don't know which microframes they will
2272 * fit into, so we over-schedule and say they will be scheduled every
2273 * microframe.
2274 */
2275 if (packets_remaining > 0)
2276 bw_used += overhead + packet_size;
2277
2278 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2279 unsigned int port_index = virt_dev->real_port - 1;
2280
2281 /* OK, we're manipulating a HS device attached to a
2282 * root port bandwidth domain. Include the number of active TTs
2283 * in the bandwidth used.
2284 */
2285 bw_used += TT_HS_OVERHEAD *
2286 xhci->rh_bw[port_index].num_active_tts;
2287 }
2288
2289 xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2290 "Available: %u " "percent\n",
2291 bw_used, max_bandwidth, bw_reserved,
2292 (max_bandwidth - bw_used - bw_reserved) * 100 /
2293 max_bandwidth);
2294
2295 bw_used += bw_reserved;
2296 if (bw_used > max_bandwidth) {
2297 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2298 bw_used, max_bandwidth);
2299 return -ENOMEM;
2300 }
2301
2302 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002303 return 0;
2304}
2305
2306static bool xhci_is_async_ep(unsigned int ep_type)
2307{
2308 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2309 ep_type != ISOC_IN_EP &&
2310 ep_type != INT_IN_EP);
2311}
2312
Sarah Sharp2b698992011-09-13 16:41:13 -07002313static bool xhci_is_sync_in_ep(unsigned int ep_type)
2314{
Sarah Sharp363cfe82012-10-25 13:44:12 -07002315 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002316}
2317
2318static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2319{
2320 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2321
2322 if (ep_bw->ep_interval == 0)
2323 return SS_OVERHEAD_BURST +
2324 (ep_bw->mult * ep_bw->num_packets *
2325 (SS_OVERHEAD + mps));
2326 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2327 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2328 1 << ep_bw->ep_interval);
2329
2330}
2331
Sarah Sharp2e279802011-09-02 11:05:50 -07002332void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2333 struct xhci_bw_info *ep_bw,
2334 struct xhci_interval_bw_table *bw_table,
2335 struct usb_device *udev,
2336 struct xhci_virt_ep *virt_ep,
2337 struct xhci_tt_bw_info *tt_info)
2338{
2339 struct xhci_interval_bw *interval_bw;
2340 int normalized_interval;
2341
Sarah Sharp2b698992011-09-13 16:41:13 -07002342 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002343 return;
2344
Sarah Sharp2b698992011-09-13 16:41:13 -07002345 if (udev->speed == USB_SPEED_SUPER) {
2346 if (xhci_is_sync_in_ep(ep_bw->type))
2347 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2348 xhci_get_ss_bw_consumed(ep_bw);
2349 else
2350 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2351 xhci_get_ss_bw_consumed(ep_bw);
2352 return;
2353 }
2354
2355 /* SuperSpeed endpoints never get added to intervals in the table, so
2356 * this check is only valid for HS/FS/LS devices.
2357 */
2358 if (list_empty(&virt_ep->bw_endpoint_list))
2359 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002360 /* For LS/FS devices, we need to translate the interval expressed in
2361 * microframes to frames.
2362 */
2363 if (udev->speed == USB_SPEED_HIGH)
2364 normalized_interval = ep_bw->ep_interval;
2365 else
2366 normalized_interval = ep_bw->ep_interval - 3;
2367
2368 if (normalized_interval == 0)
2369 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2370 interval_bw = &bw_table->interval_bw[normalized_interval];
2371 interval_bw->num_packets -= ep_bw->num_packets;
2372 switch (udev->speed) {
2373 case USB_SPEED_LOW:
2374 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2375 break;
2376 case USB_SPEED_FULL:
2377 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2378 break;
2379 case USB_SPEED_HIGH:
2380 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2381 break;
2382 case USB_SPEED_SUPER:
2383 case USB_SPEED_UNKNOWN:
2384 case USB_SPEED_WIRELESS:
2385 /* Should never happen because only LS/FS/HS endpoints will get
2386 * added to the endpoint list.
2387 */
2388 return;
2389 }
2390 if (tt_info)
2391 tt_info->active_eps -= 1;
2392 list_del_init(&virt_ep->bw_endpoint_list);
2393}
2394
2395static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2396 struct xhci_bw_info *ep_bw,
2397 struct xhci_interval_bw_table *bw_table,
2398 struct usb_device *udev,
2399 struct xhci_virt_ep *virt_ep,
2400 struct xhci_tt_bw_info *tt_info)
2401{
2402 struct xhci_interval_bw *interval_bw;
2403 struct xhci_virt_ep *smaller_ep;
2404 int normalized_interval;
2405
2406 if (xhci_is_async_ep(ep_bw->type))
2407 return;
2408
Sarah Sharp2b698992011-09-13 16:41:13 -07002409 if (udev->speed == USB_SPEED_SUPER) {
2410 if (xhci_is_sync_in_ep(ep_bw->type))
2411 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2412 xhci_get_ss_bw_consumed(ep_bw);
2413 else
2414 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2415 xhci_get_ss_bw_consumed(ep_bw);
2416 return;
2417 }
2418
Sarah Sharp2e279802011-09-02 11:05:50 -07002419 /* For LS/FS devices, we need to translate the interval expressed in
2420 * microframes to frames.
2421 */
2422 if (udev->speed == USB_SPEED_HIGH)
2423 normalized_interval = ep_bw->ep_interval;
2424 else
2425 normalized_interval = ep_bw->ep_interval - 3;
2426
2427 if (normalized_interval == 0)
2428 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2429 interval_bw = &bw_table->interval_bw[normalized_interval];
2430 interval_bw->num_packets += ep_bw->num_packets;
2431 switch (udev->speed) {
2432 case USB_SPEED_LOW:
2433 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2434 break;
2435 case USB_SPEED_FULL:
2436 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2437 break;
2438 case USB_SPEED_HIGH:
2439 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2440 break;
2441 case USB_SPEED_SUPER:
2442 case USB_SPEED_UNKNOWN:
2443 case USB_SPEED_WIRELESS:
2444 /* Should never happen because only LS/FS/HS endpoints will get
2445 * added to the endpoint list.
2446 */
2447 return;
2448 }
2449
2450 if (tt_info)
2451 tt_info->active_eps += 1;
2452 /* Insert the endpoint into the list, largest max packet size first. */
2453 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2454 bw_endpoint_list) {
2455 if (ep_bw->max_packet_size >=
2456 smaller_ep->bw_info.max_packet_size) {
2457 /* Add the new ep before the smaller endpoint */
2458 list_add_tail(&virt_ep->bw_endpoint_list,
2459 &smaller_ep->bw_endpoint_list);
2460 return;
2461 }
2462 }
2463 /* Add the new endpoint at the end of the list. */
2464 list_add_tail(&virt_ep->bw_endpoint_list,
2465 &interval_bw->endpoints);
2466}
2467
2468void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2469 struct xhci_virt_device *virt_dev,
2470 int old_active_eps)
2471{
2472 struct xhci_root_port_bw_info *rh_bw_info;
2473 if (!virt_dev->tt_info)
2474 return;
2475
2476 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2477 if (old_active_eps == 0 &&
2478 virt_dev->tt_info->active_eps != 0) {
2479 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002480 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002481 } else if (old_active_eps != 0 &&
2482 virt_dev->tt_info->active_eps == 0) {
2483 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002484 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002485 }
2486}
2487
2488static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2489 struct xhci_virt_device *virt_dev,
2490 struct xhci_container_ctx *in_ctx)
2491{
2492 struct xhci_bw_info ep_bw_info[31];
2493 int i;
2494 struct xhci_input_control_ctx *ctrl_ctx;
2495 int old_active_eps = 0;
2496
Sarah Sharp2e279802011-09-02 11:05:50 -07002497 if (virt_dev->tt_info)
2498 old_active_eps = virt_dev->tt_info->active_eps;
2499
2500 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2501
2502 for (i = 0; i < 31; i++) {
2503 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2504 continue;
2505
2506 /* Make a copy of the BW info in case we need to revert this */
2507 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2508 sizeof(ep_bw_info[i]));
2509 /* Drop the endpoint from the interval table if the endpoint is
2510 * being dropped or changed.
2511 */
2512 if (EP_IS_DROPPED(ctrl_ctx, i))
2513 xhci_drop_ep_from_interval_table(xhci,
2514 &virt_dev->eps[i].bw_info,
2515 virt_dev->bw_table,
2516 virt_dev->udev,
2517 &virt_dev->eps[i],
2518 virt_dev->tt_info);
2519 }
2520 /* Overwrite the information stored in the endpoints' bw_info */
2521 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2522 for (i = 0; i < 31; i++) {
2523 /* Add any changed or added endpoints to the interval table */
2524 if (EP_IS_ADDED(ctrl_ctx, i))
2525 xhci_add_ep_to_interval_table(xhci,
2526 &virt_dev->eps[i].bw_info,
2527 virt_dev->bw_table,
2528 virt_dev->udev,
2529 &virt_dev->eps[i],
2530 virt_dev->tt_info);
2531 }
2532
2533 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2534 /* Ok, this fits in the bandwidth we have.
2535 * Update the number of active TTs.
2536 */
2537 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2538 return 0;
2539 }
2540
2541 /* We don't have enough bandwidth for this, revert the stored info. */
2542 for (i = 0; i < 31; i++) {
2543 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2544 continue;
2545
2546 /* Drop the new copies of any added or changed endpoints from
2547 * the interval table.
2548 */
2549 if (EP_IS_ADDED(ctrl_ctx, i)) {
2550 xhci_drop_ep_from_interval_table(xhci,
2551 &virt_dev->eps[i].bw_info,
2552 virt_dev->bw_table,
2553 virt_dev->udev,
2554 &virt_dev->eps[i],
2555 virt_dev->tt_info);
2556 }
2557 /* Revert the endpoint back to its old information */
2558 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2559 sizeof(ep_bw_info[i]));
2560 /* Add any changed or dropped endpoints back into the table */
2561 if (EP_IS_DROPPED(ctrl_ctx, i))
2562 xhci_add_ep_to_interval_table(xhci,
2563 &virt_dev->eps[i].bw_info,
2564 virt_dev->bw_table,
2565 virt_dev->udev,
2566 &virt_dev->eps[i],
2567 virt_dev->tt_info);
2568 }
2569 return -ENOMEM;
2570}
2571
2572
Sarah Sharpf2217e82009-08-07 14:04:43 -07002573/* Issue a configure endpoint command or evaluate context command
2574 * and wait for it to finish.
2575 */
2576static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002577 struct usb_device *udev,
2578 struct xhci_command *command,
2579 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002580{
2581 int ret;
2582 int timeleft;
2583 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002584 struct xhci_container_ctx *in_ctx;
2585 struct completion *cmd_completion;
Matt Evans28ccd292011-03-29 13:40:46 +11002586 u32 *cmd_status;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002587 struct xhci_virt_device *virt_dev;
Elric Fu75382342012-06-27 16:31:52 +08002588 union xhci_trb *cmd_trb;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002589
2590 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002591 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002592
Sarah Sharp750645f2011-09-02 11:05:43 -07002593 if (command)
2594 in_ctx = command->in_ctx;
2595 else
2596 in_ctx = virt_dev->in_ctx;
2597
2598 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2599 xhci_reserve_host_resources(xhci, in_ctx)) {
2600 spin_unlock_irqrestore(&xhci->lock, flags);
2601 xhci_warn(xhci, "Not enough host resources, "
2602 "active endpoint contexts = %u\n",
2603 xhci->num_active_eps);
2604 return -ENOMEM;
2605 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002606 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2607 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2608 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2609 xhci_free_host_resources(xhci, in_ctx);
2610 spin_unlock_irqrestore(&xhci->lock, flags);
2611 xhci_warn(xhci, "Not enough bandwidth\n");
2612 return -ENOMEM;
2613 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002614
2615 if (command) {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002616 cmd_completion = command->completion;
2617 cmd_status = &command->status;
Mathias Nymand134fa52013-08-30 18:25:49 +03002618 command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002619 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2620 } else {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002621 cmd_completion = &virt_dev->cmd_completion;
2622 cmd_status = &virt_dev->cmd_status;
2623 }
Andiry Xu1d680642010-03-12 17:10:04 +08002624 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002625
Mathias Nymand134fa52013-08-30 18:25:49 +03002626 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002627 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07002628 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2629 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002630 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07002631 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07002632 udev->slot_id);
2633 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08002634 if (command)
2635 list_del(&command->cmd_list);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002636 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2637 xhci_free_host_resources(xhci, in_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002638 spin_unlock_irqrestore(&xhci->lock, flags);
2639 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
2640 return -ENOMEM;
2641 }
2642 xhci_ring_cmd_db(xhci);
2643 spin_unlock_irqrestore(&xhci->lock, flags);
2644
2645 /* Wait for the configure endpoint command to complete */
2646 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07002647 cmd_completion,
Elric Fu75382342012-06-27 16:31:52 +08002648 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002649 if (timeleft <= 0) {
2650 xhci_warn(xhci, "%s while waiting for %s command\n",
2651 timeleft == 0 ? "Timeout" : "Signal",
2652 ctx_change == 0 ?
2653 "configure endpoint" :
2654 "evaluate context");
Elric Fu75382342012-06-27 16:31:52 +08002655 /* cancel the configure endpoint command */
2656 ret = xhci_cancel_cmd(xhci, command, cmd_trb);
2657 if (ret < 0)
2658 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002659 return -ETIME;
2660 }
2661
2662 if (!ctx_change)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002663 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2664 else
2665 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
2666
2667 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2668 spin_lock_irqsave(&xhci->lock, flags);
2669 /* If the command failed, remove the reserved resources.
2670 * Otherwise, clean up the estimate to include dropped eps.
2671 */
2672 if (ret)
2673 xhci_free_host_resources(xhci, in_ctx);
2674 else
2675 xhci_finish_resource_reservation(xhci, in_ctx);
2676 spin_unlock_irqrestore(&xhci->lock, flags);
2677 }
2678 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002679}
2680
Sarah Sharpf88ba782009-05-14 11:44:22 -07002681/* Called after one or more calls to xhci_add_endpoint() or
2682 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2683 * to call xhci_reset_bandwidth().
2684 *
2685 * Since we are in the middle of changing either configuration or
2686 * installing a new alt setting, the USB core won't allow URBs to be
2687 * enqueued for any endpoint on the old config or interface. Nothing
2688 * else should be touching the xhci->devs[slot_id] structure, so we
2689 * don't need to take the xhci->lock for manipulating that.
2690 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002691int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2692{
2693 int i;
2694 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002695 struct xhci_hcd *xhci;
2696 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002697 struct xhci_input_control_ctx *ctrl_ctx;
2698 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002699
Andiry Xu64927732010-10-14 07:22:45 -07002700 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002701 if (ret <= 0)
2702 return ret;
2703 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002704 if (xhci->xhc_state & XHCI_STATE_DYING)
2705 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002706
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002707 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002708 virt_dev = xhci->devs[udev->slot_id];
2709
2710 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07002711 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002712 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2713 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2714 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002715
2716 /* Don't issue the command if there's no endpoints to update. */
2717 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2718 ctrl_ctx->drop_flags == 0)
2719 return 0;
2720
Sarah Sharpf94e01862009-04-27 19:58:38 -07002721 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07002722 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2723 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002724 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002725
Sarah Sharp913a8a32009-09-04 10:53:13 -07002726 ret = xhci_configure_endpoint(xhci, udev, NULL,
2727 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002728 if (ret) {
2729 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002730 return ret;
2731 }
2732
2733 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07002734 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002735 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002736
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002737 /* Free any rings that were dropped, but not changed. */
2738 for (i = 1; i < 31; ++i) {
Matt Evans4819fef2011-06-01 13:01:07 +10002739 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2740 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002741 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2742 }
John Yound115b042009-07-27 12:05:15 -07002743 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002744 /*
2745 * Install any rings for completely new endpoints or changed endpoints,
2746 * and free or cache any old rings from changed endpoints.
2747 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002748 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002749 if (!virt_dev->eps[i].new_ring)
2750 continue;
2751 /* Only cache or free the old ring if it exists.
2752 * It may not if this is the first add of an endpoint.
2753 */
2754 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08002755 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002756 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002757 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2758 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002759 }
2760
Sarah Sharpf94e01862009-04-27 19:58:38 -07002761 return ret;
2762}
2763
2764void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2765{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002766 struct xhci_hcd *xhci;
2767 struct xhci_virt_device *virt_dev;
2768 int i, ret;
2769
Andiry Xu64927732010-10-14 07:22:45 -07002770 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002771 if (ret <= 0)
2772 return;
2773 xhci = hcd_to_xhci(hcd);
2774
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002775 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002776 virt_dev = xhci->devs[udev->slot_id];
2777 /* Free any rings allocated for added endpoints */
2778 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002779 if (virt_dev->eps[i].new_ring) {
2780 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2781 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002782 }
2783 }
John Yound115b042009-07-27 12:05:15 -07002784 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002785}
2786
Sarah Sharp5270b952009-09-04 10:53:11 -07002787static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002788 struct xhci_container_ctx *in_ctx,
2789 struct xhci_container_ctx *out_ctx,
2790 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002791{
2792 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002793 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002794 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2795 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002796 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002797 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002798
Sarah Sharp913a8a32009-09-04 10:53:13 -07002799 xhci_dbg(xhci, "Input Context:\n");
2800 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07002801}
2802
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002803static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002804 unsigned int slot_id, unsigned int ep_index,
2805 struct xhci_dequeue_state *deq_state)
2806{
2807 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002808 struct xhci_ep_ctx *ep_ctx;
2809 u32 added_ctxs;
2810 dma_addr_t addr;
2811
Sarah Sharp913a8a32009-09-04 10:53:13 -07002812 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2813 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002814 in_ctx = xhci->devs[slot_id]->in_ctx;
2815 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2816 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2817 deq_state->new_deq_ptr);
2818 if (addr == 0) {
2819 xhci_warn(xhci, "WARN Cannot submit config ep after "
2820 "reset ep command\n");
2821 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2822 deq_state->new_deq_seg,
2823 deq_state->new_deq_ptr);
2824 return;
2825 }
Matt Evans28ccd292011-03-29 13:40:46 +11002826 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002827
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002828 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002829 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
2830 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002831}
2832
Sarah Sharp82d10092009-08-07 14:04:52 -07002833void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002834 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07002835{
2836 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002837 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07002838
2839 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002840 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002841 /* We need to move the HW's dequeue pointer past this TD,
2842 * or it will attempt to resend it on the next doorbell ring.
2843 */
2844 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002845 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002846 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002847
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002848 /* HW with the reset endpoint quirk will use the saved dequeue state to
2849 * issue a configure endpoint command later.
2850 */
2851 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
2852 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002853 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002854 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002855 } else {
2856 /* Better hope no one uses the input context between now and the
2857 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002858 * XXX: No idea how this hardware will react when stream rings
2859 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002860 */
2861 xhci_dbg(xhci, "Setting up input context for "
2862 "configure endpoint command\n");
2863 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2864 ep_index, &deq_state);
2865 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002866}
2867
Sarah Sharpa1587d92009-07-27 12:03:15 -07002868/* Deal with stalled endpoints. The core should have sent the control message
2869 * to clear the halt condition. However, we need to make the xHCI hardware
2870 * reset its sequence number, since a device will expect a sequence number of
2871 * zero after the halt condition is cleared.
2872 * Context: in_interrupt
2873 */
2874void xhci_endpoint_reset(struct usb_hcd *hcd,
2875 struct usb_host_endpoint *ep)
2876{
2877 struct xhci_hcd *xhci;
2878 struct usb_device *udev;
2879 unsigned int ep_index;
2880 unsigned long flags;
2881 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002882 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002883
2884 xhci = hcd_to_xhci(hcd);
2885 udev = (struct usb_device *) ep->hcpriv;
2886 /* Called with a root hub endpoint (or an endpoint that wasn't added
2887 * with xhci_add_endpoint()
2888 */
2889 if (!ep->hcpriv)
2890 return;
2891 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002892 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2893 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002894 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
2895 ep->desc.bEndpointAddress);
2896 return;
2897 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002898 if (usb_endpoint_xfer_control(&ep->desc)) {
2899 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
2900 return;
2901 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07002902
2903 xhci_dbg(xhci, "Queueing reset endpoint command\n");
2904 spin_lock_irqsave(&xhci->lock, flags);
2905 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002906 /*
2907 * Can't change the ring dequeue pointer until it's transitioned to the
2908 * stopped state, which is only upon a successful reset endpoint
2909 * command. Better hope that last command worked!
2910 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002911 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002912 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
2913 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002914 xhci_ring_cmd_db(xhci);
2915 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07002916 virt_ep->stopped_td = NULL;
2917 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07002918 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002919 spin_unlock_irqrestore(&xhci->lock, flags);
2920
2921 if (ret)
2922 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
2923}
2924
Sarah Sharp8df75f42010-04-02 15:34:16 -07002925static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2926 struct usb_device *udev, struct usb_host_endpoint *ep,
2927 unsigned int slot_id)
2928{
2929 int ret;
2930 unsigned int ep_index;
2931 unsigned int ep_state;
2932
2933 if (!ep)
2934 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002935 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002936 if (ret <= 0)
2937 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04002938 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002939 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2940 " descriptor for ep 0x%x does not support streams\n",
2941 ep->desc.bEndpointAddress);
2942 return -EINVAL;
2943 }
2944
2945 ep_index = xhci_get_endpoint_index(&ep->desc);
2946 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2947 if (ep_state & EP_HAS_STREAMS ||
2948 ep_state & EP_GETTING_STREAMS) {
2949 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2950 "already has streams set up.\n",
2951 ep->desc.bEndpointAddress);
2952 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2953 "dynamic stream context array reallocation.\n");
2954 return -EINVAL;
2955 }
2956 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2957 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2958 "endpoint 0x%x; URBs are pending.\n",
2959 ep->desc.bEndpointAddress);
2960 return -EINVAL;
2961 }
2962 return 0;
2963}
2964
2965static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
2966 unsigned int *num_streams, unsigned int *num_stream_ctxs)
2967{
2968 unsigned int max_streams;
2969
2970 /* The stream context array size must be a power of two */
2971 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
2972 /*
2973 * Find out how many primary stream array entries the host controller
2974 * supports. Later we may use secondary stream arrays (similar to 2nd
2975 * level page entries), but that's an optional feature for xHCI host
2976 * controllers. xHCs must support at least 4 stream IDs.
2977 */
2978 max_streams = HCC_MAX_PSA(xhci->hcc_params);
2979 if (*num_stream_ctxs > max_streams) {
2980 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
2981 max_streams);
2982 *num_stream_ctxs = max_streams;
2983 *num_streams = max_streams;
2984 }
2985}
2986
2987/* Returns an error code if one of the endpoint already has streams.
2988 * This does not change any data structures, it only checks and gathers
2989 * information.
2990 */
2991static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
2992 struct usb_device *udev,
2993 struct usb_host_endpoint **eps, unsigned int num_eps,
2994 unsigned int *num_streams, u32 *changed_ep_bitmask)
2995{
Sarah Sharp8df75f42010-04-02 15:34:16 -07002996 unsigned int max_streams;
2997 unsigned int endpoint_flag;
2998 int i;
2999 int ret;
3000
3001 for (i = 0; i < num_eps; i++) {
3002 ret = xhci_check_streams_endpoint(xhci, udev,
3003 eps[i], udev->slot_id);
3004 if (ret < 0)
3005 return ret;
3006
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003007 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003008 if (max_streams < (*num_streams - 1)) {
3009 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3010 eps[i]->desc.bEndpointAddress,
3011 max_streams);
3012 *num_streams = max_streams+1;
3013 }
3014
3015 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3016 if (*changed_ep_bitmask & endpoint_flag)
3017 return -EINVAL;
3018 *changed_ep_bitmask |= endpoint_flag;
3019 }
3020 return 0;
3021}
3022
3023static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3024 struct usb_device *udev,
3025 struct usb_host_endpoint **eps, unsigned int num_eps)
3026{
3027 u32 changed_ep_bitmask = 0;
3028 unsigned int slot_id;
3029 unsigned int ep_index;
3030 unsigned int ep_state;
3031 int i;
3032
3033 slot_id = udev->slot_id;
3034 if (!xhci->devs[slot_id])
3035 return 0;
3036
3037 for (i = 0; i < num_eps; i++) {
3038 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3039 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3040 /* Are streams already being freed for the endpoint? */
3041 if (ep_state & EP_GETTING_NO_STREAMS) {
3042 xhci_warn(xhci, "WARN Can't disable streams for "
3043 "endpoint 0x%x\n, "
3044 "streams are being disabled already.",
3045 eps[i]->desc.bEndpointAddress);
3046 return 0;
3047 }
3048 /* Are there actually any streams to free? */
3049 if (!(ep_state & EP_HAS_STREAMS) &&
3050 !(ep_state & EP_GETTING_STREAMS)) {
3051 xhci_warn(xhci, "WARN Can't disable streams for "
3052 "endpoint 0x%x\n, "
3053 "streams are already disabled!",
3054 eps[i]->desc.bEndpointAddress);
3055 xhci_warn(xhci, "WARN xhci_free_streams() called "
3056 "with non-streams endpoint\n");
3057 return 0;
3058 }
3059 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3060 }
3061 return changed_ep_bitmask;
3062}
3063
3064/*
3065 * The USB device drivers use this function (though the HCD interface in USB
3066 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3067 * coordinate mass storage command queueing across multiple endpoints (basically
3068 * a stream ID == a task ID).
3069 *
3070 * Setting up streams involves allocating the same size stream context array
3071 * for each endpoint and issuing a configure endpoint command for all endpoints.
3072 *
3073 * Don't allow the call to succeed if one endpoint only supports one stream
3074 * (which means it doesn't support streams at all).
3075 *
3076 * Drivers may get less stream IDs than they asked for, if the host controller
3077 * hardware or endpoints claim they can't support the number of requested
3078 * stream IDs.
3079 */
3080int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3081 struct usb_host_endpoint **eps, unsigned int num_eps,
3082 unsigned int num_streams, gfp_t mem_flags)
3083{
3084 int i, ret;
3085 struct xhci_hcd *xhci;
3086 struct xhci_virt_device *vdev;
3087 struct xhci_command *config_cmd;
3088 unsigned int ep_index;
3089 unsigned int num_stream_ctxs;
3090 unsigned long flags;
3091 u32 changed_ep_bitmask = 0;
3092
3093 if (!eps)
3094 return -EINVAL;
3095
3096 /* Add one to the number of streams requested to account for
3097 * stream 0 that is reserved for xHCI usage.
3098 */
3099 num_streams += 1;
3100 xhci = hcd_to_xhci(hcd);
3101 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3102 num_streams);
3103
3104 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3105 if (!config_cmd) {
3106 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3107 return -ENOMEM;
3108 }
3109
3110 /* Check to make sure all endpoints are not already configured for
3111 * streams. While we're at it, find the maximum number of streams that
3112 * all the endpoints will support and check for duplicate endpoints.
3113 */
3114 spin_lock_irqsave(&xhci->lock, flags);
3115 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3116 num_eps, &num_streams, &changed_ep_bitmask);
3117 if (ret < 0) {
3118 xhci_free_command(xhci, config_cmd);
3119 spin_unlock_irqrestore(&xhci->lock, flags);
3120 return ret;
3121 }
3122 if (num_streams <= 1) {
3123 xhci_warn(xhci, "WARN: endpoints can't handle "
3124 "more than one stream.\n");
3125 xhci_free_command(xhci, config_cmd);
3126 spin_unlock_irqrestore(&xhci->lock, flags);
3127 return -EINVAL;
3128 }
3129 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003130 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003131 * xhci_urb_enqueue() will reject all URBs.
3132 */
3133 for (i = 0; i < num_eps; i++) {
3134 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3135 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3136 }
3137 spin_unlock_irqrestore(&xhci->lock, flags);
3138
3139 /* Setup internal data structures and allocate HW data structures for
3140 * streams (but don't install the HW structures in the input context
3141 * until we're sure all memory allocation succeeded).
3142 */
3143 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3144 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3145 num_stream_ctxs, num_streams);
3146
3147 for (i = 0; i < num_eps; i++) {
3148 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3149 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3150 num_stream_ctxs,
3151 num_streams, mem_flags);
3152 if (!vdev->eps[ep_index].stream_info)
3153 goto cleanup;
3154 /* Set maxPstreams in endpoint context and update deq ptr to
3155 * point to stream context array. FIXME
3156 */
3157 }
3158
3159 /* Set up the input context for a configure endpoint command. */
3160 for (i = 0; i < num_eps; i++) {
3161 struct xhci_ep_ctx *ep_ctx;
3162
3163 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3164 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3165
3166 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3167 vdev->out_ctx, ep_index);
3168 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3169 vdev->eps[ep_index].stream_info);
3170 }
3171 /* Tell the HW to drop its old copy of the endpoint context info
3172 * and add the updated copy from the input context.
3173 */
3174 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
3175 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
3176
3177 /* Issue and wait for the configure endpoint command */
3178 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3179 false, false);
3180
3181 /* xHC rejected the configure endpoint command for some reason, so we
3182 * leave the old ring intact and free our internal streams data
3183 * structure.
3184 */
3185 if (ret < 0)
3186 goto cleanup;
3187
3188 spin_lock_irqsave(&xhci->lock, flags);
3189 for (i = 0; i < num_eps; i++) {
3190 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3191 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3192 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3193 udev->slot_id, ep_index);
3194 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3195 }
3196 xhci_free_command(xhci, config_cmd);
3197 spin_unlock_irqrestore(&xhci->lock, flags);
3198
3199 /* Subtract 1 for stream 0, which drivers can't use */
3200 return num_streams - 1;
3201
3202cleanup:
3203 /* If it didn't work, free the streams! */
3204 for (i = 0; i < num_eps; i++) {
3205 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3206 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003207 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003208 /* FIXME Unset maxPstreams in endpoint context and
3209 * update deq ptr to point to normal string ring.
3210 */
3211 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3212 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3213 xhci_endpoint_zero(xhci, vdev, eps[i]);
3214 }
3215 xhci_free_command(xhci, config_cmd);
3216 return -ENOMEM;
3217}
3218
3219/* Transition the endpoint from using streams to being a "normal" endpoint
3220 * without streams.
3221 *
3222 * Modify the endpoint context state, submit a configure endpoint command,
3223 * and free all endpoint rings for streams if that completes successfully.
3224 */
3225int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3226 struct usb_host_endpoint **eps, unsigned int num_eps,
3227 gfp_t mem_flags)
3228{
3229 int i, ret;
3230 struct xhci_hcd *xhci;
3231 struct xhci_virt_device *vdev;
3232 struct xhci_command *command;
3233 unsigned int ep_index;
3234 unsigned long flags;
3235 u32 changed_ep_bitmask;
3236
3237 xhci = hcd_to_xhci(hcd);
3238 vdev = xhci->devs[udev->slot_id];
3239
3240 /* Set up a configure endpoint command to remove the streams rings */
3241 spin_lock_irqsave(&xhci->lock, flags);
3242 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3243 udev, eps, num_eps);
3244 if (changed_ep_bitmask == 0) {
3245 spin_unlock_irqrestore(&xhci->lock, flags);
3246 return -EINVAL;
3247 }
3248
3249 /* Use the xhci_command structure from the first endpoint. We may have
3250 * allocated too many, but the driver may call xhci_free_streams() for
3251 * each endpoint it grouped into one call to xhci_alloc_streams().
3252 */
3253 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3254 command = vdev->eps[ep_index].stream_info->free_streams_command;
3255 for (i = 0; i < num_eps; i++) {
3256 struct xhci_ep_ctx *ep_ctx;
3257
3258 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3259 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3260 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3261 EP_GETTING_NO_STREAMS;
3262
3263 xhci_endpoint_copy(xhci, command->in_ctx,
3264 vdev->out_ctx, ep_index);
3265 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3266 &vdev->eps[ep_index]);
3267 }
3268 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3269 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
3270 spin_unlock_irqrestore(&xhci->lock, flags);
3271
3272 /* Issue and wait for the configure endpoint command,
3273 * which must succeed.
3274 */
3275 ret = xhci_configure_endpoint(xhci, udev, command,
3276 false, true);
3277
3278 /* xHC rejected the configure endpoint command for some reason, so we
3279 * leave the streams rings intact.
3280 */
3281 if (ret < 0)
3282 return ret;
3283
3284 spin_lock_irqsave(&xhci->lock, flags);
3285 for (i = 0; i < num_eps; i++) {
3286 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3287 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003288 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003289 /* FIXME Unset maxPstreams in endpoint context and
3290 * update deq ptr to point to normal string ring.
3291 */
3292 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3293 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3294 }
3295 spin_unlock_irqrestore(&xhci->lock, flags);
3296
3297 return 0;
3298}
3299
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003300/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003301 * Deletes endpoint resources for endpoints that were active before a Reset
3302 * Device command, or a Disable Slot command. The Reset Device command leaves
3303 * the control endpoint intact, whereas the Disable Slot command deletes it.
3304 *
3305 * Must be called with xhci->lock held.
3306 */
3307void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3308 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3309{
3310 int i;
3311 unsigned int num_dropped_eps = 0;
3312 unsigned int drop_flags = 0;
3313
3314 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3315 if (virt_dev->eps[i].ring) {
3316 drop_flags |= 1 << i;
3317 num_dropped_eps++;
3318 }
3319 }
3320 xhci->num_active_eps -= num_dropped_eps;
3321 if (num_dropped_eps)
3322 xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
3323 "%u now active.\n",
3324 num_dropped_eps, drop_flags,
3325 xhci->num_active_eps);
3326}
3327
3328/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003329 * This submits a Reset Device Command, which will set the device state to 0,
3330 * set the device address to 0, and disable all the endpoints except the default
3331 * control endpoint. The USB core should come back and call
3332 * xhci_address_device(), and then re-set up the configuration. If this is
3333 * called because of a usb_reset_and_verify_device(), then the old alternate
3334 * settings will be re-installed through the normal bandwidth allocation
3335 * functions.
3336 *
3337 * Wait for the Reset Device command to finish. Remove all structures
3338 * associated with the endpoints that were disabled. Clear the input device
3339 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003340 *
3341 * If the virt_dev to be reset does not exist or does not match the udev,
3342 * it means the device is lost, possibly due to the xHC restore error and
3343 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3344 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003345 */
Andiry Xuf0615c42010-10-14 07:22:48 -07003346int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003347{
3348 int ret, i;
3349 unsigned long flags;
3350 struct xhci_hcd *xhci;
3351 unsigned int slot_id;
3352 struct xhci_virt_device *virt_dev;
3353 struct xhci_command *reset_device_cmd;
3354 int timeleft;
3355 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003356 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003357 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003358
Andiry Xuf0615c42010-10-14 07:22:48 -07003359 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003360 if (ret <= 0)
3361 return ret;
3362 xhci = hcd_to_xhci(hcd);
3363 slot_id = udev->slot_id;
3364 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003365 if (!virt_dev) {
3366 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3367 "not exist. Re-allocate the device\n", slot_id);
3368 ret = xhci_alloc_dev(hcd, udev);
3369 if (ret == 1)
3370 return 0;
3371 else
3372 return -EINVAL;
3373 }
3374
3375 if (virt_dev->udev != udev) {
3376 /* If the virt_dev and the udev does not match, this virt_dev
3377 * may belong to another udev.
3378 * Re-allocate the device.
3379 */
3380 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3381 "not match the udev. Re-allocate the device\n",
3382 slot_id);
3383 ret = xhci_alloc_dev(hcd, udev);
3384 if (ret == 1)
3385 return 0;
3386 else
3387 return -EINVAL;
3388 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003389
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003390 /* If device is not setup, there is no point in resetting it */
3391 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3392 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3393 SLOT_STATE_DISABLED)
3394 return 0;
3395
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003396 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3397 /* Allocate the command structure that holds the struct completion.
3398 * Assume we're in process context, since the normal device reset
3399 * process has to wait for the device anyway. Storage devices are
3400 * reset as part of error handling, so use GFP_NOIO instead of
3401 * GFP_KERNEL.
3402 */
3403 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3404 if (!reset_device_cmd) {
3405 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3406 return -ENOMEM;
3407 }
3408
3409 /* Attempt to submit the Reset Device command to the command ring */
3410 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymand134fa52013-08-30 18:25:49 +03003411 reset_device_cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003412
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003413 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3414 ret = xhci_queue_reset_device(xhci, slot_id);
3415 if (ret) {
3416 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3417 list_del(&reset_device_cmd->cmd_list);
3418 spin_unlock_irqrestore(&xhci->lock, flags);
3419 goto command_cleanup;
3420 }
3421 xhci_ring_cmd_db(xhci);
3422 spin_unlock_irqrestore(&xhci->lock, flags);
3423
3424 /* Wait for the Reset Device command to finish */
3425 timeleft = wait_for_completion_interruptible_timeout(
3426 reset_device_cmd->completion,
3427 USB_CTRL_SET_TIMEOUT);
3428 if (timeleft <= 0) {
3429 xhci_warn(xhci, "%s while waiting for reset device command\n",
3430 timeleft == 0 ? "Timeout" : "Signal");
3431 spin_lock_irqsave(&xhci->lock, flags);
3432 /* The timeout might have raced with the event ring handler, so
3433 * only delete from the list if the item isn't poisoned.
3434 */
3435 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3436 list_del(&reset_device_cmd->cmd_list);
3437 spin_unlock_irqrestore(&xhci->lock, flags);
3438 ret = -ETIME;
3439 goto command_cleanup;
3440 }
3441
3442 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3443 * unless we tried to reset a slot ID that wasn't enabled,
3444 * or the device wasn't in the addressed or configured state.
3445 */
3446 ret = reset_device_cmd->status;
3447 switch (ret) {
3448 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3449 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
3450 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
3451 slot_id,
3452 xhci_get_slot_state(xhci, virt_dev->out_ctx));
3453 xhci_info(xhci, "Not freeing device rings.\n");
3454 /* Don't treat this as an error. May change my mind later. */
3455 ret = 0;
3456 goto command_cleanup;
3457 case COMP_SUCCESS:
3458 xhci_dbg(xhci, "Successful reset device command.\n");
3459 break;
3460 default:
3461 if (xhci_is_vendor_info_code(xhci, ret))
3462 break;
3463 xhci_warn(xhci, "Unknown completion code %u for "
3464 "reset device command.\n", ret);
3465 ret = -EINVAL;
3466 goto command_cleanup;
3467 }
3468
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003469 /* Free up host controller endpoint resources */
3470 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3471 spin_lock_irqsave(&xhci->lock, flags);
3472 /* Don't delete the default control endpoint resources */
3473 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3474 spin_unlock_irqrestore(&xhci->lock, flags);
3475 }
3476
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003477 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3478 last_freed_endpoint = 1;
3479 for (i = 1; i < 31; ++i) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003480 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3481
3482 if (ep->ep_state & EP_HAS_STREAMS) {
3483 xhci_free_stream_info(xhci, ep->stream_info);
3484 ep->stream_info = NULL;
3485 ep->ep_state &= ~EP_HAS_STREAMS;
3486 }
3487
3488 if (ep->ring) {
3489 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3490 last_freed_endpoint = i;
3491 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003492 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3493 xhci_drop_ep_from_interval_table(xhci,
3494 &virt_dev->eps[i].bw_info,
3495 virt_dev->bw_table,
3496 udev,
3497 &virt_dev->eps[i],
3498 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003499 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003500 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003501 /* If necessary, update the number of active TTs on this root port */
3502 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3503
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003504 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3505 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3506 ret = 0;
3507
3508command_cleanup:
3509 xhci_free_command(xhci, reset_device_cmd);
3510 return ret;
3511}
3512
3513/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003514 * At this point, the struct usb_device is about to go away, the device has
3515 * disconnected, and all traffic has been stopped and the endpoints have been
3516 * disabled. Free any HC data structures associated with that device.
3517 */
3518void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3519{
3520 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003521 struct xhci_virt_device *virt_dev;
Shawn Nematbakhsh8d1c1a32013-08-19 10:36:13 -07003522 struct device *dev = hcd->self.controller;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003523 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003524 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07003525 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003526
Shawn Nematbakhsh8d1c1a32013-08-19 10:36:13 -07003527#ifndef CONFIG_USB_DEFAULT_PERSIST
3528 /*
3529 * We called pm_runtime_get_noresume when the device was attached.
3530 * Decrement the counter here to allow controller to runtime suspend
3531 * if no devices remain.
3532 */
3533 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3534 pm_runtime_put_noidle(dev);
3535#endif
3536
Andiry Xu64927732010-10-14 07:22:45 -07003537 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003538 /* If the host is halted due to driver unload, we still need to free the
3539 * device.
3540 */
3541 if (ret <= 0 && ret != -ENODEV)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003542 return;
Andiry Xu64927732010-10-14 07:22:45 -07003543
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003544 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003545
3546 /* Stop any wayward timer functions (which may grab the lock) */
3547 for (i = 0; i < 31; ++i) {
3548 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3549 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3550 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003551
Andiry Xu65580b432011-09-23 14:19:52 -07003552 if (udev->usb2_hw_lpm_enabled) {
3553 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3554 udev->usb2_hw_lpm_enabled = 0;
3555 }
3556
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003557 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003558 /* Don't disable the slot if the host controller is dead. */
3559 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003560 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3561 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003562 xhci_free_virt_device(xhci, udev->slot_id);
3563 spin_unlock_irqrestore(&xhci->lock, flags);
3564 return;
3565 }
3566
Sarah Sharp23e3be12009-04-29 19:05:20 -07003567 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003568 spin_unlock_irqrestore(&xhci->lock, flags);
3569 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3570 return;
3571 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003572 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003573 spin_unlock_irqrestore(&xhci->lock, flags);
3574 /*
3575 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07003576 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003577 */
3578}
3579
3580/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003581 * Checks if we have enough host controller resources for the default control
3582 * endpoint.
3583 *
3584 * Must be called with xhci->lock held.
3585 */
3586static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3587{
3588 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3589 xhci_dbg(xhci, "Not enough ep ctxs: "
3590 "%u active, need to add 1, limit is %u.\n",
3591 xhci->num_active_eps, xhci->limit_active_eps);
3592 return -ENOMEM;
3593 }
3594 xhci->num_active_eps += 1;
3595 xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
3596 xhci->num_active_eps);
3597 return 0;
3598}
3599
3600
3601/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003602 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3603 * timed out, or allocating memory failed. Returns 1 on success.
3604 */
3605int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3606{
3607 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Shawn Nematbakhsh8d1c1a32013-08-19 10:36:13 -07003608 struct device *dev = hcd->self.controller;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003609 unsigned long flags;
3610 int timeleft;
3611 int ret;
Elric Fu75382342012-06-27 16:31:52 +08003612 union xhci_trb *cmd_trb;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003613
3614 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymand134fa52013-08-30 18:25:49 +03003615 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Sarah Sharp23e3be12009-04-29 19:05:20 -07003616 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003617 if (ret) {
3618 spin_unlock_irqrestore(&xhci->lock, flags);
3619 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3620 return 0;
3621 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003622 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003623 spin_unlock_irqrestore(&xhci->lock, flags);
3624
3625 /* XXX: how much time for xHC slot assignment? */
3626 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
Elric Fu75382342012-06-27 16:31:52 +08003627 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003628 if (timeleft <= 0) {
3629 xhci_warn(xhci, "%s while waiting for a slot\n",
3630 timeleft == 0 ? "Timeout" : "Signal");
Elric Fu75382342012-06-27 16:31:52 +08003631 /* cancel the enable slot request */
3632 return xhci_cancel_cmd(xhci, NULL, cmd_trb);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003633 }
3634
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003635 if (!xhci->slot_id) {
3636 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003637 return 0;
3638 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003639
3640 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3641 spin_lock_irqsave(&xhci->lock, flags);
3642 ret = xhci_reserve_host_control_ep_resources(xhci);
3643 if (ret) {
3644 spin_unlock_irqrestore(&xhci->lock, flags);
3645 xhci_warn(xhci, "Not enough host resources, "
3646 "active endpoint contexts = %u\n",
3647 xhci->num_active_eps);
3648 goto disable_slot;
3649 }
3650 spin_unlock_irqrestore(&xhci->lock, flags);
3651 }
3652 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003653 * xhci_discover_or_reset_device(), which may be called as part of
3654 * mass storage driver error handling.
3655 */
3656 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003657 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003658 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003659 }
3660 udev->slot_id = xhci->slot_id;
Shawn Nematbakhsh8d1c1a32013-08-19 10:36:13 -07003661
3662#ifndef CONFIG_USB_DEFAULT_PERSIST
3663 /*
3664 * If resetting upon resume, we can't put the controller into runtime
3665 * suspend if there is a device attached.
3666 */
3667 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3668 pm_runtime_get_noresume(dev);
3669#endif
3670
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003671 /* Is this a LS or FS device under a HS hub? */
3672 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003673 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003674
3675disable_slot:
3676 /* Disable slot, if we can do it without mem alloc */
3677 spin_lock_irqsave(&xhci->lock, flags);
3678 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3679 xhci_ring_cmd_db(xhci);
3680 spin_unlock_irqrestore(&xhci->lock, flags);
3681 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003682}
3683
3684/*
3685 * Issue an Address Device command (which will issue a SetAddress request to
3686 * the device).
3687 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3688 * we should only issue and wait on one address command at the same time.
3689 *
3690 * We add one to the device address issued by the hardware because the USB core
3691 * uses address 1 for the root hubs (even though they're not really devices).
3692 */
3693int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3694{
3695 unsigned long flags;
3696 int timeleft;
3697 struct xhci_virt_device *virt_dev;
3698 int ret = 0;
3699 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003700 struct xhci_slot_ctx *slot_ctx;
3701 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003702 u64 temp_64;
Elric Fu75382342012-06-27 16:31:52 +08003703 union xhci_trb *cmd_trb;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003704
3705 if (!udev->slot_id) {
3706 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
3707 return -EINVAL;
3708 }
3709
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003710 virt_dev = xhci->devs[udev->slot_id];
3711
Matt Evans7ed603e2011-03-29 13:40:56 +11003712 if (WARN_ON(!virt_dev)) {
3713 /*
3714 * In plug/unplug torture test with an NEC controller,
3715 * a zero-dereference was observed once due to virt_dev = 0.
3716 * Print useful debug rather than crash if it is observed again!
3717 */
3718 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3719 udev->slot_id);
3720 return -EINVAL;
3721 }
3722
Andiry Xuf0615c42010-10-14 07:22:48 -07003723 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
3724 /*
3725 * If this is the first Set Address since device plug-in or
3726 * virt_device realloaction after a resume with an xHCI power loss,
3727 * then set up the slot context.
3728 */
3729 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003730 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003731 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003732 else
3733 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003734 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3735 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3736 ctrl_ctx->drop_flags = 0;
3737
Sarah Sharp66e49d82009-07-27 12:03:46 -07003738 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003739 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003740
Sarah Sharpf88ba782009-05-14 11:44:22 -07003741 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymand134fa52013-08-30 18:25:49 +03003742 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
John Yound115b042009-07-27 12:05:15 -07003743 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3744 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003745 if (ret) {
3746 spin_unlock_irqrestore(&xhci->lock, flags);
3747 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3748 return ret;
3749 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003750 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003751 spin_unlock_irqrestore(&xhci->lock, flags);
3752
3753 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3754 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
Elric Fu75382342012-06-27 16:31:52 +08003755 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003756 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3757 * the SetAddress() "recovery interval" required by USB and aborting the
3758 * command on a timeout.
3759 */
3760 if (timeleft <= 0) {
Andiry Xucd681762011-09-23 14:19:55 -07003761 xhci_warn(xhci, "%s while waiting for address device command\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003762 timeleft == 0 ? "Timeout" : "Signal");
Elric Fu75382342012-06-27 16:31:52 +08003763 /* cancel the address device command */
3764 ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
3765 if (ret < 0)
3766 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003767 return -ETIME;
3768 }
3769
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003770 switch (virt_dev->cmd_status) {
3771 case COMP_CTX_STATE:
3772 case COMP_EBADSLT:
3773 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3774 udev->slot_id);
3775 ret = -EINVAL;
3776 break;
3777 case COMP_TX_ERR:
3778 dev_warn(&udev->dev, "Device not responding to set address.\n");
3779 ret = -EPROTO;
3780 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08003781 case COMP_DEV_ERR:
3782 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3783 "device command.\n");
3784 ret = -ENODEV;
3785 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003786 case COMP_SUCCESS:
3787 xhci_dbg(xhci, "Successful Address Device command\n");
3788 break;
3789 default:
3790 xhci_err(xhci, "ERROR: unexpected command completion "
3791 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07003792 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003793 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003794 ret = -EINVAL;
3795 break;
3796 }
3797 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003798 return ret;
3799 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07003800 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
3801 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
3802 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Matt Evans28ccd292011-03-29 13:40:46 +11003803 udev->slot_id,
3804 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3805 (unsigned long long)
3806 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003807 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07003808 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003809 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003810 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003811 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003812 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003813 /*
3814 * USB core uses address 1 for the roothubs, so we add one to the
3815 * address given back to us by the HC.
3816 */
John Yound115b042009-07-27 12:05:15 -07003817 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003818 /* Use kernel assigned address for devices; store xHC assigned
3819 * address locally. */
Matt Evans28ccd292011-03-29 13:40:46 +11003820 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3821 + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07003822 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003823 ctrl_ctx->add_flags = 0;
3824 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003825
Andiry Xuc8d4af82010-10-14 07:22:51 -07003826 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003827
3828 return 0;
3829}
3830
Andiry Xu95743232011-09-23 14:19:51 -07003831#ifdef CONFIG_USB_SUSPEND
3832
3833/* BESL to HIRD Encoding array for USB2 LPM */
3834static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3835 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3836
3837/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08003838static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
3839 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07003840{
Andiry Xuf99298b2011-12-12 16:45:28 +08003841 int u2del, besl, besl_host;
3842 int besl_device = 0;
3843 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07003844
Andiry Xuf99298b2011-12-12 16:45:28 +08003845 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3846 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
3847
3848 if (field & USB_BESL_SUPPORT) {
3849 for (besl_host = 0; besl_host < 16; besl_host++) {
3850 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07003851 break;
3852 }
Andiry Xuf99298b2011-12-12 16:45:28 +08003853 /* Use baseline BESL value as default */
3854 if (field & USB_BESL_BASELINE_VALID)
3855 besl_device = USB_GET_BESL_BASELINE(field);
3856 else if (field & USB_BESL_DEEP_VALID)
3857 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07003858 } else {
3859 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08003860 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07003861 else
Andiry Xuf99298b2011-12-12 16:45:28 +08003862 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07003863 }
3864
Andiry Xuf99298b2011-12-12 16:45:28 +08003865 besl = besl_host + besl_device;
3866 if (besl > 15)
3867 besl = 15;
3868
3869 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07003870}
3871
3872static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
3873 struct usb_device *udev)
3874{
3875 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3876 struct dev_info *dev_info;
3877 __le32 __iomem **port_array;
3878 __le32 __iomem *addr, *pm_addr;
3879 u32 temp, dev_id;
3880 unsigned int port_num;
3881 unsigned long flags;
Andiry Xuf99298b2011-12-12 16:45:28 +08003882 int hird;
Andiry Xu95743232011-09-23 14:19:51 -07003883 int ret;
3884
3885 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
3886 !udev->lpm_capable)
3887 return -EINVAL;
3888
3889 /* we only support lpm for non-hub device connected to root hub yet */
3890 if (!udev->parent || udev->parent->parent ||
3891 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
3892 return -EINVAL;
3893
3894 spin_lock_irqsave(&xhci->lock, flags);
3895
3896 /* Look for devices in lpm_failed_devs list */
3897 dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
3898 le16_to_cpu(udev->descriptor.idProduct);
3899 list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
3900 if (dev_info->dev_id == dev_id) {
3901 ret = -EINVAL;
3902 goto finish;
3903 }
3904 }
3905
3906 port_array = xhci->usb2_ports;
3907 port_num = udev->portnum - 1;
3908
3909 if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
3910 xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
3911 ret = -EINVAL;
3912 goto finish;
3913 }
3914
3915 /*
3916 * Test USB 2.0 software LPM.
3917 * FIXME: some xHCI 1.0 hosts may implement a new register to set up
3918 * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
3919 * in the June 2011 errata release.
3920 */
3921 xhci_dbg(xhci, "test port %d software LPM\n", port_num);
3922 /*
3923 * Set L1 Device Slot and HIRD/BESL.
3924 * Check device's USB 2.0 extension descriptor to determine whether
3925 * HIRD or BESL shoule be used. See USB2.0 LPM errata.
3926 */
3927 pm_addr = port_array[port_num] + 1;
Andiry Xuf99298b2011-12-12 16:45:28 +08003928 hird = xhci_calculate_hird_besl(xhci, udev);
Andiry Xu95743232011-09-23 14:19:51 -07003929 temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
3930 xhci_writel(xhci, temp, pm_addr);
Pavankumar Kondetibd348b62012-06-12 13:38:59 +05303931 if (xhci->quirks & XHCI_PORTSC_DELAY)
3932 ndelay(100);
Andiry Xu95743232011-09-23 14:19:51 -07003933
3934 /* Set port link state to U2(L1) */
3935 addr = port_array[port_num];
3936 xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
3937
3938 /* wait for ACK */
3939 spin_unlock_irqrestore(&xhci->lock, flags);
3940 msleep(10);
3941 spin_lock_irqsave(&xhci->lock, flags);
3942
3943 /* Check L1 Status */
3944 ret = handshake(xhci, pm_addr, PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
3945 if (ret != -ETIMEDOUT) {
3946 /* enter L1 successfully */
3947 temp = xhci_readl(xhci, addr);
3948 xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
3949 port_num, temp);
3950 ret = 0;
3951 } else {
3952 temp = xhci_readl(xhci, pm_addr);
3953 xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
3954 port_num, temp & PORT_L1S_MASK);
3955 ret = -EINVAL;
3956 }
3957
3958 /* Resume the port */
3959 xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
3960
3961 spin_unlock_irqrestore(&xhci->lock, flags);
3962 msleep(10);
3963 spin_lock_irqsave(&xhci->lock, flags);
3964
3965 /* Clear PLC */
3966 xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
3967
3968 /* Check PORTSC to make sure the device is in the right state */
3969 if (!ret) {
3970 temp = xhci_readl(xhci, addr);
3971 xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
3972 if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
3973 (temp & PORT_PLS_MASK) != XDEV_U0) {
3974 xhci_dbg(xhci, "port L1 resume fail\n");
3975 ret = -EINVAL;
3976 }
3977 }
3978
3979 if (ret) {
3980 /* Insert dev to lpm_failed_devs list */
3981 xhci_warn(xhci, "device LPM test failed, may disconnect and "
3982 "re-enumerate\n");
3983 dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
3984 if (!dev_info) {
3985 ret = -ENOMEM;
3986 goto finish;
3987 }
3988 dev_info->dev_id = dev_id;
3989 INIT_LIST_HEAD(&dev_info->list);
3990 list_add(&dev_info->list, &xhci->lpm_failed_devs);
3991 } else {
3992 xhci_ring_device(xhci, udev->slot_id);
3993 }
3994
3995finish:
3996 spin_unlock_irqrestore(&xhci->lock, flags);
3997 return ret;
3998}
3999
Andiry Xu65580b432011-09-23 14:19:52 -07004000int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4001 struct usb_device *udev, int enable)
4002{
4003 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4004 __le32 __iomem **port_array;
4005 __le32 __iomem *pm_addr;
4006 u32 temp;
4007 unsigned int port_num;
4008 unsigned long flags;
Andiry Xuf99298b2011-12-12 16:45:28 +08004009 int hird;
Pavankumar Kondetibd348b62012-06-12 13:38:59 +05304010 bool delay;
Andiry Xu65580b432011-09-23 14:19:52 -07004011
4012 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
4013 !udev->lpm_capable)
4014 return -EPERM;
4015
4016 if (!udev->parent || udev->parent->parent ||
4017 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4018 return -EPERM;
4019
4020 if (udev->usb2_hw_lpm_capable != 1)
4021 return -EPERM;
4022
Pavankumar Kondetibd348b62012-06-12 13:38:59 +05304023 if (xhci->quirks & XHCI_PORTSC_DELAY)
4024 delay = true;
4025
Andiry Xu65580b432011-09-23 14:19:52 -07004026 spin_lock_irqsave(&xhci->lock, flags);
4027
4028 port_array = xhci->usb2_ports;
4029 port_num = udev->portnum - 1;
4030 pm_addr = port_array[port_num] + 1;
4031 temp = xhci_readl(xhci, pm_addr);
4032
4033 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4034 enable ? "enable" : "disable", port_num);
4035
Andiry Xuf99298b2011-12-12 16:45:28 +08004036 hird = xhci_calculate_hird_besl(xhci, udev);
Andiry Xu65580b432011-09-23 14:19:52 -07004037
4038 if (enable) {
4039 temp &= ~PORT_HIRD_MASK;
4040 temp |= PORT_HIRD(hird) | PORT_RWE;
4041 xhci_writel(xhci, temp, pm_addr);
Pavankumar Kondetibd348b62012-06-12 13:38:59 +05304042 if (delay)
4043 ndelay(100);
Andiry Xu65580b432011-09-23 14:19:52 -07004044 temp = xhci_readl(xhci, pm_addr);
4045 temp |= PORT_HLE;
4046 xhci_writel(xhci, temp, pm_addr);
Pavankumar Kondetibd348b62012-06-12 13:38:59 +05304047 if (delay)
4048 ndelay(100);
Andiry Xu65580b432011-09-23 14:19:52 -07004049 } else {
4050 temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
4051 xhci_writel(xhci, temp, pm_addr);
Pavankumar Kondetibd348b62012-06-12 13:38:59 +05304052 if (delay)
4053 ndelay(100);
Andiry Xu65580b432011-09-23 14:19:52 -07004054 }
4055
4056 spin_unlock_irqrestore(&xhci->lock, flags);
4057 return 0;
4058}
4059
Andiry Xu95743232011-09-23 14:19:51 -07004060int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4061{
4062 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4063 int ret;
4064
4065 ret = xhci_usb2_software_lpm_test(hcd, udev);
Andiry Xu65580b432011-09-23 14:19:52 -07004066 if (!ret) {
Andiry Xu95743232011-09-23 14:19:51 -07004067 xhci_dbg(xhci, "software LPM test succeed\n");
Andiry Xu65580b432011-09-23 14:19:52 -07004068 if (xhci->hw_lpm_support == 1) {
4069 udev->usb2_hw_lpm_capable = 1;
4070 ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
4071 if (!ret)
4072 udev->usb2_hw_lpm_enabled = 1;
4073 }
4074 }
Andiry Xu95743232011-09-23 14:19:51 -07004075
4076 return 0;
4077}
4078
4079#else
4080
Andiry Xu65580b432011-09-23 14:19:52 -07004081int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4082 struct usb_device *udev, int enable)
4083{
4084 return 0;
4085}
4086
Andiry Xu95743232011-09-23 14:19:51 -07004087int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4088{
4089 return 0;
4090}
4091
4092#endif /* CONFIG_USB_SUSPEND */
4093
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004094/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4095 * internal data structures for the device.
4096 */
4097int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4098 struct usb_tt *tt, gfp_t mem_flags)
4099{
4100 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4101 struct xhci_virt_device *vdev;
4102 struct xhci_command *config_cmd;
4103 struct xhci_input_control_ctx *ctrl_ctx;
4104 struct xhci_slot_ctx *slot_ctx;
4105 unsigned long flags;
4106 unsigned think_time;
4107 int ret;
4108
4109 /* Ignore root hubs */
4110 if (!hdev->parent)
4111 return 0;
4112
4113 vdev = xhci->devs[hdev->slot_id];
4114 if (!vdev) {
4115 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4116 return -EINVAL;
4117 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004118 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004119 if (!config_cmd) {
4120 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4121 return -ENOMEM;
4122 }
4123
4124 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004125 if (hdev->speed == USB_SPEED_HIGH &&
4126 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4127 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4128 xhci_free_command(xhci, config_cmd);
4129 spin_unlock_irqrestore(&xhci->lock, flags);
4130 return -ENOMEM;
4131 }
4132
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004133 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
4134 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004135 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004136 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004137 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004138 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004139 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004140 if (xhci->hci_version > 0x95) {
4141 xhci_dbg(xhci, "xHCI version %x needs hub "
4142 "TT think time and number of ports\n",
4143 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004144 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004145 /* Set TT think time - convert from ns to FS bit times.
4146 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4147 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004148 *
4149 * xHCI 1.0: this field shall be 0 if the device is not a
4150 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004151 */
4152 think_time = tt->think_time;
4153 if (think_time != 0)
4154 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004155 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4156 slot_ctx->tt_info |=
4157 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004158 } else {
4159 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4160 "TT think time or number of ports\n",
4161 (unsigned int) xhci->hci_version);
4162 }
4163 slot_ctx->dev_state = 0;
4164 spin_unlock_irqrestore(&xhci->lock, flags);
4165
4166 xhci_dbg(xhci, "Set up %s for hub device.\n",
4167 (xhci->hci_version > 0x95) ?
4168 "configure endpoint" : "evaluate context");
4169 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4170 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4171
4172 /* Issue and wait for the configure endpoint or
4173 * evaluate context command.
4174 */
4175 if (xhci->hci_version > 0x95)
4176 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4177 false, false);
4178 else
4179 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4180 true, false);
4181
4182 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4183 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4184
4185 xhci_free_command(xhci, config_cmd);
4186 return ret;
4187}
4188
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004189int xhci_get_frame(struct usb_hcd *hcd)
4190{
4191 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4192 /* EHCI mods by the periodic size. Why? */
4193 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4194}
4195
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004196int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4197{
4198 struct xhci_hcd *xhci;
4199 struct device *dev = hcd->self.controller;
4200 int retval;
4201 u32 temp;
4202
Andiry Xufdaf8b32012-03-05 17:49:38 +08004203 /* Accept arbitrarily long scatter-gather lists */
4204 hcd->self.sg_tablesize = ~0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004205
4206 if (usb_hcd_is_primary_hcd(hcd)) {
4207 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4208 if (!xhci)
4209 return -ENOMEM;
4210 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4211 xhci->main_hcd = hcd;
4212 /* Mark the first roothub as being USB 2.0.
4213 * The xHCI driver will register the USB 3.0 roothub.
4214 */
4215 hcd->speed = HCD_USB2;
4216 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4217 /*
4218 * USB 2.0 roothub under xHCI has an integrated TT,
4219 * (rate matching hub) as opposed to having an OHCI/UHCI
4220 * companion controller.
4221 */
4222 hcd->has_tt = 1;
4223 } else {
4224 /* xHCI private pointer was set in xhci_pci_probe for the second
4225 * registered roothub.
4226 */
4227 xhci = hcd_to_xhci(hcd);
4228 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4229 if (HCC_64BIT_ADDR(temp)) {
4230 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4231 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4232 } else {
4233 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4234 }
4235 return 0;
4236 }
4237
4238 xhci->cap_regs = hcd->regs;
4239 xhci->op_regs = hcd->regs +
4240 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4241 xhci->run_regs = hcd->regs +
4242 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4243 /* Cache read-only capability registers */
4244 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4245 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4246 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4247 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4248 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4249 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4250 xhci_print_registers(xhci);
4251
4252 get_quirks(dev, xhci);
4253
George Cherian2d75d5d2013-07-01 10:59:12 +05304254 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4255 * success event after a short transfer. This quirk will ignore such
4256 * spurious event.
4257 */
4258 if (xhci->hci_version > 0x96)
4259 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4260
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004261 /* Make sure the HC is halted. */
4262 retval = xhci_halt(xhci);
4263 if (retval)
4264 goto error;
4265
4266 xhci_dbg(xhci, "Resetting HCD\n");
4267 /* Reset the internal HC memory state and registers. */
4268 retval = xhci_reset(xhci);
4269 if (retval)
4270 goto error;
4271 xhci_dbg(xhci, "Reset complete\n");
4272
4273 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4274 if (HCC_64BIT_ADDR(temp)) {
4275 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4276 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4277 } else {
4278 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4279 }
4280
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004281 return 0;
4282error:
4283 kfree(xhci);
4284 return retval;
4285}
4286
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004287MODULE_DESCRIPTION(DRIVER_DESC);
4288MODULE_AUTHOR(DRIVER_AUTHOR);
4289MODULE_LICENSE("GPL");
4290
4291static int __init xhci_hcd_init(void)
4292{
Sebastian Andrzej Siewior0cc47d52011-09-23 14:20:02 -07004293 int retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004294
4295 retval = xhci_register_pci();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004296 if (retval < 0) {
4297 printk(KERN_DEBUG "Problem registering PCI driver.");
4298 return retval;
4299 }
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004300 retval = xhci_register_plat();
4301 if (retval < 0) {
4302 printk(KERN_DEBUG "Problem registering platform driver.");
4303 goto unreg_pci;
4304 }
Sarah Sharp98441972009-05-14 11:44:18 -07004305 /*
4306 * Check the compiler generated sizes of structures that must be laid
4307 * out in specific ways for hardware access.
4308 */
4309 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4310 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4311 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4312 /* xhci_device_control has eight fields, and also
4313 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4314 */
Sarah Sharp98441972009-05-14 11:44:18 -07004315 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4316 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4317 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4318 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4319 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4320 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4321 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
4322 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004323 return 0;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004324unreg_pci:
4325 xhci_unregister_pci();
4326 return retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004327}
4328module_init(xhci_hcd_init);
4329
4330static void __exit xhci_hcd_cleanup(void)
4331{
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004332 xhci_unregister_pci();
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004333 xhci_unregister_plat();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004334}
4335module_exit(xhci_hcd_cleanup);