blob: 13320570ae8b53f201ff9d22c56d9377e2fc784b [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"
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +030033#include "xhci-trace.h"
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020034#include "xhci-mtk.h"
Sarah Sharp66d4ead2009-04-27 19:52:28 -070035
36#define DRIVER_AUTHOR "Sarah Sharp"
37#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
38
Lu Baolua1377e52014-11-18 11:27:14 +020039#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
40
Sarah Sharpb0567b32009-08-07 14:04:36 -070041/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
42static int link_quirk;
43module_param(link_quirk, int, S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
45
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +010046static unsigned int quirks;
47module_param(quirks, uint, S_IRUGO);
48MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");
49
Sarah Sharp66d4ead2009-04-27 19:52:28 -070050/* TODO: copied from ehci-hcd.c - can this be refactored? */
51/*
Sarah Sharp2611bd182012-10-25 13:27:51 -070052 * xhci_handshake - spin reading hc until handshake completes or fails
Sarah Sharp66d4ead2009-04-27 19:52:28 -070053 * @ptr: address of hc register to be read
54 * @mask: bits to look at in result of read
55 * @done: value of those bits when handshake succeeds
56 * @usec: timeout in microseconds
57 *
58 * Returns negative errno, or zero on success
59 *
60 * Success happens when the "mask" bits have the specified value (hardware
61 * handshake done). There are two failure modes: "usec" have passed (major
62 * hardware flakeout), or the register reads as all-ones (hardware removed).
63 */
Lin Wangdc0b1772015-01-09 16:06:28 +020064int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, int usec)
Sarah Sharp66d4ead2009-04-27 19:52:28 -070065{
66 u32 result;
67
68 do {
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020069 result = readl(ptr);
Sarah Sharp66d4ead2009-04-27 19:52:28 -070070 if (result == ~(u32)0) /* card removed */
71 return -ENODEV;
72 result &= mask;
73 if (result == done)
74 return 0;
75 udelay(1);
76 usec--;
77 } while (usec > 0);
78 return -ETIMEDOUT;
79}
80
81/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070082 * Disable interrupts and begin the xHCI halting process.
83 */
84void xhci_quiesce(struct xhci_hcd *xhci)
85{
86 u32 halted;
87 u32 cmd;
88 u32 mask;
89
90 mask = ~(XHCI_IRQS);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020091 halted = readl(&xhci->op_regs->status) & STS_HALT;
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070092 if (!halted)
93 mask &= ~CMD_RUN;
94
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020095 cmd = readl(&xhci->op_regs->command);
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070096 cmd &= mask;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +020097 writel(cmd, &xhci->op_regs->command);
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070098}
99
100/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 * Force HC into halt state.
102 *
103 * Disable any IRQs and clear the run/stop bit.
104 * HC will complete any current and actively pipelined transactions, and
Andiry Xubdfca502011-01-06 15:43:39 +0800105 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700106 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700107 */
108int xhci_halt(struct xhci_hcd *xhci)
109{
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800110 int ret;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300111 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700112 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700113
Lin Wangdc0b1772015-01-09 16:06:28 +0200114 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700115 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
Manu Gautama7806bc2016-09-23 16:12:07 +0530116 if (!ret)
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800117 xhci->xhc_state |= XHCI_STATE_HALTED;
Manu Gautama7806bc2016-09-23 16:12:07 +0530118 else
Sarah Sharp5af98bb2012-03-16 12:58:20 -0700119 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
120 XHCI_MAX_HALT_USEC);
Manu Gautama7806bc2016-09-23 16:12:07 +0530121
122 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
123
124 if (delayed_work_pending(&xhci->cmd_timer)) {
125 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
126 "Cleanup command queue");
127 cancel_delayed_work(&xhci->cmd_timer);
128 xhci_cleanup_command_queue(xhci);
129 }
130
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800131 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700132}
133
134/*
Sarah Sharped074532010-05-24 13:25:21 -0700135 * Set the run bit and wait for the host to be running.
136 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800137static int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700138{
139 u32 temp;
140 int ret;
141
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200142 temp = readl(&xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700143 temp |= (CMD_RUN);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300144 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
Sarah Sharped074532010-05-24 13:25:21 -0700145 temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200146 writel(temp, &xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700147
148 /*
149 * Wait for the HCHalted Status bit to be 0 to indicate the host is
150 * running.
151 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200152 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharped074532010-05-24 13:25:21 -0700153 STS_HALT, 0, XHCI_MAX_HALT_USEC);
154 if (ret == -ETIMEDOUT)
155 xhci_err(xhci, "Host took too long to start, "
156 "waited %u microseconds.\n",
157 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800158 if (!ret)
Mathias Nyman98d74f92016-04-08 16:25:10 +0300159 /* clear state flags. Including dying, halted or removing */
160 xhci->xhc_state = 0;
Roger Quadrose5bfeab2015-09-21 17:46:13 +0300161
Sarah Sharped074532010-05-24 13:25:21 -0700162 return ret;
163}
164
165/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800166 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700167 *
168 * This resets pipelines, timers, counters, state machines, etc.
169 * Transactions will be terminated immediately, and operational registers
170 * will be set to their defaults.
171 */
172int xhci_reset(struct xhci_hcd *xhci)
173{
174 u32 command;
175 u32 state;
Andiry Xuf370b992012-04-14 02:54:30 +0800176 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700177
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200178 state = readl(&xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700179 if ((state & STS_HALT) == 0) {
180 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
181 return 0;
182 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700183
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300184 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200185 command = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700186 command |= CMD_RESET;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200187 writel(command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700188
Rajmohan Mania5964392015-11-18 10:48:20 +0200189 /* Existing Intel xHCI controllers require a delay of 1 mS,
190 * after setting the CMD_RESET bit, and before accessing any
191 * HC registers. This allows the HC to complete the
192 * reset operation and be ready for HC register access.
193 * Without this delay, the subsequent HC register access,
194 * may result in a system hang very rarely.
195 */
196 if (xhci->quirks & XHCI_INTEL_HOST)
197 udelay(1000);
198
Lin Wangdc0b1772015-01-09 16:06:28 +0200199 ret = xhci_handshake(&xhci->op_regs->command,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700200 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700201 if (ret)
202 return ret;
203
Jiahau Chang24a950e2017-07-20 14:48:27 +0300204 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
205 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));
206
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300207 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
208 "Wait for controller to be ready for doorbell rings");
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700209 /*
210 * xHCI cannot write to any doorbells or operational registers other
211 * than status until the "Controller Not Ready" flag is cleared.
212 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200213 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700214 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xuf370b992012-04-14 02:54:30 +0800215
216 for (i = 0; i < 2; ++i) {
217 xhci->bus_state[i].port_c_suspend = 0;
218 xhci->bus_state[i].suspended_ports = 0;
219 xhci->bus_state[i].resuming_ports = 0;
220 }
221
222 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700223}
224
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700225#ifdef CONFIG_PCI
226static int xhci_free_msi(struct xhci_hcd *xhci)
Dong Nguyen43b86af2010-07-21 16:56:08 -0700227{
228 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700229
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700230 if (!xhci->msix_entries)
231 return -EINVAL;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700232
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700233 for (i = 0; i < xhci->msix_count; i++)
234 if (xhci->msix_entries[i].vector)
235 free_irq(xhci->msix_entries[i].vector,
236 xhci_to_hcd(xhci));
237 return 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700238}
239
240/*
241 * Set up MSI
242 */
243static int xhci_setup_msi(struct xhci_hcd *xhci)
244{
245 int ret;
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800246 /*
247 * TODO:Check with MSI Soc for sysdev
248 */
Dong Nguyen43b86af2010-07-21 16:56:08 -0700249 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
250
251 ret = pci_enable_msi(pdev);
252 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300253 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
254 "failed to allocate MSI entry");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700255 return ret;
256 }
257
Alex Shi851ec162013-05-24 10:54:19 +0800258 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700259 0, "xhci_hcd", xhci_to_hcd(xhci));
260 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300261 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
262 "disable MSI interrupt");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700263 pci_disable_msi(pdev);
264 }
265
266 return ret;
267}
268
269/*
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700270 * Free IRQs
271 * free all IRQs request
272 */
273static void xhci_free_irq(struct xhci_hcd *xhci)
274{
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800275 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.sysdev);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700276 int ret;
277
278 /* return if using legacy interrupt */
Felipe Balbicd704692012-02-29 16:46:23 +0200279 if (xhci_to_hcd(xhci)->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700280 return;
281
282 ret = xhci_free_msi(xhci);
283 if (!ret)
284 return;
Felipe Balbicd704692012-02-29 16:46:23 +0200285 if (pdev->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700286 free_irq(pdev->irq, xhci_to_hcd(xhci));
287
288 return;
289}
290
291/*
Dong Nguyen43b86af2010-07-21 16:56:08 -0700292 * Set up MSI-X
293 */
294static int xhci_setup_msix(struct xhci_hcd *xhci)
295{
296 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800297 struct usb_hcd *hcd = xhci_to_hcd(xhci);
298 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700299
300 /*
301 * calculate number of msi-x vectors supported.
302 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
303 * with max number of interrupters based on the xhci HCSPARAMS1.
304 * - num_online_cpus: maximum msi-x vectors per CPUs core.
305 * Add additional 1 vector to ensure always available interrupt.
306 */
307 xhci->msix_count = min(num_online_cpus() + 1,
308 HCS_MAX_INTRS(xhci->hcs_params1));
309
310 xhci->msix_entries =
311 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800312 GFP_KERNEL);
Wolfram Sangf4c46f12016-08-25 19:39:10 +0200313 if (!xhci->msix_entries)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700314 return -ENOMEM;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700315
316 for (i = 0; i < xhci->msix_count; i++) {
317 xhci->msix_entries[i].entry = i;
318 xhci->msix_entries[i].vector = 0;
319 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700320
Alexander Gordeeva62445a2014-05-08 19:25:58 +0300321 ret = pci_enable_msix_exact(pdev, xhci->msix_entries, xhci->msix_count);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700322 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300323 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
324 "Failed to enable MSI-X");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700325 goto free_entries;
326 }
327
Dong Nguyen43b86af2010-07-21 16:56:08 -0700328 for (i = 0; i < xhci->msix_count; i++) {
329 ret = request_irq(xhci->msix_entries[i].vector,
Alex Shi851ec162013-05-24 10:54:19 +0800330 xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700331 0, "xhci_hcd", xhci_to_hcd(xhci));
332 if (ret)
333 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700334 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700335
Andiry Xu00292272010-12-27 17:39:02 +0800336 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700337 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700338
339disable_msix:
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300340 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700341 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700342 pci_disable_msix(pdev);
343free_entries:
344 kfree(xhci->msix_entries);
345 xhci->msix_entries = NULL;
346 return ret;
347}
348
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700349/* Free any IRQs and disable MSI-X */
350static void xhci_cleanup_msix(struct xhci_hcd *xhci)
351{
Andiry Xu00292272010-12-27 17:39:02 +0800352 struct usb_hcd *hcd = xhci_to_hcd(xhci);
353 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700354
Jack Pham90053552013-11-15 14:53:14 -0800355 if (xhci->quirks & XHCI_PLAT)
356 return;
357
Dong Nguyen43b86af2010-07-21 16:56:08 -0700358 xhci_free_irq(xhci);
359
360 if (xhci->msix_entries) {
361 pci_disable_msix(pdev);
362 kfree(xhci->msix_entries);
363 xhci->msix_entries = NULL;
364 } else {
365 pci_disable_msi(pdev);
366 }
367
Andiry Xu00292272010-12-27 17:39:02 +0800368 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700369 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700370}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700371
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700372static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700373{
374 int i;
375
376 if (xhci->msix_entries) {
377 for (i = 0; i < xhci->msix_count; i++)
378 synchronize_irq(xhci->msix_entries[i].vector);
379 }
380}
381
382static int xhci_try_enable_msi(struct usb_hcd *hcd)
383{
384 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700385 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700386 int ret;
387
Sarah Sharp52fb6122013-08-08 10:08:34 -0700388 /* The xhci platform device has set up IRQs through usb_add_hcd. */
389 if (xhci->quirks & XHCI_PLAT)
390 return 0;
391
392 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700393 /*
394 * Some Fresco Logic host controllers advertise MSI, but fail to
395 * generate interrupts. Don't even try to enable MSI.
396 */
397 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100398 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700399
400 /* unregister the legacy interrupt */
401 if (hcd->irq)
402 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200403 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700404
405 ret = xhci_setup_msix(xhci);
406 if (ret)
407 /* fall back to msi*/
408 ret = xhci_setup_msi(xhci);
409
410 if (!ret)
Felipe Balbicd704692012-02-29 16:46:23 +0200411 /* hcd->irq is 0, we have MSI */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700412 return 0;
413
Sarah Sharp68d07f62012-02-13 16:25:57 -0800414 if (!pdev->irq) {
415 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
416 return -EINVAL;
417 }
418
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100419 legacy_irq:
Adrian Huang79699432014-02-27 11:26:03 +0000420 if (!strlen(hcd->irq_descr))
421 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
422 hcd->driver->description, hcd->self.busnum);
423
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700424 /* fall back to legacy interrupt*/
425 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
426 hcd->irq_descr, hcd);
427 if (ret) {
428 xhci_err(xhci, "request interrupt %d failed\n",
429 pdev->irq);
430 return ret;
431 }
432 hcd->irq = pdev->irq;
433 return 0;
434}
435
436#else
437
David Cohen01bb59e2014-04-25 19:20:16 +0300438static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700439{
440 return 0;
441}
442
David Cohen01bb59e2014-04-25 19:20:16 +0300443static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700444{
445}
446
David Cohen01bb59e2014-04-25 19:20:16 +0300447static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700448{
449}
450
451#endif
452
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500453static void compliance_mode_recovery(unsigned long arg)
454{
455 struct xhci_hcd *xhci;
456 struct usb_hcd *hcd;
457 u32 temp;
458 int i;
459
460 xhci = (struct xhci_hcd *)arg;
461
462 for (i = 0; i < xhci->num_usb3_ports; i++) {
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200463 temp = readl(xhci->usb3_ports[i]);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500464 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
465 /*
466 * Compliance Mode Detected. Letting USB Core
467 * handle the Warm Reset
468 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300469 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
470 "Compliance mode detected->port %d",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500471 i + 1);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300472 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
473 "Attempting compliance mode recovery");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500474 hcd = xhci->shared_hcd;
475
476 if (hcd->state == HC_STATE_SUSPENDED)
477 usb_hcd_resume_root_hub(hcd);
478
479 usb_hcd_poll_rh_status(hcd);
480 }
481 }
482
483 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
484 mod_timer(&xhci->comp_mode_recovery_timer,
485 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
486}
487
488/*
489 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
490 * that causes ports behind that hardware to enter compliance mode sometimes.
491 * The quirk creates a timer that polls every 2 seconds the link state of
492 * each host controller's port and recovers it by issuing a Warm reset
493 * if Compliance mode is detected, otherwise the port will become "dead" (no
494 * device connections or disconnections will be detected anymore). Becasue no
495 * status event is generated when entering compliance mode (per xhci spec),
496 * this quirk is needed on systems that have the failing hardware installed.
497 */
498static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
499{
500 xhci->port_status_u0 = 0;
Julia Lawallfc8abe02015-01-09 16:06:29 +0200501 setup_timer(&xhci->comp_mode_recovery_timer,
502 compliance_mode_recovery, (unsigned long)xhci);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500503 xhci->comp_mode_recovery_timer.expires = jiffies +
504 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
505
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500506 add_timer(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300507 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
508 "Compliance mode recovery timer initialized");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500509}
510
511/*
512 * This function identifies the systems that have installed the SN65LVPE502CP
513 * USB3.0 re-driver and that need the Compliance Mode Quirk.
514 * Systems:
515 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
516 */
Andrew Brestickere1cd9722014-10-03 11:35:27 +0300517static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500518{
519 const char *dmi_product_name, *dmi_sys_vendor;
520
521 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
522 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530523 if (!dmi_product_name || !dmi_sys_vendor)
524 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500525
526 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
527 return false;
528
529 if (strstr(dmi_product_name, "Z420") ||
530 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500531 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600532 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500533 return true;
534
535 return false;
536}
537
538static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
539{
540 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
541}
542
543
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700544/*
545 * Initialize memory for HCD and xHC (one-time init).
546 *
547 * Program the PAGESIZE register, initialize the device context array, create
548 * device contexts (?), set up a command ring segment (or two?), create event
549 * ring (one for now).
550 */
551int xhci_init(struct usb_hcd *hcd)
552{
553 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
554 int retval = 0;
555
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300556 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700557 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700558 if (xhci->hci_version == 0x95 && link_quirk) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300559 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
560 "QUIRK: Not clearing Link TRB chain bits.");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700561 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
562 } else {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300563 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
564 "xHCI doesn't need link TRB QUIRK");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700565 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700566 retval = xhci_mem_init(xhci, GFP_KERNEL);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300567 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700568
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500569 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700570 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500571 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
572 compliance_mode_recovery_timer_init(xhci);
573 }
574
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700575 return retval;
576}
577
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700578/*-------------------------------------------------------------------------*/
579
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700580
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800581static int xhci_run_finished(struct xhci_hcd *xhci)
582{
583 if (xhci_start(xhci)) {
584 xhci_halt(xhci);
585 return -ENODEV;
586 }
587 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800588 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800589
590 if (xhci->quirks & XHCI_NEC_HOST)
591 xhci_ring_cmd_db(xhci);
592
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300593 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
594 "Finished xhci_run for USB3 roothub");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800595 return 0;
596}
597
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700598/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700599 * Start the HC after it was halted.
600 *
601 * This function is called by the USB core when the HC driver is added.
602 * Its opposite is xhci_stop().
603 *
604 * xhci_init() must be called once before this function can be called.
605 * Reset the HC, enable device slot contexts, program DCBAAP, and
606 * set command ring pointer and event ring pointer.
607 *
608 * Setup MSI-X vectors and enable interrupts.
609 */
610int xhci_run(struct usb_hcd *hcd)
611{
612 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700613 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700614 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700615 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700616
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800617 /* Start the xHCI host controller running only after the USB 2.0 roothub
618 * is setup.
619 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700620
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700621 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800622 if (!usb_hcd_is_primary_hcd(hcd))
623 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700624
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300625 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700626
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700627 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700628 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700629 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700630
Sarah Sharp66e49d82009-07-27 12:03:46 -0700631 xhci_dbg(xhci, "Command ring memory map follows:\n");
632 xhci_debug_ring(xhci, xhci->cmd_ring);
633 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
634 xhci_dbg_cmd_ptrs(xhci);
635
636 xhci_dbg(xhci, "ERST memory map follows:\n");
637 xhci_dbg_erst(xhci, &xhci->erst);
638 xhci_dbg(xhci, "Event ring:\n");
639 xhci_debug_ring(xhci, xhci->event_ring);
640 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800641 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700642 temp_64 &= ~ERST_PTR_MASK;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300643 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
644 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700645
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300646 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
647 "// Set the interrupt modulation register");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200648 temp = readl(&xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700649 temp &= ~ER_IRQ_INTERVAL_MASK;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200650 /*
651 * the increment interval is 8 times as much as that defined
652 * in xHCI spec on MTK's controller
653 */
654 temp |= (u32) ((xhci->quirks & XHCI_MTK_HOST) ? 20 : 160);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200655 writel(temp, &xhci->ir_set->irq_control);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700656
657 /* Set the HCD state before we enable the irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200658 temp = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700659 temp |= (CMD_EIE);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300660 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
661 "// Enable interrupts, cmd = 0x%x.", temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200662 writel(temp, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700663
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200664 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300665 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
666 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700667 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200668 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800669 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700670
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300671 if (xhci->quirks & XHCI_NEC_HOST) {
672 struct xhci_command *command;
673 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
674 if (!command)
675 return -ENOMEM;
676 xhci_queue_vendor_command(xhci, command, 0, 0, 0,
Sarah Sharp02386342010-05-24 13:25:28 -0700677 TRB_TYPE(TRB_NEC_GET_FW));
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300678 }
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300679 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
680 "Finished xhci_run for USB2 roothub");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700681 return 0;
682}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300683EXPORT_SYMBOL_GPL(xhci_run);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700684
685/*
686 * Stop xHCI driver.
687 *
688 * This function is called by the USB core when the HC driver is removed.
689 * Its opposite is xhci_run().
690 *
691 * Disable device contexts, disable IRQs, and quiesce the HC.
692 * Reset the HC, finish any completed transactions, and cleanup memory.
693 */
694void xhci_stop(struct usb_hcd *hcd)
695{
696 u32 temp;
697 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
698
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300699 mutex_lock(&xhci->mutex);
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300700
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300701 if (!(xhci->xhc_state & XHCI_STATE_HALTED)) {
702 spin_lock_irq(&xhci->lock);
703
704 xhci->xhc_state |= XHCI_STATE_HALTED;
705 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
706 xhci_halt(xhci);
707 xhci_reset(xhci);
708
709 spin_unlock_irq(&xhci->lock);
710 }
711
712 if (!usb_hcd_is_primary_hcd(hcd)) {
713 mutex_unlock(&xhci->mutex);
714 return;
715 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700716
Zhang Rui40a9fb12010-12-17 13:17:04 -0800717 xhci_cleanup_msix(xhci);
718
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500719 /* Deleting Compliance Mode Recovery Timer */
720 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400721 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500722 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300723 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
724 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400725 __func__);
726 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500727
Andiry Xuc41136b2011-03-22 17:08:14 +0800728 if (xhci->quirks & XHCI_AMD_PLL_FIX)
729 usb_amd_dev_put();
730
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300731 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
732 "// Disabling event ring interrupts");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200733 temp = readl(&xhci->op_regs->status);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200734 writel(temp & ~STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200735 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200736 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800737 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700738
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300739 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700740 xhci_mem_cleanup(xhci);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300741 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
742 "xhci_stop completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200743 readl(&xhci->op_regs->status));
Roger Quadros85ac90f2015-09-21 17:46:12 +0300744 mutex_unlock(&xhci->mutex);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700745}
746
747/*
748 * Shutdown HC (not bus-specific)
749 *
750 * This is called when the machine is rebooting or halting. We assume that the
751 * machine will be powered off, and the HC's internal state will be reset.
752 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800753 *
754 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700755 */
756void xhci_shutdown(struct usb_hcd *hcd)
757{
758 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
759
Dan Carpenter052c7f92012-08-13 19:57:03 +0300760 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800761 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
Sarah Sharpe95829f2012-07-23 18:59:30 +0300762
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700763 spin_lock_irq(&xhci->lock);
764 xhci_halt(xhci);
Takashi Iwai638298d2013-09-12 08:11:06 +0200765 /* Workaround for spurious wakeups at shutdown with HSW */
766 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
767 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700768 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700769
Zhang Rui40a9fb12010-12-17 13:17:04 -0800770 xhci_cleanup_msix(xhci);
771
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300772 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
773 "xhci_shutdown completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200774 readl(&xhci->op_regs->status));
Takashi Iwai638298d2013-09-12 08:11:06 +0200775
776 /* Yet another workaround for spurious wakeups at shutdown with HSW */
777 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800778 pci_set_power_state(to_pci_dev(hcd->self.sysdev), PCI_D3hot);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700779}
780
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700781#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700782static void xhci_save_registers(struct xhci_hcd *xhci)
783{
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200784 xhci->s3.command = readl(&xhci->op_regs->command);
785 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800786 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200787 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
788 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800789 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
790 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200791 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
792 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700793}
794
795static void xhci_restore_registers(struct xhci_hcd *xhci)
796{
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200797 writel(xhci->s3.command, &xhci->op_regs->command);
798 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
Sarah Sharp477632d2014-01-29 14:02:00 -0800799 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200800 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
801 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
Sarah Sharp477632d2014-01-29 14:02:00 -0800802 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
803 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200804 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
805 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700806}
807
Sarah Sharp89821322010-11-12 11:59:31 -0800808static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
809{
810 u64 val_64;
811
812 /* step 2: initialize command ring buffer */
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800813 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800814 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
815 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
816 xhci->cmd_ring->dequeue) &
817 (u64) ~CMD_RING_RSVD_BITS) |
818 xhci->cmd_ring->cycle_state;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300819 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
820 "// Setting command ring address to 0x%llx",
Sarah Sharp89821322010-11-12 11:59:31 -0800821 (long unsigned long) val_64);
Sarah Sharp477632d2014-01-29 14:02:00 -0800822 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800823}
824
825/*
826 * The whole command ring must be cleared to zero when we suspend the host.
827 *
828 * The host doesn't save the command ring pointer in the suspend well, so we
829 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
830 * aligned, because of the reserved bits in the command ring dequeue pointer
831 * register. Therefore, we can't just set the dequeue pointer back in the
832 * middle of the ring (TRBs are 16-byte aligned).
833 */
834static void xhci_clear_command_ring(struct xhci_hcd *xhci)
835{
836 struct xhci_ring *ring;
837 struct xhci_segment *seg;
838
839 ring = xhci->cmd_ring;
840 seg = ring->deq_seg;
841 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800842 memset(seg->trbs, 0,
843 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
844 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
845 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800846 seg = seg->next;
847 } while (seg != ring->deq_seg);
848
849 /* Reset the software enqueue and dequeue pointers */
850 ring->deq_seg = ring->first_seg;
851 ring->dequeue = ring->first_seg->trbs;
852 ring->enq_seg = ring->deq_seg;
853 ring->enqueue = ring->dequeue;
854
Andiry Xub008df62012-03-05 17:49:34 +0800855 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800856 /*
857 * Ring is now zeroed, so the HW should look for change of ownership
858 * when the cycle bit is set to 1.
859 */
860 ring->cycle_state = 1;
861
862 /*
863 * Reset the hardware dequeue pointer.
864 * Yes, this will need to be re-written after resume, but we're paranoid
865 * and want to make sure the hardware doesn't access bogus memory
866 * because, say, the BIOS or an SMI started the host without changing
867 * the command ring pointers.
868 */
869 xhci_set_cmd_ring_deq(xhci);
870}
871
Lu Baolua1377e52014-11-18 11:27:14 +0200872static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
873{
874 int port_index;
875 __le32 __iomem **port_array;
876 unsigned long flags;
877 u32 t1, t2;
878
879 spin_lock_irqsave(&xhci->lock, flags);
880
881 /* disble usb3 ports Wake bits*/
882 port_index = xhci->num_usb3_ports;
883 port_array = xhci->usb3_ports;
884 while (port_index--) {
885 t1 = readl(port_array[port_index]);
886 t1 = xhci_port_state_to_neutral(t1);
887 t2 = t1 & ~PORT_WAKE_BITS;
888 if (t1 != t2)
889 writel(t2, port_array[port_index]);
890 }
891
892 /* disble usb2 ports Wake bits*/
893 port_index = xhci->num_usb2_ports;
894 port_array = xhci->usb2_ports;
895 while (port_index--) {
896 t1 = readl(port_array[port_index]);
897 t1 = xhci_port_state_to_neutral(t1);
898 t2 = t1 & ~PORT_WAKE_BITS;
899 if (t1 != t2)
900 writel(t2, port_array[port_index]);
901 }
902
903 spin_unlock_irqrestore(&xhci->lock, flags);
904}
905
Andiry Xu5535b1d2010-10-14 07:23:06 -0700906/*
907 * Stop HC (not bus-specific)
908 *
909 * This is called when the machine transition into S3/S4 mode.
910 *
911 */
Lu Baolua1377e52014-11-18 11:27:14 +0200912int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
Andiry Xu5535b1d2010-10-14 07:23:06 -0700913{
914 int rc = 0;
Oliver Neukum455f5892013-09-30 15:50:54 +0200915 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700916 struct usb_hcd *hcd = xhci_to_hcd(xhci);
917 u32 command;
918
Roger Quadros9fa733f2015-05-29 17:01:50 +0300919 if (!hcd->state)
920 return 0;
921
Felipe Balbi77b84762012-10-19 10:55:16 +0300922 if (hcd->state != HC_STATE_SUSPENDED ||
923 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
924 return -EINVAL;
925
Lu Baolua1377e52014-11-18 11:27:14 +0200926 /* Clear root port wake on bits if wakeup not allowed. */
927 if (!do_wakeup)
928 xhci_disable_port_wake_on_bits(xhci);
929
Sarah Sharpc52804a2012-11-27 12:30:23 -0800930 /* Don't poll the roothubs on bus suspend. */
931 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
932 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
933 del_timer_sync(&hcd->rh_timer);
Al Cooper14e61a12014-08-20 16:41:57 +0300934 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
935 del_timer_sync(&xhci->shared_hcd->rh_timer);
Sarah Sharpc52804a2012-11-27 12:30:23 -0800936
Andiry Xu5535b1d2010-10-14 07:23:06 -0700937 spin_lock_irq(&xhci->lock);
938 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -0800939 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700940 /* step 1: stop endpoint */
941 /* skipped assuming that port suspend has done */
942
943 /* step 2: clear Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200944 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700945 command &= ~CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200946 writel(command, &xhci->op_regs->command);
Oliver Neukum455f5892013-09-30 15:50:54 +0200947
948 /* Some chips from Fresco Logic need an extraordinary delay */
949 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
950
Lin Wangdc0b1772015-01-09 16:06:28 +0200951 if (xhci_handshake(&xhci->op_regs->status,
Oliver Neukum455f5892013-09-30 15:50:54 +0200952 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d2010-10-14 07:23:06 -0700953 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
954 spin_unlock_irq(&xhci->lock);
955 return -ETIMEDOUT;
956 }
Sarah Sharp89821322010-11-12 11:59:31 -0800957 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700958
959 /* step 3: save registers */
960 xhci_save_registers(xhci);
961
962 /* step 4: set CSS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200963 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700964 command |= CMD_CSS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200965 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +0200966 if (xhci_handshake(&xhci->op_regs->status,
Sarah Sharp2611bd182012-10-25 13:27:51 -0700967 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +0800968 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700969 spin_unlock_irq(&xhci->lock);
970 return -ETIMEDOUT;
971 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700972 spin_unlock_irq(&xhci->lock);
973
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500974 /*
975 * Deleting Compliance Mode Recovery Timer because the xHCI Host
976 * is about to be suspended.
977 */
978 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
979 (!(xhci_all_ports_seen_u0(xhci)))) {
980 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300981 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
982 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400983 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500984 }
985
Andiry Xu00292272010-12-27 17:39:02 +0800986 /* step 5: remove core well power */
987 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700988 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800989
Andiry Xu5535b1d2010-10-14 07:23:06 -0700990 return rc;
991}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300992EXPORT_SYMBOL_GPL(xhci_suspend);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700993
994/*
995 * start xHC (not bus-specific)
996 *
997 * This is called when the machine transition from S3/S4 mode.
998 *
999 */
1000int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
1001{
Wang, Yud6236f62014-06-24 17:14:44 +03001002 u32 command, temp = 0, status;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001003 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -08001004 struct usb_hcd *secondary_hcd;
Alan Sternf69e3122011-11-03 11:37:10 -04001005 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -05001006 bool comp_timer_running = false;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001007
Roger Quadros9fa733f2015-05-29 17:01:50 +03001008 if (!hcd->state)
1009 return 0;
1010
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001011 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001012 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001013 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001014 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
1015 time_before(jiffies,
1016 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -07001017 msleep(100);
1018
Alan Sternf69e3122011-11-03 11:37:10 -04001019 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1020 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
1021
Andiry Xu5535b1d2010-10-14 07:23:06 -07001022 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +02001023 if (xhci->quirks & XHCI_RESET_ON_RESUME)
1024 hibernated = true;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001025
1026 if (!hibernated) {
1027 /* step 1: restore register */
1028 xhci_restore_registers(xhci);
1029 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -08001030 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001031 /* step 3: restore state and start state*/
1032 /* step 3: set CRS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001033 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001034 command |= CMD_CRS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001035 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001036 if (xhci_handshake(&xhci->op_regs->status,
Andiry Xu622eb782012-06-13 10:51:57 +08001037 STS_RESTORE, 0, 10 * 1000)) {
1038 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -07001039 spin_unlock_irq(&xhci->lock);
1040 return -ETIMEDOUT;
1041 }
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001042 temp = readl(&xhci->op_regs->status);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001043 }
1044
1045 /* If restore operation fails, re-initialize the HC during resume */
1046 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -05001047
1048 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1049 !(xhci_all_ports_seen_u0(xhci))) {
1050 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001051 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1052 "Compliance Mode Recovery Timer deleted!");
Tony Camuso77df9e02013-02-21 16:11:27 -05001053 }
1054
Sarah Sharpfedd3832011-04-12 17:43:19 -07001055 /* Let the USB core know _both_ roothubs lost power. */
1056 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1057 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001058
1059 xhci_dbg(xhci, "Stop HCD\n");
1060 xhci_halt(xhci);
1061 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001062 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001063 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001064
Andiry Xu5535b1d2010-10-14 07:23:06 -07001065 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001066 temp = readl(&xhci->op_regs->status);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001067 writel(temp & ~STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001068 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001069 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -08001070 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001071
1072 xhci_dbg(xhci, "cleaning up memory\n");
1073 xhci_mem_cleanup(xhci);
1074 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001075 readl(&xhci->op_regs->status));
Andiry Xu5535b1d2010-10-14 07:23:06 -07001076
Sarah Sharp65b22f92010-12-17 12:35:05 -08001077 /* USB core calls the PCI reinit and start functions twice:
1078 * first with the primary HCD, and then with the secondary HCD.
1079 * If we don't do the same, the host will never be started.
1080 */
1081 if (!usb_hcd_is_primary_hcd(hcd))
1082 secondary_hcd = hcd;
1083 else
1084 secondary_hcd = xhci->shared_hcd;
1085
1086 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1087 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001088 if (retval)
1089 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001090 comp_timer_running = true;
1091
Sarah Sharp65b22f92010-12-17 12:35:05 -08001092 xhci_dbg(xhci, "Start the primary HCD\n");
1093 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001094 if (!retval) {
Alan Sternf69e3122011-11-03 11:37:10 -04001095 xhci_dbg(xhci, "Start the secondary HCD\n");
1096 retval = xhci_run(secondary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001097 }
Andiry Xu5535b1d2010-10-14 07:23:06 -07001098 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb3209372011-03-07 11:24:07 -08001099 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e3122011-11-03 11:37:10 -04001100 goto done;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001101 }
1102
Andiry Xu5535b1d2010-10-14 07:23:06 -07001103 /* step 4: set Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001104 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001105 command |= CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001106 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001107 xhci_handshake(&xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d2010-10-14 07:23:06 -07001108 0, 250 * 1000);
1109
1110 /* step 5: walk topology and initialize portsc,
1111 * portpmsc and portli
1112 */
1113 /* this is done in bus_resume */
1114
1115 /* step 6: restart each of the previously
1116 * Running endpoints by ringing their doorbells
1117 */
1118
Andiry Xu5535b1d2010-10-14 07:23:06 -07001119 spin_unlock_irq(&xhci->lock);
Alan Sternf69e3122011-11-03 11:37:10 -04001120
1121 done:
1122 if (retval == 0) {
Wang, Yud6236f62014-06-24 17:14:44 +03001123 /* Resume root hubs only when have pending events. */
1124 status = readl(&xhci->op_regs->status);
1125 if (status & STS_EINT) {
Wang, Yud6236f62014-06-24 17:14:44 +03001126 usb_hcd_resume_root_hub(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001127 usb_hcd_resume_root_hub(hcd);
Wang, Yud6236f62014-06-24 17:14:44 +03001128 }
Alan Sternf69e3122011-11-03 11:37:10 -04001129 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001130
1131 /*
1132 * If system is subject to the Quirk, Compliance Mode Timer needs to
1133 * be re-initialized Always after a system resume. Ports are subject
1134 * to suffer the Compliance Mode issue again. It doesn't matter if
1135 * ports have entered previously to U0 before system's suspension.
1136 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001137 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001138 compliance_mode_recovery_timer_init(xhci);
1139
Jiahau Chang24a950e2017-07-20 14:48:27 +03001140 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
1141 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
1142
Sarah Sharpc52804a2012-11-27 12:30:23 -08001143 /* Re-enable port polling. */
1144 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
Al Cooper14e61a12014-08-20 16:41:57 +03001145 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1146 usb_hcd_poll_rh_status(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001147 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1148 usb_hcd_poll_rh_status(hcd);
Sarah Sharpc52804a2012-11-27 12:30:23 -08001149
Alan Sternf69e3122011-11-03 11:37:10 -04001150 return retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001151}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001152EXPORT_SYMBOL_GPL(xhci_resume);
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001153#endif /* CONFIG_PM */
1154
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001155/*-------------------------------------------------------------------------*/
1156
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001157/**
1158 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1159 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1160 * value to right shift 1 for the bitmask.
1161 *
1162 * Index = (epnum * 2) + direction - 1,
1163 * where direction = 0 for OUT, 1 for IN.
1164 * For control endpoints, the IN index is used (OUT index is unused), so
1165 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1166 */
1167unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1168{
1169 unsigned int index;
1170 if (usb_endpoint_xfer_control(desc))
1171 index = (unsigned int) (usb_endpoint_num(desc)*2);
1172 else
1173 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1174 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1175 return index;
1176}
1177
Julius Werner01c5f442013-04-15 15:55:04 -07001178/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1179 * address from the XHCI endpoint index.
1180 */
1181unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1182{
1183 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1184 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1185 return direction | number;
1186}
1187
Sarah Sharpf94e01862009-04-27 19:58:38 -07001188/* Find the flag for this endpoint (for use in the control context). Use the
1189 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1190 * bit 1, etc.
1191 */
1192unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1193{
1194 return 1 << (xhci_get_endpoint_index(desc) + 1);
1195}
1196
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001197/* Find the flag for this endpoint (for use in the control context). Use the
1198 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1199 * bit 1, etc.
1200 */
1201unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1202{
1203 return 1 << (ep_index + 1);
1204}
1205
Sarah Sharpf94e01862009-04-27 19:58:38 -07001206/* Compute the last valid endpoint context index. Basically, this is the
1207 * endpoint index plus one. For slot contexts with more than valid endpoint,
1208 * we find the most significant bit set in the added contexts flags.
1209 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1210 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1211 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001212unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001213{
1214 return fls(added_ctxs) - 1;
1215}
1216
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001217/* Returns 1 if the arguments are OK;
1218 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1219 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001220static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001221 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1222 const char *func) {
1223 struct xhci_hcd *xhci;
1224 struct xhci_virt_device *virt_dev;
1225
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001226 if (!hcd || (check_ep && !ep) || !udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001227 pr_debug("xHCI %s called with invalid args\n", func);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001228 return -EINVAL;
1229 }
1230 if (!udev->parent) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001231 pr_debug("xHCI %s called for root hub\n", func);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001232 return 0;
1233 }
Andiry Xu64927732010-10-14 07:22:45 -07001234
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001235 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001236 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001237 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001238 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1239 func);
Andiry Xu64927732010-10-14 07:22:45 -07001240 return -EINVAL;
1241 }
1242
1243 virt_dev = xhci->devs[udev->slot_id];
1244 if (virt_dev->udev != udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001245 xhci_dbg(xhci, "xHCI %s called with udev and "
Andiry Xu64927732010-10-14 07:22:45 -07001246 "virt_dev does not match\n", func);
1247 return -EINVAL;
1248 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001249 }
Andiry Xu64927732010-10-14 07:22:45 -07001250
Sarah Sharp203a8662013-07-24 10:27:13 -07001251 if (xhci->xhc_state & XHCI_STATE_HALTED)
1252 return -ENODEV;
1253
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001254 return 1;
1255}
1256
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001257static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001258 struct usb_device *udev, struct xhci_command *command,
1259 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001260
1261/*
1262 * Full speed devices may have a max packet size greater than 8 bytes, but the
1263 * USB core doesn't know that until it reads the first 8 bytes of the
1264 * descriptor. If the usb_device's max packet size changes after that point,
1265 * we need to issue an evaluate context command and wait on it.
1266 */
1267static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1268 unsigned int ep_index, struct urb *urb)
1269{
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001270 struct xhci_container_ctx *out_ctx;
1271 struct xhci_input_control_ctx *ctrl_ctx;
1272 struct xhci_ep_ctx *ep_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001273 struct xhci_command *command;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001274 int max_packet_size;
1275 int hw_max_packet_size;
1276 int ret = 0;
1277
1278 out_ctx = xhci->devs[slot_id]->out_ctx;
1279 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001280 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001281 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001282 if (hw_max_packet_size != max_packet_size) {
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001283 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1284 "Max Packet Size for ep 0 changed.");
1285 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1286 "Max packet size in usb_device = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001287 max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001288 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1289 "Max packet size in xHCI HW = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001290 hw_max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001291 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1292 "Issuing evaluate context command.");
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001293
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001294 /* Set up the input context flags for the command */
1295 /* FIXME: This won't work if a non-default control endpoint
1296 * changes max packet sizes.
1297 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001298
1299 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
1300 if (!command)
1301 return -ENOMEM;
1302
1303 command->in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001304 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001305 if (!ctrl_ctx) {
1306 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1307 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001308 ret = -ENOMEM;
1309 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07001310 }
1311 /* Set up the modified control endpoint 0 */
1312 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1313 xhci->devs[slot_id]->out_ctx, ep_index);
1314
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001315 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001316 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1317 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1318
Matt Evans28ccd292011-03-29 13:40:46 +11001319 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001320 ctrl_ctx->drop_flags = 0;
1321
1322 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001323 xhci_dbg_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001324 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1325 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1326
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001327 ret = xhci_configure_endpoint(xhci, urb->dev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001328 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001329
1330 /* Clean up the input context for later use by bandwidth
1331 * functions.
1332 */
Matt Evans28ccd292011-03-29 13:40:46 +11001333 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001334command_cleanup:
1335 kfree(command->completion);
1336 kfree(command);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001337 }
1338 return ret;
1339}
1340
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001341/*
1342 * non-error returns are a promise to giveback() the urb later
1343 * we drop ownership so next owner (or urb unlink) can get it
1344 */
1345int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1346{
1347 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Andiry Xu2ffdea22011-09-02 11:05:57 -07001348 struct xhci_td *buffer;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001349 unsigned long flags;
1350 int ret = 0;
1351 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001352 struct urb_priv *urb_priv;
1353 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001354
Andiry Xu64927732010-10-14 07:22:45 -07001355 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1356 true, true, __func__) <= 0)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001357 return -EINVAL;
1358
1359 slot_id = urb->dev->slot_id;
1360 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001361
Alan Stern541c7d42010-06-22 16:39:10 -04001362 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001363 if (!in_interrupt())
1364 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1365 ret = -ESHUTDOWN;
1366 goto exit;
1367 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001368
1369 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1370 size = urb->number_of_packets;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03001371 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1372 urb->transfer_buffer_length > 0 &&
1373 urb->transfer_flags & URB_ZERO_PACKET &&
1374 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
1375 size = 2;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001376 else
1377 size = 1;
1378
1379 urb_priv = kzalloc(sizeof(struct urb_priv) +
1380 size * sizeof(struct xhci_td *), mem_flags);
1381 if (!urb_priv)
1382 return -ENOMEM;
1383
Andiry Xu2ffdea22011-09-02 11:05:57 -07001384 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1385 if (!buffer) {
1386 kfree(urb_priv);
1387 return -ENOMEM;
1388 }
1389
Andiry Xu8e51adc2010-07-22 15:23:31 -07001390 for (i = 0; i < size; i++) {
Andiry Xu2ffdea22011-09-02 11:05:57 -07001391 urb_priv->td[i] = buffer;
1392 buffer++;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001393 }
1394
1395 urb_priv->length = size;
1396 urb_priv->td_cnt = 0;
1397 urb->hcpriv = urb_priv;
1398
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001399 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1400 /* Check to see if the max packet size for the default control
1401 * endpoint changed during FS device enumeration
1402 */
1403 if (urb->dev->speed == USB_SPEED_FULL) {
1404 ret = xhci_check_maxpacket(xhci, slot_id,
1405 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001406 if (ret < 0) {
Lin Wang4daf9df2015-01-09 16:06:31 +02001407 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001408 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001409 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001410 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001411 }
1412
Sarah Sharpb11069f2009-07-27 12:03:23 -07001413 /* We have a spinlock and interrupts disabled, so we must pass
1414 * atomic context to this function, which may allocate memory.
1415 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001416 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001417 if (xhci->xhc_state & XHCI_STATE_DYING)
1418 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001419 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001420 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001421 if (ret)
1422 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001423 spin_unlock_irqrestore(&xhci->lock, flags);
1424 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1425 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001426 if (xhci->xhc_state & XHCI_STATE_DYING)
1427 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001428 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1429 EP_GETTING_STREAMS) {
1430 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1431 "is transitioning to using streams.\n");
1432 ret = -EINVAL;
1433 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1434 EP_GETTING_NO_STREAMS) {
1435 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1436 "is transitioning to "
1437 "not having streams.\n");
1438 ret = -EINVAL;
1439 } else {
1440 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1441 slot_id, ep_index);
1442 }
Sarah Sharpd13565c2011-07-22 14:34:34 -07001443 if (ret)
1444 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001445 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001446 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1447 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001448 if (xhci->xhc_state & XHCI_STATE_DYING)
1449 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001450 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1451 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001452 if (ret)
1453 goto free_priv;
Sarah Sharp624defa2009-09-02 12:14:28 -07001454 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001455 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001456 spin_lock_irqsave(&xhci->lock, flags);
1457 if (xhci->xhc_state & XHCI_STATE_DYING)
1458 goto dying;
1459 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1460 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001461 if (ret)
1462 goto free_priv;
Andiry Xu787f4e52010-07-22 15:23:52 -07001463 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001464 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001465exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001466 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001467dying:
1468 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1469 "non-responsive xHCI host.\n",
1470 urb->ep->desc.bEndpointAddress, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001471 ret = -ESHUTDOWN;
1472free_priv:
Lin Wang4daf9df2015-01-09 16:06:31 +02001473 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001474 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001475 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001476 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001477}
1478
Sarah Sharpae636742009-04-29 19:02:31 -07001479/*
1480 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1481 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1482 * should pick up where it left off in the TD, unless a Set Transfer Ring
1483 * Dequeue Pointer is issued.
1484 *
1485 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1486 * the ring. Since the ring is a contiguous structure, they can't be physically
1487 * removed. Instead, there are two options:
1488 *
1489 * 1) If the HC is in the middle of processing the URB to be canceled, we
1490 * simply move the ring's dequeue pointer past those TRBs using the Set
1491 * Transfer Ring Dequeue Pointer command. This will be the common case,
1492 * when drivers timeout on the last submitted URB and attempt to cancel.
1493 *
1494 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1495 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1496 * HC will need to invalidate the any TRBs it has cached after the stop
1497 * endpoint command, as noted in the xHCI 0.95 errata.
1498 *
1499 * 3) The TD may have completed by the time the Stop Endpoint Command
1500 * completes, so software needs to handle that case too.
1501 *
1502 * This function should protect against the TD enqueueing code ringing the
1503 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1504 * It also needs to account for multiple cancellations on happening at the same
1505 * time for the same endpoint.
1506 *
1507 * Note that this function can be called in any context, or so says
1508 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001509 */
1510int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1511{
Sarah Sharpae636742009-04-29 19:02:31 -07001512 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001513 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001514 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001515 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001516 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001517 struct xhci_td *td;
1518 unsigned int ep_index;
1519 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001520 struct xhci_virt_ep *ep;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001521 struct xhci_command *command;
Sarah Sharpae636742009-04-29 19:02:31 -07001522
1523 xhci = hcd_to_xhci(hcd);
1524 spin_lock_irqsave(&xhci->lock, flags);
1525 /* Make sure the URB hasn't completed or been unlinked already */
1526 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1527 if (ret || !urb->hcpriv)
1528 goto done;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001529 temp = readl(&xhci->op_regs->status);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -08001530 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001531 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1532 "HW died, freeing TD.");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001533 urb_priv = urb->hcpriv;
Mathias Nyman5c821712016-01-26 17:50:12 +02001534 for (i = urb_priv->td_cnt;
1535 i < urb_priv->length && xhci->devs[urb->dev->slot_id];
1536 i++) {
Sarah Sharp585df1d2011-08-02 15:43:40 -07001537 td = urb_priv->td[i];
1538 if (!list_empty(&td->td_list))
1539 list_del_init(&td->td_list);
1540 if (!list_empty(&td->cancelled_td_list))
1541 list_del_init(&td->cancelled_td_list);
1542 }
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001543
1544 usb_hcd_unlink_urb_from_ep(hcd, urb);
1545 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001546 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Lin Wang4daf9df2015-01-09 16:06:31 +02001547 xhci_urb_free_priv(urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001548 return ret;
1549 }
Sarah Sharpae636742009-04-29 19:02:31 -07001550
Sarah Sharpae636742009-04-29 19:02:31 -07001551 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001552 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001553 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1554 if (!ep_ring) {
1555 ret = -EINVAL;
1556 goto done;
1557 }
1558
Andiry Xu8e51adc2010-07-22 15:23:31 -07001559 urb_priv = urb->hcpriv;
Sarah Sharp79688ac2011-12-19 16:56:04 -08001560 i = urb_priv->td_cnt;
1561 if (i < urb_priv->length)
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001562 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1563 "Cancel URB %p, dev %s, ep 0x%x, "
1564 "starting at offset 0x%llx",
Sarah Sharp79688ac2011-12-19 16:56:04 -08001565 urb, urb->dev->devpath,
1566 urb->ep->desc.bEndpointAddress,
1567 (unsigned long long) xhci_trb_virt_to_dma(
1568 urb_priv->td[i]->start_seg,
1569 urb_priv->td[i]->first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001570
Sarah Sharp79688ac2011-12-19 16:56:04 -08001571 for (; i < urb_priv->length; i++) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001572 td = urb_priv->td[i];
1573 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1574 }
1575
Sarah Sharpae636742009-04-29 19:02:31 -07001576 /* Queue a stop endpoint command, but only if this is
1577 * the first cancellation to be handled.
1578 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001579 if (!(ep->ep_state & EP_HALT_PENDING)) {
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001580 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
Hans de Goedea0ee6192014-07-25 22:01:21 +02001581 if (!command) {
1582 ret = -ENOMEM;
1583 goto done;
1584 }
Sarah Sharp678539c2009-10-27 10:55:52 -07001585 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001586 ep->stop_cmds_pending++;
1587 ep->stop_cmd_timer.expires = jiffies +
1588 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1589 add_timer(&ep->stop_cmd_timer);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001590 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
1591 ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001592 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001593 }
1594done:
1595 spin_unlock_irqrestore(&xhci->lock, flags);
1596 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001597}
1598
Sarah Sharpf94e01862009-04-27 19:58:38 -07001599/* Drop an endpoint from a new bandwidth configuration for this device.
1600 * Only one call to this function is allowed per endpoint before
1601 * check_bandwidth() or reset_bandwidth() must be called.
1602 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1603 * add the endpoint to the schedule with possibly new parameters denoted by a
1604 * different endpoint descriptor in usb_host_endpoint.
1605 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1606 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001607 *
1608 * The USB core will not allow URBs to be queued to an endpoint that is being
1609 * disabled, so there's no need for mutual exclusion to protect
1610 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001611 */
1612int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1613 struct usb_host_endpoint *ep)
1614{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001615 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001616 struct xhci_container_ctx *in_ctx, *out_ctx;
1617 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001618 unsigned int ep_index;
1619 struct xhci_ep_ctx *ep_ctx;
1620 u32 drop_flag;
Julius Wernerd6759132014-06-24 17:14:42 +03001621 u32 new_add_flags, new_drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001622 int ret;
1623
Andiry Xu64927732010-10-14 07:22:45 -07001624 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001625 if (ret <= 0)
1626 return ret;
1627 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001628 if (xhci->xhc_state & XHCI_STATE_DYING)
1629 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001630
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001631 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001632 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1633 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1634 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1635 __func__, drop_flag);
1636 return 0;
1637 }
1638
Sarah Sharpf94e01862009-04-27 19:58:38 -07001639 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001640 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001641 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001642 if (!ctrl_ctx) {
1643 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1644 __func__);
1645 return 0;
1646 }
1647
Sarah Sharpf94e01862009-04-27 19:58:38 -07001648 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001649 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001650 /* If the HC already knows the endpoint is disabled,
1651 * or the HCD has noted it is disabled, ignore this request
1652 */
Matt Evansf5960b62011-06-01 10:22:55 +10001653 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1654 cpu_to_le32(EP_STATE_DISABLED)) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001655 le32_to_cpu(ctrl_ctx->drop_flags) &
1656 xhci_get_endpoint_flag(&ep->desc)) {
Hans de Goedea6134132015-01-16 17:54:02 +02001657 /* Do not warn when called after a usb_device_reset */
1658 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
1659 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1660 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001661 return 0;
1662 }
1663
Matt Evans28ccd292011-03-29 13:40:46 +11001664 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1665 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001666
Matt Evans28ccd292011-03-29 13:40:46 +11001667 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1668 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001669
Sarah Sharpf94e01862009-04-27 19:58:38 -07001670 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1671
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001672 if (xhci->quirks & XHCI_MTK_HOST)
1673 xhci_mtk_drop_ep_quirk(hcd, udev, ep);
1674
Julius Wernerd6759132014-06-24 17:14:42 +03001675 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
Sarah Sharpf94e01862009-04-27 19:58:38 -07001676 (unsigned int) ep->desc.bEndpointAddress,
1677 udev->slot_id,
1678 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001679 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001680 return 0;
1681}
1682
1683/* Add an endpoint to a new possible bandwidth configuration for this device.
1684 * Only one call to this function is allowed per endpoint before
1685 * check_bandwidth() or reset_bandwidth() must be called.
1686 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1687 * add the endpoint to the schedule with possibly new parameters denoted by a
1688 * different endpoint descriptor in usb_host_endpoint.
1689 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1690 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001691 *
1692 * The USB core will not allow URBs to be queued to an endpoint until the
1693 * configuration or alt setting is installed in the device, so there's no need
1694 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001695 */
1696int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1697 struct usb_host_endpoint *ep)
1698{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001699 struct xhci_hcd *xhci;
Lin Wang92c96912015-01-09 16:06:27 +02001700 struct xhci_container_ctx *in_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001701 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001702 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001703 u32 added_ctxs;
Julius Wernerd6759132014-06-24 17:14:42 +03001704 u32 new_add_flags, new_drop_flags;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001705 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001706 int ret = 0;
1707
Andiry Xu64927732010-10-14 07:22:45 -07001708 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001709 if (ret <= 0) {
1710 /* So we won't queue a reset ep command for a root hub */
1711 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001712 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001713 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001714 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001715 if (xhci->xhc_state & XHCI_STATE_DYING)
1716 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001717
1718 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001719 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1720 /* FIXME when we have to issue an evaluate endpoint command to
1721 * deal with ep0 max packet size changing once we get the
1722 * descriptors
1723 */
1724 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1725 __func__, added_ctxs);
1726 return 0;
1727 }
1728
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001729 virt_dev = xhci->devs[udev->slot_id];
1730 in_ctx = virt_dev->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001731 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001732 if (!ctrl_ctx) {
1733 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1734 __func__);
1735 return 0;
1736 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001737
Sarah Sharp92f8e762013-04-23 17:11:14 -07001738 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001739 /* If this endpoint is already in use, and the upper layers are trying
1740 * to add it again without dropping it, reject the addition.
1741 */
1742 if (virt_dev->eps[ep_index].ring &&
Lin Wang92c96912015-01-09 16:06:27 +02001743 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001744 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1745 "without dropping it.\n",
1746 (unsigned int) ep->desc.bEndpointAddress);
1747 return -EINVAL;
1748 }
1749
Sarah Sharpf94e01862009-04-27 19:58:38 -07001750 /* If the HCD has already noted the endpoint is enabled,
1751 * ignore this request.
1752 */
Lin Wang92c96912015-01-09 16:06:27 +02001753 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001754 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1755 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001756 return 0;
1757 }
1758
Sarah Sharpf88ba782009-05-14 11:44:22 -07001759 /*
1760 * Configuration and alternate setting changes must be done in
1761 * process context, not interrupt context (or so documenation
1762 * for usb_set_interface() and usb_set_configuration() claim).
1763 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001764 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001765 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1766 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001767 return -ENOMEM;
1768 }
1769
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001770 if (xhci->quirks & XHCI_MTK_HOST) {
1771 ret = xhci_mtk_add_ep_quirk(hcd, udev, ep);
1772 if (ret < 0) {
1773 xhci_free_or_cache_endpoint_ring(xhci,
1774 virt_dev, ep_index);
1775 return ret;
1776 }
1777 }
1778
Matt Evans28ccd292011-03-29 13:40:46 +11001779 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1780 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001781
1782 /* If xhci_endpoint_disable() was called for this endpoint, but the
1783 * xHC hasn't been notified yet through the check_bandwidth() call,
1784 * this re-adds a new state for the endpoint from the new endpoint
1785 * descriptors. We must drop and re-add this endpoint, so we leave the
1786 * drop flags alone.
1787 */
Matt Evans28ccd292011-03-29 13:40:46 +11001788 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001789
Sarah Sharpa1587d92009-07-27 12:03:15 -07001790 /* Store the usb_device pointer for later use */
1791 ep->hcpriv = udev;
1792
Julius Wernerd6759132014-06-24 17:14:42 +03001793 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
Sarah Sharpf94e01862009-04-27 19:58:38 -07001794 (unsigned int) ep->desc.bEndpointAddress,
1795 udev->slot_id,
1796 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001797 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001798 return 0;
1799}
1800
John Yound115b042009-07-27 12:05:15 -07001801static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001802{
John Yound115b042009-07-27 12:05:15 -07001803 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001804 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001805 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001806 int i;
1807
Lin Wang4daf9df2015-01-09 16:06:31 +02001808 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001809 if (!ctrl_ctx) {
1810 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1811 __func__);
1812 return;
1813 }
1814
Sarah Sharpf94e01862009-04-27 19:58:38 -07001815 /* When a device's add flag and drop flag are zero, any subsequent
1816 * configure endpoint command will leave that endpoint's state
1817 * untouched. Make sure we don't leave any old state in the input
1818 * endpoint contexts.
1819 */
John Yound115b042009-07-27 12:05:15 -07001820 ctrl_ctx->drop_flags = 0;
1821 ctrl_ctx->add_flags = 0;
1822 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001823 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001824 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001825 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001826 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001827 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001828 ep_ctx->ep_info = 0;
1829 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001830 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001831 ep_ctx->tx_info = 0;
1832 }
1833}
1834
Sarah Sharpf2217e82009-08-07 14:04:43 -07001835static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001836 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001837{
1838 int ret;
1839
Sarah Sharp913a8a32009-09-04 10:53:13 -07001840 switch (*cmd_status) {
Mathias Nymanc311e392014-05-08 19:26:03 +03001841 case COMP_CMD_ABORT:
1842 case COMP_CMD_STOP:
1843 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
1844 ret = -ETIME;
1845 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001846 case COMP_ENOMEM:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001847 dev_warn(&udev->dev,
1848 "Not enough host controller resources for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001849 ret = -ENOMEM;
1850 /* FIXME: can we allocate more resources for the HC? */
1851 break;
1852 case COMP_BW_ERR:
Hans de Goede71d85722012-01-04 23:29:18 +01001853 case COMP_2ND_BW_ERR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001854 dev_warn(&udev->dev,
1855 "Not enough bandwidth for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001856 ret = -ENOSPC;
1857 /* FIXME: can we go back to the old state? */
1858 break;
1859 case COMP_TRB_ERR:
1860 /* the HCD set up something wrong */
1861 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1862 "add flag = 1, "
1863 "and endpoint is not disabled.\n");
1864 ret = -EINVAL;
1865 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001866 case COMP_DEV_ERR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001867 dev_warn(&udev->dev,
1868 "ERROR: Incompatible device for endpoint configure command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001869 ret = -ENODEV;
1870 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001871 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001872 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1873 "Successful Endpoint Configure command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001874 ret = 0;
1875 break;
1876 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001877 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1878 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001879 ret = -EINVAL;
1880 break;
1881 }
1882 return ret;
1883}
1884
1885static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001886 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001887{
1888 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001889 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001890
Sarah Sharp913a8a32009-09-04 10:53:13 -07001891 switch (*cmd_status) {
Mathias Nymanc311e392014-05-08 19:26:03 +03001892 case COMP_CMD_ABORT:
1893 case COMP_CMD_STOP:
1894 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
1895 ret = -ETIME;
1896 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001897 case COMP_EINVAL:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001898 dev_warn(&udev->dev,
1899 "WARN: xHCI driver setup invalid evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001900 ret = -EINVAL;
1901 break;
1902 case COMP_EBADSLT:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001903 dev_warn(&udev->dev,
1904 "WARN: slot not enabled for evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001905 ret = -EINVAL;
1906 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001907 case COMP_CTX_STATE:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001908 dev_warn(&udev->dev,
1909 "WARN: invalid context state for evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001910 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1911 ret = -EINVAL;
1912 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001913 case COMP_DEV_ERR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001914 dev_warn(&udev->dev,
1915 "ERROR: Incompatible device for evaluate context command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001916 ret = -ENODEV;
1917 break;
Alex He1bb73a82011-05-05 18:14:12 +08001918 case COMP_MEL_ERR:
1919 /* Max Exit Latency too large error */
1920 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1921 ret = -EINVAL;
1922 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001923 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001924 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1925 "Successful evaluate context command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001926 ret = 0;
1927 break;
1928 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001929 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1930 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001931 ret = -EINVAL;
1932 break;
1933 }
1934 return ret;
1935}
1936
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001937static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001938 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001939{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001940 u32 valid_add_flags;
1941 u32 valid_drop_flags;
1942
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001943 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1944 * (bit 1). The default control endpoint is added during the Address
1945 * Device command and is never removed until the slot is disabled.
1946 */
Xenia Ragiadakouef734002013-09-09 21:03:06 +03001947 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1948 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001949
1950 /* Use hweight32 to count the number of ones in the add flags, or
1951 * number of endpoints added. Don't count endpoints that are changed
1952 * (both added and dropped).
1953 */
1954 return hweight32(valid_add_flags) -
1955 hweight32(valid_add_flags & valid_drop_flags);
1956}
1957
1958static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001959 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001960{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001961 u32 valid_add_flags;
1962 u32 valid_drop_flags;
1963
Xenia Ragiadakou78d1ff02013-09-09 21:03:07 +03001964 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1965 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001966
1967 return hweight32(valid_drop_flags) -
1968 hweight32(valid_add_flags & valid_drop_flags);
1969}
1970
1971/*
1972 * We need to reserve the new number of endpoints before the configure endpoint
1973 * command completes. We can't subtract the dropped endpoints from the number
1974 * of active endpoints until the command completes because we can oversubscribe
1975 * the host in this case:
1976 *
1977 * - the first configure endpoint command drops more endpoints than it adds
1978 * - a second configure endpoint command that adds more endpoints is queued
1979 * - the first configure endpoint command fails, so the config is unchanged
1980 * - the second command may succeed, even though there isn't enough resources
1981 *
1982 * Must be called with xhci->lock held.
1983 */
1984static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001985 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001986{
1987 u32 added_eps;
1988
Sarah Sharp92f8e762013-04-23 17:11:14 -07001989 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001990 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001991 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1992 "Not enough ep ctxs: "
1993 "%u active, need to add %u, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001994 xhci->num_active_eps, added_eps,
1995 xhci->limit_active_eps);
1996 return -ENOMEM;
1997 }
1998 xhci->num_active_eps += added_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001999 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2000 "Adding %u ep ctxs, %u now active.", added_eps,
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002001 xhci->num_active_eps);
2002 return 0;
2003}
2004
2005/*
2006 * The configure endpoint was failed by the xHC for some other reason, so we
2007 * need to revert the resources that failed configuration would have used.
2008 *
2009 * Must be called with xhci->lock held.
2010 */
2011static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002012 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002013{
2014 u32 num_failed_eps;
2015
Sarah Sharp92f8e762013-04-23 17:11:14 -07002016 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002017 xhci->num_active_eps -= num_failed_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002018 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2019 "Removing %u failed ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002020 num_failed_eps,
2021 xhci->num_active_eps);
2022}
2023
2024/*
2025 * Now that the command has completed, clean up the active endpoint count by
2026 * subtracting out the endpoints that were dropped (but not changed).
2027 *
2028 * Must be called with xhci->lock held.
2029 */
2030static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002031 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002032{
2033 u32 num_dropped_eps;
2034
Sarah Sharp92f8e762013-04-23 17:11:14 -07002035 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002036 xhci->num_active_eps -= num_dropped_eps;
2037 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002038 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2039 "Removing %u dropped ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002040 num_dropped_eps,
2041 xhci->num_active_eps);
2042}
2043
Felipe Balbied384bd2012-08-07 14:10:03 +03002044static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002045{
2046 switch (udev->speed) {
2047 case USB_SPEED_LOW:
2048 case USB_SPEED_FULL:
2049 return FS_BLOCK;
2050 case USB_SPEED_HIGH:
2051 return HS_BLOCK;
2052 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002053 case USB_SPEED_SUPER_PLUS:
Sarah Sharpc29eea62011-09-02 11:05:52 -07002054 return SS_BLOCK;
2055 case USB_SPEED_UNKNOWN:
2056 case USB_SPEED_WIRELESS:
2057 default:
2058 /* Should never happen */
2059 return 1;
2060 }
2061}
2062
Felipe Balbied384bd2012-08-07 14:10:03 +03002063static unsigned int
2064xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002065{
2066 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2067 return LS_OVERHEAD;
2068 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2069 return FS_OVERHEAD;
2070 return HS_OVERHEAD;
2071}
2072
2073/* If we are changing a LS/FS device under a HS hub,
2074 * make sure (if we are activating a new TT) that the HS bus has enough
2075 * bandwidth for this new TT.
2076 */
2077static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2078 struct xhci_virt_device *virt_dev,
2079 int old_active_eps)
2080{
2081 struct xhci_interval_bw_table *bw_table;
2082 struct xhci_tt_bw_info *tt_info;
2083
2084 /* Find the bandwidth table for the root port this TT is attached to. */
2085 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2086 tt_info = virt_dev->tt_info;
2087 /* If this TT already had active endpoints, the bandwidth for this TT
2088 * has already been added. Removing all periodic endpoints (and thus
2089 * making the TT enactive) will only decrease the bandwidth used.
2090 */
2091 if (old_active_eps)
2092 return 0;
2093 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2094 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2095 return -ENOMEM;
2096 return 0;
2097 }
2098 /* Not sure why we would have no new active endpoints...
2099 *
2100 * Maybe because of an Evaluate Context change for a hub update or a
2101 * control endpoint 0 max packet size change?
2102 * FIXME: skip the bandwidth calculation in that case.
2103 */
2104 return 0;
2105}
2106
Sarah Sharp2b698992011-09-13 16:41:13 -07002107static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2108 struct xhci_virt_device *virt_dev)
2109{
2110 unsigned int bw_reserved;
2111
2112 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2113 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2114 return -ENOMEM;
2115
2116 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2117 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2118 return -ENOMEM;
2119
2120 return 0;
2121}
2122
Sarah Sharpc29eea62011-09-02 11:05:52 -07002123/*
2124 * This algorithm is a very conservative estimate of the worst-case scheduling
2125 * scenario for any one interval. The hardware dynamically schedules the
2126 * packets, so we can't tell which microframe could be the limiting factor in
2127 * the bandwidth scheduling. This only takes into account periodic endpoints.
2128 *
2129 * Obviously, we can't solve an NP complete problem to find the minimum worst
2130 * case scenario. Instead, we come up with an estimate that is no less than
2131 * the worst case bandwidth used for any one microframe, but may be an
2132 * over-estimate.
2133 *
2134 * We walk the requirements for each endpoint by interval, starting with the
2135 * smallest interval, and place packets in the schedule where there is only one
2136 * possible way to schedule packets for that interval. In order to simplify
2137 * this algorithm, we record the largest max packet size for each interval, and
2138 * assume all packets will be that size.
2139 *
2140 * For interval 0, we obviously must schedule all packets for each interval.
2141 * The bandwidth for interval 0 is just the amount of data to be transmitted
2142 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2143 * the number of packets).
2144 *
2145 * For interval 1, we have two possible microframes to schedule those packets
2146 * in. For this algorithm, if we can schedule the same number of packets for
2147 * each possible scheduling opportunity (each microframe), we will do so. The
2148 * remaining number of packets will be saved to be transmitted in the gaps in
2149 * the next interval's scheduling sequence.
2150 *
2151 * As we move those remaining packets to be scheduled with interval 2 packets,
2152 * we have to double the number of remaining packets to transmit. This is
2153 * because the intervals are actually powers of 2, and we would be transmitting
2154 * the previous interval's packets twice in this interval. We also have to be
2155 * sure that when we look at the largest max packet size for this interval, we
2156 * also look at the largest max packet size for the remaining packets and take
2157 * the greater of the two.
2158 *
2159 * The algorithm continues to evenly distribute packets in each scheduling
2160 * opportunity, and push the remaining packets out, until we get to the last
2161 * interval. Then those packets and their associated overhead are just added
2162 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002163 */
2164static int xhci_check_bw_table(struct xhci_hcd *xhci,
2165 struct xhci_virt_device *virt_dev,
2166 int old_active_eps)
2167{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002168 unsigned int bw_reserved;
2169 unsigned int max_bandwidth;
2170 unsigned int bw_used;
2171 unsigned int block_size;
2172 struct xhci_interval_bw_table *bw_table;
2173 unsigned int packet_size = 0;
2174 unsigned int overhead = 0;
2175 unsigned int packets_transmitted = 0;
2176 unsigned int packets_remaining = 0;
2177 unsigned int i;
2178
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002179 if (virt_dev->udev->speed >= USB_SPEED_SUPER)
Sarah Sharp2b698992011-09-13 16:41:13 -07002180 return xhci_check_ss_bw(xhci, virt_dev);
2181
Sarah Sharpc29eea62011-09-02 11:05:52 -07002182 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2183 max_bandwidth = HS_BW_LIMIT;
2184 /* Convert percent of bus BW reserved to blocks reserved */
2185 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2186 } else {
2187 max_bandwidth = FS_BW_LIMIT;
2188 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2189 }
2190
2191 bw_table = virt_dev->bw_table;
2192 /* We need to translate the max packet size and max ESIT payloads into
2193 * the units the hardware uses.
2194 */
2195 block_size = xhci_get_block_size(virt_dev->udev);
2196
2197 /* If we are manipulating a LS/FS device under a HS hub, double check
2198 * that the HS bus has enough bandwidth if we are activing a new TT.
2199 */
2200 if (virt_dev->tt_info) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002201 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2202 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002203 virt_dev->real_port);
2204 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2205 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2206 "newly activated TT.\n");
2207 return -ENOMEM;
2208 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002209 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2210 "Recalculating BW for TT slot %u port %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002211 virt_dev->tt_info->slot_id,
2212 virt_dev->tt_info->ttport);
2213 } else {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002214 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2215 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002216 virt_dev->real_port);
2217 }
2218
2219 /* Add in how much bandwidth will be used for interval zero, or the
2220 * rounded max ESIT payload + number of packets * largest overhead.
2221 */
2222 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2223 bw_table->interval_bw[0].num_packets *
2224 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2225
2226 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2227 unsigned int bw_added;
2228 unsigned int largest_mps;
2229 unsigned int interval_overhead;
2230
2231 /*
2232 * How many packets could we transmit in this interval?
2233 * If packets didn't fit in the previous interval, we will need
2234 * to transmit that many packets twice within this interval.
2235 */
2236 packets_remaining = 2 * packets_remaining +
2237 bw_table->interval_bw[i].num_packets;
2238
2239 /* Find the largest max packet size of this or the previous
2240 * interval.
2241 */
2242 if (list_empty(&bw_table->interval_bw[i].endpoints))
2243 largest_mps = 0;
2244 else {
2245 struct xhci_virt_ep *virt_ep;
2246 struct list_head *ep_entry;
2247
2248 ep_entry = bw_table->interval_bw[i].endpoints.next;
2249 virt_ep = list_entry(ep_entry,
2250 struct xhci_virt_ep, bw_endpoint_list);
2251 /* Convert to blocks, rounding up */
2252 largest_mps = DIV_ROUND_UP(
2253 virt_ep->bw_info.max_packet_size,
2254 block_size);
2255 }
2256 if (largest_mps > packet_size)
2257 packet_size = largest_mps;
2258
2259 /* Use the larger overhead of this or the previous interval. */
2260 interval_overhead = xhci_get_largest_overhead(
2261 &bw_table->interval_bw[i]);
2262 if (interval_overhead > overhead)
2263 overhead = interval_overhead;
2264
2265 /* How many packets can we evenly distribute across
2266 * (1 << (i + 1)) possible scheduling opportunities?
2267 */
2268 packets_transmitted = packets_remaining >> (i + 1);
2269
2270 /* Add in the bandwidth used for those scheduled packets */
2271 bw_added = packets_transmitted * (overhead + packet_size);
2272
2273 /* How many packets do we have remaining to transmit? */
2274 packets_remaining = packets_remaining % (1 << (i + 1));
2275
2276 /* What largest max packet size should those packets have? */
2277 /* If we've transmitted all packets, don't carry over the
2278 * largest packet size.
2279 */
2280 if (packets_remaining == 0) {
2281 packet_size = 0;
2282 overhead = 0;
2283 } else if (packets_transmitted > 0) {
2284 /* Otherwise if we do have remaining packets, and we've
2285 * scheduled some packets in this interval, take the
2286 * largest max packet size from endpoints with this
2287 * interval.
2288 */
2289 packet_size = largest_mps;
2290 overhead = interval_overhead;
2291 }
2292 /* Otherwise carry over packet_size and overhead from the last
2293 * time we had a remainder.
2294 */
2295 bw_used += bw_added;
2296 if (bw_used > max_bandwidth) {
2297 xhci_warn(xhci, "Not enough bandwidth. "
2298 "Proposed: %u, Max: %u\n",
2299 bw_used, max_bandwidth);
2300 return -ENOMEM;
2301 }
2302 }
2303 /*
2304 * Ok, we know we have some packets left over after even-handedly
2305 * scheduling interval 15. We don't know which microframes they will
2306 * fit into, so we over-schedule and say they will be scheduled every
2307 * microframe.
2308 */
2309 if (packets_remaining > 0)
2310 bw_used += overhead + packet_size;
2311
2312 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2313 unsigned int port_index = virt_dev->real_port - 1;
2314
2315 /* OK, we're manipulating a HS device attached to a
2316 * root port bandwidth domain. Include the number of active TTs
2317 * in the bandwidth used.
2318 */
2319 bw_used += TT_HS_OVERHEAD *
2320 xhci->rh_bw[port_index].num_active_tts;
2321 }
2322
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002323 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2324 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2325 "Available: %u " "percent",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002326 bw_used, max_bandwidth, bw_reserved,
2327 (max_bandwidth - bw_used - bw_reserved) * 100 /
2328 max_bandwidth);
2329
2330 bw_used += bw_reserved;
2331 if (bw_used > max_bandwidth) {
2332 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2333 bw_used, max_bandwidth);
2334 return -ENOMEM;
2335 }
2336
2337 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002338 return 0;
2339}
2340
2341static bool xhci_is_async_ep(unsigned int ep_type)
2342{
2343 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2344 ep_type != ISOC_IN_EP &&
2345 ep_type != INT_IN_EP);
2346}
2347
Sarah Sharp2b698992011-09-13 16:41:13 -07002348static bool xhci_is_sync_in_ep(unsigned int ep_type)
2349{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002350 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002351}
2352
2353static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2354{
2355 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2356
2357 if (ep_bw->ep_interval == 0)
2358 return SS_OVERHEAD_BURST +
2359 (ep_bw->mult * ep_bw->num_packets *
2360 (SS_OVERHEAD + mps));
2361 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2362 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2363 1 << ep_bw->ep_interval);
2364
2365}
2366
Sarah Sharp2e279802011-09-02 11:05:50 -07002367void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2368 struct xhci_bw_info *ep_bw,
2369 struct xhci_interval_bw_table *bw_table,
2370 struct usb_device *udev,
2371 struct xhci_virt_ep *virt_ep,
2372 struct xhci_tt_bw_info *tt_info)
2373{
2374 struct xhci_interval_bw *interval_bw;
2375 int normalized_interval;
2376
Sarah Sharp2b698992011-09-13 16:41:13 -07002377 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002378 return;
2379
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002380 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharp2b698992011-09-13 16:41:13 -07002381 if (xhci_is_sync_in_ep(ep_bw->type))
2382 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2383 xhci_get_ss_bw_consumed(ep_bw);
2384 else
2385 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2386 xhci_get_ss_bw_consumed(ep_bw);
2387 return;
2388 }
2389
2390 /* SuperSpeed endpoints never get added to intervals in the table, so
2391 * this check is only valid for HS/FS/LS devices.
2392 */
2393 if (list_empty(&virt_ep->bw_endpoint_list))
2394 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002395 /* For LS/FS devices, we need to translate the interval expressed in
2396 * microframes to frames.
2397 */
2398 if (udev->speed == USB_SPEED_HIGH)
2399 normalized_interval = ep_bw->ep_interval;
2400 else
2401 normalized_interval = ep_bw->ep_interval - 3;
2402
2403 if (normalized_interval == 0)
2404 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2405 interval_bw = &bw_table->interval_bw[normalized_interval];
2406 interval_bw->num_packets -= ep_bw->num_packets;
2407 switch (udev->speed) {
2408 case USB_SPEED_LOW:
2409 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2410 break;
2411 case USB_SPEED_FULL:
2412 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2413 break;
2414 case USB_SPEED_HIGH:
2415 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2416 break;
2417 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002418 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002419 case USB_SPEED_UNKNOWN:
2420 case USB_SPEED_WIRELESS:
2421 /* Should never happen because only LS/FS/HS endpoints will get
2422 * added to the endpoint list.
2423 */
2424 return;
2425 }
2426 if (tt_info)
2427 tt_info->active_eps -= 1;
2428 list_del_init(&virt_ep->bw_endpoint_list);
2429}
2430
2431static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2432 struct xhci_bw_info *ep_bw,
2433 struct xhci_interval_bw_table *bw_table,
2434 struct usb_device *udev,
2435 struct xhci_virt_ep *virt_ep,
2436 struct xhci_tt_bw_info *tt_info)
2437{
2438 struct xhci_interval_bw *interval_bw;
2439 struct xhci_virt_ep *smaller_ep;
2440 int normalized_interval;
2441
2442 if (xhci_is_async_ep(ep_bw->type))
2443 return;
2444
Sarah Sharp2b698992011-09-13 16:41:13 -07002445 if (udev->speed == USB_SPEED_SUPER) {
2446 if (xhci_is_sync_in_ep(ep_bw->type))
2447 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2448 xhci_get_ss_bw_consumed(ep_bw);
2449 else
2450 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2451 xhci_get_ss_bw_consumed(ep_bw);
2452 return;
2453 }
2454
Sarah Sharp2e279802011-09-02 11:05:50 -07002455 /* For LS/FS devices, we need to translate the interval expressed in
2456 * microframes to frames.
2457 */
2458 if (udev->speed == USB_SPEED_HIGH)
2459 normalized_interval = ep_bw->ep_interval;
2460 else
2461 normalized_interval = ep_bw->ep_interval - 3;
2462
2463 if (normalized_interval == 0)
2464 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2465 interval_bw = &bw_table->interval_bw[normalized_interval];
2466 interval_bw->num_packets += ep_bw->num_packets;
2467 switch (udev->speed) {
2468 case USB_SPEED_LOW:
2469 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2470 break;
2471 case USB_SPEED_FULL:
2472 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2473 break;
2474 case USB_SPEED_HIGH:
2475 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2476 break;
2477 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002478 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002479 case USB_SPEED_UNKNOWN:
2480 case USB_SPEED_WIRELESS:
2481 /* Should never happen because only LS/FS/HS endpoints will get
2482 * added to the endpoint list.
2483 */
2484 return;
2485 }
2486
2487 if (tt_info)
2488 tt_info->active_eps += 1;
2489 /* Insert the endpoint into the list, largest max packet size first. */
2490 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2491 bw_endpoint_list) {
2492 if (ep_bw->max_packet_size >=
2493 smaller_ep->bw_info.max_packet_size) {
2494 /* Add the new ep before the smaller endpoint */
2495 list_add_tail(&virt_ep->bw_endpoint_list,
2496 &smaller_ep->bw_endpoint_list);
2497 return;
2498 }
2499 }
2500 /* Add the new endpoint at the end of the list. */
2501 list_add_tail(&virt_ep->bw_endpoint_list,
2502 &interval_bw->endpoints);
2503}
2504
2505void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2506 struct xhci_virt_device *virt_dev,
2507 int old_active_eps)
2508{
2509 struct xhci_root_port_bw_info *rh_bw_info;
2510 if (!virt_dev->tt_info)
2511 return;
2512
2513 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2514 if (old_active_eps == 0 &&
2515 virt_dev->tt_info->active_eps != 0) {
2516 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002517 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002518 } else if (old_active_eps != 0 &&
2519 virt_dev->tt_info->active_eps == 0) {
2520 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002521 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002522 }
2523}
2524
2525static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2526 struct xhci_virt_device *virt_dev,
2527 struct xhci_container_ctx *in_ctx)
2528{
2529 struct xhci_bw_info ep_bw_info[31];
2530 int i;
2531 struct xhci_input_control_ctx *ctrl_ctx;
2532 int old_active_eps = 0;
2533
Sarah Sharp2e279802011-09-02 11:05:50 -07002534 if (virt_dev->tt_info)
2535 old_active_eps = virt_dev->tt_info->active_eps;
2536
Lin Wang4daf9df2015-01-09 16:06:31 +02002537 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002538 if (!ctrl_ctx) {
2539 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2540 __func__);
2541 return -ENOMEM;
2542 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002543
2544 for (i = 0; i < 31; i++) {
2545 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2546 continue;
2547
2548 /* Make a copy of the BW info in case we need to revert this */
2549 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2550 sizeof(ep_bw_info[i]));
2551 /* Drop the endpoint from the interval table if the endpoint is
2552 * being dropped or changed.
2553 */
2554 if (EP_IS_DROPPED(ctrl_ctx, i))
2555 xhci_drop_ep_from_interval_table(xhci,
2556 &virt_dev->eps[i].bw_info,
2557 virt_dev->bw_table,
2558 virt_dev->udev,
2559 &virt_dev->eps[i],
2560 virt_dev->tt_info);
2561 }
2562 /* Overwrite the information stored in the endpoints' bw_info */
2563 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2564 for (i = 0; i < 31; i++) {
2565 /* Add any changed or added endpoints to the interval table */
2566 if (EP_IS_ADDED(ctrl_ctx, i))
2567 xhci_add_ep_to_interval_table(xhci,
2568 &virt_dev->eps[i].bw_info,
2569 virt_dev->bw_table,
2570 virt_dev->udev,
2571 &virt_dev->eps[i],
2572 virt_dev->tt_info);
2573 }
2574
2575 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2576 /* Ok, this fits in the bandwidth we have.
2577 * Update the number of active TTs.
2578 */
2579 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2580 return 0;
2581 }
2582
2583 /* We don't have enough bandwidth for this, revert the stored info. */
2584 for (i = 0; i < 31; i++) {
2585 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2586 continue;
2587
2588 /* Drop the new copies of any added or changed endpoints from
2589 * the interval table.
2590 */
2591 if (EP_IS_ADDED(ctrl_ctx, i)) {
2592 xhci_drop_ep_from_interval_table(xhci,
2593 &virt_dev->eps[i].bw_info,
2594 virt_dev->bw_table,
2595 virt_dev->udev,
2596 &virt_dev->eps[i],
2597 virt_dev->tt_info);
2598 }
2599 /* Revert the endpoint back to its old information */
2600 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2601 sizeof(ep_bw_info[i]));
2602 /* Add any changed or dropped endpoints back into the table */
2603 if (EP_IS_DROPPED(ctrl_ctx, i))
2604 xhci_add_ep_to_interval_table(xhci,
2605 &virt_dev->eps[i].bw_info,
2606 virt_dev->bw_table,
2607 virt_dev->udev,
2608 &virt_dev->eps[i],
2609 virt_dev->tt_info);
2610 }
2611 return -ENOMEM;
2612}
2613
2614
Sarah Sharpf2217e82009-08-07 14:04:43 -07002615/* Issue a configure endpoint command or evaluate context command
2616 * and wait for it to finish.
2617 */
2618static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002619 struct usb_device *udev,
2620 struct xhci_command *command,
2621 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002622{
2623 int ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002624 unsigned long flags;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002625 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002626 struct xhci_virt_device *virt_dev;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002627
2628 if (!command)
2629 return -EINVAL;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002630
2631 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002632 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002633
Lin Wang4daf9df2015-01-09 16:06:31 +02002634 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002635 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002636 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002637 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2638 __func__);
2639 return -ENOMEM;
2640 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002641
2642 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002643 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002644 spin_unlock_irqrestore(&xhci->lock, flags);
2645 xhci_warn(xhci, "Not enough host resources, "
2646 "active endpoint contexts = %u\n",
2647 xhci->num_active_eps);
2648 return -ENOMEM;
2649 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002650 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002651 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
Sarah Sharp2e279802011-09-02 11:05:50 -07002652 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002653 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002654 spin_unlock_irqrestore(&xhci->lock, flags);
2655 xhci_warn(xhci, "Not enough bandwidth\n");
2656 return -ENOMEM;
2657 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002658
Sarah Sharpf2217e82009-08-07 14:04:43 -07002659 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002660 ret = xhci_queue_configure_endpoint(xhci, command,
2661 command->in_ctx->dma,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002662 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002663 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002664 ret = xhci_queue_evaluate_context(xhci, command,
2665 command->in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002666 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002667 if (ret < 0) {
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002668 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002669 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002670 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03002671 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2672 "FIXME allocate a new ring segment");
Sarah Sharpf2217e82009-08-07 14:04:43 -07002673 return -ENOMEM;
2674 }
2675 xhci_ring_cmd_db(xhci);
2676 spin_unlock_irqrestore(&xhci->lock, flags);
2677
2678 /* Wait for the configure endpoint command to complete */
Mathias Nymanc311e392014-05-08 19:26:03 +03002679 wait_for_completion(command->completion);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002680
2681 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002682 ret = xhci_configure_endpoint_result(xhci, udev,
2683 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002684 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002685 ret = xhci_evaluate_context_result(xhci, udev,
2686 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002687
2688 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2689 spin_lock_irqsave(&xhci->lock, flags);
2690 /* If the command failed, remove the reserved resources.
2691 * Otherwise, clean up the estimate to include dropped eps.
2692 */
2693 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002694 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002695 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002696 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002697 spin_unlock_irqrestore(&xhci->lock, flags);
2698 }
2699 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002700}
2701
Hans de Goededf613832013-10-04 00:29:45 +02002702static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2703 struct xhci_virt_device *vdev, int i)
2704{
2705 struct xhci_virt_ep *ep = &vdev->eps[i];
2706
2707 if (ep->ep_state & EP_HAS_STREAMS) {
2708 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2709 xhci_get_endpoint_address(i));
2710 xhci_free_stream_info(xhci, ep->stream_info);
2711 ep->stream_info = NULL;
2712 ep->ep_state &= ~EP_HAS_STREAMS;
2713 }
2714}
2715
Sarah Sharpf88ba782009-05-14 11:44:22 -07002716/* Called after one or more calls to xhci_add_endpoint() or
2717 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2718 * to call xhci_reset_bandwidth().
2719 *
2720 * Since we are in the middle of changing either configuration or
2721 * installing a new alt setting, the USB core won't allow URBs to be
2722 * enqueued for any endpoint on the old config or interface. Nothing
2723 * else should be touching the xhci->devs[slot_id] structure, so we
2724 * don't need to take the xhci->lock for manipulating that.
2725 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002726int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2727{
2728 int i;
2729 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002730 struct xhci_hcd *xhci;
2731 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002732 struct xhci_input_control_ctx *ctrl_ctx;
2733 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002734 struct xhci_command *command;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002735
Andiry Xu64927732010-10-14 07:22:45 -07002736 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002737 if (ret <= 0)
2738 return ret;
2739 xhci = hcd_to_xhci(hcd);
Mathias Nyman98d74f92016-04-08 16:25:10 +03002740 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
2741 (xhci->xhc_state & XHCI_STATE_REMOVING))
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002742 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002743
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002744 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002745 virt_dev = xhci->devs[udev->slot_id];
2746
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002747 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
2748 if (!command)
2749 return -ENOMEM;
2750
2751 command->in_ctx = virt_dev->in_ctx;
2752
Sarah Sharpf94e01862009-04-27 19:58:38 -07002753 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
Lin Wang4daf9df2015-01-09 16:06:31 +02002754 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002755 if (!ctrl_ctx) {
2756 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2757 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002758 ret = -ENOMEM;
2759 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002760 }
Matt Evans28ccd292011-03-29 13:40:46 +11002761 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2762 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2763 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002764
2765 /* Don't issue the command if there's no endpoints to update. */
2766 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002767 ctrl_ctx->drop_flags == 0) {
2768 ret = 0;
2769 goto command_cleanup;
2770 }
Julius Wernerd6759132014-06-24 17:14:42 +03002771 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
John Yound115b042009-07-27 12:05:15 -07002772 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Julius Wernerd6759132014-06-24 17:14:42 +03002773 for (i = 31; i >= 1; i--) {
2774 __le32 le32 = cpu_to_le32(BIT(i));
2775
2776 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
2777 || (ctrl_ctx->add_flags & le32) || i == 1) {
2778 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
2779 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
2780 break;
2781 }
2782 }
2783 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07002784 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002785 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002786
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002787 ret = xhci_configure_endpoint(xhci, udev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002788 false, false);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002789 if (ret)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002790 /* Callee should call reset_bandwidth() */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002791 goto command_cleanup;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002792
2793 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07002794 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002795 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002796
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002797 /* Free any rings that were dropped, but not changed. */
2798 for (i = 1; i < 31; ++i) {
Matt Evans4819fef2011-06-01 13:01:07 +10002799 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
Hans de Goededf613832013-10-04 00:29:45 +02002800 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002801 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Hans de Goededf613832013-10-04 00:29:45 +02002802 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2803 }
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002804 }
John Yound115b042009-07-27 12:05:15 -07002805 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002806 /*
2807 * Install any rings for completely new endpoints or changed endpoints,
2808 * and free or cache any old rings from changed endpoints.
2809 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002810 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002811 if (!virt_dev->eps[i].new_ring)
2812 continue;
2813 /* Only cache or free the old ring if it exists.
2814 * It may not if this is the first add of an endpoint.
2815 */
2816 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08002817 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002818 }
Hans de Goededf613832013-10-04 00:29:45 +02002819 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002820 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2821 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002822 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002823command_cleanup:
2824 kfree(command->completion);
2825 kfree(command);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002826
Sarah Sharpf94e01862009-04-27 19:58:38 -07002827 return ret;
2828}
2829
2830void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2831{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002832 struct xhci_hcd *xhci;
2833 struct xhci_virt_device *virt_dev;
2834 int i, ret;
2835
Andiry Xu64927732010-10-14 07:22:45 -07002836 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002837 if (ret <= 0)
2838 return;
2839 xhci = hcd_to_xhci(hcd);
2840
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002841 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002842 virt_dev = xhci->devs[udev->slot_id];
2843 /* Free any rings allocated for added endpoints */
2844 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002845 if (virt_dev->eps[i].new_ring) {
2846 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2847 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002848 }
2849 }
John Yound115b042009-07-27 12:05:15 -07002850 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002851}
2852
Sarah Sharp5270b952009-09-04 10:53:11 -07002853static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002854 struct xhci_container_ctx *in_ctx,
2855 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002856 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002857 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002858{
Matt Evans28ccd292011-03-29 13:40:46 +11002859 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2860 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002861 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002862 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002863
Sarah Sharp913a8a32009-09-04 10:53:13 -07002864 xhci_dbg(xhci, "Input Context:\n");
2865 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07002866}
2867
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002868static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002869 unsigned int slot_id, unsigned int ep_index,
2870 struct xhci_dequeue_state *deq_state)
2871{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002872 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002873 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002874 struct xhci_ep_ctx *ep_ctx;
2875 u32 added_ctxs;
2876 dma_addr_t addr;
2877
Sarah Sharp92f8e762013-04-23 17:11:14 -07002878 in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02002879 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002880 if (!ctrl_ctx) {
2881 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2882 __func__);
2883 return;
2884 }
2885
Sarah Sharp913a8a32009-09-04 10:53:13 -07002886 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2887 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002888 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2889 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2890 deq_state->new_deq_ptr);
2891 if (addr == 0) {
2892 xhci_warn(xhci, "WARN Cannot submit config ep after "
2893 "reset ep command\n");
2894 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2895 deq_state->new_deq_seg,
2896 deq_state->new_deq_ptr);
2897 return;
2898 }
Matt Evans28ccd292011-03-29 13:40:46 +11002899 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002900
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002901 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002902 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002903 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2904 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002905}
2906
Sarah Sharp82d10092009-08-07 14:04:52 -07002907void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Mathias Nymand97b4f82014-11-27 18:19:16 +02002908 unsigned int ep_index, struct xhci_td *td)
Sarah Sharp82d10092009-08-07 14:04:52 -07002909{
2910 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002911 struct xhci_virt_ep *ep;
Mathias Nymand97b4f82014-11-27 18:19:16 +02002912 struct usb_device *udev = td->urb->dev;
Sarah Sharp82d10092009-08-07 14:04:52 -07002913
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002914 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2915 "Cleaning up stalled endpoint ring");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002916 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002917 /* We need to move the HW's dequeue pointer past this TD,
2918 * or it will attempt to resend it on the next doorbell ring.
2919 */
2920 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Mathias Nymand97b4f82014-11-27 18:19:16 +02002921 ep_index, ep->stopped_stream, td, &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002922
Mathias Nyman365038d2014-08-19 15:17:58 +03002923 if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
2924 return;
2925
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002926 /* HW with the reset endpoint quirk will use the saved dequeue state to
2927 * issue a configure endpoint command later.
2928 */
2929 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002930 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2931 "Queueing new dequeue state");
Hans de Goede1e3452e2014-08-20 16:41:52 +03002932 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002933 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002934 } else {
2935 /* Better hope no one uses the input context between now and the
2936 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002937 * XXX: No idea how this hardware will react when stream rings
2938 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002939 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002940 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2941 "Setting up input context for "
2942 "configure endpoint command");
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002943 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2944 ep_index, &deq_state);
2945 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002946}
2947
Mathias Nymand0167ad2015-03-10 19:49:00 +02002948/* Called when clearing halted device. The core should have sent the control
Mathias Nyman8e71a322014-11-18 11:27:12 +02002949 * message to clear the device halt condition. The host side of the halt should
Mathias Nymand0167ad2015-03-10 19:49:00 +02002950 * already be cleared with a reset endpoint command issued when the STALL tx
2951 * event was received.
2952 *
2953 * Context: in_interrupt
Sarah Sharpa1587d92009-07-27 12:03:15 -07002954 */
Mathias Nyman8e71a322014-11-18 11:27:12 +02002955
Sarah Sharpa1587d92009-07-27 12:03:15 -07002956void xhci_endpoint_reset(struct usb_hcd *hcd,
2957 struct usb_host_endpoint *ep)
2958{
2959 struct xhci_hcd *xhci;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002960
2961 xhci = hcd_to_xhci(hcd);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002962
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002963 /*
Mathias Nymand0167ad2015-03-10 19:49:00 +02002964 * We might need to implement the config ep cmd in xhci 4.8.1 note:
Mathias Nyman8e71a322014-11-18 11:27:12 +02002965 * The Reset Endpoint Command may only be issued to endpoints in the
2966 * Halted state. If software wishes reset the Data Toggle or Sequence
2967 * Number of an endpoint that isn't in the Halted state, then software
2968 * may issue a Configure Endpoint Command with the Drop and Add bits set
2969 * for the target endpoint. that is in the Stopped state.
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002970 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002971
Mathias Nymand0167ad2015-03-10 19:49:00 +02002972 /* For now just print debug to follow the situation */
2973 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2974 ep->desc.bEndpointAddress);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002975}
2976
Sarah Sharp8df75f42010-04-02 15:34:16 -07002977static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2978 struct usb_device *udev, struct usb_host_endpoint *ep,
2979 unsigned int slot_id)
2980{
2981 int ret;
2982 unsigned int ep_index;
2983 unsigned int ep_state;
2984
2985 if (!ep)
2986 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002987 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002988 if (ret <= 0)
2989 return -EINVAL;
Hans de Goedea3901532013-10-04 17:05:55 +02002990 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002991 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2992 " descriptor for ep 0x%x does not support streams\n",
2993 ep->desc.bEndpointAddress);
2994 return -EINVAL;
2995 }
2996
2997 ep_index = xhci_get_endpoint_index(&ep->desc);
2998 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2999 if (ep_state & EP_HAS_STREAMS ||
3000 ep_state & EP_GETTING_STREAMS) {
3001 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
3002 "already has streams set up.\n",
3003 ep->desc.bEndpointAddress);
3004 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
3005 "dynamic stream context array reallocation.\n");
3006 return -EINVAL;
3007 }
3008 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
3009 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
3010 "endpoint 0x%x; URBs are pending.\n",
3011 ep->desc.bEndpointAddress);
3012 return -EINVAL;
3013 }
3014 return 0;
3015}
3016
3017static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3018 unsigned int *num_streams, unsigned int *num_stream_ctxs)
3019{
3020 unsigned int max_streams;
3021
3022 /* The stream context array size must be a power of two */
3023 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3024 /*
3025 * Find out how many primary stream array entries the host controller
3026 * supports. Later we may use secondary stream arrays (similar to 2nd
3027 * level page entries), but that's an optional feature for xHCI host
3028 * controllers. xHCs must support at least 4 stream IDs.
3029 */
3030 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3031 if (*num_stream_ctxs > max_streams) {
3032 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3033 max_streams);
3034 *num_stream_ctxs = max_streams;
3035 *num_streams = max_streams;
3036 }
3037}
3038
3039/* Returns an error code if one of the endpoint already has streams.
3040 * This does not change any data structures, it only checks and gathers
3041 * information.
3042 */
3043static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3044 struct usb_device *udev,
3045 struct usb_host_endpoint **eps, unsigned int num_eps,
3046 unsigned int *num_streams, u32 *changed_ep_bitmask)
3047{
Sarah Sharp8df75f42010-04-02 15:34:16 -07003048 unsigned int max_streams;
3049 unsigned int endpoint_flag;
3050 int i;
3051 int ret;
3052
3053 for (i = 0; i < num_eps; i++) {
3054 ret = xhci_check_streams_endpoint(xhci, udev,
3055 eps[i], udev->slot_id);
3056 if (ret < 0)
3057 return ret;
3058
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003059 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003060 if (max_streams < (*num_streams - 1)) {
3061 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3062 eps[i]->desc.bEndpointAddress,
3063 max_streams);
3064 *num_streams = max_streams+1;
3065 }
3066
3067 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3068 if (*changed_ep_bitmask & endpoint_flag)
3069 return -EINVAL;
3070 *changed_ep_bitmask |= endpoint_flag;
3071 }
3072 return 0;
3073}
3074
3075static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3076 struct usb_device *udev,
3077 struct usb_host_endpoint **eps, unsigned int num_eps)
3078{
3079 u32 changed_ep_bitmask = 0;
3080 unsigned int slot_id;
3081 unsigned int ep_index;
3082 unsigned int ep_state;
3083 int i;
3084
3085 slot_id = udev->slot_id;
3086 if (!xhci->devs[slot_id])
3087 return 0;
3088
3089 for (i = 0; i < num_eps; i++) {
3090 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3091 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3092 /* Are streams already being freed for the endpoint? */
3093 if (ep_state & EP_GETTING_NO_STREAMS) {
3094 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003095 "endpoint 0x%x, "
3096 "streams are being disabled already\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003097 eps[i]->desc.bEndpointAddress);
3098 return 0;
3099 }
3100 /* Are there actually any streams to free? */
3101 if (!(ep_state & EP_HAS_STREAMS) &&
3102 !(ep_state & EP_GETTING_STREAMS)) {
3103 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003104 "endpoint 0x%x, "
3105 "streams are already disabled!\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003106 eps[i]->desc.bEndpointAddress);
3107 xhci_warn(xhci, "WARN xhci_free_streams() called "
3108 "with non-streams endpoint\n");
3109 return 0;
3110 }
3111 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3112 }
3113 return changed_ep_bitmask;
3114}
3115
3116/*
Luis de Bethencourtc2a298d2015-06-30 16:48:54 +02003117 * The USB device drivers use this function (through the HCD interface in USB
Sarah Sharp8df75f42010-04-02 15:34:16 -07003118 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3119 * coordinate mass storage command queueing across multiple endpoints (basically
3120 * a stream ID == a task ID).
3121 *
3122 * Setting up streams involves allocating the same size stream context array
3123 * for each endpoint and issuing a configure endpoint command for all endpoints.
3124 *
3125 * Don't allow the call to succeed if one endpoint only supports one stream
3126 * (which means it doesn't support streams at all).
3127 *
3128 * Drivers may get less stream IDs than they asked for, if the host controller
3129 * hardware or endpoints claim they can't support the number of requested
3130 * stream IDs.
3131 */
3132int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3133 struct usb_host_endpoint **eps, unsigned int num_eps,
3134 unsigned int num_streams, gfp_t mem_flags)
3135{
3136 int i, ret;
3137 struct xhci_hcd *xhci;
3138 struct xhci_virt_device *vdev;
3139 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003140 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003141 unsigned int ep_index;
3142 unsigned int num_stream_ctxs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003143 unsigned int max_packet;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003144 unsigned long flags;
3145 u32 changed_ep_bitmask = 0;
3146
3147 if (!eps)
3148 return -EINVAL;
3149
3150 /* Add one to the number of streams requested to account for
3151 * stream 0 that is reserved for xHCI usage.
3152 */
3153 num_streams += 1;
3154 xhci = hcd_to_xhci(hcd);
3155 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3156 num_streams);
3157
Hans de Goedef7920882013-11-15 12:14:38 +01003158 /* MaxPSASize value 0 (2 streams) means streams are not supported */
Hans de Goede8f873c12014-07-25 22:01:18 +02003159 if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
3160 HCC_MAX_PSA(xhci->hcc_params) < 4) {
Hans de Goedef7920882013-11-15 12:14:38 +01003161 xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3162 return -ENOSYS;
3163 }
3164
Sarah Sharp8df75f42010-04-02 15:34:16 -07003165 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3166 if (!config_cmd) {
3167 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3168 return -ENOMEM;
3169 }
Lin Wang4daf9df2015-01-09 16:06:31 +02003170 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003171 if (!ctrl_ctx) {
3172 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3173 __func__);
3174 xhci_free_command(xhci, config_cmd);
3175 return -ENOMEM;
3176 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003177
3178 /* Check to make sure all endpoints are not already configured for
3179 * streams. While we're at it, find the maximum number of streams that
3180 * all the endpoints will support and check for duplicate endpoints.
3181 */
3182 spin_lock_irqsave(&xhci->lock, flags);
3183 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3184 num_eps, &num_streams, &changed_ep_bitmask);
3185 if (ret < 0) {
3186 xhci_free_command(xhci, config_cmd);
3187 spin_unlock_irqrestore(&xhci->lock, flags);
3188 return ret;
3189 }
3190 if (num_streams <= 1) {
3191 xhci_warn(xhci, "WARN: endpoints can't handle "
3192 "more than one stream.\n");
3193 xhci_free_command(xhci, config_cmd);
3194 spin_unlock_irqrestore(&xhci->lock, flags);
3195 return -EINVAL;
3196 }
3197 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003198 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003199 * xhci_urb_enqueue() will reject all URBs.
3200 */
3201 for (i = 0; i < num_eps; i++) {
3202 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3203 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3204 }
3205 spin_unlock_irqrestore(&xhci->lock, flags);
3206
3207 /* Setup internal data structures and allocate HW data structures for
3208 * streams (but don't install the HW structures in the input context
3209 * until we're sure all memory allocation succeeded).
3210 */
3211 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3212 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3213 num_stream_ctxs, num_streams);
3214
3215 for (i = 0; i < num_eps; i++) {
3216 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003217 max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&eps[i]->desc));
Sarah Sharp8df75f42010-04-02 15:34:16 -07003218 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3219 num_stream_ctxs,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003220 num_streams,
3221 max_packet, mem_flags);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003222 if (!vdev->eps[ep_index].stream_info)
3223 goto cleanup;
3224 /* Set maxPstreams in endpoint context and update deq ptr to
3225 * point to stream context array. FIXME
3226 */
3227 }
3228
3229 /* Set up the input context for a configure endpoint command. */
3230 for (i = 0; i < num_eps; i++) {
3231 struct xhci_ep_ctx *ep_ctx;
3232
3233 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3234 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3235
3236 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3237 vdev->out_ctx, ep_index);
3238 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3239 vdev->eps[ep_index].stream_info);
3240 }
3241 /* Tell the HW to drop its old copy of the endpoint context info
3242 * and add the updated copy from the input context.
3243 */
3244 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003245 vdev->out_ctx, ctrl_ctx,
3246 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003247
3248 /* Issue and wait for the configure endpoint command */
3249 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3250 false, false);
3251
3252 /* xHC rejected the configure endpoint command for some reason, so we
3253 * leave the old ring intact and free our internal streams data
3254 * structure.
3255 */
3256 if (ret < 0)
3257 goto cleanup;
3258
3259 spin_lock_irqsave(&xhci->lock, flags);
3260 for (i = 0; i < num_eps; i++) {
3261 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3262 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3263 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3264 udev->slot_id, ep_index);
3265 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3266 }
3267 xhci_free_command(xhci, config_cmd);
3268 spin_unlock_irqrestore(&xhci->lock, flags);
3269
3270 /* Subtract 1 for stream 0, which drivers can't use */
3271 return num_streams - 1;
3272
3273cleanup:
3274 /* If it didn't work, free the streams! */
3275 for (i = 0; i < num_eps; i++) {
3276 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3277 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003278 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003279 /* FIXME Unset maxPstreams in endpoint context and
3280 * update deq ptr to point to normal string ring.
3281 */
3282 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3283 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3284 xhci_endpoint_zero(xhci, vdev, eps[i]);
3285 }
3286 xhci_free_command(xhci, config_cmd);
3287 return -ENOMEM;
3288}
3289
3290/* Transition the endpoint from using streams to being a "normal" endpoint
3291 * without streams.
3292 *
3293 * Modify the endpoint context state, submit a configure endpoint command,
3294 * and free all endpoint rings for streams if that completes successfully.
3295 */
3296int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3297 struct usb_host_endpoint **eps, unsigned int num_eps,
3298 gfp_t mem_flags)
3299{
3300 int i, ret;
3301 struct xhci_hcd *xhci;
3302 struct xhci_virt_device *vdev;
3303 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003304 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003305 unsigned int ep_index;
3306 unsigned long flags;
3307 u32 changed_ep_bitmask;
3308
3309 xhci = hcd_to_xhci(hcd);
3310 vdev = xhci->devs[udev->slot_id];
3311
3312 /* Set up a configure endpoint command to remove the streams rings */
3313 spin_lock_irqsave(&xhci->lock, flags);
3314 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3315 udev, eps, num_eps);
3316 if (changed_ep_bitmask == 0) {
3317 spin_unlock_irqrestore(&xhci->lock, flags);
3318 return -EINVAL;
3319 }
3320
3321 /* Use the xhci_command structure from the first endpoint. We may have
3322 * allocated too many, but the driver may call xhci_free_streams() for
3323 * each endpoint it grouped into one call to xhci_alloc_streams().
3324 */
3325 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3326 command = vdev->eps[ep_index].stream_info->free_streams_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02003327 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003328 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003329 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003330 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3331 __func__);
3332 return -EINVAL;
3333 }
3334
Sarah Sharp8df75f42010-04-02 15:34:16 -07003335 for (i = 0; i < num_eps; i++) {
3336 struct xhci_ep_ctx *ep_ctx;
3337
3338 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3339 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3340 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3341 EP_GETTING_NO_STREAMS;
3342
3343 xhci_endpoint_copy(xhci, command->in_ctx,
3344 vdev->out_ctx, ep_index);
Lin Wang4daf9df2015-01-09 16:06:31 +02003345 xhci_setup_no_streams_ep_input_ctx(ep_ctx,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003346 &vdev->eps[ep_index]);
3347 }
3348 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003349 vdev->out_ctx, ctrl_ctx,
3350 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003351 spin_unlock_irqrestore(&xhci->lock, flags);
3352
3353 /* Issue and wait for the configure endpoint command,
3354 * which must succeed.
3355 */
3356 ret = xhci_configure_endpoint(xhci, udev, command,
3357 false, true);
3358
3359 /* xHC rejected the configure endpoint command for some reason, so we
3360 * leave the streams rings intact.
3361 */
3362 if (ret < 0)
3363 return ret;
3364
3365 spin_lock_irqsave(&xhci->lock, flags);
3366 for (i = 0; i < num_eps; i++) {
3367 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3368 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003369 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003370 /* FIXME Unset maxPstreams in endpoint context and
3371 * update deq ptr to point to normal string ring.
3372 */
3373 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3374 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3375 }
3376 spin_unlock_irqrestore(&xhci->lock, flags);
3377
3378 return 0;
3379}
3380
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003381/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003382 * Deletes endpoint resources for endpoints that were active before a Reset
3383 * Device command, or a Disable Slot command. The Reset Device command leaves
3384 * the control endpoint intact, whereas the Disable Slot command deletes it.
3385 *
3386 * Must be called with xhci->lock held.
3387 */
3388void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3389 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3390{
3391 int i;
3392 unsigned int num_dropped_eps = 0;
3393 unsigned int drop_flags = 0;
3394
3395 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3396 if (virt_dev->eps[i].ring) {
3397 drop_flags |= 1 << i;
3398 num_dropped_eps++;
3399 }
3400 }
3401 xhci->num_active_eps -= num_dropped_eps;
3402 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003403 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3404 "Dropped %u ep ctxs, flags = 0x%x, "
3405 "%u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003406 num_dropped_eps, drop_flags,
3407 xhci->num_active_eps);
3408}
3409
3410/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003411 * This submits a Reset Device Command, which will set the device state to 0,
3412 * set the device address to 0, and disable all the endpoints except the default
3413 * control endpoint. The USB core should come back and call
3414 * xhci_address_device(), and then re-set up the configuration. If this is
3415 * called because of a usb_reset_and_verify_device(), then the old alternate
3416 * settings will be re-installed through the normal bandwidth allocation
3417 * functions.
3418 *
3419 * Wait for the Reset Device command to finish. Remove all structures
3420 * associated with the endpoints that were disabled. Clear the input device
3421 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003422 *
3423 * If the virt_dev to be reset does not exist or does not match the udev,
3424 * it means the device is lost, possibly due to the xHC restore error and
3425 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3426 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003427 */
Andiry Xuf0615c42010-10-14 07:22:48 -07003428int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003429{
3430 int ret, i;
3431 unsigned long flags;
3432 struct xhci_hcd *xhci;
3433 unsigned int slot_id;
3434 struct xhci_virt_device *virt_dev;
3435 struct xhci_command *reset_device_cmd;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003436 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003437 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003438 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003439
Andiry Xuf0615c42010-10-14 07:22:48 -07003440 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003441 if (ret <= 0)
3442 return ret;
3443 xhci = hcd_to_xhci(hcd);
3444 slot_id = udev->slot_id;
3445 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003446 if (!virt_dev) {
3447 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3448 "not exist. Re-allocate the device\n", slot_id);
3449 ret = xhci_alloc_dev(hcd, udev);
3450 if (ret == 1)
3451 return 0;
3452 else
3453 return -EINVAL;
3454 }
3455
Brian Campbell326124a2015-07-21 17:20:28 +03003456 if (virt_dev->tt_info)
3457 old_active_eps = virt_dev->tt_info->active_eps;
3458
Andiry Xuf0615c42010-10-14 07:22:48 -07003459 if (virt_dev->udev != udev) {
3460 /* If the virt_dev and the udev does not match, this virt_dev
3461 * may belong to another udev.
3462 * Re-allocate the device.
3463 */
3464 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3465 "not match the udev. Re-allocate the device\n",
3466 slot_id);
3467 ret = xhci_alloc_dev(hcd, udev);
3468 if (ret == 1)
3469 return 0;
3470 else
3471 return -EINVAL;
3472 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003473
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003474 /* If device is not setup, there is no point in resetting it */
3475 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3476 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3477 SLOT_STATE_DISABLED)
3478 return 0;
3479
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003480 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3481 /* Allocate the command structure that holds the struct completion.
3482 * Assume we're in process context, since the normal device reset
3483 * process has to wait for the device anyway. Storage devices are
3484 * reset as part of error handling, so use GFP_NOIO instead of
3485 * GFP_KERNEL.
3486 */
3487 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3488 if (!reset_device_cmd) {
3489 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3490 return -ENOMEM;
3491 }
3492
3493 /* Attempt to submit the Reset Device command to the command ring */
3494 spin_lock_irqsave(&xhci->lock, flags);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003495
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003496 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003497 if (ret) {
3498 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003499 spin_unlock_irqrestore(&xhci->lock, flags);
3500 goto command_cleanup;
3501 }
3502 xhci_ring_cmd_db(xhci);
3503 spin_unlock_irqrestore(&xhci->lock, flags);
3504
3505 /* Wait for the Reset Device command to finish */
Mathias Nymanc311e392014-05-08 19:26:03 +03003506 wait_for_completion(reset_device_cmd->completion);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003507
3508 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3509 * unless we tried to reset a slot ID that wasn't enabled,
3510 * or the device wasn't in the addressed or configured state.
3511 */
3512 ret = reset_device_cmd->status;
3513 switch (ret) {
Mathias Nymanc311e392014-05-08 19:26:03 +03003514 case COMP_CMD_ABORT:
3515 case COMP_CMD_STOP:
3516 xhci_warn(xhci, "Timeout waiting for reset device command\n");
3517 ret = -ETIME;
3518 goto command_cleanup;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003519 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3520 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003521 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003522 slot_id,
3523 xhci_get_slot_state(xhci, virt_dev->out_ctx));
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003524 xhci_dbg(xhci, "Not freeing device rings.\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003525 /* Don't treat this as an error. May change my mind later. */
3526 ret = 0;
3527 goto command_cleanup;
3528 case COMP_SUCCESS:
3529 xhci_dbg(xhci, "Successful reset device command.\n");
3530 break;
3531 default:
3532 if (xhci_is_vendor_info_code(xhci, ret))
3533 break;
3534 xhci_warn(xhci, "Unknown completion code %u for "
3535 "reset device command.\n", ret);
3536 ret = -EINVAL;
3537 goto command_cleanup;
3538 }
3539
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003540 /* Free up host controller endpoint resources */
3541 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3542 spin_lock_irqsave(&xhci->lock, flags);
3543 /* Don't delete the default control endpoint resources */
3544 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3545 spin_unlock_irqrestore(&xhci->lock, flags);
3546 }
3547
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003548 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3549 last_freed_endpoint = 1;
3550 for (i = 1; i < 31; ++i) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003551 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3552
3553 if (ep->ep_state & EP_HAS_STREAMS) {
Hans de Goededf613832013-10-04 00:29:45 +02003554 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3555 xhci_get_endpoint_address(i));
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003556 xhci_free_stream_info(xhci, ep->stream_info);
3557 ep->stream_info = NULL;
3558 ep->ep_state &= ~EP_HAS_STREAMS;
3559 }
3560
3561 if (ep->ring) {
3562 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3563 last_freed_endpoint = i;
3564 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003565 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3566 xhci_drop_ep_from_interval_table(xhci,
3567 &virt_dev->eps[i].bw_info,
3568 virt_dev->bw_table,
3569 udev,
3570 &virt_dev->eps[i],
3571 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003572 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003573 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003574 /* If necessary, update the number of active TTs on this root port */
3575 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3576
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003577 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3578 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3579 ret = 0;
3580
3581command_cleanup:
3582 xhci_free_command(xhci, reset_device_cmd);
3583 return ret;
3584}
3585
3586/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003587 * At this point, the struct usb_device is about to go away, the device has
3588 * disconnected, and all traffic has been stopped and the endpoints have been
3589 * disabled. Free any HC data structures associated with that device.
3590 */
3591void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3592{
3593 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003594 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003595 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003596 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07003597 int i, ret;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003598 struct xhci_command *command;
3599
3600 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
3601 if (!command)
3602 return;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003603
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003604#ifndef CONFIG_USB_DEFAULT_PERSIST
3605 /*
3606 * We called pm_runtime_get_noresume when the device was attached.
3607 * Decrement the counter here to allow controller to runtime suspend
3608 * if no devices remain.
3609 */
3610 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003611 pm_runtime_put_noidle(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003612#endif
3613
Andiry Xu64927732010-10-14 07:22:45 -07003614 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003615 /* If the host is halted due to driver unload, we still need to free the
3616 * device.
3617 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003618 if (ret <= 0 && ret != -ENODEV) {
3619 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003620 return;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003621 }
Andiry Xu64927732010-10-14 07:22:45 -07003622
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003623 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003624
3625 /* Stop any wayward timer functions (which may grab the lock) */
3626 for (i = 0; i < 31; ++i) {
3627 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3628 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3629 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003630
3631 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003632 /* Don't disable the slot if the host controller is dead. */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02003633 state = readl(&xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003634 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3635 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003636 xhci_free_virt_device(xhci, udev->slot_id);
3637 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003638 kfree(command);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003639 return;
3640 }
3641
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003642 if (xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3643 udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003644 spin_unlock_irqrestore(&xhci->lock, flags);
3645 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3646 return;
3647 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003648 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003649 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003650
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003651 /*
3652 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07003653 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003654 */
3655}
3656
3657/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003658 * Checks if we have enough host controller resources for the default control
3659 * endpoint.
3660 *
3661 * Must be called with xhci->lock held.
3662 */
3663static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3664{
3665 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003666 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3667 "Not enough ep ctxs: "
3668 "%u active, need to add 1, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003669 xhci->num_active_eps, xhci->limit_active_eps);
3670 return -ENOMEM;
3671 }
3672 xhci->num_active_eps += 1;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003673 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3674 "Adding 1 ep ctx, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003675 xhci->num_active_eps);
3676 return 0;
3677}
3678
3679
3680/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003681 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3682 * timed out, or allocating memory failed. Returns 1 on success.
3683 */
3684int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3685{
3686 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3687 unsigned long flags;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003688 int ret, slot_id;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003689 struct xhci_command *command;
3690
3691 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
3692 if (!command)
3693 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003694
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003695 /* xhci->slot_id and xhci->addr_dev are not thread-safe */
3696 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003697 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003698 command->completion = &xhci->addr_dev;
3699 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003700 if (ret) {
3701 spin_unlock_irqrestore(&xhci->lock, flags);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003702 mutex_unlock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003703 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003704 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003705 return 0;
3706 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003707 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003708 spin_unlock_irqrestore(&xhci->lock, flags);
3709
Mathias Nymanc311e392014-05-08 19:26:03 +03003710 wait_for_completion(command->completion);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003711 slot_id = xhci->slot_id;
3712 mutex_unlock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003713
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003714 if (!slot_id || command->status != COMP_SUCCESS) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003715 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharpbe982032014-05-08 19:25:59 +03003716 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
3717 HCS_MAX_SLOTS(
3718 readl(&xhci->cap_regs->hcs_params1)));
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003719 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003720 return 0;
3721 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003722
3723 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3724 spin_lock_irqsave(&xhci->lock, flags);
3725 ret = xhci_reserve_host_control_ep_resources(xhci);
3726 if (ret) {
3727 spin_unlock_irqrestore(&xhci->lock, flags);
3728 xhci_warn(xhci, "Not enough host resources, "
3729 "active endpoint contexts = %u\n",
3730 xhci->num_active_eps);
3731 goto disable_slot;
3732 }
3733 spin_unlock_irqrestore(&xhci->lock, flags);
3734 }
3735 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003736 * xhci_discover_or_reset_device(), which may be called as part of
3737 * mass storage driver error handling.
3738 */
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003739 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003740 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003741 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003742 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003743 udev->slot_id = slot_id;
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003744
3745#ifndef CONFIG_USB_DEFAULT_PERSIST
3746 /*
3747 * If resetting upon resume, we can't put the controller into runtime
3748 * suspend if there is a device attached.
3749 */
3750 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003751 pm_runtime_get_noresume(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003752#endif
3753
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003754
3755 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003756 /* Is this a LS or FS device under a HS hub? */
3757 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003758 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003759
3760disable_slot:
3761 /* Disable slot, if we can do it without mem alloc */
3762 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003763 command->completion = NULL;
3764 command->status = 0;
3765 if (!xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3766 udev->slot_id))
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003767 xhci_ring_cmd_db(xhci);
3768 spin_unlock_irqrestore(&xhci->lock, flags);
3769 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003770}
3771
3772/*
Dan Williams48fc7db2013-12-05 17:07:27 -08003773 * Issue an Address Device command and optionally send a corresponding
3774 * SetAddress request to the device.
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003775 */
Dan Williams48fc7db2013-12-05 17:07:27 -08003776static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
3777 enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003778{
Dan Williams6f8ffc02013-11-22 01:20:01 -08003779 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003780 unsigned long flags;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003781 struct xhci_virt_device *virt_dev;
3782 int ret = 0;
3783 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003784 struct xhci_slot_ctx *slot_ctx;
3785 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003786 u64 temp_64;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003787 struct xhci_command *command = NULL;
3788
3789 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003790
Lu Baolua2118d02017-01-03 18:28:44 +02003791 if (xhci->xhc_state) { /* dying, removing or halted */
3792 ret = -ESHUTDOWN;
Roger Quadros448116b2015-09-21 17:46:15 +03003793 goto out;
Lu Baolua2118d02017-01-03 18:28:44 +02003794 }
Roger Quadros448116b2015-09-21 17:46:15 +03003795
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003796 if (!udev->slot_id) {
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003797 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3798 "Bad Slot ID %d", udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003799 ret = -EINVAL;
3800 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003801 }
3802
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003803 virt_dev = xhci->devs[udev->slot_id];
3804
Matt Evans7ed603e2011-03-29 13:40:56 +11003805 if (WARN_ON(!virt_dev)) {
3806 /*
3807 * In plug/unplug torture test with an NEC controller,
3808 * a zero-dereference was observed once due to virt_dev = 0.
3809 * Print useful debug rather than crash if it is observed again!
3810 */
3811 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3812 udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003813 ret = -EINVAL;
3814 goto out;
Matt Evans7ed603e2011-03-29 13:40:56 +11003815 }
3816
Mathias Nymanf161ead2015-01-09 17:18:28 +02003817 if (setup == SETUP_CONTEXT_ONLY) {
3818 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3819 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3820 SLOT_STATE_DEFAULT) {
3821 xhci_dbg(xhci, "Slot already in default state\n");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003822 goto out;
Mathias Nymanf161ead2015-01-09 17:18:28 +02003823 }
3824 }
3825
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003826 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003827 if (!command) {
3828 ret = -ENOMEM;
3829 goto out;
3830 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003831
3832 command->in_ctx = virt_dev->in_ctx;
3833 command->completion = &xhci->addr_dev;
3834
Andiry Xuf0615c42010-10-14 07:22:48 -07003835 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Lin Wang4daf9df2015-01-09 16:06:31 +02003836 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003837 if (!ctrl_ctx) {
3838 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3839 __func__);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003840 ret = -EINVAL;
3841 goto out;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003842 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003843 /*
3844 * If this is the first Set Address since device plug-in or
3845 * virt_device realloaction after a resume with an xHCI power loss,
3846 * then set up the slot context.
3847 */
3848 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003849 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003850 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003851 else
3852 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003853 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3854 ctrl_ctx->drop_flags = 0;
3855
Sarah Sharp66e49d82009-07-27 12:03:46 -07003856 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003857 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003858 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003859 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003860
Sarah Sharpf88ba782009-05-14 11:44:22 -07003861 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003862 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
Dan Williams48fc7db2013-12-05 17:07:27 -08003863 udev->slot_id, setup);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003864 if (ret) {
3865 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003866 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3867 "FIXME: allocate a command ring segment");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003868 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003869 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003870 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003871 spin_unlock_irqrestore(&xhci->lock, flags);
3872
3873 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
Mathias Nymanc311e392014-05-08 19:26:03 +03003874 wait_for_completion(command->completion);
3875
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003876 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3877 * the SetAddress() "recovery interval" required by USB and aborting the
3878 * command on a timeout.
3879 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03003880 switch (command->status) {
Mathias Nymanc311e392014-05-08 19:26:03 +03003881 case COMP_CMD_ABORT:
3882 case COMP_CMD_STOP:
3883 xhci_warn(xhci, "Timeout while waiting for setup device command\n");
3884 ret = -ETIME;
3885 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003886 case COMP_CTX_STATE:
3887 case COMP_EBADSLT:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003888 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
3889 act, udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003890 ret = -EINVAL;
3891 break;
3892 case COMP_TX_ERR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003893 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003894 ret = -EPROTO;
3895 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08003896 case COMP_DEV_ERR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003897 dev_warn(&udev->dev,
3898 "ERROR: Incompatible device for setup %s command\n", act);
Alex Hef6ba6fe2011-06-08 18:34:06 +08003899 ret = -ENODEV;
3900 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003901 case COMP_SUCCESS:
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003902 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williams6f8ffc02013-11-22 01:20:01 -08003903 "Successful setup %s command", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003904 break;
3905 default:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003906 xhci_err(xhci,
3907 "ERROR: unexpected setup %s command completion code 0x%x.\n",
Mathias Nyman9ea18332014-05-08 19:26:02 +03003908 act, command->status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07003909 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003910 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003911 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003912 ret = -EINVAL;
3913 break;
3914 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003915 if (ret)
3916 goto out;
Sarah Sharpf7b2e402014-01-30 13:27:49 -08003917 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003918 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3919 "Op regs DCBAA ptr = %#016llx", temp_64);
3920 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3921 "Slot ID %d dcbaa entry @%p = %#016llx",
3922 udev->slot_id,
3923 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3924 (unsigned long long)
3925 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3926 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3927 "Output Context DMA address = %#08llx",
John Yound115b042009-07-27 12:05:15 -07003928 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003929 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003930 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003931 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003932 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003933 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003934 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003935 /*
3936 * USB core uses address 1 for the roothubs, so we add one to the
3937 * address given back to us by the HC.
3938 */
John Yound115b042009-07-27 12:05:15 -07003939 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003940 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003941 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003942 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003943 ctrl_ctx->add_flags = 0;
3944 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003945
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003946 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williamsa2cdc342013-10-16 12:25:44 -07003947 "Internal device address = %d",
3948 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003949out:
3950 mutex_unlock(&xhci->mutex);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003951 kfree(command);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003952 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003953}
3954
Dan Williams48fc7db2013-12-05 17:07:27 -08003955int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3956{
3957 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
3958}
3959
3960int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
3961{
3962 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
3963}
3964
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003965/*
3966 * Transfer the port index into real index in the HW port status
3967 * registers. Caculate offset between the port's PORTSC register
3968 * and port status base. Divide the number of per port register
3969 * to get the real index. The raw port number bases 1.
3970 */
3971int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3972{
3973 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3974 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3975 __le32 __iomem *addr;
3976 int raw_port;
3977
Mathias Nymanb50107b2015-10-01 18:40:38 +03003978 if (hcd->speed < HCD_USB3)
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003979 addr = xhci->usb2_ports[port1 - 1];
3980 else
3981 addr = xhci->usb3_ports[port1 - 1];
3982
3983 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3984 return raw_port;
3985}
3986
Mathias Nymana558ccd2013-05-23 17:14:30 +03003987/*
3988 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3989 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3990 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07003991static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03003992 struct usb_device *udev, u16 max_exit_latency)
3993{
3994 struct xhci_virt_device *virt_dev;
3995 struct xhci_command *command;
3996 struct xhci_input_control_ctx *ctrl_ctx;
3997 struct xhci_slot_ctx *slot_ctx;
3998 unsigned long flags;
3999 int ret;
4000
4001 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nyman96044692014-09-11 13:55:50 +03004002
4003 virt_dev = xhci->devs[udev->slot_id];
4004
4005 /*
4006 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4007 * xHC was re-initialized. Exit latency will be set later after
4008 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4009 */
4010
4011 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004012 spin_unlock_irqrestore(&xhci->lock, flags);
4013 return 0;
4014 }
4015
4016 /* Attempt to issue an Evaluate Context command to change the MEL. */
Mathias Nymana558ccd2013-05-23 17:14:30 +03004017 command = xhci->lpm_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02004018 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004019 if (!ctrl_ctx) {
4020 spin_unlock_irqrestore(&xhci->lock, flags);
4021 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4022 __func__);
4023 return -ENOMEM;
4024 }
4025
Mathias Nymana558ccd2013-05-23 17:14:30 +03004026 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
4027 spin_unlock_irqrestore(&xhci->lock, flags);
4028
Mathias Nymana558ccd2013-05-23 17:14:30 +03004029 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4030 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
4031 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
4032 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
Mathias Nyman4801d4ea2014-11-27 18:19:15 +02004033 slot_ctx->dev_state = 0;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004034
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03004035 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
4036 "Set up evaluate context for LPM MEL change.");
Mathias Nymana558ccd2013-05-23 17:14:30 +03004037 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
4038 xhci_dbg_ctx(xhci, command->in_ctx, 0);
4039
4040 /* Issue and wait for the evaluate context command. */
4041 ret = xhci_configure_endpoint(xhci, udev, command,
4042 true, true);
4043 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
4044 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
4045
4046 if (!ret) {
4047 spin_lock_irqsave(&xhci->lock, flags);
4048 virt_dev->current_mel = max_exit_latency;
4049 spin_unlock_irqrestore(&xhci->lock, flags);
4050 }
4051 return ret;
4052}
4053
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004054#ifdef CONFIG_PM
Andiry Xu95743232011-09-23 14:19:51 -07004055
4056/* BESL to HIRD Encoding array for USB2 LPM */
4057static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4058 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4059
4060/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08004061static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4062 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07004063{
Andiry Xuf99298b2011-12-12 16:45:28 +08004064 int u2del, besl, besl_host;
4065 int besl_device = 0;
4066 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07004067
Andiry Xuf99298b2011-12-12 16:45:28 +08004068 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4069 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4070
4071 if (field & USB_BESL_SUPPORT) {
4072 for (besl_host = 0; besl_host < 16; besl_host++) {
4073 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07004074 break;
4075 }
Andiry Xuf99298b2011-12-12 16:45:28 +08004076 /* Use baseline BESL value as default */
4077 if (field & USB_BESL_BASELINE_VALID)
4078 besl_device = USB_GET_BESL_BASELINE(field);
4079 else if (field & USB_BESL_DEEP_VALID)
4080 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07004081 } else {
4082 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08004083 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07004084 else
Andiry Xuf99298b2011-12-12 16:45:28 +08004085 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07004086 }
4087
Andiry Xuf99298b2011-12-12 16:45:28 +08004088 besl = besl_host + besl_device;
4089 if (besl > 15)
4090 besl = 15;
4091
4092 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07004093}
4094
Mathias Nymana558ccd2013-05-23 17:14:30 +03004095/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4096static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4097{
4098 u32 field;
4099 int l1;
4100 int besld = 0;
4101 int hirdm = 0;
4102
4103 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4104
4105 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004106 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004107
4108 /* device has preferred BESLD */
4109 if (field & USB_BESL_DEEP_VALID) {
4110 besld = USB_GET_BESL_DEEP(field);
4111 hirdm = 1;
4112 }
4113
4114 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4115}
4116
Andiry Xu65580b432011-09-23 14:19:52 -07004117int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4118 struct usb_device *udev, int enable)
4119{
4120 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4121 __le32 __iomem **port_array;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004122 __le32 __iomem *pm_addr, *hlpm_addr;
4123 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004124 unsigned int port_num;
4125 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004126 int hird, exit_latency;
4127 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004128
Mathias Nymanb50107b2015-10-01 18:40:38 +03004129 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
Andiry Xu65580b432011-09-23 14:19:52 -07004130 !udev->lpm_capable)
4131 return -EPERM;
4132
4133 if (!udev->parent || udev->parent->parent ||
4134 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4135 return -EPERM;
4136
4137 if (udev->usb2_hw_lpm_capable != 1)
4138 return -EPERM;
4139
4140 spin_lock_irqsave(&xhci->lock, flags);
4141
4142 port_array = xhci->usb2_ports;
4143 port_num = udev->portnum - 1;
Mathias Nymanb6e76372013-05-23 17:14:29 +03004144 pm_addr = port_array[port_num] + PORTPMSC;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004145 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004146 hlpm_addr = port_array[port_num] + PORTHLPMC;
4147 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004148
4149 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
Lin Wang654a55d2014-05-08 19:25:54 +03004150 enable ? "enable" : "disable", port_num + 1);
Andiry Xu65580b432011-09-23 14:19:52 -07004151
Andiry Xu65580b432011-09-23 14:19:52 -07004152 if (enable) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004153 /* Host supports BESL timeout instead of HIRD */
4154 if (udev->usb2_hw_lpm_besl_capable) {
4155 /* if device doesn't have a preferred BESL value use a
4156 * default one which works with mixed HIRD and BESL
4157 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4158 */
4159 if ((field & USB_BESL_SUPPORT) &&
4160 (field & USB_BESL_BASELINE_VALID))
4161 hird = USB_GET_BESL_BASELINE(field);
4162 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004163 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004164
4165 exit_latency = xhci_besl_encoding[hird];
4166 spin_unlock_irqrestore(&xhci->lock, flags);
4167
4168 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4169 * input context for link powermanagement evaluate
4170 * context commands. It is protected by hcd->bandwidth
4171 * mutex and is shared by all devices. We need to set
4172 * the max ext latency in USB 2 BESL LPM as well, so
4173 * use the same mutex and xhci_change_max_exit_latency()
4174 */
4175 mutex_lock(hcd->bandwidth_mutex);
4176 ret = xhci_change_max_exit_latency(xhci, udev,
4177 exit_latency);
4178 mutex_unlock(hcd->bandwidth_mutex);
4179
4180 if (ret < 0)
4181 return ret;
4182 spin_lock_irqsave(&xhci->lock, flags);
4183
4184 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004185 writel(hlpm_val, hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004186 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004187 readl(hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004188 } else {
4189 hird = xhci_calculate_hird_besl(xhci, udev);
4190 }
4191
4192 pm_val &= ~PORT_HIRD_MASK;
Sarah Sharp58e21f72013-10-07 17:17:20 -07004193 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004194 writel(pm_val, pm_addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004195 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004196 pm_val |= PORT_HLE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004197 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004198 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004199 readl(pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004200 } else {
Sarah Sharp58e21f72013-10-07 17:17:20 -07004201 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004202 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004203 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004204 readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004205 if (udev->usb2_hw_lpm_besl_capable) {
4206 spin_unlock_irqrestore(&xhci->lock, flags);
4207 mutex_lock(hcd->bandwidth_mutex);
4208 xhci_change_max_exit_latency(xhci, udev, 0);
4209 mutex_unlock(hcd->bandwidth_mutex);
4210 return 0;
4211 }
Andiry Xu65580b432011-09-23 14:19:52 -07004212 }
4213
4214 spin_unlock_irqrestore(&xhci->lock, flags);
4215 return 0;
4216}
4217
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004218/* check if a usb2 port supports a given extened capability protocol
4219 * only USB2 ports extended protocol capability values are cached.
4220 * Return 1 if capability is supported
4221 */
4222static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4223 unsigned capability)
4224{
4225 u32 port_offset, port_count;
4226 int i;
4227
4228 for (i = 0; i < xhci->num_ext_caps; i++) {
4229 if (xhci->ext_caps[i] & capability) {
4230 /* port offsets starts at 1 */
4231 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4232 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4233 if (port >= port_offset &&
4234 port < port_offset + port_count)
4235 return 1;
4236 }
4237 }
4238 return 0;
4239}
4240
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004241int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4242{
4243 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004244 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004245
Mathias Nymanb50107b2015-10-01 18:40:38 +03004246 if (hcd->speed >= HCD_USB3 || !xhci->sw_lpm_support ||
Sarah Sharpde68bab2013-09-30 17:26:28 +03004247 !udev->lpm_capable)
4248 return 0;
4249
4250 /* we only support lpm for non-hub device connected to root hub yet */
4251 if (!udev->parent || udev->parent->parent ||
4252 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4253 return 0;
4254
4255 if (xhci->hw_lpm_support == 1 &&
4256 xhci_check_usb2_port_capability(
4257 xhci, portnum, XHCI_HLC)) {
4258 udev->usb2_hw_lpm_capable = 1;
4259 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4260 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4261 if (xhci_check_usb2_port_capability(xhci, portnum,
4262 XHCI_BLC))
4263 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004264 }
4265
4266 return 0;
4267}
4268
Sarah Sharp3b3db022012-05-09 10:55:03 -07004269/*---------------------- USB 3.0 Link PM functions ------------------------*/
4270
Sarah Sharpe3567d22012-05-16 13:36:24 -07004271/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4272static unsigned long long xhci_service_interval_to_ns(
4273 struct usb_endpoint_descriptor *desc)
4274{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004275 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004276}
4277
Sarah Sharp3b3db022012-05-09 10:55:03 -07004278static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4279 enum usb3_link_state state)
4280{
4281 unsigned long long sel;
4282 unsigned long long pel;
4283 unsigned int max_sel_pel;
4284 char *state_name;
4285
4286 switch (state) {
4287 case USB3_LPM_U1:
4288 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4289 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4290 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4291 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4292 state_name = "U1";
4293 break;
4294 case USB3_LPM_U2:
4295 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4296 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4297 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4298 state_name = "U2";
4299 break;
4300 default:
4301 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4302 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004303 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004304 }
4305
4306 if (sel <= max_sel_pel && pel <= max_sel_pel)
4307 return USB3_LPM_DEVICE_INITIATED;
4308
4309 if (sel > max_sel_pel)
4310 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4311 "due to long SEL %llu ms\n",
4312 state_name, sel);
4313 else
4314 dev_dbg(&udev->dev, "Device-initiated %s disabled "
Joe Perches03e64e92013-07-16 19:25:59 -07004315 "due to long PEL %llu ms\n",
Sarah Sharp3b3db022012-05-09 10:55:03 -07004316 state_name, pel);
4317 return USB3_LPM_DISABLED;
4318}
4319
Pratyush Anand9502c462014-07-04 17:01:23 +03004320/* The U1 timeout should be the maximum of the following values:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004321 * - For control endpoints, U1 system exit latency (SEL) * 3
4322 * - For bulk endpoints, U1 SEL * 5
4323 * - For interrupt endpoints:
4324 * - Notification EPs, U1 SEL * 3
4325 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4326 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4327 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004328static unsigned long long xhci_calculate_intel_u1_timeout(
4329 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004330 struct usb_endpoint_descriptor *desc)
4331{
4332 unsigned long long timeout_ns;
4333 int ep_type;
4334 int intr_type;
4335
4336 ep_type = usb_endpoint_type(desc);
4337 switch (ep_type) {
4338 case USB_ENDPOINT_XFER_CONTROL:
4339 timeout_ns = udev->u1_params.sel * 3;
4340 break;
4341 case USB_ENDPOINT_XFER_BULK:
4342 timeout_ns = udev->u1_params.sel * 5;
4343 break;
4344 case USB_ENDPOINT_XFER_INT:
4345 intr_type = usb_endpoint_interrupt_type(desc);
4346 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4347 timeout_ns = udev->u1_params.sel * 3;
4348 break;
4349 }
4350 /* Otherwise the calculation is the same as isoc eps */
4351 case USB_ENDPOINT_XFER_ISOC:
4352 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004353 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004354 if (timeout_ns < udev->u1_params.sel * 2)
4355 timeout_ns = udev->u1_params.sel * 2;
4356 break;
4357 default:
4358 return 0;
4359 }
4360
Pratyush Anand9502c462014-07-04 17:01:23 +03004361 return timeout_ns;
4362}
4363
4364/* Returns the hub-encoded U1 timeout value. */
4365static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
4366 struct usb_device *udev,
4367 struct usb_endpoint_descriptor *desc)
4368{
4369 unsigned long long timeout_ns;
4370
4371 if (xhci->quirks & XHCI_INTEL_HOST)
4372 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
4373 else
4374 timeout_ns = udev->u1_params.sel;
4375
4376 /* The U1 timeout is encoded in 1us intervals.
4377 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4378 */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004379 if (timeout_ns == USB3_LPM_DISABLED)
Pratyush Anand9502c462014-07-04 17:01:23 +03004380 timeout_ns = 1;
4381 else
4382 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004383
4384 /* If the necessary timeout value is bigger than what we can set in the
4385 * USB 3.0 hub, we have to disable hub-initiated U1.
4386 */
4387 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4388 return timeout_ns;
4389 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4390 "due to long timeout %llu ms\n", timeout_ns);
4391 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4392}
4393
Pratyush Anand9502c462014-07-04 17:01:23 +03004394/* The U2 timeout should be the maximum of:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004395 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4396 * - largest bInterval of any active periodic endpoint (to avoid going
4397 * into lower power link states between intervals).
4398 * - the U2 Exit Latency of the device
4399 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004400static unsigned long long xhci_calculate_intel_u2_timeout(
4401 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004402 struct usb_endpoint_descriptor *desc)
4403{
4404 unsigned long long timeout_ns;
4405 unsigned long long u2_del_ns;
4406
4407 timeout_ns = 10 * 1000 * 1000;
4408
4409 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4410 (xhci_service_interval_to_ns(desc) > timeout_ns))
4411 timeout_ns = xhci_service_interval_to_ns(desc);
4412
Oliver Neukum966e7a82012-10-17 12:17:50 +02004413 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004414 if (u2_del_ns > timeout_ns)
4415 timeout_ns = u2_del_ns;
4416
Pratyush Anand9502c462014-07-04 17:01:23 +03004417 return timeout_ns;
4418}
4419
4420/* Returns the hub-encoded U2 timeout value. */
4421static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
4422 struct usb_device *udev,
4423 struct usb_endpoint_descriptor *desc)
4424{
4425 unsigned long long timeout_ns;
4426
4427 if (xhci->quirks & XHCI_INTEL_HOST)
4428 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
4429 else
4430 timeout_ns = udev->u2_params.sel;
4431
Sarah Sharpe3567d22012-05-16 13:36:24 -07004432 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004433 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004434 /* If the necessary timeout value is bigger than what we can set in the
4435 * USB 3.0 hub, we have to disable hub-initiated U2.
4436 */
4437 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4438 return timeout_ns;
4439 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4440 "due to long timeout %llu ms\n", timeout_ns);
4441 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4442}
4443
Sarah Sharp3b3db022012-05-09 10:55:03 -07004444static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4445 struct usb_device *udev,
4446 struct usb_endpoint_descriptor *desc,
4447 enum usb3_link_state state,
4448 u16 *timeout)
4449{
Pratyush Anand9502c462014-07-04 17:01:23 +03004450 if (state == USB3_LPM_U1)
4451 return xhci_calculate_u1_timeout(xhci, udev, desc);
4452 else if (state == USB3_LPM_U2)
4453 return xhci_calculate_u2_timeout(xhci, udev, desc);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004454
Sarah Sharp3b3db022012-05-09 10:55:03 -07004455 return USB3_LPM_DISABLED;
4456}
4457
4458static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4459 struct usb_device *udev,
4460 struct usb_endpoint_descriptor *desc,
4461 enum usb3_link_state state,
4462 u16 *timeout)
4463{
4464 u16 alt_timeout;
4465
4466 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4467 desc, state, timeout);
4468
4469 /* If we found we can't enable hub-initiated LPM, or
4470 * the U1 or U2 exit latency was too high to allow
4471 * device-initiated LPM as well, just stop searching.
4472 */
4473 if (alt_timeout == USB3_LPM_DISABLED ||
4474 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4475 *timeout = alt_timeout;
4476 return -E2BIG;
4477 }
4478 if (alt_timeout > *timeout)
4479 *timeout = alt_timeout;
4480 return 0;
4481}
4482
4483static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4484 struct usb_device *udev,
4485 struct usb_host_interface *alt,
4486 enum usb3_link_state state,
4487 u16 *timeout)
4488{
4489 int j;
4490
4491 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4492 if (xhci_update_timeout_for_endpoint(xhci, udev,
4493 &alt->endpoint[j].desc, state, timeout))
4494 return -E2BIG;
4495 continue;
4496 }
4497 return 0;
4498}
4499
Sarah Sharpe3567d22012-05-16 13:36:24 -07004500static int xhci_check_intel_tier_policy(struct usb_device *udev,
4501 enum usb3_link_state state)
4502{
4503 struct usb_device *parent;
4504 unsigned int num_hubs;
4505
4506 if (state == USB3_LPM_U2)
4507 return 0;
4508
4509 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4510 for (parent = udev->parent, num_hubs = 0; parent->parent;
4511 parent = parent->parent)
4512 num_hubs++;
4513
4514 if (num_hubs < 2)
4515 return 0;
4516
4517 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4518 " below second-tier hub.\n");
4519 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4520 "to decrease power consumption.\n");
4521 return -E2BIG;
4522}
4523
Sarah Sharp3b3db022012-05-09 10:55:03 -07004524static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4525 struct usb_device *udev,
4526 enum usb3_link_state state)
4527{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004528 if (xhci->quirks & XHCI_INTEL_HOST)
4529 return xhci_check_intel_tier_policy(udev, state);
Pratyush Anand9502c462014-07-04 17:01:23 +03004530 else
4531 return 0;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004532}
4533
4534/* Returns the U1 or U2 timeout that should be enabled.
4535 * If the tier check or timeout setting functions return with a non-zero exit
4536 * code, that means the timeout value has been finalized and we shouldn't look
4537 * at any more endpoints.
4538 */
4539static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4540 struct usb_device *udev, enum usb3_link_state state)
4541{
4542 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4543 struct usb_host_config *config;
4544 char *state_name;
4545 int i;
4546 u16 timeout = USB3_LPM_DISABLED;
4547
4548 if (state == USB3_LPM_U1)
4549 state_name = "U1";
4550 else if (state == USB3_LPM_U2)
4551 state_name = "U2";
4552 else {
4553 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4554 state);
4555 return timeout;
4556 }
4557
4558 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4559 return timeout;
4560
4561 /* Gather some information about the currently installed configuration
4562 * and alternate interface settings.
4563 */
4564 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4565 state, &timeout))
4566 return timeout;
4567
4568 config = udev->actconfig;
4569 if (!config)
4570 return timeout;
4571
Xenia Ragiadakou64ba4192013-08-26 23:29:46 +03004572 for (i = 0; i < config->desc.bNumInterfaces; i++) {
Sarah Sharp3b3db022012-05-09 10:55:03 -07004573 struct usb_driver *driver;
4574 struct usb_interface *intf = config->interface[i];
4575
4576 if (!intf)
4577 continue;
4578
4579 /* Check if any currently bound drivers want hub-initiated LPM
4580 * disabled.
4581 */
4582 if (intf->dev.driver) {
4583 driver = to_usb_driver(intf->dev.driver);
4584 if (driver && driver->disable_hub_initiated_lpm) {
4585 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4586 "at request of driver %s\n",
4587 state_name, driver->name);
4588 return xhci_get_timeout_no_hub_lpm(udev, state);
4589 }
4590 }
4591
4592 /* Not sure how this could happen... */
4593 if (!intf->cur_altsetting)
4594 continue;
4595
4596 if (xhci_update_timeout_for_interface(xhci, udev,
4597 intf->cur_altsetting,
4598 state, &timeout))
4599 return timeout;
4600 }
4601 return timeout;
4602}
4603
Sarah Sharp3b3db022012-05-09 10:55:03 -07004604static int calculate_max_exit_latency(struct usb_device *udev,
4605 enum usb3_link_state state_changed,
4606 u16 hub_encoded_timeout)
4607{
4608 unsigned long long u1_mel_us = 0;
4609 unsigned long long u2_mel_us = 0;
4610 unsigned long long mel_us = 0;
4611 bool disabling_u1;
4612 bool disabling_u2;
4613 bool enabling_u1;
4614 bool enabling_u2;
4615
4616 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4617 hub_encoded_timeout == USB3_LPM_DISABLED);
4618 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4619 hub_encoded_timeout == USB3_LPM_DISABLED);
4620
4621 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4622 hub_encoded_timeout != USB3_LPM_DISABLED);
4623 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4624 hub_encoded_timeout != USB3_LPM_DISABLED);
4625
4626 /* If U1 was already enabled and we're not disabling it,
4627 * or we're going to enable U1, account for the U1 max exit latency.
4628 */
4629 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4630 enabling_u1)
4631 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4632 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4633 enabling_u2)
4634 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4635
4636 if (u1_mel_us > u2_mel_us)
4637 mel_us = u1_mel_us;
4638 else
4639 mel_us = u2_mel_us;
4640 /* xHCI host controller max exit latency field is only 16 bits wide. */
4641 if (mel_us > MAX_EXIT) {
4642 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4643 "is too big.\n", mel_us);
4644 return -E2BIG;
4645 }
4646 return mel_us;
4647}
4648
4649/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4650int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4651 struct usb_device *udev, enum usb3_link_state state)
4652{
4653 struct xhci_hcd *xhci;
4654 u16 hub_encoded_timeout;
4655 int mel;
4656 int ret;
4657
4658 xhci = hcd_to_xhci(hcd);
4659 /* The LPM timeout values are pretty host-controller specific, so don't
4660 * enable hub-initiated timeouts unless the vendor has provided
4661 * information about their timeout algorithm.
4662 */
4663 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4664 !xhci->devs[udev->slot_id])
4665 return USB3_LPM_DISABLED;
4666
4667 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4668 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4669 if (mel < 0) {
4670 /* Max Exit Latency is too big, disable LPM. */
4671 hub_encoded_timeout = USB3_LPM_DISABLED;
4672 mel = 0;
4673 }
4674
4675 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4676 if (ret)
4677 return ret;
4678 return hub_encoded_timeout;
4679}
4680
4681int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4682 struct usb_device *udev, enum usb3_link_state state)
4683{
4684 struct xhci_hcd *xhci;
4685 u16 mel;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004686
4687 xhci = hcd_to_xhci(hcd);
4688 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4689 !xhci->devs[udev->slot_id])
4690 return 0;
4691
4692 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
Saurabh Karajgaonkarf1cda542015-08-04 14:04:09 +00004693 return xhci_change_max_exit_latency(xhci, udev, mel);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004694}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004695#else /* CONFIG_PM */
4696
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004697int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4698 struct usb_device *udev, int enable)
4699{
4700 return 0;
4701}
4702
4703int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4704{
4705 return 0;
4706}
4707
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004708int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4709 struct usb_device *udev, enum usb3_link_state state)
4710{
4711 return USB3_LPM_DISABLED;
4712}
4713
4714int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4715 struct usb_device *udev, enum usb3_link_state state)
4716{
4717 return 0;
4718}
4719#endif /* CONFIG_PM */
4720
Sarah Sharp3b3db022012-05-09 10:55:03 -07004721/*-------------------------------------------------------------------------*/
4722
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004723/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4724 * internal data structures for the device.
4725 */
4726int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4727 struct usb_tt *tt, gfp_t mem_flags)
4728{
4729 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4730 struct xhci_virt_device *vdev;
4731 struct xhci_command *config_cmd;
4732 struct xhci_input_control_ctx *ctrl_ctx;
4733 struct xhci_slot_ctx *slot_ctx;
4734 unsigned long flags;
4735 unsigned think_time;
4736 int ret;
4737
4738 /* Ignore root hubs */
4739 if (!hdev->parent)
4740 return 0;
4741
4742 vdev = xhci->devs[hdev->slot_id];
4743 if (!vdev) {
4744 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4745 return -EINVAL;
4746 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004747 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004748 if (!config_cmd) {
4749 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4750 return -ENOMEM;
4751 }
Lin Wang4daf9df2015-01-09 16:06:31 +02004752 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004753 if (!ctrl_ctx) {
4754 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4755 __func__);
4756 xhci_free_command(xhci, config_cmd);
4757 return -ENOMEM;
4758 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004759
4760 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004761 if (hdev->speed == USB_SPEED_HIGH &&
4762 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4763 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4764 xhci_free_command(xhci, config_cmd);
4765 spin_unlock_irqrestore(&xhci->lock, flags);
4766 return -ENOMEM;
4767 }
4768
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004769 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004770 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004771 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004772 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004773 /*
4774 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4775 * but it may be already set to 1 when setup an xHCI virtual
4776 * device, so clear it anyway.
4777 */
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004778 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004779 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004780 else if (hdev->speed == USB_SPEED_FULL)
4781 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
4782
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004783 if (xhci->hci_version > 0x95) {
4784 xhci_dbg(xhci, "xHCI version %x needs hub "
4785 "TT think time and number of ports\n",
4786 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004787 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004788 /* Set TT think time - convert from ns to FS bit times.
4789 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4790 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004791 *
4792 * xHCI 1.0: this field shall be 0 if the device is not a
4793 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004794 */
4795 think_time = tt->think_time;
4796 if (think_time != 0)
4797 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004798 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4799 slot_ctx->tt_info |=
4800 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004801 } else {
4802 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4803 "TT think time or number of ports\n",
4804 (unsigned int) xhci->hci_version);
4805 }
4806 slot_ctx->dev_state = 0;
4807 spin_unlock_irqrestore(&xhci->lock, flags);
4808
4809 xhci_dbg(xhci, "Set up %s for hub device.\n",
4810 (xhci->hci_version > 0x95) ?
4811 "configure endpoint" : "evaluate context");
4812 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4813 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4814
4815 /* Issue and wait for the configure endpoint or
4816 * evaluate context command.
4817 */
4818 if (xhci->hci_version > 0x95)
4819 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4820 false, false);
4821 else
4822 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4823 true, false);
4824
4825 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4826 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4827
4828 xhci_free_command(xhci, config_cmd);
4829 return ret;
4830}
4831
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004832int xhci_get_frame(struct usb_hcd *hcd)
4833{
4834 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4835 /* EHCI mods by the periodic size. Why? */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004836 return readl(&xhci->run_regs->microframe_index) >> 3;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004837}
4838
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004839int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4840{
4841 struct xhci_hcd *xhci;
Arnd Bergmanna9ff9112017-03-13 10:18:44 +08004842 /*
4843 * TODO: Check with DWC3 clients for sysdev according to
4844 * quirks
4845 */
4846 struct device *dev = hcd->self.sysdev;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004847 int retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004848
Sarah Sharp1386ff72014-01-31 11:45:02 -08004849 /* Accept arbitrarily long scatter-gather lists */
4850 hcd->self.sg_tablesize = ~0;
Ming Leifc760512013-08-08 21:48:23 +08004851
Mathias Nymane2ed5112014-03-07 17:06:57 +02004852 /* support to build packet from discontinuous buffers */
4853 hcd->self.no_sg_constraint = 1;
4854
Hans de Goede19181bc2012-07-04 09:18:02 +02004855 /* XHCI controllers don't stop the ep queue on short packets :| */
4856 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004857
Mathias Nymanb50107b2015-10-01 18:40:38 +03004858 xhci = hcd_to_xhci(hcd);
4859
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004860 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004861 xhci->main_hcd = hcd;
4862 /* Mark the first roothub as being USB 2.0.
4863 * The xHCI driver will register the USB 3.0 roothub.
4864 */
4865 hcd->speed = HCD_USB2;
4866 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4867 /*
4868 * USB 2.0 roothub under xHCI has an integrated TT,
4869 * (rate matching hub) as opposed to having an OHCI/UHCI
4870 * companion controller.
4871 */
4872 hcd->has_tt = 1;
4873 } else {
Mathias Nymandc1858a2017-10-06 17:45:27 +03004874 /* Some 3.1 hosts return sbrn 0x30, can't rely on sbrn alone */
4875 if (xhci->sbrn == 0x31 || xhci->usb3_rhub.min_rev >= 1) {
Mathias Nymanb50107b2015-10-01 18:40:38 +03004876 xhci_info(xhci, "Host supports USB 3.1 Enhanced SuperSpeed\n");
4877 hcd->speed = HCD_USB31;
Mathias Nyman2c0e06f2016-01-25 15:30:45 +02004878 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
Mathias Nymanb50107b2015-10-01 18:40:38 +03004879 }
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004880 /* xHCI private pointer was set in xhci_pci_probe for the second
4881 * registered roothub.
4882 */
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004883 return 0;
4884 }
4885
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004886 mutex_init(&xhci->mutex);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004887 xhci->cap_regs = hcd->regs;
4888 xhci->op_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004889 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004890 xhci->run_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004891 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004892 /* Cache read-only capability registers */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004893 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
4894 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
4895 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
4896 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004897 xhci->hci_version = HC_VERSION(xhci->hcc_params);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004898 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
Lu Baolu04abb6d2015-10-01 18:40:31 +03004899 if (xhci->hci_version > 0x100)
4900 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004901 xhci_print_registers(xhci);
4902
Mathias Nyman757de492016-06-01 18:09:10 +03004903 xhci->quirks |= quirks;
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +01004904
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004905 get_quirks(dev, xhci);
4906
George Cherian07f3cb72013-07-01 10:59:12 +05304907 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4908 * success event after a short transfer. This quirk will ignore such
4909 * spurious event.
4910 */
4911 if (xhci->hci_version > 0x96)
4912 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4913
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004914 /* Make sure the HC is halted. */
4915 retval = xhci_halt(xhci);
4916 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004917 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004918
4919 xhci_dbg(xhci, "Resetting HCD\n");
4920 /* Reset the internal HC memory state and registers. */
4921 retval = xhci_reset(xhci);
4922 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004923 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004924 xhci_dbg(xhci, "Reset complete\n");
4925
Yoshihiro Shimoda0a380be2016-04-08 16:25:07 +03004926 /*
4927 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
4928 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
4929 * address memory pointers actually. So, this driver clears the AC64
4930 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
4931 * DMA_BIT_MASK(32)) in this xhci_gen_setup().
4932 */
4933 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
4934 xhci->hcc_params &= ~BIT(0);
4935
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004936 /* Set dma_mask and coherent_dma_mask to 64-bits,
4937 * if xHC supports 64-bit addressing */
4938 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4939 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004940 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004941 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
Duc Dangfda182d2015-10-09 13:30:13 +03004942 } else {
4943 /*
4944 * This is to avoid error in cases where a 32-bit USB
4945 * controller is used on a 64-bit capable system.
4946 */
4947 retval = dma_set_mask(dev, DMA_BIT_MASK(32));
4948 if (retval)
4949 return retval;
4950 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
4951 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004952 }
4953
4954 xhci_dbg(xhci, "Calling HCD init\n");
4955 /* Initialize HCD and host controller data structures. */
4956 retval = xhci_init(hcd);
4957 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004958 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004959 xhci_dbg(xhci, "Called HCD init\n");
Hans de Goede99705092015-01-16 17:54:01 +02004960
4961 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%08x\n",
4962 xhci->hcc_params, xhci->hci_version, xhci->quirks);
4963
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004964 return 0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004965}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03004966EXPORT_SYMBOL_GPL(xhci_gen_setup);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004967
Hemant Kumar1346a802017-09-22 15:03:45 -07004968static phys_addr_t xhci_get_sec_event_ring_phys_addr(struct usb_hcd *hcd,
4969 unsigned int intr_num, dma_addr_t *dma)
Hemant Kumar258b4b42016-03-22 19:34:20 -07004970{
4971 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Hemant Kumar1346a802017-09-22 15:03:45 -07004972 struct device *dev = hcd->self.sysdev;
4973 struct sg_table sgt;
4974 phys_addr_t pa;
Hemant Kumar258b4b42016-03-22 19:34:20 -07004975
Hemant Kumar48cb3882017-02-07 11:50:13 -08004976 if (intr_num >= xhci->max_interrupters) {
4977 xhci_err(xhci, "intr num %d >= max intrs %d\n", intr_num,
Hemant Kumar258b4b42016-03-22 19:34:20 -07004978 xhci->max_interrupters);
4979 return 0;
4980 }
4981
4982 if (!(xhci->xhc_state & XHCI_STATE_HALTED) &&
4983 xhci->sec_event_ring && xhci->sec_event_ring[intr_num]
Hemant Kumar1346a802017-09-22 15:03:45 -07004984 && xhci->sec_event_ring[intr_num]->first_seg) {
4985
4986 dma_get_sgtable(dev, &sgt,
4987 xhci->sec_event_ring[intr_num]->first_seg->trbs,
4988 xhci->sec_event_ring[intr_num]->first_seg->dma,
4989 TRB_SEGMENT_SIZE);
4990
4991 *dma = xhci->sec_event_ring[intr_num]->first_seg->dma;
4992
4993 pa = page_to_phys(sg_page(sgt.sgl));
4994 sg_free_table(&sgt);
4995
4996 return pa;
4997 }
Hemant Kumar258b4b42016-03-22 19:34:20 -07004998
4999 return 0;
5000}
5001
Hemant Kumar1346a802017-09-22 15:03:45 -07005002static phys_addr_t xhci_get_xfer_ring_phys_addr(struct usb_hcd *hcd,
5003 struct usb_device *udev, struct usb_host_endpoint *ep, dma_addr_t *dma)
Hemant Kumar258b4b42016-03-22 19:34:20 -07005004{
5005 int ret;
5006 unsigned int ep_index;
5007 struct xhci_virt_device *virt_dev;
Hemant Kumar1346a802017-09-22 15:03:45 -07005008 struct device *dev = hcd->self.sysdev;
Hemant Kumar258b4b42016-03-22 19:34:20 -07005009 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Hemant Kumar1346a802017-09-22 15:03:45 -07005010 struct sg_table sgt;
5011 phys_addr_t pa;
Hemant Kumar258b4b42016-03-22 19:34:20 -07005012
5013 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
5014 if (ret <= 0) {
5015 xhci_err(xhci, "%s: invalid args\n", __func__);
5016 return 0;
5017 }
5018
5019 virt_dev = xhci->devs[udev->slot_id];
5020 ep_index = xhci_get_endpoint_index(&ep->desc);
5021
5022 if (virt_dev->eps[ep_index].ring &&
Hemant Kumar1346a802017-09-22 15:03:45 -07005023 virt_dev->eps[ep_index].ring->first_seg) {
5024
5025 dma_get_sgtable(dev, &sgt,
5026 virt_dev->eps[ep_index].ring->first_seg->trbs,
5027 virt_dev->eps[ep_index].ring->first_seg->dma,
5028 TRB_SEGMENT_SIZE);
5029
5030 *dma = virt_dev->eps[ep_index].ring->first_seg->dma;
5031
5032 pa = page_to_phys(sg_page(sgt.sgl));
5033 sg_free_table(&sgt);
5034
5035 return pa;
5036 }
Hemant Kumar258b4b42016-03-22 19:34:20 -07005037
5038 return 0;
5039}
5040
Hemant Kumarf0cdec42017-08-18 16:59:33 -07005041int xhci_get_core_id(struct usb_hcd *hcd)
5042{
5043 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
5044
5045 return xhci->core_id;
5046}
5047
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005048static const struct hc_driver xhci_hc_driver = {
5049 .description = "xhci-hcd",
5050 .product_desc = "xHCI Host Controller",
Yoshihiro Shimoda32479d42015-11-24 13:09:47 +02005051 .hcd_priv_size = sizeof(struct xhci_hcd),
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005052
5053 /*
5054 * generic hardware linkage
5055 */
5056 .irq = xhci_irq,
5057 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
5058
5059 /*
5060 * basic lifecycle operations
5061 */
5062 .reset = NULL, /* set in xhci_init_driver() */
5063 .start = xhci_run,
5064 .stop = xhci_stop,
5065 .shutdown = xhci_shutdown,
5066
5067 /*
5068 * managing i/o requests and associated device resources
5069 */
5070 .urb_enqueue = xhci_urb_enqueue,
5071 .urb_dequeue = xhci_urb_dequeue,
5072 .alloc_dev = xhci_alloc_dev,
5073 .free_dev = xhci_free_dev,
5074 .alloc_streams = xhci_alloc_streams,
5075 .free_streams = xhci_free_streams,
5076 .add_endpoint = xhci_add_endpoint,
5077 .drop_endpoint = xhci_drop_endpoint,
5078 .endpoint_reset = xhci_endpoint_reset,
5079 .check_bandwidth = xhci_check_bandwidth,
5080 .reset_bandwidth = xhci_reset_bandwidth,
5081 .address_device = xhci_address_device,
5082 .enable_device = xhci_enable_device,
5083 .update_hub_device = xhci_update_hub_device,
5084 .reset_device = xhci_discover_or_reset_device,
5085
5086 /*
5087 * scheduling support
5088 */
5089 .get_frame_number = xhci_get_frame,
5090
5091 /*
5092 * root hub support
5093 */
5094 .hub_control = xhci_hub_control,
5095 .hub_status_data = xhci_hub_status_data,
5096 .bus_suspend = xhci_bus_suspend,
5097 .bus_resume = xhci_bus_resume,
5098
5099 /*
5100 * call back when device connected and addressed
5101 */
5102 .update_device = xhci_update_device,
5103 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
5104 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
5105 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
5106 .find_raw_port_number = xhci_find_raw_port_number,
Hemant Kumar8aad8422016-03-22 13:41:59 -07005107 .sec_event_ring_setup = xhci_sec_event_ring_setup,
5108 .sec_event_ring_cleanup = xhci_sec_event_ring_cleanup,
Hemant Kumar1346a802017-09-22 15:03:45 -07005109 .get_sec_event_ring_phys_addr = xhci_get_sec_event_ring_phys_addr,
5110 .get_xfer_ring_phys_addr = xhci_get_xfer_ring_phys_addr,
Hemant Kumarf0cdec42017-08-18 16:59:33 -07005111 .get_core_id = xhci_get_core_id,
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005112};
5113
Roger Quadroscd33a322015-05-29 17:01:46 +03005114void xhci_init_driver(struct hc_driver *drv,
5115 const struct xhci_driver_overrides *over)
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005116{
Roger Quadroscd33a322015-05-29 17:01:46 +03005117 BUG_ON(!over);
5118
5119 /* Copy the generic table to drv then apply the overrides */
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005120 *drv = xhci_hc_driver;
Roger Quadroscd33a322015-05-29 17:01:46 +03005121
5122 if (over) {
5123 drv->hcd_priv_size += over->extra_priv_size;
5124 if (over->reset)
5125 drv->reset = over->reset;
5126 if (over->start)
5127 drv->start = over->start;
5128 }
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005129}
5130EXPORT_SYMBOL_GPL(xhci_init_driver);
5131
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005132MODULE_DESCRIPTION(DRIVER_DESC);
5133MODULE_AUTHOR(DRIVER_AUTHOR);
5134MODULE_LICENSE("GPL");
5135
5136static int __init xhci_hcd_init(void)
5137{
Sarah Sharp98441972009-05-14 11:44:18 -07005138 /*
5139 * Check the compiler generated sizes of structures that must be laid
5140 * out in specific ways for hardware access.
5141 */
5142 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
5143 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
5144 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
5145 /* xhci_device_control has eight fields, and also
5146 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5147 */
Sarah Sharp98441972009-05-14 11:44:18 -07005148 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
5149 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
5150 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
Lu Baolu04abb6d2015-10-01 18:40:31 +03005151 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
Sarah Sharp98441972009-05-14 11:44:18 -07005152 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
5153 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5154 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Oliver Neukum1eaf35e2015-12-03 15:03:34 +01005155
5156 if (usb_disabled())
5157 return -ENODEV;
5158
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005159 return 0;
5160}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005161
5162/*
5163 * If an init function is provided, an exit function must also be provided
5164 * to allow module unload.
5165 */
5166static void __exit xhci_hcd_fini(void) { }
5167
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005168module_init(xhci_hcd_init);
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005169module_exit(xhci_hcd_fini);