blob: 51535ba2bcd425d6c039d30ed4b63283c5d39240 [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);
Mathias Nyman99154fd2016-11-11 15:13:11 +0200116 if (ret) {
117 xhci_warn(xhci, "Host halt failed, %d\n", ret);
118 return ret;
119 }
120 xhci->xhc_state |= XHCI_STATE_HALTED;
121 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800122 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700123}
124
125/*
Sarah Sharped074532010-05-24 13:25:21 -0700126 * Set the run bit and wait for the host to be running.
127 */
Guoqing Zhang26bba5c2017-04-07 17:56:53 +0300128int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700129{
130 u32 temp;
131 int ret;
132
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200133 temp = readl(&xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700134 temp |= (CMD_RUN);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300135 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
Sarah Sharped074532010-05-24 13:25:21 -0700136 temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200137 writel(temp, &xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700138
139 /*
140 * Wait for the HCHalted Status bit to be 0 to indicate the host is
141 * running.
142 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200143 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharped074532010-05-24 13:25:21 -0700144 STS_HALT, 0, XHCI_MAX_HALT_USEC);
145 if (ret == -ETIMEDOUT)
146 xhci_err(xhci, "Host took too long to start, "
147 "waited %u microseconds.\n",
148 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800149 if (!ret)
Mathias Nyman98d74f92016-04-08 16:25:10 +0300150 /* clear state flags. Including dying, halted or removing */
151 xhci->xhc_state = 0;
Roger Quadrose5bfeab2015-09-21 17:46:13 +0300152
Sarah Sharped074532010-05-24 13:25:21 -0700153 return ret;
154}
155
156/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800157 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700158 *
159 * This resets pipelines, timers, counters, state machines, etc.
160 * Transactions will be terminated immediately, and operational registers
161 * will be set to their defaults.
162 */
163int xhci_reset(struct xhci_hcd *xhci)
164{
165 u32 command;
166 u32 state;
Andiry Xuf370b992012-04-14 02:54:30 +0800167 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700168
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200169 state = readl(&xhci->op_regs->status);
Mathias Nymanc11ae032016-11-11 15:13:12 +0200170
171 if (state == ~(u32)0) {
172 xhci_warn(xhci, "Host not accessible, reset failed.\n");
173 return -ENODEV;
174 }
175
Sarah Sharpd3512f62009-07-27 12:03:50 -0700176 if ((state & STS_HALT) == 0) {
177 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
178 return 0;
179 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700180
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300181 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200182 command = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700183 command |= CMD_RESET;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200184 writel(command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700185
Rajmohan Mania5964392015-11-18 10:48:20 +0200186 /* Existing Intel xHCI controllers require a delay of 1 mS,
187 * after setting the CMD_RESET bit, and before accessing any
188 * HC registers. This allows the HC to complete the
189 * reset operation and be ready for HC register access.
190 * Without this delay, the subsequent HC register access,
191 * may result in a system hang very rarely.
192 */
193 if (xhci->quirks & XHCI_INTEL_HOST)
194 udelay(1000);
195
Lin Wangdc0b1772015-01-09 16:06:28 +0200196 ret = xhci_handshake(&xhci->op_regs->command,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700197 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700198 if (ret)
199 return ret;
200
Jiahau Chang9da5a102017-07-20 14:48:27 +0300201 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
202 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));
203
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300204 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
205 "Wait for controller to be ready for doorbell rings");
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700206 /*
207 * xHCI cannot write to any doorbells or operational registers other
208 * than status until the "Controller Not Ready" flag is cleared.
209 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200210 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700211 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xuf370b992012-04-14 02:54:30 +0800212
Felipe Balbi98871e92017-01-23 14:20:04 +0200213 for (i = 0; i < 2; i++) {
Andiry Xuf370b992012-04-14 02:54:30 +0800214 xhci->bus_state[i].port_c_suspend = 0;
215 xhci->bus_state[i].suspended_ports = 0;
216 xhci->bus_state[i].resuming_ports = 0;
217 }
218
219 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700220}
221
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300222
yuan linyu2c93e792017-02-25 19:20:55 +0800223#ifdef CONFIG_USB_PCI
Dong Nguyen43b86af2010-07-21 16:56:08 -0700224/*
225 * Set up MSI
226 */
227static int xhci_setup_msi(struct xhci_hcd *xhci)
228{
229 int ret;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800230 /*
231 * TODO:Check with MSI Soc for sysdev
232 */
Dong Nguyen43b86af2010-07-21 16:56:08 -0700233 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
234
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300235 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
236 if (ret < 0) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300237 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
238 "failed to allocate MSI entry");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700239 return ret;
240 }
241
Alex Shi851ec162013-05-24 10:54:19 +0800242 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700243 0, "xhci_hcd", xhci_to_hcd(xhci));
244 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300245 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
246 "disable MSI interrupt");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300247 pci_free_irq_vectors(pdev);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700248 }
249
250 return ret;
251}
252
253/*
254 * Set up MSI-X
255 */
256static int xhci_setup_msix(struct xhci_hcd *xhci)
257{
258 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800259 struct usb_hcd *hcd = xhci_to_hcd(xhci);
260 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700261
262 /*
263 * calculate number of msi-x vectors supported.
264 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
265 * with max number of interrupters based on the xhci HCSPARAMS1.
266 * - num_online_cpus: maximum msi-x vectors per CPUs core.
267 * Add additional 1 vector to ensure always available interrupt.
268 */
269 xhci->msix_count = min(num_online_cpus() + 1,
270 HCS_MAX_INTRS(xhci->hcs_params1));
271
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300272 ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count,
273 PCI_IRQ_MSIX);
274 if (ret < 0) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300275 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
276 "Failed to enable MSI-X");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300277 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700278 }
279
Dong Nguyen43b86af2010-07-21 16:56:08 -0700280 for (i = 0; i < xhci->msix_count; i++) {
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300281 ret = request_irq(pci_irq_vector(pdev, i), xhci_msi_irq, 0,
282 "xhci_hcd", xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700283 if (ret)
284 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700285 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700286
Andiry Xu00292272010-12-27 17:39:02 +0800287 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700288 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700289
290disable_msix:
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300291 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300292 while (--i >= 0)
293 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
294 pci_free_irq_vectors(pdev);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700295 return ret;
296}
297
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700298/* Free any IRQs and disable MSI-X */
299static void xhci_cleanup_msix(struct xhci_hcd *xhci)
300{
Andiry Xu00292272010-12-27 17:39:02 +0800301 struct usb_hcd *hcd = xhci_to_hcd(xhci);
302 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700303
Jack Pham90053552013-11-15 14:53:14 -0800304 if (xhci->quirks & XHCI_PLAT)
305 return;
306
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300307 /* return if using legacy interrupt */
308 if (hcd->irq > 0)
309 return;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700310
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300311 if (hcd->msix_enabled) {
312 int i;
313
314 for (i = 0; i < xhci->msix_count; i++)
315 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700316 } else {
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300317 free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700318 }
319
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300320 pci_free_irq_vectors(pdev);
Andiry Xu00292272010-12-27 17:39:02 +0800321 hcd->msix_enabled = 0;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700322}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700323
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700324static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700325{
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300326 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700327
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300328 if (hcd->msix_enabled) {
329 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
330 int i;
331
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700332 for (i = 0; i < xhci->msix_count; i++)
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300333 synchronize_irq(pci_irq_vector(pdev, i));
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700334 }
335}
336
337static int xhci_try_enable_msi(struct usb_hcd *hcd)
338{
339 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700340 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700341 int ret;
342
Sarah Sharp52fb6122013-08-08 10:08:34 -0700343 /* The xhci platform device has set up IRQs through usb_add_hcd. */
344 if (xhci->quirks & XHCI_PLAT)
345 return 0;
346
347 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700348 /*
349 * Some Fresco Logic host controllers advertise MSI, but fail to
350 * generate interrupts. Don't even try to enable MSI.
351 */
352 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100353 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700354
355 /* unregister the legacy interrupt */
356 if (hcd->irq)
357 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200358 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700359
360 ret = xhci_setup_msix(xhci);
361 if (ret)
362 /* fall back to msi*/
363 ret = xhci_setup_msi(xhci);
364
Peter Chen6a29bee2017-05-17 18:32:02 +0300365 if (!ret) {
366 hcd->msi_enabled = 1;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700367 return 0;
Peter Chen6a29bee2017-05-17 18:32:02 +0300368 }
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700369
Sarah Sharp68d07f62012-02-13 16:25:57 -0800370 if (!pdev->irq) {
371 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
372 return -EINVAL;
373 }
374
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100375 legacy_irq:
Adrian Huang79699432014-02-27 11:26:03 +0000376 if (!strlen(hcd->irq_descr))
377 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
378 hcd->driver->description, hcd->self.busnum);
379
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700380 /* fall back to legacy interrupt*/
381 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
382 hcd->irq_descr, hcd);
383 if (ret) {
384 xhci_err(xhci, "request interrupt %d failed\n",
385 pdev->irq);
386 return ret;
387 }
388 hcd->irq = pdev->irq;
389 return 0;
390}
391
392#else
393
David Cohen01bb59e2014-04-25 19:20:16 +0300394static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700395{
396 return 0;
397}
398
David Cohen01bb59e2014-04-25 19:20:16 +0300399static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700400{
401}
402
David Cohen01bb59e2014-04-25 19:20:16 +0300403static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700404{
405}
406
407#endif
408
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500409static void compliance_mode_recovery(unsigned long arg)
410{
411 struct xhci_hcd *xhci;
412 struct usb_hcd *hcd;
413 u32 temp;
414 int i;
415
416 xhci = (struct xhci_hcd *)arg;
417
418 for (i = 0; i < xhci->num_usb3_ports; i++) {
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200419 temp = readl(xhci->usb3_ports[i]);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500420 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
421 /*
422 * Compliance Mode Detected. Letting USB Core
423 * handle the Warm Reset
424 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300425 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
426 "Compliance mode detected->port %d",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500427 i + 1);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300428 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
429 "Attempting compliance mode recovery");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500430 hcd = xhci->shared_hcd;
431
432 if (hcd->state == HC_STATE_SUSPENDED)
433 usb_hcd_resume_root_hub(hcd);
434
435 usb_hcd_poll_rh_status(hcd);
436 }
437 }
438
439 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
440 mod_timer(&xhci->comp_mode_recovery_timer,
441 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
442}
443
444/*
445 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
446 * that causes ports behind that hardware to enter compliance mode sometimes.
447 * The quirk creates a timer that polls every 2 seconds the link state of
448 * each host controller's port and recovers it by issuing a Warm reset
449 * if Compliance mode is detected, otherwise the port will become "dead" (no
450 * device connections or disconnections will be detected anymore). Becasue no
451 * status event is generated when entering compliance mode (per xhci spec),
452 * this quirk is needed on systems that have the failing hardware installed.
453 */
454static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
455{
456 xhci->port_status_u0 = 0;
Julia Lawallfc8abe02015-01-09 16:06:29 +0200457 setup_timer(&xhci->comp_mode_recovery_timer,
458 compliance_mode_recovery, (unsigned long)xhci);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500459 xhci->comp_mode_recovery_timer.expires = jiffies +
460 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
461
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500462 add_timer(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300463 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
464 "Compliance mode recovery timer initialized");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500465}
466
467/*
468 * This function identifies the systems that have installed the SN65LVPE502CP
469 * USB3.0 re-driver and that need the Compliance Mode Quirk.
470 * Systems:
471 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
472 */
Andrew Brestickere1cd9722014-10-03 11:35:27 +0300473static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500474{
475 const char *dmi_product_name, *dmi_sys_vendor;
476
477 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
478 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530479 if (!dmi_product_name || !dmi_sys_vendor)
480 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500481
482 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
483 return false;
484
485 if (strstr(dmi_product_name, "Z420") ||
486 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500487 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600488 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500489 return true;
490
491 return false;
492}
493
494static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
495{
496 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
497}
498
499
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700500/*
501 * Initialize memory for HCD and xHC (one-time init).
502 *
503 * Program the PAGESIZE register, initialize the device context array, create
504 * device contexts (?), set up a command ring segment (or two?), create event
505 * ring (one for now).
506 */
Lu Baolu39693842017-04-07 17:57:04 +0300507static int xhci_init(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700508{
509 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
510 int retval = 0;
511
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300512 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700513 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700514 if (xhci->hci_version == 0x95 && link_quirk) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300515 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
516 "QUIRK: Not clearing Link TRB chain bits.");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700517 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
518 } else {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300519 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
520 "xHCI doesn't need link TRB QUIRK");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700521 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700522 retval = xhci_mem_init(xhci, GFP_KERNEL);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300523 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700524
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500525 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700526 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500527 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
528 compliance_mode_recovery_timer_init(xhci);
529 }
530
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700531 return retval;
532}
533
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700534/*-------------------------------------------------------------------------*/
535
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700536
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800537static int xhci_run_finished(struct xhci_hcd *xhci)
538{
539 if (xhci_start(xhci)) {
540 xhci_halt(xhci);
541 return -ENODEV;
542 }
543 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800544 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800545
546 if (xhci->quirks & XHCI_NEC_HOST)
547 xhci_ring_cmd_db(xhci);
548
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300549 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
550 "Finished xhci_run for USB3 roothub");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800551 return 0;
552}
553
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700554/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700555 * Start the HC after it was halted.
556 *
557 * This function is called by the USB core when the HC driver is added.
558 * Its opposite is xhci_stop().
559 *
560 * xhci_init() must be called once before this function can be called.
561 * Reset the HC, enable device slot contexts, program DCBAAP, and
562 * set command ring pointer and event ring pointer.
563 *
564 * Setup MSI-X vectors and enable interrupts.
565 */
566int xhci_run(struct usb_hcd *hcd)
567{
568 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700569 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700570 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700571 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700572
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800573 /* Start the xHCI host controller running only after the USB 2.0 roothub
574 * is setup.
575 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700576
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700577 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800578 if (!usb_hcd_is_primary_hcd(hcd))
579 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700580
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300581 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700582
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700583 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700584 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700585 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700586
Sarah Sharp66e49d82009-07-27 12:03:46 -0700587 xhci_dbg_cmd_ptrs(xhci);
588
589 xhci_dbg(xhci, "ERST memory map follows:\n");
590 xhci_dbg_erst(xhci, &xhci->erst);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800591 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700592 temp_64 &= ~ERST_PTR_MASK;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300593 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
594 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700595
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300596 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
597 "// Set the interrupt modulation register");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200598 temp = readl(&xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700599 temp &= ~ER_IRQ_INTERVAL_MASK;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200600 /*
601 * the increment interval is 8 times as much as that defined
602 * in xHCI spec on MTK's controller
603 */
604 temp |= (u32) ((xhci->quirks & XHCI_MTK_HOST) ? 20 : 160);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200605 writel(temp, &xhci->ir_set->irq_control);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700606
607 /* Set the HCD state before we enable the irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200608 temp = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700609 temp |= (CMD_EIE);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300610 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
611 "// Enable interrupts, cmd = 0x%x.", temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200612 writel(temp, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700613
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200614 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300615 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
616 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700617 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200618 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800619 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700620
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300621 if (xhci->quirks & XHCI_NEC_HOST) {
622 struct xhci_command *command;
Lu Baolu74e0b562017-04-07 17:57:05 +0300623
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300624 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
625 if (!command)
626 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +0300627
Shu Wangd6f5f072017-07-20 14:48:31 +0300628 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0,
Sarah Sharp02386342010-05-24 13:25:28 -0700629 TRB_TYPE(TRB_NEC_GET_FW));
Shu Wangd6f5f072017-07-20 14:48:31 +0300630 if (ret)
631 xhci_free_command(xhci, command);
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300632 }
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300633 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
634 "Finished xhci_run for USB2 roothub");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700635 return 0;
636}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300637EXPORT_SYMBOL_GPL(xhci_run);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700638
639/*
640 * Stop xHCI driver.
641 *
642 * This function is called by the USB core when the HC driver is removed.
643 * Its opposite is xhci_run().
644 *
645 * Disable device contexts, disable IRQs, and quiesce the HC.
646 * Reset the HC, finish any completed transactions, and cleanup memory.
647 */
Lu Baolu39693842017-04-07 17:57:04 +0300648static void xhci_stop(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700649{
650 u32 temp;
651 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
652
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300653 mutex_lock(&xhci->mutex);
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300654
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300655 /* Only halt host and free memory after both hcds are removed */
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300656 if (!usb_hcd_is_primary_hcd(hcd)) {
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300657 /* usb core will free this hcd shortly, unset pointer */
658 xhci->shared_hcd = NULL;
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300659 mutex_unlock(&xhci->mutex);
660 return;
661 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700662
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300663 spin_lock_irq(&xhci->lock);
664 xhci->xhc_state |= XHCI_STATE_HALTED;
665 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
666 xhci_halt(xhci);
667 xhci_reset(xhci);
668 spin_unlock_irq(&xhci->lock);
669
Zhang Rui40a9fb12010-12-17 13:17:04 -0800670 xhci_cleanup_msix(xhci);
671
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500672 /* Deleting Compliance Mode Recovery Timer */
673 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400674 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500675 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300676 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
677 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400678 __func__);
679 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500680
Andiry Xuc41136b2011-03-22 17:08:14 +0800681 if (xhci->quirks & XHCI_AMD_PLL_FIX)
682 usb_amd_dev_put();
683
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300684 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
685 "// Disabling event ring interrupts");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200686 temp = readl(&xhci->op_regs->status);
Lu Baolud1001ab2017-04-07 17:56:50 +0300687 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200688 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200689 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800690 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700691
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300692 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700693 xhci_mem_cleanup(xhci);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300694 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
695 "xhci_stop completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200696 readl(&xhci->op_regs->status));
Roger Quadros85ac90f2015-09-21 17:46:12 +0300697 mutex_unlock(&xhci->mutex);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700698}
699
700/*
701 * Shutdown HC (not bus-specific)
702 *
703 * This is called when the machine is rebooting or halting. We assume that the
704 * machine will be powered off, and the HC's internal state will be reset.
705 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800706 *
707 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700708 */
Lu Baolu39693842017-04-07 17:57:04 +0300709static void xhci_shutdown(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700710{
711 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
712
Dan Carpenter052c7f92012-08-13 19:57:03 +0300713 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800714 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
Sarah Sharpe95829f2012-07-23 18:59:30 +0300715
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700716 spin_lock_irq(&xhci->lock);
717 xhci_halt(xhci);
Takashi Iwai638298d2013-09-12 08:11:06 +0200718 /* Workaround for spurious wakeups at shutdown with HSW */
719 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
720 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700721 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700722
Zhang Rui40a9fb12010-12-17 13:17:04 -0800723 xhci_cleanup_msix(xhci);
724
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300725 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
726 "xhci_shutdown completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200727 readl(&xhci->op_regs->status));
Takashi Iwai638298d2013-09-12 08:11:06 +0200728
729 /* Yet another workaround for spurious wakeups at shutdown with HSW */
730 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800731 pci_set_power_state(to_pci_dev(hcd->self.sysdev), PCI_D3hot);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700732}
733
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700734#ifdef CONFIG_PM
Andiry Xu5535b1d52010-10-14 07:23:06 -0700735static void xhci_save_registers(struct xhci_hcd *xhci)
736{
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200737 xhci->s3.command = readl(&xhci->op_regs->command);
738 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800739 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200740 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
741 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800742 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
743 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200744 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
745 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700746}
747
748static void xhci_restore_registers(struct xhci_hcd *xhci)
749{
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200750 writel(xhci->s3.command, &xhci->op_regs->command);
751 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
Sarah Sharp477632d2014-01-29 14:02:00 -0800752 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200753 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
754 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
Sarah Sharp477632d2014-01-29 14:02:00 -0800755 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
756 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200757 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
758 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700759}
760
Sarah Sharp89821322010-11-12 11:59:31 -0800761static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
762{
763 u64 val_64;
764
765 /* step 2: initialize command ring buffer */
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800766 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800767 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
768 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
769 xhci->cmd_ring->dequeue) &
770 (u64) ~CMD_RING_RSVD_BITS) |
771 xhci->cmd_ring->cycle_state;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300772 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
773 "// Setting command ring address to 0x%llx",
Sarah Sharp89821322010-11-12 11:59:31 -0800774 (long unsigned long) val_64);
Sarah Sharp477632d2014-01-29 14:02:00 -0800775 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800776}
777
778/*
779 * The whole command ring must be cleared to zero when we suspend the host.
780 *
781 * The host doesn't save the command ring pointer in the suspend well, so we
782 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
783 * aligned, because of the reserved bits in the command ring dequeue pointer
784 * register. Therefore, we can't just set the dequeue pointer back in the
785 * middle of the ring (TRBs are 16-byte aligned).
786 */
787static void xhci_clear_command_ring(struct xhci_hcd *xhci)
788{
789 struct xhci_ring *ring;
790 struct xhci_segment *seg;
791
792 ring = xhci->cmd_ring;
793 seg = ring->deq_seg;
794 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800795 memset(seg->trbs, 0,
796 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
797 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
798 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800799 seg = seg->next;
800 } while (seg != ring->deq_seg);
801
802 /* Reset the software enqueue and dequeue pointers */
803 ring->deq_seg = ring->first_seg;
804 ring->dequeue = ring->first_seg->trbs;
805 ring->enq_seg = ring->deq_seg;
806 ring->enqueue = ring->dequeue;
807
Andiry Xub008df62012-03-05 17:49:34 +0800808 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800809 /*
810 * Ring is now zeroed, so the HW should look for change of ownership
811 * when the cycle bit is set to 1.
812 */
813 ring->cycle_state = 1;
814
815 /*
816 * Reset the hardware dequeue pointer.
817 * Yes, this will need to be re-written after resume, but we're paranoid
818 * and want to make sure the hardware doesn't access bogus memory
819 * because, say, the BIOS or an SMI started the host without changing
820 * the command ring pointers.
821 */
822 xhci_set_cmd_ring_deq(xhci);
823}
824
Lu Baolua1377e52014-11-18 11:27:14 +0200825static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
826{
827 int port_index;
828 __le32 __iomem **port_array;
829 unsigned long flags;
830 u32 t1, t2;
831
832 spin_lock_irqsave(&xhci->lock, flags);
833
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800834 /* disable usb3 ports Wake bits */
Lu Baolua1377e52014-11-18 11:27:14 +0200835 port_index = xhci->num_usb3_ports;
836 port_array = xhci->usb3_ports;
837 while (port_index--) {
838 t1 = readl(port_array[port_index]);
839 t1 = xhci_port_state_to_neutral(t1);
840 t2 = t1 & ~PORT_WAKE_BITS;
841 if (t1 != t2)
842 writel(t2, port_array[port_index]);
843 }
844
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800845 /* disable usb2 ports Wake bits */
Lu Baolua1377e52014-11-18 11:27:14 +0200846 port_index = xhci->num_usb2_ports;
847 port_array = xhci->usb2_ports;
848 while (port_index--) {
849 t1 = readl(port_array[port_index]);
850 t1 = xhci_port_state_to_neutral(t1);
851 t2 = t1 & ~PORT_WAKE_BITS;
852 if (t1 != t2)
853 writel(t2, port_array[port_index]);
854 }
855
856 spin_unlock_irqrestore(&xhci->lock, flags);
857}
858
Andiry Xu5535b1d52010-10-14 07:23:06 -0700859/*
860 * Stop HC (not bus-specific)
861 *
862 * This is called when the machine transition into S3/S4 mode.
863 *
864 */
Lu Baolua1377e52014-11-18 11:27:14 +0200865int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
Andiry Xu5535b1d52010-10-14 07:23:06 -0700866{
867 int rc = 0;
Oliver Neukum455f5892013-09-30 15:50:54 +0200868 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700869 struct usb_hcd *hcd = xhci_to_hcd(xhci);
870 u32 command;
871
Roger Quadros9fa733f2015-05-29 17:01:50 +0300872 if (!hcd->state)
873 return 0;
874
Felipe Balbi77b84762012-10-19 10:55:16 +0300875 if (hcd->state != HC_STATE_SUSPENDED ||
876 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
877 return -EINVAL;
878
Lu Baolua1377e52014-11-18 11:27:14 +0200879 /* Clear root port wake on bits if wakeup not allowed. */
880 if (!do_wakeup)
881 xhci_disable_port_wake_on_bits(xhci);
882
Sarah Sharpc52804a2012-11-27 12:30:23 -0800883 /* Don't poll the roothubs on bus suspend. */
884 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
885 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
886 del_timer_sync(&hcd->rh_timer);
Al Cooper14e61a12014-08-20 16:41:57 +0300887 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
888 del_timer_sync(&xhci->shared_hcd->rh_timer);
Sarah Sharpc52804a2012-11-27 12:30:23 -0800889
Andiry Xu5535b1d52010-10-14 07:23:06 -0700890 spin_lock_irq(&xhci->lock);
891 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb32093792011-03-07 11:24:07 -0800892 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700893 /* step 1: stop endpoint */
894 /* skipped assuming that port suspend has done */
895
896 /* step 2: clear Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200897 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700898 command &= ~CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200899 writel(command, &xhci->op_regs->command);
Oliver Neukum455f5892013-09-30 15:50:54 +0200900
901 /* Some chips from Fresco Logic need an extraordinary delay */
902 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
903
Lin Wangdc0b1772015-01-09 16:06:28 +0200904 if (xhci_handshake(&xhci->op_regs->status,
Oliver Neukum455f5892013-09-30 15:50:54 +0200905 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d52010-10-14 07:23:06 -0700906 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
907 spin_unlock_irq(&xhci->lock);
908 return -ETIMEDOUT;
909 }
Sarah Sharp89821322010-11-12 11:59:31 -0800910 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700911
912 /* step 3: save registers */
913 xhci_save_registers(xhci);
914
915 /* step 4: set CSS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200916 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700917 command |= CMD_CSS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200918 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +0200919 if (xhci_handshake(&xhci->op_regs->status,
Sarah Sharp2611bd182012-10-25 13:27:51 -0700920 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +0800921 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d52010-10-14 07:23:06 -0700922 spin_unlock_irq(&xhci->lock);
923 return -ETIMEDOUT;
924 }
Andiry Xu5535b1d52010-10-14 07:23:06 -0700925 spin_unlock_irq(&xhci->lock);
926
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500927 /*
928 * Deleting Compliance Mode Recovery Timer because the xHCI Host
929 * is about to be suspended.
930 */
931 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
932 (!(xhci_all_ports_seen_u0(xhci)))) {
933 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300934 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
935 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400936 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500937 }
938
Andiry Xu00292272010-12-27 17:39:02 +0800939 /* step 5: remove core well power */
940 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700941 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800942
Andiry Xu5535b1d52010-10-14 07:23:06 -0700943 return rc;
944}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300945EXPORT_SYMBOL_GPL(xhci_suspend);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700946
947/*
948 * start xHC (not bus-specific)
949 *
950 * This is called when the machine transition from S3/S4 mode.
951 *
952 */
953int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
954{
Wang, Yud6236f62014-06-24 17:14:44 +0300955 u32 command, temp = 0, status;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700956 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -0800957 struct usb_hcd *secondary_hcd;
Alan Sternf69e31202011-11-03 11:37:10 -0400958 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -0500959 bool comp_timer_running = false;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700960
Roger Quadros9fa733f2015-05-29 17:01:50 +0300961 if (!hcd->state)
962 return 0;
963
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800964 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300965 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800966 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800967 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
968 time_before(jiffies,
969 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d52010-10-14 07:23:06 -0700970 msleep(100);
971
Alan Sternf69e31202011-11-03 11:37:10 -0400972 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
973 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
974
Andiry Xu5535b1d52010-10-14 07:23:06 -0700975 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +0200976 if (xhci->quirks & XHCI_RESET_ON_RESUME)
977 hibernated = true;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700978
979 if (!hibernated) {
980 /* step 1: restore register */
981 xhci_restore_registers(xhci);
982 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800983 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700984 /* step 3: restore state and start state*/
985 /* step 3: set CRS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200986 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700987 command |= CMD_CRS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200988 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +0200989 if (xhci_handshake(&xhci->op_regs->status,
Andiry Xu622eb782012-06-13 10:51:57 +0800990 STS_RESTORE, 0, 10 * 1000)) {
991 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d52010-10-14 07:23:06 -0700992 spin_unlock_irq(&xhci->lock);
993 return -ETIMEDOUT;
994 }
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200995 temp = readl(&xhci->op_regs->status);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700996 }
997
998 /* If restore operation fails, re-initialize the HC during resume */
999 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -05001000
1001 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1002 !(xhci_all_ports_seen_u0(xhci))) {
1003 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001004 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1005 "Compliance Mode Recovery Timer deleted!");
Tony Camuso77df9e02013-02-21 16:11:27 -05001006 }
1007
Sarah Sharpfedd3832011-04-12 17:43:19 -07001008 /* Let the USB core know _both_ roothubs lost power. */
1009 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1010 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001011
1012 xhci_dbg(xhci, "Stop HCD\n");
1013 xhci_halt(xhci);
1014 xhci_reset(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001015 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001016 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001017
Andiry Xu5535b1d52010-10-14 07:23:06 -07001018 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001019 temp = readl(&xhci->op_regs->status);
Lu Baolud1001ab2017-04-07 17:56:50 +03001020 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001021 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001022 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -08001023 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001024
1025 xhci_dbg(xhci, "cleaning up memory\n");
1026 xhci_mem_cleanup(xhci);
1027 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001028 readl(&xhci->op_regs->status));
Andiry Xu5535b1d52010-10-14 07:23:06 -07001029
Sarah Sharp65b22f92010-12-17 12:35:05 -08001030 /* USB core calls the PCI reinit and start functions twice:
1031 * first with the primary HCD, and then with the secondary HCD.
1032 * If we don't do the same, the host will never be started.
1033 */
1034 if (!usb_hcd_is_primary_hcd(hcd))
1035 secondary_hcd = hcd;
1036 else
1037 secondary_hcd = xhci->shared_hcd;
1038
1039 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1040 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001041 if (retval)
1042 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001043 comp_timer_running = true;
1044
Sarah Sharp65b22f92010-12-17 12:35:05 -08001045 xhci_dbg(xhci, "Start the primary HCD\n");
1046 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb32093792011-03-07 11:24:07 -08001047 if (!retval) {
Alan Sternf69e31202011-11-03 11:37:10 -04001048 xhci_dbg(xhci, "Start the secondary HCD\n");
1049 retval = xhci_run(secondary_hcd);
Sarah Sharpb32093792011-03-07 11:24:07 -08001050 }
Andiry Xu5535b1d52010-10-14 07:23:06 -07001051 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb32093792011-03-07 11:24:07 -08001052 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e31202011-11-03 11:37:10 -04001053 goto done;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001054 }
1055
Andiry Xu5535b1d52010-10-14 07:23:06 -07001056 /* step 4: set Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001057 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001058 command |= CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001059 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001060 xhci_handshake(&xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d52010-10-14 07:23:06 -07001061 0, 250 * 1000);
1062
1063 /* step 5: walk topology and initialize portsc,
1064 * portpmsc and portli
1065 */
1066 /* this is done in bus_resume */
1067
1068 /* step 6: restart each of the previously
1069 * Running endpoints by ringing their doorbells
1070 */
1071
Andiry Xu5535b1d52010-10-14 07:23:06 -07001072 spin_unlock_irq(&xhci->lock);
Alan Sternf69e31202011-11-03 11:37:10 -04001073
1074 done:
1075 if (retval == 0) {
Wang, Yud6236f62014-06-24 17:14:44 +03001076 /* Resume root hubs only when have pending events. */
1077 status = readl(&xhci->op_regs->status);
1078 if (status & STS_EINT) {
Wang, Yud6236f62014-06-24 17:14:44 +03001079 usb_hcd_resume_root_hub(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001080 usb_hcd_resume_root_hub(hcd);
Wang, Yud6236f62014-06-24 17:14:44 +03001081 }
Alan Sternf69e31202011-11-03 11:37:10 -04001082 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001083
1084 /*
1085 * If system is subject to the Quirk, Compliance Mode Timer needs to
1086 * be re-initialized Always after a system resume. Ports are subject
1087 * to suffer the Compliance Mode issue again. It doesn't matter if
1088 * ports have entered previously to U0 before system's suspension.
1089 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001090 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001091 compliance_mode_recovery_timer_init(xhci);
1092
Jiahau Chang9da5a102017-07-20 14:48:27 +03001093 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
1094 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
1095
Sarah Sharpc52804a2012-11-27 12:30:23 -08001096 /* Re-enable port polling. */
1097 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
Al Cooper14e61a12014-08-20 16:41:57 +03001098 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1099 usb_hcd_poll_rh_status(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001100 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1101 usb_hcd_poll_rh_status(hcd);
Sarah Sharpc52804a2012-11-27 12:30:23 -08001102
Alan Sternf69e31202011-11-03 11:37:10 -04001103 return retval;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001104}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001105EXPORT_SYMBOL_GPL(xhci_resume);
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001106#endif /* CONFIG_PM */
1107
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001108/*-------------------------------------------------------------------------*/
1109
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001110/**
1111 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1112 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1113 * value to right shift 1 for the bitmask.
1114 *
1115 * Index = (epnum * 2) + direction - 1,
1116 * where direction = 0 for OUT, 1 for IN.
1117 * For control endpoints, the IN index is used (OUT index is unused), so
1118 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1119 */
1120unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1121{
1122 unsigned int index;
1123 if (usb_endpoint_xfer_control(desc))
1124 index = (unsigned int) (usb_endpoint_num(desc)*2);
1125 else
1126 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1127 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1128 return index;
1129}
1130
Julius Werner01c5f442013-04-15 15:55:04 -07001131/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1132 * address from the XHCI endpoint index.
1133 */
1134unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1135{
1136 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1137 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1138 return direction | number;
1139}
1140
Sarah Sharpf94e01862009-04-27 19:58:38 -07001141/* Find the flag for this endpoint (for use in the control context). Use the
1142 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1143 * bit 1, etc.
1144 */
Lu Baolu39693842017-04-07 17:57:04 +03001145static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001146{
1147 return 1 << (xhci_get_endpoint_index(desc) + 1);
1148}
1149
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001150/* Find the flag for this endpoint (for use in the control context). Use the
1151 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1152 * bit 1, etc.
1153 */
Lu Baolu39693842017-04-07 17:57:04 +03001154static unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001155{
1156 return 1 << (ep_index + 1);
1157}
1158
Sarah Sharpf94e01862009-04-27 19:58:38 -07001159/* Compute the last valid endpoint context index. Basically, this is the
1160 * endpoint index plus one. For slot contexts with more than valid endpoint,
1161 * we find the most significant bit set in the added contexts flags.
1162 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1163 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1164 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001165unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001166{
1167 return fls(added_ctxs) - 1;
1168}
1169
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001170/* Returns 1 if the arguments are OK;
1171 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1172 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001173static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001174 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1175 const char *func) {
1176 struct xhci_hcd *xhci;
1177 struct xhci_virt_device *virt_dev;
1178
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001179 if (!hcd || (check_ep && !ep) || !udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001180 pr_debug("xHCI %s called with invalid args\n", func);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001181 return -EINVAL;
1182 }
1183 if (!udev->parent) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001184 pr_debug("xHCI %s called for root hub\n", func);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001185 return 0;
1186 }
Andiry Xu64927732010-10-14 07:22:45 -07001187
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001188 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001189 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001190 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001191 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1192 func);
Andiry Xu64927732010-10-14 07:22:45 -07001193 return -EINVAL;
1194 }
1195
1196 virt_dev = xhci->devs[udev->slot_id];
1197 if (virt_dev->udev != udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001198 xhci_dbg(xhci, "xHCI %s called with udev and "
Andiry Xu64927732010-10-14 07:22:45 -07001199 "virt_dev does not match\n", func);
1200 return -EINVAL;
1201 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001202 }
Andiry Xu64927732010-10-14 07:22:45 -07001203
Sarah Sharp203a8662013-07-24 10:27:13 -07001204 if (xhci->xhc_state & XHCI_STATE_HALTED)
1205 return -ENODEV;
1206
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001207 return 1;
1208}
1209
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001210static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001211 struct usb_device *udev, struct xhci_command *command,
1212 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001213
1214/*
1215 * Full speed devices may have a max packet size greater than 8 bytes, but the
1216 * USB core doesn't know that until it reads the first 8 bytes of the
1217 * descriptor. If the usb_device's max packet size changes after that point,
1218 * we need to issue an evaluate context command and wait on it.
1219 */
1220static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1221 unsigned int ep_index, struct urb *urb)
1222{
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001223 struct xhci_container_ctx *out_ctx;
1224 struct xhci_input_control_ctx *ctrl_ctx;
1225 struct xhci_ep_ctx *ep_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001226 struct xhci_command *command;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001227 int max_packet_size;
1228 int hw_max_packet_size;
1229 int ret = 0;
1230
1231 out_ctx = xhci->devs[slot_id]->out_ctx;
1232 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001233 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001234 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001235 if (hw_max_packet_size != max_packet_size) {
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001236 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1237 "Max Packet Size for ep 0 changed.");
1238 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1239 "Max packet size in usb_device = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001240 max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001241 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1242 "Max packet size in xHCI HW = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001243 hw_max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001244 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1245 "Issuing evaluate context command.");
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001246
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001247 /* Set up the input context flags for the command */
1248 /* FIXME: This won't work if a non-default control endpoint
1249 * changes max packet sizes.
1250 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001251
1252 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
1253 if (!command)
1254 return -ENOMEM;
1255
1256 command->in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001257 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001258 if (!ctrl_ctx) {
1259 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1260 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001261 ret = -ENOMEM;
1262 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07001263 }
1264 /* Set up the modified control endpoint 0 */
1265 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1266 xhci->devs[slot_id]->out_ctx, ep_index);
1267
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001268 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001269 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1270 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1271
Matt Evans28ccd292011-03-29 13:40:46 +11001272 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001273 ctrl_ctx->drop_flags = 0;
1274
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001275 ret = xhci_configure_endpoint(xhci, urb->dev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001276 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001277
1278 /* Clean up the input context for later use by bandwidth
1279 * functions.
1280 */
Matt Evans28ccd292011-03-29 13:40:46 +11001281 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001282command_cleanup:
1283 kfree(command->completion);
1284 kfree(command);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001285 }
1286 return ret;
1287}
1288
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001289/*
1290 * non-error returns are a promise to giveback() the urb later
1291 * we drop ownership so next owner (or urb unlink) can get it
1292 */
Lu Baolu39693842017-04-07 17:57:04 +03001293static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001294{
1295 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1296 unsigned long flags;
1297 int ret = 0;
Mathias Nyman69694082017-01-23 14:20:27 +02001298 unsigned int slot_id, ep_index, ep_state;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001299 struct urb_priv *urb_priv;
Mathias Nyman7e64b032017-01-23 14:20:26 +02001300 int num_tds;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001301
Andiry Xu64927732010-10-14 07:22:45 -07001302 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1303 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001304 return -EINVAL;
1305
1306 slot_id = urb->dev->slot_id;
1307 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001308
Alan Stern541c7d42010-06-22 16:39:10 -04001309 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001310 if (!in_interrupt())
1311 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
Mathias Nyman69694082017-01-23 14:20:27 +02001312 return -ESHUTDOWN;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001313 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001314
1315 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001316 num_tds = urb->number_of_packets;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03001317 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1318 urb->transfer_buffer_length > 0 &&
1319 urb->transfer_flags & URB_ZERO_PACKET &&
1320 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001321 num_tds = 2;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001322 else
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001323 num_tds = 1;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001324
1325 urb_priv = kzalloc(sizeof(struct urb_priv) +
Mathias Nyman7e64b032017-01-23 14:20:26 +02001326 num_tds * sizeof(struct xhci_td), mem_flags);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001327 if (!urb_priv)
1328 return -ENOMEM;
1329
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001330 urb_priv->num_tds = num_tds;
1331 urb_priv->num_tds_done = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001332 urb->hcpriv = urb_priv;
1333
Felipe Balbi5abdc2e2017-01-23 14:20:20 +02001334 trace_xhci_urb_enqueue(urb);
1335
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001336 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1337 /* Check to see if the max packet size for the default control
1338 * endpoint changed during FS device enumeration
1339 */
1340 if (urb->dev->speed == USB_SPEED_FULL) {
1341 ret = xhci_check_maxpacket(xhci, slot_id,
1342 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001343 if (ret < 0) {
Lin Wang4daf9df2015-01-09 16:06:31 +02001344 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001345 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001346 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001347 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001348 }
Mathias Nyman69694082017-01-23 14:20:27 +02001349 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001350
Mathias Nyman69694082017-01-23 14:20:27 +02001351 spin_lock_irqsave(&xhci->lock, flags);
1352
1353 if (xhci->xhc_state & XHCI_STATE_DYING) {
1354 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n",
1355 urb->ep->desc.bEndpointAddress, urb);
1356 ret = -ESHUTDOWN;
1357 goto free_priv;
1358 }
1359
1360 switch (usb_endpoint_type(&urb->ep->desc)) {
1361
1362 case USB_ENDPOINT_XFER_CONTROL:
Sarah Sharpb11069f2009-07-27 12:03:23 -07001363 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Mathias Nyman69694082017-01-23 14:20:27 +02001364 slot_id, ep_index);
1365 break;
1366 case USB_ENDPOINT_XFER_BULK:
1367 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1368 if (ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) {
1369 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n",
1370 ep_state);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001371 ret = -EINVAL;
Mathias Nyman69694082017-01-23 14:20:27 +02001372 break;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001373 }
Mathias Nyman69694082017-01-23 14:20:27 +02001374 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1375 slot_id, ep_index);
1376 break;
1377
1378
1379 case USB_ENDPOINT_XFER_INT:
Sarah Sharp624defa2009-09-02 12:14:28 -07001380 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1381 slot_id, ep_index);
Mathias Nyman69694082017-01-23 14:20:27 +02001382 break;
1383
1384 case USB_ENDPOINT_XFER_ISOC:
Andiry Xu787f4e52010-07-22 15:23:52 -07001385 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1386 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001387 }
Mathias Nyman69694082017-01-23 14:20:27 +02001388
1389 if (ret) {
Sarah Sharpd13565c2011-07-22 14:34:34 -07001390free_priv:
Mathias Nyman69694082017-01-23 14:20:27 +02001391 xhci_urb_free_priv(urb_priv);
1392 urb->hcpriv = NULL;
1393 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001394 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001395 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001396}
1397
Sarah Sharpae636742009-04-29 19:02:31 -07001398/*
1399 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1400 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1401 * should pick up where it left off in the TD, unless a Set Transfer Ring
1402 * Dequeue Pointer is issued.
1403 *
1404 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1405 * the ring. Since the ring is a contiguous structure, they can't be physically
1406 * removed. Instead, there are two options:
1407 *
1408 * 1) If the HC is in the middle of processing the URB to be canceled, we
1409 * simply move the ring's dequeue pointer past those TRBs using the Set
1410 * Transfer Ring Dequeue Pointer command. This will be the common case,
1411 * when drivers timeout on the last submitted URB and attempt to cancel.
1412 *
1413 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1414 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1415 * HC will need to invalidate the any TRBs it has cached after the stop
1416 * endpoint command, as noted in the xHCI 0.95 errata.
1417 *
1418 * 3) The TD may have completed by the time the Stop Endpoint Command
1419 * completes, so software needs to handle that case too.
1420 *
1421 * This function should protect against the TD enqueueing code ringing the
1422 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1423 * It also needs to account for multiple cancellations on happening at the same
1424 * time for the same endpoint.
1425 *
1426 * Note that this function can be called in any context, or so says
1427 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001428 */
Lu Baolu39693842017-04-07 17:57:04 +03001429static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001430{
Sarah Sharpae636742009-04-29 19:02:31 -07001431 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001432 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001433 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001434 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001435 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001436 struct xhci_td *td;
1437 unsigned int ep_index;
1438 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001439 struct xhci_virt_ep *ep;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001440 struct xhci_command *command;
Mathias Nymand3519b92017-03-28 15:55:30 +03001441 struct xhci_virt_device *vdev;
Sarah Sharpae636742009-04-29 19:02:31 -07001442
1443 xhci = hcd_to_xhci(hcd);
1444 spin_lock_irqsave(&xhci->lock, flags);
Felipe Balbi5abdc2e2017-01-23 14:20:20 +02001445
1446 trace_xhci_urb_dequeue(urb);
1447
Sarah Sharpae636742009-04-29 19:02:31 -07001448 /* Make sure the URB hasn't completed or been unlinked already */
1449 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
Mathias Nymand3519b92017-03-28 15:55:30 +03001450 if (ret)
Sarah Sharpae636742009-04-29 19:02:31 -07001451 goto done;
Mathias Nymand3519b92017-03-28 15:55:30 +03001452
1453 /* give back URB now if we can't queue it for cancel */
1454 vdev = xhci->devs[urb->dev->slot_id];
1455 urb_priv = urb->hcpriv;
1456 if (!vdev || !urb_priv)
1457 goto err_giveback;
1458
1459 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1460 ep = &vdev->eps[ep_index];
1461 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1462 if (!ep || !ep_ring)
1463 goto err_giveback;
1464
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001465 /* If xHC is dead take it down and return ALL URBs in xhci_hc_died() */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001466 temp = readl(&xhci->op_regs->status);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001467 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) {
1468 xhci_hc_died(xhci);
1469 goto done;
1470 }
1471
1472 if (xhci->xhc_state & XHCI_STATE_HALTED) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001473 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001474 "HC halted, freeing TD manually.");
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001475 for (i = urb_priv->num_tds_done;
Mathias Nymand3519b92017-03-28 15:55:30 +03001476 i < urb_priv->num_tds;
Mathias Nyman5c821712016-01-26 17:50:12 +02001477 i++) {
Mathias Nyman7e64b032017-01-23 14:20:26 +02001478 td = &urb_priv->td[i];
Sarah Sharp585df1d2011-08-02 15:43:40 -07001479 if (!list_empty(&td->td_list))
1480 list_del_init(&td->td_list);
1481 if (!list_empty(&td->cancelled_td_list))
1482 list_del_init(&td->cancelled_td_list);
1483 }
Mathias Nymand3519b92017-03-28 15:55:30 +03001484 goto err_giveback;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001485 }
Sarah Sharpae636742009-04-29 19:02:31 -07001486
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001487 i = urb_priv->num_tds_done;
1488 if (i < urb_priv->num_tds)
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001489 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1490 "Cancel URB %p, dev %s, ep 0x%x, "
1491 "starting at offset 0x%llx",
Sarah Sharp79688ac2011-12-19 16:56:04 -08001492 urb, urb->dev->devpath,
1493 urb->ep->desc.bEndpointAddress,
1494 (unsigned long long) xhci_trb_virt_to_dma(
Mathias Nyman7e64b032017-01-23 14:20:26 +02001495 urb_priv->td[i].start_seg,
1496 urb_priv->td[i].first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001497
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001498 for (; i < urb_priv->num_tds; i++) {
Mathias Nyman7e64b032017-01-23 14:20:26 +02001499 td = &urb_priv->td[i];
Andiry Xu8e51adc2010-07-22 15:23:31 -07001500 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1501 }
1502
Sarah Sharpae636742009-04-29 19:02:31 -07001503 /* Queue a stop endpoint command, but only if this is
1504 * the first cancellation to be handled.
1505 */
Mathias Nyman9983a5f2017-01-23 14:19:52 +02001506 if (!(ep->ep_state & EP_STOP_CMD_PENDING)) {
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001507 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
Hans de Goedea0ee6192014-07-25 22:01:21 +02001508 if (!command) {
1509 ret = -ENOMEM;
1510 goto done;
1511 }
Mathias Nyman9983a5f2017-01-23 14:19:52 +02001512 ep->ep_state |= EP_STOP_CMD_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001513 ep->stop_cmd_timer.expires = jiffies +
1514 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1515 add_timer(&ep->stop_cmd_timer);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001516 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
1517 ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001518 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001519 }
1520done:
1521 spin_unlock_irqrestore(&xhci->lock, flags);
1522 return ret;
Mathias Nymand3519b92017-03-28 15:55:30 +03001523
1524err_giveback:
1525 if (urb_priv)
1526 xhci_urb_free_priv(urb_priv);
1527 usb_hcd_unlink_urb_from_ep(hcd, urb);
1528 spin_unlock_irqrestore(&xhci->lock, flags);
1529 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
1530 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001531}
1532
Sarah Sharpf94e01862009-04-27 19:58:38 -07001533/* Drop an endpoint from a new bandwidth configuration for this device.
1534 * Only one call to this function is allowed per endpoint before
1535 * check_bandwidth() or reset_bandwidth() must be called.
1536 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1537 * add the endpoint to the schedule with possibly new parameters denoted by a
1538 * different endpoint descriptor in usb_host_endpoint.
1539 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1540 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001541 *
1542 * The USB core will not allow URBs to be queued to an endpoint that is being
1543 * disabled, so there's no need for mutual exclusion to protect
1544 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001545 */
Lu Baolu39693842017-04-07 17:57:04 +03001546static int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharpf94e01862009-04-27 19:58:38 -07001547 struct usb_host_endpoint *ep)
1548{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001549 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001550 struct xhci_container_ctx *in_ctx, *out_ctx;
1551 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001552 unsigned int ep_index;
1553 struct xhci_ep_ctx *ep_ctx;
1554 u32 drop_flag;
Julius Wernerd6759132014-06-24 17:14:42 +03001555 u32 new_add_flags, new_drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001556 int ret;
1557
Andiry Xu64927732010-10-14 07:22:45 -07001558 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001559 if (ret <= 0)
1560 return ret;
1561 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001562 if (xhci->xhc_state & XHCI_STATE_DYING)
1563 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001564
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001565 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001566 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1567 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1568 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1569 __func__, drop_flag);
1570 return 0;
1571 }
1572
Sarah Sharpf94e01862009-04-27 19:58:38 -07001573 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001574 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001575 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001576 if (!ctrl_ctx) {
1577 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1578 __func__);
1579 return 0;
1580 }
1581
Sarah Sharpf94e01862009-04-27 19:58:38 -07001582 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001583 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001584 /* If the HC already knows the endpoint is disabled,
1585 * or the HCD has noted it is disabled, ignore this request
1586 */
Mathias Nyman5071e6b2016-11-11 15:13:28 +02001587 if ((GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001588 le32_to_cpu(ctrl_ctx->drop_flags) &
1589 xhci_get_endpoint_flag(&ep->desc)) {
Hans de Goedea6134132015-01-16 17:54:02 +02001590 /* Do not warn when called after a usb_device_reset */
1591 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
1592 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1593 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001594 return 0;
1595 }
1596
Matt Evans28ccd292011-03-29 13:40:46 +11001597 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1598 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001599
Matt Evans28ccd292011-03-29 13:40:46 +11001600 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1601 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001602
Sarah Sharpf94e01862009-04-27 19:58:38 -07001603 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1604
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001605 if (xhci->quirks & XHCI_MTK_HOST)
1606 xhci_mtk_drop_ep_quirk(hcd, udev, ep);
1607
Julius Wernerd6759132014-06-24 17:14:42 +03001608 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 -07001609 (unsigned int) ep->desc.bEndpointAddress,
1610 udev->slot_id,
1611 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001612 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001613 return 0;
1614}
1615
1616/* Add an endpoint to a new possible bandwidth configuration for this device.
1617 * Only one call to this function is allowed per endpoint before
1618 * check_bandwidth() or reset_bandwidth() must be called.
1619 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1620 * add the endpoint to the schedule with possibly new parameters denoted by a
1621 * different endpoint descriptor in usb_host_endpoint.
1622 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1623 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001624 *
1625 * The USB core will not allow URBs to be queued to an endpoint until the
1626 * configuration or alt setting is installed in the device, so there's no need
1627 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001628 */
Lu Baolu39693842017-04-07 17:57:04 +03001629static int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharpf94e01862009-04-27 19:58:38 -07001630 struct usb_host_endpoint *ep)
1631{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001632 struct xhci_hcd *xhci;
Lin Wang92c96912015-01-09 16:06:27 +02001633 struct xhci_container_ctx *in_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001634 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001635 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001636 u32 added_ctxs;
Julius Wernerd6759132014-06-24 17:14:42 +03001637 u32 new_add_flags, new_drop_flags;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001638 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001639 int ret = 0;
1640
Andiry Xu64927732010-10-14 07:22:45 -07001641 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001642 if (ret <= 0) {
1643 /* So we won't queue a reset ep command for a root hub */
1644 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001645 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001646 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001647 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001648 if (xhci->xhc_state & XHCI_STATE_DYING)
1649 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001650
1651 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001652 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1653 /* FIXME when we have to issue an evaluate endpoint command to
1654 * deal with ep0 max packet size changing once we get the
1655 * descriptors
1656 */
1657 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1658 __func__, added_ctxs);
1659 return 0;
1660 }
1661
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001662 virt_dev = xhci->devs[udev->slot_id];
1663 in_ctx = virt_dev->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001664 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001665 if (!ctrl_ctx) {
1666 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1667 __func__);
1668 return 0;
1669 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001670
Sarah Sharp92f8e762013-04-23 17:11:14 -07001671 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001672 /* If this endpoint is already in use, and the upper layers are trying
1673 * to add it again without dropping it, reject the addition.
1674 */
1675 if (virt_dev->eps[ep_index].ring &&
Lin Wang92c96912015-01-09 16:06:27 +02001676 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001677 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1678 "without dropping it.\n",
1679 (unsigned int) ep->desc.bEndpointAddress);
1680 return -EINVAL;
1681 }
1682
Sarah Sharpf94e01862009-04-27 19:58:38 -07001683 /* If the HCD has already noted the endpoint is enabled,
1684 * ignore this request.
1685 */
Lin Wang92c96912015-01-09 16:06:27 +02001686 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001687 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1688 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001689 return 0;
1690 }
1691
Sarah Sharpf88ba782009-05-14 11:44:22 -07001692 /*
1693 * Configuration and alternate setting changes must be done in
1694 * process context, not interrupt context (or so documenation
1695 * for usb_set_interface() and usb_set_configuration() claim).
1696 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001697 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001698 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1699 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001700 return -ENOMEM;
1701 }
1702
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001703 if (xhci->quirks & XHCI_MTK_HOST) {
1704 ret = xhci_mtk_add_ep_quirk(hcd, udev, ep);
1705 if (ret < 0) {
Lu Baolu98217862017-09-18 17:39:12 +03001706 xhci_ring_free(xhci, virt_dev->eps[ep_index].new_ring);
1707 virt_dev->eps[ep_index].new_ring = NULL;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001708 return ret;
1709 }
1710 }
1711
Matt Evans28ccd292011-03-29 13:40:46 +11001712 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1713 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001714
1715 /* If xhci_endpoint_disable() was called for this endpoint, but the
1716 * xHC hasn't been notified yet through the check_bandwidth() call,
1717 * this re-adds a new state for the endpoint from the new endpoint
1718 * descriptors. We must drop and re-add this endpoint, so we leave the
1719 * drop flags alone.
1720 */
Matt Evans28ccd292011-03-29 13:40:46 +11001721 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001722
Sarah Sharpa1587d92009-07-27 12:03:15 -07001723 /* Store the usb_device pointer for later use */
1724 ep->hcpriv = udev;
1725
Julius Wernerd6759132014-06-24 17:14:42 +03001726 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 -07001727 (unsigned int) ep->desc.bEndpointAddress,
1728 udev->slot_id,
1729 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001730 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001731 return 0;
1732}
1733
John Yound115b042009-07-27 12:05:15 -07001734static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001735{
John Yound115b042009-07-27 12:05:15 -07001736 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001737 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001738 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001739 int i;
1740
Lin Wang4daf9df2015-01-09 16:06:31 +02001741 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001742 if (!ctrl_ctx) {
1743 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1744 __func__);
1745 return;
1746 }
1747
Sarah Sharpf94e01862009-04-27 19:58:38 -07001748 /* When a device's add flag and drop flag are zero, any subsequent
1749 * configure endpoint command will leave that endpoint's state
1750 * untouched. Make sure we don't leave any old state in the input
1751 * endpoint contexts.
1752 */
John Yound115b042009-07-27 12:05:15 -07001753 ctrl_ctx->drop_flags = 0;
1754 ctrl_ctx->add_flags = 0;
1755 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001756 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001757 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001758 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Felipe Balbi98871e92017-01-23 14:20:04 +02001759 for (i = 1; i < 31; i++) {
John Yound115b042009-07-27 12:05:15 -07001760 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001761 ep_ctx->ep_info = 0;
1762 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001763 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001764 ep_ctx->tx_info = 0;
1765 }
1766}
1767
Sarah Sharpf2217e82009-08-07 14:04:43 -07001768static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001769 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001770{
1771 int ret;
1772
Sarah Sharp913a8a32009-09-04 10:53:13 -07001773 switch (*cmd_status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001774 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03001775 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03001776 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
1777 ret = -ETIME;
1778 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001779 case COMP_RESOURCE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001780 dev_warn(&udev->dev,
1781 "Not enough host controller resources for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001782 ret = -ENOMEM;
1783 /* FIXME: can we allocate more resources for the HC? */
1784 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001785 case COMP_BANDWIDTH_ERROR:
1786 case COMP_SECONDARY_BANDWIDTH_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001787 dev_warn(&udev->dev,
1788 "Not enough bandwidth for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001789 ret = -ENOSPC;
1790 /* FIXME: can we go back to the old state? */
1791 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001792 case COMP_TRB_ERROR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001793 /* the HCD set up something wrong */
1794 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1795 "add flag = 1, "
1796 "and endpoint is not disabled.\n");
1797 ret = -EINVAL;
1798 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001799 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001800 dev_warn(&udev->dev,
1801 "ERROR: Incompatible device for endpoint configure command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001802 ret = -ENODEV;
1803 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001804 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001805 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1806 "Successful Endpoint Configure command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001807 ret = 0;
1808 break;
1809 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001810 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1811 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001812 ret = -EINVAL;
1813 break;
1814 }
1815 return ret;
1816}
1817
1818static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001819 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001820{
1821 int ret;
1822
Sarah Sharp913a8a32009-09-04 10:53:13 -07001823 switch (*cmd_status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001824 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03001825 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03001826 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
1827 ret = -ETIME;
1828 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001829 case COMP_PARAMETER_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001830 dev_warn(&udev->dev,
1831 "WARN: xHCI driver setup invalid evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001832 ret = -EINVAL;
1833 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001834 case COMP_SLOT_NOT_ENABLED_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001835 dev_warn(&udev->dev,
1836 "WARN: slot not enabled for evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001837 ret = -EINVAL;
1838 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001839 case COMP_CONTEXT_STATE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001840 dev_warn(&udev->dev,
1841 "WARN: invalid context state for evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001842 ret = -EINVAL;
1843 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001844 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001845 dev_warn(&udev->dev,
1846 "ERROR: Incompatible device for evaluate context command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001847 ret = -ENODEV;
1848 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001849 case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
Alex He1bb73a82011-05-05 18:14:12 +08001850 /* Max Exit Latency too large error */
1851 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1852 ret = -EINVAL;
1853 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001854 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001855 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1856 "Successful evaluate context command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001857 ret = 0;
1858 break;
1859 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001860 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1861 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001862 ret = -EINVAL;
1863 break;
1864 }
1865 return ret;
1866}
1867
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001868static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001869 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001870{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001871 u32 valid_add_flags;
1872 u32 valid_drop_flags;
1873
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001874 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1875 * (bit 1). The default control endpoint is added during the Address
1876 * Device command and is never removed until the slot is disabled.
1877 */
Xenia Ragiadakouef734002013-09-09 21:03:06 +03001878 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1879 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001880
1881 /* Use hweight32 to count the number of ones in the add flags, or
1882 * number of endpoints added. Don't count endpoints that are changed
1883 * (both added and dropped).
1884 */
1885 return hweight32(valid_add_flags) -
1886 hweight32(valid_add_flags & valid_drop_flags);
1887}
1888
1889static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001890 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001891{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001892 u32 valid_add_flags;
1893 u32 valid_drop_flags;
1894
Xenia Ragiadakou78d1ff02013-09-09 21:03:07 +03001895 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1896 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001897
1898 return hweight32(valid_drop_flags) -
1899 hweight32(valid_add_flags & valid_drop_flags);
1900}
1901
1902/*
1903 * We need to reserve the new number of endpoints before the configure endpoint
1904 * command completes. We can't subtract the dropped endpoints from the number
1905 * of active endpoints until the command completes because we can oversubscribe
1906 * the host in this case:
1907 *
1908 * - the first configure endpoint command drops more endpoints than it adds
1909 * - a second configure endpoint command that adds more endpoints is queued
1910 * - the first configure endpoint command fails, so the config is unchanged
1911 * - the second command may succeed, even though there isn't enough resources
1912 *
1913 * Must be called with xhci->lock held.
1914 */
1915static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001916 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001917{
1918 u32 added_eps;
1919
Sarah Sharp92f8e762013-04-23 17:11:14 -07001920 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001921 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001922 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1923 "Not enough ep ctxs: "
1924 "%u active, need to add %u, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001925 xhci->num_active_eps, added_eps,
1926 xhci->limit_active_eps);
1927 return -ENOMEM;
1928 }
1929 xhci->num_active_eps += added_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001930 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1931 "Adding %u ep ctxs, %u now active.", added_eps,
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001932 xhci->num_active_eps);
1933 return 0;
1934}
1935
1936/*
1937 * The configure endpoint was failed by the xHC for some other reason, so we
1938 * need to revert the resources that failed configuration would have used.
1939 *
1940 * Must be called with xhci->lock held.
1941 */
1942static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001943 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001944{
1945 u32 num_failed_eps;
1946
Sarah Sharp92f8e762013-04-23 17:11:14 -07001947 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001948 xhci->num_active_eps -= num_failed_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001949 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1950 "Removing %u failed ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001951 num_failed_eps,
1952 xhci->num_active_eps);
1953}
1954
1955/*
1956 * Now that the command has completed, clean up the active endpoint count by
1957 * subtracting out the endpoints that were dropped (but not changed).
1958 *
1959 * Must be called with xhci->lock held.
1960 */
1961static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001962 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001963{
1964 u32 num_dropped_eps;
1965
Sarah Sharp92f8e762013-04-23 17:11:14 -07001966 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001967 xhci->num_active_eps -= num_dropped_eps;
1968 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001969 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1970 "Removing %u dropped ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001971 num_dropped_eps,
1972 xhci->num_active_eps);
1973}
1974
Felipe Balbied384bd2012-08-07 14:10:03 +03001975static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07001976{
1977 switch (udev->speed) {
1978 case USB_SPEED_LOW:
1979 case USB_SPEED_FULL:
1980 return FS_BLOCK;
1981 case USB_SPEED_HIGH:
1982 return HS_BLOCK;
1983 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02001984 case USB_SPEED_SUPER_PLUS:
Sarah Sharpc29eea62011-09-02 11:05:52 -07001985 return SS_BLOCK;
1986 case USB_SPEED_UNKNOWN:
1987 case USB_SPEED_WIRELESS:
1988 default:
1989 /* Should never happen */
1990 return 1;
1991 }
1992}
1993
Felipe Balbied384bd2012-08-07 14:10:03 +03001994static unsigned int
1995xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07001996{
1997 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
1998 return LS_OVERHEAD;
1999 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2000 return FS_OVERHEAD;
2001 return HS_OVERHEAD;
2002}
2003
2004/* If we are changing a LS/FS device under a HS hub,
2005 * make sure (if we are activating a new TT) that the HS bus has enough
2006 * bandwidth for this new TT.
2007 */
2008static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2009 struct xhci_virt_device *virt_dev,
2010 int old_active_eps)
2011{
2012 struct xhci_interval_bw_table *bw_table;
2013 struct xhci_tt_bw_info *tt_info;
2014
2015 /* Find the bandwidth table for the root port this TT is attached to. */
2016 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2017 tt_info = virt_dev->tt_info;
2018 /* If this TT already had active endpoints, the bandwidth for this TT
2019 * has already been added. Removing all periodic endpoints (and thus
2020 * making the TT enactive) will only decrease the bandwidth used.
2021 */
2022 if (old_active_eps)
2023 return 0;
2024 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2025 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2026 return -ENOMEM;
2027 return 0;
2028 }
2029 /* Not sure why we would have no new active endpoints...
2030 *
2031 * Maybe because of an Evaluate Context change for a hub update or a
2032 * control endpoint 0 max packet size change?
2033 * FIXME: skip the bandwidth calculation in that case.
2034 */
2035 return 0;
2036}
2037
Sarah Sharp2b698992011-09-13 16:41:13 -07002038static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2039 struct xhci_virt_device *virt_dev)
2040{
2041 unsigned int bw_reserved;
2042
2043 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2044 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2045 return -ENOMEM;
2046
2047 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2048 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2049 return -ENOMEM;
2050
2051 return 0;
2052}
2053
Sarah Sharpc29eea62011-09-02 11:05:52 -07002054/*
2055 * This algorithm is a very conservative estimate of the worst-case scheduling
2056 * scenario for any one interval. The hardware dynamically schedules the
2057 * packets, so we can't tell which microframe could be the limiting factor in
2058 * the bandwidth scheduling. This only takes into account periodic endpoints.
2059 *
2060 * Obviously, we can't solve an NP complete problem to find the minimum worst
2061 * case scenario. Instead, we come up with an estimate that is no less than
2062 * the worst case bandwidth used for any one microframe, but may be an
2063 * over-estimate.
2064 *
2065 * We walk the requirements for each endpoint by interval, starting with the
2066 * smallest interval, and place packets in the schedule where there is only one
2067 * possible way to schedule packets for that interval. In order to simplify
2068 * this algorithm, we record the largest max packet size for each interval, and
2069 * assume all packets will be that size.
2070 *
2071 * For interval 0, we obviously must schedule all packets for each interval.
2072 * The bandwidth for interval 0 is just the amount of data to be transmitted
2073 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2074 * the number of packets).
2075 *
2076 * For interval 1, we have two possible microframes to schedule those packets
2077 * in. For this algorithm, if we can schedule the same number of packets for
2078 * each possible scheduling opportunity (each microframe), we will do so. The
2079 * remaining number of packets will be saved to be transmitted in the gaps in
2080 * the next interval's scheduling sequence.
2081 *
2082 * As we move those remaining packets to be scheduled with interval 2 packets,
2083 * we have to double the number of remaining packets to transmit. This is
2084 * because the intervals are actually powers of 2, and we would be transmitting
2085 * the previous interval's packets twice in this interval. We also have to be
2086 * sure that when we look at the largest max packet size for this interval, we
2087 * also look at the largest max packet size for the remaining packets and take
2088 * the greater of the two.
2089 *
2090 * The algorithm continues to evenly distribute packets in each scheduling
2091 * opportunity, and push the remaining packets out, until we get to the last
2092 * interval. Then those packets and their associated overhead are just added
2093 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002094 */
2095static int xhci_check_bw_table(struct xhci_hcd *xhci,
2096 struct xhci_virt_device *virt_dev,
2097 int old_active_eps)
2098{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002099 unsigned int bw_reserved;
2100 unsigned int max_bandwidth;
2101 unsigned int bw_used;
2102 unsigned int block_size;
2103 struct xhci_interval_bw_table *bw_table;
2104 unsigned int packet_size = 0;
2105 unsigned int overhead = 0;
2106 unsigned int packets_transmitted = 0;
2107 unsigned int packets_remaining = 0;
2108 unsigned int i;
2109
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002110 if (virt_dev->udev->speed >= USB_SPEED_SUPER)
Sarah Sharp2b698992011-09-13 16:41:13 -07002111 return xhci_check_ss_bw(xhci, virt_dev);
2112
Sarah Sharpc29eea62011-09-02 11:05:52 -07002113 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2114 max_bandwidth = HS_BW_LIMIT;
2115 /* Convert percent of bus BW reserved to blocks reserved */
2116 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2117 } else {
2118 max_bandwidth = FS_BW_LIMIT;
2119 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2120 }
2121
2122 bw_table = virt_dev->bw_table;
2123 /* We need to translate the max packet size and max ESIT payloads into
2124 * the units the hardware uses.
2125 */
2126 block_size = xhci_get_block_size(virt_dev->udev);
2127
2128 /* If we are manipulating a LS/FS device under a HS hub, double check
2129 * that the HS bus has enough bandwidth if we are activing a new TT.
2130 */
2131 if (virt_dev->tt_info) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002132 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2133 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002134 virt_dev->real_port);
2135 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2136 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2137 "newly activated TT.\n");
2138 return -ENOMEM;
2139 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002140 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2141 "Recalculating BW for TT slot %u port %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002142 virt_dev->tt_info->slot_id,
2143 virt_dev->tt_info->ttport);
2144 } else {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002145 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2146 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002147 virt_dev->real_port);
2148 }
2149
2150 /* Add in how much bandwidth will be used for interval zero, or the
2151 * rounded max ESIT payload + number of packets * largest overhead.
2152 */
2153 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2154 bw_table->interval_bw[0].num_packets *
2155 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2156
2157 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2158 unsigned int bw_added;
2159 unsigned int largest_mps;
2160 unsigned int interval_overhead;
2161
2162 /*
2163 * How many packets could we transmit in this interval?
2164 * If packets didn't fit in the previous interval, we will need
2165 * to transmit that many packets twice within this interval.
2166 */
2167 packets_remaining = 2 * packets_remaining +
2168 bw_table->interval_bw[i].num_packets;
2169
2170 /* Find the largest max packet size of this or the previous
2171 * interval.
2172 */
2173 if (list_empty(&bw_table->interval_bw[i].endpoints))
2174 largest_mps = 0;
2175 else {
2176 struct xhci_virt_ep *virt_ep;
2177 struct list_head *ep_entry;
2178
2179 ep_entry = bw_table->interval_bw[i].endpoints.next;
2180 virt_ep = list_entry(ep_entry,
2181 struct xhci_virt_ep, bw_endpoint_list);
2182 /* Convert to blocks, rounding up */
2183 largest_mps = DIV_ROUND_UP(
2184 virt_ep->bw_info.max_packet_size,
2185 block_size);
2186 }
2187 if (largest_mps > packet_size)
2188 packet_size = largest_mps;
2189
2190 /* Use the larger overhead of this or the previous interval. */
2191 interval_overhead = xhci_get_largest_overhead(
2192 &bw_table->interval_bw[i]);
2193 if (interval_overhead > overhead)
2194 overhead = interval_overhead;
2195
2196 /* How many packets can we evenly distribute across
2197 * (1 << (i + 1)) possible scheduling opportunities?
2198 */
2199 packets_transmitted = packets_remaining >> (i + 1);
2200
2201 /* Add in the bandwidth used for those scheduled packets */
2202 bw_added = packets_transmitted * (overhead + packet_size);
2203
2204 /* How many packets do we have remaining to transmit? */
2205 packets_remaining = packets_remaining % (1 << (i + 1));
2206
2207 /* What largest max packet size should those packets have? */
2208 /* If we've transmitted all packets, don't carry over the
2209 * largest packet size.
2210 */
2211 if (packets_remaining == 0) {
2212 packet_size = 0;
2213 overhead = 0;
2214 } else if (packets_transmitted > 0) {
2215 /* Otherwise if we do have remaining packets, and we've
2216 * scheduled some packets in this interval, take the
2217 * largest max packet size from endpoints with this
2218 * interval.
2219 */
2220 packet_size = largest_mps;
2221 overhead = interval_overhead;
2222 }
2223 /* Otherwise carry over packet_size and overhead from the last
2224 * time we had a remainder.
2225 */
2226 bw_used += bw_added;
2227 if (bw_used > max_bandwidth) {
2228 xhci_warn(xhci, "Not enough bandwidth. "
2229 "Proposed: %u, Max: %u\n",
2230 bw_used, max_bandwidth);
2231 return -ENOMEM;
2232 }
2233 }
2234 /*
2235 * Ok, we know we have some packets left over after even-handedly
2236 * scheduling interval 15. We don't know which microframes they will
2237 * fit into, so we over-schedule and say they will be scheduled every
2238 * microframe.
2239 */
2240 if (packets_remaining > 0)
2241 bw_used += overhead + packet_size;
2242
2243 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2244 unsigned int port_index = virt_dev->real_port - 1;
2245
2246 /* OK, we're manipulating a HS device attached to a
2247 * root port bandwidth domain. Include the number of active TTs
2248 * in the bandwidth used.
2249 */
2250 bw_used += TT_HS_OVERHEAD *
2251 xhci->rh_bw[port_index].num_active_tts;
2252 }
2253
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002254 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2255 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2256 "Available: %u " "percent",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002257 bw_used, max_bandwidth, bw_reserved,
2258 (max_bandwidth - bw_used - bw_reserved) * 100 /
2259 max_bandwidth);
2260
2261 bw_used += bw_reserved;
2262 if (bw_used > max_bandwidth) {
2263 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2264 bw_used, max_bandwidth);
2265 return -ENOMEM;
2266 }
2267
2268 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002269 return 0;
2270}
2271
2272static bool xhci_is_async_ep(unsigned int ep_type)
2273{
2274 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2275 ep_type != ISOC_IN_EP &&
2276 ep_type != INT_IN_EP);
2277}
2278
Sarah Sharp2b698992011-09-13 16:41:13 -07002279static bool xhci_is_sync_in_ep(unsigned int ep_type)
2280{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002281 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002282}
2283
2284static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2285{
2286 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2287
2288 if (ep_bw->ep_interval == 0)
2289 return SS_OVERHEAD_BURST +
2290 (ep_bw->mult * ep_bw->num_packets *
2291 (SS_OVERHEAD + mps));
2292 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2293 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2294 1 << ep_bw->ep_interval);
2295
2296}
2297
Lu Baolu39693842017-04-07 17:57:04 +03002298static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
Sarah Sharp2e279802011-09-02 11:05:50 -07002299 struct xhci_bw_info *ep_bw,
2300 struct xhci_interval_bw_table *bw_table,
2301 struct usb_device *udev,
2302 struct xhci_virt_ep *virt_ep,
2303 struct xhci_tt_bw_info *tt_info)
2304{
2305 struct xhci_interval_bw *interval_bw;
2306 int normalized_interval;
2307
Sarah Sharp2b698992011-09-13 16:41:13 -07002308 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002309 return;
2310
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002311 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharp2b698992011-09-13 16:41:13 -07002312 if (xhci_is_sync_in_ep(ep_bw->type))
2313 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2314 xhci_get_ss_bw_consumed(ep_bw);
2315 else
2316 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2317 xhci_get_ss_bw_consumed(ep_bw);
2318 return;
2319 }
2320
2321 /* SuperSpeed endpoints never get added to intervals in the table, so
2322 * this check is only valid for HS/FS/LS devices.
2323 */
2324 if (list_empty(&virt_ep->bw_endpoint_list))
2325 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002326 /* For LS/FS devices, we need to translate the interval expressed in
2327 * microframes to frames.
2328 */
2329 if (udev->speed == USB_SPEED_HIGH)
2330 normalized_interval = ep_bw->ep_interval;
2331 else
2332 normalized_interval = ep_bw->ep_interval - 3;
2333
2334 if (normalized_interval == 0)
2335 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2336 interval_bw = &bw_table->interval_bw[normalized_interval];
2337 interval_bw->num_packets -= ep_bw->num_packets;
2338 switch (udev->speed) {
2339 case USB_SPEED_LOW:
2340 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2341 break;
2342 case USB_SPEED_FULL:
2343 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2344 break;
2345 case USB_SPEED_HIGH:
2346 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2347 break;
2348 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002349 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002350 case USB_SPEED_UNKNOWN:
2351 case USB_SPEED_WIRELESS:
2352 /* Should never happen because only LS/FS/HS endpoints will get
2353 * added to the endpoint list.
2354 */
2355 return;
2356 }
2357 if (tt_info)
2358 tt_info->active_eps -= 1;
2359 list_del_init(&virt_ep->bw_endpoint_list);
2360}
2361
2362static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2363 struct xhci_bw_info *ep_bw,
2364 struct xhci_interval_bw_table *bw_table,
2365 struct usb_device *udev,
2366 struct xhci_virt_ep *virt_ep,
2367 struct xhci_tt_bw_info *tt_info)
2368{
2369 struct xhci_interval_bw *interval_bw;
2370 struct xhci_virt_ep *smaller_ep;
2371 int normalized_interval;
2372
2373 if (xhci_is_async_ep(ep_bw->type))
2374 return;
2375
Sarah Sharp2b698992011-09-13 16:41:13 -07002376 if (udev->speed == USB_SPEED_SUPER) {
2377 if (xhci_is_sync_in_ep(ep_bw->type))
2378 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2379 xhci_get_ss_bw_consumed(ep_bw);
2380 else
2381 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2382 xhci_get_ss_bw_consumed(ep_bw);
2383 return;
2384 }
2385
Sarah Sharp2e279802011-09-02 11:05:50 -07002386 /* For LS/FS devices, we need to translate the interval expressed in
2387 * microframes to frames.
2388 */
2389 if (udev->speed == USB_SPEED_HIGH)
2390 normalized_interval = ep_bw->ep_interval;
2391 else
2392 normalized_interval = ep_bw->ep_interval - 3;
2393
2394 if (normalized_interval == 0)
2395 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2396 interval_bw = &bw_table->interval_bw[normalized_interval];
2397 interval_bw->num_packets += ep_bw->num_packets;
2398 switch (udev->speed) {
2399 case USB_SPEED_LOW:
2400 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2401 break;
2402 case USB_SPEED_FULL:
2403 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2404 break;
2405 case USB_SPEED_HIGH:
2406 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2407 break;
2408 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002409 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002410 case USB_SPEED_UNKNOWN:
2411 case USB_SPEED_WIRELESS:
2412 /* Should never happen because only LS/FS/HS endpoints will get
2413 * added to the endpoint list.
2414 */
2415 return;
2416 }
2417
2418 if (tt_info)
2419 tt_info->active_eps += 1;
2420 /* Insert the endpoint into the list, largest max packet size first. */
2421 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2422 bw_endpoint_list) {
2423 if (ep_bw->max_packet_size >=
2424 smaller_ep->bw_info.max_packet_size) {
2425 /* Add the new ep before the smaller endpoint */
2426 list_add_tail(&virt_ep->bw_endpoint_list,
2427 &smaller_ep->bw_endpoint_list);
2428 return;
2429 }
2430 }
2431 /* Add the new endpoint at the end of the list. */
2432 list_add_tail(&virt_ep->bw_endpoint_list,
2433 &interval_bw->endpoints);
2434}
2435
2436void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2437 struct xhci_virt_device *virt_dev,
2438 int old_active_eps)
2439{
2440 struct xhci_root_port_bw_info *rh_bw_info;
2441 if (!virt_dev->tt_info)
2442 return;
2443
2444 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2445 if (old_active_eps == 0 &&
2446 virt_dev->tt_info->active_eps != 0) {
2447 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002448 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002449 } else if (old_active_eps != 0 &&
2450 virt_dev->tt_info->active_eps == 0) {
2451 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002452 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002453 }
2454}
2455
2456static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2457 struct xhci_virt_device *virt_dev,
2458 struct xhci_container_ctx *in_ctx)
2459{
2460 struct xhci_bw_info ep_bw_info[31];
2461 int i;
2462 struct xhci_input_control_ctx *ctrl_ctx;
2463 int old_active_eps = 0;
2464
Sarah Sharp2e279802011-09-02 11:05:50 -07002465 if (virt_dev->tt_info)
2466 old_active_eps = virt_dev->tt_info->active_eps;
2467
Lin Wang4daf9df2015-01-09 16:06:31 +02002468 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002469 if (!ctrl_ctx) {
2470 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2471 __func__);
2472 return -ENOMEM;
2473 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002474
2475 for (i = 0; i < 31; i++) {
2476 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2477 continue;
2478
2479 /* Make a copy of the BW info in case we need to revert this */
2480 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2481 sizeof(ep_bw_info[i]));
2482 /* Drop the endpoint from the interval table if the endpoint is
2483 * being dropped or changed.
2484 */
2485 if (EP_IS_DROPPED(ctrl_ctx, i))
2486 xhci_drop_ep_from_interval_table(xhci,
2487 &virt_dev->eps[i].bw_info,
2488 virt_dev->bw_table,
2489 virt_dev->udev,
2490 &virt_dev->eps[i],
2491 virt_dev->tt_info);
2492 }
2493 /* Overwrite the information stored in the endpoints' bw_info */
2494 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2495 for (i = 0; i < 31; i++) {
2496 /* Add any changed or added endpoints to the interval table */
2497 if (EP_IS_ADDED(ctrl_ctx, i))
2498 xhci_add_ep_to_interval_table(xhci,
2499 &virt_dev->eps[i].bw_info,
2500 virt_dev->bw_table,
2501 virt_dev->udev,
2502 &virt_dev->eps[i],
2503 virt_dev->tt_info);
2504 }
2505
2506 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2507 /* Ok, this fits in the bandwidth we have.
2508 * Update the number of active TTs.
2509 */
2510 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2511 return 0;
2512 }
2513
2514 /* We don't have enough bandwidth for this, revert the stored info. */
2515 for (i = 0; i < 31; i++) {
2516 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2517 continue;
2518
2519 /* Drop the new copies of any added or changed endpoints from
2520 * the interval table.
2521 */
2522 if (EP_IS_ADDED(ctrl_ctx, i)) {
2523 xhci_drop_ep_from_interval_table(xhci,
2524 &virt_dev->eps[i].bw_info,
2525 virt_dev->bw_table,
2526 virt_dev->udev,
2527 &virt_dev->eps[i],
2528 virt_dev->tt_info);
2529 }
2530 /* Revert the endpoint back to its old information */
2531 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2532 sizeof(ep_bw_info[i]));
2533 /* Add any changed or dropped endpoints back into the table */
2534 if (EP_IS_DROPPED(ctrl_ctx, i))
2535 xhci_add_ep_to_interval_table(xhci,
2536 &virt_dev->eps[i].bw_info,
2537 virt_dev->bw_table,
2538 virt_dev->udev,
2539 &virt_dev->eps[i],
2540 virt_dev->tt_info);
2541 }
2542 return -ENOMEM;
2543}
2544
2545
Sarah Sharpf2217e82009-08-07 14:04:43 -07002546/* Issue a configure endpoint command or evaluate context command
2547 * and wait for it to finish.
2548 */
2549static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002550 struct usb_device *udev,
2551 struct xhci_command *command,
2552 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002553{
2554 int ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002555 unsigned long flags;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002556 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002557 struct xhci_virt_device *virt_dev;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002558
2559 if (!command)
2560 return -EINVAL;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002561
2562 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03002563
2564 if (xhci->xhc_state & XHCI_STATE_DYING) {
2565 spin_unlock_irqrestore(&xhci->lock, flags);
2566 return -ESHUTDOWN;
2567 }
2568
Sarah Sharp913a8a32009-09-04 10:53:13 -07002569 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002570
Lin Wang4daf9df2015-01-09 16:06:31 +02002571 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002572 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002573 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002574 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2575 __func__);
2576 return -ENOMEM;
2577 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002578
2579 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002580 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002581 spin_unlock_irqrestore(&xhci->lock, flags);
2582 xhci_warn(xhci, "Not enough host resources, "
2583 "active endpoint contexts = %u\n",
2584 xhci->num_active_eps);
2585 return -ENOMEM;
2586 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002587 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002588 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
Sarah Sharp2e279802011-09-02 11:05:50 -07002589 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002590 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002591 spin_unlock_irqrestore(&xhci->lock, flags);
2592 xhci_warn(xhci, "Not enough bandwidth\n");
2593 return -ENOMEM;
2594 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002595
Sarah Sharpf2217e82009-08-07 14:04:43 -07002596 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002597 ret = xhci_queue_configure_endpoint(xhci, command,
2598 command->in_ctx->dma,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002599 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002600 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002601 ret = xhci_queue_evaluate_context(xhci, command,
2602 command->in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002603 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002604 if (ret < 0) {
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002605 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002606 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002607 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03002608 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2609 "FIXME allocate a new ring segment");
Sarah Sharpf2217e82009-08-07 14:04:43 -07002610 return -ENOMEM;
2611 }
2612 xhci_ring_cmd_db(xhci);
2613 spin_unlock_irqrestore(&xhci->lock, flags);
2614
2615 /* Wait for the configure endpoint command to complete */
Mathias Nymanc311e392014-05-08 19:26:03 +03002616 wait_for_completion(command->completion);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002617
2618 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002619 ret = xhci_configure_endpoint_result(xhci, udev,
2620 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002621 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002622 ret = xhci_evaluate_context_result(xhci, udev,
2623 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002624
2625 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2626 spin_lock_irqsave(&xhci->lock, flags);
2627 /* If the command failed, remove the reserved resources.
2628 * Otherwise, clean up the estimate to include dropped eps.
2629 */
2630 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002631 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002632 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002633 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002634 spin_unlock_irqrestore(&xhci->lock, flags);
2635 }
2636 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002637}
2638
Hans de Goededf613832013-10-04 00:29:45 +02002639static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2640 struct xhci_virt_device *vdev, int i)
2641{
2642 struct xhci_virt_ep *ep = &vdev->eps[i];
2643
2644 if (ep->ep_state & EP_HAS_STREAMS) {
2645 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2646 xhci_get_endpoint_address(i));
2647 xhci_free_stream_info(xhci, ep->stream_info);
2648 ep->stream_info = NULL;
2649 ep->ep_state &= ~EP_HAS_STREAMS;
2650 }
2651}
2652
Sarah Sharpf88ba782009-05-14 11:44:22 -07002653/* Called after one or more calls to xhci_add_endpoint() or
2654 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2655 * to call xhci_reset_bandwidth().
2656 *
2657 * Since we are in the middle of changing either configuration or
2658 * installing a new alt setting, the USB core won't allow URBs to be
2659 * enqueued for any endpoint on the old config or interface. Nothing
2660 * else should be touching the xhci->devs[slot_id] structure, so we
2661 * don't need to take the xhci->lock for manipulating that.
2662 */
Lu Baolu39693842017-04-07 17:57:04 +03002663static int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002664{
2665 int i;
2666 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002667 struct xhci_hcd *xhci;
2668 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002669 struct xhci_input_control_ctx *ctrl_ctx;
2670 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002671 struct xhci_command *command;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002672
Andiry Xu64927732010-10-14 07:22:45 -07002673 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002674 if (ret <= 0)
2675 return ret;
2676 xhci = hcd_to_xhci(hcd);
Mathias Nyman98d74f92016-04-08 16:25:10 +03002677 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
2678 (xhci->xhc_state & XHCI_STATE_REMOVING))
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002679 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002680
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002681 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002682 virt_dev = xhci->devs[udev->slot_id];
2683
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002684 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
2685 if (!command)
2686 return -ENOMEM;
2687
2688 command->in_ctx = virt_dev->in_ctx;
2689
Sarah Sharpf94e01862009-04-27 19:58:38 -07002690 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
Lin Wang4daf9df2015-01-09 16:06:31 +02002691 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002692 if (!ctrl_ctx) {
2693 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2694 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002695 ret = -ENOMEM;
2696 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002697 }
Matt Evans28ccd292011-03-29 13:40:46 +11002698 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2699 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2700 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002701
2702 /* Don't issue the command if there's no endpoints to update. */
2703 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002704 ctrl_ctx->drop_flags == 0) {
2705 ret = 0;
2706 goto command_cleanup;
2707 }
Julius Wernerd6759132014-06-24 17:14:42 +03002708 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
John Yound115b042009-07-27 12:05:15 -07002709 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Julius Wernerd6759132014-06-24 17:14:42 +03002710 for (i = 31; i >= 1; i--) {
2711 __le32 le32 = cpu_to_le32(BIT(i));
2712
2713 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
2714 || (ctrl_ctx->add_flags & le32) || i == 1) {
2715 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
2716 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
2717 break;
2718 }
2719 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07002720
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002721 ret = xhci_configure_endpoint(xhci, udev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002722 false, false);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002723 if (ret)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002724 /* Callee should call reset_bandwidth() */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002725 goto command_cleanup;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002726
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002727 /* Free any rings that were dropped, but not changed. */
Felipe Balbi98871e92017-01-23 14:20:04 +02002728 for (i = 1; i < 31; i++) {
Matt Evans4819fef2011-06-01 13:01:07 +10002729 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
Hans de Goededf613832013-10-04 00:29:45 +02002730 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03002731 xhci_free_endpoint_ring(xhci, virt_dev, i);
Hans de Goededf613832013-10-04 00:29:45 +02002732 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2733 }
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002734 }
John Yound115b042009-07-27 12:05:15 -07002735 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002736 /*
2737 * Install any rings for completely new endpoints or changed endpoints,
Mathias Nymanc5628a22017-06-15 11:55:42 +03002738 * and free any old rings from changed endpoints.
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002739 */
Felipe Balbi98871e92017-01-23 14:20:04 +02002740 for (i = 1; i < 31; i++) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002741 if (!virt_dev->eps[i].new_ring)
2742 continue;
Mathias Nymanc5628a22017-06-15 11:55:42 +03002743 /* Only free the old ring if it exists.
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002744 * It may not if this is the first add of an endpoint.
2745 */
2746 if (virt_dev->eps[i].ring) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03002747 xhci_free_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002748 }
Hans de Goededf613832013-10-04 00:29:45 +02002749 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002750 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2751 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002752 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002753command_cleanup:
2754 kfree(command->completion);
2755 kfree(command);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002756
Sarah Sharpf94e01862009-04-27 19:58:38 -07002757 return ret;
2758}
2759
Lu Baolu39693842017-04-07 17:57:04 +03002760static void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002761{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002762 struct xhci_hcd *xhci;
2763 struct xhci_virt_device *virt_dev;
2764 int i, ret;
2765
Andiry Xu64927732010-10-14 07:22:45 -07002766 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002767 if (ret <= 0)
2768 return;
2769 xhci = hcd_to_xhci(hcd);
2770
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002771 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002772 virt_dev = xhci->devs[udev->slot_id];
2773 /* Free any rings allocated for added endpoints */
Felipe Balbi98871e92017-01-23 14:20:04 +02002774 for (i = 0; i < 31; i++) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002775 if (virt_dev->eps[i].new_ring) {
2776 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2777 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002778 }
2779 }
John Yound115b042009-07-27 12:05:15 -07002780 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002781}
2782
Sarah Sharp5270b952009-09-04 10:53:11 -07002783static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002784 struct xhci_container_ctx *in_ctx,
2785 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002786 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002787 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002788{
Matt Evans28ccd292011-03-29 13:40:46 +11002789 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2790 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002791 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002792 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002793}
2794
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002795static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002796 unsigned int slot_id, unsigned int ep_index,
2797 struct xhci_dequeue_state *deq_state)
2798{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002799 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002800 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002801 struct xhci_ep_ctx *ep_ctx;
2802 u32 added_ctxs;
2803 dma_addr_t addr;
2804
Sarah Sharp92f8e762013-04-23 17:11:14 -07002805 in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02002806 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002807 if (!ctrl_ctx) {
2808 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2809 __func__);
2810 return;
2811 }
2812
Sarah Sharp913a8a32009-09-04 10:53:13 -07002813 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2814 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002815 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2816 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2817 deq_state->new_deq_ptr);
2818 if (addr == 0) {
2819 xhci_warn(xhci, "WARN Cannot submit config ep after "
2820 "reset ep command\n");
2821 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2822 deq_state->new_deq_seg,
2823 deq_state->new_deq_ptr);
2824 return;
2825 }
Matt Evans28ccd292011-03-29 13:40:46 +11002826 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002827
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002828 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002829 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002830 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2831 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002832}
2833
Mathias Nymand36374f2017-06-15 11:55:47 +03002834void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
2835 unsigned int stream_id, struct xhci_td *td)
Sarah Sharp82d10092009-08-07 14:04:52 -07002836{
2837 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002838 struct xhci_virt_ep *ep;
Mathias Nymand97b4f82014-11-27 18:19:16 +02002839 struct usb_device *udev = td->urb->dev;
Sarah Sharp82d10092009-08-07 14:04:52 -07002840
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002841 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2842 "Cleaning up stalled endpoint ring");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002843 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002844 /* We need to move the HW's dequeue pointer past this TD,
2845 * or it will attempt to resend it on the next doorbell ring.
2846 */
2847 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Mathias Nymand36374f2017-06-15 11:55:47 +03002848 ep_index, stream_id, td, &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002849
Mathias Nyman365038d2014-08-19 15:17:58 +03002850 if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
2851 return;
2852
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002853 /* HW with the reset endpoint quirk will use the saved dequeue state to
2854 * issue a configure endpoint command later.
2855 */
2856 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002857 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2858 "Queueing new dequeue state");
Hans de Goede1e3452e2014-08-20 16:41:52 +03002859 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Mathias Nyman87907362017-06-02 16:36:23 +03002860 ep_index, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002861 } else {
2862 /* Better hope no one uses the input context between now and the
2863 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002864 * XXX: No idea how this hardware will react when stream rings
2865 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002866 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002867 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2868 "Setting up input context for "
2869 "configure endpoint command");
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002870 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2871 ep_index, &deq_state);
2872 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002873}
2874
Mathias Nymand0167ad2015-03-10 19:49:00 +02002875/* Called when clearing halted device. The core should have sent the control
Mathias Nyman8e71a322014-11-18 11:27:12 +02002876 * message to clear the device halt condition. The host side of the halt should
Mathias Nymand0167ad2015-03-10 19:49:00 +02002877 * already be cleared with a reset endpoint command issued when the STALL tx
2878 * event was received.
2879 *
2880 * Context: in_interrupt
Sarah Sharpa1587d92009-07-27 12:03:15 -07002881 */
Mathias Nyman8e71a322014-11-18 11:27:12 +02002882
Lu Baolu39693842017-04-07 17:57:04 +03002883static void xhci_endpoint_reset(struct usb_hcd *hcd,
Sarah Sharpa1587d92009-07-27 12:03:15 -07002884 struct usb_host_endpoint *ep)
2885{
2886 struct xhci_hcd *xhci;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002887
2888 xhci = hcd_to_xhci(hcd);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002889
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002890 /*
Mathias Nymand0167ad2015-03-10 19:49:00 +02002891 * We might need to implement the config ep cmd in xhci 4.8.1 note:
Mathias Nyman8e71a322014-11-18 11:27:12 +02002892 * The Reset Endpoint Command may only be issued to endpoints in the
2893 * Halted state. If software wishes reset the Data Toggle or Sequence
2894 * Number of an endpoint that isn't in the Halted state, then software
2895 * may issue a Configure Endpoint Command with the Drop and Add bits set
2896 * for the target endpoint. that is in the Stopped state.
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002897 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002898
Mathias Nymand0167ad2015-03-10 19:49:00 +02002899 /* For now just print debug to follow the situation */
2900 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2901 ep->desc.bEndpointAddress);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002902}
2903
Sarah Sharp8df75f42010-04-02 15:34:16 -07002904static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2905 struct usb_device *udev, struct usb_host_endpoint *ep,
2906 unsigned int slot_id)
2907{
2908 int ret;
2909 unsigned int ep_index;
2910 unsigned int ep_state;
2911
2912 if (!ep)
2913 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002914 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002915 if (ret <= 0)
2916 return -EINVAL;
Hans de Goedea3901532013-10-04 17:05:55 +02002917 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002918 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2919 " descriptor for ep 0x%x does not support streams\n",
2920 ep->desc.bEndpointAddress);
2921 return -EINVAL;
2922 }
2923
2924 ep_index = xhci_get_endpoint_index(&ep->desc);
2925 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2926 if (ep_state & EP_HAS_STREAMS ||
2927 ep_state & EP_GETTING_STREAMS) {
2928 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2929 "already has streams set up.\n",
2930 ep->desc.bEndpointAddress);
2931 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2932 "dynamic stream context array reallocation.\n");
2933 return -EINVAL;
2934 }
2935 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2936 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2937 "endpoint 0x%x; URBs are pending.\n",
2938 ep->desc.bEndpointAddress);
2939 return -EINVAL;
2940 }
2941 return 0;
2942}
2943
2944static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
2945 unsigned int *num_streams, unsigned int *num_stream_ctxs)
2946{
2947 unsigned int max_streams;
2948
2949 /* The stream context array size must be a power of two */
2950 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
2951 /*
2952 * Find out how many primary stream array entries the host controller
2953 * supports. Later we may use secondary stream arrays (similar to 2nd
2954 * level page entries), but that's an optional feature for xHCI host
2955 * controllers. xHCs must support at least 4 stream IDs.
2956 */
2957 max_streams = HCC_MAX_PSA(xhci->hcc_params);
2958 if (*num_stream_ctxs > max_streams) {
2959 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
2960 max_streams);
2961 *num_stream_ctxs = max_streams;
2962 *num_streams = max_streams;
2963 }
2964}
2965
2966/* Returns an error code if one of the endpoint already has streams.
2967 * This does not change any data structures, it only checks and gathers
2968 * information.
2969 */
2970static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
2971 struct usb_device *udev,
2972 struct usb_host_endpoint **eps, unsigned int num_eps,
2973 unsigned int *num_streams, u32 *changed_ep_bitmask)
2974{
Sarah Sharp8df75f42010-04-02 15:34:16 -07002975 unsigned int max_streams;
2976 unsigned int endpoint_flag;
2977 int i;
2978 int ret;
2979
2980 for (i = 0; i < num_eps; i++) {
2981 ret = xhci_check_streams_endpoint(xhci, udev,
2982 eps[i], udev->slot_id);
2983 if (ret < 0)
2984 return ret;
2985
Felipe Balbi18b7ede2012-01-02 13:35:41 +02002986 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002987 if (max_streams < (*num_streams - 1)) {
2988 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
2989 eps[i]->desc.bEndpointAddress,
2990 max_streams);
2991 *num_streams = max_streams+1;
2992 }
2993
2994 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
2995 if (*changed_ep_bitmask & endpoint_flag)
2996 return -EINVAL;
2997 *changed_ep_bitmask |= endpoint_flag;
2998 }
2999 return 0;
3000}
3001
3002static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3003 struct usb_device *udev,
3004 struct usb_host_endpoint **eps, unsigned int num_eps)
3005{
3006 u32 changed_ep_bitmask = 0;
3007 unsigned int slot_id;
3008 unsigned int ep_index;
3009 unsigned int ep_state;
3010 int i;
3011
3012 slot_id = udev->slot_id;
3013 if (!xhci->devs[slot_id])
3014 return 0;
3015
3016 for (i = 0; i < num_eps; i++) {
3017 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3018 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3019 /* Are streams already being freed for the endpoint? */
3020 if (ep_state & EP_GETTING_NO_STREAMS) {
3021 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003022 "endpoint 0x%x, "
3023 "streams are being disabled already\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003024 eps[i]->desc.bEndpointAddress);
3025 return 0;
3026 }
3027 /* Are there actually any streams to free? */
3028 if (!(ep_state & EP_HAS_STREAMS) &&
3029 !(ep_state & EP_GETTING_STREAMS)) {
3030 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003031 "endpoint 0x%x, "
3032 "streams are already disabled!\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003033 eps[i]->desc.bEndpointAddress);
3034 xhci_warn(xhci, "WARN xhci_free_streams() called "
3035 "with non-streams endpoint\n");
3036 return 0;
3037 }
3038 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3039 }
3040 return changed_ep_bitmask;
3041}
3042
3043/*
Luis de Bethencourtc2a298d2015-06-30 16:48:54 +02003044 * The USB device drivers use this function (through the HCD interface in USB
Sarah Sharp8df75f42010-04-02 15:34:16 -07003045 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3046 * coordinate mass storage command queueing across multiple endpoints (basically
3047 * a stream ID == a task ID).
3048 *
3049 * Setting up streams involves allocating the same size stream context array
3050 * for each endpoint and issuing a configure endpoint command for all endpoints.
3051 *
3052 * Don't allow the call to succeed if one endpoint only supports one stream
3053 * (which means it doesn't support streams at all).
3054 *
3055 * Drivers may get less stream IDs than they asked for, if the host controller
3056 * hardware or endpoints claim they can't support the number of requested
3057 * stream IDs.
3058 */
Lu Baolu39693842017-04-07 17:57:04 +03003059static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003060 struct usb_host_endpoint **eps, unsigned int num_eps,
3061 unsigned int num_streams, gfp_t mem_flags)
3062{
3063 int i, ret;
3064 struct xhci_hcd *xhci;
3065 struct xhci_virt_device *vdev;
3066 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003067 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003068 unsigned int ep_index;
3069 unsigned int num_stream_ctxs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003070 unsigned int max_packet;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003071 unsigned long flags;
3072 u32 changed_ep_bitmask = 0;
3073
3074 if (!eps)
3075 return -EINVAL;
3076
3077 /* Add one to the number of streams requested to account for
3078 * stream 0 that is reserved for xHCI usage.
3079 */
3080 num_streams += 1;
3081 xhci = hcd_to_xhci(hcd);
3082 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3083 num_streams);
3084
Hans de Goedef7920882013-11-15 12:14:38 +01003085 /* MaxPSASize value 0 (2 streams) means streams are not supported */
Hans de Goede8f873c12014-07-25 22:01:18 +02003086 if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
3087 HCC_MAX_PSA(xhci->hcc_params) < 4) {
Hans de Goedef7920882013-11-15 12:14:38 +01003088 xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3089 return -ENOSYS;
3090 }
3091
Sarah Sharp8df75f42010-04-02 15:34:16 -07003092 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Lu Baolu74e0b562017-04-07 17:57:05 +03003093 if (!config_cmd)
Sarah Sharp8df75f42010-04-02 15:34:16 -07003094 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +03003095
Lin Wang4daf9df2015-01-09 16:06:31 +02003096 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003097 if (!ctrl_ctx) {
3098 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3099 __func__);
3100 xhci_free_command(xhci, config_cmd);
3101 return -ENOMEM;
3102 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003103
3104 /* Check to make sure all endpoints are not already configured for
3105 * streams. While we're at it, find the maximum number of streams that
3106 * all the endpoints will support and check for duplicate endpoints.
3107 */
3108 spin_lock_irqsave(&xhci->lock, flags);
3109 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3110 num_eps, &num_streams, &changed_ep_bitmask);
3111 if (ret < 0) {
3112 xhci_free_command(xhci, config_cmd);
3113 spin_unlock_irqrestore(&xhci->lock, flags);
3114 return ret;
3115 }
3116 if (num_streams <= 1) {
3117 xhci_warn(xhci, "WARN: endpoints can't handle "
3118 "more than one stream.\n");
3119 xhci_free_command(xhci, config_cmd);
3120 spin_unlock_irqrestore(&xhci->lock, flags);
3121 return -EINVAL;
3122 }
3123 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003124 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003125 * xhci_urb_enqueue() will reject all URBs.
3126 */
3127 for (i = 0; i < num_eps; i++) {
3128 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3129 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3130 }
3131 spin_unlock_irqrestore(&xhci->lock, flags);
3132
3133 /* Setup internal data structures and allocate HW data structures for
3134 * streams (but don't install the HW structures in the input context
3135 * until we're sure all memory allocation succeeded).
3136 */
3137 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3138 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3139 num_stream_ctxs, num_streams);
3140
3141 for (i = 0; i < num_eps; i++) {
3142 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
Felipe Balbi734d3dd2016-09-28 13:46:37 +03003143 max_packet = usb_endpoint_maxp(&eps[i]->desc);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003144 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3145 num_stream_ctxs,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003146 num_streams,
3147 max_packet, mem_flags);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003148 if (!vdev->eps[ep_index].stream_info)
3149 goto cleanup;
3150 /* Set maxPstreams in endpoint context and update deq ptr to
3151 * point to stream context array. FIXME
3152 */
3153 }
3154
3155 /* Set up the input context for a configure endpoint command. */
3156 for (i = 0; i < num_eps; i++) {
3157 struct xhci_ep_ctx *ep_ctx;
3158
3159 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3160 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3161
3162 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3163 vdev->out_ctx, ep_index);
3164 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3165 vdev->eps[ep_index].stream_info);
3166 }
3167 /* Tell the HW to drop its old copy of the endpoint context info
3168 * and add the updated copy from the input context.
3169 */
3170 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003171 vdev->out_ctx, ctrl_ctx,
3172 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003173
3174 /* Issue and wait for the configure endpoint command */
3175 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3176 false, false);
3177
3178 /* xHC rejected the configure endpoint command for some reason, so we
3179 * leave the old ring intact and free our internal streams data
3180 * structure.
3181 */
3182 if (ret < 0)
3183 goto cleanup;
3184
3185 spin_lock_irqsave(&xhci->lock, flags);
3186 for (i = 0; i < num_eps; i++) {
3187 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3188 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3189 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3190 udev->slot_id, ep_index);
3191 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3192 }
3193 xhci_free_command(xhci, config_cmd);
3194 spin_unlock_irqrestore(&xhci->lock, flags);
3195
3196 /* Subtract 1 for stream 0, which drivers can't use */
3197 return num_streams - 1;
3198
3199cleanup:
3200 /* If it didn't work, free the streams! */
3201 for (i = 0; i < num_eps; i++) {
3202 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3203 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003204 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003205 /* FIXME Unset maxPstreams in endpoint context and
3206 * update deq ptr to point to normal string ring.
3207 */
3208 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3209 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3210 xhci_endpoint_zero(xhci, vdev, eps[i]);
3211 }
3212 xhci_free_command(xhci, config_cmd);
3213 return -ENOMEM;
3214}
3215
3216/* Transition the endpoint from using streams to being a "normal" endpoint
3217 * without streams.
3218 *
3219 * Modify the endpoint context state, submit a configure endpoint command,
3220 * and free all endpoint rings for streams if that completes successfully.
3221 */
Lu Baolu39693842017-04-07 17:57:04 +03003222static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003223 struct usb_host_endpoint **eps, unsigned int num_eps,
3224 gfp_t mem_flags)
3225{
3226 int i, ret;
3227 struct xhci_hcd *xhci;
3228 struct xhci_virt_device *vdev;
3229 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003230 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003231 unsigned int ep_index;
3232 unsigned long flags;
3233 u32 changed_ep_bitmask;
3234
3235 xhci = hcd_to_xhci(hcd);
3236 vdev = xhci->devs[udev->slot_id];
3237
3238 /* Set up a configure endpoint command to remove the streams rings */
3239 spin_lock_irqsave(&xhci->lock, flags);
3240 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3241 udev, eps, num_eps);
3242 if (changed_ep_bitmask == 0) {
3243 spin_unlock_irqrestore(&xhci->lock, flags);
3244 return -EINVAL;
3245 }
3246
3247 /* Use the xhci_command structure from the first endpoint. We may have
3248 * allocated too many, but the driver may call xhci_free_streams() for
3249 * each endpoint it grouped into one call to xhci_alloc_streams().
3250 */
3251 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3252 command = vdev->eps[ep_index].stream_info->free_streams_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02003253 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003254 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003255 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003256 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3257 __func__);
3258 return -EINVAL;
3259 }
3260
Sarah Sharp8df75f42010-04-02 15:34:16 -07003261 for (i = 0; i < num_eps; i++) {
3262 struct xhci_ep_ctx *ep_ctx;
3263
3264 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3265 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3266 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3267 EP_GETTING_NO_STREAMS;
3268
3269 xhci_endpoint_copy(xhci, command->in_ctx,
3270 vdev->out_ctx, ep_index);
Lin Wang4daf9df2015-01-09 16:06:31 +02003271 xhci_setup_no_streams_ep_input_ctx(ep_ctx,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003272 &vdev->eps[ep_index]);
3273 }
3274 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003275 vdev->out_ctx, ctrl_ctx,
3276 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003277 spin_unlock_irqrestore(&xhci->lock, flags);
3278
3279 /* Issue and wait for the configure endpoint command,
3280 * which must succeed.
3281 */
3282 ret = xhci_configure_endpoint(xhci, udev, command,
3283 false, true);
3284
3285 /* xHC rejected the configure endpoint command for some reason, so we
3286 * leave the streams rings intact.
3287 */
3288 if (ret < 0)
3289 return ret;
3290
3291 spin_lock_irqsave(&xhci->lock, flags);
3292 for (i = 0; i < num_eps; i++) {
3293 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3294 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003295 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003296 /* FIXME Unset maxPstreams in endpoint context and
3297 * update deq ptr to point to normal string ring.
3298 */
3299 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3300 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3301 }
3302 spin_unlock_irqrestore(&xhci->lock, flags);
3303
3304 return 0;
3305}
3306
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003307/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003308 * Deletes endpoint resources for endpoints that were active before a Reset
3309 * Device command, or a Disable Slot command. The Reset Device command leaves
3310 * the control endpoint intact, whereas the Disable Slot command deletes it.
3311 *
3312 * Must be called with xhci->lock held.
3313 */
3314void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3315 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3316{
3317 int i;
3318 unsigned int num_dropped_eps = 0;
3319 unsigned int drop_flags = 0;
3320
3321 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3322 if (virt_dev->eps[i].ring) {
3323 drop_flags |= 1 << i;
3324 num_dropped_eps++;
3325 }
3326 }
3327 xhci->num_active_eps -= num_dropped_eps;
3328 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003329 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3330 "Dropped %u ep ctxs, flags = 0x%x, "
3331 "%u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003332 num_dropped_eps, drop_flags,
3333 xhci->num_active_eps);
3334}
3335
3336/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003337 * This submits a Reset Device Command, which will set the device state to 0,
3338 * set the device address to 0, and disable all the endpoints except the default
3339 * control endpoint. The USB core should come back and call
3340 * xhci_address_device(), and then re-set up the configuration. If this is
3341 * called because of a usb_reset_and_verify_device(), then the old alternate
3342 * settings will be re-installed through the normal bandwidth allocation
3343 * functions.
3344 *
3345 * Wait for the Reset Device command to finish. Remove all structures
3346 * associated with the endpoints that were disabled. Clear the input device
Mathias Nymanc5628a22017-06-15 11:55:42 +03003347 * structure? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003348 *
3349 * If the virt_dev to be reset does not exist or does not match the udev,
3350 * it means the device is lost, possibly due to the xHC restore error and
3351 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3352 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003353 */
Lu Baolu39693842017-04-07 17:57:04 +03003354static int xhci_discover_or_reset_device(struct usb_hcd *hcd,
3355 struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003356{
3357 int ret, i;
3358 unsigned long flags;
3359 struct xhci_hcd *xhci;
3360 unsigned int slot_id;
3361 struct xhci_virt_device *virt_dev;
3362 struct xhci_command *reset_device_cmd;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003363 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003364 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003365 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003366
Andiry Xuf0615c42010-10-14 07:22:48 -07003367 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003368 if (ret <= 0)
3369 return ret;
3370 xhci = hcd_to_xhci(hcd);
3371 slot_id = udev->slot_id;
3372 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003373 if (!virt_dev) {
3374 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3375 "not exist. Re-allocate the device\n", slot_id);
3376 ret = xhci_alloc_dev(hcd, udev);
3377 if (ret == 1)
3378 return 0;
3379 else
3380 return -EINVAL;
3381 }
3382
Brian Campbell326124a2015-07-21 17:20:28 +03003383 if (virt_dev->tt_info)
3384 old_active_eps = virt_dev->tt_info->active_eps;
3385
Andiry Xuf0615c42010-10-14 07:22:48 -07003386 if (virt_dev->udev != udev) {
3387 /* If the virt_dev and the udev does not match, this virt_dev
3388 * may belong to another udev.
3389 * Re-allocate the device.
3390 */
3391 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3392 "not match the udev. Re-allocate the device\n",
3393 slot_id);
3394 ret = xhci_alloc_dev(hcd, udev);
3395 if (ret == 1)
3396 return 0;
3397 else
3398 return -EINVAL;
3399 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003400
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003401 /* If device is not setup, there is no point in resetting it */
3402 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3403 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3404 SLOT_STATE_DISABLED)
3405 return 0;
3406
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003407 trace_xhci_discover_or_reset_device(slot_ctx);
3408
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003409 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3410 /* Allocate the command structure that holds the struct completion.
3411 * Assume we're in process context, since the normal device reset
3412 * process has to wait for the device anyway. Storage devices are
3413 * reset as part of error handling, so use GFP_NOIO instead of
3414 * GFP_KERNEL.
3415 */
3416 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3417 if (!reset_device_cmd) {
3418 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3419 return -ENOMEM;
3420 }
3421
3422 /* Attempt to submit the Reset Device command to the command ring */
3423 spin_lock_irqsave(&xhci->lock, flags);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003424
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003425 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003426 if (ret) {
3427 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003428 spin_unlock_irqrestore(&xhci->lock, flags);
3429 goto command_cleanup;
3430 }
3431 xhci_ring_cmd_db(xhci);
3432 spin_unlock_irqrestore(&xhci->lock, flags);
3433
3434 /* Wait for the Reset Device command to finish */
Mathias Nymanc311e392014-05-08 19:26:03 +03003435 wait_for_completion(reset_device_cmd->completion);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003436
3437 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3438 * unless we tried to reset a slot ID that wasn't enabled,
3439 * or the device wasn't in the addressed or configured state.
3440 */
3441 ret = reset_device_cmd->status;
3442 switch (ret) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003443 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03003444 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03003445 xhci_warn(xhci, "Timeout waiting for reset device command\n");
3446 ret = -ETIME;
3447 goto command_cleanup;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003448 case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */
3449 case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003450 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003451 slot_id,
3452 xhci_get_slot_state(xhci, virt_dev->out_ctx));
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003453 xhci_dbg(xhci, "Not freeing device rings.\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003454 /* Don't treat this as an error. May change my mind later. */
3455 ret = 0;
3456 goto command_cleanup;
3457 case COMP_SUCCESS:
3458 xhci_dbg(xhci, "Successful reset device command.\n");
3459 break;
3460 default:
3461 if (xhci_is_vendor_info_code(xhci, ret))
3462 break;
3463 xhci_warn(xhci, "Unknown completion code %u for "
3464 "reset device command.\n", ret);
3465 ret = -EINVAL;
3466 goto command_cleanup;
3467 }
3468
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003469 /* Free up host controller endpoint resources */
3470 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3471 spin_lock_irqsave(&xhci->lock, flags);
3472 /* Don't delete the default control endpoint resources */
3473 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3474 spin_unlock_irqrestore(&xhci->lock, flags);
3475 }
3476
Mathias Nymanc5628a22017-06-15 11:55:42 +03003477 /* Everything but endpoint 0 is disabled, so free the rings. */
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003478 last_freed_endpoint = 1;
Felipe Balbi98871e92017-01-23 14:20:04 +02003479 for (i = 1; i < 31; i++) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003480 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3481
3482 if (ep->ep_state & EP_HAS_STREAMS) {
Hans de Goededf613832013-10-04 00:29:45 +02003483 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3484 xhci_get_endpoint_address(i));
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003485 xhci_free_stream_info(xhci, ep->stream_info);
3486 ep->stream_info = NULL;
3487 ep->ep_state &= ~EP_HAS_STREAMS;
3488 }
3489
3490 if (ep->ring) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03003491 xhci_free_endpoint_ring(xhci, virt_dev, i);
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003492 last_freed_endpoint = i;
3493 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003494 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3495 xhci_drop_ep_from_interval_table(xhci,
3496 &virt_dev->eps[i].bw_info,
3497 virt_dev->bw_table,
3498 udev,
3499 &virt_dev->eps[i],
3500 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003501 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003502 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003503 /* If necessary, update the number of active TTs on this root port */
3504 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003505 ret = 0;
3506
3507command_cleanup:
3508 xhci_free_command(xhci, reset_device_cmd);
3509 return ret;
3510}
3511
3512/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003513 * At this point, the struct usb_device is about to go away, the device has
3514 * disconnected, and all traffic has been stopped and the endpoints have been
3515 * disabled. Free any HC data structures associated with that device.
3516 */
Lu Baolu39693842017-04-07 17:57:04 +03003517static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003518{
3519 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003520 struct xhci_virt_device *virt_dev;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003521 struct xhci_slot_ctx *slot_ctx;
Andiry Xu64927732010-10-14 07:22:45 -07003522 int i, ret;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003523 struct xhci_command *command;
3524
3525 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
3526 if (!command)
3527 return;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003528
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003529#ifndef CONFIG_USB_DEFAULT_PERSIST
3530 /*
3531 * We called pm_runtime_get_noresume when the device was attached.
3532 * Decrement the counter here to allow controller to runtime suspend
3533 * if no devices remain.
3534 */
3535 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003536 pm_runtime_put_noidle(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003537#endif
3538
Andiry Xu64927732010-10-14 07:22:45 -07003539 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003540 /* If the host is halted due to driver unload, we still need to free the
3541 * device.
3542 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003543 if (ret <= 0 && ret != -ENODEV) {
3544 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003545 return;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003546 }
Andiry Xu64927732010-10-14 07:22:45 -07003547
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003548 virt_dev = xhci->devs[udev->slot_id];
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003549 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3550 trace_xhci_free_dev(slot_ctx);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003551
3552 /* Stop any wayward timer functions (which may grab the lock) */
Felipe Balbi98871e92017-01-23 14:20:04 +02003553 for (i = 0; i < 31; i++) {
Mathias Nyman9983a5f2017-01-23 14:19:52 +02003554 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003555 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3556 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003557
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003558 xhci_disable_slot(xhci, command, udev->slot_id);
3559 /*
3560 * Event command completion handler will free any data structures
3561 * associated with the slot. XXX Can free sleep?
3562 */
3563}
3564
3565int xhci_disable_slot(struct xhci_hcd *xhci, struct xhci_command *command,
3566 u32 slot_id)
3567{
3568 unsigned long flags;
3569 u32 state;
3570 int ret = 0;
3571 struct xhci_virt_device *virt_dev;
3572
3573 virt_dev = xhci->devs[slot_id];
3574 if (!virt_dev)
3575 return -EINVAL;
3576 if (!command)
3577 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
3578 if (!command)
3579 return -ENOMEM;
3580
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003581 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003582 /* Don't disable the slot if the host controller is dead. */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02003583 state = readl(&xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003584 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3585 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003586 xhci_free_virt_device(xhci, slot_id);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003587 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003588 kfree(command);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003589 return ret;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003590 }
3591
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003592 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3593 slot_id);
3594 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003595 spin_unlock_irqrestore(&xhci->lock, flags);
3596 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003597 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003598 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003599 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003600 spin_unlock_irqrestore(&xhci->lock, flags);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003601 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003602}
3603
3604/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003605 * Checks if we have enough host controller resources for the default control
3606 * endpoint.
3607 *
3608 * Must be called with xhci->lock held.
3609 */
3610static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3611{
3612 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003613 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3614 "Not enough ep ctxs: "
3615 "%u active, need to add 1, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003616 xhci->num_active_eps, xhci->limit_active_eps);
3617 return -ENOMEM;
3618 }
3619 xhci->num_active_eps += 1;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003620 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3621 "Adding 1 ep ctx, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003622 xhci->num_active_eps);
3623 return 0;
3624}
3625
3626
3627/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003628 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3629 * timed out, or allocating memory failed. Returns 1 on success.
3630 */
3631int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3632{
3633 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003634 struct xhci_virt_device *vdev;
3635 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003636 unsigned long flags;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003637 int ret, slot_id;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003638 struct xhci_command *command;
3639
Lu Baolu87e44f22016-11-11 15:13:30 +02003640 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003641 if (!command)
3642 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003643
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003644 /* xhci->slot_id and xhci->addr_dev are not thread-safe */
3645 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003646 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003647 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003648 if (ret) {
3649 spin_unlock_irqrestore(&xhci->lock, flags);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003650 mutex_unlock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003651 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Lu Baolu87e44f22016-11-11 15:13:30 +02003652 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003653 return 0;
3654 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003655 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003656 spin_unlock_irqrestore(&xhci->lock, flags);
3657
Mathias Nymanc311e392014-05-08 19:26:03 +03003658 wait_for_completion(command->completion);
Lu Baoluc2d3d492016-11-11 15:13:31 +02003659 slot_id = command->slot_id;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003660 mutex_unlock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003661
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003662 if (!slot_id || command->status != COMP_SUCCESS) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003663 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharpbe982032014-05-08 19:25:59 +03003664 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
3665 HCS_MAX_SLOTS(
3666 readl(&xhci->cap_regs->hcs_params1)));
Lu Baolu87e44f22016-11-11 15:13:30 +02003667 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003668 return 0;
3669 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003670
3671 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3672 spin_lock_irqsave(&xhci->lock, flags);
3673 ret = xhci_reserve_host_control_ep_resources(xhci);
3674 if (ret) {
3675 spin_unlock_irqrestore(&xhci->lock, flags);
3676 xhci_warn(xhci, "Not enough host resources, "
3677 "active endpoint contexts = %u\n",
3678 xhci->num_active_eps);
3679 goto disable_slot;
3680 }
3681 spin_unlock_irqrestore(&xhci->lock, flags);
3682 }
3683 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003684 * xhci_discover_or_reset_device(), which may be called as part of
3685 * mass storage driver error handling.
3686 */
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003687 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003688 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003689 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003690 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003691 vdev = xhci->devs[slot_id];
3692 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
3693 trace_xhci_alloc_dev(slot_ctx);
3694
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003695 udev->slot_id = slot_id;
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003696
3697#ifndef CONFIG_USB_DEFAULT_PERSIST
3698 /*
3699 * If resetting upon resume, we can't put the controller into runtime
3700 * suspend if there is a device attached.
3701 */
3702 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003703 pm_runtime_get_noresume(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003704#endif
3705
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003706
Lu Baolu87e44f22016-11-11 15:13:30 +02003707 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003708 /* Is this a LS or FS device under a HS hub? */
3709 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003710 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003711
3712disable_slot:
3713 /* Disable slot, if we can do it without mem alloc */
Lu Baolu87e44f22016-11-11 15:13:30 +02003714 kfree(command->completion);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003715 command->completion = NULL;
3716 command->status = 0;
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003717 return xhci_disable_slot(xhci, command, udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003718}
3719
3720/*
Dan Williams48fc7db2013-12-05 17:07:27 -08003721 * Issue an Address Device command and optionally send a corresponding
3722 * SetAddress request to the device.
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003723 */
Dan Williams48fc7db2013-12-05 17:07:27 -08003724static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
3725 enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003726{
Dan Williams6f8ffc02013-11-22 01:20:01 -08003727 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003728 unsigned long flags;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003729 struct xhci_virt_device *virt_dev;
3730 int ret = 0;
3731 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003732 struct xhci_slot_ctx *slot_ctx;
3733 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003734 u64 temp_64;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003735 struct xhci_command *command = NULL;
3736
3737 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003738
Lu Baolu90797ae2017-01-03 18:28:44 +02003739 if (xhci->xhc_state) { /* dying, removing or halted */
3740 ret = -ESHUTDOWN;
Roger Quadros448116b2015-09-21 17:46:15 +03003741 goto out;
Lu Baolu90797ae2017-01-03 18:28:44 +02003742 }
Roger Quadros448116b2015-09-21 17:46:15 +03003743
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003744 if (!udev->slot_id) {
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003745 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3746 "Bad Slot ID %d", udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003747 ret = -EINVAL;
3748 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003749 }
3750
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003751 virt_dev = xhci->devs[udev->slot_id];
3752
Matt Evans7ed603e2011-03-29 13:40:56 +11003753 if (WARN_ON(!virt_dev)) {
3754 /*
3755 * In plug/unplug torture test with an NEC controller,
3756 * a zero-dereference was observed once due to virt_dev = 0.
3757 * Print useful debug rather than crash if it is observed again!
3758 */
3759 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3760 udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003761 ret = -EINVAL;
3762 goto out;
Matt Evans7ed603e2011-03-29 13:40:56 +11003763 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003764 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3765 trace_xhci_setup_device_slot(slot_ctx);
Matt Evans7ed603e2011-03-29 13:40:56 +11003766
Mathias Nymanf161ead2015-01-09 17:18:28 +02003767 if (setup == SETUP_CONTEXT_ONLY) {
Mathias Nymanf161ead2015-01-09 17:18:28 +02003768 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3769 SLOT_STATE_DEFAULT) {
3770 xhci_dbg(xhci, "Slot already in default state\n");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003771 goto out;
Mathias Nymanf161ead2015-01-09 17:18:28 +02003772 }
3773 }
3774
Lu Baolu87e44f22016-11-11 15:13:30 +02003775 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003776 if (!command) {
3777 ret = -ENOMEM;
3778 goto out;
3779 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003780
3781 command->in_ctx = virt_dev->in_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003782
Andiry Xuf0615c42010-10-14 07:22:48 -07003783 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Lin Wang4daf9df2015-01-09 16:06:31 +02003784 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003785 if (!ctrl_ctx) {
3786 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3787 __func__);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003788 ret = -EINVAL;
3789 goto out;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003790 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003791 /*
3792 * If this is the first Set Address since device plug-in or
3793 * virt_device realloaction after a resume with an xHCI power loss,
3794 * then set up the slot context.
3795 */
3796 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003797 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003798 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003799 else
3800 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003801 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3802 ctrl_ctx->drop_flags = 0;
3803
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003804 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003805 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003806
Sarah Sharpf88ba782009-05-14 11:44:22 -07003807 spin_lock_irqsave(&xhci->lock, flags);
Felipe Balbia711ede2017-01-23 14:20:23 +02003808 trace_xhci_setup_device(virt_dev);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003809 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
Dan Williams48fc7db2013-12-05 17:07:27 -08003810 udev->slot_id, setup);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003811 if (ret) {
3812 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003813 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3814 "FIXME: allocate a command ring segment");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003815 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003816 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003817 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003818 spin_unlock_irqrestore(&xhci->lock, flags);
3819
3820 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
Mathias Nymanc311e392014-05-08 19:26:03 +03003821 wait_for_completion(command->completion);
3822
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003823 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3824 * the SetAddress() "recovery interval" required by USB and aborting the
3825 * command on a timeout.
3826 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03003827 switch (command->status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003828 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03003829 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03003830 xhci_warn(xhci, "Timeout while waiting for setup device command\n");
3831 ret = -ETIME;
3832 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003833 case COMP_CONTEXT_STATE_ERROR:
3834 case COMP_SLOT_NOT_ENABLED_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003835 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
3836 act, udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003837 ret = -EINVAL;
3838 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003839 case COMP_USB_TRANSACTION_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003840 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003841 ret = -EPROTO;
3842 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003843 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003844 dev_warn(&udev->dev,
3845 "ERROR: Incompatible device for setup %s command\n", act);
Alex Hef6ba6fe2011-06-08 18:34:06 +08003846 ret = -ENODEV;
3847 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003848 case COMP_SUCCESS:
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003849 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williams6f8ffc02013-11-22 01:20:01 -08003850 "Successful setup %s command", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003851 break;
3852 default:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003853 xhci_err(xhci,
3854 "ERROR: unexpected setup %s command completion code 0x%x.\n",
Mathias Nyman9ea18332014-05-08 19:26:02 +03003855 act, command->status);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003856 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003857 ret = -EINVAL;
3858 break;
3859 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003860 if (ret)
3861 goto out;
Sarah Sharpf7b2e402014-01-30 13:27:49 -08003862 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003863 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3864 "Op regs DCBAA ptr = %#016llx", temp_64);
3865 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3866 "Slot ID %d dcbaa entry @%p = %#016llx",
3867 udev->slot_id,
3868 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3869 (unsigned long long)
3870 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3871 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3872 "Output Context DMA address = %#08llx",
John Yound115b042009-07-27 12:05:15 -07003873 (unsigned long long)virt_dev->out_ctx->dma);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003874 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003875 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003876 /*
3877 * USB core uses address 1 for the roothubs, so we add one to the
3878 * address given back to us by the HC.
3879 */
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003880 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003881 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003882 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003883 ctrl_ctx->add_flags = 0;
3884 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003885
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003886 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williamsa2cdc342013-10-16 12:25:44 -07003887 "Internal device address = %d",
3888 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003889out:
3890 mutex_unlock(&xhci->mutex);
Lu Baolu87e44f22016-11-11 15:13:30 +02003891 if (command) {
3892 kfree(command->completion);
3893 kfree(command);
3894 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003895 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003896}
3897
Lu Baolu39693842017-04-07 17:57:04 +03003898static int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
Dan Williams48fc7db2013-12-05 17:07:27 -08003899{
3900 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
3901}
3902
Lu Baolu39693842017-04-07 17:57:04 +03003903static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
Dan Williams48fc7db2013-12-05 17:07:27 -08003904{
3905 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
3906}
3907
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003908/*
3909 * Transfer the port index into real index in the HW port status
3910 * registers. Caculate offset between the port's PORTSC register
3911 * and port status base. Divide the number of per port register
3912 * to get the real index. The raw port number bases 1.
3913 */
3914int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3915{
3916 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3917 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3918 __le32 __iomem *addr;
3919 int raw_port;
3920
Mathias Nymanb50107b2015-10-01 18:40:38 +03003921 if (hcd->speed < HCD_USB3)
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003922 addr = xhci->usb2_ports[port1 - 1];
3923 else
3924 addr = xhci->usb3_ports[port1 - 1];
3925
3926 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3927 return raw_port;
3928}
3929
Mathias Nymana558ccd2013-05-23 17:14:30 +03003930/*
3931 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3932 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3933 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07003934static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03003935 struct usb_device *udev, u16 max_exit_latency)
3936{
3937 struct xhci_virt_device *virt_dev;
3938 struct xhci_command *command;
3939 struct xhci_input_control_ctx *ctrl_ctx;
3940 struct xhci_slot_ctx *slot_ctx;
3941 unsigned long flags;
3942 int ret;
3943
3944 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nyman96044692014-09-11 13:55:50 +03003945
3946 virt_dev = xhci->devs[udev->slot_id];
3947
3948 /*
3949 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
3950 * xHC was re-initialized. Exit latency will be set later after
3951 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
3952 */
3953
3954 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03003955 spin_unlock_irqrestore(&xhci->lock, flags);
3956 return 0;
3957 }
3958
3959 /* Attempt to issue an Evaluate Context command to change the MEL. */
Mathias Nymana558ccd2013-05-23 17:14:30 +03003960 command = xhci->lpm_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02003961 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003962 if (!ctrl_ctx) {
3963 spin_unlock_irqrestore(&xhci->lock, flags);
3964 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3965 __func__);
3966 return -ENOMEM;
3967 }
3968
Mathias Nymana558ccd2013-05-23 17:14:30 +03003969 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
3970 spin_unlock_irqrestore(&xhci->lock, flags);
3971
Mathias Nymana558ccd2013-05-23 17:14:30 +03003972 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
3973 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
3974 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
3975 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
Mathias Nyman4801d4ea2014-11-27 18:19:15 +02003976 slot_ctx->dev_state = 0;
Mathias Nymana558ccd2013-05-23 17:14:30 +03003977
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03003978 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
3979 "Set up evaluate context for LPM MEL change.");
Mathias Nymana558ccd2013-05-23 17:14:30 +03003980
3981 /* Issue and wait for the evaluate context command. */
3982 ret = xhci_configure_endpoint(xhci, udev, command,
3983 true, true);
Mathias Nymana558ccd2013-05-23 17:14:30 +03003984
3985 if (!ret) {
3986 spin_lock_irqsave(&xhci->lock, flags);
3987 virt_dev->current_mel = max_exit_latency;
3988 spin_unlock_irqrestore(&xhci->lock, flags);
3989 }
3990 return ret;
3991}
3992
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01003993#ifdef CONFIG_PM
Andiry Xu95743232011-09-23 14:19:51 -07003994
3995/* BESL to HIRD Encoding array for USB2 LPM */
3996static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3997 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3998
3999/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08004000static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4001 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07004002{
Andiry Xuf99298b2011-12-12 16:45:28 +08004003 int u2del, besl, besl_host;
4004 int besl_device = 0;
4005 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07004006
Andiry Xuf99298b2011-12-12 16:45:28 +08004007 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4008 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4009
4010 if (field & USB_BESL_SUPPORT) {
4011 for (besl_host = 0; besl_host < 16; besl_host++) {
4012 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07004013 break;
4014 }
Andiry Xuf99298b2011-12-12 16:45:28 +08004015 /* Use baseline BESL value as default */
4016 if (field & USB_BESL_BASELINE_VALID)
4017 besl_device = USB_GET_BESL_BASELINE(field);
4018 else if (field & USB_BESL_DEEP_VALID)
4019 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07004020 } else {
4021 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08004022 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07004023 else
Andiry Xuf99298b2011-12-12 16:45:28 +08004024 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07004025 }
4026
Andiry Xuf99298b2011-12-12 16:45:28 +08004027 besl = besl_host + besl_device;
4028 if (besl > 15)
4029 besl = 15;
4030
4031 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07004032}
4033
Mathias Nymana558ccd2013-05-23 17:14:30 +03004034/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4035static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4036{
4037 u32 field;
4038 int l1;
4039 int besld = 0;
4040 int hirdm = 0;
4041
4042 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4043
4044 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004045 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004046
4047 /* device has preferred BESLD */
4048 if (field & USB_BESL_DEEP_VALID) {
4049 besld = USB_GET_BESL_DEEP(field);
4050 hirdm = 1;
4051 }
4052
4053 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4054}
4055
Lu Baolu39693842017-04-07 17:57:04 +03004056static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
Andiry Xu65580b432011-09-23 14:19:52 -07004057 struct usb_device *udev, int enable)
4058{
4059 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4060 __le32 __iomem **port_array;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004061 __le32 __iomem *pm_addr, *hlpm_addr;
4062 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004063 unsigned int port_num;
4064 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004065 int hird, exit_latency;
4066 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004067
Mathias Nymanb50107b2015-10-01 18:40:38 +03004068 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
Andiry Xu65580b432011-09-23 14:19:52 -07004069 !udev->lpm_capable)
4070 return -EPERM;
4071
4072 if (!udev->parent || udev->parent->parent ||
4073 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4074 return -EPERM;
4075
4076 if (udev->usb2_hw_lpm_capable != 1)
4077 return -EPERM;
4078
4079 spin_lock_irqsave(&xhci->lock, flags);
4080
4081 port_array = xhci->usb2_ports;
4082 port_num = udev->portnum - 1;
Mathias Nymanb6e76372013-05-23 17:14:29 +03004083 pm_addr = port_array[port_num] + PORTPMSC;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004084 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004085 hlpm_addr = port_array[port_num] + PORTHLPMC;
4086 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004087
4088 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
Lin Wang654a55d2014-05-08 19:25:54 +03004089 enable ? "enable" : "disable", port_num + 1);
Andiry Xu65580b432011-09-23 14:19:52 -07004090
Andiry Xu65580b432011-09-23 14:19:52 -07004091 if (enable) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004092 /* Host supports BESL timeout instead of HIRD */
4093 if (udev->usb2_hw_lpm_besl_capable) {
4094 /* if device doesn't have a preferred BESL value use a
4095 * default one which works with mixed HIRD and BESL
4096 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4097 */
4098 if ((field & USB_BESL_SUPPORT) &&
4099 (field & USB_BESL_BASELINE_VALID))
4100 hird = USB_GET_BESL_BASELINE(field);
4101 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004102 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004103
4104 exit_latency = xhci_besl_encoding[hird];
4105 spin_unlock_irqrestore(&xhci->lock, flags);
4106
4107 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4108 * input context for link powermanagement evaluate
4109 * context commands. It is protected by hcd->bandwidth
4110 * mutex and is shared by all devices. We need to set
4111 * the max ext latency in USB 2 BESL LPM as well, so
4112 * use the same mutex and xhci_change_max_exit_latency()
4113 */
4114 mutex_lock(hcd->bandwidth_mutex);
4115 ret = xhci_change_max_exit_latency(xhci, udev,
4116 exit_latency);
4117 mutex_unlock(hcd->bandwidth_mutex);
4118
4119 if (ret < 0)
4120 return ret;
4121 spin_lock_irqsave(&xhci->lock, flags);
4122
4123 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004124 writel(hlpm_val, hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004125 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004126 readl(hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004127 } else {
4128 hird = xhci_calculate_hird_besl(xhci, udev);
4129 }
4130
4131 pm_val &= ~PORT_HIRD_MASK;
Sarah Sharp58e21f72013-10-07 17:17:20 -07004132 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004133 writel(pm_val, pm_addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004134 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004135 pm_val |= PORT_HLE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004136 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004137 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004138 readl(pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004139 } else {
Sarah Sharp58e21f72013-10-07 17:17:20 -07004140 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004141 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004142 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004143 readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004144 if (udev->usb2_hw_lpm_besl_capable) {
4145 spin_unlock_irqrestore(&xhci->lock, flags);
4146 mutex_lock(hcd->bandwidth_mutex);
4147 xhci_change_max_exit_latency(xhci, udev, 0);
4148 mutex_unlock(hcd->bandwidth_mutex);
4149 return 0;
4150 }
Andiry Xu65580b432011-09-23 14:19:52 -07004151 }
4152
4153 spin_unlock_irqrestore(&xhci->lock, flags);
4154 return 0;
4155}
4156
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004157/* check if a usb2 port supports a given extened capability protocol
4158 * only USB2 ports extended protocol capability values are cached.
4159 * Return 1 if capability is supported
4160 */
4161static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4162 unsigned capability)
4163{
4164 u32 port_offset, port_count;
4165 int i;
4166
4167 for (i = 0; i < xhci->num_ext_caps; i++) {
4168 if (xhci->ext_caps[i] & capability) {
4169 /* port offsets starts at 1 */
4170 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4171 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4172 if (port >= port_offset &&
4173 port < port_offset + port_count)
4174 return 1;
4175 }
4176 }
4177 return 0;
4178}
4179
Lu Baolu39693842017-04-07 17:57:04 +03004180static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004181{
4182 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004183 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004184
Mathias Nymanb50107b2015-10-01 18:40:38 +03004185 if (hcd->speed >= HCD_USB3 || !xhci->sw_lpm_support ||
Sarah Sharpde68bab2013-09-30 17:26:28 +03004186 !udev->lpm_capable)
4187 return 0;
4188
4189 /* we only support lpm for non-hub device connected to root hub yet */
4190 if (!udev->parent || udev->parent->parent ||
4191 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4192 return 0;
4193
4194 if (xhci->hw_lpm_support == 1 &&
4195 xhci_check_usb2_port_capability(
4196 xhci, portnum, XHCI_HLC)) {
4197 udev->usb2_hw_lpm_capable = 1;
4198 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4199 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4200 if (xhci_check_usb2_port_capability(xhci, portnum,
4201 XHCI_BLC))
4202 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004203 }
4204
4205 return 0;
4206}
4207
Sarah Sharp3b3db022012-05-09 10:55:03 -07004208/*---------------------- USB 3.0 Link PM functions ------------------------*/
4209
Sarah Sharpe3567d22012-05-16 13:36:24 -07004210/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4211static unsigned long long xhci_service_interval_to_ns(
4212 struct usb_endpoint_descriptor *desc)
4213{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004214 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004215}
4216
Sarah Sharp3b3db022012-05-09 10:55:03 -07004217static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4218 enum usb3_link_state state)
4219{
4220 unsigned long long sel;
4221 unsigned long long pel;
4222 unsigned int max_sel_pel;
4223 char *state_name;
4224
4225 switch (state) {
4226 case USB3_LPM_U1:
4227 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4228 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4229 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4230 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4231 state_name = "U1";
4232 break;
4233 case USB3_LPM_U2:
4234 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4235 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4236 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4237 state_name = "U2";
4238 break;
4239 default:
4240 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4241 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004242 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004243 }
4244
4245 if (sel <= max_sel_pel && pel <= max_sel_pel)
4246 return USB3_LPM_DEVICE_INITIATED;
4247
4248 if (sel > max_sel_pel)
4249 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4250 "due to long SEL %llu ms\n",
4251 state_name, sel);
4252 else
4253 dev_dbg(&udev->dev, "Device-initiated %s disabled "
Joe Perches03e64e92013-07-16 19:25:59 -07004254 "due to long PEL %llu ms\n",
Sarah Sharp3b3db022012-05-09 10:55:03 -07004255 state_name, pel);
4256 return USB3_LPM_DISABLED;
4257}
4258
Pratyush Anand9502c462014-07-04 17:01:23 +03004259/* The U1 timeout should be the maximum of the following values:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004260 * - For control endpoints, U1 system exit latency (SEL) * 3
4261 * - For bulk endpoints, U1 SEL * 5
4262 * - For interrupt endpoints:
4263 * - Notification EPs, U1 SEL * 3
4264 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4265 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4266 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004267static unsigned long long xhci_calculate_intel_u1_timeout(
4268 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004269 struct usb_endpoint_descriptor *desc)
4270{
4271 unsigned long long timeout_ns;
4272 int ep_type;
4273 int intr_type;
4274
4275 ep_type = usb_endpoint_type(desc);
4276 switch (ep_type) {
4277 case USB_ENDPOINT_XFER_CONTROL:
4278 timeout_ns = udev->u1_params.sel * 3;
4279 break;
4280 case USB_ENDPOINT_XFER_BULK:
4281 timeout_ns = udev->u1_params.sel * 5;
4282 break;
4283 case USB_ENDPOINT_XFER_INT:
4284 intr_type = usb_endpoint_interrupt_type(desc);
4285 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4286 timeout_ns = udev->u1_params.sel * 3;
4287 break;
4288 }
4289 /* Otherwise the calculation is the same as isoc eps */
4290 case USB_ENDPOINT_XFER_ISOC:
4291 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004292 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004293 if (timeout_ns < udev->u1_params.sel * 2)
4294 timeout_ns = udev->u1_params.sel * 2;
4295 break;
4296 default:
4297 return 0;
4298 }
4299
Pratyush Anand9502c462014-07-04 17:01:23 +03004300 return timeout_ns;
4301}
4302
4303/* Returns the hub-encoded U1 timeout value. */
4304static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
4305 struct usb_device *udev,
4306 struct usb_endpoint_descriptor *desc)
4307{
4308 unsigned long long timeout_ns;
4309
4310 if (xhci->quirks & XHCI_INTEL_HOST)
4311 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
4312 else
4313 timeout_ns = udev->u1_params.sel;
4314
4315 /* The U1 timeout is encoded in 1us intervals.
4316 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4317 */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004318 if (timeout_ns == USB3_LPM_DISABLED)
Pratyush Anand9502c462014-07-04 17:01:23 +03004319 timeout_ns = 1;
4320 else
4321 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004322
4323 /* If the necessary timeout value is bigger than what we can set in the
4324 * USB 3.0 hub, we have to disable hub-initiated U1.
4325 */
4326 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4327 return timeout_ns;
4328 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4329 "due to long timeout %llu ms\n", timeout_ns);
4330 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4331}
4332
Pratyush Anand9502c462014-07-04 17:01:23 +03004333/* The U2 timeout should be the maximum of:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004334 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4335 * - largest bInterval of any active periodic endpoint (to avoid going
4336 * into lower power link states between intervals).
4337 * - the U2 Exit Latency of the device
4338 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004339static unsigned long long xhci_calculate_intel_u2_timeout(
4340 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004341 struct usb_endpoint_descriptor *desc)
4342{
4343 unsigned long long timeout_ns;
4344 unsigned long long u2_del_ns;
4345
4346 timeout_ns = 10 * 1000 * 1000;
4347
4348 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4349 (xhci_service_interval_to_ns(desc) > timeout_ns))
4350 timeout_ns = xhci_service_interval_to_ns(desc);
4351
Oliver Neukum966e7a82012-10-17 12:17:50 +02004352 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004353 if (u2_del_ns > timeout_ns)
4354 timeout_ns = u2_del_ns;
4355
Pratyush Anand9502c462014-07-04 17:01:23 +03004356 return timeout_ns;
4357}
4358
4359/* Returns the hub-encoded U2 timeout value. */
4360static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
4361 struct usb_device *udev,
4362 struct usb_endpoint_descriptor *desc)
4363{
4364 unsigned long long timeout_ns;
4365
4366 if (xhci->quirks & XHCI_INTEL_HOST)
4367 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
4368 else
4369 timeout_ns = udev->u2_params.sel;
4370
Sarah Sharpe3567d22012-05-16 13:36:24 -07004371 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004372 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004373 /* If the necessary timeout value is bigger than what we can set in the
4374 * USB 3.0 hub, we have to disable hub-initiated U2.
4375 */
4376 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4377 return timeout_ns;
4378 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4379 "due to long timeout %llu ms\n", timeout_ns);
4380 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4381}
4382
Sarah Sharp3b3db022012-05-09 10:55:03 -07004383static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4384 struct usb_device *udev,
4385 struct usb_endpoint_descriptor *desc,
4386 enum usb3_link_state state,
4387 u16 *timeout)
4388{
Pratyush Anand9502c462014-07-04 17:01:23 +03004389 if (state == USB3_LPM_U1)
4390 return xhci_calculate_u1_timeout(xhci, udev, desc);
4391 else if (state == USB3_LPM_U2)
4392 return xhci_calculate_u2_timeout(xhci, udev, desc);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004393
Sarah Sharp3b3db022012-05-09 10:55:03 -07004394 return USB3_LPM_DISABLED;
4395}
4396
4397static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4398 struct usb_device *udev,
4399 struct usb_endpoint_descriptor *desc,
4400 enum usb3_link_state state,
4401 u16 *timeout)
4402{
4403 u16 alt_timeout;
4404
4405 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4406 desc, state, timeout);
4407
4408 /* If we found we can't enable hub-initiated LPM, or
4409 * the U1 or U2 exit latency was too high to allow
4410 * device-initiated LPM as well, just stop searching.
4411 */
4412 if (alt_timeout == USB3_LPM_DISABLED ||
4413 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4414 *timeout = alt_timeout;
4415 return -E2BIG;
4416 }
4417 if (alt_timeout > *timeout)
4418 *timeout = alt_timeout;
4419 return 0;
4420}
4421
4422static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4423 struct usb_device *udev,
4424 struct usb_host_interface *alt,
4425 enum usb3_link_state state,
4426 u16 *timeout)
4427{
4428 int j;
4429
4430 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4431 if (xhci_update_timeout_for_endpoint(xhci, udev,
4432 &alt->endpoint[j].desc, state, timeout))
4433 return -E2BIG;
4434 continue;
4435 }
4436 return 0;
4437}
4438
Sarah Sharpe3567d22012-05-16 13:36:24 -07004439static int xhci_check_intel_tier_policy(struct usb_device *udev,
4440 enum usb3_link_state state)
4441{
4442 struct usb_device *parent;
4443 unsigned int num_hubs;
4444
4445 if (state == USB3_LPM_U2)
4446 return 0;
4447
4448 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4449 for (parent = udev->parent, num_hubs = 0; parent->parent;
4450 parent = parent->parent)
4451 num_hubs++;
4452
4453 if (num_hubs < 2)
4454 return 0;
4455
4456 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4457 " below second-tier hub.\n");
4458 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4459 "to decrease power consumption.\n");
4460 return -E2BIG;
4461}
4462
Sarah Sharp3b3db022012-05-09 10:55:03 -07004463static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4464 struct usb_device *udev,
4465 enum usb3_link_state state)
4466{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004467 if (xhci->quirks & XHCI_INTEL_HOST)
4468 return xhci_check_intel_tier_policy(udev, state);
Pratyush Anand9502c462014-07-04 17:01:23 +03004469 else
4470 return 0;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004471}
4472
4473/* Returns the U1 or U2 timeout that should be enabled.
4474 * If the tier check or timeout setting functions return with a non-zero exit
4475 * code, that means the timeout value has been finalized and we shouldn't look
4476 * at any more endpoints.
4477 */
4478static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4479 struct usb_device *udev, enum usb3_link_state state)
4480{
4481 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4482 struct usb_host_config *config;
4483 char *state_name;
4484 int i;
4485 u16 timeout = USB3_LPM_DISABLED;
4486
4487 if (state == USB3_LPM_U1)
4488 state_name = "U1";
4489 else if (state == USB3_LPM_U2)
4490 state_name = "U2";
4491 else {
4492 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4493 state);
4494 return timeout;
4495 }
4496
4497 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4498 return timeout;
4499
4500 /* Gather some information about the currently installed configuration
4501 * and alternate interface settings.
4502 */
4503 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4504 state, &timeout))
4505 return timeout;
4506
4507 config = udev->actconfig;
4508 if (!config)
4509 return timeout;
4510
Xenia Ragiadakou64ba4192013-08-26 23:29:46 +03004511 for (i = 0; i < config->desc.bNumInterfaces; i++) {
Sarah Sharp3b3db022012-05-09 10:55:03 -07004512 struct usb_driver *driver;
4513 struct usb_interface *intf = config->interface[i];
4514
4515 if (!intf)
4516 continue;
4517
4518 /* Check if any currently bound drivers want hub-initiated LPM
4519 * disabled.
4520 */
4521 if (intf->dev.driver) {
4522 driver = to_usb_driver(intf->dev.driver);
4523 if (driver && driver->disable_hub_initiated_lpm) {
4524 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4525 "at request of driver %s\n",
4526 state_name, driver->name);
4527 return xhci_get_timeout_no_hub_lpm(udev, state);
4528 }
4529 }
4530
4531 /* Not sure how this could happen... */
4532 if (!intf->cur_altsetting)
4533 continue;
4534
4535 if (xhci_update_timeout_for_interface(xhci, udev,
4536 intf->cur_altsetting,
4537 state, &timeout))
4538 return timeout;
4539 }
4540 return timeout;
4541}
4542
Sarah Sharp3b3db022012-05-09 10:55:03 -07004543static int calculate_max_exit_latency(struct usb_device *udev,
4544 enum usb3_link_state state_changed,
4545 u16 hub_encoded_timeout)
4546{
4547 unsigned long long u1_mel_us = 0;
4548 unsigned long long u2_mel_us = 0;
4549 unsigned long long mel_us = 0;
4550 bool disabling_u1;
4551 bool disabling_u2;
4552 bool enabling_u1;
4553 bool enabling_u2;
4554
4555 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4556 hub_encoded_timeout == USB3_LPM_DISABLED);
4557 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4558 hub_encoded_timeout == USB3_LPM_DISABLED);
4559
4560 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4561 hub_encoded_timeout != USB3_LPM_DISABLED);
4562 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4563 hub_encoded_timeout != USB3_LPM_DISABLED);
4564
4565 /* If U1 was already enabled and we're not disabling it,
4566 * or we're going to enable U1, account for the U1 max exit latency.
4567 */
4568 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4569 enabling_u1)
4570 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4571 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4572 enabling_u2)
4573 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4574
4575 if (u1_mel_us > u2_mel_us)
4576 mel_us = u1_mel_us;
4577 else
4578 mel_us = u2_mel_us;
4579 /* xHCI host controller max exit latency field is only 16 bits wide. */
4580 if (mel_us > MAX_EXIT) {
4581 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4582 "is too big.\n", mel_us);
4583 return -E2BIG;
4584 }
4585 return mel_us;
4586}
4587
4588/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
Lu Baolu39693842017-04-07 17:57:04 +03004589static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharp3b3db022012-05-09 10:55:03 -07004590 struct usb_device *udev, enum usb3_link_state state)
4591{
4592 struct xhci_hcd *xhci;
4593 u16 hub_encoded_timeout;
4594 int mel;
4595 int ret;
4596
4597 xhci = hcd_to_xhci(hcd);
4598 /* The LPM timeout values are pretty host-controller specific, so don't
4599 * enable hub-initiated timeouts unless the vendor has provided
4600 * information about their timeout algorithm.
4601 */
4602 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4603 !xhci->devs[udev->slot_id])
4604 return USB3_LPM_DISABLED;
4605
4606 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4607 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4608 if (mel < 0) {
4609 /* Max Exit Latency is too big, disable LPM. */
4610 hub_encoded_timeout = USB3_LPM_DISABLED;
4611 mel = 0;
4612 }
4613
4614 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4615 if (ret)
4616 return ret;
4617 return hub_encoded_timeout;
4618}
4619
Lu Baolu39693842017-04-07 17:57:04 +03004620static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharp3b3db022012-05-09 10:55:03 -07004621 struct usb_device *udev, enum usb3_link_state state)
4622{
4623 struct xhci_hcd *xhci;
4624 u16 mel;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004625
4626 xhci = hcd_to_xhci(hcd);
4627 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4628 !xhci->devs[udev->slot_id])
4629 return 0;
4630
4631 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
Saurabh Karajgaonkarf1cda542015-08-04 14:04:09 +00004632 return xhci_change_max_exit_latency(xhci, udev, mel);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004633}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004634#else /* CONFIG_PM */
4635
Lu Baolu39693842017-04-07 17:57:04 +03004636static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004637 struct usb_device *udev, int enable)
4638{
4639 return 0;
4640}
4641
Lu Baolu39693842017-04-07 17:57:04 +03004642static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004643{
4644 return 0;
4645}
4646
Lu Baolu39693842017-04-07 17:57:04 +03004647static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004648 struct usb_device *udev, enum usb3_link_state state)
4649{
4650 return USB3_LPM_DISABLED;
4651}
4652
Lu Baolu39693842017-04-07 17:57:04 +03004653static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004654 struct usb_device *udev, enum usb3_link_state state)
4655{
4656 return 0;
4657}
4658#endif /* CONFIG_PM */
4659
Sarah Sharp3b3db022012-05-09 10:55:03 -07004660/*-------------------------------------------------------------------------*/
4661
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004662/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4663 * internal data structures for the device.
4664 */
Lu Baolu39693842017-04-07 17:57:04 +03004665static int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004666 struct usb_tt *tt, gfp_t mem_flags)
4667{
4668 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4669 struct xhci_virt_device *vdev;
4670 struct xhci_command *config_cmd;
4671 struct xhci_input_control_ctx *ctrl_ctx;
4672 struct xhci_slot_ctx *slot_ctx;
4673 unsigned long flags;
4674 unsigned think_time;
4675 int ret;
4676
4677 /* Ignore root hubs */
4678 if (!hdev->parent)
4679 return 0;
4680
4681 vdev = xhci->devs[hdev->slot_id];
4682 if (!vdev) {
4683 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4684 return -EINVAL;
4685 }
Lu Baolu74e0b562017-04-07 17:57:05 +03004686
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004687 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Lu Baolu74e0b562017-04-07 17:57:05 +03004688 if (!config_cmd)
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004689 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +03004690
Lin Wang4daf9df2015-01-09 16:06:31 +02004691 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004692 if (!ctrl_ctx) {
4693 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4694 __func__);
4695 xhci_free_command(xhci, config_cmd);
4696 return -ENOMEM;
4697 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004698
4699 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004700 if (hdev->speed == USB_SPEED_HIGH &&
4701 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4702 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4703 xhci_free_command(xhci, config_cmd);
4704 spin_unlock_irqrestore(&xhci->lock, flags);
4705 return -ENOMEM;
4706 }
4707
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004708 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004709 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004710 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004711 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004712 /*
4713 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4714 * but it may be already set to 1 when setup an xHCI virtual
4715 * device, so clear it anyway.
4716 */
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004717 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004718 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004719 else if (hdev->speed == USB_SPEED_FULL)
4720 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
4721
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004722 if (xhci->hci_version > 0x95) {
4723 xhci_dbg(xhci, "xHCI version %x needs hub "
4724 "TT think time and number of ports\n",
4725 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004726 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004727 /* Set TT think time - convert from ns to FS bit times.
4728 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4729 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004730 *
4731 * xHCI 1.0: this field shall be 0 if the device is not a
4732 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004733 */
4734 think_time = tt->think_time;
4735 if (think_time != 0)
4736 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004737 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4738 slot_ctx->tt_info |=
4739 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004740 } else {
4741 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4742 "TT think time or number of ports\n",
4743 (unsigned int) xhci->hci_version);
4744 }
4745 slot_ctx->dev_state = 0;
4746 spin_unlock_irqrestore(&xhci->lock, flags);
4747
4748 xhci_dbg(xhci, "Set up %s for hub device.\n",
4749 (xhci->hci_version > 0x95) ?
4750 "configure endpoint" : "evaluate context");
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004751
4752 /* Issue and wait for the configure endpoint or
4753 * evaluate context command.
4754 */
4755 if (xhci->hci_version > 0x95)
4756 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4757 false, false);
4758 else
4759 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4760 true, false);
4761
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004762 xhci_free_command(xhci, config_cmd);
4763 return ret;
4764}
4765
Lu Baolu39693842017-04-07 17:57:04 +03004766static int xhci_get_frame(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004767{
4768 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4769 /* EHCI mods by the periodic size. Why? */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004770 return readl(&xhci->run_regs->microframe_index) >> 3;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004771}
4772
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004773int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4774{
4775 struct xhci_hcd *xhci;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +08004776 /*
4777 * TODO: Check with DWC3 clients for sysdev according to
4778 * quirks
4779 */
4780 struct device *dev = hcd->self.sysdev;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004781 int retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004782
Sarah Sharp1386ff72014-01-31 11:45:02 -08004783 /* Accept arbitrarily long scatter-gather lists */
4784 hcd->self.sg_tablesize = ~0;
Ming Leifc760512013-08-08 21:48:23 +08004785
Mathias Nymane2ed5112014-03-07 17:06:57 +02004786 /* support to build packet from discontinuous buffers */
4787 hcd->self.no_sg_constraint = 1;
4788
Hans de Goede19181bc2012-07-04 09:18:02 +02004789 /* XHCI controllers don't stop the ep queue on short packets :| */
4790 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004791
Mathias Nymanb50107b2015-10-01 18:40:38 +03004792 xhci = hcd_to_xhci(hcd);
4793
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004794 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004795 xhci->main_hcd = hcd;
4796 /* Mark the first roothub as being USB 2.0.
4797 * The xHCI driver will register the USB 3.0 roothub.
4798 */
4799 hcd->speed = HCD_USB2;
4800 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4801 /*
4802 * USB 2.0 roothub under xHCI has an integrated TT,
4803 * (rate matching hub) as opposed to having an OHCI/UHCI
4804 * companion controller.
4805 */
4806 hcd->has_tt = 1;
4807 } else {
Mathias Nymanea7d0d62017-10-06 17:45:27 +03004808 /* Some 3.1 hosts return sbrn 0x30, can't rely on sbrn alone */
4809 if (xhci->sbrn == 0x31 || xhci->usb3_rhub.min_rev >= 1) {
Mathias Nymanb50107b2015-10-01 18:40:38 +03004810 xhci_info(xhci, "Host supports USB 3.1 Enhanced SuperSpeed\n");
4811 hcd->speed = HCD_USB31;
Mathias Nyman2c0e06f2016-01-25 15:30:45 +02004812 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
Mathias Nymanb50107b2015-10-01 18:40:38 +03004813 }
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004814 /* xHCI private pointer was set in xhci_pci_probe for the second
4815 * registered roothub.
4816 */
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004817 return 0;
4818 }
4819
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004820 mutex_init(&xhci->mutex);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004821 xhci->cap_regs = hcd->regs;
4822 xhci->op_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004823 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004824 xhci->run_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004825 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004826 /* Cache read-only capability registers */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004827 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
4828 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
4829 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
4830 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004831 xhci->hci_version = HC_VERSION(xhci->hcc_params);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004832 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
Lu Baolu04abb6d2015-10-01 18:40:31 +03004833 if (xhci->hci_version > 0x100)
4834 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004835 xhci_print_registers(xhci);
4836
Mathias Nyman757de492016-06-01 18:09:10 +03004837 xhci->quirks |= quirks;
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +01004838
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004839 get_quirks(dev, xhci);
4840
George Cherian07f3cb72013-07-01 10:59:12 +05304841 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4842 * success event after a short transfer. This quirk will ignore such
4843 * spurious event.
4844 */
4845 if (xhci->hci_version > 0x96)
4846 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4847
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004848 /* Make sure the HC is halted. */
4849 retval = xhci_halt(xhci);
4850 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004851 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004852
4853 xhci_dbg(xhci, "Resetting HCD\n");
4854 /* Reset the internal HC memory state and registers. */
4855 retval = xhci_reset(xhci);
4856 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004857 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004858 xhci_dbg(xhci, "Reset complete\n");
4859
Yoshihiro Shimoda0a380be2016-04-08 16:25:07 +03004860 /*
4861 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
4862 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
4863 * address memory pointers actually. So, this driver clears the AC64
4864 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
4865 * DMA_BIT_MASK(32)) in this xhci_gen_setup().
4866 */
4867 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
4868 xhci->hcc_params &= ~BIT(0);
4869
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004870 /* Set dma_mask and coherent_dma_mask to 64-bits,
4871 * if xHC supports 64-bit addressing */
4872 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4873 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004874 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004875 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
Duc Dangfda182d2015-10-09 13:30:13 +03004876 } else {
4877 /*
4878 * This is to avoid error in cases where a 32-bit USB
4879 * controller is used on a 64-bit capable system.
4880 */
4881 retval = dma_set_mask(dev, DMA_BIT_MASK(32));
4882 if (retval)
4883 return retval;
4884 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
4885 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004886 }
4887
4888 xhci_dbg(xhci, "Calling HCD init\n");
4889 /* Initialize HCD and host controller data structures. */
4890 retval = xhci_init(hcd);
4891 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004892 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004893 xhci_dbg(xhci, "Called HCD init\n");
Hans de Goede99705092015-01-16 17:54:01 +02004894
4895 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%08x\n",
4896 xhci->hcc_params, xhci->hci_version, xhci->quirks);
4897
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004898 return 0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004899}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03004900EXPORT_SYMBOL_GPL(xhci_gen_setup);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004901
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004902static const struct hc_driver xhci_hc_driver = {
4903 .description = "xhci-hcd",
4904 .product_desc = "xHCI Host Controller",
Yoshihiro Shimoda32479d42015-11-24 13:09:47 +02004905 .hcd_priv_size = sizeof(struct xhci_hcd),
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004906
4907 /*
4908 * generic hardware linkage
4909 */
4910 .irq = xhci_irq,
4911 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
4912
4913 /*
4914 * basic lifecycle operations
4915 */
4916 .reset = NULL, /* set in xhci_init_driver() */
4917 .start = xhci_run,
4918 .stop = xhci_stop,
4919 .shutdown = xhci_shutdown,
4920
4921 /*
4922 * managing i/o requests and associated device resources
4923 */
4924 .urb_enqueue = xhci_urb_enqueue,
4925 .urb_dequeue = xhci_urb_dequeue,
4926 .alloc_dev = xhci_alloc_dev,
4927 .free_dev = xhci_free_dev,
4928 .alloc_streams = xhci_alloc_streams,
4929 .free_streams = xhci_free_streams,
4930 .add_endpoint = xhci_add_endpoint,
4931 .drop_endpoint = xhci_drop_endpoint,
4932 .endpoint_reset = xhci_endpoint_reset,
4933 .check_bandwidth = xhci_check_bandwidth,
4934 .reset_bandwidth = xhci_reset_bandwidth,
4935 .address_device = xhci_address_device,
4936 .enable_device = xhci_enable_device,
4937 .update_hub_device = xhci_update_hub_device,
4938 .reset_device = xhci_discover_or_reset_device,
4939
4940 /*
4941 * scheduling support
4942 */
4943 .get_frame_number = xhci_get_frame,
4944
4945 /*
4946 * root hub support
4947 */
4948 .hub_control = xhci_hub_control,
4949 .hub_status_data = xhci_hub_status_data,
4950 .bus_suspend = xhci_bus_suspend,
4951 .bus_resume = xhci_bus_resume,
4952
4953 /*
4954 * call back when device connected and addressed
4955 */
4956 .update_device = xhci_update_device,
4957 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
4958 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
4959 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
4960 .find_raw_port_number = xhci_find_raw_port_number,
4961};
4962
Roger Quadroscd33a322015-05-29 17:01:46 +03004963void xhci_init_driver(struct hc_driver *drv,
4964 const struct xhci_driver_overrides *over)
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004965{
Roger Quadroscd33a322015-05-29 17:01:46 +03004966 BUG_ON(!over);
4967
4968 /* Copy the generic table to drv then apply the overrides */
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004969 *drv = xhci_hc_driver;
Roger Quadroscd33a322015-05-29 17:01:46 +03004970
4971 if (over) {
4972 drv->hcd_priv_size += over->extra_priv_size;
4973 if (over->reset)
4974 drv->reset = over->reset;
4975 if (over->start)
4976 drv->start = over->start;
4977 }
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004978}
4979EXPORT_SYMBOL_GPL(xhci_init_driver);
4980
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004981MODULE_DESCRIPTION(DRIVER_DESC);
4982MODULE_AUTHOR(DRIVER_AUTHOR);
4983MODULE_LICENSE("GPL");
4984
4985static int __init xhci_hcd_init(void)
4986{
Sarah Sharp98441972009-05-14 11:44:18 -07004987 /*
4988 * Check the compiler generated sizes of structures that must be laid
4989 * out in specific ways for hardware access.
4990 */
4991 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4992 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4993 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4994 /* xhci_device_control has eight fields, and also
4995 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4996 */
Sarah Sharp98441972009-05-14 11:44:18 -07004997 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4998 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4999 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
Lu Baolu04abb6d2015-10-01 18:40:31 +03005000 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
Sarah Sharp98441972009-05-14 11:44:18 -07005001 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
5002 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5003 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Oliver Neukum1eaf35e2015-12-03 15:03:34 +01005004
5005 if (usb_disabled())
5006 return -ENODEV;
5007
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005008 return 0;
5009}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005010
5011/*
5012 * If an init function is provided, an exit function must also be provided
5013 * to allow module unload.
5014 */
5015static void __exit xhci_hcd_fini(void) { }
5016
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005017module_init(xhci_hcd_init);
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005018module_exit(xhci_hcd_fini);