blob: ead3555d6dba5e149dcdd508ec05a1717a479151 [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. Cortes71c731a2012-08-03 14:00:27 -050029#include <linux/dmi.h>
James Hogan008eb952013-07-26 13:34:43 +010030#include <linux/dma-mapping.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070031
32#include "xhci.h"
33
34#define DRIVER_AUTHOR "Sarah Sharp"
35#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
36
Sarah Sharpb0567b32009-08-07 14:04:36 -070037/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
38static int link_quirk;
39module_param(link_quirk, int, S_IRUGO | S_IWUSR);
40MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
41
Sarah Sharp66d4ead2009-04-27 19:52:28 -070042/* TODO: copied from ehci-hcd.c - can this be refactored? */
43/*
Sarah Sharp2611bd12012-10-25 13:27:51 -070044 * xhci_handshake - spin reading hc until handshake completes or fails
Sarah Sharp66d4ead2009-04-27 19:52:28 -070045 * @ptr: address of hc register to be read
46 * @mask: bits to look at in result of read
47 * @done: value of those bits when handshake succeeds
48 * @usec: timeout in microseconds
49 *
50 * Returns negative errno, or zero on success
51 *
52 * Success happens when the "mask" bits have the specified value (hardware
53 * handshake done). There are two failure modes: "usec" have passed (major
54 * hardware flakeout), or the register reads as all-ones (hardware removed).
55 */
Sarah Sharp2611bd12012-10-25 13:27:51 -070056int xhci_handshake(struct xhci_hcd *xhci, void __iomem *ptr,
Sarah Sharp66d4ead2009-04-27 19:52:28 -070057 u32 mask, u32 done, int usec)
58{
59 u32 result;
60
61 do {
62 result = xhci_readl(xhci, ptr);
63 if (result == ~(u32)0) /* card removed */
64 return -ENODEV;
65 result &= mask;
66 if (result == done)
67 return 0;
68 udelay(1);
69 usec--;
70 } while (usec > 0);
71 return -ETIMEDOUT;
72}
73
74/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070075 * Disable interrupts and begin the xHCI halting process.
76 */
77void xhci_quiesce(struct xhci_hcd *xhci)
78{
79 u32 halted;
80 u32 cmd;
81 u32 mask;
82
83 mask = ~(XHCI_IRQS);
84 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
85 if (!halted)
86 mask &= ~CMD_RUN;
87
88 cmd = xhci_readl(xhci, &xhci->op_regs->command);
89 cmd &= mask;
90 xhci_writel(xhci, cmd, &xhci->op_regs->command);
91}
92
93/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070094 * Force HC into halt state.
95 *
96 * Disable any IRQs and clear the run/stop bit.
97 * HC will complete any current and actively pipelined transactions, and
Andiry Xubdfca502011-01-06 15:43:39 +080098 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070099 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700100 */
101int xhci_halt(struct xhci_hcd *xhci)
102{
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800103 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700104 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700105 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700106
Sarah Sharp2611bd12012-10-25 13:27:51 -0700107 ret = xhci_handshake(xhci, &xhci->op_regs->status,
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700108 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
Elric Fuc181bc52012-06-27 16:30:57 +0800109 if (!ret) {
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800110 xhci->xhc_state |= XHCI_STATE_HALTED;
Elric Fuc181bc52012-06-27 16:30:57 +0800111 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
112 } else
Sarah Sharp5af98bb2012-03-16 12:58:20 -0700113 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
114 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800115 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700116}
117
118/*
Sarah Sharped074532010-05-24 13:25:21 -0700119 * Set the run bit and wait for the host to be running.
120 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800121static int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700122{
123 u32 temp;
124 int ret;
125
126 temp = xhci_readl(xhci, &xhci->op_regs->command);
127 temp |= (CMD_RUN);
128 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
129 temp);
130 xhci_writel(xhci, temp, &xhci->op_regs->command);
131
132 /*
133 * Wait for the HCHalted Status bit to be 0 to indicate the host is
134 * running.
135 */
Sarah Sharp2611bd12012-10-25 13:27:51 -0700136 ret = xhci_handshake(xhci, &xhci->op_regs->status,
Sarah Sharped074532010-05-24 13:25:21 -0700137 STS_HALT, 0, XHCI_MAX_HALT_USEC);
138 if (ret == -ETIMEDOUT)
139 xhci_err(xhci, "Host took too long to start, "
140 "waited %u microseconds.\n",
141 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800142 if (!ret)
143 xhci->xhc_state &= ~XHCI_STATE_HALTED;
Sarah Sharped074532010-05-24 13:25:21 -0700144 return ret;
145}
146
147/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800148 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700149 *
150 * This resets pipelines, timers, counters, state machines, etc.
151 * Transactions will be terminated immediately, and operational registers
152 * will be set to their defaults.
153 */
154int xhci_reset(struct xhci_hcd *xhci)
155{
156 u32 command;
157 u32 state;
Andiry Xuf370b992012-04-14 02:54:30 +0800158 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700159
160 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700161 if ((state & STS_HALT) == 0) {
162 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
163 return 0;
164 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700165
166 xhci_dbg(xhci, "// Reset the HC\n");
167 command = xhci_readl(xhci, &xhci->op_regs->command);
168 command |= CMD_RESET;
169 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700170
Sarah Sharp2611bd12012-10-25 13:27:51 -0700171 ret = xhci_handshake(xhci, &xhci->op_regs->command,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700172 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700173 if (ret)
174 return ret;
175
176 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
177 /*
178 * xHCI cannot write to any doorbells or operational registers other
179 * than status until the "Controller Not Ready" flag is cleared.
180 */
Sarah Sharp2611bd12012-10-25 13:27:51 -0700181 ret = xhci_handshake(xhci, &xhci->op_regs->status,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700182 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xuf370b992012-04-14 02:54:30 +0800183
184 for (i = 0; i < 2; ++i) {
185 xhci->bus_state[i].port_c_suspend = 0;
186 xhci->bus_state[i].suspended_ports = 0;
187 xhci->bus_state[i].resuming_ports = 0;
188 }
189
190 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700191}
192
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700193#ifdef CONFIG_PCI
194static int xhci_free_msi(struct xhci_hcd *xhci)
Dong Nguyen43b86af2010-07-21 16:56:08 -0700195{
196 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700197
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700198 if (!xhci->msix_entries)
199 return -EINVAL;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700200
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700201 for (i = 0; i < xhci->msix_count; i++)
202 if (xhci->msix_entries[i].vector)
203 free_irq(xhci->msix_entries[i].vector,
204 xhci_to_hcd(xhci));
205 return 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700206}
207
208/*
209 * Set up MSI
210 */
211static int xhci_setup_msi(struct xhci_hcd *xhci)
212{
213 int ret;
214 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
215
216 ret = pci_enable_msi(pdev);
217 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800218 xhci_dbg(xhci, "failed to allocate MSI entry\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700219 return ret;
220 }
221
Alex Shi851ec162013-05-24 10:54:19 +0800222 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700223 0, "xhci_hcd", xhci_to_hcd(xhci));
224 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800225 xhci_dbg(xhci, "disable MSI interrupt\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700226 pci_disable_msi(pdev);
227 }
228
229 return ret;
230}
231
232/*
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700233 * Free IRQs
234 * free all IRQs request
235 */
236static void xhci_free_irq(struct xhci_hcd *xhci)
237{
238 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
239 int ret;
240
241 /* return if using legacy interrupt */
Felipe Balbicd704692012-02-29 16:46:23 +0200242 if (xhci_to_hcd(xhci)->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700243 return;
244
245 ret = xhci_free_msi(xhci);
246 if (!ret)
247 return;
Felipe Balbicd704692012-02-29 16:46:23 +0200248 if (pdev->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700249 free_irq(pdev->irq, xhci_to_hcd(xhci));
250
251 return;
252}
253
254/*
Dong Nguyen43b86af2010-07-21 16:56:08 -0700255 * Set up MSI-X
256 */
257static int xhci_setup_msix(struct xhci_hcd *xhci)
258{
259 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800260 struct usb_hcd *hcd = xhci_to_hcd(xhci);
261 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700262
263 /*
264 * calculate number of msi-x vectors supported.
265 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
266 * with max number of interrupters based on the xhci HCSPARAMS1.
267 * - num_online_cpus: maximum msi-x vectors per CPUs core.
268 * Add additional 1 vector to ensure always available interrupt.
269 */
270 xhci->msix_count = min(num_online_cpus() + 1,
271 HCS_MAX_INTRS(xhci->hcs_params1));
272
273 xhci->msix_entries =
274 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800275 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700276 if (!xhci->msix_entries) {
277 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
278 return -ENOMEM;
279 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700280
281 for (i = 0; i < xhci->msix_count; i++) {
282 xhci->msix_entries[i].entry = i;
283 xhci->msix_entries[i].vector = 0;
284 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700285
286 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
287 if (ret) {
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800288 xhci_dbg(xhci, "Failed to enable MSI-X\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700289 goto free_entries;
290 }
291
Dong Nguyen43b86af2010-07-21 16:56:08 -0700292 for (i = 0; i < xhci->msix_count; i++) {
293 ret = request_irq(xhci->msix_entries[i].vector,
Alex Shi851ec162013-05-24 10:54:19 +0800294 xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700295 0, "xhci_hcd", xhci_to_hcd(xhci));
296 if (ret)
297 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700298 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700299
Andiry Xu00292272010-12-27 17:39:02 +0800300 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700301 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700302
303disable_msix:
Sarah Sharp3b9783b2011-12-22 15:02:13 -0800304 xhci_dbg(xhci, "disable MSI-X interrupt\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700305 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700306 pci_disable_msix(pdev);
307free_entries:
308 kfree(xhci->msix_entries);
309 xhci->msix_entries = NULL;
310 return ret;
311}
312
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700313/* Free any IRQs and disable MSI-X */
314static void xhci_cleanup_msix(struct xhci_hcd *xhci)
315{
Andiry Xu00292272010-12-27 17:39:02 +0800316 struct usb_hcd *hcd = xhci_to_hcd(xhci);
317 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700318
Dong Nguyen43b86af2010-07-21 16:56:08 -0700319 xhci_free_irq(xhci);
320
321 if (xhci->msix_entries) {
322 pci_disable_msix(pdev);
323 kfree(xhci->msix_entries);
324 xhci->msix_entries = NULL;
325 } else {
326 pci_disable_msi(pdev);
327 }
328
Andiry Xu00292272010-12-27 17:39:02 +0800329 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700330 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700331}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700332
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700333static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700334{
335 int i;
336
337 if (xhci->msix_entries) {
338 for (i = 0; i < xhci->msix_count; i++)
339 synchronize_irq(xhci->msix_entries[i].vector);
340 }
341}
342
343static int xhci_try_enable_msi(struct usb_hcd *hcd)
344{
345 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700346 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700347 int ret;
348
Sarah Sharp52fb6122013-08-08 10:08:34 -0700349 /* The xhci platform device has set up IRQs through usb_add_hcd. */
350 if (xhci->quirks & XHCI_PLAT)
351 return 0;
352
353 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700354 /*
355 * Some Fresco Logic host controllers advertise MSI, but fail to
356 * generate interrupts. Don't even try to enable MSI.
357 */
358 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100359 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700360
361 /* unregister the legacy interrupt */
362 if (hcd->irq)
363 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200364 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700365
366 ret = xhci_setup_msix(xhci);
367 if (ret)
368 /* fall back to msi*/
369 ret = xhci_setup_msi(xhci);
370
371 if (!ret)
Felipe Balbicd704692012-02-29 16:46:23 +0200372 /* hcd->irq is 0, we have MSI */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700373 return 0;
374
Sarah Sharp68d07f62012-02-13 16:25:57 -0800375 if (!pdev->irq) {
376 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
377 return -EINVAL;
378 }
379
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100380 legacy_irq:
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700381 /* fall back to legacy interrupt*/
382 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
383 hcd->irq_descr, hcd);
384 if (ret) {
385 xhci_err(xhci, "request interrupt %d failed\n",
386 pdev->irq);
387 return ret;
388 }
389 hcd->irq = pdev->irq;
390 return 0;
391}
392
393#else
394
395static int xhci_try_enable_msi(struct usb_hcd *hcd)
396{
397 return 0;
398}
399
400static void xhci_cleanup_msix(struct xhci_hcd *xhci)
401{
402}
403
404static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
405{
406}
407
408#endif
409
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500410static void compliance_mode_recovery(unsigned long arg)
411{
412 struct xhci_hcd *xhci;
413 struct usb_hcd *hcd;
414 u32 temp;
415 int i;
416
417 xhci = (struct xhci_hcd *)arg;
418
419 for (i = 0; i < xhci->num_usb3_ports; i++) {
420 temp = xhci_readl(xhci, xhci->usb3_ports[i]);
421 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
422 /*
423 * Compliance Mode Detected. Letting USB Core
424 * handle the Warm Reset
425 */
Tony Camuso58b1d792013-04-05 14:27:07 -0400426 xhci_dbg(xhci, "Compliance mode detected->port %d\n",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500427 i + 1);
Tony Camuso58b1d792013-04-05 14:27:07 -0400428 xhci_dbg(xhci, "Attempting compliance mode recovery\n");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500429 hcd = xhci->shared_hcd;
430
431 if (hcd->state == HC_STATE_SUSPENDED)
432 usb_hcd_resume_root_hub(hcd);
433
434 usb_hcd_poll_rh_status(hcd);
435 }
436 }
437
438 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
439 mod_timer(&xhci->comp_mode_recovery_timer,
440 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
441}
442
443/*
444 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
445 * that causes ports behind that hardware to enter compliance mode sometimes.
446 * The quirk creates a timer that polls every 2 seconds the link state of
447 * each host controller's port and recovers it by issuing a Warm reset
448 * if Compliance mode is detected, otherwise the port will become "dead" (no
449 * device connections or disconnections will be detected anymore). Becasue no
450 * status event is generated when entering compliance mode (per xhci spec),
451 * this quirk is needed on systems that have the failing hardware installed.
452 */
453static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
454{
455 xhci->port_status_u0 = 0;
456 init_timer(&xhci->comp_mode_recovery_timer);
457
458 xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
459 xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
460 xhci->comp_mode_recovery_timer.expires = jiffies +
461 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
462
463 set_timer_slack(&xhci->comp_mode_recovery_timer,
464 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
465 add_timer(&xhci->comp_mode_recovery_timer);
Tony Camuso58b1d792013-04-05 14:27:07 -0400466 xhci_dbg(xhci, "Compliance mode recovery timer initialized\n");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500467}
468
469/*
470 * This function identifies the systems that have installed the SN65LVPE502CP
471 * USB3.0 re-driver and that need the Compliance Mode Quirk.
472 * Systems:
473 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
474 */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700475bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500476{
477 const char *dmi_product_name, *dmi_sys_vendor;
478
479 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
480 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530481 if (!dmi_product_name || !dmi_sys_vendor)
482 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500483
484 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
485 return false;
486
487 if (strstr(dmi_product_name, "Z420") ||
488 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500489 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600490 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500491 return true;
492
493 return false;
494}
495
496static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
497{
498 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
499}
500
501
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700502/*
503 * Initialize memory for HCD and xHC (one-time init).
504 *
505 * Program the PAGESIZE register, initialize the device context array, create
506 * device contexts (?), set up a command ring segment (or two?), create event
507 * ring (one for now).
508 */
509int xhci_init(struct usb_hcd *hcd)
510{
511 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
512 int retval = 0;
513
514 xhci_dbg(xhci, "xhci_init\n");
515 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700516 if (xhci->hci_version == 0x95 && link_quirk) {
Sarah Sharpb0567b32009-08-07 14:04:36 -0700517 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
518 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
519 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700520 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700521 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700522 retval = xhci_mem_init(xhci, GFP_KERNEL);
523 xhci_dbg(xhci, "Finished xhci_init\n");
524
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500525 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700526 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500527 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
528 compliance_mode_recovery_timer_init(xhci);
529 }
530
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700531 return retval;
532}
533
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700534/*-------------------------------------------------------------------------*/
535
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700536
537#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800538static void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700539{
540 unsigned long flags;
541 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700542 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700543 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
544 int i, j;
545
546 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
547
548 spin_lock_irqsave(&xhci->lock, flags);
549 temp = xhci_readl(xhci, &xhci->op_regs->status);
550 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp7bd89b42011-07-01 13:35:40 -0700551 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
552 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700553 xhci_dbg(xhci, "HW died, polling stopped.\n");
554 spin_unlock_irqrestore(&xhci->lock, flags);
555 return;
556 }
557
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700558 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
559 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700560 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
561 xhci->error_bitmask = 0;
562 xhci_dbg(xhci, "Event ring:\n");
563 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
564 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700565 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
566 temp_64 &= ~ERST_PTR_MASK;
567 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700568 xhci_dbg(xhci, "Command ring:\n");
569 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
570 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
571 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700572 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700573 if (!xhci->devs[i])
574 continue;
575 for (j = 0; j < 31; ++j) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700576 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700577 }
578 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700579 spin_unlock_irqrestore(&xhci->lock, flags);
580
581 if (!xhci->zombie)
582 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
583 else
584 xhci_dbg(xhci, "Quit polling the event ring.\n");
585}
586#endif
587
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800588static int xhci_run_finished(struct xhci_hcd *xhci)
589{
590 if (xhci_start(xhci)) {
591 xhci_halt(xhci);
592 return -ENODEV;
593 }
594 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800595 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800596
597 if (xhci->quirks & XHCI_NEC_HOST)
598 xhci_ring_cmd_db(xhci);
599
600 xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
601 return 0;
602}
603
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700604/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700605 * Start the HC after it was halted.
606 *
607 * This function is called by the USB core when the HC driver is added.
608 * Its opposite is xhci_stop().
609 *
610 * xhci_init() must be called once before this function can be called.
611 * Reset the HC, enable device slot contexts, program DCBAAP, and
612 * set command ring pointer and event ring pointer.
613 *
614 * Setup MSI-X vectors and enable interrupts.
615 */
616int xhci_run(struct usb_hcd *hcd)
617{
618 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700619 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700620 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700621 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700622
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800623 /* Start the xHCI host controller running only after the USB 2.0 roothub
624 * is setup.
625 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700626
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700627 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800628 if (!usb_hcd_is_primary_hcd(hcd))
629 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700630
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700631 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700632
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700633 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700634 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700635 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700636
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700637#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
638 init_timer(&xhci->event_ring_timer);
639 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700640 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700641 /* Poll the event ring */
642 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
643 xhci->zombie = 0;
644 xhci_dbg(xhci, "Setting event ring polling timer\n");
645 add_timer(&xhci->event_ring_timer);
646#endif
647
Sarah Sharp66e49d82009-07-27 12:03:46 -0700648 xhci_dbg(xhci, "Command ring memory map follows:\n");
649 xhci_debug_ring(xhci, xhci->cmd_ring);
650 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
651 xhci_dbg_cmd_ptrs(xhci);
652
653 xhci_dbg(xhci, "ERST memory map follows:\n");
654 xhci_dbg_erst(xhci, &xhci->erst);
655 xhci_dbg(xhci, "Event ring:\n");
656 xhci_debug_ring(xhci, xhci->event_ring);
657 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
658 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
659 temp_64 &= ~ERST_PTR_MASK;
660 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
661
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700662 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
663 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700664 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700665 temp |= (u32) 160;
666 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
667
668 /* Set the HCD state before we enable the irqs */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700669 temp = xhci_readl(xhci, &xhci->op_regs->command);
670 temp |= (CMD_EIE);
671 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
672 temp);
673 xhci_writel(xhci, temp, &xhci->op_regs->command);
674
675 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700676 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
677 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700678 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
679 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800680 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700681
Sarah Sharp02386342010-05-24 13:25:28 -0700682 if (xhci->quirks & XHCI_NEC_HOST)
683 xhci_queue_vendor_command(xhci, 0, 0, 0,
684 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700685
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800686 xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700687 return 0;
688}
689
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800690static void xhci_only_stop_hcd(struct usb_hcd *hcd)
691{
692 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
693
694 spin_lock_irq(&xhci->lock);
695 xhci_halt(xhci);
696
697 /* The shared_hcd is going to be deallocated shortly (the USB core only
698 * calls this function when allocation fails in usb_add_hcd(), or
699 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
700 */
701 xhci->shared_hcd = NULL;
702 spin_unlock_irq(&xhci->lock);
703}
704
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700705/*
706 * Stop xHCI driver.
707 *
708 * This function is called by the USB core when the HC driver is removed.
709 * Its opposite is xhci_run().
710 *
711 * Disable device contexts, disable IRQs, and quiesce the HC.
712 * Reset the HC, finish any completed transactions, and cleanup memory.
713 */
714void xhci_stop(struct usb_hcd *hcd)
715{
716 u32 temp;
717 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
718
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800719 if (!usb_hcd_is_primary_hcd(hcd)) {
720 xhci_only_stop_hcd(xhci->shared_hcd);
721 return;
722 }
723
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700724 spin_lock_irq(&xhci->lock);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800725 /* Make sure the xHC is halted for a USB3 roothub
726 * (xhci_stop() could be called as part of failed init).
727 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700728 xhci_halt(xhci);
729 xhci_reset(xhci);
730 spin_unlock_irq(&xhci->lock);
731
Zhang Rui40a9fb12010-12-17 13:17:04 -0800732 xhci_cleanup_msix(xhci);
733
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700734#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
735 /* Tell the event ring poll function not to reschedule */
736 xhci->zombie = 1;
737 del_timer_sync(&xhci->event_ring_timer);
738#endif
739
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500740 /* Deleting Compliance Mode Recovery Timer */
741 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400742 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500743 del_timer_sync(&xhci->comp_mode_recovery_timer);
Tony Camuso58b1d792013-04-05 14:27:07 -0400744 xhci_dbg(xhci, "%s: compliance mode recovery timer deleted\n",
745 __func__);
746 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500747
Andiry Xuc41136b2011-03-22 17:08:14 +0800748 if (xhci->quirks & XHCI_AMD_PLL_FIX)
749 usb_amd_dev_put();
750
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700751 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
752 temp = xhci_readl(xhci, &xhci->op_regs->status);
753 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
754 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
755 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
756 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800757 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700758
759 xhci_dbg(xhci, "cleaning up memory\n");
760 xhci_mem_cleanup(xhci);
761 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
762 xhci_readl(xhci, &xhci->op_regs->status));
763}
764
765/*
766 * Shutdown HC (not bus-specific)
767 *
768 * This is called when the machine is rebooting or halting. We assume that the
769 * machine will be powered off, and the HC's internal state will be reset.
770 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800771 *
772 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700773 */
774void xhci_shutdown(struct usb_hcd *hcd)
775{
776 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
777
Dan Carpenter052c7f92012-08-13 19:57:03 +0300778 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Sarah Sharpe95829f2012-07-23 18:59:30 +0300779 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
780
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700781 spin_lock_irq(&xhci->lock);
782 xhci_halt(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700783 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700784
Zhang Rui40a9fb12010-12-17 13:17:04 -0800785 xhci_cleanup_msix(xhci);
786
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700787 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
788 xhci_readl(xhci, &xhci->op_regs->status));
789}
790
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700791#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700792static void xhci_save_registers(struct xhci_hcd *xhci)
793{
794 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
795 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
796 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
797 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700798 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
799 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
800 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700801 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
802 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700803}
804
805static void xhci_restore_registers(struct xhci_hcd *xhci)
806{
807 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
808 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
809 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
810 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700811 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
812 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
Sarah Sharpfb3d85b2012-03-16 13:27:39 -0700813 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700814 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
815 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700816}
817
Sarah Sharp89821322010-11-12 11:59:31 -0800818static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
819{
820 u64 val_64;
821
822 /* step 2: initialize command ring buffer */
823 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
824 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
825 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
826 xhci->cmd_ring->dequeue) &
827 (u64) ~CMD_RING_RSVD_BITS) |
828 xhci->cmd_ring->cycle_state;
829 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
830 (long unsigned long) val_64);
831 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
832}
833
834/*
835 * The whole command ring must be cleared to zero when we suspend the host.
836 *
837 * The host doesn't save the command ring pointer in the suspend well, so we
838 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
839 * aligned, because of the reserved bits in the command ring dequeue pointer
840 * register. Therefore, we can't just set the dequeue pointer back in the
841 * middle of the ring (TRBs are 16-byte aligned).
842 */
843static void xhci_clear_command_ring(struct xhci_hcd *xhci)
844{
845 struct xhci_ring *ring;
846 struct xhci_segment *seg;
847
848 ring = xhci->cmd_ring;
849 seg = ring->deq_seg;
850 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800851 memset(seg->trbs, 0,
852 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
853 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
854 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800855 seg = seg->next;
856 } while (seg != ring->deq_seg);
857
858 /* Reset the software enqueue and dequeue pointers */
859 ring->deq_seg = ring->first_seg;
860 ring->dequeue = ring->first_seg->trbs;
861 ring->enq_seg = ring->deq_seg;
862 ring->enqueue = ring->dequeue;
863
Andiry Xub008df62012-03-05 17:49:34 +0800864 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800865 /*
866 * Ring is now zeroed, so the HW should look for change of ownership
867 * when the cycle bit is set to 1.
868 */
869 ring->cycle_state = 1;
870
871 /*
872 * Reset the hardware dequeue pointer.
873 * Yes, this will need to be re-written after resume, but we're paranoid
874 * and want to make sure the hardware doesn't access bogus memory
875 * because, say, the BIOS or an SMI started the host without changing
876 * the command ring pointers.
877 */
878 xhci_set_cmd_ring_deq(xhci);
879}
880
Andiry Xu5535b1d2010-10-14 07:23:06 -0700881/*
882 * Stop HC (not bus-specific)
883 *
884 * This is called when the machine transition into S3/S4 mode.
885 *
886 */
887int xhci_suspend(struct xhci_hcd *xhci)
888{
889 int rc = 0;
890 struct usb_hcd *hcd = xhci_to_hcd(xhci);
891 u32 command;
892
Felipe Balbi77b84762012-10-19 10:55:16 +0300893 if (hcd->state != HC_STATE_SUSPENDED ||
894 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
895 return -EINVAL;
896
Sarah Sharpc52804a2012-11-27 12:30:23 -0800897 /* Don't poll the roothubs on bus suspend. */
898 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
899 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
900 del_timer_sync(&hcd->rh_timer);
901
Andiry Xu5535b1d2010-10-14 07:23:06 -0700902 spin_lock_irq(&xhci->lock);
903 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -0800904 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700905 /* step 1: stop endpoint */
906 /* skipped assuming that port suspend has done */
907
908 /* step 2: clear Run/Stop bit */
909 command = xhci_readl(xhci, &xhci->op_regs->command);
910 command &= ~CMD_RUN;
911 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd12012-10-25 13:27:51 -0700912 if (xhci_handshake(xhci, &xhci->op_regs->status,
Michael Spanga6e097d2012-09-14 13:05:49 -0400913 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC)) {
Andiry Xu5535b1d2010-10-14 07:23:06 -0700914 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
915 spin_unlock_irq(&xhci->lock);
916 return -ETIMEDOUT;
917 }
Sarah Sharp89821322010-11-12 11:59:31 -0800918 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700919
920 /* step 3: save registers */
921 xhci_save_registers(xhci);
922
923 /* step 4: set CSS flag */
924 command = xhci_readl(xhci, &xhci->op_regs->command);
925 command |= CMD_CSS;
926 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd12012-10-25 13:27:51 -0700927 if (xhci_handshake(xhci, &xhci->op_regs->status,
928 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +0800929 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700930 spin_unlock_irq(&xhci->lock);
931 return -ETIMEDOUT;
932 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700933 spin_unlock_irq(&xhci->lock);
934
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500935 /*
936 * Deleting Compliance Mode Recovery Timer because the xHCI Host
937 * is about to be suspended.
938 */
939 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
940 (!(xhci_all_ports_seen_u0(xhci)))) {
941 del_timer_sync(&xhci->comp_mode_recovery_timer);
Tony Camuso58b1d792013-04-05 14:27:07 -0400942 xhci_dbg(xhci, "%s: compliance mode recovery timer deleted\n",
943 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500944 }
945
Andiry Xu00292272010-12-27 17:39:02 +0800946 /* step 5: remove core well power */
947 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700948 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800949
Andiry Xu5535b1d2010-10-14 07:23:06 -0700950 return rc;
951}
952
953/*
954 * start xHC (not bus-specific)
955 *
956 * This is called when the machine transition from S3/S4 mode.
957 *
958 */
959int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
960{
961 u32 command, temp = 0;
962 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -0800963 struct usb_hcd *secondary_hcd;
Alan Sternf69e3122011-11-03 11:37:10 -0400964 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -0500965 bool comp_timer_running = false;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700966
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800967 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300968 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800969 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800970 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
971 time_before(jiffies,
972 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -0700973 msleep(100);
974
Alan Sternf69e3122011-11-03 11:37:10 -0400975 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
976 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
977
Andiry Xu5535b1d2010-10-14 07:23:06 -0700978 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +0200979 if (xhci->quirks & XHCI_RESET_ON_RESUME)
980 hibernated = true;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700981
982 if (!hibernated) {
983 /* step 1: restore register */
984 xhci_restore_registers(xhci);
985 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800986 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700987 /* step 3: restore state and start state*/
988 /* step 3: set CRS flag */
989 command = xhci_readl(xhci, &xhci->op_regs->command);
990 command |= CMD_CRS;
991 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd12012-10-25 13:27:51 -0700992 if (xhci_handshake(xhci, &xhci->op_regs->status,
Andiry Xu622eb782012-06-13 10:51:57 +0800993 STS_RESTORE, 0, 10 * 1000)) {
994 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700995 spin_unlock_irq(&xhci->lock);
996 return -ETIMEDOUT;
997 }
998 temp = xhci_readl(xhci, &xhci->op_regs->status);
999 }
1000
1001 /* If restore operation fails, re-initialize the HC during resume */
1002 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -05001003
1004 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1005 !(xhci_all_ports_seen_u0(xhci))) {
1006 del_timer_sync(&xhci->comp_mode_recovery_timer);
1007 xhci_dbg(xhci, "Compliance Mode Recovery Timer deleted!\n");
1008 }
1009
Sarah Sharpfedd3832011-04-12 17:43:19 -07001010 /* Let the USB core know _both_ roothubs lost power. */
1011 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1012 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001013
1014 xhci_dbg(xhci, "Stop HCD\n");
1015 xhci_halt(xhci);
1016 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001017 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001018 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001019
1020#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
1021 /* Tell the event ring poll function not to reschedule */
1022 xhci->zombie = 1;
1023 del_timer_sync(&xhci->event_ring_timer);
1024#endif
1025
1026 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
1027 temp = xhci_readl(xhci, &xhci->op_regs->status);
1028 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
1029 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
1030 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
1031 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -08001032 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001033
1034 xhci_dbg(xhci, "cleaning up memory\n");
1035 xhci_mem_cleanup(xhci);
1036 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1037 xhci_readl(xhci, &xhci->op_regs->status));
1038
Sarah Sharp65b22f92010-12-17 12:35:05 -08001039 /* USB core calls the PCI reinit and start functions twice:
1040 * first with the primary HCD, and then with the secondary HCD.
1041 * If we don't do the same, the host will never be started.
1042 */
1043 if (!usb_hcd_is_primary_hcd(hcd))
1044 secondary_hcd = hcd;
1045 else
1046 secondary_hcd = xhci->shared_hcd;
1047
1048 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1049 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001050 if (retval)
1051 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001052 comp_timer_running = true;
1053
Sarah Sharp65b22f92010-12-17 12:35:05 -08001054 xhci_dbg(xhci, "Start the primary HCD\n");
1055 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001056 if (!retval) {
Alan Sternf69e3122011-11-03 11:37:10 -04001057 xhci_dbg(xhci, "Start the secondary HCD\n");
1058 retval = xhci_run(secondary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001059 }
Andiry Xu5535b1d2010-10-14 07:23:06 -07001060 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb3209372011-03-07 11:24:07 -08001061 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e3122011-11-03 11:37:10 -04001062 goto done;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001063 }
1064
Andiry Xu5535b1d2010-10-14 07:23:06 -07001065 /* step 4: set Run/Stop bit */
1066 command = xhci_readl(xhci, &xhci->op_regs->command);
1067 command |= CMD_RUN;
1068 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd12012-10-25 13:27:51 -07001069 xhci_handshake(xhci, &xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d2010-10-14 07:23:06 -07001070 0, 250 * 1000);
1071
1072 /* step 5: walk topology and initialize portsc,
1073 * portpmsc and portli
1074 */
1075 /* this is done in bus_resume */
1076
1077 /* step 6: restart each of the previously
1078 * Running endpoints by ringing their doorbells
1079 */
1080
Andiry Xu5535b1d2010-10-14 07:23:06 -07001081 spin_unlock_irq(&xhci->lock);
Alan Sternf69e3122011-11-03 11:37:10 -04001082
1083 done:
1084 if (retval == 0) {
1085 usb_hcd_resume_root_hub(hcd);
1086 usb_hcd_resume_root_hub(xhci->shared_hcd);
1087 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001088
1089 /*
1090 * If system is subject to the Quirk, Compliance Mode Timer needs to
1091 * be re-initialized Always after a system resume. Ports are subject
1092 * to suffer the Compliance Mode issue again. It doesn't matter if
1093 * ports have entered previously to U0 before system's suspension.
1094 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001095 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001096 compliance_mode_recovery_timer_init(xhci);
1097
Sarah Sharpc52804a2012-11-27 12:30:23 -08001098 /* Re-enable port polling. */
1099 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1100 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1101 usb_hcd_poll_rh_status(hcd);
1102
Alan Sternf69e3122011-11-03 11:37:10 -04001103 return retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001104}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001105#endif /* CONFIG_PM */
1106
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001107/*-------------------------------------------------------------------------*/
1108
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001109/**
1110 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1111 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1112 * value to right shift 1 for the bitmask.
1113 *
1114 * Index = (epnum * 2) + direction - 1,
1115 * where direction = 0 for OUT, 1 for IN.
1116 * For control endpoints, the IN index is used (OUT index is unused), so
1117 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1118 */
1119unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1120{
1121 unsigned int index;
1122 if (usb_endpoint_xfer_control(desc))
1123 index = (unsigned int) (usb_endpoint_num(desc)*2);
1124 else
1125 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1126 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1127 return index;
1128}
1129
Julius Werner01c5f442013-04-15 15:55:04 -07001130/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1131 * address from the XHCI endpoint index.
1132 */
1133unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1134{
1135 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1136 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1137 return direction | number;
1138}
1139
Sarah Sharpf94e01862009-04-27 19:58:38 -07001140/* Find the flag for this endpoint (for use in the control context). Use the
1141 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1142 * bit 1, etc.
1143 */
1144unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1145{
1146 return 1 << (xhci_get_endpoint_index(desc) + 1);
1147}
1148
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001149/* Find the flag for this endpoint (for use in the control context). Use the
1150 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1151 * bit 1, etc.
1152 */
1153unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1154{
1155 return 1 << (ep_index + 1);
1156}
1157
Sarah Sharpf94e01862009-04-27 19:58:38 -07001158/* Compute the last valid endpoint context index. Basically, this is the
1159 * endpoint index plus one. For slot contexts with more than valid endpoint,
1160 * we find the most significant bit set in the added contexts flags.
1161 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1162 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1163 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001164unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001165{
1166 return fls(added_ctxs) - 1;
1167}
1168
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001169/* Returns 1 if the arguments are OK;
1170 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1171 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001172static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001173 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1174 const char *func) {
1175 struct xhci_hcd *xhci;
1176 struct xhci_virt_device *virt_dev;
1177
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001178 if (!hcd || (check_ep && !ep) || !udev) {
1179 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
1180 func);
1181 return -EINVAL;
1182 }
1183 if (!udev->parent) {
1184 printk(KERN_DEBUG "xHCI %s called for root hub\n",
1185 func);
1186 return 0;
1187 }
Andiry Xu64927732010-10-14 07:22:45 -07001188
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001189 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001190 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001191 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Andiry Xu64927732010-10-14 07:22:45 -07001192 printk(KERN_DEBUG "xHCI %s called with unaddressed "
1193 "device\n", func);
1194 return -EINVAL;
1195 }
1196
1197 virt_dev = xhci->devs[udev->slot_id];
1198 if (virt_dev->udev != udev) {
1199 printk(KERN_DEBUG "xHCI %s called with udev and "
1200 "virt_dev does not match\n", func);
1201 return -EINVAL;
1202 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001203 }
Andiry Xu64927732010-10-14 07:22:45 -07001204
Sarah Sharp203a8662013-07-24 10:27:13 -07001205 if (xhci->xhc_state & XHCI_STATE_HALTED)
1206 return -ENODEV;
1207
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001208 return 1;
1209}
1210
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001211static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001212 struct usb_device *udev, struct xhci_command *command,
1213 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001214
1215/*
1216 * Full speed devices may have a max packet size greater than 8 bytes, but the
1217 * USB core doesn't know that until it reads the first 8 bytes of the
1218 * descriptor. If the usb_device's max packet size changes after that point,
1219 * we need to issue an evaluate context command and wait on it.
1220 */
1221static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1222 unsigned int ep_index, struct urb *urb)
1223{
1224 struct xhci_container_ctx *in_ctx;
1225 struct xhci_container_ctx *out_ctx;
1226 struct xhci_input_control_ctx *ctrl_ctx;
1227 struct xhci_ep_ctx *ep_ctx;
1228 int max_packet_size;
1229 int hw_max_packet_size;
1230 int ret = 0;
1231
1232 out_ctx = xhci->devs[slot_id]->out_ctx;
1233 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001234 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001235 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001236 if (hw_max_packet_size != max_packet_size) {
1237 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
1238 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
1239 max_packet_size);
1240 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
1241 hw_max_packet_size);
1242 xhci_dbg(xhci, "Issuing evaluate context command.\n");
1243
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001244 /* Set up the input context flags for the command */
1245 /* FIXME: This won't work if a non-default control endpoint
1246 * changes max packet sizes.
1247 */
Sarah Sharp92f8e762013-04-23 17:11:14 -07001248 in_ctx = xhci->devs[slot_id]->in_ctx;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001249 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001250 if (!ctrl_ctx) {
1251 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1252 __func__);
1253 return -ENOMEM;
1254 }
1255 /* Set up the modified control endpoint 0 */
1256 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1257 xhci->devs[slot_id]->out_ctx, ep_index);
1258
1259 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1260 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1261 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1262
Matt Evans28ccd292011-03-29 13:40:46 +11001263 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001264 ctrl_ctx->drop_flags = 0;
1265
1266 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1267 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1268 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1269 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1270
Sarah Sharp913a8a32009-09-04 10:53:13 -07001271 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1272 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001273
1274 /* Clean up the input context for later use by bandwidth
1275 * functions.
1276 */
Matt Evans28ccd292011-03-29 13:40:46 +11001277 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001278 }
1279 return ret;
1280}
1281
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001282/*
1283 * non-error returns are a promise to giveback() the urb later
1284 * we drop ownership so next owner (or urb unlink) can get it
1285 */
1286int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1287{
1288 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Andiry Xu2ffdea22011-09-02 11:05:57 -07001289 struct xhci_td *buffer;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001290 unsigned long flags;
1291 int ret = 0;
1292 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001293 struct urb_priv *urb_priv;
1294 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001295
Andiry Xu64927732010-10-14 07:22:45 -07001296 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1297 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001298 return -EINVAL;
1299
1300 slot_id = urb->dev->slot_id;
1301 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001302
Alan Stern541c7d42010-06-22 16:39:10 -04001303 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001304 if (!in_interrupt())
1305 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1306 ret = -ESHUTDOWN;
1307 goto exit;
1308 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001309
1310 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1311 size = urb->number_of_packets;
1312 else
1313 size = 1;
1314
1315 urb_priv = kzalloc(sizeof(struct urb_priv) +
1316 size * sizeof(struct xhci_td *), mem_flags);
1317 if (!urb_priv)
1318 return -ENOMEM;
1319
Andiry Xu2ffdea22011-09-02 11:05:57 -07001320 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1321 if (!buffer) {
1322 kfree(urb_priv);
1323 return -ENOMEM;
1324 }
1325
Andiry Xu8e51adc2010-07-22 15:23:31 -07001326 for (i = 0; i < size; i++) {
Andiry Xu2ffdea22011-09-02 11:05:57 -07001327 urb_priv->td[i] = buffer;
1328 buffer++;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001329 }
1330
1331 urb_priv->length = size;
1332 urb_priv->td_cnt = 0;
1333 urb->hcpriv = urb_priv;
1334
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001335 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1336 /* Check to see if the max packet size for the default control
1337 * endpoint changed during FS device enumeration
1338 */
1339 if (urb->dev->speed == USB_SPEED_FULL) {
1340 ret = xhci_check_maxpacket(xhci, slot_id,
1341 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001342 if (ret < 0) {
1343 xhci_urb_free_priv(xhci, urb_priv);
1344 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001345 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001346 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001347 }
1348
Sarah Sharpb11069f2009-07-27 12:03:23 -07001349 /* We have a spinlock and interrupts disabled, so we must pass
1350 * atomic context to this function, which may allocate memory.
1351 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001352 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001353 if (xhci->xhc_state & XHCI_STATE_DYING)
1354 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001355 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001356 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001357 if (ret)
1358 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001359 spin_unlock_irqrestore(&xhci->lock, flags);
1360 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1361 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001362 if (xhci->xhc_state & XHCI_STATE_DYING)
1363 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001364 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1365 EP_GETTING_STREAMS) {
1366 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1367 "is transitioning to using streams.\n");
1368 ret = -EINVAL;
1369 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1370 EP_GETTING_NO_STREAMS) {
1371 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1372 "is transitioning to "
1373 "not having streams.\n");
1374 ret = -EINVAL;
1375 } else {
1376 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1377 slot_id, ep_index);
1378 }
Sarah Sharpd13565c2011-07-22 14:34:34 -07001379 if (ret)
1380 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001381 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001382 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1383 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001384 if (xhci->xhc_state & XHCI_STATE_DYING)
1385 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001386 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1387 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001388 if (ret)
1389 goto free_priv;
Sarah Sharp624defa2009-09-02 12:14:28 -07001390 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001391 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001392 spin_lock_irqsave(&xhci->lock, flags);
1393 if (xhci->xhc_state & XHCI_STATE_DYING)
1394 goto dying;
1395 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1396 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001397 if (ret)
1398 goto free_priv;
Andiry Xu787f4e52010-07-22 15:23:52 -07001399 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001400 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001401exit:
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001402 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001403dying:
1404 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1405 "non-responsive xHCI host.\n",
1406 urb->ep->desc.bEndpointAddress, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001407 ret = -ESHUTDOWN;
1408free_priv:
1409 xhci_urb_free_priv(xhci, urb_priv);
1410 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001411 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001412 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001413}
1414
Sarah Sharp021bff92010-07-29 22:12:20 -07001415/* Get the right ring for the given URB.
1416 * If the endpoint supports streams, boundary check the URB's stream ID.
1417 * If the endpoint doesn't support streams, return the singular endpoint ring.
1418 */
1419static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1420 struct urb *urb)
1421{
1422 unsigned int slot_id;
1423 unsigned int ep_index;
1424 unsigned int stream_id;
1425 struct xhci_virt_ep *ep;
1426
1427 slot_id = urb->dev->slot_id;
1428 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1429 stream_id = urb->stream_id;
1430 ep = &xhci->devs[slot_id]->eps[ep_index];
1431 /* Common case: no streams */
1432 if (!(ep->ep_state & EP_HAS_STREAMS))
1433 return ep->ring;
1434
1435 if (stream_id == 0) {
1436 xhci_warn(xhci,
1437 "WARN: Slot ID %u, ep index %u has streams, "
1438 "but URB has no stream ID.\n",
1439 slot_id, ep_index);
1440 return NULL;
1441 }
1442
1443 if (stream_id < ep->stream_info->num_streams)
1444 return ep->stream_info->stream_rings[stream_id];
1445
1446 xhci_warn(xhci,
1447 "WARN: Slot ID %u, ep index %u has "
1448 "stream IDs 1 to %u allocated, "
1449 "but stream ID %u is requested.\n",
1450 slot_id, ep_index,
1451 ep->stream_info->num_streams - 1,
1452 stream_id);
1453 return NULL;
1454}
1455
Sarah Sharpae636742009-04-29 19:02:31 -07001456/*
1457 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1458 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1459 * should pick up where it left off in the TD, unless a Set Transfer Ring
1460 * Dequeue Pointer is issued.
1461 *
1462 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1463 * the ring. Since the ring is a contiguous structure, they can't be physically
1464 * removed. Instead, there are two options:
1465 *
1466 * 1) If the HC is in the middle of processing the URB to be canceled, we
1467 * simply move the ring's dequeue pointer past those TRBs using the Set
1468 * Transfer Ring Dequeue Pointer command. This will be the common case,
1469 * when drivers timeout on the last submitted URB and attempt to cancel.
1470 *
1471 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1472 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1473 * HC will need to invalidate the any TRBs it has cached after the stop
1474 * endpoint command, as noted in the xHCI 0.95 errata.
1475 *
1476 * 3) The TD may have completed by the time the Stop Endpoint Command
1477 * completes, so software needs to handle that case too.
1478 *
1479 * This function should protect against the TD enqueueing code ringing the
1480 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1481 * It also needs to account for multiple cancellations on happening at the same
1482 * time for the same endpoint.
1483 *
1484 * Note that this function can be called in any context, or so says
1485 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001486 */
1487int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1488{
Sarah Sharpae636742009-04-29 19:02:31 -07001489 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001490 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001491 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001492 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001493 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001494 struct xhci_td *td;
1495 unsigned int ep_index;
1496 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001497 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001498
1499 xhci = hcd_to_xhci(hcd);
1500 spin_lock_irqsave(&xhci->lock, flags);
1501 /* Make sure the URB hasn't completed or been unlinked already */
1502 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1503 if (ret || !urb->hcpriv)
1504 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001505 temp = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -08001506 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001507 xhci_dbg(xhci, "HW died, freeing TD.\n");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001508 urb_priv = urb->hcpriv;
Sarah Sharp585df1d2011-08-02 15:43:40 -07001509 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1510 td = urb_priv->td[i];
1511 if (!list_empty(&td->td_list))
1512 list_del_init(&td->td_list);
1513 if (!list_empty(&td->cancelled_td_list))
1514 list_del_init(&td->cancelled_td_list);
1515 }
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001516
1517 usb_hcd_unlink_urb_from_ep(hcd, urb);
1518 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001519 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001520 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001521 return ret;
1522 }
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001523 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1524 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001525 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1526 "non-responsive xHCI host.\n",
1527 urb->ep->desc.bEndpointAddress, urb);
1528 /* Let the stop endpoint command watchdog timer (which set this
1529 * state) finish cleaning up the endpoint TD lists. We must
1530 * have caught it in the middle of dropping a lock and giving
1531 * back an URB.
1532 */
1533 goto done;
1534 }
Sarah Sharpae636742009-04-29 19:02:31 -07001535
Sarah Sharpae636742009-04-29 19:02:31 -07001536 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001537 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001538 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1539 if (!ep_ring) {
1540 ret = -EINVAL;
1541 goto done;
1542 }
1543
Andiry Xu8e51adc2010-07-22 15:23:31 -07001544 urb_priv = urb->hcpriv;
Sarah Sharp79688ac2011-12-19 16:56:04 -08001545 i = urb_priv->td_cnt;
1546 if (i < urb_priv->length)
1547 xhci_dbg(xhci, "Cancel URB %p, dev %s, ep 0x%x, "
1548 "starting at offset 0x%llx\n",
1549 urb, urb->dev->devpath,
1550 urb->ep->desc.bEndpointAddress,
1551 (unsigned long long) xhci_trb_virt_to_dma(
1552 urb_priv->td[i]->start_seg,
1553 urb_priv->td[i]->first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001554
Sarah Sharp79688ac2011-12-19 16:56:04 -08001555 for (; i < urb_priv->length; i++) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001556 td = urb_priv->td[i];
1557 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1558 }
1559
Sarah Sharpae636742009-04-29 19:02:31 -07001560 /* Queue a stop endpoint command, but only if this is
1561 * the first cancellation to be handled.
1562 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001563 if (!(ep->ep_state & EP_HALT_PENDING)) {
1564 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001565 ep->stop_cmds_pending++;
1566 ep->stop_cmd_timer.expires = jiffies +
1567 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1568 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001569 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001570 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001571 }
1572done:
1573 spin_unlock_irqrestore(&xhci->lock, flags);
1574 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001575}
1576
Sarah Sharpf94e01862009-04-27 19:58:38 -07001577/* Drop an endpoint from a new bandwidth configuration for this device.
1578 * Only one call to this function is allowed per endpoint before
1579 * check_bandwidth() or reset_bandwidth() must be called.
1580 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1581 * add the endpoint to the schedule with possibly new parameters denoted by a
1582 * different endpoint descriptor in usb_host_endpoint.
1583 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1584 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001585 *
1586 * The USB core will not allow URBs to be queued to an endpoint that is being
1587 * disabled, so there's no need for mutual exclusion to protect
1588 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001589 */
1590int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1591 struct usb_host_endpoint *ep)
1592{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001593 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001594 struct xhci_container_ctx *in_ctx, *out_ctx;
1595 struct xhci_input_control_ctx *ctrl_ctx;
1596 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001597 unsigned int last_ctx;
1598 unsigned int ep_index;
1599 struct xhci_ep_ctx *ep_ctx;
1600 u32 drop_flag;
1601 u32 new_add_flags, new_drop_flags, new_slot_info;
1602 int ret;
1603
Andiry Xu64927732010-10-14 07:22:45 -07001604 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001605 if (ret <= 0)
1606 return ret;
1607 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001608 if (xhci->xhc_state & XHCI_STATE_DYING)
1609 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001610
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001611 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001612 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1613 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1614 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1615 __func__, drop_flag);
1616 return 0;
1617 }
1618
Sarah Sharpf94e01862009-04-27 19:58:38 -07001619 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001620 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1621 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001622 if (!ctrl_ctx) {
1623 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1624 __func__);
1625 return 0;
1626 }
1627
Sarah Sharpf94e01862009-04-27 19:58:38 -07001628 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001629 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001630 /* If the HC already knows the endpoint is disabled,
1631 * or the HCD has noted it is disabled, ignore this request
1632 */
Matt Evansf5960b62011-06-01 10:22:55 +10001633 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1634 cpu_to_le32(EP_STATE_DISABLED)) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001635 le32_to_cpu(ctrl_ctx->drop_flags) &
1636 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001637 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1638 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001639 return 0;
1640 }
1641
Matt Evans28ccd292011-03-29 13:40:46 +11001642 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1643 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001644
Matt Evans28ccd292011-03-29 13:40:46 +11001645 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1646 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001647
Matt Evans28ccd292011-03-29 13:40:46 +11001648 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
John Yound115b042009-07-27 12:05:15 -07001649 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001650 /* Update the last valid endpoint context, if we deleted the last one */
Matt Evans28ccd292011-03-29 13:40:46 +11001651 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1652 LAST_CTX(last_ctx)) {
1653 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1654 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001655 }
Matt Evans28ccd292011-03-29 13:40:46 +11001656 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001657
1658 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1659
Sarah Sharpf94e01862009-04-27 19:58:38 -07001660 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1661 (unsigned int) ep->desc.bEndpointAddress,
1662 udev->slot_id,
1663 (unsigned int) new_drop_flags,
1664 (unsigned int) new_add_flags,
1665 (unsigned int) new_slot_info);
1666 return 0;
1667}
1668
1669/* Add an endpoint to a new possible bandwidth configuration for this device.
1670 * Only one call to this function is allowed per endpoint before
1671 * check_bandwidth() or reset_bandwidth() must be called.
1672 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1673 * add the endpoint to the schedule with possibly new parameters denoted by a
1674 * different endpoint descriptor in usb_host_endpoint.
1675 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1676 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001677 *
1678 * The USB core will not allow URBs to be queued to an endpoint until the
1679 * configuration or alt setting is installed in the device, so there's no need
1680 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001681 */
1682int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1683 struct usb_host_endpoint *ep)
1684{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001685 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001686 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001687 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001688 struct xhci_slot_ctx *slot_ctx;
1689 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001690 u32 added_ctxs;
1691 unsigned int last_ctx;
1692 u32 new_add_flags, new_drop_flags, new_slot_info;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001693 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001694 int ret = 0;
1695
Andiry Xu64927732010-10-14 07:22:45 -07001696 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001697 if (ret <= 0) {
1698 /* So we won't queue a reset ep command for a root hub */
1699 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001700 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001701 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001702 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001703 if (xhci->xhc_state & XHCI_STATE_DYING)
1704 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001705
1706 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1707 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1708 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1709 /* FIXME when we have to issue an evaluate endpoint command to
1710 * deal with ep0 max packet size changing once we get the
1711 * descriptors
1712 */
1713 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1714 __func__, added_ctxs);
1715 return 0;
1716 }
1717
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001718 virt_dev = xhci->devs[udev->slot_id];
1719 in_ctx = virt_dev->in_ctx;
1720 out_ctx = virt_dev->out_ctx;
John Yound115b042009-07-27 12:05:15 -07001721 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001722 if (!ctrl_ctx) {
1723 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1724 __func__);
1725 return 0;
1726 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001727
Sarah Sharp92f8e762013-04-23 17:11:14 -07001728 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001729 /* If this endpoint is already in use, and the upper layers are trying
1730 * to add it again without dropping it, reject the addition.
1731 */
1732 if (virt_dev->eps[ep_index].ring &&
1733 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1734 xhci_get_endpoint_flag(&ep->desc))) {
1735 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1736 "without dropping it.\n",
1737 (unsigned int) ep->desc.bEndpointAddress);
1738 return -EINVAL;
1739 }
1740
Sarah Sharpf94e01862009-04-27 19:58:38 -07001741 /* If the HCD has already noted the endpoint is enabled,
1742 * ignore this request.
1743 */
Matt Evans28ccd292011-03-29 13:40:46 +11001744 if (le32_to_cpu(ctrl_ctx->add_flags) &
1745 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001746 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1747 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001748 return 0;
1749 }
1750
Sarah Sharpf88ba782009-05-14 11:44:22 -07001751 /*
1752 * Configuration and alternate setting changes must be done in
1753 * process context, not interrupt context (or so documenation
1754 * for usb_set_interface() and usb_set_configuration() claim).
1755 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001756 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001757 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1758 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001759 return -ENOMEM;
1760 }
1761
Matt Evans28ccd292011-03-29 13:40:46 +11001762 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1763 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001764
1765 /* If xhci_endpoint_disable() was called for this endpoint, but the
1766 * xHC hasn't been notified yet through the check_bandwidth() call,
1767 * this re-adds a new state for the endpoint from the new endpoint
1768 * descriptors. We must drop and re-add this endpoint, so we leave the
1769 * drop flags alone.
1770 */
Matt Evans28ccd292011-03-29 13:40:46 +11001771 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001772
John Yound115b042009-07-27 12:05:15 -07001773 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001774 /* Update the last valid endpoint context, if we just added one past */
Matt Evans28ccd292011-03-29 13:40:46 +11001775 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1776 LAST_CTX(last_ctx)) {
1777 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1778 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001779 }
Matt Evans28ccd292011-03-29 13:40:46 +11001780 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001781
Sarah Sharpa1587d92009-07-27 12:03:15 -07001782 /* Store the usb_device pointer for later use */
1783 ep->hcpriv = udev;
1784
Sarah Sharpf94e01862009-04-27 19:58:38 -07001785 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1786 (unsigned int) ep->desc.bEndpointAddress,
1787 udev->slot_id,
1788 (unsigned int) new_drop_flags,
1789 (unsigned int) new_add_flags,
1790 (unsigned int) new_slot_info);
1791 return 0;
1792}
1793
John Yound115b042009-07-27 12:05:15 -07001794static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001795{
John Yound115b042009-07-27 12:05:15 -07001796 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001797 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001798 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001799 int i;
1800
Sarah Sharp92f8e762013-04-23 17:11:14 -07001801 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1802 if (!ctrl_ctx) {
1803 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1804 __func__);
1805 return;
1806 }
1807
Sarah Sharpf94e01862009-04-27 19:58:38 -07001808 /* When a device's add flag and drop flag are zero, any subsequent
1809 * configure endpoint command will leave that endpoint's state
1810 * untouched. Make sure we don't leave any old state in the input
1811 * endpoint contexts.
1812 */
John Yound115b042009-07-27 12:05:15 -07001813 ctrl_ctx->drop_flags = 0;
1814 ctrl_ctx->add_flags = 0;
1815 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001816 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001817 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001818 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001819 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001820 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001821 ep_ctx->ep_info = 0;
1822 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001823 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001824 ep_ctx->tx_info = 0;
1825 }
1826}
1827
Sarah Sharpf2217e82009-08-07 14:04:43 -07001828static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001829 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001830{
1831 int ret;
1832
Sarah Sharp913a8a32009-09-04 10:53:13 -07001833 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001834 case COMP_ENOMEM:
1835 dev_warn(&udev->dev, "Not enough host controller resources "
1836 "for new device state.\n");
1837 ret = -ENOMEM;
1838 /* FIXME: can we allocate more resources for the HC? */
1839 break;
1840 case COMP_BW_ERR:
Hans de Goede71d85722012-01-04 23:29:18 +01001841 case COMP_2ND_BW_ERR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001842 dev_warn(&udev->dev, "Not enough bandwidth "
1843 "for new device state.\n");
1844 ret = -ENOSPC;
1845 /* FIXME: can we go back to the old state? */
1846 break;
1847 case COMP_TRB_ERR:
1848 /* the HCD set up something wrong */
1849 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1850 "add flag = 1, "
1851 "and endpoint is not disabled.\n");
1852 ret = -EINVAL;
1853 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001854 case COMP_DEV_ERR:
1855 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1856 "configure command.\n");
1857 ret = -ENODEV;
1858 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001859 case COMP_SUCCESS:
1860 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1861 ret = 0;
1862 break;
1863 default:
1864 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001865 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001866 ret = -EINVAL;
1867 break;
1868 }
1869 return ret;
1870}
1871
1872static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001873 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001874{
1875 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001876 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001877
Sarah Sharp913a8a32009-09-04 10:53:13 -07001878 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001879 case COMP_EINVAL:
1880 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1881 "context command.\n");
1882 ret = -EINVAL;
1883 break;
1884 case COMP_EBADSLT:
1885 dev_warn(&udev->dev, "WARN: slot not enabled for"
1886 "evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001887 ret = -EINVAL;
1888 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001889 case COMP_CTX_STATE:
1890 dev_warn(&udev->dev, "WARN: invalid context state for "
1891 "evaluate context command.\n");
1892 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1893 ret = -EINVAL;
1894 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001895 case COMP_DEV_ERR:
1896 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1897 "context command.\n");
1898 ret = -ENODEV;
1899 break;
Alex He1bb73a82011-05-05 18:14:12 +08001900 case COMP_MEL_ERR:
1901 /* Max Exit Latency too large error */
1902 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1903 ret = -EINVAL;
1904 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001905 case COMP_SUCCESS:
1906 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1907 ret = 0;
1908 break;
1909 default:
1910 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001911 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001912 ret = -EINVAL;
1913 break;
1914 }
1915 return ret;
1916}
1917
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001918static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001919 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001920{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001921 u32 valid_add_flags;
1922 u32 valid_drop_flags;
1923
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001924 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1925 * (bit 1). The default control endpoint is added during the Address
1926 * Device command and is never removed until the slot is disabled.
1927 */
1928 valid_add_flags = ctrl_ctx->add_flags >> 2;
1929 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1930
1931 /* Use hweight32 to count the number of ones in the add flags, or
1932 * number of endpoints added. Don't count endpoints that are changed
1933 * (both added and dropped).
1934 */
1935 return hweight32(valid_add_flags) -
1936 hweight32(valid_add_flags & valid_drop_flags);
1937}
1938
1939static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001940 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001941{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001942 u32 valid_add_flags;
1943 u32 valid_drop_flags;
1944
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001945 valid_add_flags = ctrl_ctx->add_flags >> 2;
1946 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1947
1948 return hweight32(valid_drop_flags) -
1949 hweight32(valid_add_flags & valid_drop_flags);
1950}
1951
1952/*
1953 * We need to reserve the new number of endpoints before the configure endpoint
1954 * command completes. We can't subtract the dropped endpoints from the number
1955 * of active endpoints until the command completes because we can oversubscribe
1956 * the host in this case:
1957 *
1958 * - the first configure endpoint command drops more endpoints than it adds
1959 * - a second configure endpoint command that adds more endpoints is queued
1960 * - the first configure endpoint command fails, so the config is unchanged
1961 * - the second command may succeed, even though there isn't enough resources
1962 *
1963 * Must be called with xhci->lock held.
1964 */
1965static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001966 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001967{
1968 u32 added_eps;
1969
Sarah Sharp92f8e762013-04-23 17:11:14 -07001970 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001971 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
1972 xhci_dbg(xhci, "Not enough ep ctxs: "
1973 "%u active, need to add %u, limit is %u.\n",
1974 xhci->num_active_eps, added_eps,
1975 xhci->limit_active_eps);
1976 return -ENOMEM;
1977 }
1978 xhci->num_active_eps += added_eps;
1979 xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
1980 xhci->num_active_eps);
1981 return 0;
1982}
1983
1984/*
1985 * The configure endpoint was failed by the xHC for some other reason, so we
1986 * need to revert the resources that failed configuration would have used.
1987 *
1988 * Must be called with xhci->lock held.
1989 */
1990static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001991 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001992{
1993 u32 num_failed_eps;
1994
Sarah Sharp92f8e762013-04-23 17:11:14 -07001995 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001996 xhci->num_active_eps -= num_failed_eps;
1997 xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
1998 num_failed_eps,
1999 xhci->num_active_eps);
2000}
2001
2002/*
2003 * Now that the command has completed, clean up the active endpoint count by
2004 * subtracting out the endpoints that were dropped (but not changed).
2005 *
2006 * Must be called with xhci->lock held.
2007 */
2008static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002009 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002010{
2011 u32 num_dropped_eps;
2012
Sarah Sharp92f8e762013-04-23 17:11:14 -07002013 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002014 xhci->num_active_eps -= num_dropped_eps;
2015 if (num_dropped_eps)
2016 xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
2017 num_dropped_eps,
2018 xhci->num_active_eps);
2019}
2020
Felipe Balbied384bd2012-08-07 14:10:03 +03002021static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002022{
2023 switch (udev->speed) {
2024 case USB_SPEED_LOW:
2025 case USB_SPEED_FULL:
2026 return FS_BLOCK;
2027 case USB_SPEED_HIGH:
2028 return HS_BLOCK;
2029 case USB_SPEED_SUPER:
2030 return SS_BLOCK;
2031 case USB_SPEED_UNKNOWN:
2032 case USB_SPEED_WIRELESS:
2033 default:
2034 /* Should never happen */
2035 return 1;
2036 }
2037}
2038
Felipe Balbied384bd2012-08-07 14:10:03 +03002039static unsigned int
2040xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002041{
2042 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2043 return LS_OVERHEAD;
2044 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2045 return FS_OVERHEAD;
2046 return HS_OVERHEAD;
2047}
2048
2049/* If we are changing a LS/FS device under a HS hub,
2050 * make sure (if we are activating a new TT) that the HS bus has enough
2051 * bandwidth for this new TT.
2052 */
2053static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2054 struct xhci_virt_device *virt_dev,
2055 int old_active_eps)
2056{
2057 struct xhci_interval_bw_table *bw_table;
2058 struct xhci_tt_bw_info *tt_info;
2059
2060 /* Find the bandwidth table for the root port this TT is attached to. */
2061 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2062 tt_info = virt_dev->tt_info;
2063 /* If this TT already had active endpoints, the bandwidth for this TT
2064 * has already been added. Removing all periodic endpoints (and thus
2065 * making the TT enactive) will only decrease the bandwidth used.
2066 */
2067 if (old_active_eps)
2068 return 0;
2069 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2070 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2071 return -ENOMEM;
2072 return 0;
2073 }
2074 /* Not sure why we would have no new active endpoints...
2075 *
2076 * Maybe because of an Evaluate Context change for a hub update or a
2077 * control endpoint 0 max packet size change?
2078 * FIXME: skip the bandwidth calculation in that case.
2079 */
2080 return 0;
2081}
2082
Sarah Sharp2b698992011-09-13 16:41:13 -07002083static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2084 struct xhci_virt_device *virt_dev)
2085{
2086 unsigned int bw_reserved;
2087
2088 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2089 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2090 return -ENOMEM;
2091
2092 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2093 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2094 return -ENOMEM;
2095
2096 return 0;
2097}
2098
Sarah Sharpc29eea62011-09-02 11:05:52 -07002099/*
2100 * This algorithm is a very conservative estimate of the worst-case scheduling
2101 * scenario for any one interval. The hardware dynamically schedules the
2102 * packets, so we can't tell which microframe could be the limiting factor in
2103 * the bandwidth scheduling. This only takes into account periodic endpoints.
2104 *
2105 * Obviously, we can't solve an NP complete problem to find the minimum worst
2106 * case scenario. Instead, we come up with an estimate that is no less than
2107 * the worst case bandwidth used for any one microframe, but may be an
2108 * over-estimate.
2109 *
2110 * We walk the requirements for each endpoint by interval, starting with the
2111 * smallest interval, and place packets in the schedule where there is only one
2112 * possible way to schedule packets for that interval. In order to simplify
2113 * this algorithm, we record the largest max packet size for each interval, and
2114 * assume all packets will be that size.
2115 *
2116 * For interval 0, we obviously must schedule all packets for each interval.
2117 * The bandwidth for interval 0 is just the amount of data to be transmitted
2118 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2119 * the number of packets).
2120 *
2121 * For interval 1, we have two possible microframes to schedule those packets
2122 * in. For this algorithm, if we can schedule the same number of packets for
2123 * each possible scheduling opportunity (each microframe), we will do so. The
2124 * remaining number of packets will be saved to be transmitted in the gaps in
2125 * the next interval's scheduling sequence.
2126 *
2127 * As we move those remaining packets to be scheduled with interval 2 packets,
2128 * we have to double the number of remaining packets to transmit. This is
2129 * because the intervals are actually powers of 2, and we would be transmitting
2130 * the previous interval's packets twice in this interval. We also have to be
2131 * sure that when we look at the largest max packet size for this interval, we
2132 * also look at the largest max packet size for the remaining packets and take
2133 * the greater of the two.
2134 *
2135 * The algorithm continues to evenly distribute packets in each scheduling
2136 * opportunity, and push the remaining packets out, until we get to the last
2137 * interval. Then those packets and their associated overhead are just added
2138 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002139 */
2140static int xhci_check_bw_table(struct xhci_hcd *xhci,
2141 struct xhci_virt_device *virt_dev,
2142 int old_active_eps)
2143{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002144 unsigned int bw_reserved;
2145 unsigned int max_bandwidth;
2146 unsigned int bw_used;
2147 unsigned int block_size;
2148 struct xhci_interval_bw_table *bw_table;
2149 unsigned int packet_size = 0;
2150 unsigned int overhead = 0;
2151 unsigned int packets_transmitted = 0;
2152 unsigned int packets_remaining = 0;
2153 unsigned int i;
2154
Sarah Sharp2b698992011-09-13 16:41:13 -07002155 if (virt_dev->udev->speed == USB_SPEED_SUPER)
2156 return xhci_check_ss_bw(xhci, virt_dev);
2157
Sarah Sharpc29eea62011-09-02 11:05:52 -07002158 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2159 max_bandwidth = HS_BW_LIMIT;
2160 /* Convert percent of bus BW reserved to blocks reserved */
2161 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2162 } else {
2163 max_bandwidth = FS_BW_LIMIT;
2164 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2165 }
2166
2167 bw_table = virt_dev->bw_table;
2168 /* We need to translate the max packet size and max ESIT payloads into
2169 * the units the hardware uses.
2170 */
2171 block_size = xhci_get_block_size(virt_dev->udev);
2172
2173 /* If we are manipulating a LS/FS device under a HS hub, double check
2174 * that the HS bus has enough bandwidth if we are activing a new TT.
2175 */
2176 if (virt_dev->tt_info) {
2177 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2178 virt_dev->real_port);
2179 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2180 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2181 "newly activated TT.\n");
2182 return -ENOMEM;
2183 }
2184 xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
2185 virt_dev->tt_info->slot_id,
2186 virt_dev->tt_info->ttport);
2187 } else {
2188 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2189 virt_dev->real_port);
2190 }
2191
2192 /* Add in how much bandwidth will be used for interval zero, or the
2193 * rounded max ESIT payload + number of packets * largest overhead.
2194 */
2195 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2196 bw_table->interval_bw[0].num_packets *
2197 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2198
2199 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2200 unsigned int bw_added;
2201 unsigned int largest_mps;
2202 unsigned int interval_overhead;
2203
2204 /*
2205 * How many packets could we transmit in this interval?
2206 * If packets didn't fit in the previous interval, we will need
2207 * to transmit that many packets twice within this interval.
2208 */
2209 packets_remaining = 2 * packets_remaining +
2210 bw_table->interval_bw[i].num_packets;
2211
2212 /* Find the largest max packet size of this or the previous
2213 * interval.
2214 */
2215 if (list_empty(&bw_table->interval_bw[i].endpoints))
2216 largest_mps = 0;
2217 else {
2218 struct xhci_virt_ep *virt_ep;
2219 struct list_head *ep_entry;
2220
2221 ep_entry = bw_table->interval_bw[i].endpoints.next;
2222 virt_ep = list_entry(ep_entry,
2223 struct xhci_virt_ep, bw_endpoint_list);
2224 /* Convert to blocks, rounding up */
2225 largest_mps = DIV_ROUND_UP(
2226 virt_ep->bw_info.max_packet_size,
2227 block_size);
2228 }
2229 if (largest_mps > packet_size)
2230 packet_size = largest_mps;
2231
2232 /* Use the larger overhead of this or the previous interval. */
2233 interval_overhead = xhci_get_largest_overhead(
2234 &bw_table->interval_bw[i]);
2235 if (interval_overhead > overhead)
2236 overhead = interval_overhead;
2237
2238 /* How many packets can we evenly distribute across
2239 * (1 << (i + 1)) possible scheduling opportunities?
2240 */
2241 packets_transmitted = packets_remaining >> (i + 1);
2242
2243 /* Add in the bandwidth used for those scheduled packets */
2244 bw_added = packets_transmitted * (overhead + packet_size);
2245
2246 /* How many packets do we have remaining to transmit? */
2247 packets_remaining = packets_remaining % (1 << (i + 1));
2248
2249 /* What largest max packet size should those packets have? */
2250 /* If we've transmitted all packets, don't carry over the
2251 * largest packet size.
2252 */
2253 if (packets_remaining == 0) {
2254 packet_size = 0;
2255 overhead = 0;
2256 } else if (packets_transmitted > 0) {
2257 /* Otherwise if we do have remaining packets, and we've
2258 * scheduled some packets in this interval, take the
2259 * largest max packet size from endpoints with this
2260 * interval.
2261 */
2262 packet_size = largest_mps;
2263 overhead = interval_overhead;
2264 }
2265 /* Otherwise carry over packet_size and overhead from the last
2266 * time we had a remainder.
2267 */
2268 bw_used += bw_added;
2269 if (bw_used > max_bandwidth) {
2270 xhci_warn(xhci, "Not enough bandwidth. "
2271 "Proposed: %u, Max: %u\n",
2272 bw_used, max_bandwidth);
2273 return -ENOMEM;
2274 }
2275 }
2276 /*
2277 * Ok, we know we have some packets left over after even-handedly
2278 * scheduling interval 15. We don't know which microframes they will
2279 * fit into, so we over-schedule and say they will be scheduled every
2280 * microframe.
2281 */
2282 if (packets_remaining > 0)
2283 bw_used += overhead + packet_size;
2284
2285 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2286 unsigned int port_index = virt_dev->real_port - 1;
2287
2288 /* OK, we're manipulating a HS device attached to a
2289 * root port bandwidth domain. Include the number of active TTs
2290 * in the bandwidth used.
2291 */
2292 bw_used += TT_HS_OVERHEAD *
2293 xhci->rh_bw[port_index].num_active_tts;
2294 }
2295
2296 xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2297 "Available: %u " "percent\n",
2298 bw_used, max_bandwidth, bw_reserved,
2299 (max_bandwidth - bw_used - bw_reserved) * 100 /
2300 max_bandwidth);
2301
2302 bw_used += bw_reserved;
2303 if (bw_used > max_bandwidth) {
2304 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2305 bw_used, max_bandwidth);
2306 return -ENOMEM;
2307 }
2308
2309 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002310 return 0;
2311}
2312
2313static bool xhci_is_async_ep(unsigned int ep_type)
2314{
2315 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2316 ep_type != ISOC_IN_EP &&
2317 ep_type != INT_IN_EP);
2318}
2319
Sarah Sharp2b698992011-09-13 16:41:13 -07002320static bool xhci_is_sync_in_ep(unsigned int ep_type)
2321{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002322 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002323}
2324
2325static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2326{
2327 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2328
2329 if (ep_bw->ep_interval == 0)
2330 return SS_OVERHEAD_BURST +
2331 (ep_bw->mult * ep_bw->num_packets *
2332 (SS_OVERHEAD + mps));
2333 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2334 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2335 1 << ep_bw->ep_interval);
2336
2337}
2338
Sarah Sharp2e279802011-09-02 11:05:50 -07002339void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2340 struct xhci_bw_info *ep_bw,
2341 struct xhci_interval_bw_table *bw_table,
2342 struct usb_device *udev,
2343 struct xhci_virt_ep *virt_ep,
2344 struct xhci_tt_bw_info *tt_info)
2345{
2346 struct xhci_interval_bw *interval_bw;
2347 int normalized_interval;
2348
Sarah Sharp2b698992011-09-13 16:41:13 -07002349 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002350 return;
2351
Sarah Sharp2b698992011-09-13 16:41:13 -07002352 if (udev->speed == USB_SPEED_SUPER) {
2353 if (xhci_is_sync_in_ep(ep_bw->type))
2354 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2355 xhci_get_ss_bw_consumed(ep_bw);
2356 else
2357 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2358 xhci_get_ss_bw_consumed(ep_bw);
2359 return;
2360 }
2361
2362 /* SuperSpeed endpoints never get added to intervals in the table, so
2363 * this check is only valid for HS/FS/LS devices.
2364 */
2365 if (list_empty(&virt_ep->bw_endpoint_list))
2366 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002367 /* For LS/FS devices, we need to translate the interval expressed in
2368 * microframes to frames.
2369 */
2370 if (udev->speed == USB_SPEED_HIGH)
2371 normalized_interval = ep_bw->ep_interval;
2372 else
2373 normalized_interval = ep_bw->ep_interval - 3;
2374
2375 if (normalized_interval == 0)
2376 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2377 interval_bw = &bw_table->interval_bw[normalized_interval];
2378 interval_bw->num_packets -= ep_bw->num_packets;
2379 switch (udev->speed) {
2380 case USB_SPEED_LOW:
2381 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2382 break;
2383 case USB_SPEED_FULL:
2384 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2385 break;
2386 case USB_SPEED_HIGH:
2387 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2388 break;
2389 case USB_SPEED_SUPER:
2390 case USB_SPEED_UNKNOWN:
2391 case USB_SPEED_WIRELESS:
2392 /* Should never happen because only LS/FS/HS endpoints will get
2393 * added to the endpoint list.
2394 */
2395 return;
2396 }
2397 if (tt_info)
2398 tt_info->active_eps -= 1;
2399 list_del_init(&virt_ep->bw_endpoint_list);
2400}
2401
2402static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2403 struct xhci_bw_info *ep_bw,
2404 struct xhci_interval_bw_table *bw_table,
2405 struct usb_device *udev,
2406 struct xhci_virt_ep *virt_ep,
2407 struct xhci_tt_bw_info *tt_info)
2408{
2409 struct xhci_interval_bw *interval_bw;
2410 struct xhci_virt_ep *smaller_ep;
2411 int normalized_interval;
2412
2413 if (xhci_is_async_ep(ep_bw->type))
2414 return;
2415
Sarah Sharp2b698992011-09-13 16:41:13 -07002416 if (udev->speed == USB_SPEED_SUPER) {
2417 if (xhci_is_sync_in_ep(ep_bw->type))
2418 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2419 xhci_get_ss_bw_consumed(ep_bw);
2420 else
2421 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2422 xhci_get_ss_bw_consumed(ep_bw);
2423 return;
2424 }
2425
Sarah Sharp2e279802011-09-02 11:05:50 -07002426 /* For LS/FS devices, we need to translate the interval expressed in
2427 * microframes to frames.
2428 */
2429 if (udev->speed == USB_SPEED_HIGH)
2430 normalized_interval = ep_bw->ep_interval;
2431 else
2432 normalized_interval = ep_bw->ep_interval - 3;
2433
2434 if (normalized_interval == 0)
2435 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2436 interval_bw = &bw_table->interval_bw[normalized_interval];
2437 interval_bw->num_packets += ep_bw->num_packets;
2438 switch (udev->speed) {
2439 case USB_SPEED_LOW:
2440 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2441 break;
2442 case USB_SPEED_FULL:
2443 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2444 break;
2445 case USB_SPEED_HIGH:
2446 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2447 break;
2448 case USB_SPEED_SUPER:
2449 case USB_SPEED_UNKNOWN:
2450 case USB_SPEED_WIRELESS:
2451 /* Should never happen because only LS/FS/HS endpoints will get
2452 * added to the endpoint list.
2453 */
2454 return;
2455 }
2456
2457 if (tt_info)
2458 tt_info->active_eps += 1;
2459 /* Insert the endpoint into the list, largest max packet size first. */
2460 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2461 bw_endpoint_list) {
2462 if (ep_bw->max_packet_size >=
2463 smaller_ep->bw_info.max_packet_size) {
2464 /* Add the new ep before the smaller endpoint */
2465 list_add_tail(&virt_ep->bw_endpoint_list,
2466 &smaller_ep->bw_endpoint_list);
2467 return;
2468 }
2469 }
2470 /* Add the new endpoint at the end of the list. */
2471 list_add_tail(&virt_ep->bw_endpoint_list,
2472 &interval_bw->endpoints);
2473}
2474
2475void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2476 struct xhci_virt_device *virt_dev,
2477 int old_active_eps)
2478{
2479 struct xhci_root_port_bw_info *rh_bw_info;
2480 if (!virt_dev->tt_info)
2481 return;
2482
2483 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2484 if (old_active_eps == 0 &&
2485 virt_dev->tt_info->active_eps != 0) {
2486 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002487 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002488 } else if (old_active_eps != 0 &&
2489 virt_dev->tt_info->active_eps == 0) {
2490 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002491 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002492 }
2493}
2494
2495static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2496 struct xhci_virt_device *virt_dev,
2497 struct xhci_container_ctx *in_ctx)
2498{
2499 struct xhci_bw_info ep_bw_info[31];
2500 int i;
2501 struct xhci_input_control_ctx *ctrl_ctx;
2502 int old_active_eps = 0;
2503
Sarah Sharp2e279802011-09-02 11:05:50 -07002504 if (virt_dev->tt_info)
2505 old_active_eps = virt_dev->tt_info->active_eps;
2506
2507 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002508 if (!ctrl_ctx) {
2509 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2510 __func__);
2511 return -ENOMEM;
2512 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002513
2514 for (i = 0; i < 31; i++) {
2515 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2516 continue;
2517
2518 /* Make a copy of the BW info in case we need to revert this */
2519 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2520 sizeof(ep_bw_info[i]));
2521 /* Drop the endpoint from the interval table if the endpoint is
2522 * being dropped or changed.
2523 */
2524 if (EP_IS_DROPPED(ctrl_ctx, i))
2525 xhci_drop_ep_from_interval_table(xhci,
2526 &virt_dev->eps[i].bw_info,
2527 virt_dev->bw_table,
2528 virt_dev->udev,
2529 &virt_dev->eps[i],
2530 virt_dev->tt_info);
2531 }
2532 /* Overwrite the information stored in the endpoints' bw_info */
2533 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2534 for (i = 0; i < 31; i++) {
2535 /* Add any changed or added endpoints to the interval table */
2536 if (EP_IS_ADDED(ctrl_ctx, i))
2537 xhci_add_ep_to_interval_table(xhci,
2538 &virt_dev->eps[i].bw_info,
2539 virt_dev->bw_table,
2540 virt_dev->udev,
2541 &virt_dev->eps[i],
2542 virt_dev->tt_info);
2543 }
2544
2545 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2546 /* Ok, this fits in the bandwidth we have.
2547 * Update the number of active TTs.
2548 */
2549 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2550 return 0;
2551 }
2552
2553 /* We don't have enough bandwidth for this, revert the stored info. */
2554 for (i = 0; i < 31; i++) {
2555 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2556 continue;
2557
2558 /* Drop the new copies of any added or changed endpoints from
2559 * the interval table.
2560 */
2561 if (EP_IS_ADDED(ctrl_ctx, i)) {
2562 xhci_drop_ep_from_interval_table(xhci,
2563 &virt_dev->eps[i].bw_info,
2564 virt_dev->bw_table,
2565 virt_dev->udev,
2566 &virt_dev->eps[i],
2567 virt_dev->tt_info);
2568 }
2569 /* Revert the endpoint back to its old information */
2570 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2571 sizeof(ep_bw_info[i]));
2572 /* Add any changed or dropped endpoints back into the table */
2573 if (EP_IS_DROPPED(ctrl_ctx, i))
2574 xhci_add_ep_to_interval_table(xhci,
2575 &virt_dev->eps[i].bw_info,
2576 virt_dev->bw_table,
2577 virt_dev->udev,
2578 &virt_dev->eps[i],
2579 virt_dev->tt_info);
2580 }
2581 return -ENOMEM;
2582}
2583
2584
Sarah Sharpf2217e82009-08-07 14:04:43 -07002585/* Issue a configure endpoint command or evaluate context command
2586 * and wait for it to finish.
2587 */
2588static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002589 struct usb_device *udev,
2590 struct xhci_command *command,
2591 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002592{
2593 int ret;
2594 int timeleft;
2595 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002596 struct xhci_container_ctx *in_ctx;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002597 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002598 struct completion *cmd_completion;
Matt Evans28ccd292011-03-29 13:40:46 +11002599 u32 *cmd_status;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002600 struct xhci_virt_device *virt_dev;
Elric Fu6e4468b2012-06-27 16:31:52 +08002601 union xhci_trb *cmd_trb;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002602
2603 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002604 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002605
Sarah Sharp750645f2011-09-02 11:05:43 -07002606 if (command)
2607 in_ctx = command->in_ctx;
2608 else
2609 in_ctx = virt_dev->in_ctx;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002610 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2611 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002612 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002613 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2614 __func__);
2615 return -ENOMEM;
2616 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002617
2618 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002619 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002620 spin_unlock_irqrestore(&xhci->lock, flags);
2621 xhci_warn(xhci, "Not enough host resources, "
2622 "active endpoint contexts = %u\n",
2623 xhci->num_active_eps);
2624 return -ENOMEM;
2625 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002626 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2627 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2628 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002629 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002630 spin_unlock_irqrestore(&xhci->lock, flags);
2631 xhci_warn(xhci, "Not enough bandwidth\n");
2632 return -ENOMEM;
2633 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002634
2635 if (command) {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002636 cmd_completion = command->completion;
2637 cmd_status = &command->status;
2638 command->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002639
2640 /* Enqueue pointer can be left pointing to the link TRB,
2641 * we must handle that
2642 */
Matt Evansf5960b62011-06-01 10:22:55 +10002643 if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002644 command->command_trb =
2645 xhci->cmd_ring->enq_seg->next->trbs;
2646
Sarah Sharp913a8a32009-09-04 10:53:13 -07002647 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2648 } else {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002649 cmd_completion = &virt_dev->cmd_completion;
2650 cmd_status = &virt_dev->cmd_status;
2651 }
Andiry Xu1d680642010-03-12 17:10:04 +08002652 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002653
Elric Fu6e4468b2012-06-27 16:31:52 +08002654 cmd_trb = xhci->cmd_ring->dequeue;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002655 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07002656 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2657 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002658 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07002659 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002660 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002661 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08002662 if (command)
2663 list_del(&command->cmd_list);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002664 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002665 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002666 spin_unlock_irqrestore(&xhci->lock, flags);
2667 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
2668 return -ENOMEM;
2669 }
2670 xhci_ring_cmd_db(xhci);
2671 spin_unlock_irqrestore(&xhci->lock, flags);
2672
2673 /* Wait for the configure endpoint command to complete */
2674 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07002675 cmd_completion,
Elric Fu6e4468b2012-06-27 16:31:52 +08002676 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002677 if (timeleft <= 0) {
2678 xhci_warn(xhci, "%s while waiting for %s command\n",
2679 timeleft == 0 ? "Timeout" : "Signal",
2680 ctx_change == 0 ?
2681 "configure endpoint" :
2682 "evaluate context");
Elric Fu6e4468b2012-06-27 16:31:52 +08002683 /* cancel the configure endpoint command */
2684 ret = xhci_cancel_cmd(xhci, command, cmd_trb);
2685 if (ret < 0)
2686 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002687 return -ETIME;
2688 }
2689
2690 if (!ctx_change)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002691 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2692 else
2693 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
2694
2695 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2696 spin_lock_irqsave(&xhci->lock, flags);
2697 /* If the command failed, remove the reserved resources.
2698 * Otherwise, clean up the estimate to include dropped eps.
2699 */
2700 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002701 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002702 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002703 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002704 spin_unlock_irqrestore(&xhci->lock, flags);
2705 }
2706 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002707}
2708
Sarah Sharpf88ba782009-05-14 11:44:22 -07002709/* Called after one or more calls to xhci_add_endpoint() or
2710 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2711 * to call xhci_reset_bandwidth().
2712 *
2713 * Since we are in the middle of changing either configuration or
2714 * installing a new alt setting, the USB core won't allow URBs to be
2715 * enqueued for any endpoint on the old config or interface. Nothing
2716 * else should be touching the xhci->devs[slot_id] structure, so we
2717 * don't need to take the xhci->lock for manipulating that.
2718 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002719int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2720{
2721 int i;
2722 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002723 struct xhci_hcd *xhci;
2724 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002725 struct xhci_input_control_ctx *ctrl_ctx;
2726 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002727
Andiry Xu64927732010-10-14 07:22:45 -07002728 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002729 if (ret <= 0)
2730 return ret;
2731 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002732 if (xhci->xhc_state & XHCI_STATE_DYING)
2733 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002734
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002735 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002736 virt_dev = xhci->devs[udev->slot_id];
2737
2738 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07002739 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002740 if (!ctrl_ctx) {
2741 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2742 __func__);
2743 return -ENOMEM;
2744 }
Matt Evans28ccd292011-03-29 13:40:46 +11002745 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2746 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2747 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002748
2749 /* Don't issue the command if there's no endpoints to update. */
2750 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2751 ctrl_ctx->drop_flags == 0)
2752 return 0;
2753
Sarah Sharpf94e01862009-04-27 19:58:38 -07002754 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07002755 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2756 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002757 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002758
Sarah Sharp913a8a32009-09-04 10:53:13 -07002759 ret = xhci_configure_endpoint(xhci, udev, NULL,
2760 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002761 if (ret) {
2762 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002763 return ret;
2764 }
2765
2766 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07002767 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002768 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002769
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002770 /* Free any rings that were dropped, but not changed. */
2771 for (i = 1; i < 31; ++i) {
Matt Evans4819fef2011-06-01 13:01:07 +10002772 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2773 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002774 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2775 }
John Yound115b042009-07-27 12:05:15 -07002776 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002777 /*
2778 * Install any rings for completely new endpoints or changed endpoints,
2779 * and free or cache any old rings from changed endpoints.
2780 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002781 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002782 if (!virt_dev->eps[i].new_ring)
2783 continue;
2784 /* Only cache or free the old ring if it exists.
2785 * It may not if this is the first add of an endpoint.
2786 */
2787 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08002788 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002789 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002790 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2791 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002792 }
2793
Sarah Sharpf94e01862009-04-27 19:58:38 -07002794 return ret;
2795}
2796
2797void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2798{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002799 struct xhci_hcd *xhci;
2800 struct xhci_virt_device *virt_dev;
2801 int i, ret;
2802
Andiry Xu64927732010-10-14 07:22:45 -07002803 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002804 if (ret <= 0)
2805 return;
2806 xhci = hcd_to_xhci(hcd);
2807
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002808 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002809 virt_dev = xhci->devs[udev->slot_id];
2810 /* Free any rings allocated for added endpoints */
2811 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002812 if (virt_dev->eps[i].new_ring) {
2813 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2814 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002815 }
2816 }
John Yound115b042009-07-27 12:05:15 -07002817 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002818}
2819
Sarah Sharp5270b952009-09-04 10:53:11 -07002820static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002821 struct xhci_container_ctx *in_ctx,
2822 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002823 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002824 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002825{
Matt Evans28ccd292011-03-29 13:40:46 +11002826 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2827 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002828 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002829 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002830
Sarah Sharp913a8a32009-09-04 10:53:13 -07002831 xhci_dbg(xhci, "Input Context:\n");
2832 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07002833}
2834
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002835static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002836 unsigned int slot_id, unsigned int ep_index,
2837 struct xhci_dequeue_state *deq_state)
2838{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002839 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002840 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002841 struct xhci_ep_ctx *ep_ctx;
2842 u32 added_ctxs;
2843 dma_addr_t addr;
2844
Sarah Sharp92f8e762013-04-23 17:11:14 -07002845 in_ctx = xhci->devs[slot_id]->in_ctx;
2846 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2847 if (!ctrl_ctx) {
2848 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2849 __func__);
2850 return;
2851 }
2852
Sarah Sharp913a8a32009-09-04 10:53:13 -07002853 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2854 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002855 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2856 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2857 deq_state->new_deq_ptr);
2858 if (addr == 0) {
2859 xhci_warn(xhci, "WARN Cannot submit config ep after "
2860 "reset ep command\n");
2861 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2862 deq_state->new_deq_seg,
2863 deq_state->new_deq_ptr);
2864 return;
2865 }
Matt Evans28ccd292011-03-29 13:40:46 +11002866 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002867
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002868 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002869 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002870 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2871 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002872}
2873
Sarah Sharp82d10092009-08-07 14:04:52 -07002874void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002875 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07002876{
2877 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002878 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07002879
2880 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002881 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002882 /* We need to move the HW's dequeue pointer past this TD,
2883 * or it will attempt to resend it on the next doorbell ring.
2884 */
2885 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002886 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002887 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002888
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002889 /* HW with the reset endpoint quirk will use the saved dequeue state to
2890 * issue a configure endpoint command later.
2891 */
2892 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
2893 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002894 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002895 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002896 } else {
2897 /* Better hope no one uses the input context between now and the
2898 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002899 * XXX: No idea how this hardware will react when stream rings
2900 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002901 */
2902 xhci_dbg(xhci, "Setting up input context for "
2903 "configure endpoint command\n");
2904 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2905 ep_index, &deq_state);
2906 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002907}
2908
Sarah Sharpa1587d92009-07-27 12:03:15 -07002909/* Deal with stalled endpoints. The core should have sent the control message
2910 * to clear the halt condition. However, we need to make the xHCI hardware
2911 * reset its sequence number, since a device will expect a sequence number of
2912 * zero after the halt condition is cleared.
2913 * Context: in_interrupt
2914 */
2915void xhci_endpoint_reset(struct usb_hcd *hcd,
2916 struct usb_host_endpoint *ep)
2917{
2918 struct xhci_hcd *xhci;
2919 struct usb_device *udev;
2920 unsigned int ep_index;
2921 unsigned long flags;
2922 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002923 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002924
2925 xhci = hcd_to_xhci(hcd);
2926 udev = (struct usb_device *) ep->hcpriv;
2927 /* Called with a root hub endpoint (or an endpoint that wasn't added
2928 * with xhci_add_endpoint()
2929 */
2930 if (!ep->hcpriv)
2931 return;
2932 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002933 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2934 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002935 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
2936 ep->desc.bEndpointAddress);
2937 return;
2938 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002939 if (usb_endpoint_xfer_control(&ep->desc)) {
2940 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
2941 return;
2942 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07002943
2944 xhci_dbg(xhci, "Queueing reset endpoint command\n");
2945 spin_lock_irqsave(&xhci->lock, flags);
2946 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002947 /*
2948 * Can't change the ring dequeue pointer until it's transitioned to the
2949 * stopped state, which is only upon a successful reset endpoint
2950 * command. Better hope that last command worked!
2951 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002952 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002953 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
2954 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002955 xhci_ring_cmd_db(xhci);
2956 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07002957 virt_ep->stopped_td = NULL;
2958 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07002959 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002960 spin_unlock_irqrestore(&xhci->lock, flags);
2961
2962 if (ret)
2963 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
2964}
2965
Sarah Sharp8df75f42010-04-02 15:34:16 -07002966static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2967 struct usb_device *udev, struct usb_host_endpoint *ep,
2968 unsigned int slot_id)
2969{
2970 int ret;
2971 unsigned int ep_index;
2972 unsigned int ep_state;
2973
2974 if (!ep)
2975 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002976 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002977 if (ret <= 0)
2978 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04002979 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002980 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2981 " descriptor for ep 0x%x does not support streams\n",
2982 ep->desc.bEndpointAddress);
2983 return -EINVAL;
2984 }
2985
2986 ep_index = xhci_get_endpoint_index(&ep->desc);
2987 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2988 if (ep_state & EP_HAS_STREAMS ||
2989 ep_state & EP_GETTING_STREAMS) {
2990 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2991 "already has streams set up.\n",
2992 ep->desc.bEndpointAddress);
2993 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2994 "dynamic stream context array reallocation.\n");
2995 return -EINVAL;
2996 }
2997 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2998 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2999 "endpoint 0x%x; URBs are pending.\n",
3000 ep->desc.bEndpointAddress);
3001 return -EINVAL;
3002 }
3003 return 0;
3004}
3005
3006static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3007 unsigned int *num_streams, unsigned int *num_stream_ctxs)
3008{
3009 unsigned int max_streams;
3010
3011 /* The stream context array size must be a power of two */
3012 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3013 /*
3014 * Find out how many primary stream array entries the host controller
3015 * supports. Later we may use secondary stream arrays (similar to 2nd
3016 * level page entries), but that's an optional feature for xHCI host
3017 * controllers. xHCs must support at least 4 stream IDs.
3018 */
3019 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3020 if (*num_stream_ctxs > max_streams) {
3021 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3022 max_streams);
3023 *num_stream_ctxs = max_streams;
3024 *num_streams = max_streams;
3025 }
3026}
3027
3028/* Returns an error code if one of the endpoint already has streams.
3029 * This does not change any data structures, it only checks and gathers
3030 * information.
3031 */
3032static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3033 struct usb_device *udev,
3034 struct usb_host_endpoint **eps, unsigned int num_eps,
3035 unsigned int *num_streams, u32 *changed_ep_bitmask)
3036{
Sarah Sharp8df75f42010-04-02 15:34:16 -07003037 unsigned int max_streams;
3038 unsigned int endpoint_flag;
3039 int i;
3040 int ret;
3041
3042 for (i = 0; i < num_eps; i++) {
3043 ret = xhci_check_streams_endpoint(xhci, udev,
3044 eps[i], udev->slot_id);
3045 if (ret < 0)
3046 return ret;
3047
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003048 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003049 if (max_streams < (*num_streams - 1)) {
3050 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3051 eps[i]->desc.bEndpointAddress,
3052 max_streams);
3053 *num_streams = max_streams+1;
3054 }
3055
3056 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3057 if (*changed_ep_bitmask & endpoint_flag)
3058 return -EINVAL;
3059 *changed_ep_bitmask |= endpoint_flag;
3060 }
3061 return 0;
3062}
3063
3064static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3065 struct usb_device *udev,
3066 struct usb_host_endpoint **eps, unsigned int num_eps)
3067{
3068 u32 changed_ep_bitmask = 0;
3069 unsigned int slot_id;
3070 unsigned int ep_index;
3071 unsigned int ep_state;
3072 int i;
3073
3074 slot_id = udev->slot_id;
3075 if (!xhci->devs[slot_id])
3076 return 0;
3077
3078 for (i = 0; i < num_eps; i++) {
3079 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3080 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3081 /* Are streams already being freed for the endpoint? */
3082 if (ep_state & EP_GETTING_NO_STREAMS) {
3083 xhci_warn(xhci, "WARN Can't disable streams for "
3084 "endpoint 0x%x\n, "
3085 "streams are being disabled already.",
3086 eps[i]->desc.bEndpointAddress);
3087 return 0;
3088 }
3089 /* Are there actually any streams to free? */
3090 if (!(ep_state & EP_HAS_STREAMS) &&
3091 !(ep_state & EP_GETTING_STREAMS)) {
3092 xhci_warn(xhci, "WARN Can't disable streams for "
3093 "endpoint 0x%x\n, "
3094 "streams are already disabled!",
3095 eps[i]->desc.bEndpointAddress);
3096 xhci_warn(xhci, "WARN xhci_free_streams() called "
3097 "with non-streams endpoint\n");
3098 return 0;
3099 }
3100 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3101 }
3102 return changed_ep_bitmask;
3103}
3104
3105/*
3106 * The USB device drivers use this function (though the HCD interface in USB
3107 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3108 * coordinate mass storage command queueing across multiple endpoints (basically
3109 * a stream ID == a task ID).
3110 *
3111 * Setting up streams involves allocating the same size stream context array
3112 * for each endpoint and issuing a configure endpoint command for all endpoints.
3113 *
3114 * Don't allow the call to succeed if one endpoint only supports one stream
3115 * (which means it doesn't support streams at all).
3116 *
3117 * Drivers may get less stream IDs than they asked for, if the host controller
3118 * hardware or endpoints claim they can't support the number of requested
3119 * stream IDs.
3120 */
3121int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3122 struct usb_host_endpoint **eps, unsigned int num_eps,
3123 unsigned int num_streams, gfp_t mem_flags)
3124{
3125 int i, ret;
3126 struct xhci_hcd *xhci;
3127 struct xhci_virt_device *vdev;
3128 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003129 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003130 unsigned int ep_index;
3131 unsigned int num_stream_ctxs;
3132 unsigned long flags;
3133 u32 changed_ep_bitmask = 0;
3134
3135 if (!eps)
3136 return -EINVAL;
3137
3138 /* Add one to the number of streams requested to account for
3139 * stream 0 that is reserved for xHCI usage.
3140 */
3141 num_streams += 1;
3142 xhci = hcd_to_xhci(hcd);
3143 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3144 num_streams);
3145
3146 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3147 if (!config_cmd) {
3148 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3149 return -ENOMEM;
3150 }
Sarah Sharp92f8e762013-04-23 17:11:14 -07003151 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
3152 if (!ctrl_ctx) {
3153 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3154 __func__);
3155 xhci_free_command(xhci, config_cmd);
3156 return -ENOMEM;
3157 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003158
3159 /* Check to make sure all endpoints are not already configured for
3160 * streams. While we're at it, find the maximum number of streams that
3161 * all the endpoints will support and check for duplicate endpoints.
3162 */
3163 spin_lock_irqsave(&xhci->lock, flags);
3164 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3165 num_eps, &num_streams, &changed_ep_bitmask);
3166 if (ret < 0) {
3167 xhci_free_command(xhci, config_cmd);
3168 spin_unlock_irqrestore(&xhci->lock, flags);
3169 return ret;
3170 }
3171 if (num_streams <= 1) {
3172 xhci_warn(xhci, "WARN: endpoints can't handle "
3173 "more than one stream.\n");
3174 xhci_free_command(xhci, config_cmd);
3175 spin_unlock_irqrestore(&xhci->lock, flags);
3176 return -EINVAL;
3177 }
3178 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003179 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003180 * xhci_urb_enqueue() will reject all URBs.
3181 */
3182 for (i = 0; i < num_eps; i++) {
3183 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3184 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3185 }
3186 spin_unlock_irqrestore(&xhci->lock, flags);
3187
3188 /* Setup internal data structures and allocate HW data structures for
3189 * streams (but don't install the HW structures in the input context
3190 * until we're sure all memory allocation succeeded).
3191 */
3192 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3193 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3194 num_stream_ctxs, num_streams);
3195
3196 for (i = 0; i < num_eps; i++) {
3197 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3198 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3199 num_stream_ctxs,
3200 num_streams, mem_flags);
3201 if (!vdev->eps[ep_index].stream_info)
3202 goto cleanup;
3203 /* Set maxPstreams in endpoint context and update deq ptr to
3204 * point to stream context array. FIXME
3205 */
3206 }
3207
3208 /* Set up the input context for a configure endpoint command. */
3209 for (i = 0; i < num_eps; i++) {
3210 struct xhci_ep_ctx *ep_ctx;
3211
3212 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3213 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3214
3215 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3216 vdev->out_ctx, ep_index);
3217 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3218 vdev->eps[ep_index].stream_info);
3219 }
3220 /* Tell the HW to drop its old copy of the endpoint context info
3221 * and add the updated copy from the input context.
3222 */
3223 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003224 vdev->out_ctx, ctrl_ctx,
3225 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003226
3227 /* Issue and wait for the configure endpoint command */
3228 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3229 false, false);
3230
3231 /* xHC rejected the configure endpoint command for some reason, so we
3232 * leave the old ring intact and free our internal streams data
3233 * structure.
3234 */
3235 if (ret < 0)
3236 goto cleanup;
3237
3238 spin_lock_irqsave(&xhci->lock, flags);
3239 for (i = 0; i < num_eps; i++) {
3240 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3241 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3242 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3243 udev->slot_id, ep_index);
3244 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3245 }
3246 xhci_free_command(xhci, config_cmd);
3247 spin_unlock_irqrestore(&xhci->lock, flags);
3248
3249 /* Subtract 1 for stream 0, which drivers can't use */
3250 return num_streams - 1;
3251
3252cleanup:
3253 /* If it didn't work, free the streams! */
3254 for (i = 0; i < num_eps; i++) {
3255 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3256 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003257 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003258 /* FIXME Unset maxPstreams in endpoint context and
3259 * update deq ptr to point to normal string ring.
3260 */
3261 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3262 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3263 xhci_endpoint_zero(xhci, vdev, eps[i]);
3264 }
3265 xhci_free_command(xhci, config_cmd);
3266 return -ENOMEM;
3267}
3268
3269/* Transition the endpoint from using streams to being a "normal" endpoint
3270 * without streams.
3271 *
3272 * Modify the endpoint context state, submit a configure endpoint command,
3273 * and free all endpoint rings for streams if that completes successfully.
3274 */
3275int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3276 struct usb_host_endpoint **eps, unsigned int num_eps,
3277 gfp_t mem_flags)
3278{
3279 int i, ret;
3280 struct xhci_hcd *xhci;
3281 struct xhci_virt_device *vdev;
3282 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003283 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003284 unsigned int ep_index;
3285 unsigned long flags;
3286 u32 changed_ep_bitmask;
3287
3288 xhci = hcd_to_xhci(hcd);
3289 vdev = xhci->devs[udev->slot_id];
3290
3291 /* Set up a configure endpoint command to remove the streams rings */
3292 spin_lock_irqsave(&xhci->lock, flags);
3293 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3294 udev, eps, num_eps);
3295 if (changed_ep_bitmask == 0) {
3296 spin_unlock_irqrestore(&xhci->lock, flags);
3297 return -EINVAL;
3298 }
3299
3300 /* Use the xhci_command structure from the first endpoint. We may have
3301 * allocated too many, but the driver may call xhci_free_streams() for
3302 * each endpoint it grouped into one call to xhci_alloc_streams().
3303 */
3304 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3305 command = vdev->eps[ep_index].stream_info->free_streams_command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003306 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3307 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003308 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003309 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3310 __func__);
3311 return -EINVAL;
3312 }
3313
Sarah Sharp8df75f42010-04-02 15:34:16 -07003314 for (i = 0; i < num_eps; i++) {
3315 struct xhci_ep_ctx *ep_ctx;
3316
3317 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3318 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3319 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3320 EP_GETTING_NO_STREAMS;
3321
3322 xhci_endpoint_copy(xhci, command->in_ctx,
3323 vdev->out_ctx, ep_index);
3324 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3325 &vdev->eps[ep_index]);
3326 }
3327 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003328 vdev->out_ctx, ctrl_ctx,
3329 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003330 spin_unlock_irqrestore(&xhci->lock, flags);
3331
3332 /* Issue and wait for the configure endpoint command,
3333 * which must succeed.
3334 */
3335 ret = xhci_configure_endpoint(xhci, udev, command,
3336 false, true);
3337
3338 /* xHC rejected the configure endpoint command for some reason, so we
3339 * leave the streams rings intact.
3340 */
3341 if (ret < 0)
3342 return ret;
3343
3344 spin_lock_irqsave(&xhci->lock, flags);
3345 for (i = 0; i < num_eps; i++) {
3346 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3347 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003348 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003349 /* FIXME Unset maxPstreams in endpoint context and
3350 * update deq ptr to point to normal string ring.
3351 */
3352 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3353 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3354 }
3355 spin_unlock_irqrestore(&xhci->lock, flags);
3356
3357 return 0;
3358}
3359
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003360/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003361 * Deletes endpoint resources for endpoints that were active before a Reset
3362 * Device command, or a Disable Slot command. The Reset Device command leaves
3363 * the control endpoint intact, whereas the Disable Slot command deletes it.
3364 *
3365 * Must be called with xhci->lock held.
3366 */
3367void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3368 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3369{
3370 int i;
3371 unsigned int num_dropped_eps = 0;
3372 unsigned int drop_flags = 0;
3373
3374 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3375 if (virt_dev->eps[i].ring) {
3376 drop_flags |= 1 << i;
3377 num_dropped_eps++;
3378 }
3379 }
3380 xhci->num_active_eps -= num_dropped_eps;
3381 if (num_dropped_eps)
3382 xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
3383 "%u now active.\n",
3384 num_dropped_eps, drop_flags,
3385 xhci->num_active_eps);
3386}
3387
3388/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003389 * This submits a Reset Device Command, which will set the device state to 0,
3390 * set the device address to 0, and disable all the endpoints except the default
3391 * control endpoint. The USB core should come back and call
3392 * xhci_address_device(), and then re-set up the configuration. If this is
3393 * called because of a usb_reset_and_verify_device(), then the old alternate
3394 * settings will be re-installed through the normal bandwidth allocation
3395 * functions.
3396 *
3397 * Wait for the Reset Device command to finish. Remove all structures
3398 * associated with the endpoints that were disabled. Clear the input device
3399 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003400 *
3401 * If the virt_dev to be reset does not exist or does not match the udev,
3402 * it means the device is lost, possibly due to the xHC restore error and
3403 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3404 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003405 */
Andiry Xuf0615c42010-10-14 07:22:48 -07003406int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003407{
3408 int ret, i;
3409 unsigned long flags;
3410 struct xhci_hcd *xhci;
3411 unsigned int slot_id;
3412 struct xhci_virt_device *virt_dev;
3413 struct xhci_command *reset_device_cmd;
3414 int timeleft;
3415 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003416 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003417 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003418
Andiry Xuf0615c42010-10-14 07:22:48 -07003419 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003420 if (ret <= 0)
3421 return ret;
3422 xhci = hcd_to_xhci(hcd);
3423 slot_id = udev->slot_id;
3424 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003425 if (!virt_dev) {
3426 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3427 "not exist. Re-allocate the device\n", slot_id);
3428 ret = xhci_alloc_dev(hcd, udev);
3429 if (ret == 1)
3430 return 0;
3431 else
3432 return -EINVAL;
3433 }
3434
3435 if (virt_dev->udev != udev) {
3436 /* If the virt_dev and the udev does not match, this virt_dev
3437 * may belong to another udev.
3438 * Re-allocate the device.
3439 */
3440 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3441 "not match the udev. Re-allocate the device\n",
3442 slot_id);
3443 ret = xhci_alloc_dev(hcd, udev);
3444 if (ret == 1)
3445 return 0;
3446 else
3447 return -EINVAL;
3448 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003449
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003450 /* If device is not setup, there is no point in resetting it */
3451 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3452 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3453 SLOT_STATE_DISABLED)
3454 return 0;
3455
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003456 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3457 /* Allocate the command structure that holds the struct completion.
3458 * Assume we're in process context, since the normal device reset
3459 * process has to wait for the device anyway. Storage devices are
3460 * reset as part of error handling, so use GFP_NOIO instead of
3461 * GFP_KERNEL.
3462 */
3463 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3464 if (!reset_device_cmd) {
3465 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3466 return -ENOMEM;
3467 }
3468
3469 /* Attempt to submit the Reset Device command to the command ring */
3470 spin_lock_irqsave(&xhci->lock, flags);
3471 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003472
3473 /* Enqueue pointer can be left pointing to the link TRB,
3474 * we must handle that
3475 */
Matt Evansf5960b62011-06-01 10:22:55 +10003476 if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003477 reset_device_cmd->command_trb =
3478 xhci->cmd_ring->enq_seg->next->trbs;
3479
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003480 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3481 ret = xhci_queue_reset_device(xhci, slot_id);
3482 if (ret) {
3483 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3484 list_del(&reset_device_cmd->cmd_list);
3485 spin_unlock_irqrestore(&xhci->lock, flags);
3486 goto command_cleanup;
3487 }
3488 xhci_ring_cmd_db(xhci);
3489 spin_unlock_irqrestore(&xhci->lock, flags);
3490
3491 /* Wait for the Reset Device command to finish */
3492 timeleft = wait_for_completion_interruptible_timeout(
3493 reset_device_cmd->completion,
3494 USB_CTRL_SET_TIMEOUT);
3495 if (timeleft <= 0) {
3496 xhci_warn(xhci, "%s while waiting for reset device command\n",
3497 timeleft == 0 ? "Timeout" : "Signal");
3498 spin_lock_irqsave(&xhci->lock, flags);
3499 /* The timeout might have raced with the event ring handler, so
3500 * only delete from the list if the item isn't poisoned.
3501 */
3502 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3503 list_del(&reset_device_cmd->cmd_list);
3504 spin_unlock_irqrestore(&xhci->lock, flags);
3505 ret = -ETIME;
3506 goto command_cleanup;
3507 }
3508
3509 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3510 * unless we tried to reset a slot ID that wasn't enabled,
3511 * or the device wasn't in the addressed or configured state.
3512 */
3513 ret = reset_device_cmd->status;
3514 switch (ret) {
3515 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3516 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
3517 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
3518 slot_id,
3519 xhci_get_slot_state(xhci, virt_dev->out_ctx));
3520 xhci_info(xhci, "Not freeing device rings.\n");
3521 /* Don't treat this as an error. May change my mind later. */
3522 ret = 0;
3523 goto command_cleanup;
3524 case COMP_SUCCESS:
3525 xhci_dbg(xhci, "Successful reset device command.\n");
3526 break;
3527 default:
3528 if (xhci_is_vendor_info_code(xhci, ret))
3529 break;
3530 xhci_warn(xhci, "Unknown completion code %u for "
3531 "reset device command.\n", ret);
3532 ret = -EINVAL;
3533 goto command_cleanup;
3534 }
3535
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003536 /* Free up host controller endpoint resources */
3537 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3538 spin_lock_irqsave(&xhci->lock, flags);
3539 /* Don't delete the default control endpoint resources */
3540 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3541 spin_unlock_irqrestore(&xhci->lock, flags);
3542 }
3543
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003544 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3545 last_freed_endpoint = 1;
3546 for (i = 1; i < 31; ++i) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003547 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3548
3549 if (ep->ep_state & EP_HAS_STREAMS) {
3550 xhci_free_stream_info(xhci, ep->stream_info);
3551 ep->stream_info = NULL;
3552 ep->ep_state &= ~EP_HAS_STREAMS;
3553 }
3554
3555 if (ep->ring) {
3556 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3557 last_freed_endpoint = i;
3558 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003559 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3560 xhci_drop_ep_from_interval_table(xhci,
3561 &virt_dev->eps[i].bw_info,
3562 virt_dev->bw_table,
3563 udev,
3564 &virt_dev->eps[i],
3565 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003566 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003567 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003568 /* If necessary, update the number of active TTs on this root port */
3569 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3570
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003571 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3572 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3573 ret = 0;
3574
3575command_cleanup:
3576 xhci_free_command(xhci, reset_device_cmd);
3577 return ret;
3578}
3579
3580/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003581 * At this point, the struct usb_device is about to go away, the device has
3582 * disconnected, and all traffic has been stopped and the endpoints have been
3583 * disabled. Free any HC data structures associated with that device.
3584 */
3585void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3586{
3587 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003588 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003589 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003590 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07003591 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003592
Andiry Xu64927732010-10-14 07:22:45 -07003593 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003594 /* If the host is halted due to driver unload, we still need to free the
3595 * device.
3596 */
3597 if (ret <= 0 && ret != -ENODEV)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003598 return;
Andiry Xu64927732010-10-14 07:22:45 -07003599
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003600 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003601
3602 /* Stop any wayward timer functions (which may grab the lock) */
3603 for (i = 0; i < 31; ++i) {
3604 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3605 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3606 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003607
Andiry Xu65580b432011-09-23 14:19:52 -07003608 if (udev->usb2_hw_lpm_enabled) {
3609 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3610 udev->usb2_hw_lpm_enabled = 0;
3611 }
3612
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003613 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003614 /* Don't disable the slot if the host controller is dead. */
3615 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003616 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3617 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003618 xhci_free_virt_device(xhci, udev->slot_id);
3619 spin_unlock_irqrestore(&xhci->lock, flags);
3620 return;
3621 }
3622
Sarah Sharp23e3be12009-04-29 19:05:20 -07003623 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003624 spin_unlock_irqrestore(&xhci->lock, flags);
3625 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3626 return;
3627 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003628 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003629 spin_unlock_irqrestore(&xhci->lock, flags);
3630 /*
3631 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07003632 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003633 */
3634}
3635
3636/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003637 * Checks if we have enough host controller resources for the default control
3638 * endpoint.
3639 *
3640 * Must be called with xhci->lock held.
3641 */
3642static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3643{
3644 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3645 xhci_dbg(xhci, "Not enough ep ctxs: "
3646 "%u active, need to add 1, limit is %u.\n",
3647 xhci->num_active_eps, xhci->limit_active_eps);
3648 return -ENOMEM;
3649 }
3650 xhci->num_active_eps += 1;
3651 xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
3652 xhci->num_active_eps);
3653 return 0;
3654}
3655
3656
3657/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003658 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3659 * timed out, or allocating memory failed. Returns 1 on success.
3660 */
3661int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3662{
3663 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3664 unsigned long flags;
3665 int timeleft;
3666 int ret;
Elric Fu6e4468b2012-06-27 16:31:52 +08003667 union xhci_trb *cmd_trb;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003668
3669 spin_lock_irqsave(&xhci->lock, flags);
Elric Fu6e4468b2012-06-27 16:31:52 +08003670 cmd_trb = xhci->cmd_ring->dequeue;
Sarah Sharp23e3be12009-04-29 19:05:20 -07003671 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003672 if (ret) {
3673 spin_unlock_irqrestore(&xhci->lock, flags);
3674 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3675 return 0;
3676 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003677 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003678 spin_unlock_irqrestore(&xhci->lock, flags);
3679
3680 /* XXX: how much time for xHC slot assignment? */
3681 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
Elric Fu6e4468b2012-06-27 16:31:52 +08003682 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003683 if (timeleft <= 0) {
3684 xhci_warn(xhci, "%s while waiting for a slot\n",
3685 timeleft == 0 ? "Timeout" : "Signal");
Elric Fu6e4468b2012-06-27 16:31:52 +08003686 /* cancel the enable slot request */
3687 return xhci_cancel_cmd(xhci, NULL, cmd_trb);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003688 }
3689
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003690 if (!xhci->slot_id) {
3691 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003692 return 0;
3693 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003694
3695 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3696 spin_lock_irqsave(&xhci->lock, flags);
3697 ret = xhci_reserve_host_control_ep_resources(xhci);
3698 if (ret) {
3699 spin_unlock_irqrestore(&xhci->lock, flags);
3700 xhci_warn(xhci, "Not enough host resources, "
3701 "active endpoint contexts = %u\n",
3702 xhci->num_active_eps);
3703 goto disable_slot;
3704 }
3705 spin_unlock_irqrestore(&xhci->lock, flags);
3706 }
3707 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003708 * xhci_discover_or_reset_device(), which may be called as part of
3709 * mass storage driver error handling.
3710 */
3711 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003712 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003713 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003714 }
3715 udev->slot_id = xhci->slot_id;
3716 /* Is this a LS or FS device under a HS hub? */
3717 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003718 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003719
3720disable_slot:
3721 /* Disable slot, if we can do it without mem alloc */
3722 spin_lock_irqsave(&xhci->lock, flags);
3723 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3724 xhci_ring_cmd_db(xhci);
3725 spin_unlock_irqrestore(&xhci->lock, flags);
3726 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003727}
3728
3729/*
3730 * Issue an Address Device command (which will issue a SetAddress request to
3731 * the device).
3732 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3733 * we should only issue and wait on one address command at the same time.
3734 *
3735 * We add one to the device address issued by the hardware because the USB core
3736 * uses address 1 for the root hubs (even though they're not really devices).
3737 */
3738int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3739{
3740 unsigned long flags;
3741 int timeleft;
3742 struct xhci_virt_device *virt_dev;
3743 int ret = 0;
3744 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003745 struct xhci_slot_ctx *slot_ctx;
3746 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003747 u64 temp_64;
Elric Fu6e4468b2012-06-27 16:31:52 +08003748 union xhci_trb *cmd_trb;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003749
3750 if (!udev->slot_id) {
3751 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
3752 return -EINVAL;
3753 }
3754
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003755 virt_dev = xhci->devs[udev->slot_id];
3756
Matt Evans7ed603e2011-03-29 13:40:56 +11003757 if (WARN_ON(!virt_dev)) {
3758 /*
3759 * In plug/unplug torture test with an NEC controller,
3760 * a zero-dereference was observed once due to virt_dev = 0.
3761 * Print useful debug rather than crash if it is observed again!
3762 */
3763 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3764 udev->slot_id);
3765 return -EINVAL;
3766 }
3767
Andiry Xuf0615c42010-10-14 07:22:48 -07003768 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003769 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3770 if (!ctrl_ctx) {
3771 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3772 __func__);
3773 return -EINVAL;
3774 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003775 /*
3776 * If this is the first Set Address since device plug-in or
3777 * virt_device realloaction after a resume with an xHCI power loss,
3778 * then set up the slot context.
3779 */
3780 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003781 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003782 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003783 else
3784 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003785 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3786 ctrl_ctx->drop_flags = 0;
3787
Sarah Sharp66e49d82009-07-27 12:03:46 -07003788 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003789 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003790
Sarah Sharpf88ba782009-05-14 11:44:22 -07003791 spin_lock_irqsave(&xhci->lock, flags);
Elric Fu6e4468b2012-06-27 16:31:52 +08003792 cmd_trb = xhci->cmd_ring->dequeue;
John Yound115b042009-07-27 12:05:15 -07003793 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3794 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003795 if (ret) {
3796 spin_unlock_irqrestore(&xhci->lock, flags);
3797 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3798 return ret;
3799 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003800 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003801 spin_unlock_irqrestore(&xhci->lock, flags);
3802
3803 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3804 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
Elric Fu6e4468b2012-06-27 16:31:52 +08003805 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003806 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3807 * the SetAddress() "recovery interval" required by USB and aborting the
3808 * command on a timeout.
3809 */
3810 if (timeleft <= 0) {
Andiry Xucd681762011-09-23 14:19:55 -07003811 xhci_warn(xhci, "%s while waiting for address device command\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003812 timeleft == 0 ? "Timeout" : "Signal");
Elric Fu6e4468b2012-06-27 16:31:52 +08003813 /* cancel the address device command */
3814 ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
3815 if (ret < 0)
3816 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003817 return -ETIME;
3818 }
3819
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003820 switch (virt_dev->cmd_status) {
3821 case COMP_CTX_STATE:
3822 case COMP_EBADSLT:
3823 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3824 udev->slot_id);
3825 ret = -EINVAL;
3826 break;
3827 case COMP_TX_ERR:
3828 dev_warn(&udev->dev, "Device not responding to set address.\n");
3829 ret = -EPROTO;
3830 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08003831 case COMP_DEV_ERR:
3832 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3833 "device command.\n");
3834 ret = -ENODEV;
3835 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003836 case COMP_SUCCESS:
3837 xhci_dbg(xhci, "Successful Address Device command\n");
3838 break;
3839 default:
3840 xhci_err(xhci, "ERROR: unexpected command completion "
3841 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07003842 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003843 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003844 ret = -EINVAL;
3845 break;
3846 }
3847 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003848 return ret;
3849 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07003850 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
3851 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
3852 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Matt Evans28ccd292011-03-29 13:40:46 +11003853 udev->slot_id,
3854 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3855 (unsigned long long)
3856 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07003857 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07003858 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003859 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003860 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003861 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003862 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003863 /*
3864 * USB core uses address 1 for the roothubs, so we add one to the
3865 * address given back to us by the HC.
3866 */
John Yound115b042009-07-27 12:05:15 -07003867 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003868 /* Use kernel assigned address for devices; store xHC assigned
3869 * address locally. */
Matt Evans28ccd292011-03-29 13:40:46 +11003870 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3871 + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07003872 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003873 ctrl_ctx->add_flags = 0;
3874 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003875
Andiry Xuc8d4af82010-10-14 07:22:51 -07003876 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003877
3878 return 0;
3879}
3880
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003881/*
3882 * Transfer the port index into real index in the HW port status
3883 * registers. Caculate offset between the port's PORTSC register
3884 * and port status base. Divide the number of per port register
3885 * to get the real index. The raw port number bases 1.
3886 */
3887int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3888{
3889 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3890 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3891 __le32 __iomem *addr;
3892 int raw_port;
3893
3894 if (hcd->speed != HCD_USB3)
3895 addr = xhci->usb2_ports[port1 - 1];
3896 else
3897 addr = xhci->usb3_ports[port1 - 1];
3898
3899 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3900 return raw_port;
3901}
3902
Mathias Nymana558ccd2013-05-23 17:14:30 +03003903/*
3904 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3905 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3906 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07003907static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03003908 struct usb_device *udev, u16 max_exit_latency)
3909{
3910 struct xhci_virt_device *virt_dev;
3911 struct xhci_command *command;
3912 struct xhci_input_control_ctx *ctrl_ctx;
3913 struct xhci_slot_ctx *slot_ctx;
3914 unsigned long flags;
3915 int ret;
3916
3917 spin_lock_irqsave(&xhci->lock, flags);
3918 if (max_exit_latency == xhci->devs[udev->slot_id]->current_mel) {
3919 spin_unlock_irqrestore(&xhci->lock, flags);
3920 return 0;
3921 }
3922
3923 /* Attempt to issue an Evaluate Context command to change the MEL. */
3924 virt_dev = xhci->devs[udev->slot_id];
3925 command = xhci->lpm_command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003926 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3927 if (!ctrl_ctx) {
3928 spin_unlock_irqrestore(&xhci->lock, flags);
3929 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3930 __func__);
3931 return -ENOMEM;
3932 }
3933
Mathias Nymana558ccd2013-05-23 17:14:30 +03003934 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
3935 spin_unlock_irqrestore(&xhci->lock, flags);
3936
Mathias Nymana558ccd2013-05-23 17:14:30 +03003937 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
3938 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
3939 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
3940 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
3941
3942 xhci_dbg(xhci, "Set up evaluate context for LPM MEL change.\n");
3943 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
3944 xhci_dbg_ctx(xhci, command->in_ctx, 0);
3945
3946 /* Issue and wait for the evaluate context command. */
3947 ret = xhci_configure_endpoint(xhci, udev, command,
3948 true, true);
3949 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
3950 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
3951
3952 if (!ret) {
3953 spin_lock_irqsave(&xhci->lock, flags);
3954 virt_dev->current_mel = max_exit_latency;
3955 spin_unlock_irqrestore(&xhci->lock, flags);
3956 }
3957 return ret;
3958}
3959
Alan Stern84ebc102013-03-27 16:14:46 -04003960#ifdef CONFIG_PM_RUNTIME
Andiry Xu95743232011-09-23 14:19:51 -07003961
3962/* BESL to HIRD Encoding array for USB2 LPM */
3963static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3964 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3965
3966/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08003967static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
3968 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07003969{
Andiry Xuf99298b2011-12-12 16:45:28 +08003970 int u2del, besl, besl_host;
3971 int besl_device = 0;
3972 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07003973
Andiry Xuf99298b2011-12-12 16:45:28 +08003974 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3975 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
3976
3977 if (field & USB_BESL_SUPPORT) {
3978 for (besl_host = 0; besl_host < 16; besl_host++) {
3979 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07003980 break;
3981 }
Andiry Xuf99298b2011-12-12 16:45:28 +08003982 /* Use baseline BESL value as default */
3983 if (field & USB_BESL_BASELINE_VALID)
3984 besl_device = USB_GET_BESL_BASELINE(field);
3985 else if (field & USB_BESL_DEEP_VALID)
3986 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07003987 } else {
3988 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08003989 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07003990 else
Andiry Xuf99298b2011-12-12 16:45:28 +08003991 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07003992 }
3993
Andiry Xuf99298b2011-12-12 16:45:28 +08003994 besl = besl_host + besl_device;
3995 if (besl > 15)
3996 besl = 15;
3997
3998 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07003999}
4000
Mathias Nymana558ccd2013-05-23 17:14:30 +03004001/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4002static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4003{
4004 u32 field;
4005 int l1;
4006 int besld = 0;
4007 int hirdm = 0;
4008
4009 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4010
4011 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004012 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004013
4014 /* device has preferred BESLD */
4015 if (field & USB_BESL_DEEP_VALID) {
4016 besld = USB_GET_BESL_DEEP(field);
4017 hirdm = 1;
4018 }
4019
4020 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4021}
4022
Andiry Xu95743232011-09-23 14:19:51 -07004023static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
4024 struct usb_device *udev)
4025{
4026 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4027 struct dev_info *dev_info;
4028 __le32 __iomem **port_array;
4029 __le32 __iomem *addr, *pm_addr;
4030 u32 temp, dev_id;
4031 unsigned int port_num;
4032 unsigned long flags;
Andiry Xuf99298b2011-12-12 16:45:28 +08004033 int hird;
Andiry Xu95743232011-09-23 14:19:51 -07004034 int ret;
4035
4036 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
4037 !udev->lpm_capable)
4038 return -EINVAL;
4039
4040 /* we only support lpm for non-hub device connected to root hub yet */
4041 if (!udev->parent || udev->parent->parent ||
4042 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4043 return -EINVAL;
4044
4045 spin_lock_irqsave(&xhci->lock, flags);
4046
4047 /* Look for devices in lpm_failed_devs list */
4048 dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
4049 le16_to_cpu(udev->descriptor.idProduct);
4050 list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
4051 if (dev_info->dev_id == dev_id) {
4052 ret = -EINVAL;
4053 goto finish;
4054 }
4055 }
4056
4057 port_array = xhci->usb2_ports;
4058 port_num = udev->portnum - 1;
4059
4060 if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
4061 xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
4062 ret = -EINVAL;
4063 goto finish;
4064 }
4065
4066 /*
4067 * Test USB 2.0 software LPM.
4068 * FIXME: some xHCI 1.0 hosts may implement a new register to set up
4069 * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
4070 * in the June 2011 errata release.
4071 */
4072 xhci_dbg(xhci, "test port %d software LPM\n", port_num);
4073 /*
4074 * Set L1 Device Slot and HIRD/BESL.
4075 * Check device's USB 2.0 extension descriptor to determine whether
4076 * HIRD or BESL shoule be used. See USB2.0 LPM errata.
4077 */
Mathias Nymanb6e76372013-05-23 17:14:29 +03004078 pm_addr = port_array[port_num] + PORTPMSC;
Andiry Xuf99298b2011-12-12 16:45:28 +08004079 hird = xhci_calculate_hird_besl(xhci, udev);
Andiry Xu95743232011-09-23 14:19:51 -07004080 temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
4081 xhci_writel(xhci, temp, pm_addr);
4082
4083 /* Set port link state to U2(L1) */
4084 addr = port_array[port_num];
4085 xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
4086
4087 /* wait for ACK */
4088 spin_unlock_irqrestore(&xhci->lock, flags);
4089 msleep(10);
4090 spin_lock_irqsave(&xhci->lock, flags);
4091
4092 /* Check L1 Status */
Sarah Sharp2611bd12012-10-25 13:27:51 -07004093 ret = xhci_handshake(xhci, pm_addr,
4094 PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
Andiry Xu95743232011-09-23 14:19:51 -07004095 if (ret != -ETIMEDOUT) {
4096 /* enter L1 successfully */
4097 temp = xhci_readl(xhci, addr);
4098 xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
4099 port_num, temp);
4100 ret = 0;
4101 } else {
4102 temp = xhci_readl(xhci, pm_addr);
4103 xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
4104 port_num, temp & PORT_L1S_MASK);
4105 ret = -EINVAL;
4106 }
4107
4108 /* Resume the port */
4109 xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
4110
4111 spin_unlock_irqrestore(&xhci->lock, flags);
4112 msleep(10);
4113 spin_lock_irqsave(&xhci->lock, flags);
4114
4115 /* Clear PLC */
4116 xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
4117
4118 /* Check PORTSC to make sure the device is in the right state */
4119 if (!ret) {
4120 temp = xhci_readl(xhci, addr);
4121 xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
4122 if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
4123 (temp & PORT_PLS_MASK) != XDEV_U0) {
4124 xhci_dbg(xhci, "port L1 resume fail\n");
4125 ret = -EINVAL;
4126 }
4127 }
4128
4129 if (ret) {
4130 /* Insert dev to lpm_failed_devs list */
4131 xhci_warn(xhci, "device LPM test failed, may disconnect and "
4132 "re-enumerate\n");
4133 dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
4134 if (!dev_info) {
4135 ret = -ENOMEM;
4136 goto finish;
4137 }
4138 dev_info->dev_id = dev_id;
4139 INIT_LIST_HEAD(&dev_info->list);
4140 list_add(&dev_info->list, &xhci->lpm_failed_devs);
4141 } else {
4142 xhci_ring_device(xhci, udev->slot_id);
4143 }
4144
4145finish:
4146 spin_unlock_irqrestore(&xhci->lock, flags);
4147 return ret;
4148}
4149
Andiry Xu65580b432011-09-23 14:19:52 -07004150int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4151 struct usb_device *udev, int enable)
4152{
4153 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4154 __le32 __iomem **port_array;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004155 __le32 __iomem *pm_addr, *hlpm_addr;
4156 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004157 unsigned int port_num;
4158 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004159 int hird, exit_latency;
4160 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004161
4162 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
4163 !udev->lpm_capable)
4164 return -EPERM;
4165
4166 if (!udev->parent || udev->parent->parent ||
4167 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4168 return -EPERM;
4169
4170 if (udev->usb2_hw_lpm_capable != 1)
4171 return -EPERM;
4172
4173 spin_lock_irqsave(&xhci->lock, flags);
4174
4175 port_array = xhci->usb2_ports;
4176 port_num = udev->portnum - 1;
Mathias Nymanb6e76372013-05-23 17:14:29 +03004177 pm_addr = port_array[port_num] + PORTPMSC;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004178 pm_val = xhci_readl(xhci, pm_addr);
4179 hlpm_addr = port_array[port_num] + PORTHLPMC;
4180 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004181
4182 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4183 enable ? "enable" : "disable", port_num);
4184
Andiry Xu65580b432011-09-23 14:19:52 -07004185 if (enable) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004186 /* Host supports BESL timeout instead of HIRD */
4187 if (udev->usb2_hw_lpm_besl_capable) {
4188 /* if device doesn't have a preferred BESL value use a
4189 * default one which works with mixed HIRD and BESL
4190 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4191 */
4192 if ((field & USB_BESL_SUPPORT) &&
4193 (field & USB_BESL_BASELINE_VALID))
4194 hird = USB_GET_BESL_BASELINE(field);
4195 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004196 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004197
4198 exit_latency = xhci_besl_encoding[hird];
4199 spin_unlock_irqrestore(&xhci->lock, flags);
4200
4201 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4202 * input context for link powermanagement evaluate
4203 * context commands. It is protected by hcd->bandwidth
4204 * mutex and is shared by all devices. We need to set
4205 * the max ext latency in USB 2 BESL LPM as well, so
4206 * use the same mutex and xhci_change_max_exit_latency()
4207 */
4208 mutex_lock(hcd->bandwidth_mutex);
4209 ret = xhci_change_max_exit_latency(xhci, udev,
4210 exit_latency);
4211 mutex_unlock(hcd->bandwidth_mutex);
4212
4213 if (ret < 0)
4214 return ret;
4215 spin_lock_irqsave(&xhci->lock, flags);
4216
4217 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
4218 xhci_writel(xhci, hlpm_val, hlpm_addr);
4219 /* flush write */
4220 xhci_readl(xhci, hlpm_addr);
4221 } else {
4222 hird = xhci_calculate_hird_besl(xhci, udev);
4223 }
4224
4225 pm_val &= ~PORT_HIRD_MASK;
4226 pm_val |= PORT_HIRD(hird) | PORT_RWE;
4227 xhci_writel(xhci, pm_val, pm_addr);
4228 pm_val = xhci_readl(xhci, pm_addr);
4229 pm_val |= PORT_HLE;
4230 xhci_writel(xhci, pm_val, pm_addr);
4231 /* flush write */
4232 xhci_readl(xhci, pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004233 } else {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004234 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
4235 xhci_writel(xhci, pm_val, pm_addr);
4236 /* flush write */
4237 xhci_readl(xhci, pm_addr);
4238 if (udev->usb2_hw_lpm_besl_capable) {
4239 spin_unlock_irqrestore(&xhci->lock, flags);
4240 mutex_lock(hcd->bandwidth_mutex);
4241 xhci_change_max_exit_latency(xhci, udev, 0);
4242 mutex_unlock(hcd->bandwidth_mutex);
4243 return 0;
4244 }
Andiry Xu65580b432011-09-23 14:19:52 -07004245 }
4246
4247 spin_unlock_irqrestore(&xhci->lock, flags);
4248 return 0;
4249}
4250
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004251/* check if a usb2 port supports a given extened capability protocol
4252 * only USB2 ports extended protocol capability values are cached.
4253 * Return 1 if capability is supported
4254 */
4255static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4256 unsigned capability)
4257{
4258 u32 port_offset, port_count;
4259 int i;
4260
4261 for (i = 0; i < xhci->num_ext_caps; i++) {
4262 if (xhci->ext_caps[i] & capability) {
4263 /* port offsets starts at 1 */
4264 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4265 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4266 if (port >= port_offset &&
4267 port < port_offset + port_count)
4268 return 1;
4269 }
4270 }
4271 return 0;
4272}
4273
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004274int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4275{
4276 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4277 int ret;
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004278 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004279
4280 ret = xhci_usb2_software_lpm_test(hcd, udev);
4281 if (!ret) {
4282 xhci_dbg(xhci, "software LPM test succeed\n");
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004283 if (xhci->hw_lpm_support == 1 &&
4284 xhci_check_usb2_port_capability(xhci, portnum, XHCI_HLC)) {
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004285 udev->usb2_hw_lpm_capable = 1;
Mathias Nyman17f34862013-05-23 17:14:31 +03004286 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4287 udev->l1_params.besl = XHCI_DEFAULT_BESL;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004288 if (xhci_check_usb2_port_capability(xhci, portnum,
4289 XHCI_BLC))
4290 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004291 ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
4292 if (!ret)
4293 udev->usb2_hw_lpm_enabled = 1;
4294 }
4295 }
4296
4297 return 0;
4298}
4299
4300#else
4301
4302int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4303 struct usb_device *udev, int enable)
4304{
4305 return 0;
4306}
4307
4308int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4309{
4310 return 0;
4311}
4312
Alan Stern84ebc102013-03-27 16:14:46 -04004313#endif /* CONFIG_PM_RUNTIME */
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004314
Sarah Sharp3b3db022012-05-09 10:55:03 -07004315/*---------------------- USB 3.0 Link PM functions ------------------------*/
4316
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004317#ifdef CONFIG_PM
Sarah Sharpe3567d22012-05-16 13:36:24 -07004318/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4319static unsigned long long xhci_service_interval_to_ns(
4320 struct usb_endpoint_descriptor *desc)
4321{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004322 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004323}
4324
Sarah Sharp3b3db022012-05-09 10:55:03 -07004325static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4326 enum usb3_link_state state)
4327{
4328 unsigned long long sel;
4329 unsigned long long pel;
4330 unsigned int max_sel_pel;
4331 char *state_name;
4332
4333 switch (state) {
4334 case USB3_LPM_U1:
4335 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4336 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4337 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4338 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4339 state_name = "U1";
4340 break;
4341 case USB3_LPM_U2:
4342 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4343 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4344 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4345 state_name = "U2";
4346 break;
4347 default:
4348 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4349 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004350 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004351 }
4352
4353 if (sel <= max_sel_pel && pel <= max_sel_pel)
4354 return USB3_LPM_DEVICE_INITIATED;
4355
4356 if (sel > max_sel_pel)
4357 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4358 "due to long SEL %llu ms\n",
4359 state_name, sel);
4360 else
4361 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4362 "due to long PEL %llu\n ms",
4363 state_name, pel);
4364 return USB3_LPM_DISABLED;
4365}
4366
Sarah Sharpe3567d22012-05-16 13:36:24 -07004367/* Returns the hub-encoded U1 timeout value.
4368 * The U1 timeout should be the maximum of the following values:
4369 * - For control endpoints, U1 system exit latency (SEL) * 3
4370 * - For bulk endpoints, U1 SEL * 5
4371 * - For interrupt endpoints:
4372 * - Notification EPs, U1 SEL * 3
4373 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4374 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4375 */
4376static u16 xhci_calculate_intel_u1_timeout(struct usb_device *udev,
4377 struct usb_endpoint_descriptor *desc)
4378{
4379 unsigned long long timeout_ns;
4380 int ep_type;
4381 int intr_type;
4382
4383 ep_type = usb_endpoint_type(desc);
4384 switch (ep_type) {
4385 case USB_ENDPOINT_XFER_CONTROL:
4386 timeout_ns = udev->u1_params.sel * 3;
4387 break;
4388 case USB_ENDPOINT_XFER_BULK:
4389 timeout_ns = udev->u1_params.sel * 5;
4390 break;
4391 case USB_ENDPOINT_XFER_INT:
4392 intr_type = usb_endpoint_interrupt_type(desc);
4393 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4394 timeout_ns = udev->u1_params.sel * 3;
4395 break;
4396 }
4397 /* Otherwise the calculation is the same as isoc eps */
4398 case USB_ENDPOINT_XFER_ISOC:
4399 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004400 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004401 if (timeout_ns < udev->u1_params.sel * 2)
4402 timeout_ns = udev->u1_params.sel * 2;
4403 break;
4404 default:
4405 return 0;
4406 }
4407
4408 /* The U1 timeout is encoded in 1us intervals. */
Sarah Sharpc88db162012-05-21 08:44:33 -07004409 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004410 /* Don't return a timeout of zero, because that's USB3_LPM_DISABLED. */
4411 if (timeout_ns == USB3_LPM_DISABLED)
4412 timeout_ns++;
4413
4414 /* If the necessary timeout value is bigger than what we can set in the
4415 * USB 3.0 hub, we have to disable hub-initiated U1.
4416 */
4417 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4418 return timeout_ns;
4419 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4420 "due to long timeout %llu ms\n", timeout_ns);
4421 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4422}
4423
4424/* Returns the hub-encoded U2 timeout value.
4425 * The U2 timeout should be the maximum of:
4426 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4427 * - largest bInterval of any active periodic endpoint (to avoid going
4428 * into lower power link states between intervals).
4429 * - the U2 Exit Latency of the device
4430 */
4431static u16 xhci_calculate_intel_u2_timeout(struct usb_device *udev,
4432 struct usb_endpoint_descriptor *desc)
4433{
4434 unsigned long long timeout_ns;
4435 unsigned long long u2_del_ns;
4436
4437 timeout_ns = 10 * 1000 * 1000;
4438
4439 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4440 (xhci_service_interval_to_ns(desc) > timeout_ns))
4441 timeout_ns = xhci_service_interval_to_ns(desc);
4442
Oliver Neukum966e7a82012-10-17 12:17:50 +02004443 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004444 if (u2_del_ns > timeout_ns)
4445 timeout_ns = u2_del_ns;
4446
4447 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004448 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004449 /* If the necessary timeout value is bigger than what we can set in the
4450 * USB 3.0 hub, we have to disable hub-initiated U2.
4451 */
4452 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4453 return timeout_ns;
4454 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4455 "due to long timeout %llu ms\n", timeout_ns);
4456 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4457}
4458
Sarah Sharp3b3db022012-05-09 10:55:03 -07004459static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4460 struct usb_device *udev,
4461 struct usb_endpoint_descriptor *desc,
4462 enum usb3_link_state state,
4463 u16 *timeout)
4464{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004465 if (state == USB3_LPM_U1) {
4466 if (xhci->quirks & XHCI_INTEL_HOST)
4467 return xhci_calculate_intel_u1_timeout(udev, desc);
4468 } else {
4469 if (xhci->quirks & XHCI_INTEL_HOST)
4470 return xhci_calculate_intel_u2_timeout(udev, desc);
4471 }
4472
Sarah Sharp3b3db022012-05-09 10:55:03 -07004473 return USB3_LPM_DISABLED;
4474}
4475
4476static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4477 struct usb_device *udev,
4478 struct usb_endpoint_descriptor *desc,
4479 enum usb3_link_state state,
4480 u16 *timeout)
4481{
4482 u16 alt_timeout;
4483
4484 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4485 desc, state, timeout);
4486
4487 /* If we found we can't enable hub-initiated LPM, or
4488 * the U1 or U2 exit latency was too high to allow
4489 * device-initiated LPM as well, just stop searching.
4490 */
4491 if (alt_timeout == USB3_LPM_DISABLED ||
4492 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4493 *timeout = alt_timeout;
4494 return -E2BIG;
4495 }
4496 if (alt_timeout > *timeout)
4497 *timeout = alt_timeout;
4498 return 0;
4499}
4500
4501static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4502 struct usb_device *udev,
4503 struct usb_host_interface *alt,
4504 enum usb3_link_state state,
4505 u16 *timeout)
4506{
4507 int j;
4508
4509 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4510 if (xhci_update_timeout_for_endpoint(xhci, udev,
4511 &alt->endpoint[j].desc, state, timeout))
4512 return -E2BIG;
4513 continue;
4514 }
4515 return 0;
4516}
4517
Sarah Sharpe3567d22012-05-16 13:36:24 -07004518static int xhci_check_intel_tier_policy(struct usb_device *udev,
4519 enum usb3_link_state state)
4520{
4521 struct usb_device *parent;
4522 unsigned int num_hubs;
4523
4524 if (state == USB3_LPM_U2)
4525 return 0;
4526
4527 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4528 for (parent = udev->parent, num_hubs = 0; parent->parent;
4529 parent = parent->parent)
4530 num_hubs++;
4531
4532 if (num_hubs < 2)
4533 return 0;
4534
4535 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4536 " below second-tier hub.\n");
4537 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4538 "to decrease power consumption.\n");
4539 return -E2BIG;
4540}
4541
Sarah Sharp3b3db022012-05-09 10:55:03 -07004542static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4543 struct usb_device *udev,
4544 enum usb3_link_state state)
4545{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004546 if (xhci->quirks & XHCI_INTEL_HOST)
4547 return xhci_check_intel_tier_policy(udev, state);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004548 return -EINVAL;
4549}
4550
4551/* Returns the U1 or U2 timeout that should be enabled.
4552 * If the tier check or timeout setting functions return with a non-zero exit
4553 * code, that means the timeout value has been finalized and we shouldn't look
4554 * at any more endpoints.
4555 */
4556static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4557 struct usb_device *udev, enum usb3_link_state state)
4558{
4559 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4560 struct usb_host_config *config;
4561 char *state_name;
4562 int i;
4563 u16 timeout = USB3_LPM_DISABLED;
4564
4565 if (state == USB3_LPM_U1)
4566 state_name = "U1";
4567 else if (state == USB3_LPM_U2)
4568 state_name = "U2";
4569 else {
4570 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4571 state);
4572 return timeout;
4573 }
4574
4575 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4576 return timeout;
4577
4578 /* Gather some information about the currently installed configuration
4579 * and alternate interface settings.
4580 */
4581 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4582 state, &timeout))
4583 return timeout;
4584
4585 config = udev->actconfig;
4586 if (!config)
4587 return timeout;
4588
4589 for (i = 0; i < USB_MAXINTERFACES; i++) {
4590 struct usb_driver *driver;
4591 struct usb_interface *intf = config->interface[i];
4592
4593 if (!intf)
4594 continue;
4595
4596 /* Check if any currently bound drivers want hub-initiated LPM
4597 * disabled.
4598 */
4599 if (intf->dev.driver) {
4600 driver = to_usb_driver(intf->dev.driver);
4601 if (driver && driver->disable_hub_initiated_lpm) {
4602 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4603 "at request of driver %s\n",
4604 state_name, driver->name);
4605 return xhci_get_timeout_no_hub_lpm(udev, state);
4606 }
4607 }
4608
4609 /* Not sure how this could happen... */
4610 if (!intf->cur_altsetting)
4611 continue;
4612
4613 if (xhci_update_timeout_for_interface(xhci, udev,
4614 intf->cur_altsetting,
4615 state, &timeout))
4616 return timeout;
4617 }
4618 return timeout;
4619}
4620
Sarah Sharp3b3db022012-05-09 10:55:03 -07004621static int calculate_max_exit_latency(struct usb_device *udev,
4622 enum usb3_link_state state_changed,
4623 u16 hub_encoded_timeout)
4624{
4625 unsigned long long u1_mel_us = 0;
4626 unsigned long long u2_mel_us = 0;
4627 unsigned long long mel_us = 0;
4628 bool disabling_u1;
4629 bool disabling_u2;
4630 bool enabling_u1;
4631 bool enabling_u2;
4632
4633 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4634 hub_encoded_timeout == USB3_LPM_DISABLED);
4635 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4636 hub_encoded_timeout == USB3_LPM_DISABLED);
4637
4638 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4639 hub_encoded_timeout != USB3_LPM_DISABLED);
4640 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4641 hub_encoded_timeout != USB3_LPM_DISABLED);
4642
4643 /* If U1 was already enabled and we're not disabling it,
4644 * or we're going to enable U1, account for the U1 max exit latency.
4645 */
4646 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4647 enabling_u1)
4648 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4649 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4650 enabling_u2)
4651 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4652
4653 if (u1_mel_us > u2_mel_us)
4654 mel_us = u1_mel_us;
4655 else
4656 mel_us = u2_mel_us;
4657 /* xHCI host controller max exit latency field is only 16 bits wide. */
4658 if (mel_us > MAX_EXIT) {
4659 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4660 "is too big.\n", mel_us);
4661 return -E2BIG;
4662 }
4663 return mel_us;
4664}
4665
4666/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4667int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4668 struct usb_device *udev, enum usb3_link_state state)
4669{
4670 struct xhci_hcd *xhci;
4671 u16 hub_encoded_timeout;
4672 int mel;
4673 int ret;
4674
4675 xhci = hcd_to_xhci(hcd);
4676 /* The LPM timeout values are pretty host-controller specific, so don't
4677 * enable hub-initiated timeouts unless the vendor has provided
4678 * information about their timeout algorithm.
4679 */
4680 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4681 !xhci->devs[udev->slot_id])
4682 return USB3_LPM_DISABLED;
4683
4684 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4685 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4686 if (mel < 0) {
4687 /* Max Exit Latency is too big, disable LPM. */
4688 hub_encoded_timeout = USB3_LPM_DISABLED;
4689 mel = 0;
4690 }
4691
4692 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4693 if (ret)
4694 return ret;
4695 return hub_encoded_timeout;
4696}
4697
4698int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4699 struct usb_device *udev, enum usb3_link_state state)
4700{
4701 struct xhci_hcd *xhci;
4702 u16 mel;
4703 int ret;
4704
4705 xhci = hcd_to_xhci(hcd);
4706 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4707 !xhci->devs[udev->slot_id])
4708 return 0;
4709
4710 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
4711 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4712 if (ret)
4713 return ret;
4714 return 0;
4715}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004716#else /* CONFIG_PM */
4717
4718int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4719 struct usb_device *udev, enum usb3_link_state state)
4720{
4721 return USB3_LPM_DISABLED;
4722}
4723
4724int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4725 struct usb_device *udev, enum usb3_link_state state)
4726{
4727 return 0;
4728}
4729#endif /* CONFIG_PM */
4730
Sarah Sharp3b3db022012-05-09 10:55:03 -07004731/*-------------------------------------------------------------------------*/
4732
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004733/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4734 * internal data structures for the device.
4735 */
4736int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4737 struct usb_tt *tt, gfp_t mem_flags)
4738{
4739 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4740 struct xhci_virt_device *vdev;
4741 struct xhci_command *config_cmd;
4742 struct xhci_input_control_ctx *ctrl_ctx;
4743 struct xhci_slot_ctx *slot_ctx;
4744 unsigned long flags;
4745 unsigned think_time;
4746 int ret;
4747
4748 /* Ignore root hubs */
4749 if (!hdev->parent)
4750 return 0;
4751
4752 vdev = xhci->devs[hdev->slot_id];
4753 if (!vdev) {
4754 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4755 return -EINVAL;
4756 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004757 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004758 if (!config_cmd) {
4759 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4760 return -ENOMEM;
4761 }
Sarah Sharp92f8e762013-04-23 17:11:14 -07004762 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
4763 if (!ctrl_ctx) {
4764 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4765 __func__);
4766 xhci_free_command(xhci, config_cmd);
4767 return -ENOMEM;
4768 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004769
4770 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004771 if (hdev->speed == USB_SPEED_HIGH &&
4772 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4773 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4774 xhci_free_command(xhci, config_cmd);
4775 spin_unlock_irqrestore(&xhci->lock, flags);
4776 return -ENOMEM;
4777 }
4778
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004779 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004780 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004781 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004782 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004783 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004784 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004785 if (xhci->hci_version > 0x95) {
4786 xhci_dbg(xhci, "xHCI version %x needs hub "
4787 "TT think time and number of ports\n",
4788 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004789 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004790 /* Set TT think time - convert from ns to FS bit times.
4791 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4792 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004793 *
4794 * xHCI 1.0: this field shall be 0 if the device is not a
4795 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004796 */
4797 think_time = tt->think_time;
4798 if (think_time != 0)
4799 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004800 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4801 slot_ctx->tt_info |=
4802 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004803 } else {
4804 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4805 "TT think time or number of ports\n",
4806 (unsigned int) xhci->hci_version);
4807 }
4808 slot_ctx->dev_state = 0;
4809 spin_unlock_irqrestore(&xhci->lock, flags);
4810
4811 xhci_dbg(xhci, "Set up %s for hub device.\n",
4812 (xhci->hci_version > 0x95) ?
4813 "configure endpoint" : "evaluate context");
4814 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4815 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4816
4817 /* Issue and wait for the configure endpoint or
4818 * evaluate context command.
4819 */
4820 if (xhci->hci_version > 0x95)
4821 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4822 false, false);
4823 else
4824 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4825 true, false);
4826
4827 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4828 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4829
4830 xhci_free_command(xhci, config_cmd);
4831 return ret;
4832}
4833
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004834int xhci_get_frame(struct usb_hcd *hcd)
4835{
4836 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4837 /* EHCI mods by the periodic size. Why? */
4838 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4839}
4840
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004841int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4842{
4843 struct xhci_hcd *xhci;
4844 struct device *dev = hcd->self.controller;
4845 int retval;
4846 u32 temp;
4847
Andiry Xufdaf8b32012-03-05 17:49:38 +08004848 /* Accept arbitrarily long scatter-gather lists */
4849 hcd->self.sg_tablesize = ~0;
Hans de Goede19181bc2012-07-04 09:18:02 +02004850 /* XHCI controllers don't stop the ep queue on short packets :| */
4851 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004852
4853 if (usb_hcd_is_primary_hcd(hcd)) {
4854 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4855 if (!xhci)
4856 return -ENOMEM;
4857 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4858 xhci->main_hcd = hcd;
4859 /* Mark the first roothub as being USB 2.0.
4860 * The xHCI driver will register the USB 3.0 roothub.
4861 */
4862 hcd->speed = HCD_USB2;
4863 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4864 /*
4865 * USB 2.0 roothub under xHCI has an integrated TT,
4866 * (rate matching hub) as opposed to having an OHCI/UHCI
4867 * companion controller.
4868 */
4869 hcd->has_tt = 1;
4870 } else {
4871 /* xHCI private pointer was set in xhci_pci_probe for the second
4872 * registered roothub.
4873 */
4874 xhci = hcd_to_xhci(hcd);
4875 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4876 if (HCC_64BIT_ADDR(temp)) {
4877 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4878 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4879 } else {
4880 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4881 }
4882 return 0;
4883 }
4884
4885 xhci->cap_regs = hcd->regs;
4886 xhci->op_regs = hcd->regs +
4887 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4888 xhci->run_regs = hcd->regs +
4889 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4890 /* Cache read-only capability registers */
4891 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4892 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4893 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4894 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4895 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4896 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4897 xhci_print_registers(xhci);
4898
4899 get_quirks(dev, xhci);
4900
George Cherian07f3cb72013-07-01 10:59:12 +05304901 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4902 * success event after a short transfer. This quirk will ignore such
4903 * spurious event.
4904 */
4905 if (xhci->hci_version > 0x96)
4906 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4907
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004908 /* Make sure the HC is halted. */
4909 retval = xhci_halt(xhci);
4910 if (retval)
4911 goto error;
4912
4913 xhci_dbg(xhci, "Resetting HCD\n");
4914 /* Reset the internal HC memory state and registers. */
4915 retval = xhci_reset(xhci);
4916 if (retval)
4917 goto error;
4918 xhci_dbg(xhci, "Reset complete\n");
4919
4920 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4921 if (HCC_64BIT_ADDR(temp)) {
4922 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4923 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4924 } else {
4925 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4926 }
4927
4928 xhci_dbg(xhci, "Calling HCD init\n");
4929 /* Initialize HCD and host controller data structures. */
4930 retval = xhci_init(hcd);
4931 if (retval)
4932 goto error;
4933 xhci_dbg(xhci, "Called HCD init\n");
4934 return 0;
4935error:
4936 kfree(xhci);
4937 return retval;
4938}
4939
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004940MODULE_DESCRIPTION(DRIVER_DESC);
4941MODULE_AUTHOR(DRIVER_AUTHOR);
4942MODULE_LICENSE("GPL");
4943
4944static int __init xhci_hcd_init(void)
4945{
Sebastian Andrzej Siewior0cc47d52011-09-23 14:20:02 -07004946 int retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004947
4948 retval = xhci_register_pci();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004949 if (retval < 0) {
4950 printk(KERN_DEBUG "Problem registering PCI driver.");
4951 return retval;
4952 }
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004953 retval = xhci_register_plat();
4954 if (retval < 0) {
4955 printk(KERN_DEBUG "Problem registering platform driver.");
4956 goto unreg_pci;
4957 }
Sarah Sharp98441972009-05-14 11:44:18 -07004958 /*
4959 * Check the compiler generated sizes of structures that must be laid
4960 * out in specific ways for hardware access.
4961 */
4962 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4963 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4964 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4965 /* xhci_device_control has eight fields, and also
4966 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4967 */
Sarah Sharp98441972009-05-14 11:44:18 -07004968 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4969 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4970 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4971 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4972 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4973 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4974 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004975 return 0;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004976unreg_pci:
4977 xhci_unregister_pci();
4978 return retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004979}
4980module_init(xhci_hcd_init);
4981
4982static void __exit xhci_hcd_cleanup(void)
4983{
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004984 xhci_unregister_pci();
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004985 xhci_unregister_plat();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004986}
4987module_exit(xhci_hcd_cleanup);