blob: 495c7a3a9971bd34ba9c2f45974f52c0f5dc2499 [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 */
55static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
56 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);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800108 if (!ret)
109 xhci->xhc_state |= XHCI_STATE_HALTED;
Sarah Sharp5af98bb2012-03-16 12:58:20 -0700110 else
111 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
112 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800113 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700114}
115
116/*
Sarah Sharped074532010-05-24 13:25:21 -0700117 * Set the run bit and wait for the host to be running.
118 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800119static int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700120{
121 u32 temp;
122 int ret;
123
124 temp = xhci_readl(xhci, &xhci->op_regs->command);
125 temp |= (CMD_RUN);
126 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
127 temp);
128 xhci_writel(xhci, temp, &xhci->op_regs->command);
129
130 /*
131 * Wait for the HCHalted Status bit to be 0 to indicate the host is
132 * running.
133 */
134 ret = handshake(xhci, &xhci->op_regs->status,
135 STS_HALT, 0, XHCI_MAX_HALT_USEC);
136 if (ret == -ETIMEDOUT)
137 xhci_err(xhci, "Host took too long to start, "
138 "waited %u microseconds.\n",
139 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800140 if (!ret)
141 xhci->xhc_state &= ~XHCI_STATE_HALTED;
Sarah Sharped074532010-05-24 13:25:21 -0700142 return ret;
143}
144
145/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800146 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700147 *
148 * This resets pipelines, timers, counters, state machines, etc.
149 * Transactions will be terminated immediately, and operational registers
150 * will be set to their defaults.
151 */
152int xhci_reset(struct xhci_hcd *xhci)
153{
154 u32 command;
155 u32 state;
Andiry Xu296b8ce2012-04-14 02:54:30 +0800156 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700157
158 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700159 if ((state & STS_HALT) == 0) {
160 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
161 return 0;
162 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700163
164 xhci_dbg(xhci, "// Reset the HC\n");
165 command = xhci_readl(xhci, &xhci->op_regs->command);
166 command |= CMD_RESET;
167 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700168
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700169 ret = handshake(xhci, &xhci->op_regs->command,
Sarah Sharpebd311e2012-07-23 16:06:08 -0700170 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700171 if (ret)
172 return ret;
173
174 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
175 /*
176 * xHCI cannot write to any doorbells or operational registers other
177 * than status until the "Controller Not Ready" flag is cleared.
178 */
Sarah Sharpebd311e2012-07-23 16:06:08 -0700179 ret = handshake(xhci, &xhci->op_regs->status,
180 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xu296b8ce2012-04-14 02:54:30 +0800181
182 for (i = 0; i < 2; ++i) {
183 xhci->bus_state[i].port_c_suspend = 0;
184 xhci->bus_state[i].suspended_ports = 0;
185 xhci->bus_state[i].resuming_ports = 0;
186 }
187
188 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700189}
190
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700191#ifdef CONFIG_PCI
192static int xhci_free_msi(struct xhci_hcd *xhci)
Dong Nguyen43b86af2010-07-21 16:56:08 -0700193{
194 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700195
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700196 if (!xhci->msix_entries)
197 return -EINVAL;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700198
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700199 for (i = 0; i < xhci->msix_count; i++)
200 if (xhci->msix_entries[i].vector)
201 free_irq(xhci->msix_entries[i].vector,
202 xhci_to_hcd(xhci));
203 return 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700204}
205
206/*
207 * Set up MSI
208 */
209static int xhci_setup_msi(struct xhci_hcd *xhci)
210{
211 int ret;
212 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
213
214 ret = pci_enable_msi(pdev);
215 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800216 xhci_dbg(xhci, "failed to allocate MSI entry\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700217 return ret;
218 }
219
220 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
221 0, "xhci_hcd", xhci_to_hcd(xhci));
222 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800223 xhci_dbg(xhci, "disable MSI interrupt\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700224 pci_disable_msi(pdev);
225 }
226
227 return ret;
228}
229
230/*
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700231 * Free IRQs
232 * free all IRQs request
233 */
234static void xhci_free_irq(struct xhci_hcd *xhci)
235{
236 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
237 int ret;
238
239 /* return if using legacy interrupt */
Felipe Balbicd704692012-02-29 16:46:23 +0200240 if (xhci_to_hcd(xhci)->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700241 return;
242
243 ret = xhci_free_msi(xhci);
244 if (!ret)
245 return;
Felipe Balbicd704692012-02-29 16:46:23 +0200246 if (pdev->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700247 free_irq(pdev->irq, xhci_to_hcd(xhci));
248
249 return;
250}
251
252/*
Dong Nguyen43b86af2010-07-21 16:56:08 -0700253 * Set up MSI-X
254 */
255static int xhci_setup_msix(struct xhci_hcd *xhci)
256{
257 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800258 struct usb_hcd *hcd = xhci_to_hcd(xhci);
259 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700260
261 /*
262 * calculate number of msi-x vectors supported.
263 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
264 * with max number of interrupters based on the xhci HCSPARAMS1.
265 * - num_online_cpus: maximum msi-x vectors per CPUs core.
266 * Add additional 1 vector to ensure always available interrupt.
267 */
268 xhci->msix_count = min(num_online_cpus() + 1,
269 HCS_MAX_INTRS(xhci->hcs_params1));
270
271 xhci->msix_entries =
272 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800273 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700274 if (!xhci->msix_entries) {
275 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
276 return -ENOMEM;
277 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700278
279 for (i = 0; i < xhci->msix_count; i++) {
280 xhci->msix_entries[i].entry = i;
281 xhci->msix_entries[i].vector = 0;
282 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700283
284 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
285 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800286 xhci_dbg(xhci, "Failed to enable MSI-X\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700287 goto free_entries;
288 }
289
Dong Nguyen43b86af2010-07-21 16:56:08 -0700290 for (i = 0; i < xhci->msix_count; i++) {
291 ret = request_irq(xhci->msix_entries[i].vector,
292 (irq_handler_t)xhci_msi_irq,
293 0, "xhci_hcd", xhci_to_hcd(xhci));
294 if (ret)
295 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700296 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700297
Andiry Xu00292272010-12-27 17:39:02 +0800298 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700299 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700300
301disable_msix:
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800302 xhci_dbg(xhci, "disable MSI-X interrupt\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700303 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700304 pci_disable_msix(pdev);
305free_entries:
306 kfree(xhci->msix_entries);
307 xhci->msix_entries = NULL;
308 return ret;
309}
310
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700311/* Free any IRQs and disable MSI-X */
312static void xhci_cleanup_msix(struct xhci_hcd *xhci)
313{
Andiry Xu00292272010-12-27 17:39:02 +0800314 struct usb_hcd *hcd = xhci_to_hcd(xhci);
315 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700316
Dong Nguyen43b86af2010-07-21 16:56:08 -0700317 xhci_free_irq(xhci);
318
319 if (xhci->msix_entries) {
320 pci_disable_msix(pdev);
321 kfree(xhci->msix_entries);
322 xhci->msix_entries = NULL;
323 } else {
324 pci_disable_msi(pdev);
325 }
326
Andiry Xu00292272010-12-27 17:39:02 +0800327 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700328 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700329}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700330
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700331static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
332{
333 int i;
334
335 if (xhci->msix_entries) {
336 for (i = 0; i < xhci->msix_count; i++)
337 synchronize_irq(xhci->msix_entries[i].vector);
338 }
339}
340
341static int xhci_try_enable_msi(struct usb_hcd *hcd)
342{
343 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
344 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
345 int ret;
346
347 /*
348 * Some Fresco Logic host controllers advertise MSI, but fail to
349 * generate interrupts. Don't even try to enable MSI.
350 */
351 if (xhci->quirks & XHCI_BROKEN_MSI)
352 return 0;
353
354 /* unregister the legacy interrupt */
355 if (hcd->irq)
356 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200357 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700358
359 ret = xhci_setup_msix(xhci);
360 if (ret)
361 /* fall back to msi*/
362 ret = xhci_setup_msi(xhci);
363
364 if (!ret)
Felipe Balbicd704692012-02-29 16:46:23 +0200365 /* hcd->irq is 0, we have MSI */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700366 return 0;
367
Sarah Sharp68d07f62012-02-13 16:25:57 -0800368 if (!pdev->irq) {
369 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
370 return -EINVAL;
371 }
372
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
387static int xhci_try_enable_msi(struct usb_hcd *hcd)
388{
389 return 0;
390}
391
392static void xhci_cleanup_msix(struct xhci_hcd *xhci)
393{
394}
395
396static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
397{
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") ||
481 strstr(dmi_product_name, "Z820"))
482 return true;
483
484 return false;
485}
486
487static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
488{
489 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
490}
491
492
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700493/*
494 * Initialize memory for HCD and xHC (one-time init).
495 *
496 * Program the PAGESIZE register, initialize the device context array, create
497 * device contexts (?), set up a command ring segment (or two?), create event
498 * ring (one for now).
499 */
500int xhci_init(struct usb_hcd *hcd)
501{
502 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
503 int retval = 0;
504
505 xhci_dbg(xhci, "xhci_init\n");
506 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700507 if (xhci->hci_version == 0x95 && link_quirk) {
Sarah Sharpb0567b32009-08-07 14:04:36 -0700508 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
509 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
510 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700511 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700512 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700513 retval = xhci_mem_init(xhci, GFP_KERNEL);
514 xhci_dbg(xhci, "Finished xhci_init\n");
515
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500516 /* Initializing Compliance Mode Recovery Data If Needed */
517 if (compliance_mode_recovery_timer_quirk_check()) {
518 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
519 compliance_mode_recovery_timer_init(xhci);
520 }
521
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700522 return retval;
523}
524
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700525/*-------------------------------------------------------------------------*/
526
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700527
528#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800529static void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700530{
531 unsigned long flags;
532 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700533 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700534 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
535 int i, j;
536
537 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
538
539 spin_lock_irqsave(&xhci->lock, flags);
540 temp = xhci_readl(xhci, &xhci->op_regs->status);
541 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp7bd89b42011-07-01 13:35:40 -0700542 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
543 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700544 xhci_dbg(xhci, "HW died, polling stopped.\n");
545 spin_unlock_irqrestore(&xhci->lock, flags);
546 return;
547 }
548
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700549 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
550 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700551 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
552 xhci->error_bitmask = 0;
553 xhci_dbg(xhci, "Event ring:\n");
554 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
555 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700556 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
557 temp_64 &= ~ERST_PTR_MASK;
558 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700559 xhci_dbg(xhci, "Command ring:\n");
560 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
561 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
562 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700563 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700564 if (!xhci->devs[i])
565 continue;
566 for (j = 0; j < 31; ++j) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700567 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700568 }
569 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700570 spin_unlock_irqrestore(&xhci->lock, flags);
571
572 if (!xhci->zombie)
573 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
574 else
575 xhci_dbg(xhci, "Quit polling the event ring.\n");
576}
577#endif
578
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800579static int xhci_run_finished(struct xhci_hcd *xhci)
580{
581 if (xhci_start(xhci)) {
582 xhci_halt(xhci);
583 return -ENODEV;
584 }
585 xhci->shared_hcd->state = HC_STATE_RUNNING;
586
587 if (xhci->quirks & XHCI_NEC_HOST)
588 xhci_ring_cmd_db(xhci);
589
590 xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
591 return 0;
592}
593
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700594/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700595 * Start the HC after it was halted.
596 *
597 * This function is called by the USB core when the HC driver is added.
598 * Its opposite is xhci_stop().
599 *
600 * xhci_init() must be called once before this function can be called.
601 * Reset the HC, enable device slot contexts, program DCBAAP, and
602 * set command ring pointer and event ring pointer.
603 *
604 * Setup MSI-X vectors and enable interrupts.
605 */
606int xhci_run(struct usb_hcd *hcd)
607{
608 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700609 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700610 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700611 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700612
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800613 /* Start the xHCI host controller running only after the USB 2.0 roothub
614 * is setup.
615 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700616
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700617 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800618 if (!usb_hcd_is_primary_hcd(hcd))
619 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700620
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700621 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700622
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700623 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700624 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700625 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700626
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700627#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
628 init_timer(&xhci->event_ring_timer);
629 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700630 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700631 /* Poll the event ring */
632 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
633 xhci->zombie = 0;
634 xhci_dbg(xhci, "Setting event ring polling timer\n");
635 add_timer(&xhci->event_ring_timer);
636#endif
637
Sarah Sharp66e49d82009-07-27 12:03:46 -0700638 xhci_dbg(xhci, "Command ring memory map follows:\n");
639 xhci_debug_ring(xhci, xhci->cmd_ring);
640 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
641 xhci_dbg_cmd_ptrs(xhci);
642
643 xhci_dbg(xhci, "ERST memory map follows:\n");
644 xhci_dbg_erst(xhci, &xhci->erst);
645 xhci_dbg(xhci, "Event ring:\n");
646 xhci_debug_ring(xhci, xhci->event_ring);
647 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
648 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
649 temp_64 &= ~ERST_PTR_MASK;
650 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
651
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700652 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
653 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700654 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700655 temp |= (u32) 160;
656 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
657
658 /* Set the HCD state before we enable the irqs */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700659 temp = xhci_readl(xhci, &xhci->op_regs->command);
660 temp |= (CMD_EIE);
661 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
662 temp);
663 xhci_writel(xhci, temp, &xhci->op_regs->command);
664
665 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700666 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
667 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700668 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
669 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800670 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700671
Sarah Sharp02386342010-05-24 13:25:28 -0700672 if (xhci->quirks & XHCI_NEC_HOST)
673 xhci_queue_vendor_command(xhci, 0, 0, 0,
674 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700675
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800676 xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700677 return 0;
678}
679
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800680static void xhci_only_stop_hcd(struct usb_hcd *hcd)
681{
682 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
683
684 spin_lock_irq(&xhci->lock);
685 xhci_halt(xhci);
686
687 /* The shared_hcd is going to be deallocated shortly (the USB core only
688 * calls this function when allocation fails in usb_add_hcd(), or
689 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
690 */
691 xhci->shared_hcd = NULL;
692 spin_unlock_irq(&xhci->lock);
693}
694
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700695/*
696 * Stop xHCI driver.
697 *
698 * This function is called by the USB core when the HC driver is removed.
699 * Its opposite is xhci_run().
700 *
701 * Disable device contexts, disable IRQs, and quiesce the HC.
702 * Reset the HC, finish any completed transactions, and cleanup memory.
703 */
704void xhci_stop(struct usb_hcd *hcd)
705{
706 u32 temp;
707 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
708
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800709 if (!usb_hcd_is_primary_hcd(hcd)) {
710 xhci_only_stop_hcd(xhci->shared_hcd);
711 return;
712 }
713
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700714 spin_lock_irq(&xhci->lock);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800715 /* Make sure the xHC is halted for a USB3 roothub
716 * (xhci_stop() could be called as part of failed init).
717 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700718 xhci_halt(xhci);
719 xhci_reset(xhci);
720 spin_unlock_irq(&xhci->lock);
721
Zhang Rui40a9fb12010-12-17 13:17:04 -0800722 xhci_cleanup_msix(xhci);
723
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700724#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
725 /* Tell the event ring poll function not to reschedule */
726 xhci->zombie = 1;
727 del_timer_sync(&xhci->event_ring_timer);
728#endif
729
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500730 /* Deleting Compliance Mode Recovery Timer */
731 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
732 (!(xhci_all_ports_seen_u0(xhci))))
733 del_timer_sync(&xhci->comp_mode_recovery_timer);
734
Andiry Xuc41136b2011-03-22 17:08:14 +0800735 if (xhci->quirks & XHCI_AMD_PLL_FIX)
736 usb_amd_dev_put();
737
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700738 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
739 temp = xhci_readl(xhci, &xhci->op_regs->status);
740 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
741 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
742 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
743 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800744 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700745
746 xhci_dbg(xhci, "cleaning up memory\n");
747 xhci_mem_cleanup(xhci);
748 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
749 xhci_readl(xhci, &xhci->op_regs->status));
750}
751
752/*
753 * Shutdown HC (not bus-specific)
754 *
755 * This is called when the machine is rebooting or halting. We assume that the
756 * machine will be powered off, and the HC's internal state will be reset.
757 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800758 *
759 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700760 */
761void xhci_shutdown(struct usb_hcd *hcd)
762{
763 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
764
Dan Carpenter3dd2f0b2012-08-13 19:57:03 +0300765 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Sarah Sharp0adf7a02012-07-23 18:59:30 +0300766 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
767
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700768 spin_lock_irq(&xhci->lock);
769 xhci_halt(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700770 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700771
Zhang Rui40a9fb12010-12-17 13:17:04 -0800772 xhci_cleanup_msix(xhci);
773
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700774 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
775 xhci_readl(xhci, &xhci->op_regs->status));
776}
777
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700778#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700779static void xhci_save_registers(struct xhci_hcd *xhci)
780{
781 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
782 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
783 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
784 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700785 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
786 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
787 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700788 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
789 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700790}
791
792static void xhci_restore_registers(struct xhci_hcd *xhci)
793{
794 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
795 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
796 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
797 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700798 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
799 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
Sarah Sharpfb3d85b2012-03-16 13:27:39 -0700800 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700801 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
802 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700803}
804
Sarah Sharp89821322010-11-12 11:59:31 -0800805static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
806{
807 u64 val_64;
808
809 /* step 2: initialize command ring buffer */
810 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
811 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
812 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
813 xhci->cmd_ring->dequeue) &
814 (u64) ~CMD_RING_RSVD_BITS) |
815 xhci->cmd_ring->cycle_state;
816 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
817 (long unsigned long) val_64);
818 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
819}
820
821/*
822 * The whole command ring must be cleared to zero when we suspend the host.
823 *
824 * The host doesn't save the command ring pointer in the suspend well, so we
825 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
826 * aligned, because of the reserved bits in the command ring dequeue pointer
827 * register. Therefore, we can't just set the dequeue pointer back in the
828 * middle of the ring (TRBs are 16-byte aligned).
829 */
830static void xhci_clear_command_ring(struct xhci_hcd *xhci)
831{
832 struct xhci_ring *ring;
833 struct xhci_segment *seg;
834
835 ring = xhci->cmd_ring;
836 seg = ring->deq_seg;
837 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800838 memset(seg->trbs, 0,
839 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
840 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
841 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800842 seg = seg->next;
843 } while (seg != ring->deq_seg);
844
845 /* Reset the software enqueue and dequeue pointers */
846 ring->deq_seg = ring->first_seg;
847 ring->dequeue = ring->first_seg->trbs;
848 ring->enq_seg = ring->deq_seg;
849 ring->enqueue = ring->dequeue;
850
Andiry Xub008df62012-03-05 17:49:34 +0800851 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800852 /*
853 * Ring is now zeroed, so the HW should look for change of ownership
854 * when the cycle bit is set to 1.
855 */
856 ring->cycle_state = 1;
857
858 /*
859 * Reset the hardware dequeue pointer.
860 * Yes, this will need to be re-written after resume, but we're paranoid
861 * and want to make sure the hardware doesn't access bogus memory
862 * because, say, the BIOS or an SMI started the host without changing
863 * the command ring pointers.
864 */
865 xhci_set_cmd_ring_deq(xhci);
866}
867
Andiry Xu5535b1d2010-10-14 07:23:06 -0700868/*
869 * Stop HC (not bus-specific)
870 *
871 * This is called when the machine transition into S3/S4 mode.
872 *
873 */
874int xhci_suspend(struct xhci_hcd *xhci)
875{
876 int rc = 0;
877 struct usb_hcd *hcd = xhci_to_hcd(xhci);
878 u32 command;
879
880 spin_lock_irq(&xhci->lock);
881 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -0800882 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700883 /* step 1: stop endpoint */
884 /* skipped assuming that port suspend has done */
885
886 /* step 2: clear Run/Stop bit */
887 command = xhci_readl(xhci, &xhci->op_regs->command);
888 command &= ~CMD_RUN;
889 xhci_writel(xhci, command, &xhci->op_regs->command);
890 if (handshake(xhci, &xhci->op_regs->status,
891 STS_HALT, STS_HALT, 100*100)) {
892 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
893 spin_unlock_irq(&xhci->lock);
894 return -ETIMEDOUT;
895 }
Sarah Sharp89821322010-11-12 11:59:31 -0800896 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700897
898 /* step 3: save registers */
899 xhci_save_registers(xhci);
900
901 /* step 4: set CSS flag */
902 command = xhci_readl(xhci, &xhci->op_regs->command);
903 command |= CMD_CSS;
904 xhci_writel(xhci, command, &xhci->op_regs->command);
Andiry Xu5dc6fed2012-06-13 10:51:57 +0800905 if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10 * 1000)) {
906 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700907 spin_unlock_irq(&xhci->lock);
908 return -ETIMEDOUT;
909 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700910 spin_unlock_irq(&xhci->lock);
911
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -0500912 /*
913 * Deleting Compliance Mode Recovery Timer because the xHCI Host
914 * is about to be suspended.
915 */
916 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
917 (!(xhci_all_ports_seen_u0(xhci)))) {
918 del_timer_sync(&xhci->comp_mode_recovery_timer);
919 xhci_dbg(xhci, "Compliance Mode Recovery Timer Deleted!\n");
920 }
921
Andiry Xu00292272010-12-27 17:39:02 +0800922 /* step 5: remove core well power */
923 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700924 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800925
Andiry Xu5535b1d2010-10-14 07:23:06 -0700926 return rc;
927}
928
929/*
930 * start xHC (not bus-specific)
931 *
932 * This is called when the machine transition from S3/S4 mode.
933 *
934 */
935int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
936{
937 u32 command, temp = 0;
938 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -0800939 struct usb_hcd *secondary_hcd;
Alan Sternf69e3122011-11-03 11:37:10 -0400940 int retval = 0;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700941
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800942 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300943 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800944 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800945 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
946 time_before(jiffies,
947 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -0700948 msleep(100);
949
Alan Sternf69e3122011-11-03 11:37:10 -0400950 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
951 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
952
Andiry Xu5535b1d2010-10-14 07:23:06 -0700953 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +0200954 if (xhci->quirks & XHCI_RESET_ON_RESUME)
955 hibernated = true;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700956
957 if (!hibernated) {
958 /* step 1: restore register */
959 xhci_restore_registers(xhci);
960 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800961 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700962 /* step 3: restore state and start state*/
963 /* step 3: set CRS flag */
964 command = xhci_readl(xhci, &xhci->op_regs->command);
965 command |= CMD_CRS;
966 xhci_writel(xhci, command, &xhci->op_regs->command);
967 if (handshake(xhci, &xhci->op_regs->status,
Andiry Xu5dc6fed2012-06-13 10:51:57 +0800968 STS_RESTORE, 0, 10 * 1000)) {
969 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700970 spin_unlock_irq(&xhci->lock);
971 return -ETIMEDOUT;
972 }
973 temp = xhci_readl(xhci, &xhci->op_regs->status);
974 }
975
976 /* If restore operation fails, re-initialize the HC during resume */
977 if ((temp & STS_SRE) || hibernated) {
Sarah Sharpfedd3832011-04-12 17:43:19 -0700978 /* Let the USB core know _both_ roothubs lost power. */
979 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
980 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700981
982 xhci_dbg(xhci, "Stop HCD\n");
983 xhci_halt(xhci);
984 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700985 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +0800986 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700987
988#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
989 /* Tell the event ring poll function not to reschedule */
990 xhci->zombie = 1;
991 del_timer_sync(&xhci->event_ring_timer);
992#endif
993
994 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
995 temp = xhci_readl(xhci, &xhci->op_regs->status);
996 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
997 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
998 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
999 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -08001000 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001001
1002 xhci_dbg(xhci, "cleaning up memory\n");
1003 xhci_mem_cleanup(xhci);
1004 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1005 xhci_readl(xhci, &xhci->op_regs->status));
1006
Sarah Sharp65b22f92010-12-17 12:35:05 -08001007 /* USB core calls the PCI reinit and start functions twice:
1008 * first with the primary HCD, and then with the secondary HCD.
1009 * If we don't do the same, the host will never be started.
1010 */
1011 if (!usb_hcd_is_primary_hcd(hcd))
1012 secondary_hcd = hcd;
1013 else
1014 secondary_hcd = xhci->shared_hcd;
1015
1016 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1017 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001018 if (retval)
1019 return retval;
Sarah Sharp65b22f92010-12-17 12:35:05 -08001020 xhci_dbg(xhci, "Start the primary HCD\n");
1021 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001022 if (!retval) {
Alan Sternf69e3122011-11-03 11:37:10 -04001023 xhci_dbg(xhci, "Start the secondary HCD\n");
1024 retval = xhci_run(secondary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001025 }
Andiry Xu5535b1d2010-10-14 07:23:06 -07001026 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb3209372011-03-07 11:24:07 -08001027 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e3122011-11-03 11:37:10 -04001028 goto done;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001029 }
1030
Andiry Xu5535b1d2010-10-14 07:23:06 -07001031 /* step 4: set Run/Stop bit */
1032 command = xhci_readl(xhci, &xhci->op_regs->command);
1033 command |= CMD_RUN;
1034 xhci_writel(xhci, command, &xhci->op_regs->command);
1035 handshake(xhci, &xhci->op_regs->status, STS_HALT,
1036 0, 250 * 1000);
1037
1038 /* step 5: walk topology and initialize portsc,
1039 * portpmsc and portli
1040 */
1041 /* this is done in bus_resume */
1042
1043 /* step 6: restart each of the previously
1044 * Running endpoints by ringing their doorbells
1045 */
1046
Andiry Xu5535b1d2010-10-14 07:23:06 -07001047 spin_unlock_irq(&xhci->lock);
Alan Sternf69e3122011-11-03 11:37:10 -04001048
1049 done:
1050 if (retval == 0) {
1051 usb_hcd_resume_root_hub(hcd);
1052 usb_hcd_resume_root_hub(xhci->shared_hcd);
1053 }
Alexis R. Cortesdadc5da2012-08-03 14:00:27 -05001054
1055 /*
1056 * If system is subject to the Quirk, Compliance Mode Timer needs to
1057 * be re-initialized Always after a system resume. Ports are subject
1058 * to suffer the Compliance Mode issue again. It doesn't matter if
1059 * ports have entered previously to U0 before system's suspension.
1060 */
1061 if (xhci->quirks & XHCI_COMP_MODE_QUIRK)
1062 compliance_mode_recovery_timer_init(xhci);
1063
Alan Sternf69e3122011-11-03 11:37:10 -04001064 return retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001065}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001066#endif /* CONFIG_PM */
1067
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001068/*-------------------------------------------------------------------------*/
1069
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001070/**
1071 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1072 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1073 * value to right shift 1 for the bitmask.
1074 *
1075 * Index = (epnum * 2) + direction - 1,
1076 * where direction = 0 for OUT, 1 for IN.
1077 * For control endpoints, the IN index is used (OUT index is unused), so
1078 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1079 */
1080unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1081{
1082 unsigned int index;
1083 if (usb_endpoint_xfer_control(desc))
1084 index = (unsigned int) (usb_endpoint_num(desc)*2);
1085 else
1086 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1087 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1088 return index;
1089}
1090
Sarah Sharpf94e01862009-04-27 19:58:38 -07001091/* Find the flag for this endpoint (for use in the control context). Use the
1092 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1093 * bit 1, etc.
1094 */
1095unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1096{
1097 return 1 << (xhci_get_endpoint_index(desc) + 1);
1098}
1099
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001100/* Find the flag for this endpoint (for use in the control context). Use the
1101 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1102 * bit 1, etc.
1103 */
1104unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1105{
1106 return 1 << (ep_index + 1);
1107}
1108
Sarah Sharpf94e01862009-04-27 19:58:38 -07001109/* Compute the last valid endpoint context index. Basically, this is the
1110 * endpoint index plus one. For slot contexts with more than valid endpoint,
1111 * we find the most significant bit set in the added contexts flags.
1112 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1113 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1114 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001115unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001116{
1117 return fls(added_ctxs) - 1;
1118}
1119
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001120/* Returns 1 if the arguments are OK;
1121 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1122 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001123static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001124 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1125 const char *func) {
1126 struct xhci_hcd *xhci;
1127 struct xhci_virt_device *virt_dev;
1128
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001129 if (!hcd || (check_ep && !ep) || !udev) {
1130 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
1131 func);
1132 return -EINVAL;
1133 }
1134 if (!udev->parent) {
1135 printk(KERN_DEBUG "xHCI %s called for root hub\n",
1136 func);
1137 return 0;
1138 }
Andiry Xu64927732010-10-14 07:22:45 -07001139
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001140 xhci = hcd_to_xhci(hcd);
1141 if (xhci->xhc_state & XHCI_STATE_HALTED)
1142 return -ENODEV;
1143
Andiry Xu64927732010-10-14 07:22:45 -07001144 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001145 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Andiry Xu64927732010-10-14 07:22:45 -07001146 printk(KERN_DEBUG "xHCI %s called with unaddressed "
1147 "device\n", func);
1148 return -EINVAL;
1149 }
1150
1151 virt_dev = xhci->devs[udev->slot_id];
1152 if (virt_dev->udev != udev) {
1153 printk(KERN_DEBUG "xHCI %s called with udev and "
1154 "virt_dev does not match\n", func);
1155 return -EINVAL;
1156 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001157 }
Andiry Xu64927732010-10-14 07:22:45 -07001158
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001159 return 1;
1160}
1161
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001162static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001163 struct usb_device *udev, struct xhci_command *command,
1164 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001165
1166/*
1167 * Full speed devices may have a max packet size greater than 8 bytes, but the
1168 * USB core doesn't know that until it reads the first 8 bytes of the
1169 * descriptor. If the usb_device's max packet size changes after that point,
1170 * we need to issue an evaluate context command and wait on it.
1171 */
1172static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1173 unsigned int ep_index, struct urb *urb)
1174{
1175 struct xhci_container_ctx *in_ctx;
1176 struct xhci_container_ctx *out_ctx;
1177 struct xhci_input_control_ctx *ctrl_ctx;
1178 struct xhci_ep_ctx *ep_ctx;
1179 int max_packet_size;
1180 int hw_max_packet_size;
1181 int ret = 0;
1182
1183 out_ctx = xhci->devs[slot_id]->out_ctx;
1184 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001185 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001186 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001187 if (hw_max_packet_size != max_packet_size) {
1188 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
1189 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
1190 max_packet_size);
1191 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
1192 hw_max_packet_size);
1193 xhci_dbg(xhci, "Issuing evaluate context command.\n");
1194
1195 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -07001196 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1197 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001198 in_ctx = xhci->devs[slot_id]->in_ctx;
1199 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001200 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1201 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001202
1203 /* Set up the input context flags for the command */
1204 /* FIXME: This won't work if a non-default control endpoint
1205 * changes max packet sizes.
1206 */
1207 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001208 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001209 ctrl_ctx->drop_flags = 0;
1210
1211 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1212 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1213 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1214 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1215
Sarah Sharp913a8a32009-09-04 10:53:13 -07001216 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1217 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001218
1219 /* Clean up the input context for later use by bandwidth
1220 * functions.
1221 */
Matt Evans28ccd292011-03-29 13:40:46 +11001222 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001223 }
1224 return ret;
1225}
1226
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001227/*
1228 * non-error returns are a promise to giveback() the urb later
1229 * we drop ownership so next owner (or urb unlink) can get it
1230 */
1231int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1232{
1233 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Andiry Xu2ffdea22011-09-02 11:05:57 -07001234 struct xhci_td *buffer;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001235 unsigned long flags;
1236 int ret = 0;
1237 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001238 struct urb_priv *urb_priv;
1239 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001240
Andiry Xu64927732010-10-14 07:22:45 -07001241 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1242 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001243 return -EINVAL;
1244
1245 slot_id = urb->dev->slot_id;
1246 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001247
Alan Stern541c7d42010-06-22 16:39:10 -04001248 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001249 if (!in_interrupt())
1250 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1251 ret = -ESHUTDOWN;
1252 goto exit;
1253 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001254
1255 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1256 size = urb->number_of_packets;
1257 else
1258 size = 1;
1259
1260 urb_priv = kzalloc(sizeof(struct urb_priv) +
1261 size * sizeof(struct xhci_td *), mem_flags);
1262 if (!urb_priv)
1263 return -ENOMEM;
1264
Andiry Xu2ffdea22011-09-02 11:05:57 -07001265 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1266 if (!buffer) {
1267 kfree(urb_priv);
1268 return -ENOMEM;
1269 }
1270
Andiry Xu8e51adc2010-07-22 15:23:31 -07001271 for (i = 0; i < size; i++) {
Andiry Xu2ffdea22011-09-02 11:05:57 -07001272 urb_priv->td[i] = buffer;
1273 buffer++;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001274 }
1275
1276 urb_priv->length = size;
1277 urb_priv->td_cnt = 0;
1278 urb->hcpriv = urb_priv;
1279
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001280 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1281 /* Check to see if the max packet size for the default control
1282 * endpoint changed during FS device enumeration
1283 */
1284 if (urb->dev->speed == USB_SPEED_FULL) {
1285 ret = xhci_check_maxpacket(xhci, slot_id,
1286 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001287 if (ret < 0) {
1288 xhci_urb_free_priv(xhci, urb_priv);
1289 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001290 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001291 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001292 }
1293
Sarah Sharpb11069f2009-07-27 12:03:23 -07001294 /* We have a spinlock and interrupts disabled, so we must pass
1295 * atomic context to this function, which may allocate memory.
1296 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001297 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001298 if (xhci->xhc_state & XHCI_STATE_DYING)
1299 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001300 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001301 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001302 if (ret)
1303 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001304 spin_unlock_irqrestore(&xhci->lock, flags);
1305 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1306 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001307 if (xhci->xhc_state & XHCI_STATE_DYING)
1308 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001309 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1310 EP_GETTING_STREAMS) {
1311 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1312 "is transitioning to using streams.\n");
1313 ret = -EINVAL;
1314 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1315 EP_GETTING_NO_STREAMS) {
1316 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1317 "is transitioning to "
1318 "not having streams.\n");
1319 ret = -EINVAL;
1320 } else {
1321 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1322 slot_id, ep_index);
1323 }
Sarah Sharpd13565c2011-07-22 14:34:34 -07001324 if (ret)
1325 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001326 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001327 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1328 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001329 if (xhci->xhc_state & XHCI_STATE_DYING)
1330 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001331 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1332 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001333 if (ret)
1334 goto free_priv;
Sarah Sharp624defa2009-09-02 12:14:28 -07001335 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001336 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001337 spin_lock_irqsave(&xhci->lock, flags);
1338 if (xhci->xhc_state & XHCI_STATE_DYING)
1339 goto dying;
1340 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1341 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001342 if (ret)
1343 goto free_priv;
Andiry Xu787f4e52010-07-22 15:23:52 -07001344 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001345 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001346exit:
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001347 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001348dying:
1349 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1350 "non-responsive xHCI host.\n",
1351 urb->ep->desc.bEndpointAddress, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001352 ret = -ESHUTDOWN;
1353free_priv:
1354 xhci_urb_free_priv(xhci, urb_priv);
1355 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001356 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001357 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001358}
1359
Sarah Sharp021bff92010-07-29 22:12:20 -07001360/* Get the right ring for the given URB.
1361 * If the endpoint supports streams, boundary check the URB's stream ID.
1362 * If the endpoint doesn't support streams, return the singular endpoint ring.
1363 */
1364static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1365 struct urb *urb)
1366{
1367 unsigned int slot_id;
1368 unsigned int ep_index;
1369 unsigned int stream_id;
1370 struct xhci_virt_ep *ep;
1371
1372 slot_id = urb->dev->slot_id;
1373 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1374 stream_id = urb->stream_id;
1375 ep = &xhci->devs[slot_id]->eps[ep_index];
1376 /* Common case: no streams */
1377 if (!(ep->ep_state & EP_HAS_STREAMS))
1378 return ep->ring;
1379
1380 if (stream_id == 0) {
1381 xhci_warn(xhci,
1382 "WARN: Slot ID %u, ep index %u has streams, "
1383 "but URB has no stream ID.\n",
1384 slot_id, ep_index);
1385 return NULL;
1386 }
1387
1388 if (stream_id < ep->stream_info->num_streams)
1389 return ep->stream_info->stream_rings[stream_id];
1390
1391 xhci_warn(xhci,
1392 "WARN: Slot ID %u, ep index %u has "
1393 "stream IDs 1 to %u allocated, "
1394 "but stream ID %u is requested.\n",
1395 slot_id, ep_index,
1396 ep->stream_info->num_streams - 1,
1397 stream_id);
1398 return NULL;
1399}
1400
Sarah Sharpae636742009-04-29 19:02:31 -07001401/*
1402 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1403 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1404 * should pick up where it left off in the TD, unless a Set Transfer Ring
1405 * Dequeue Pointer is issued.
1406 *
1407 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1408 * the ring. Since the ring is a contiguous structure, they can't be physically
1409 * removed. Instead, there are two options:
1410 *
1411 * 1) If the HC is in the middle of processing the URB to be canceled, we
1412 * simply move the ring's dequeue pointer past those TRBs using the Set
1413 * Transfer Ring Dequeue Pointer command. This will be the common case,
1414 * when drivers timeout on the last submitted URB and attempt to cancel.
1415 *
1416 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1417 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1418 * HC will need to invalidate the any TRBs it has cached after the stop
1419 * endpoint command, as noted in the xHCI 0.95 errata.
1420 *
1421 * 3) The TD may have completed by the time the Stop Endpoint Command
1422 * completes, so software needs to handle that case too.
1423 *
1424 * This function should protect against the TD enqueueing code ringing the
1425 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1426 * It also needs to account for multiple cancellations on happening at the same
1427 * time for the same endpoint.
1428 *
1429 * Note that this function can be called in any context, or so says
1430 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001431 */
1432int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1433{
Sarah Sharpae636742009-04-29 19:02:31 -07001434 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001435 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001436 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001437 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001438 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001439 struct xhci_td *td;
1440 unsigned int ep_index;
1441 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001442 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001443
1444 xhci = hcd_to_xhci(hcd);
1445 spin_lock_irqsave(&xhci->lock, flags);
1446 /* Make sure the URB hasn't completed or been unlinked already */
1447 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1448 if (ret || !urb->hcpriv)
1449 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001450 temp = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -08001451 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001452 xhci_dbg(xhci, "HW died, freeing TD.\n");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001453 urb_priv = urb->hcpriv;
Sarah Sharp585df1d2011-08-02 15:43:40 -07001454 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1455 td = urb_priv->td[i];
1456 if (!list_empty(&td->td_list))
1457 list_del_init(&td->td_list);
1458 if (!list_empty(&td->cancelled_td_list))
1459 list_del_init(&td->cancelled_td_list);
1460 }
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001461
1462 usb_hcd_unlink_urb_from_ep(hcd, urb);
1463 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001464 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001465 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001466 return ret;
1467 }
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001468 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1469 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001470 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1471 "non-responsive xHCI host.\n",
1472 urb->ep->desc.bEndpointAddress, urb);
1473 /* Let the stop endpoint command watchdog timer (which set this
1474 * state) finish cleaning up the endpoint TD lists. We must
1475 * have caught it in the middle of dropping a lock and giving
1476 * back an URB.
1477 */
1478 goto done;
1479 }
Sarah Sharpae636742009-04-29 19:02:31 -07001480
Sarah Sharpae636742009-04-29 19:02:31 -07001481 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001482 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001483 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1484 if (!ep_ring) {
1485 ret = -EINVAL;
1486 goto done;
1487 }
1488
Andiry Xu8e51adc2010-07-22 15:23:31 -07001489 urb_priv = urb->hcpriv;
Sarah Sharp79688ac2011-12-19 16:56:04 -08001490 i = urb_priv->td_cnt;
1491 if (i < urb_priv->length)
1492 xhci_dbg(xhci, "Cancel URB %p, dev %s, ep 0x%x, "
1493 "starting at offset 0x%llx\n",
1494 urb, urb->dev->devpath,
1495 urb->ep->desc.bEndpointAddress,
1496 (unsigned long long) xhci_trb_virt_to_dma(
1497 urb_priv->td[i]->start_seg,
1498 urb_priv->td[i]->first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001499
Sarah Sharp79688ac2011-12-19 16:56:04 -08001500 for (; i < urb_priv->length; i++) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001501 td = urb_priv->td[i];
1502 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1503 }
1504
Sarah Sharpae636742009-04-29 19:02:31 -07001505 /* Queue a stop endpoint command, but only if this is
1506 * the first cancellation to be handled.
1507 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001508 if (!(ep->ep_state & EP_HALT_PENDING)) {
1509 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001510 ep->stop_cmds_pending++;
1511 ep->stop_cmd_timer.expires = jiffies +
1512 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1513 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001514 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001515 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001516 }
1517done:
1518 spin_unlock_irqrestore(&xhci->lock, flags);
1519 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001520}
1521
Sarah Sharpf94e01862009-04-27 19:58:38 -07001522/* Drop an endpoint from a new bandwidth configuration for this device.
1523 * Only one call to this function is allowed per endpoint before
1524 * check_bandwidth() or reset_bandwidth() must be called.
1525 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1526 * add the endpoint to the schedule with possibly new parameters denoted by a
1527 * different endpoint descriptor in usb_host_endpoint.
1528 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1529 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001530 *
1531 * The USB core will not allow URBs to be queued to an endpoint that is being
1532 * disabled, so there's no need for mutual exclusion to protect
1533 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001534 */
1535int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1536 struct usb_host_endpoint *ep)
1537{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001538 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001539 struct xhci_container_ctx *in_ctx, *out_ctx;
1540 struct xhci_input_control_ctx *ctrl_ctx;
1541 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001542 unsigned int last_ctx;
1543 unsigned int ep_index;
1544 struct xhci_ep_ctx *ep_ctx;
1545 u32 drop_flag;
1546 u32 new_add_flags, new_drop_flags, new_slot_info;
1547 int ret;
1548
Andiry Xu64927732010-10-14 07:22:45 -07001549 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001550 if (ret <= 0)
1551 return ret;
1552 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001553 if (xhci->xhc_state & XHCI_STATE_DYING)
1554 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001555
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001556 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001557 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1558 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1559 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1560 __func__, drop_flag);
1561 return 0;
1562 }
1563
Sarah Sharpf94e01862009-04-27 19:58:38 -07001564 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001565 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1566 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001567 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001568 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001569 /* If the HC already knows the endpoint is disabled,
1570 * or the HCD has noted it is disabled, ignore this request
1571 */
Matt Evansf5960b62011-06-01 10:22:55 +10001572 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1573 cpu_to_le32(EP_STATE_DISABLED)) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001574 le32_to_cpu(ctrl_ctx->drop_flags) &
1575 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001576 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1577 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001578 return 0;
1579 }
1580
Matt Evans28ccd292011-03-29 13:40:46 +11001581 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1582 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001583
Matt Evans28ccd292011-03-29 13:40:46 +11001584 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1585 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001586
Matt Evans28ccd292011-03-29 13:40:46 +11001587 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
John Yound115b042009-07-27 12:05:15 -07001588 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001589 /* Update the last valid endpoint context, if we deleted the last one */
Matt Evans28ccd292011-03-29 13:40:46 +11001590 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1591 LAST_CTX(last_ctx)) {
1592 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1593 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001594 }
Matt Evans28ccd292011-03-29 13:40:46 +11001595 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001596
1597 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1598
Sarah Sharpf94e01862009-04-27 19:58:38 -07001599 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1600 (unsigned int) ep->desc.bEndpointAddress,
1601 udev->slot_id,
1602 (unsigned int) new_drop_flags,
1603 (unsigned int) new_add_flags,
1604 (unsigned int) new_slot_info);
1605 return 0;
1606}
1607
1608/* Add an endpoint to a new possible bandwidth configuration for this device.
1609 * Only one call to this function is allowed per endpoint before
1610 * check_bandwidth() or reset_bandwidth() must be called.
1611 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1612 * add the endpoint to the schedule with possibly new parameters denoted by a
1613 * different endpoint descriptor in usb_host_endpoint.
1614 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1615 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001616 *
1617 * The USB core will not allow URBs to be queued to an endpoint until the
1618 * configuration or alt setting is installed in the device, so there's no need
1619 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001620 */
1621int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1622 struct usb_host_endpoint *ep)
1623{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001624 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001625 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001626 unsigned int ep_index;
1627 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001628 struct xhci_slot_ctx *slot_ctx;
1629 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001630 u32 added_ctxs;
1631 unsigned int last_ctx;
1632 u32 new_add_flags, new_drop_flags, new_slot_info;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001633 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001634 int ret = 0;
1635
Andiry Xu64927732010-10-14 07:22:45 -07001636 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001637 if (ret <= 0) {
1638 /* So we won't queue a reset ep command for a root hub */
1639 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001640 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001641 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001642 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001643 if (xhci->xhc_state & XHCI_STATE_DYING)
1644 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001645
1646 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1647 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1648 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1649 /* FIXME when we have to issue an evaluate endpoint command to
1650 * deal with ep0 max packet size changing once we get the
1651 * descriptors
1652 */
1653 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1654 __func__, added_ctxs);
1655 return 0;
1656 }
1657
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001658 virt_dev = xhci->devs[udev->slot_id];
1659 in_ctx = virt_dev->in_ctx;
1660 out_ctx = virt_dev->out_ctx;
John Yound115b042009-07-27 12:05:15 -07001661 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001662 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001663 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001664
1665 /* If this endpoint is already in use, and the upper layers are trying
1666 * to add it again without dropping it, reject the addition.
1667 */
1668 if (virt_dev->eps[ep_index].ring &&
1669 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1670 xhci_get_endpoint_flag(&ep->desc))) {
1671 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1672 "without dropping it.\n",
1673 (unsigned int) ep->desc.bEndpointAddress);
1674 return -EINVAL;
1675 }
1676
Sarah Sharpf94e01862009-04-27 19:58:38 -07001677 /* If the HCD has already noted the endpoint is enabled,
1678 * ignore this request.
1679 */
Matt Evans28ccd292011-03-29 13:40:46 +11001680 if (le32_to_cpu(ctrl_ctx->add_flags) &
1681 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001682 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1683 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001684 return 0;
1685 }
1686
Sarah Sharpf88ba782009-05-14 11:44:22 -07001687 /*
1688 * Configuration and alternate setting changes must be done in
1689 * process context, not interrupt context (or so documenation
1690 * for usb_set_interface() and usb_set_configuration() claim).
1691 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001692 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001693 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1694 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001695 return -ENOMEM;
1696 }
1697
Matt Evans28ccd292011-03-29 13:40:46 +11001698 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1699 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001700
1701 /* If xhci_endpoint_disable() was called for this endpoint, but the
1702 * xHC hasn't been notified yet through the check_bandwidth() call,
1703 * this re-adds a new state for the endpoint from the new endpoint
1704 * descriptors. We must drop and re-add this endpoint, so we leave the
1705 * drop flags alone.
1706 */
Matt Evans28ccd292011-03-29 13:40:46 +11001707 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001708
John Yound115b042009-07-27 12:05:15 -07001709 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001710 /* Update the last valid endpoint context, if we just added one past */
Matt Evans28ccd292011-03-29 13:40:46 +11001711 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1712 LAST_CTX(last_ctx)) {
1713 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1714 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001715 }
Matt Evans28ccd292011-03-29 13:40:46 +11001716 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001717
Sarah Sharpa1587d92009-07-27 12:03:15 -07001718 /* Store the usb_device pointer for later use */
1719 ep->hcpriv = udev;
1720
Sarah Sharpf94e01862009-04-27 19:58:38 -07001721 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1722 (unsigned int) ep->desc.bEndpointAddress,
1723 udev->slot_id,
1724 (unsigned int) new_drop_flags,
1725 (unsigned int) new_add_flags,
1726 (unsigned int) new_slot_info);
1727 return 0;
1728}
1729
John Yound115b042009-07-27 12:05:15 -07001730static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001731{
John Yound115b042009-07-27 12:05:15 -07001732 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001733 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001734 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001735 int i;
1736
1737 /* When a device's add flag and drop flag are zero, any subsequent
1738 * configure endpoint command will leave that endpoint's state
1739 * untouched. Make sure we don't leave any old state in the input
1740 * endpoint contexts.
1741 */
John Yound115b042009-07-27 12:05:15 -07001742 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1743 ctrl_ctx->drop_flags = 0;
1744 ctrl_ctx->add_flags = 0;
1745 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001746 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001747 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001748 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001749 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001750 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001751 ep_ctx->ep_info = 0;
1752 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001753 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001754 ep_ctx->tx_info = 0;
1755 }
1756}
1757
Sarah Sharpf2217e82009-08-07 14:04:43 -07001758static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001759 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001760{
1761 int ret;
1762
Sarah Sharp913a8a32009-09-04 10:53:13 -07001763 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001764 case COMP_ENOMEM:
1765 dev_warn(&udev->dev, "Not enough host controller resources "
1766 "for new device state.\n");
1767 ret = -ENOMEM;
1768 /* FIXME: can we allocate more resources for the HC? */
1769 break;
1770 case COMP_BW_ERR:
Hans de Goede71d85722012-01-04 23:29:18 +01001771 case COMP_2ND_BW_ERR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001772 dev_warn(&udev->dev, "Not enough bandwidth "
1773 "for new device state.\n");
1774 ret = -ENOSPC;
1775 /* FIXME: can we go back to the old state? */
1776 break;
1777 case COMP_TRB_ERR:
1778 /* the HCD set up something wrong */
1779 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1780 "add flag = 1, "
1781 "and endpoint is not disabled.\n");
1782 ret = -EINVAL;
1783 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001784 case COMP_DEV_ERR:
1785 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1786 "configure command.\n");
1787 ret = -ENODEV;
1788 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001789 case COMP_SUCCESS:
1790 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1791 ret = 0;
1792 break;
1793 default:
1794 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001795 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001796 ret = -EINVAL;
1797 break;
1798 }
1799 return ret;
1800}
1801
1802static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001803 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001804{
1805 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001806 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001807
Sarah Sharp913a8a32009-09-04 10:53:13 -07001808 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001809 case COMP_EINVAL:
1810 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1811 "context command.\n");
1812 ret = -EINVAL;
1813 break;
1814 case COMP_EBADSLT:
1815 dev_warn(&udev->dev, "WARN: slot not enabled for"
1816 "evaluate context command.\n");
1817 case COMP_CTX_STATE:
1818 dev_warn(&udev->dev, "WARN: invalid context state for "
1819 "evaluate context command.\n");
1820 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1821 ret = -EINVAL;
1822 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001823 case COMP_DEV_ERR:
1824 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1825 "context command.\n");
1826 ret = -ENODEV;
1827 break;
Alex He1bb73a82011-05-05 18:14:12 +08001828 case COMP_MEL_ERR:
1829 /* Max Exit Latency too large error */
1830 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1831 ret = -EINVAL;
1832 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001833 case COMP_SUCCESS:
1834 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1835 ret = 0;
1836 break;
1837 default:
1838 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001839 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001840 ret = -EINVAL;
1841 break;
1842 }
1843 return ret;
1844}
1845
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001846static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
1847 struct xhci_container_ctx *in_ctx)
1848{
1849 struct xhci_input_control_ctx *ctrl_ctx;
1850 u32 valid_add_flags;
1851 u32 valid_drop_flags;
1852
1853 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1854 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1855 * (bit 1). The default control endpoint is added during the Address
1856 * Device command and is never removed until the slot is disabled.
1857 */
1858 valid_add_flags = ctrl_ctx->add_flags >> 2;
1859 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1860
1861 /* Use hweight32 to count the number of ones in the add flags, or
1862 * number of endpoints added. Don't count endpoints that are changed
1863 * (both added and dropped).
1864 */
1865 return hweight32(valid_add_flags) -
1866 hweight32(valid_add_flags & valid_drop_flags);
1867}
1868
1869static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
1870 struct xhci_container_ctx *in_ctx)
1871{
1872 struct xhci_input_control_ctx *ctrl_ctx;
1873 u32 valid_add_flags;
1874 u32 valid_drop_flags;
1875
1876 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1877 valid_add_flags = ctrl_ctx->add_flags >> 2;
1878 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1879
1880 return hweight32(valid_drop_flags) -
1881 hweight32(valid_add_flags & valid_drop_flags);
1882}
1883
1884/*
1885 * We need to reserve the new number of endpoints before the configure endpoint
1886 * command completes. We can't subtract the dropped endpoints from the number
1887 * of active endpoints until the command completes because we can oversubscribe
1888 * the host in this case:
1889 *
1890 * - the first configure endpoint command drops more endpoints than it adds
1891 * - a second configure endpoint command that adds more endpoints is queued
1892 * - the first configure endpoint command fails, so the config is unchanged
1893 * - the second command may succeed, even though there isn't enough resources
1894 *
1895 * Must be called with xhci->lock held.
1896 */
1897static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
1898 struct xhci_container_ctx *in_ctx)
1899{
1900 u32 added_eps;
1901
1902 added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
1903 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
1904 xhci_dbg(xhci, "Not enough ep ctxs: "
1905 "%u active, need to add %u, limit is %u.\n",
1906 xhci->num_active_eps, added_eps,
1907 xhci->limit_active_eps);
1908 return -ENOMEM;
1909 }
1910 xhci->num_active_eps += added_eps;
1911 xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
1912 xhci->num_active_eps);
1913 return 0;
1914}
1915
1916/*
1917 * The configure endpoint was failed by the xHC for some other reason, so we
1918 * need to revert the resources that failed configuration would have used.
1919 *
1920 * Must be called with xhci->lock held.
1921 */
1922static void xhci_free_host_resources(struct xhci_hcd *xhci,
1923 struct xhci_container_ctx *in_ctx)
1924{
1925 u32 num_failed_eps;
1926
1927 num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
1928 xhci->num_active_eps -= num_failed_eps;
1929 xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
1930 num_failed_eps,
1931 xhci->num_active_eps);
1932}
1933
1934/*
1935 * Now that the command has completed, clean up the active endpoint count by
1936 * subtracting out the endpoints that were dropped (but not changed).
1937 *
1938 * Must be called with xhci->lock held.
1939 */
1940static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
1941 struct xhci_container_ctx *in_ctx)
1942{
1943 u32 num_dropped_eps;
1944
1945 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
1946 xhci->num_active_eps -= num_dropped_eps;
1947 if (num_dropped_eps)
1948 xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
1949 num_dropped_eps,
1950 xhci->num_active_eps);
1951}
1952
Sarah Sharpc29eea62011-09-02 11:05:52 -07001953unsigned int xhci_get_block_size(struct usb_device *udev)
1954{
1955 switch (udev->speed) {
1956 case USB_SPEED_LOW:
1957 case USB_SPEED_FULL:
1958 return FS_BLOCK;
1959 case USB_SPEED_HIGH:
1960 return HS_BLOCK;
1961 case USB_SPEED_SUPER:
1962 return SS_BLOCK;
1963 case USB_SPEED_UNKNOWN:
1964 case USB_SPEED_WIRELESS:
1965 default:
1966 /* Should never happen */
1967 return 1;
1968 }
1969}
1970
1971unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
1972{
1973 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
1974 return LS_OVERHEAD;
1975 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
1976 return FS_OVERHEAD;
1977 return HS_OVERHEAD;
1978}
1979
1980/* If we are changing a LS/FS device under a HS hub,
1981 * make sure (if we are activating a new TT) that the HS bus has enough
1982 * bandwidth for this new TT.
1983 */
1984static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
1985 struct xhci_virt_device *virt_dev,
1986 int old_active_eps)
1987{
1988 struct xhci_interval_bw_table *bw_table;
1989 struct xhci_tt_bw_info *tt_info;
1990
1991 /* Find the bandwidth table for the root port this TT is attached to. */
1992 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
1993 tt_info = virt_dev->tt_info;
1994 /* If this TT already had active endpoints, the bandwidth for this TT
1995 * has already been added. Removing all periodic endpoints (and thus
1996 * making the TT enactive) will only decrease the bandwidth used.
1997 */
1998 if (old_active_eps)
1999 return 0;
2000 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2001 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2002 return -ENOMEM;
2003 return 0;
2004 }
2005 /* Not sure why we would have no new active endpoints...
2006 *
2007 * Maybe because of an Evaluate Context change for a hub update or a
2008 * control endpoint 0 max packet size change?
2009 * FIXME: skip the bandwidth calculation in that case.
2010 */
2011 return 0;
2012}
2013
Sarah Sharp2b698992011-09-13 16:41:13 -07002014static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2015 struct xhci_virt_device *virt_dev)
2016{
2017 unsigned int bw_reserved;
2018
2019 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2020 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2021 return -ENOMEM;
2022
2023 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2024 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2025 return -ENOMEM;
2026
2027 return 0;
2028}
2029
Sarah Sharpc29eea62011-09-02 11:05:52 -07002030/*
2031 * This algorithm is a very conservative estimate of the worst-case scheduling
2032 * scenario for any one interval. The hardware dynamically schedules the
2033 * packets, so we can't tell which microframe could be the limiting factor in
2034 * the bandwidth scheduling. This only takes into account periodic endpoints.
2035 *
2036 * Obviously, we can't solve an NP complete problem to find the minimum worst
2037 * case scenario. Instead, we come up with an estimate that is no less than
2038 * the worst case bandwidth used for any one microframe, but may be an
2039 * over-estimate.
2040 *
2041 * We walk the requirements for each endpoint by interval, starting with the
2042 * smallest interval, and place packets in the schedule where there is only one
2043 * possible way to schedule packets for that interval. In order to simplify
2044 * this algorithm, we record the largest max packet size for each interval, and
2045 * assume all packets will be that size.
2046 *
2047 * For interval 0, we obviously must schedule all packets for each interval.
2048 * The bandwidth for interval 0 is just the amount of data to be transmitted
2049 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2050 * the number of packets).
2051 *
2052 * For interval 1, we have two possible microframes to schedule those packets
2053 * in. For this algorithm, if we can schedule the same number of packets for
2054 * each possible scheduling opportunity (each microframe), we will do so. The
2055 * remaining number of packets will be saved to be transmitted in the gaps in
2056 * the next interval's scheduling sequence.
2057 *
2058 * As we move those remaining packets to be scheduled with interval 2 packets,
2059 * we have to double the number of remaining packets to transmit. This is
2060 * because the intervals are actually powers of 2, and we would be transmitting
2061 * the previous interval's packets twice in this interval. We also have to be
2062 * sure that when we look at the largest max packet size for this interval, we
2063 * also look at the largest max packet size for the remaining packets and take
2064 * the greater of the two.
2065 *
2066 * The algorithm continues to evenly distribute packets in each scheduling
2067 * opportunity, and push the remaining packets out, until we get to the last
2068 * interval. Then those packets and their associated overhead are just added
2069 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002070 */
2071static int xhci_check_bw_table(struct xhci_hcd *xhci,
2072 struct xhci_virt_device *virt_dev,
2073 int old_active_eps)
2074{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002075 unsigned int bw_reserved;
2076 unsigned int max_bandwidth;
2077 unsigned int bw_used;
2078 unsigned int block_size;
2079 struct xhci_interval_bw_table *bw_table;
2080 unsigned int packet_size = 0;
2081 unsigned int overhead = 0;
2082 unsigned int packets_transmitted = 0;
2083 unsigned int packets_remaining = 0;
2084 unsigned int i;
2085
Sarah Sharp2b698992011-09-13 16:41:13 -07002086 if (virt_dev->udev->speed == USB_SPEED_SUPER)
2087 return xhci_check_ss_bw(xhci, virt_dev);
2088
Sarah Sharpc29eea62011-09-02 11:05:52 -07002089 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2090 max_bandwidth = HS_BW_LIMIT;
2091 /* Convert percent of bus BW reserved to blocks reserved */
2092 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2093 } else {
2094 max_bandwidth = FS_BW_LIMIT;
2095 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2096 }
2097
2098 bw_table = virt_dev->bw_table;
2099 /* We need to translate the max packet size and max ESIT payloads into
2100 * the units the hardware uses.
2101 */
2102 block_size = xhci_get_block_size(virt_dev->udev);
2103
2104 /* If we are manipulating a LS/FS device under a HS hub, double check
2105 * that the HS bus has enough bandwidth if we are activing a new TT.
2106 */
2107 if (virt_dev->tt_info) {
2108 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2109 virt_dev->real_port);
2110 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2111 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2112 "newly activated TT.\n");
2113 return -ENOMEM;
2114 }
2115 xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
2116 virt_dev->tt_info->slot_id,
2117 virt_dev->tt_info->ttport);
2118 } else {
2119 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2120 virt_dev->real_port);
2121 }
2122
2123 /* Add in how much bandwidth will be used for interval zero, or the
2124 * rounded max ESIT payload + number of packets * largest overhead.
2125 */
2126 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2127 bw_table->interval_bw[0].num_packets *
2128 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2129
2130 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2131 unsigned int bw_added;
2132 unsigned int largest_mps;
2133 unsigned int interval_overhead;
2134
2135 /*
2136 * How many packets could we transmit in this interval?
2137 * If packets didn't fit in the previous interval, we will need
2138 * to transmit that many packets twice within this interval.
2139 */
2140 packets_remaining = 2 * packets_remaining +
2141 bw_table->interval_bw[i].num_packets;
2142
2143 /* Find the largest max packet size of this or the previous
2144 * interval.
2145 */
2146 if (list_empty(&bw_table->interval_bw[i].endpoints))
2147 largest_mps = 0;
2148 else {
2149 struct xhci_virt_ep *virt_ep;
2150 struct list_head *ep_entry;
2151
2152 ep_entry = bw_table->interval_bw[i].endpoints.next;
2153 virt_ep = list_entry(ep_entry,
2154 struct xhci_virt_ep, bw_endpoint_list);
2155 /* Convert to blocks, rounding up */
2156 largest_mps = DIV_ROUND_UP(
2157 virt_ep->bw_info.max_packet_size,
2158 block_size);
2159 }
2160 if (largest_mps > packet_size)
2161 packet_size = largest_mps;
2162
2163 /* Use the larger overhead of this or the previous interval. */
2164 interval_overhead = xhci_get_largest_overhead(
2165 &bw_table->interval_bw[i]);
2166 if (interval_overhead > overhead)
2167 overhead = interval_overhead;
2168
2169 /* How many packets can we evenly distribute across
2170 * (1 << (i + 1)) possible scheduling opportunities?
2171 */
2172 packets_transmitted = packets_remaining >> (i + 1);
2173
2174 /* Add in the bandwidth used for those scheduled packets */
2175 bw_added = packets_transmitted * (overhead + packet_size);
2176
2177 /* How many packets do we have remaining to transmit? */
2178 packets_remaining = packets_remaining % (1 << (i + 1));
2179
2180 /* What largest max packet size should those packets have? */
2181 /* If we've transmitted all packets, don't carry over the
2182 * largest packet size.
2183 */
2184 if (packets_remaining == 0) {
2185 packet_size = 0;
2186 overhead = 0;
2187 } else if (packets_transmitted > 0) {
2188 /* Otherwise if we do have remaining packets, and we've
2189 * scheduled some packets in this interval, take the
2190 * largest max packet size from endpoints with this
2191 * interval.
2192 */
2193 packet_size = largest_mps;
2194 overhead = interval_overhead;
2195 }
2196 /* Otherwise carry over packet_size and overhead from the last
2197 * time we had a remainder.
2198 */
2199 bw_used += bw_added;
2200 if (bw_used > max_bandwidth) {
2201 xhci_warn(xhci, "Not enough bandwidth. "
2202 "Proposed: %u, Max: %u\n",
2203 bw_used, max_bandwidth);
2204 return -ENOMEM;
2205 }
2206 }
2207 /*
2208 * Ok, we know we have some packets left over after even-handedly
2209 * scheduling interval 15. We don't know which microframes they will
2210 * fit into, so we over-schedule and say they will be scheduled every
2211 * microframe.
2212 */
2213 if (packets_remaining > 0)
2214 bw_used += overhead + packet_size;
2215
2216 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2217 unsigned int port_index = virt_dev->real_port - 1;
2218
2219 /* OK, we're manipulating a HS device attached to a
2220 * root port bandwidth domain. Include the number of active TTs
2221 * in the bandwidth used.
2222 */
2223 bw_used += TT_HS_OVERHEAD *
2224 xhci->rh_bw[port_index].num_active_tts;
2225 }
2226
2227 xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2228 "Available: %u " "percent\n",
2229 bw_used, max_bandwidth, bw_reserved,
2230 (max_bandwidth - bw_used - bw_reserved) * 100 /
2231 max_bandwidth);
2232
2233 bw_used += bw_reserved;
2234 if (bw_used > max_bandwidth) {
2235 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2236 bw_used, max_bandwidth);
2237 return -ENOMEM;
2238 }
2239
2240 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002241 return 0;
2242}
2243
2244static bool xhci_is_async_ep(unsigned int ep_type)
2245{
2246 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2247 ep_type != ISOC_IN_EP &&
2248 ep_type != INT_IN_EP);
2249}
2250
Sarah Sharp2b698992011-09-13 16:41:13 -07002251static bool xhci_is_sync_in_ep(unsigned int ep_type)
2252{
2253 return (ep_type == ISOC_IN_EP || ep_type != INT_IN_EP);
2254}
2255
2256static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2257{
2258 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2259
2260 if (ep_bw->ep_interval == 0)
2261 return SS_OVERHEAD_BURST +
2262 (ep_bw->mult * ep_bw->num_packets *
2263 (SS_OVERHEAD + mps));
2264 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2265 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2266 1 << ep_bw->ep_interval);
2267
2268}
2269
Sarah Sharp2e279802011-09-02 11:05:50 -07002270void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2271 struct xhci_bw_info *ep_bw,
2272 struct xhci_interval_bw_table *bw_table,
2273 struct usb_device *udev,
2274 struct xhci_virt_ep *virt_ep,
2275 struct xhci_tt_bw_info *tt_info)
2276{
2277 struct xhci_interval_bw *interval_bw;
2278 int normalized_interval;
2279
Sarah Sharp2b698992011-09-13 16:41:13 -07002280 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002281 return;
2282
Sarah Sharp2b698992011-09-13 16:41:13 -07002283 if (udev->speed == USB_SPEED_SUPER) {
2284 if (xhci_is_sync_in_ep(ep_bw->type))
2285 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2286 xhci_get_ss_bw_consumed(ep_bw);
2287 else
2288 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2289 xhci_get_ss_bw_consumed(ep_bw);
2290 return;
2291 }
2292
2293 /* SuperSpeed endpoints never get added to intervals in the table, so
2294 * this check is only valid for HS/FS/LS devices.
2295 */
2296 if (list_empty(&virt_ep->bw_endpoint_list))
2297 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002298 /* For LS/FS devices, we need to translate the interval expressed in
2299 * microframes to frames.
2300 */
2301 if (udev->speed == USB_SPEED_HIGH)
2302 normalized_interval = ep_bw->ep_interval;
2303 else
2304 normalized_interval = ep_bw->ep_interval - 3;
2305
2306 if (normalized_interval == 0)
2307 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2308 interval_bw = &bw_table->interval_bw[normalized_interval];
2309 interval_bw->num_packets -= ep_bw->num_packets;
2310 switch (udev->speed) {
2311 case USB_SPEED_LOW:
2312 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2313 break;
2314 case USB_SPEED_FULL:
2315 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2316 break;
2317 case USB_SPEED_HIGH:
2318 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2319 break;
2320 case USB_SPEED_SUPER:
2321 case USB_SPEED_UNKNOWN:
2322 case USB_SPEED_WIRELESS:
2323 /* Should never happen because only LS/FS/HS endpoints will get
2324 * added to the endpoint list.
2325 */
2326 return;
2327 }
2328 if (tt_info)
2329 tt_info->active_eps -= 1;
2330 list_del_init(&virt_ep->bw_endpoint_list);
2331}
2332
2333static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2334 struct xhci_bw_info *ep_bw,
2335 struct xhci_interval_bw_table *bw_table,
2336 struct usb_device *udev,
2337 struct xhci_virt_ep *virt_ep,
2338 struct xhci_tt_bw_info *tt_info)
2339{
2340 struct xhci_interval_bw *interval_bw;
2341 struct xhci_virt_ep *smaller_ep;
2342 int normalized_interval;
2343
2344 if (xhci_is_async_ep(ep_bw->type))
2345 return;
2346
Sarah Sharp2b698992011-09-13 16:41:13 -07002347 if (udev->speed == USB_SPEED_SUPER) {
2348 if (xhci_is_sync_in_ep(ep_bw->type))
2349 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2350 xhci_get_ss_bw_consumed(ep_bw);
2351 else
2352 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2353 xhci_get_ss_bw_consumed(ep_bw);
2354 return;
2355 }
2356
Sarah Sharp2e279802011-09-02 11:05:50 -07002357 /* For LS/FS devices, we need to translate the interval expressed in
2358 * microframes to frames.
2359 */
2360 if (udev->speed == USB_SPEED_HIGH)
2361 normalized_interval = ep_bw->ep_interval;
2362 else
2363 normalized_interval = ep_bw->ep_interval - 3;
2364
2365 if (normalized_interval == 0)
2366 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2367 interval_bw = &bw_table->interval_bw[normalized_interval];
2368 interval_bw->num_packets += ep_bw->num_packets;
2369 switch (udev->speed) {
2370 case USB_SPEED_LOW:
2371 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2372 break;
2373 case USB_SPEED_FULL:
2374 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2375 break;
2376 case USB_SPEED_HIGH:
2377 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2378 break;
2379 case USB_SPEED_SUPER:
2380 case USB_SPEED_UNKNOWN:
2381 case USB_SPEED_WIRELESS:
2382 /* Should never happen because only LS/FS/HS endpoints will get
2383 * added to the endpoint list.
2384 */
2385 return;
2386 }
2387
2388 if (tt_info)
2389 tt_info->active_eps += 1;
2390 /* Insert the endpoint into the list, largest max packet size first. */
2391 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2392 bw_endpoint_list) {
2393 if (ep_bw->max_packet_size >=
2394 smaller_ep->bw_info.max_packet_size) {
2395 /* Add the new ep before the smaller endpoint */
2396 list_add_tail(&virt_ep->bw_endpoint_list,
2397 &smaller_ep->bw_endpoint_list);
2398 return;
2399 }
2400 }
2401 /* Add the new endpoint at the end of the list. */
2402 list_add_tail(&virt_ep->bw_endpoint_list,
2403 &interval_bw->endpoints);
2404}
2405
2406void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2407 struct xhci_virt_device *virt_dev,
2408 int old_active_eps)
2409{
2410 struct xhci_root_port_bw_info *rh_bw_info;
2411 if (!virt_dev->tt_info)
2412 return;
2413
2414 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2415 if (old_active_eps == 0 &&
2416 virt_dev->tt_info->active_eps != 0) {
2417 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002418 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002419 } else if (old_active_eps != 0 &&
2420 virt_dev->tt_info->active_eps == 0) {
2421 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002422 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002423 }
2424}
2425
2426static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2427 struct xhci_virt_device *virt_dev,
2428 struct xhci_container_ctx *in_ctx)
2429{
2430 struct xhci_bw_info ep_bw_info[31];
2431 int i;
2432 struct xhci_input_control_ctx *ctrl_ctx;
2433 int old_active_eps = 0;
2434
Sarah Sharp2e279802011-09-02 11:05:50 -07002435 if (virt_dev->tt_info)
2436 old_active_eps = virt_dev->tt_info->active_eps;
2437
2438 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2439
2440 for (i = 0; i < 31; i++) {
2441 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2442 continue;
2443
2444 /* Make a copy of the BW info in case we need to revert this */
2445 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2446 sizeof(ep_bw_info[i]));
2447 /* Drop the endpoint from the interval table if the endpoint is
2448 * being dropped or changed.
2449 */
2450 if (EP_IS_DROPPED(ctrl_ctx, i))
2451 xhci_drop_ep_from_interval_table(xhci,
2452 &virt_dev->eps[i].bw_info,
2453 virt_dev->bw_table,
2454 virt_dev->udev,
2455 &virt_dev->eps[i],
2456 virt_dev->tt_info);
2457 }
2458 /* Overwrite the information stored in the endpoints' bw_info */
2459 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2460 for (i = 0; i < 31; i++) {
2461 /* Add any changed or added endpoints to the interval table */
2462 if (EP_IS_ADDED(ctrl_ctx, i))
2463 xhci_add_ep_to_interval_table(xhci,
2464 &virt_dev->eps[i].bw_info,
2465 virt_dev->bw_table,
2466 virt_dev->udev,
2467 &virt_dev->eps[i],
2468 virt_dev->tt_info);
2469 }
2470
2471 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2472 /* Ok, this fits in the bandwidth we have.
2473 * Update the number of active TTs.
2474 */
2475 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2476 return 0;
2477 }
2478
2479 /* We don't have enough bandwidth for this, revert the stored info. */
2480 for (i = 0; i < 31; i++) {
2481 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2482 continue;
2483
2484 /* Drop the new copies of any added or changed endpoints from
2485 * the interval table.
2486 */
2487 if (EP_IS_ADDED(ctrl_ctx, i)) {
2488 xhci_drop_ep_from_interval_table(xhci,
2489 &virt_dev->eps[i].bw_info,
2490 virt_dev->bw_table,
2491 virt_dev->udev,
2492 &virt_dev->eps[i],
2493 virt_dev->tt_info);
2494 }
2495 /* Revert the endpoint back to its old information */
2496 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2497 sizeof(ep_bw_info[i]));
2498 /* Add any changed or dropped endpoints back into the table */
2499 if (EP_IS_DROPPED(ctrl_ctx, i))
2500 xhci_add_ep_to_interval_table(xhci,
2501 &virt_dev->eps[i].bw_info,
2502 virt_dev->bw_table,
2503 virt_dev->udev,
2504 &virt_dev->eps[i],
2505 virt_dev->tt_info);
2506 }
2507 return -ENOMEM;
2508}
2509
2510
Sarah Sharpf2217e82009-08-07 14:04:43 -07002511/* Issue a configure endpoint command or evaluate context command
2512 * and wait for it to finish.
2513 */
2514static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002515 struct usb_device *udev,
2516 struct xhci_command *command,
2517 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002518{
2519 int ret;
2520 int timeleft;
2521 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002522 struct xhci_container_ctx *in_ctx;
2523 struct completion *cmd_completion;
Matt Evans28ccd292011-03-29 13:40:46 +11002524 u32 *cmd_status;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002525 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002526
2527 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002528 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002529
Sarah Sharp750645f2011-09-02 11:05:43 -07002530 if (command)
2531 in_ctx = command->in_ctx;
2532 else
2533 in_ctx = virt_dev->in_ctx;
2534
2535 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2536 xhci_reserve_host_resources(xhci, in_ctx)) {
2537 spin_unlock_irqrestore(&xhci->lock, flags);
2538 xhci_warn(xhci, "Not enough host resources, "
2539 "active endpoint contexts = %u\n",
2540 xhci->num_active_eps);
2541 return -ENOMEM;
2542 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002543 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2544 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2545 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2546 xhci_free_host_resources(xhci, in_ctx);
2547 spin_unlock_irqrestore(&xhci->lock, flags);
2548 xhci_warn(xhci, "Not enough bandwidth\n");
2549 return -ENOMEM;
2550 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002551
2552 if (command) {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002553 cmd_completion = command->completion;
2554 cmd_status = &command->status;
2555 command->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002556
2557 /* Enqueue pointer can be left pointing to the link TRB,
2558 * we must handle that
2559 */
Matt Evansf5960b62011-06-01 10:22:55 +10002560 if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002561 command->command_trb =
2562 xhci->cmd_ring->enq_seg->next->trbs;
2563
Sarah Sharp913a8a32009-09-04 10:53:13 -07002564 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2565 } else {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002566 cmd_completion = &virt_dev->cmd_completion;
2567 cmd_status = &virt_dev->cmd_status;
2568 }
Andiry Xu1d680642010-03-12 17:10:04 +08002569 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002570
Sarah Sharpf2217e82009-08-07 14:04:43 -07002571 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07002572 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2573 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002574 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07002575 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07002576 udev->slot_id);
2577 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08002578 if (command)
2579 list_del(&command->cmd_list);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002580 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2581 xhci_free_host_resources(xhci, in_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002582 spin_unlock_irqrestore(&xhci->lock, flags);
2583 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
2584 return -ENOMEM;
2585 }
2586 xhci_ring_cmd_db(xhci);
2587 spin_unlock_irqrestore(&xhci->lock, flags);
2588
2589 /* Wait for the configure endpoint command to complete */
2590 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07002591 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07002592 USB_CTRL_SET_TIMEOUT);
2593 if (timeleft <= 0) {
2594 xhci_warn(xhci, "%s while waiting for %s command\n",
2595 timeleft == 0 ? "Timeout" : "Signal",
2596 ctx_change == 0 ?
2597 "configure endpoint" :
2598 "evaluate context");
2599 /* FIXME cancel the configure endpoint command */
2600 return -ETIME;
2601 }
2602
2603 if (!ctx_change)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002604 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2605 else
2606 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
2607
2608 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2609 spin_lock_irqsave(&xhci->lock, flags);
2610 /* If the command failed, remove the reserved resources.
2611 * Otherwise, clean up the estimate to include dropped eps.
2612 */
2613 if (ret)
2614 xhci_free_host_resources(xhci, in_ctx);
2615 else
2616 xhci_finish_resource_reservation(xhci, in_ctx);
2617 spin_unlock_irqrestore(&xhci->lock, flags);
2618 }
2619 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002620}
2621
Sarah Sharpf88ba782009-05-14 11:44:22 -07002622/* Called after one or more calls to xhci_add_endpoint() or
2623 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2624 * to call xhci_reset_bandwidth().
2625 *
2626 * Since we are in the middle of changing either configuration or
2627 * installing a new alt setting, the USB core won't allow URBs to be
2628 * enqueued for any endpoint on the old config or interface. Nothing
2629 * else should be touching the xhci->devs[slot_id] structure, so we
2630 * don't need to take the xhci->lock for manipulating that.
2631 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002632int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2633{
2634 int i;
2635 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002636 struct xhci_hcd *xhci;
2637 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002638 struct xhci_input_control_ctx *ctrl_ctx;
2639 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002640
Andiry Xu64927732010-10-14 07:22:45 -07002641 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002642 if (ret <= 0)
2643 return ret;
2644 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002645 if (xhci->xhc_state & XHCI_STATE_DYING)
2646 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002647
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002648 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002649 virt_dev = xhci->devs[udev->slot_id];
2650
2651 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07002652 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002653 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2654 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2655 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002656
2657 /* Don't issue the command if there's no endpoints to update. */
2658 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2659 ctrl_ctx->drop_flags == 0)
2660 return 0;
2661
Sarah Sharpf94e01862009-04-27 19:58:38 -07002662 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07002663 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2664 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002665 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002666
Sarah Sharp913a8a32009-09-04 10:53:13 -07002667 ret = xhci_configure_endpoint(xhci, udev, NULL,
2668 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002669 if (ret) {
2670 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002671 return ret;
2672 }
2673
2674 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07002675 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002676 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002677
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002678 /* Free any rings that were dropped, but not changed. */
2679 for (i = 1; i < 31; ++i) {
Matt Evans4819fef2011-06-01 13:01:07 +10002680 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2681 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002682 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2683 }
John Yound115b042009-07-27 12:05:15 -07002684 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002685 /*
2686 * Install any rings for completely new endpoints or changed endpoints,
2687 * and free or cache any old rings from changed endpoints.
2688 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002689 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002690 if (!virt_dev->eps[i].new_ring)
2691 continue;
2692 /* Only cache or free the old ring if it exists.
2693 * It may not if this is the first add of an endpoint.
2694 */
2695 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08002696 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002697 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002698 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2699 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002700 }
2701
Sarah Sharpf94e01862009-04-27 19:58:38 -07002702 return ret;
2703}
2704
2705void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2706{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002707 struct xhci_hcd *xhci;
2708 struct xhci_virt_device *virt_dev;
2709 int i, ret;
2710
Andiry Xu64927732010-10-14 07:22:45 -07002711 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002712 if (ret <= 0)
2713 return;
2714 xhci = hcd_to_xhci(hcd);
2715
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002716 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002717 virt_dev = xhci->devs[udev->slot_id];
2718 /* Free any rings allocated for added endpoints */
2719 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002720 if (virt_dev->eps[i].new_ring) {
2721 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2722 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002723 }
2724 }
John Yound115b042009-07-27 12:05:15 -07002725 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002726}
2727
Sarah Sharp5270b952009-09-04 10:53:11 -07002728static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002729 struct xhci_container_ctx *in_ctx,
2730 struct xhci_container_ctx *out_ctx,
2731 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002732{
2733 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002734 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002735 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2736 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002737 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002738 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002739
Sarah Sharp913a8a32009-09-04 10:53:13 -07002740 xhci_dbg(xhci, "Input Context:\n");
2741 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07002742}
2743
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002744static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002745 unsigned int slot_id, unsigned int ep_index,
2746 struct xhci_dequeue_state *deq_state)
2747{
2748 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002749 struct xhci_ep_ctx *ep_ctx;
2750 u32 added_ctxs;
2751 dma_addr_t addr;
2752
Sarah Sharp913a8a32009-09-04 10:53:13 -07002753 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2754 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002755 in_ctx = xhci->devs[slot_id]->in_ctx;
2756 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2757 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2758 deq_state->new_deq_ptr);
2759 if (addr == 0) {
2760 xhci_warn(xhci, "WARN Cannot submit config ep after "
2761 "reset ep command\n");
2762 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2763 deq_state->new_deq_seg,
2764 deq_state->new_deq_ptr);
2765 return;
2766 }
Matt Evans28ccd292011-03-29 13:40:46 +11002767 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002768
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002769 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002770 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
2771 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002772}
2773
Sarah Sharp82d10092009-08-07 14:04:52 -07002774void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002775 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07002776{
2777 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002778 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07002779
2780 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002781 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002782 /* We need to move the HW's dequeue pointer past this TD,
2783 * or it will attempt to resend it on the next doorbell ring.
2784 */
2785 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002786 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002787 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002788
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002789 /* HW with the reset endpoint quirk will use the saved dequeue state to
2790 * issue a configure endpoint command later.
2791 */
2792 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
2793 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002794 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002795 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002796 } else {
2797 /* Better hope no one uses the input context between now and the
2798 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002799 * XXX: No idea how this hardware will react when stream rings
2800 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002801 */
2802 xhci_dbg(xhci, "Setting up input context for "
2803 "configure endpoint command\n");
2804 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2805 ep_index, &deq_state);
2806 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002807}
2808
Sarah Sharpa1587d92009-07-27 12:03:15 -07002809/* Deal with stalled endpoints. The core should have sent the control message
2810 * to clear the halt condition. However, we need to make the xHCI hardware
2811 * reset its sequence number, since a device will expect a sequence number of
2812 * zero after the halt condition is cleared.
2813 * Context: in_interrupt
2814 */
2815void xhci_endpoint_reset(struct usb_hcd *hcd,
2816 struct usb_host_endpoint *ep)
2817{
2818 struct xhci_hcd *xhci;
2819 struct usb_device *udev;
2820 unsigned int ep_index;
2821 unsigned long flags;
2822 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002823 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002824
2825 xhci = hcd_to_xhci(hcd);
2826 udev = (struct usb_device *) ep->hcpriv;
2827 /* Called with a root hub endpoint (or an endpoint that wasn't added
2828 * with xhci_add_endpoint()
2829 */
2830 if (!ep->hcpriv)
2831 return;
2832 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002833 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2834 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002835 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
2836 ep->desc.bEndpointAddress);
2837 return;
2838 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002839 if (usb_endpoint_xfer_control(&ep->desc)) {
2840 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
2841 return;
2842 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07002843
2844 xhci_dbg(xhci, "Queueing reset endpoint command\n");
2845 spin_lock_irqsave(&xhci->lock, flags);
2846 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002847 /*
2848 * Can't change the ring dequeue pointer until it's transitioned to the
2849 * stopped state, which is only upon a successful reset endpoint
2850 * command. Better hope that last command worked!
2851 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002852 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002853 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
2854 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002855 xhci_ring_cmd_db(xhci);
2856 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07002857 virt_ep->stopped_td = NULL;
2858 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07002859 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002860 spin_unlock_irqrestore(&xhci->lock, flags);
2861
2862 if (ret)
2863 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
2864}
2865
Sarah Sharp8df75f42010-04-02 15:34:16 -07002866static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2867 struct usb_device *udev, struct usb_host_endpoint *ep,
2868 unsigned int slot_id)
2869{
2870 int ret;
2871 unsigned int ep_index;
2872 unsigned int ep_state;
2873
2874 if (!ep)
2875 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002876 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002877 if (ret <= 0)
2878 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04002879 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002880 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2881 " descriptor for ep 0x%x does not support streams\n",
2882 ep->desc.bEndpointAddress);
2883 return -EINVAL;
2884 }
2885
2886 ep_index = xhci_get_endpoint_index(&ep->desc);
2887 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2888 if (ep_state & EP_HAS_STREAMS ||
2889 ep_state & EP_GETTING_STREAMS) {
2890 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2891 "already has streams set up.\n",
2892 ep->desc.bEndpointAddress);
2893 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2894 "dynamic stream context array reallocation.\n");
2895 return -EINVAL;
2896 }
2897 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2898 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2899 "endpoint 0x%x; URBs are pending.\n",
2900 ep->desc.bEndpointAddress);
2901 return -EINVAL;
2902 }
2903 return 0;
2904}
2905
2906static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
2907 unsigned int *num_streams, unsigned int *num_stream_ctxs)
2908{
2909 unsigned int max_streams;
2910
2911 /* The stream context array size must be a power of two */
2912 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
2913 /*
2914 * Find out how many primary stream array entries the host controller
2915 * supports. Later we may use secondary stream arrays (similar to 2nd
2916 * level page entries), but that's an optional feature for xHCI host
2917 * controllers. xHCs must support at least 4 stream IDs.
2918 */
2919 max_streams = HCC_MAX_PSA(xhci->hcc_params);
2920 if (*num_stream_ctxs > max_streams) {
2921 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
2922 max_streams);
2923 *num_stream_ctxs = max_streams;
2924 *num_streams = max_streams;
2925 }
2926}
2927
2928/* Returns an error code if one of the endpoint already has streams.
2929 * This does not change any data structures, it only checks and gathers
2930 * information.
2931 */
2932static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
2933 struct usb_device *udev,
2934 struct usb_host_endpoint **eps, unsigned int num_eps,
2935 unsigned int *num_streams, u32 *changed_ep_bitmask)
2936{
Sarah Sharp8df75f42010-04-02 15:34:16 -07002937 unsigned int max_streams;
2938 unsigned int endpoint_flag;
2939 int i;
2940 int ret;
2941
2942 for (i = 0; i < num_eps; i++) {
2943 ret = xhci_check_streams_endpoint(xhci, udev,
2944 eps[i], udev->slot_id);
2945 if (ret < 0)
2946 return ret;
2947
Felipe Balbi18b7ede2012-01-02 13:35:41 +02002948 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002949 if (max_streams < (*num_streams - 1)) {
2950 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
2951 eps[i]->desc.bEndpointAddress,
2952 max_streams);
2953 *num_streams = max_streams+1;
2954 }
2955
2956 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
2957 if (*changed_ep_bitmask & endpoint_flag)
2958 return -EINVAL;
2959 *changed_ep_bitmask |= endpoint_flag;
2960 }
2961 return 0;
2962}
2963
2964static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
2965 struct usb_device *udev,
2966 struct usb_host_endpoint **eps, unsigned int num_eps)
2967{
2968 u32 changed_ep_bitmask = 0;
2969 unsigned int slot_id;
2970 unsigned int ep_index;
2971 unsigned int ep_state;
2972 int i;
2973
2974 slot_id = udev->slot_id;
2975 if (!xhci->devs[slot_id])
2976 return 0;
2977
2978 for (i = 0; i < num_eps; i++) {
2979 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2980 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2981 /* Are streams already being freed for the endpoint? */
2982 if (ep_state & EP_GETTING_NO_STREAMS) {
2983 xhci_warn(xhci, "WARN Can't disable streams for "
2984 "endpoint 0x%x\n, "
2985 "streams are being disabled already.",
2986 eps[i]->desc.bEndpointAddress);
2987 return 0;
2988 }
2989 /* Are there actually any streams to free? */
2990 if (!(ep_state & EP_HAS_STREAMS) &&
2991 !(ep_state & EP_GETTING_STREAMS)) {
2992 xhci_warn(xhci, "WARN Can't disable streams for "
2993 "endpoint 0x%x\n, "
2994 "streams are already disabled!",
2995 eps[i]->desc.bEndpointAddress);
2996 xhci_warn(xhci, "WARN xhci_free_streams() called "
2997 "with non-streams endpoint\n");
2998 return 0;
2999 }
3000 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3001 }
3002 return changed_ep_bitmask;
3003}
3004
3005/*
3006 * The USB device drivers use this function (though the HCD interface in USB
3007 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3008 * coordinate mass storage command queueing across multiple endpoints (basically
3009 * a stream ID == a task ID).
3010 *
3011 * Setting up streams involves allocating the same size stream context array
3012 * for each endpoint and issuing a configure endpoint command for all endpoints.
3013 *
3014 * Don't allow the call to succeed if one endpoint only supports one stream
3015 * (which means it doesn't support streams at all).
3016 *
3017 * Drivers may get less stream IDs than they asked for, if the host controller
3018 * hardware or endpoints claim they can't support the number of requested
3019 * stream IDs.
3020 */
3021int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3022 struct usb_host_endpoint **eps, unsigned int num_eps,
3023 unsigned int num_streams, gfp_t mem_flags)
3024{
3025 int i, ret;
3026 struct xhci_hcd *xhci;
3027 struct xhci_virt_device *vdev;
3028 struct xhci_command *config_cmd;
3029 unsigned int ep_index;
3030 unsigned int num_stream_ctxs;
3031 unsigned long flags;
3032 u32 changed_ep_bitmask = 0;
3033
3034 if (!eps)
3035 return -EINVAL;
3036
3037 /* Add one to the number of streams requested to account for
3038 * stream 0 that is reserved for xHCI usage.
3039 */
3040 num_streams += 1;
3041 xhci = hcd_to_xhci(hcd);
3042 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3043 num_streams);
3044
3045 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3046 if (!config_cmd) {
3047 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3048 return -ENOMEM;
3049 }
3050
3051 /* Check to make sure all endpoints are not already configured for
3052 * streams. While we're at it, find the maximum number of streams that
3053 * all the endpoints will support and check for duplicate endpoints.
3054 */
3055 spin_lock_irqsave(&xhci->lock, flags);
3056 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3057 num_eps, &num_streams, &changed_ep_bitmask);
3058 if (ret < 0) {
3059 xhci_free_command(xhci, config_cmd);
3060 spin_unlock_irqrestore(&xhci->lock, flags);
3061 return ret;
3062 }
3063 if (num_streams <= 1) {
3064 xhci_warn(xhci, "WARN: endpoints can't handle "
3065 "more than one stream.\n");
3066 xhci_free_command(xhci, config_cmd);
3067 spin_unlock_irqrestore(&xhci->lock, flags);
3068 return -EINVAL;
3069 }
3070 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003071 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003072 * xhci_urb_enqueue() will reject all URBs.
3073 */
3074 for (i = 0; i < num_eps; i++) {
3075 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3076 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3077 }
3078 spin_unlock_irqrestore(&xhci->lock, flags);
3079
3080 /* Setup internal data structures and allocate HW data structures for
3081 * streams (but don't install the HW structures in the input context
3082 * until we're sure all memory allocation succeeded).
3083 */
3084 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3085 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3086 num_stream_ctxs, num_streams);
3087
3088 for (i = 0; i < num_eps; i++) {
3089 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3090 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3091 num_stream_ctxs,
3092 num_streams, mem_flags);
3093 if (!vdev->eps[ep_index].stream_info)
3094 goto cleanup;
3095 /* Set maxPstreams in endpoint context and update deq ptr to
3096 * point to stream context array. FIXME
3097 */
3098 }
3099
3100 /* Set up the input context for a configure endpoint command. */
3101 for (i = 0; i < num_eps; i++) {
3102 struct xhci_ep_ctx *ep_ctx;
3103
3104 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3105 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3106
3107 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3108 vdev->out_ctx, ep_index);
3109 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3110 vdev->eps[ep_index].stream_info);
3111 }
3112 /* Tell the HW to drop its old copy of the endpoint context info
3113 * and add the updated copy from the input context.
3114 */
3115 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
3116 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
3117
3118 /* Issue and wait for the configure endpoint command */
3119 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3120 false, false);
3121
3122 /* xHC rejected the configure endpoint command for some reason, so we
3123 * leave the old ring intact and free our internal streams data
3124 * structure.
3125 */
3126 if (ret < 0)
3127 goto cleanup;
3128
3129 spin_lock_irqsave(&xhci->lock, flags);
3130 for (i = 0; i < num_eps; i++) {
3131 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3132 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3133 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3134 udev->slot_id, ep_index);
3135 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3136 }
3137 xhci_free_command(xhci, config_cmd);
3138 spin_unlock_irqrestore(&xhci->lock, flags);
3139
3140 /* Subtract 1 for stream 0, which drivers can't use */
3141 return num_streams - 1;
3142
3143cleanup:
3144 /* If it didn't work, free the streams! */
3145 for (i = 0; i < num_eps; i++) {
3146 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3147 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003148 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003149 /* FIXME Unset maxPstreams in endpoint context and
3150 * update deq ptr to point to normal string ring.
3151 */
3152 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3153 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3154 xhci_endpoint_zero(xhci, vdev, eps[i]);
3155 }
3156 xhci_free_command(xhci, config_cmd);
3157 return -ENOMEM;
3158}
3159
3160/* Transition the endpoint from using streams to being a "normal" endpoint
3161 * without streams.
3162 *
3163 * Modify the endpoint context state, submit a configure endpoint command,
3164 * and free all endpoint rings for streams if that completes successfully.
3165 */
3166int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3167 struct usb_host_endpoint **eps, unsigned int num_eps,
3168 gfp_t mem_flags)
3169{
3170 int i, ret;
3171 struct xhci_hcd *xhci;
3172 struct xhci_virt_device *vdev;
3173 struct xhci_command *command;
3174 unsigned int ep_index;
3175 unsigned long flags;
3176 u32 changed_ep_bitmask;
3177
3178 xhci = hcd_to_xhci(hcd);
3179 vdev = xhci->devs[udev->slot_id];
3180
3181 /* Set up a configure endpoint command to remove the streams rings */
3182 spin_lock_irqsave(&xhci->lock, flags);
3183 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3184 udev, eps, num_eps);
3185 if (changed_ep_bitmask == 0) {
3186 spin_unlock_irqrestore(&xhci->lock, flags);
3187 return -EINVAL;
3188 }
3189
3190 /* Use the xhci_command structure from the first endpoint. We may have
3191 * allocated too many, but the driver may call xhci_free_streams() for
3192 * each endpoint it grouped into one call to xhci_alloc_streams().
3193 */
3194 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3195 command = vdev->eps[ep_index].stream_info->free_streams_command;
3196 for (i = 0; i < num_eps; i++) {
3197 struct xhci_ep_ctx *ep_ctx;
3198
3199 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3200 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3201 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3202 EP_GETTING_NO_STREAMS;
3203
3204 xhci_endpoint_copy(xhci, command->in_ctx,
3205 vdev->out_ctx, ep_index);
3206 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3207 &vdev->eps[ep_index]);
3208 }
3209 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3210 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
3211 spin_unlock_irqrestore(&xhci->lock, flags);
3212
3213 /* Issue and wait for the configure endpoint command,
3214 * which must succeed.
3215 */
3216 ret = xhci_configure_endpoint(xhci, udev, command,
3217 false, true);
3218
3219 /* xHC rejected the configure endpoint command for some reason, so we
3220 * leave the streams rings intact.
3221 */
3222 if (ret < 0)
3223 return ret;
3224
3225 spin_lock_irqsave(&xhci->lock, flags);
3226 for (i = 0; i < num_eps; i++) {
3227 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3228 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003229 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003230 /* FIXME Unset maxPstreams in endpoint context and
3231 * update deq ptr to point to normal string ring.
3232 */
3233 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3234 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3235 }
3236 spin_unlock_irqrestore(&xhci->lock, flags);
3237
3238 return 0;
3239}
3240
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003241/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003242 * Deletes endpoint resources for endpoints that were active before a Reset
3243 * Device command, or a Disable Slot command. The Reset Device command leaves
3244 * the control endpoint intact, whereas the Disable Slot command deletes it.
3245 *
3246 * Must be called with xhci->lock held.
3247 */
3248void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3249 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3250{
3251 int i;
3252 unsigned int num_dropped_eps = 0;
3253 unsigned int drop_flags = 0;
3254
3255 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3256 if (virt_dev->eps[i].ring) {
3257 drop_flags |= 1 << i;
3258 num_dropped_eps++;
3259 }
3260 }
3261 xhci->num_active_eps -= num_dropped_eps;
3262 if (num_dropped_eps)
3263 xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
3264 "%u now active.\n",
3265 num_dropped_eps, drop_flags,
3266 xhci->num_active_eps);
3267}
3268
3269/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003270 * This submits a Reset Device Command, which will set the device state to 0,
3271 * set the device address to 0, and disable all the endpoints except the default
3272 * control endpoint. The USB core should come back and call
3273 * xhci_address_device(), and then re-set up the configuration. If this is
3274 * called because of a usb_reset_and_verify_device(), then the old alternate
3275 * settings will be re-installed through the normal bandwidth allocation
3276 * functions.
3277 *
3278 * Wait for the Reset Device command to finish. Remove all structures
3279 * associated with the endpoints that were disabled. Clear the input device
3280 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003281 *
3282 * If the virt_dev to be reset does not exist or does not match the udev,
3283 * it means the device is lost, possibly due to the xHC restore error and
3284 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3285 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003286 */
Andiry Xuf0615c42010-10-14 07:22:48 -07003287int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003288{
3289 int ret, i;
3290 unsigned long flags;
3291 struct xhci_hcd *xhci;
3292 unsigned int slot_id;
3293 struct xhci_virt_device *virt_dev;
3294 struct xhci_command *reset_device_cmd;
3295 int timeleft;
3296 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003297 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003298 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003299
Andiry Xuf0615c42010-10-14 07:22:48 -07003300 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003301 if (ret <= 0)
3302 return ret;
3303 xhci = hcd_to_xhci(hcd);
3304 slot_id = udev->slot_id;
3305 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003306 if (!virt_dev) {
3307 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3308 "not exist. Re-allocate the device\n", slot_id);
3309 ret = xhci_alloc_dev(hcd, udev);
3310 if (ret == 1)
3311 return 0;
3312 else
3313 return -EINVAL;
3314 }
3315
3316 if (virt_dev->udev != udev) {
3317 /* If the virt_dev and the udev does not match, this virt_dev
3318 * may belong to another udev.
3319 * Re-allocate the device.
3320 */
3321 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3322 "not match the udev. Re-allocate the device\n",
3323 slot_id);
3324 ret = xhci_alloc_dev(hcd, udev);
3325 if (ret == 1)
3326 return 0;
3327 else
3328 return -EINVAL;
3329 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003330
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003331 /* If device is not setup, there is no point in resetting it */
3332 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3333 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3334 SLOT_STATE_DISABLED)
3335 return 0;
3336
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003337 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3338 /* Allocate the command structure that holds the struct completion.
3339 * Assume we're in process context, since the normal device reset
3340 * process has to wait for the device anyway. Storage devices are
3341 * reset as part of error handling, so use GFP_NOIO instead of
3342 * GFP_KERNEL.
3343 */
3344 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3345 if (!reset_device_cmd) {
3346 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3347 return -ENOMEM;
3348 }
3349
3350 /* Attempt to submit the Reset Device command to the command ring */
3351 spin_lock_irqsave(&xhci->lock, flags);
3352 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003353
3354 /* Enqueue pointer can be left pointing to the link TRB,
3355 * we must handle that
3356 */
Matt Evansf5960b62011-06-01 10:22:55 +10003357 if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003358 reset_device_cmd->command_trb =
3359 xhci->cmd_ring->enq_seg->next->trbs;
3360
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003361 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3362 ret = xhci_queue_reset_device(xhci, slot_id);
3363 if (ret) {
3364 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3365 list_del(&reset_device_cmd->cmd_list);
3366 spin_unlock_irqrestore(&xhci->lock, flags);
3367 goto command_cleanup;
3368 }
3369 xhci_ring_cmd_db(xhci);
3370 spin_unlock_irqrestore(&xhci->lock, flags);
3371
3372 /* Wait for the Reset Device command to finish */
3373 timeleft = wait_for_completion_interruptible_timeout(
3374 reset_device_cmd->completion,
3375 USB_CTRL_SET_TIMEOUT);
3376 if (timeleft <= 0) {
3377 xhci_warn(xhci, "%s while waiting for reset device command\n",
3378 timeleft == 0 ? "Timeout" : "Signal");
3379 spin_lock_irqsave(&xhci->lock, flags);
3380 /* The timeout might have raced with the event ring handler, so
3381 * only delete from the list if the item isn't poisoned.
3382 */
3383 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3384 list_del(&reset_device_cmd->cmd_list);
3385 spin_unlock_irqrestore(&xhci->lock, flags);
3386 ret = -ETIME;
3387 goto command_cleanup;
3388 }
3389
3390 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3391 * unless we tried to reset a slot ID that wasn't enabled,
3392 * or the device wasn't in the addressed or configured state.
3393 */
3394 ret = reset_device_cmd->status;
3395 switch (ret) {
3396 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3397 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
3398 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
3399 slot_id,
3400 xhci_get_slot_state(xhci, virt_dev->out_ctx));
3401 xhci_info(xhci, "Not freeing device rings.\n");
3402 /* Don't treat this as an error. May change my mind later. */
3403 ret = 0;
3404 goto command_cleanup;
3405 case COMP_SUCCESS:
3406 xhci_dbg(xhci, "Successful reset device command.\n");
3407 break;
3408 default:
3409 if (xhci_is_vendor_info_code(xhci, ret))
3410 break;
3411 xhci_warn(xhci, "Unknown completion code %u for "
3412 "reset device command.\n", ret);
3413 ret = -EINVAL;
3414 goto command_cleanup;
3415 }
3416
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003417 /* Free up host controller endpoint resources */
3418 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3419 spin_lock_irqsave(&xhci->lock, flags);
3420 /* Don't delete the default control endpoint resources */
3421 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3422 spin_unlock_irqrestore(&xhci->lock, flags);
3423 }
3424
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003425 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3426 last_freed_endpoint = 1;
3427 for (i = 1; i < 31; ++i) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003428 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3429
3430 if (ep->ep_state & EP_HAS_STREAMS) {
3431 xhci_free_stream_info(xhci, ep->stream_info);
3432 ep->stream_info = NULL;
3433 ep->ep_state &= ~EP_HAS_STREAMS;
3434 }
3435
3436 if (ep->ring) {
3437 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3438 last_freed_endpoint = i;
3439 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003440 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3441 xhci_drop_ep_from_interval_table(xhci,
3442 &virt_dev->eps[i].bw_info,
3443 virt_dev->bw_table,
3444 udev,
3445 &virt_dev->eps[i],
3446 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003447 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003448 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003449 /* If necessary, update the number of active TTs on this root port */
3450 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3451
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003452 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3453 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3454 ret = 0;
3455
3456command_cleanup:
3457 xhci_free_command(xhci, reset_device_cmd);
3458 return ret;
3459}
3460
3461/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003462 * At this point, the struct usb_device is about to go away, the device has
3463 * disconnected, and all traffic has been stopped and the endpoints have been
3464 * disabled. Free any HC data structures associated with that device.
3465 */
3466void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3467{
3468 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003469 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003470 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003471 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07003472 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003473
Andiry Xu64927732010-10-14 07:22:45 -07003474 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003475 /* If the host is halted due to driver unload, we still need to free the
3476 * device.
3477 */
3478 if (ret <= 0 && ret != -ENODEV)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003479 return;
Andiry Xu64927732010-10-14 07:22:45 -07003480
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003481 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003482
3483 /* Stop any wayward timer functions (which may grab the lock) */
3484 for (i = 0; i < 31; ++i) {
3485 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3486 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3487 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003488
Andiry Xu65580b432011-09-23 14:19:52 -07003489 if (udev->usb2_hw_lpm_enabled) {
3490 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3491 udev->usb2_hw_lpm_enabled = 0;
3492 }
3493
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003494 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003495 /* Don't disable the slot if the host controller is dead. */
3496 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003497 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3498 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003499 xhci_free_virt_device(xhci, udev->slot_id);
3500 spin_unlock_irqrestore(&xhci->lock, flags);
3501 return;
3502 }
3503
Sarah Sharp23e3be12009-04-29 19:05:20 -07003504 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003505 spin_unlock_irqrestore(&xhci->lock, flags);
3506 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3507 return;
3508 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003509 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003510 spin_unlock_irqrestore(&xhci->lock, flags);
3511 /*
3512 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07003513 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003514 */
3515}
3516
3517/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003518 * Checks if we have enough host controller resources for the default control
3519 * endpoint.
3520 *
3521 * Must be called with xhci->lock held.
3522 */
3523static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3524{
3525 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3526 xhci_dbg(xhci, "Not enough ep ctxs: "
3527 "%u active, need to add 1, limit is %u.\n",
3528 xhci->num_active_eps, xhci->limit_active_eps);
3529 return -ENOMEM;
3530 }
3531 xhci->num_active_eps += 1;
3532 xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
3533 xhci->num_active_eps);
3534 return 0;
3535}
3536
3537
3538/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003539 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3540 * timed out, or allocating memory failed. Returns 1 on success.
3541 */
3542int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3543{
3544 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3545 unsigned long flags;
3546 int timeleft;
3547 int ret;
3548
3549 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07003550 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003551 if (ret) {
3552 spin_unlock_irqrestore(&xhci->lock, flags);
3553 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3554 return 0;
3555 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003556 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003557 spin_unlock_irqrestore(&xhci->lock, flags);
3558
3559 /* XXX: how much time for xHC slot assignment? */
3560 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3561 USB_CTRL_SET_TIMEOUT);
3562 if (timeleft <= 0) {
3563 xhci_warn(xhci, "%s while waiting for a slot\n",
3564 timeleft == 0 ? "Timeout" : "Signal");
3565 /* FIXME cancel the enable slot request */
3566 return 0;
3567 }
3568
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003569 if (!xhci->slot_id) {
3570 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003571 return 0;
3572 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003573
3574 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3575 spin_lock_irqsave(&xhci->lock, flags);
3576 ret = xhci_reserve_host_control_ep_resources(xhci);
3577 if (ret) {
3578 spin_unlock_irqrestore(&xhci->lock, flags);
3579 xhci_warn(xhci, "Not enough host resources, "
3580 "active endpoint contexts = %u\n",
3581 xhci->num_active_eps);
3582 goto disable_slot;
3583 }
3584 spin_unlock_irqrestore(&xhci->lock, flags);
3585 }
3586 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003587 * xhci_discover_or_reset_device(), which may be called as part of
3588 * mass storage driver error handling.
3589 */
3590 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003591 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003592 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003593 }
3594 udev->slot_id = xhci->slot_id;
3595 /* Is this a LS or FS device under a HS hub? */
3596 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003597 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003598
3599disable_slot:
3600 /* Disable slot, if we can do it without mem alloc */
3601 spin_lock_irqsave(&xhci->lock, flags);
3602 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3603 xhci_ring_cmd_db(xhci);
3604 spin_unlock_irqrestore(&xhci->lock, flags);
3605 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003606}
3607
3608/*
3609 * Issue an Address Device command (which will issue a SetAddress request to
3610 * the device).
3611 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3612 * we should only issue and wait on one address command at the same time.
3613 *
3614 * We add one to the device address issued by the hardware because the USB core
3615 * uses address 1 for the root hubs (even though they're not really devices).
3616 */
3617int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3618{
3619 unsigned long flags;
3620 int timeleft;
3621 struct xhci_virt_device *virt_dev;
3622 int ret = 0;
3623 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003624 struct xhci_slot_ctx *slot_ctx;
3625 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003626 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003627
3628 if (!udev->slot_id) {
3629 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
3630 return -EINVAL;
3631 }
3632
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003633 virt_dev = xhci->devs[udev->slot_id];
3634
Matt Evans7ed603e2011-03-29 13:40:56 +11003635 if (WARN_ON(!virt_dev)) {
3636 /*
3637 * In plug/unplug torture test with an NEC controller,
3638 * a zero-dereference was observed once due to virt_dev = 0.
3639 * Print useful debug rather than crash if it is observed again!
3640 */
3641 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3642 udev->slot_id);
3643 return -EINVAL;
3644 }
3645
Andiry Xuf0615c42010-10-14 07:22:48 -07003646 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
3647 /*
3648 * If this is the first Set Address since device plug-in or
3649 * virt_device realloaction after a resume with an xHCI power loss,
3650 * then set up the slot context.
3651 */
3652 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003653 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003654 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003655 else
3656 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003657 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3658 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3659 ctrl_ctx->drop_flags = 0;
3660
Sarah Sharp66e49d82009-07-27 12:03:46 -07003661 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003662 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003663
Sarah Sharpf88ba782009-05-14 11:44:22 -07003664 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07003665 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3666 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003667 if (ret) {
3668 spin_unlock_irqrestore(&xhci->lock, flags);
3669 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3670 return ret;
3671 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003672 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003673 spin_unlock_irqrestore(&xhci->lock, flags);
3674
3675 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3676 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3677 USB_CTRL_SET_TIMEOUT);
3678 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3679 * the SetAddress() "recovery interval" required by USB and aborting the
3680 * command on a timeout.
3681 */
3682 if (timeleft <= 0) {
Andiry Xucd681762011-09-23 14:19:55 -07003683 xhci_warn(xhci, "%s while waiting for address device command\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003684 timeleft == 0 ? "Timeout" : "Signal");
3685 /* FIXME cancel the address device command */
3686 return -ETIME;
3687 }
3688
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003689 switch (virt_dev->cmd_status) {
3690 case COMP_CTX_STATE:
3691 case COMP_EBADSLT:
3692 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3693 udev->slot_id);
3694 ret = -EINVAL;
3695 break;
3696 case COMP_TX_ERR:
3697 dev_warn(&udev->dev, "Device not responding to set address.\n");
3698 ret = -EPROTO;
3699 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08003700 case COMP_DEV_ERR:
3701 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3702 "device command.\n");
3703 ret = -ENODEV;
3704 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003705 case COMP_SUCCESS:
3706 xhci_dbg(xhci, "Successful Address Device command\n");
3707 break;
3708 default:
3709 xhci_err(xhci, "ERROR: unexpected command completion "
3710 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07003711 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003712 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003713 ret = -EINVAL;
3714 break;
3715 }
3716 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003717 return ret;
3718 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07003719 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
3720 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
3721 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Matt Evans28ccd292011-03-29 13:40:46 +11003722 udev->slot_id,
3723 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3724 (unsigned long long)
3725 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003726 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07003727 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003728 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003729 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003730 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003731 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003732 /*
3733 * USB core uses address 1 for the roothubs, so we add one to the
3734 * address given back to us by the HC.
3735 */
John Yound115b042009-07-27 12:05:15 -07003736 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003737 /* Use kernel assigned address for devices; store xHC assigned
3738 * address locally. */
Matt Evans28ccd292011-03-29 13:40:46 +11003739 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3740 + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07003741 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003742 ctrl_ctx->add_flags = 0;
3743 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003744
Andiry Xuc8d4af82010-10-14 07:22:51 -07003745 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003746
3747 return 0;
3748}
3749
Andiry Xu95743232011-09-23 14:19:51 -07003750#ifdef CONFIG_USB_SUSPEND
3751
3752/* BESL to HIRD Encoding array for USB2 LPM */
3753static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3754 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3755
3756/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08003757static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
3758 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07003759{
Andiry Xuf99298b2011-12-12 16:45:28 +08003760 int u2del, besl, besl_host;
3761 int besl_device = 0;
3762 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07003763
Andiry Xuf99298b2011-12-12 16:45:28 +08003764 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3765 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
3766
3767 if (field & USB_BESL_SUPPORT) {
3768 for (besl_host = 0; besl_host < 16; besl_host++) {
3769 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07003770 break;
3771 }
Andiry Xuf99298b2011-12-12 16:45:28 +08003772 /* Use baseline BESL value as default */
3773 if (field & USB_BESL_BASELINE_VALID)
3774 besl_device = USB_GET_BESL_BASELINE(field);
3775 else if (field & USB_BESL_DEEP_VALID)
3776 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07003777 } else {
3778 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08003779 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07003780 else
Andiry Xuf99298b2011-12-12 16:45:28 +08003781 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07003782 }
3783
Andiry Xuf99298b2011-12-12 16:45:28 +08003784 besl = besl_host + besl_device;
3785 if (besl > 15)
3786 besl = 15;
3787
3788 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07003789}
3790
3791static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
3792 struct usb_device *udev)
3793{
3794 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3795 struct dev_info *dev_info;
3796 __le32 __iomem **port_array;
3797 __le32 __iomem *addr, *pm_addr;
3798 u32 temp, dev_id;
3799 unsigned int port_num;
3800 unsigned long flags;
Andiry Xuf99298b2011-12-12 16:45:28 +08003801 int hird;
Andiry Xu95743232011-09-23 14:19:51 -07003802 int ret;
3803
3804 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
3805 !udev->lpm_capable)
3806 return -EINVAL;
3807
3808 /* we only support lpm for non-hub device connected to root hub yet */
3809 if (!udev->parent || udev->parent->parent ||
3810 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
3811 return -EINVAL;
3812
3813 spin_lock_irqsave(&xhci->lock, flags);
3814
3815 /* Look for devices in lpm_failed_devs list */
3816 dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
3817 le16_to_cpu(udev->descriptor.idProduct);
3818 list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
3819 if (dev_info->dev_id == dev_id) {
3820 ret = -EINVAL;
3821 goto finish;
3822 }
3823 }
3824
3825 port_array = xhci->usb2_ports;
3826 port_num = udev->portnum - 1;
3827
3828 if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
3829 xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
3830 ret = -EINVAL;
3831 goto finish;
3832 }
3833
3834 /*
3835 * Test USB 2.0 software LPM.
3836 * FIXME: some xHCI 1.0 hosts may implement a new register to set up
3837 * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
3838 * in the June 2011 errata release.
3839 */
3840 xhci_dbg(xhci, "test port %d software LPM\n", port_num);
3841 /*
3842 * Set L1 Device Slot and HIRD/BESL.
3843 * Check device's USB 2.0 extension descriptor to determine whether
3844 * HIRD or BESL shoule be used. See USB2.0 LPM errata.
3845 */
3846 pm_addr = port_array[port_num] + 1;
Andiry Xuf99298b2011-12-12 16:45:28 +08003847 hird = xhci_calculate_hird_besl(xhci, udev);
Andiry Xu95743232011-09-23 14:19:51 -07003848 temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
3849 xhci_writel(xhci, temp, pm_addr);
3850
3851 /* Set port link state to U2(L1) */
3852 addr = port_array[port_num];
3853 xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
3854
3855 /* wait for ACK */
3856 spin_unlock_irqrestore(&xhci->lock, flags);
3857 msleep(10);
3858 spin_lock_irqsave(&xhci->lock, flags);
3859
3860 /* Check L1 Status */
3861 ret = handshake(xhci, pm_addr, PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
3862 if (ret != -ETIMEDOUT) {
3863 /* enter L1 successfully */
3864 temp = xhci_readl(xhci, addr);
3865 xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
3866 port_num, temp);
3867 ret = 0;
3868 } else {
3869 temp = xhci_readl(xhci, pm_addr);
3870 xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
3871 port_num, temp & PORT_L1S_MASK);
3872 ret = -EINVAL;
3873 }
3874
3875 /* Resume the port */
3876 xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
3877
3878 spin_unlock_irqrestore(&xhci->lock, flags);
3879 msleep(10);
3880 spin_lock_irqsave(&xhci->lock, flags);
3881
3882 /* Clear PLC */
3883 xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
3884
3885 /* Check PORTSC to make sure the device is in the right state */
3886 if (!ret) {
3887 temp = xhci_readl(xhci, addr);
3888 xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
3889 if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
3890 (temp & PORT_PLS_MASK) != XDEV_U0) {
3891 xhci_dbg(xhci, "port L1 resume fail\n");
3892 ret = -EINVAL;
3893 }
3894 }
3895
3896 if (ret) {
3897 /* Insert dev to lpm_failed_devs list */
3898 xhci_warn(xhci, "device LPM test failed, may disconnect and "
3899 "re-enumerate\n");
3900 dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
3901 if (!dev_info) {
3902 ret = -ENOMEM;
3903 goto finish;
3904 }
3905 dev_info->dev_id = dev_id;
3906 INIT_LIST_HEAD(&dev_info->list);
3907 list_add(&dev_info->list, &xhci->lpm_failed_devs);
3908 } else {
3909 xhci_ring_device(xhci, udev->slot_id);
3910 }
3911
3912finish:
3913 spin_unlock_irqrestore(&xhci->lock, flags);
3914 return ret;
3915}
3916
Andiry Xu65580b432011-09-23 14:19:52 -07003917int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
3918 struct usb_device *udev, int enable)
3919{
3920 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3921 __le32 __iomem **port_array;
3922 __le32 __iomem *pm_addr;
3923 u32 temp;
3924 unsigned int port_num;
3925 unsigned long flags;
Andiry Xuf99298b2011-12-12 16:45:28 +08003926 int hird;
Andiry Xu65580b432011-09-23 14:19:52 -07003927
3928 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
3929 !udev->lpm_capable)
3930 return -EPERM;
3931
3932 if (!udev->parent || udev->parent->parent ||
3933 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
3934 return -EPERM;
3935
3936 if (udev->usb2_hw_lpm_capable != 1)
3937 return -EPERM;
3938
3939 spin_lock_irqsave(&xhci->lock, flags);
3940
3941 port_array = xhci->usb2_ports;
3942 port_num = udev->portnum - 1;
3943 pm_addr = port_array[port_num] + 1;
3944 temp = xhci_readl(xhci, pm_addr);
3945
3946 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
3947 enable ? "enable" : "disable", port_num);
3948
Andiry Xuf99298b2011-12-12 16:45:28 +08003949 hird = xhci_calculate_hird_besl(xhci, udev);
Andiry Xu65580b432011-09-23 14:19:52 -07003950
3951 if (enable) {
3952 temp &= ~PORT_HIRD_MASK;
3953 temp |= PORT_HIRD(hird) | PORT_RWE;
3954 xhci_writel(xhci, temp, pm_addr);
3955 temp = xhci_readl(xhci, pm_addr);
3956 temp |= PORT_HLE;
3957 xhci_writel(xhci, temp, pm_addr);
3958 } else {
3959 temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
3960 xhci_writel(xhci, temp, pm_addr);
3961 }
3962
3963 spin_unlock_irqrestore(&xhci->lock, flags);
3964 return 0;
3965}
3966
Andiry Xu95743232011-09-23 14:19:51 -07003967int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
3968{
3969 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3970 int ret;
3971
3972 ret = xhci_usb2_software_lpm_test(hcd, udev);
Andiry Xu65580b432011-09-23 14:19:52 -07003973 if (!ret) {
Andiry Xu95743232011-09-23 14:19:51 -07003974 xhci_dbg(xhci, "software LPM test succeed\n");
Andiry Xu65580b432011-09-23 14:19:52 -07003975 if (xhci->hw_lpm_support == 1) {
3976 udev->usb2_hw_lpm_capable = 1;
3977 ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
3978 if (!ret)
3979 udev->usb2_hw_lpm_enabled = 1;
3980 }
3981 }
Andiry Xu95743232011-09-23 14:19:51 -07003982
3983 return 0;
3984}
3985
3986#else
3987
Andiry Xu65580b432011-09-23 14:19:52 -07003988int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
3989 struct usb_device *udev, int enable)
3990{
3991 return 0;
3992}
3993
Andiry Xu95743232011-09-23 14:19:51 -07003994int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
3995{
3996 return 0;
3997}
3998
3999#endif /* CONFIG_USB_SUSPEND */
4000
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004001/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4002 * internal data structures for the device.
4003 */
4004int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4005 struct usb_tt *tt, gfp_t mem_flags)
4006{
4007 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4008 struct xhci_virt_device *vdev;
4009 struct xhci_command *config_cmd;
4010 struct xhci_input_control_ctx *ctrl_ctx;
4011 struct xhci_slot_ctx *slot_ctx;
4012 unsigned long flags;
4013 unsigned think_time;
4014 int ret;
4015
4016 /* Ignore root hubs */
4017 if (!hdev->parent)
4018 return 0;
4019
4020 vdev = xhci->devs[hdev->slot_id];
4021 if (!vdev) {
4022 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4023 return -EINVAL;
4024 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004025 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004026 if (!config_cmd) {
4027 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4028 return -ENOMEM;
4029 }
4030
4031 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004032 if (hdev->speed == USB_SPEED_HIGH &&
4033 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4034 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4035 xhci_free_command(xhci, config_cmd);
4036 spin_unlock_irqrestore(&xhci->lock, flags);
4037 return -ENOMEM;
4038 }
4039
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004040 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
4041 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004042 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004043 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004044 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004045 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004046 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004047 if (xhci->hci_version > 0x95) {
4048 xhci_dbg(xhci, "xHCI version %x needs hub "
4049 "TT think time and number of ports\n",
4050 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004051 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004052 /* Set TT think time - convert from ns to FS bit times.
4053 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4054 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004055 *
4056 * xHCI 1.0: this field shall be 0 if the device is not a
4057 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004058 */
4059 think_time = tt->think_time;
4060 if (think_time != 0)
4061 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004062 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4063 slot_ctx->tt_info |=
4064 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004065 } else {
4066 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4067 "TT think time or number of ports\n",
4068 (unsigned int) xhci->hci_version);
4069 }
4070 slot_ctx->dev_state = 0;
4071 spin_unlock_irqrestore(&xhci->lock, flags);
4072
4073 xhci_dbg(xhci, "Set up %s for hub device.\n",
4074 (xhci->hci_version > 0x95) ?
4075 "configure endpoint" : "evaluate context");
4076 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4077 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4078
4079 /* Issue and wait for the configure endpoint or
4080 * evaluate context command.
4081 */
4082 if (xhci->hci_version > 0x95)
4083 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4084 false, false);
4085 else
4086 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4087 true, false);
4088
4089 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4090 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4091
4092 xhci_free_command(xhci, config_cmd);
4093 return ret;
4094}
4095
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004096int xhci_get_frame(struct usb_hcd *hcd)
4097{
4098 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4099 /* EHCI mods by the periodic size. Why? */
4100 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4101}
4102
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004103int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4104{
4105 struct xhci_hcd *xhci;
4106 struct device *dev = hcd->self.controller;
4107 int retval;
4108 u32 temp;
4109
Andiry Xufdaf8b32012-03-05 17:49:38 +08004110 /* Accept arbitrarily long scatter-gather lists */
4111 hcd->self.sg_tablesize = ~0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004112
4113 if (usb_hcd_is_primary_hcd(hcd)) {
4114 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4115 if (!xhci)
4116 return -ENOMEM;
4117 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4118 xhci->main_hcd = hcd;
4119 /* Mark the first roothub as being USB 2.0.
4120 * The xHCI driver will register the USB 3.0 roothub.
4121 */
4122 hcd->speed = HCD_USB2;
4123 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4124 /*
4125 * USB 2.0 roothub under xHCI has an integrated TT,
4126 * (rate matching hub) as opposed to having an OHCI/UHCI
4127 * companion controller.
4128 */
4129 hcd->has_tt = 1;
4130 } else {
4131 /* xHCI private pointer was set in xhci_pci_probe for the second
4132 * registered roothub.
4133 */
4134 xhci = hcd_to_xhci(hcd);
4135 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4136 if (HCC_64BIT_ADDR(temp)) {
4137 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4138 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4139 } else {
4140 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4141 }
4142 return 0;
4143 }
4144
4145 xhci->cap_regs = hcd->regs;
4146 xhci->op_regs = hcd->regs +
4147 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4148 xhci->run_regs = hcd->regs +
4149 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4150 /* Cache read-only capability registers */
4151 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4152 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4153 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4154 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4155 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4156 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4157 xhci_print_registers(xhci);
4158
4159 get_quirks(dev, xhci);
4160
4161 /* Make sure the HC is halted. */
4162 retval = xhci_halt(xhci);
4163 if (retval)
4164 goto error;
4165
4166 xhci_dbg(xhci, "Resetting HCD\n");
4167 /* Reset the internal HC memory state and registers. */
4168 retval = xhci_reset(xhci);
4169 if (retval)
4170 goto error;
4171 xhci_dbg(xhci, "Reset complete\n");
4172
4173 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4174 if (HCC_64BIT_ADDR(temp)) {
4175 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4176 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4177 } else {
4178 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4179 }
4180
4181 xhci_dbg(xhci, "Calling HCD init\n");
4182 /* Initialize HCD and host controller data structures. */
4183 retval = xhci_init(hcd);
4184 if (retval)
4185 goto error;
4186 xhci_dbg(xhci, "Called HCD init\n");
4187 return 0;
4188error:
4189 kfree(xhci);
4190 return retval;
4191}
4192
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004193MODULE_DESCRIPTION(DRIVER_DESC);
4194MODULE_AUTHOR(DRIVER_AUTHOR);
4195MODULE_LICENSE("GPL");
4196
4197static int __init xhci_hcd_init(void)
4198{
Sebastian Andrzej Siewior0cc47d52011-09-23 14:20:02 -07004199 int retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004200
4201 retval = xhci_register_pci();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004202 if (retval < 0) {
4203 printk(KERN_DEBUG "Problem registering PCI driver.");
4204 return retval;
4205 }
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004206 retval = xhci_register_plat();
4207 if (retval < 0) {
4208 printk(KERN_DEBUG "Problem registering platform driver.");
4209 goto unreg_pci;
4210 }
Sarah Sharp98441972009-05-14 11:44:18 -07004211 /*
4212 * Check the compiler generated sizes of structures that must be laid
4213 * out in specific ways for hardware access.
4214 */
4215 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4216 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4217 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4218 /* xhci_device_control has eight fields, and also
4219 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4220 */
Sarah Sharp98441972009-05-14 11:44:18 -07004221 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4222 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4223 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4224 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4225 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4226 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4227 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
4228 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004229 return 0;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004230unreg_pci:
4231 xhci_unregister_pci();
4232 return retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004233}
4234module_init(xhci_hcd_init);
4235
4236static void __exit xhci_hcd_cleanup(void)
4237{
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004238 xhci_unregister_pci();
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004239 xhci_unregister_plat();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004240}
4241module_exit(xhci_hcd_cleanup);