blob: 02b5ea790a905b2f5d62b812a379ec1abbe8e169 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002/*
3 * xHCI host controller driver
4 *
5 * Copyright (C) 2008 Intel Corp.
6 *
7 * Author: Sarah Sharp
8 * Some code borrowed from the Linux EHCI driver.
Sarah Sharp66d4ead2009-04-27 19:52:28 -07009 */
10
Dong Nguyen43b86af2010-07-21 16:56:08 -070011#include <linux/pci.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070012#include <linux/irq.h>
Sarah Sharp8df75f42010-04-02 15:34:16 -070013#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070014#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070015#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Alexis R. Cortes71c731a2012-08-03 14:00:27 -050017#include <linux/dmi.h>
James Hogan008eb952013-07-26 13:34:43 +010018#include <linux/dma-mapping.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070019
20#include "xhci.h"
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +030021#include "xhci-trace.h"
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020022#include "xhci-mtk.h"
Lu Baolu02b6fdc2017-10-05 11:21:39 +030023#include "xhci-debugfs.h"
Lu Baoludfba2172017-12-08 17:59:10 +020024#include "xhci-dbgcap.h"
Sarah Sharp66d4ead2009-04-27 19:52:28 -070025
26#define DRIVER_AUTHOR "Sarah Sharp"
27#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
28
Lu Baolua1377e52014-11-18 11:27:14 +020029#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
30
Sarah Sharpb0567b32009-08-07 14:04:36 -070031/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
32static int link_quirk;
33module_param(link_quirk, int, S_IRUGO | S_IWUSR);
34MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
35
Marc Zyngier36b68572018-05-23 18:41:36 +010036static unsigned long long quirks;
37module_param(quirks, ullong, S_IRUGO);
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +010038MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");
39
Sarah Sharp66d4ead2009-04-27 19:52:28 -070040/* TODO: copied from ehci-hcd.c - can this be refactored? */
41/*
Sarah Sharp2611bd182012-10-25 13:27:51 -070042 * xhci_handshake - spin reading hc until handshake completes or fails
Sarah Sharp66d4ead2009-04-27 19:52:28 -070043 * @ptr: address of hc register to be read
44 * @mask: bits to look at in result of read
45 * @done: value of those bits when handshake succeeds
46 * @usec: timeout in microseconds
47 *
48 * Returns negative errno, or zero on success
49 *
50 * Success happens when the "mask" bits have the specified value (hardware
51 * handshake done). There are two failure modes: "usec" have passed (major
52 * hardware flakeout), or the register reads as all-ones (hardware removed).
53 */
Lin Wangdc0b1772015-01-09 16:06:28 +020054int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, int usec)
Sarah Sharp66d4ead2009-04-27 19:52:28 -070055{
56 u32 result;
57
58 do {
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020059 result = readl(ptr);
Sarah Sharp66d4ead2009-04-27 19:52:28 -070060 if (result == ~(u32)0) /* card removed */
61 return -ENODEV;
62 result &= mask;
63 if (result == done)
64 return 0;
65 udelay(1);
66 usec--;
67 } while (usec > 0);
68 return -ETIMEDOUT;
69}
70
71/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070072 * Disable interrupts and begin the xHCI halting process.
73 */
74void xhci_quiesce(struct xhci_hcd *xhci)
75{
76 u32 halted;
77 u32 cmd;
78 u32 mask;
79
80 mask = ~(XHCI_IRQS);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020081 halted = readl(&xhci->op_regs->status) & STS_HALT;
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070082 if (!halted)
83 mask &= ~CMD_RUN;
84
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020085 cmd = readl(&xhci->op_regs->command);
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070086 cmd &= mask;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +020087 writel(cmd, &xhci->op_regs->command);
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070088}
89
90/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070091 * Force HC into halt state.
92 *
93 * Disable any IRQs and clear the run/stop bit.
94 * HC will complete any current and actively pipelined transactions, and
Andiry Xubdfca502011-01-06 15:43:39 +080095 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070096 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070097 */
98int xhci_halt(struct xhci_hcd *xhci)
99{
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800100 int ret;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300101 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700102 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700103
Lin Wangdc0b1772015-01-09 16:06:28 +0200104 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
Mathias Nyman99154fd2016-11-11 15:13:11 +0200106 if (ret) {
107 xhci_warn(xhci, "Host halt failed, %d\n", ret);
108 return ret;
109 }
110 xhci->xhc_state |= XHCI_STATE_HALTED;
111 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800112 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700113}
114
115/*
Sarah Sharped074532010-05-24 13:25:21 -0700116 * Set the run bit and wait for the host to be running.
117 */
Guoqing Zhang26bba5c2017-04-07 17:56:53 +0300118int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700119{
120 u32 temp;
121 int ret;
122
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200123 temp = readl(&xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700124 temp |= (CMD_RUN);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300125 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
Sarah Sharped074532010-05-24 13:25:21 -0700126 temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200127 writel(temp, &xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700128
129 /*
130 * Wait for the HCHalted Status bit to be 0 to indicate the host is
131 * running.
132 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200133 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharped074532010-05-24 13:25:21 -0700134 STS_HALT, 0, XHCI_MAX_HALT_USEC);
135 if (ret == -ETIMEDOUT)
136 xhci_err(xhci, "Host took too long to start, "
137 "waited %u microseconds.\n",
138 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800139 if (!ret)
Mathias Nyman98d74f92016-04-08 16:25:10 +0300140 /* clear state flags. Including dying, halted or removing */
141 xhci->xhc_state = 0;
Roger Quadrose5bfeab2015-09-21 17:46:13 +0300142
Sarah Sharped074532010-05-24 13:25:21 -0700143 return ret;
144}
145
146/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800147 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700148 *
149 * This resets pipelines, timers, counters, state machines, etc.
150 * Transactions will be terminated immediately, and operational registers
151 * will be set to their defaults.
152 */
153int xhci_reset(struct xhci_hcd *xhci)
154{
155 u32 command;
156 u32 state;
Andiry Xuf370b992012-04-14 02:54:30 +0800157 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700158
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200159 state = readl(&xhci->op_regs->status);
Mathias Nymanc11ae032016-11-11 15:13:12 +0200160
161 if (state == ~(u32)0) {
162 xhci_warn(xhci, "Host not accessible, reset failed.\n");
163 return -ENODEV;
164 }
165
Sarah Sharpd3512f62009-07-27 12:03:50 -0700166 if ((state & STS_HALT) == 0) {
167 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
168 return 0;
169 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700170
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300171 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200172 command = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700173 command |= CMD_RESET;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200174 writel(command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700175
Rajmohan Mania5964392015-11-18 10:48:20 +0200176 /* Existing Intel xHCI controllers require a delay of 1 mS,
177 * after setting the CMD_RESET bit, and before accessing any
178 * HC registers. This allows the HC to complete the
179 * reset operation and be ready for HC register access.
180 * Without this delay, the subsequent HC register access,
181 * may result in a system hang very rarely.
182 */
183 if (xhci->quirks & XHCI_INTEL_HOST)
184 udelay(1000);
185
Lin Wangdc0b1772015-01-09 16:06:28 +0200186 ret = xhci_handshake(&xhci->op_regs->command,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700187 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700188 if (ret)
189 return ret;
190
Jiahau Chang9da5a102017-07-20 14:48:27 +0300191 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
192 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));
193
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300194 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
195 "Wait for controller to be ready for doorbell rings");
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700196 /*
197 * xHCI cannot write to any doorbells or operational registers other
198 * than status until the "Controller Not Ready" flag is cleared.
199 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200200 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700201 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xuf370b992012-04-14 02:54:30 +0800202
Felipe Balbi98871e92017-01-23 14:20:04 +0200203 for (i = 0; i < 2; i++) {
Andiry Xuf370b992012-04-14 02:54:30 +0800204 xhci->bus_state[i].port_c_suspend = 0;
205 xhci->bus_state[i].suspended_ports = 0;
206 xhci->bus_state[i].resuming_ports = 0;
207 }
208
209 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700210}
211
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300212
yuan linyu2c93e792017-02-25 19:20:55 +0800213#ifdef CONFIG_USB_PCI
Dong Nguyen43b86af2010-07-21 16:56:08 -0700214/*
215 * Set up MSI
216 */
217static int xhci_setup_msi(struct xhci_hcd *xhci)
218{
219 int ret;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800220 /*
221 * TODO:Check with MSI Soc for sysdev
222 */
Dong Nguyen43b86af2010-07-21 16:56:08 -0700223 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
224
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300225 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
226 if (ret < 0) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300227 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
228 "failed to allocate MSI entry");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700229 return ret;
230 }
231
Alex Shi851ec162013-05-24 10:54:19 +0800232 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700233 0, "xhci_hcd", xhci_to_hcd(xhci));
234 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300235 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
236 "disable MSI interrupt");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300237 pci_free_irq_vectors(pdev);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700238 }
239
240 return ret;
241}
242
243/*
244 * Set up MSI-X
245 */
246static int xhci_setup_msix(struct xhci_hcd *xhci)
247{
248 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800249 struct usb_hcd *hcd = xhci_to_hcd(xhci);
250 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700251
252 /*
253 * calculate number of msi-x vectors supported.
254 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
255 * with max number of interrupters based on the xhci HCSPARAMS1.
256 * - num_online_cpus: maximum msi-x vectors per CPUs core.
257 * Add additional 1 vector to ensure always available interrupt.
258 */
259 xhci->msix_count = min(num_online_cpus() + 1,
260 HCS_MAX_INTRS(xhci->hcs_params1));
261
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300262 ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count,
263 PCI_IRQ_MSIX);
264 if (ret < 0) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300265 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
266 "Failed to enable MSI-X");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300267 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700268 }
269
Dong Nguyen43b86af2010-07-21 16:56:08 -0700270 for (i = 0; i < xhci->msix_count; i++) {
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300271 ret = request_irq(pci_irq_vector(pdev, i), xhci_msi_irq, 0,
272 "xhci_hcd", xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700273 if (ret)
274 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700275 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700276
Andiry Xu00292272010-12-27 17:39:02 +0800277 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700278 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700279
280disable_msix:
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300281 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300282 while (--i >= 0)
283 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
284 pci_free_irq_vectors(pdev);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700285 return ret;
286}
287
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700288/* Free any IRQs and disable MSI-X */
289static void xhci_cleanup_msix(struct xhci_hcd *xhci)
290{
Andiry Xu00292272010-12-27 17:39:02 +0800291 struct usb_hcd *hcd = xhci_to_hcd(xhci);
292 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700293
Jack Pham90053552013-11-15 14:53:14 -0800294 if (xhci->quirks & XHCI_PLAT)
295 return;
296
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300297 /* return if using legacy interrupt */
298 if (hcd->irq > 0)
299 return;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700300
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300301 if (hcd->msix_enabled) {
302 int i;
303
304 for (i = 0; i < xhci->msix_count; i++)
305 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700306 } else {
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300307 free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700308 }
309
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300310 pci_free_irq_vectors(pdev);
Andiry Xu00292272010-12-27 17:39:02 +0800311 hcd->msix_enabled = 0;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700312}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700313
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700314static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700315{
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300316 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700317
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300318 if (hcd->msix_enabled) {
319 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
320 int i;
321
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700322 for (i = 0; i < xhci->msix_count; i++)
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300323 synchronize_irq(pci_irq_vector(pdev, i));
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700324 }
325}
326
327static int xhci_try_enable_msi(struct usb_hcd *hcd)
328{
329 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700330 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700331 int ret;
332
Sarah Sharp52fb6122013-08-08 10:08:34 -0700333 /* The xhci platform device has set up IRQs through usb_add_hcd. */
334 if (xhci->quirks & XHCI_PLAT)
335 return 0;
336
337 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700338 /*
339 * Some Fresco Logic host controllers advertise MSI, but fail to
340 * generate interrupts. Don't even try to enable MSI.
341 */
342 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100343 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700344
345 /* unregister the legacy interrupt */
346 if (hcd->irq)
347 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200348 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700349
350 ret = xhci_setup_msix(xhci);
351 if (ret)
352 /* fall back to msi*/
353 ret = xhci_setup_msi(xhci);
354
Peter Chen6a29bee2017-05-17 18:32:02 +0300355 if (!ret) {
356 hcd->msi_enabled = 1;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700357 return 0;
Peter Chen6a29bee2017-05-17 18:32:02 +0300358 }
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700359
Sarah Sharp68d07f62012-02-13 16:25:57 -0800360 if (!pdev->irq) {
361 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
362 return -EINVAL;
363 }
364
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100365 legacy_irq:
Adrian Huang79699432014-02-27 11:26:03 +0000366 if (!strlen(hcd->irq_descr))
367 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
368 hcd->driver->description, hcd->self.busnum);
369
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700370 /* fall back to legacy interrupt*/
371 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
372 hcd->irq_descr, hcd);
373 if (ret) {
374 xhci_err(xhci, "request interrupt %d failed\n",
375 pdev->irq);
376 return ret;
377 }
378 hcd->irq = pdev->irq;
379 return 0;
380}
381
382#else
383
David Cohen01bb59e2014-04-25 19:20:16 +0300384static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700385{
386 return 0;
387}
388
David Cohen01bb59e2014-04-25 19:20:16 +0300389static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700390{
391}
392
David Cohen01bb59e2014-04-25 19:20:16 +0300393static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700394{
395}
396
397#endif
398
Kees Cooke99e88a2017-10-16 14:43:17 -0700399static void compliance_mode_recovery(struct timer_list *t)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500400{
401 struct xhci_hcd *xhci;
402 struct usb_hcd *hcd;
Mathias Nyman38986ff2018-05-21 16:40:01 +0300403 struct xhci_hub *rhub;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500404 u32 temp;
405 int i;
406
Kees Cooke99e88a2017-10-16 14:43:17 -0700407 xhci = from_timer(xhci, t, comp_mode_recovery_timer);
Mathias Nyman38986ff2018-05-21 16:40:01 +0300408 rhub = &xhci->usb3_rhub;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500409
Mathias Nyman38986ff2018-05-21 16:40:01 +0300410 for (i = 0; i < rhub->num_ports; i++) {
411 temp = readl(rhub->ports[i]->addr);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500412 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
413 /*
414 * Compliance Mode Detected. Letting USB Core
415 * handle the Warm Reset
416 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300417 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
418 "Compliance mode detected->port %d",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500419 i + 1);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300420 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
421 "Attempting compliance mode recovery");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500422 hcd = xhci->shared_hcd;
423
424 if (hcd->state == HC_STATE_SUSPENDED)
425 usb_hcd_resume_root_hub(hcd);
426
427 usb_hcd_poll_rh_status(hcd);
428 }
429 }
430
Mathias Nyman38986ff2018-05-21 16:40:01 +0300431 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500432 mod_timer(&xhci->comp_mode_recovery_timer,
433 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
434}
435
436/*
437 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
438 * that causes ports behind that hardware to enter compliance mode sometimes.
439 * The quirk creates a timer that polls every 2 seconds the link state of
440 * each host controller's port and recovers it by issuing a Warm reset
441 * if Compliance mode is detected, otherwise the port will become "dead" (no
442 * device connections or disconnections will be detected anymore). Becasue no
443 * status event is generated when entering compliance mode (per xhci spec),
444 * this quirk is needed on systems that have the failing hardware installed.
445 */
446static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
447{
448 xhci->port_status_u0 = 0;
Kees Cooke99e88a2017-10-16 14:43:17 -0700449 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery,
450 0);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500451 xhci->comp_mode_recovery_timer.expires = jiffies +
452 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
453
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500454 add_timer(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300455 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
456 "Compliance mode recovery timer initialized");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500457}
458
459/*
460 * This function identifies the systems that have installed the SN65LVPE502CP
461 * USB3.0 re-driver and that need the Compliance Mode Quirk.
462 * Systems:
463 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
464 */
Andrew Brestickere1cd9722014-10-03 11:35:27 +0300465static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500466{
467 const char *dmi_product_name, *dmi_sys_vendor;
468
469 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
470 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530471 if (!dmi_product_name || !dmi_sys_vendor)
472 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500473
474 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
475 return false;
476
477 if (strstr(dmi_product_name, "Z420") ||
478 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500479 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600480 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500481 return true;
482
483 return false;
484}
485
486static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
487{
Mathias Nyman38986ff2018-05-21 16:40:01 +0300488 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1));
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500489}
490
491
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700492/*
493 * Initialize memory for HCD and xHC (one-time init).
494 *
495 * Program the PAGESIZE register, initialize the device context array, create
496 * device contexts (?), set up a command ring segment (or two?), create event
497 * ring (one for now).
498 */
Lu Baolu39693842017-04-07 17:57:04 +0300499static int xhci_init(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700500{
501 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
502 int retval = 0;
503
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300504 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700505 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700506 if (xhci->hci_version == 0x95 && link_quirk) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300507 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
508 "QUIRK: Not clearing Link TRB chain bits.");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700509 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
510 } else {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300511 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
512 "xHCI doesn't need link TRB QUIRK");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700513 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700514 retval = xhci_mem_init(xhci, GFP_KERNEL);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300515 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700516
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500517 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700518 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500519 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
520 compliance_mode_recovery_timer_init(xhci);
521 }
522
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700523 return retval;
524}
525
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700526/*-------------------------------------------------------------------------*/
527
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700528
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800529static int xhci_run_finished(struct xhci_hcd *xhci)
530{
531 if (xhci_start(xhci)) {
532 xhci_halt(xhci);
533 return -ENODEV;
534 }
535 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800536 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800537
538 if (xhci->quirks & XHCI_NEC_HOST)
539 xhci_ring_cmd_db(xhci);
540
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300541 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
542 "Finished xhci_run for USB3 roothub");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800543 return 0;
544}
545
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700546/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700547 * Start the HC after it was halted.
548 *
549 * This function is called by the USB core when the HC driver is added.
550 * Its opposite is xhci_stop().
551 *
552 * xhci_init() must be called once before this function can be called.
553 * Reset the HC, enable device slot contexts, program DCBAAP, and
554 * set command ring pointer and event ring pointer.
555 *
556 * Setup MSI-X vectors and enable interrupts.
557 */
558int xhci_run(struct usb_hcd *hcd)
559{
560 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700561 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700562 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700563 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700564
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800565 /* Start the xHCI host controller running only after the USB 2.0 roothub
566 * is setup.
567 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700568
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700569 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800570 if (!usb_hcd_is_primary_hcd(hcd))
571 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700572
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300573 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700574
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700575 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700576 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700577 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700578
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800579 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700580 temp_64 &= ~ERST_PTR_MASK;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300581 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
582 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700583
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300584 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
585 "// Set the interrupt modulation register");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200586 temp = readl(&xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700587 temp &= ~ER_IRQ_INTERVAL_MASK;
Adam Wallisab725cb2017-12-08 17:59:13 +0200588 temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200589 writel(temp, &xhci->ir_set->irq_control);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700590
591 /* Set the HCD state before we enable the irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200592 temp = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700593 temp |= (CMD_EIE);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300594 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
595 "// Enable interrupts, cmd = 0x%x.", temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200596 writel(temp, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700597
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200598 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300599 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
600 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700601 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200602 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700603
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300604 if (xhci->quirks & XHCI_NEC_HOST) {
605 struct xhci_command *command;
Lu Baolu74e0b562017-04-07 17:57:05 +0300606
Mathias Nyman103afda2017-12-08 17:59:08 +0200607 command = xhci_alloc_command(xhci, false, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300608 if (!command)
609 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +0300610
Shu Wangd6f5f072017-07-20 14:48:31 +0300611 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0,
Sarah Sharp02386342010-05-24 13:25:28 -0700612 TRB_TYPE(TRB_NEC_GET_FW));
Shu Wangd6f5f072017-07-20 14:48:31 +0300613 if (ret)
614 xhci_free_command(xhci, command);
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300615 }
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300616 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
617 "Finished xhci_run for USB2 roothub");
Lu Baolu02b6fdc2017-10-05 11:21:39 +0300618
Lu Baoludfba2172017-12-08 17:59:10 +0200619 xhci_dbc_init(xhci);
620
Lu Baolu02b6fdc2017-10-05 11:21:39 +0300621 xhci_debugfs_init(xhci);
622
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700623 return 0;
624}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300625EXPORT_SYMBOL_GPL(xhci_run);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700626
627/*
628 * Stop xHCI driver.
629 *
630 * This function is called by the USB core when the HC driver is removed.
631 * Its opposite is xhci_run().
632 *
633 * Disable device contexts, disable IRQs, and quiesce the HC.
634 * Reset the HC, finish any completed transactions, and cleanup memory.
635 */
Lu Baolu39693842017-04-07 17:57:04 +0300636static void xhci_stop(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700637{
638 u32 temp;
639 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
640
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300641 mutex_lock(&xhci->mutex);
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300642
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300643 /* Only halt host and free memory after both hcds are removed */
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300644 if (!usb_hcd_is_primary_hcd(hcd)) {
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300645 /* usb core will free this hcd shortly, unset pointer */
646 xhci->shared_hcd = NULL;
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300647 mutex_unlock(&xhci->mutex);
648 return;
649 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700650
Lu Baoludfba2172017-12-08 17:59:10 +0200651 xhci_dbc_exit(xhci);
652
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300653 spin_lock_irq(&xhci->lock);
654 xhci->xhc_state |= XHCI_STATE_HALTED;
655 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
656 xhci_halt(xhci);
657 xhci_reset(xhci);
658 spin_unlock_irq(&xhci->lock);
659
Zhang Rui40a9fb12010-12-17 13:17:04 -0800660 xhci_cleanup_msix(xhci);
661
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500662 /* Deleting Compliance Mode Recovery Timer */
663 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400664 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500665 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300666 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
667 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400668 __func__);
669 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500670
Andiry Xuc41136b2011-03-22 17:08:14 +0800671 if (xhci->quirks & XHCI_AMD_PLL_FIX)
672 usb_amd_dev_put();
673
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300674 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
675 "// Disabling event ring interrupts");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200676 temp = readl(&xhci->op_regs->status);
Lu Baolud1001ab2017-04-07 17:56:50 +0300677 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200678 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200679 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700680
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300681 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700682 xhci_mem_cleanup(xhci);
Zhengjun Xing11cd7642018-02-12 14:24:51 +0200683 xhci_debugfs_exit(xhci);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300684 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
685 "xhci_stop completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200686 readl(&xhci->op_regs->status));
Roger Quadros85ac90f2015-09-21 17:46:12 +0300687 mutex_unlock(&xhci->mutex);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700688}
689
690/*
691 * Shutdown HC (not bus-specific)
692 *
693 * This is called when the machine is rebooting or halting. We assume that the
694 * machine will be powered off, and the HC's internal state will be reset.
695 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800696 *
697 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700698 */
Lu Baolu39693842017-04-07 17:57:04 +0300699static void xhci_shutdown(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700700{
701 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
702
Dan Carpenter052c7f92012-08-13 19:57:03 +0300703 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800704 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
Sarah Sharpe95829f2012-07-23 18:59:30 +0300705
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700706 spin_lock_irq(&xhci->lock);
707 xhci_halt(xhci);
Takashi Iwai638298d2013-09-12 08:11:06 +0200708 /* Workaround for spurious wakeups at shutdown with HSW */
709 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
710 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700711 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700712
Zhang Rui40a9fb12010-12-17 13:17:04 -0800713 xhci_cleanup_msix(xhci);
714
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300715 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
716 "xhci_shutdown completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200717 readl(&xhci->op_regs->status));
Takashi Iwai638298d2013-09-12 08:11:06 +0200718
719 /* Yet another workaround for spurious wakeups at shutdown with HSW */
720 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800721 pci_set_power_state(to_pci_dev(hcd->self.sysdev), PCI_D3hot);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700722}
723
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700724#ifdef CONFIG_PM
Andiry Xu5535b1d52010-10-14 07:23:06 -0700725static void xhci_save_registers(struct xhci_hcd *xhci)
726{
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200727 xhci->s3.command = readl(&xhci->op_regs->command);
728 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800729 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200730 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
731 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800732 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
733 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200734 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
735 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700736}
737
738static void xhci_restore_registers(struct xhci_hcd *xhci)
739{
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200740 writel(xhci->s3.command, &xhci->op_regs->command);
741 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
Sarah Sharp477632d2014-01-29 14:02:00 -0800742 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200743 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
744 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
Sarah Sharp477632d2014-01-29 14:02:00 -0800745 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
746 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200747 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
748 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700749}
750
Sarah Sharp89821322010-11-12 11:59:31 -0800751static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
752{
753 u64 val_64;
754
755 /* step 2: initialize command ring buffer */
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800756 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800757 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
758 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
759 xhci->cmd_ring->dequeue) &
760 (u64) ~CMD_RING_RSVD_BITS) |
761 xhci->cmd_ring->cycle_state;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300762 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
763 "// Setting command ring address to 0x%llx",
Sarah Sharp89821322010-11-12 11:59:31 -0800764 (long unsigned long) val_64);
Sarah Sharp477632d2014-01-29 14:02:00 -0800765 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800766}
767
768/*
769 * The whole command ring must be cleared to zero when we suspend the host.
770 *
771 * The host doesn't save the command ring pointer in the suspend well, so we
772 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
773 * aligned, because of the reserved bits in the command ring dequeue pointer
774 * register. Therefore, we can't just set the dequeue pointer back in the
775 * middle of the ring (TRBs are 16-byte aligned).
776 */
777static void xhci_clear_command_ring(struct xhci_hcd *xhci)
778{
779 struct xhci_ring *ring;
780 struct xhci_segment *seg;
781
782 ring = xhci->cmd_ring;
783 seg = ring->deq_seg;
784 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800785 memset(seg->trbs, 0,
786 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
787 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
788 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800789 seg = seg->next;
790 } while (seg != ring->deq_seg);
791
792 /* Reset the software enqueue and dequeue pointers */
793 ring->deq_seg = ring->first_seg;
794 ring->dequeue = ring->first_seg->trbs;
795 ring->enq_seg = ring->deq_seg;
796 ring->enqueue = ring->dequeue;
797
Andiry Xub008df62012-03-05 17:49:34 +0800798 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800799 /*
800 * Ring is now zeroed, so the HW should look for change of ownership
801 * when the cycle bit is set to 1.
802 */
803 ring->cycle_state = 1;
804
805 /*
806 * Reset the hardware dequeue pointer.
807 * Yes, this will need to be re-written after resume, but we're paranoid
808 * and want to make sure the hardware doesn't access bogus memory
809 * because, say, the BIOS or an SMI started the host without changing
810 * the command ring pointers.
811 */
812 xhci_set_cmd_ring_deq(xhci);
813}
814
Lu Baolua1377e52014-11-18 11:27:14 +0200815static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
816{
Mathias Nyman38986ff2018-05-21 16:40:01 +0300817 struct xhci_port **ports;
Lu Baolua1377e52014-11-18 11:27:14 +0200818 int port_index;
Lu Baolua1377e52014-11-18 11:27:14 +0200819 unsigned long flags;
820 u32 t1, t2;
821
822 spin_lock_irqsave(&xhci->lock, flags);
823
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800824 /* disable usb3 ports Wake bits */
Mathias Nyman38986ff2018-05-21 16:40:01 +0300825 port_index = xhci->usb3_rhub.num_ports;
826 ports = xhci->usb3_rhub.ports;
Lu Baolua1377e52014-11-18 11:27:14 +0200827 while (port_index--) {
Mathias Nyman38986ff2018-05-21 16:40:01 +0300828 t1 = readl(ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200829 t1 = xhci_port_state_to_neutral(t1);
830 t2 = t1 & ~PORT_WAKE_BITS;
831 if (t1 != t2)
Mathias Nyman38986ff2018-05-21 16:40:01 +0300832 writel(t2, ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200833 }
834
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800835 /* disable usb2 ports Wake bits */
Mathias Nyman38986ff2018-05-21 16:40:01 +0300836 port_index = xhci->usb2_rhub.num_ports;
837 ports = xhci->usb2_rhub.ports;
Lu Baolua1377e52014-11-18 11:27:14 +0200838 while (port_index--) {
Mathias Nyman38986ff2018-05-21 16:40:01 +0300839 t1 = readl(ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200840 t1 = xhci_port_state_to_neutral(t1);
841 t2 = t1 & ~PORT_WAKE_BITS;
842 if (t1 != t2)
Mathias Nyman38986ff2018-05-21 16:40:01 +0300843 writel(t2, ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200844 }
845
846 spin_unlock_irqrestore(&xhci->lock, flags);
847}
848
Andiry Xu5535b1d52010-10-14 07:23:06 -0700849/*
850 * Stop HC (not bus-specific)
851 *
852 * This is called when the machine transition into S3/S4 mode.
853 *
854 */
Lu Baolua1377e52014-11-18 11:27:14 +0200855int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
Andiry Xu5535b1d52010-10-14 07:23:06 -0700856{
857 int rc = 0;
Oliver Neukum455f5892013-09-30 15:50:54 +0200858 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700859 struct usb_hcd *hcd = xhci_to_hcd(xhci);
860 u32 command;
861
Roger Quadros9fa733f2015-05-29 17:01:50 +0300862 if (!hcd->state)
863 return 0;
864
Felipe Balbi77b84762012-10-19 10:55:16 +0300865 if (hcd->state != HC_STATE_SUSPENDED ||
866 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
867 return -EINVAL;
868
Lu Baoludfba2172017-12-08 17:59:10 +0200869 xhci_dbc_suspend(xhci);
870
Lu Baolua1377e52014-11-18 11:27:14 +0200871 /* Clear root port wake on bits if wakeup not allowed. */
872 if (!do_wakeup)
873 xhci_disable_port_wake_on_bits(xhci);
874
Sarah Sharpc52804a2012-11-27 12:30:23 -0800875 /* Don't poll the roothubs on bus suspend. */
876 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
877 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
878 del_timer_sync(&hcd->rh_timer);
Al Cooper14e61a12014-08-20 16:41:57 +0300879 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
880 del_timer_sync(&xhci->shared_hcd->rh_timer);
Sarah Sharpc52804a2012-11-27 12:30:23 -0800881
Kai-Heng Feng191edc52018-03-08 17:17:17 +0200882 if (xhci->quirks & XHCI_SUSPEND_DELAY)
883 usleep_range(1000, 1500);
884
Andiry Xu5535b1d52010-10-14 07:23:06 -0700885 spin_lock_irq(&xhci->lock);
886 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb32093792011-03-07 11:24:07 -0800887 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700888 /* step 1: stop endpoint */
889 /* skipped assuming that port suspend has done */
890
891 /* step 2: clear Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200892 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700893 command &= ~CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200894 writel(command, &xhci->op_regs->command);
Oliver Neukum455f5892013-09-30 15:50:54 +0200895
896 /* Some chips from Fresco Logic need an extraordinary delay */
897 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
898
Lin Wangdc0b1772015-01-09 16:06:28 +0200899 if (xhci_handshake(&xhci->op_regs->status,
Oliver Neukum455f5892013-09-30 15:50:54 +0200900 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d52010-10-14 07:23:06 -0700901 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
902 spin_unlock_irq(&xhci->lock);
903 return -ETIMEDOUT;
904 }
Sarah Sharp89821322010-11-12 11:59:31 -0800905 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700906
907 /* step 3: save registers */
908 xhci_save_registers(xhci);
909
910 /* step 4: set CSS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200911 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700912 command |= CMD_CSS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200913 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +0200914 if (xhci_handshake(&xhci->op_regs->status,
Sarah Sharp2611bd182012-10-25 13:27:51 -0700915 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +0800916 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d52010-10-14 07:23:06 -0700917 spin_unlock_irq(&xhci->lock);
918 return -ETIMEDOUT;
919 }
Andiry Xu5535b1d52010-10-14 07:23:06 -0700920 spin_unlock_irq(&xhci->lock);
921
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500922 /*
923 * Deleting Compliance Mode Recovery Timer because the xHCI Host
924 * is about to be suspended.
925 */
926 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
927 (!(xhci_all_ports_seen_u0(xhci)))) {
928 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300929 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
930 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400931 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500932 }
933
Andiry Xu00292272010-12-27 17:39:02 +0800934 /* step 5: remove core well power */
935 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700936 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800937
Andiry Xu5535b1d52010-10-14 07:23:06 -0700938 return rc;
939}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300940EXPORT_SYMBOL_GPL(xhci_suspend);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700941
942/*
943 * start xHC (not bus-specific)
944 *
945 * This is called when the machine transition from S3/S4 mode.
946 *
947 */
948int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
949{
Wang, Yud6236f62014-06-24 17:14:44 +0300950 u32 command, temp = 0, status;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700951 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -0800952 struct usb_hcd *secondary_hcd;
Alan Sternf69e31202011-11-03 11:37:10 -0400953 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -0500954 bool comp_timer_running = false;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700955
Roger Quadros9fa733f2015-05-29 17:01:50 +0300956 if (!hcd->state)
957 return 0;
958
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800959 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300960 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800961 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800962 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
963 time_before(jiffies,
964 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d52010-10-14 07:23:06 -0700965 msleep(100);
966
Alan Sternf69e31202011-11-03 11:37:10 -0400967 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
968 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
969
Andiry Xu5535b1d52010-10-14 07:23:06 -0700970 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +0200971 if (xhci->quirks & XHCI_RESET_ON_RESUME)
972 hibernated = true;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700973
974 if (!hibernated) {
975 /* step 1: restore register */
976 xhci_restore_registers(xhci);
977 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800978 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700979 /* step 3: restore state and start state*/
980 /* step 3: set CRS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200981 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700982 command |= CMD_CRS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200983 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +0200984 if (xhci_handshake(&xhci->op_regs->status,
Andiry Xu622eb782012-06-13 10:51:57 +0800985 STS_RESTORE, 0, 10 * 1000)) {
986 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d52010-10-14 07:23:06 -0700987 spin_unlock_irq(&xhci->lock);
988 return -ETIMEDOUT;
989 }
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200990 temp = readl(&xhci->op_regs->status);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700991 }
992
993 /* If restore operation fails, re-initialize the HC during resume */
994 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -0500995
996 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
997 !(xhci_all_ports_seen_u0(xhci))) {
998 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300999 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1000 "Compliance Mode Recovery Timer deleted!");
Tony Camuso77df9e02013-02-21 16:11:27 -05001001 }
1002
Sarah Sharpfedd3832011-04-12 17:43:19 -07001003 /* Let the USB core know _both_ roothubs lost power. */
1004 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1005 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001006
1007 xhci_dbg(xhci, "Stop HCD\n");
1008 xhci_halt(xhci);
1009 xhci_reset(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001010 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001011 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001012
Andiry Xu5535b1d52010-10-14 07:23:06 -07001013 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001014 temp = readl(&xhci->op_regs->status);
Lu Baolud1001ab2017-04-07 17:56:50 +03001015 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001016 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001017 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001018
1019 xhci_dbg(xhci, "cleaning up memory\n");
1020 xhci_mem_cleanup(xhci);
Zhengjun Xingd91676712018-02-12 14:24:49 +02001021 xhci_debugfs_exit(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001022 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001023 readl(&xhci->op_regs->status));
Andiry Xu5535b1d52010-10-14 07:23:06 -07001024
Sarah Sharp65b22f92010-12-17 12:35:05 -08001025 /* USB core calls the PCI reinit and start functions twice:
1026 * first with the primary HCD, and then with the secondary HCD.
1027 * If we don't do the same, the host will never be started.
1028 */
1029 if (!usb_hcd_is_primary_hcd(hcd))
1030 secondary_hcd = hcd;
1031 else
1032 secondary_hcd = xhci->shared_hcd;
1033
1034 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1035 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001036 if (retval)
1037 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001038 comp_timer_running = true;
1039
Sarah Sharp65b22f92010-12-17 12:35:05 -08001040 xhci_dbg(xhci, "Start the primary HCD\n");
1041 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb32093792011-03-07 11:24:07 -08001042 if (!retval) {
Alan Sternf69e31202011-11-03 11:37:10 -04001043 xhci_dbg(xhci, "Start the secondary HCD\n");
1044 retval = xhci_run(secondary_hcd);
Sarah Sharpb32093792011-03-07 11:24:07 -08001045 }
Andiry Xu5535b1d52010-10-14 07:23:06 -07001046 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb32093792011-03-07 11:24:07 -08001047 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e31202011-11-03 11:37:10 -04001048 goto done;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001049 }
1050
Andiry Xu5535b1d52010-10-14 07:23:06 -07001051 /* step 4: set Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001052 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001053 command |= CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001054 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001055 xhci_handshake(&xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d52010-10-14 07:23:06 -07001056 0, 250 * 1000);
1057
1058 /* step 5: walk topology and initialize portsc,
1059 * portpmsc and portli
1060 */
1061 /* this is done in bus_resume */
1062
1063 /* step 6: restart each of the previously
1064 * Running endpoints by ringing their doorbells
1065 */
1066
Andiry Xu5535b1d52010-10-14 07:23:06 -07001067 spin_unlock_irq(&xhci->lock);
Alan Sternf69e31202011-11-03 11:37:10 -04001068
Lu Baoludfba2172017-12-08 17:59:10 +02001069 xhci_dbc_resume(xhci);
1070
Alan Sternf69e31202011-11-03 11:37:10 -04001071 done:
1072 if (retval == 0) {
Wang, Yud6236f62014-06-24 17:14:44 +03001073 /* Resume root hubs only when have pending events. */
1074 status = readl(&xhci->op_regs->status);
1075 if (status & STS_EINT) {
Wang, Yud6236f62014-06-24 17:14:44 +03001076 usb_hcd_resume_root_hub(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001077 usb_hcd_resume_root_hub(hcd);
Wang, Yud6236f62014-06-24 17:14:44 +03001078 }
Alan Sternf69e31202011-11-03 11:37:10 -04001079 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001080
1081 /*
1082 * If system is subject to the Quirk, Compliance Mode Timer needs to
1083 * be re-initialized Always after a system resume. Ports are subject
1084 * to suffer the Compliance Mode issue again. It doesn't matter if
1085 * ports have entered previously to U0 before system's suspension.
1086 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001087 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001088 compliance_mode_recovery_timer_init(xhci);
1089
Jiahau Chang9da5a102017-07-20 14:48:27 +03001090 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
1091 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
1092
Sarah Sharpc52804a2012-11-27 12:30:23 -08001093 /* Re-enable port polling. */
1094 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
Al Cooper14e61a12014-08-20 16:41:57 +03001095 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1096 usb_hcd_poll_rh_status(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001097 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1098 usb_hcd_poll_rh_status(hcd);
Sarah Sharpc52804a2012-11-27 12:30:23 -08001099
Alan Sternf69e31202011-11-03 11:37:10 -04001100 return retval;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001101}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001102EXPORT_SYMBOL_GPL(xhci_resume);
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001103#endif /* CONFIG_PM */
1104
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001105/*-------------------------------------------------------------------------*/
1106
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001107/**
1108 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1109 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1110 * value to right shift 1 for the bitmask.
1111 *
1112 * Index = (epnum * 2) + direction - 1,
1113 * where direction = 0 for OUT, 1 for IN.
1114 * For control endpoints, the IN index is used (OUT index is unused), so
1115 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1116 */
1117unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1118{
1119 unsigned int index;
1120 if (usb_endpoint_xfer_control(desc))
1121 index = (unsigned int) (usb_endpoint_num(desc)*2);
1122 else
1123 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1124 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1125 return index;
1126}
1127
Julius Werner01c5f442013-04-15 15:55:04 -07001128/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1129 * address from the XHCI endpoint index.
1130 */
1131unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1132{
1133 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1134 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1135 return direction | number;
1136}
1137
Sarah Sharpf94e01862009-04-27 19:58:38 -07001138/* Find the flag for this endpoint (for use in the control context). Use the
1139 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1140 * bit 1, etc.
1141 */
Lu Baolu39693842017-04-07 17:57:04 +03001142static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001143{
1144 return 1 << (xhci_get_endpoint_index(desc) + 1);
1145}
1146
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001147/* Find the flag for this endpoint (for use in the control context). Use the
1148 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1149 * bit 1, etc.
1150 */
Lu Baolu39693842017-04-07 17:57:04 +03001151static unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001152{
1153 return 1 << (ep_index + 1);
1154}
1155
Sarah Sharpf94e01862009-04-27 19:58:38 -07001156/* Compute the last valid endpoint context index. Basically, this is the
1157 * endpoint index plus one. For slot contexts with more than valid endpoint,
1158 * we find the most significant bit set in the added contexts flags.
1159 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1160 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1161 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001162unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001163{
1164 return fls(added_ctxs) - 1;
1165}
1166
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001167/* Returns 1 if the arguments are OK;
1168 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1169 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001170static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001171 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1172 const char *func) {
1173 struct xhci_hcd *xhci;
1174 struct xhci_virt_device *virt_dev;
1175
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001176 if (!hcd || (check_ep && !ep) || !udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001177 pr_debug("xHCI %s called with invalid args\n", func);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001178 return -EINVAL;
1179 }
1180 if (!udev->parent) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001181 pr_debug("xHCI %s called for root hub\n", func);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001182 return 0;
1183 }
Andiry Xu64927732010-10-14 07:22:45 -07001184
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001185 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001186 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001187 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001188 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1189 func);
Andiry Xu64927732010-10-14 07:22:45 -07001190 return -EINVAL;
1191 }
1192
1193 virt_dev = xhci->devs[udev->slot_id];
1194 if (virt_dev->udev != udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001195 xhci_dbg(xhci, "xHCI %s called with udev and "
Andiry Xu64927732010-10-14 07:22:45 -07001196 "virt_dev does not match\n", func);
1197 return -EINVAL;
1198 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001199 }
Andiry Xu64927732010-10-14 07:22:45 -07001200
Sarah Sharp203a8662013-07-24 10:27:13 -07001201 if (xhci->xhc_state & XHCI_STATE_HALTED)
1202 return -ENODEV;
1203
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001204 return 1;
1205}
1206
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001207static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001208 struct usb_device *udev, struct xhci_command *command,
1209 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001210
1211/*
1212 * Full speed devices may have a max packet size greater than 8 bytes, but the
1213 * USB core doesn't know that until it reads the first 8 bytes of the
1214 * descriptor. If the usb_device's max packet size changes after that point,
1215 * we need to issue an evaluate context command and wait on it.
1216 */
1217static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1218 unsigned int ep_index, struct urb *urb)
1219{
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001220 struct xhci_container_ctx *out_ctx;
1221 struct xhci_input_control_ctx *ctrl_ctx;
1222 struct xhci_ep_ctx *ep_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001223 struct xhci_command *command;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001224 int max_packet_size;
1225 int hw_max_packet_size;
1226 int ret = 0;
1227
1228 out_ctx = xhci->devs[slot_id]->out_ctx;
1229 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001230 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001231 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001232 if (hw_max_packet_size != max_packet_size) {
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001233 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1234 "Max Packet Size for ep 0 changed.");
1235 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1236 "Max packet size in usb_device = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001237 max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001238 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1239 "Max packet size in xHCI HW = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001240 hw_max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001241 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1242 "Issuing evaluate context command.");
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001243
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001244 /* Set up the input context flags for the command */
1245 /* FIXME: This won't work if a non-default control endpoint
1246 * changes max packet sizes.
1247 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001248
Mathias Nyman103afda2017-12-08 17:59:08 +02001249 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001250 if (!command)
1251 return -ENOMEM;
1252
1253 command->in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001254 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001255 if (!ctrl_ctx) {
1256 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1257 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001258 ret = -ENOMEM;
1259 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07001260 }
1261 /* Set up the modified control endpoint 0 */
1262 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1263 xhci->devs[slot_id]->out_ctx, ep_index);
1264
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001265 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001266 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1267 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1268
Matt Evans28ccd292011-03-29 13:40:46 +11001269 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001270 ctrl_ctx->drop_flags = 0;
1271
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001272 ret = xhci_configure_endpoint(xhci, urb->dev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001273 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001274
1275 /* Clean up the input context for later use by bandwidth
1276 * functions.
1277 */
Matt Evans28ccd292011-03-29 13:40:46 +11001278 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001279command_cleanup:
1280 kfree(command->completion);
1281 kfree(command);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001282 }
1283 return ret;
1284}
1285
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001286/*
1287 * non-error returns are a promise to giveback() the urb later
1288 * we drop ownership so next owner (or urb unlink) can get it
1289 */
Lu Baolu39693842017-04-07 17:57:04 +03001290static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001291{
1292 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1293 unsigned long flags;
1294 int ret = 0;
Mathias Nyman15febf52018-03-16 16:33:03 +02001295 unsigned int slot_id, ep_index;
1296 unsigned int *ep_state;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001297 struct urb_priv *urb_priv;
Mathias Nyman7e64b032017-01-23 14:20:26 +02001298 int num_tds;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001299
Andiry Xu64927732010-10-14 07:22:45 -07001300 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1301 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001302 return -EINVAL;
1303
1304 slot_id = urb->dev->slot_id;
1305 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Mathias Nyman15febf52018-03-16 16:33:03 +02001306 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001307
Alan Stern541c7d42010-06-22 16:39:10 -04001308 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001309 if (!in_interrupt())
1310 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
Mathias Nyman69694082017-01-23 14:20:27 +02001311 return -ESHUTDOWN;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001312 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001313
1314 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001315 num_tds = urb->number_of_packets;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03001316 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1317 urb->transfer_buffer_length > 0 &&
1318 urb->transfer_flags & URB_ZERO_PACKET &&
1319 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001320 num_tds = 2;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001321 else
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001322 num_tds = 1;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001323
1324 urb_priv = kzalloc(sizeof(struct urb_priv) +
Mathias Nyman7e64b032017-01-23 14:20:26 +02001325 num_tds * sizeof(struct xhci_td), mem_flags);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001326 if (!urb_priv)
1327 return -ENOMEM;
1328
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001329 urb_priv->num_tds = num_tds;
1330 urb_priv->num_tds_done = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001331 urb->hcpriv = urb_priv;
1332
Felipe Balbi5abdc2e2017-01-23 14:20:20 +02001333 trace_xhci_urb_enqueue(urb);
1334
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001335 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1336 /* Check to see if the max packet size for the default control
1337 * endpoint changed during FS device enumeration
1338 */
1339 if (urb->dev->speed == USB_SPEED_FULL) {
1340 ret = xhci_check_maxpacket(xhci, slot_id,
1341 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001342 if (ret < 0) {
Lin Wang4daf9df2015-01-09 16:06:31 +02001343 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001344 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001345 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001346 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001347 }
Mathias Nyman69694082017-01-23 14:20:27 +02001348 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001349
Mathias Nyman69694082017-01-23 14:20:27 +02001350 spin_lock_irqsave(&xhci->lock, flags);
1351
1352 if (xhci->xhc_state & XHCI_STATE_DYING) {
1353 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n",
1354 urb->ep->desc.bEndpointAddress, urb);
1355 ret = -ESHUTDOWN;
1356 goto free_priv;
1357 }
Mathias Nyman15febf52018-03-16 16:33:03 +02001358 if (*ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) {
1359 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n",
1360 *ep_state);
1361 ret = -EINVAL;
1362 goto free_priv;
1363 }
Mathias Nymanf5249462018-03-16 16:33:04 +02001364 if (*ep_state & EP_SOFT_CLEAR_TOGGLE) {
1365 xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n");
1366 ret = -EINVAL;
1367 goto free_priv;
1368 }
Mathias Nyman69694082017-01-23 14:20:27 +02001369
1370 switch (usb_endpoint_type(&urb->ep->desc)) {
1371
1372 case USB_ENDPOINT_XFER_CONTROL:
Sarah Sharpb11069f2009-07-27 12:03:23 -07001373 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Mathias Nyman69694082017-01-23 14:20:27 +02001374 slot_id, ep_index);
1375 break;
1376 case USB_ENDPOINT_XFER_BULK:
Mathias Nyman69694082017-01-23 14:20:27 +02001377 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1378 slot_id, ep_index);
1379 break;
Mathias Nyman69694082017-01-23 14:20:27 +02001380 case USB_ENDPOINT_XFER_INT:
Sarah Sharp624defa2009-09-02 12:14:28 -07001381 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1382 slot_id, ep_index);
Mathias Nyman69694082017-01-23 14:20:27 +02001383 break;
Mathias Nyman69694082017-01-23 14:20:27 +02001384 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 Nyman103afda2017-12-08 17:59:08 +02001507 command = xhci_alloc_command(xhci, 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
Lu Baolu02b6fdc2017-10-05 11:21:39 +03001603 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index);
1604
Sarah Sharpf94e01862009-04-27 19:58:38 -07001605 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1606
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001607 if (xhci->quirks & XHCI_MTK_HOST)
1608 xhci_mtk_drop_ep_quirk(hcd, udev, ep);
1609
Julius Wernerd6759132014-06-24 17:14:42 +03001610 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 -07001611 (unsigned int) ep->desc.bEndpointAddress,
1612 udev->slot_id,
1613 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001614 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001615 return 0;
1616}
1617
1618/* Add an endpoint to a new possible bandwidth configuration for this device.
1619 * Only one call to this function is allowed per endpoint before
1620 * check_bandwidth() or reset_bandwidth() must be called.
1621 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1622 * add the endpoint to the schedule with possibly new parameters denoted by a
1623 * different endpoint descriptor in usb_host_endpoint.
1624 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1625 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001626 *
1627 * The USB core will not allow URBs to be queued to an endpoint until the
1628 * configuration or alt setting is installed in the device, so there's no need
1629 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001630 */
Lu Baolu39693842017-04-07 17:57:04 +03001631static int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharpf94e01862009-04-27 19:58:38 -07001632 struct usb_host_endpoint *ep)
1633{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001634 struct xhci_hcd *xhci;
Lin Wang92c96912015-01-09 16:06:27 +02001635 struct xhci_container_ctx *in_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001636 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001637 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001638 u32 added_ctxs;
Julius Wernerd6759132014-06-24 17:14:42 +03001639 u32 new_add_flags, new_drop_flags;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001640 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001641 int ret = 0;
1642
Andiry Xu64927732010-10-14 07:22:45 -07001643 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001644 if (ret <= 0) {
1645 /* So we won't queue a reset ep command for a root hub */
1646 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001647 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001648 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001649 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001650 if (xhci->xhc_state & XHCI_STATE_DYING)
1651 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001652
1653 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001654 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1655 /* FIXME when we have to issue an evaluate endpoint command to
1656 * deal with ep0 max packet size changing once we get the
1657 * descriptors
1658 */
1659 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1660 __func__, added_ctxs);
1661 return 0;
1662 }
1663
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001664 virt_dev = xhci->devs[udev->slot_id];
1665 in_ctx = virt_dev->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001666 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001667 if (!ctrl_ctx) {
1668 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1669 __func__);
1670 return 0;
1671 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001672
Sarah Sharp92f8e762013-04-23 17:11:14 -07001673 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001674 /* If this endpoint is already in use, and the upper layers are trying
1675 * to add it again without dropping it, reject the addition.
1676 */
1677 if (virt_dev->eps[ep_index].ring &&
Lin Wang92c96912015-01-09 16:06:27 +02001678 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001679 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1680 "without dropping it.\n",
1681 (unsigned int) ep->desc.bEndpointAddress);
1682 return -EINVAL;
1683 }
1684
Sarah Sharpf94e01862009-04-27 19:58:38 -07001685 /* If the HCD has already noted the endpoint is enabled,
1686 * ignore this request.
1687 */
Lin Wang92c96912015-01-09 16:06:27 +02001688 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001689 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1690 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001691 return 0;
1692 }
1693
Sarah Sharpf88ba782009-05-14 11:44:22 -07001694 /*
1695 * Configuration and alternate setting changes must be done in
1696 * process context, not interrupt context (or so documenation
1697 * for usb_set_interface() and usb_set_configuration() claim).
1698 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001699 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001700 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1701 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001702 return -ENOMEM;
1703 }
1704
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001705 if (xhci->quirks & XHCI_MTK_HOST) {
1706 ret = xhci_mtk_add_ep_quirk(hcd, udev, ep);
1707 if (ret < 0) {
Lu Baolu98217862017-09-18 17:39:12 +03001708 xhci_ring_free(xhci, virt_dev->eps[ep_index].new_ring);
1709 virt_dev->eps[ep_index].new_ring = NULL;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001710 return ret;
1711 }
1712 }
1713
Matt Evans28ccd292011-03-29 13:40:46 +11001714 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1715 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001716
1717 /* If xhci_endpoint_disable() was called for this endpoint, but the
1718 * xHC hasn't been notified yet through the check_bandwidth() call,
1719 * this re-adds a new state for the endpoint from the new endpoint
1720 * descriptors. We must drop and re-add this endpoint, so we leave the
1721 * drop flags alone.
1722 */
Matt Evans28ccd292011-03-29 13:40:46 +11001723 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001724
Sarah Sharpa1587d92009-07-27 12:03:15 -07001725 /* Store the usb_device pointer for later use */
1726 ep->hcpriv = udev;
1727
Lu Baolu02b6fdc2017-10-05 11:21:39 +03001728 xhci_debugfs_create_endpoint(xhci, virt_dev, ep_index);
1729
Julius Wernerd6759132014-06-24 17:14:42 +03001730 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 -07001731 (unsigned int) ep->desc.bEndpointAddress,
1732 udev->slot_id,
1733 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001734 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001735 return 0;
1736}
1737
John Yound115b042009-07-27 12:05:15 -07001738static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001739{
John Yound115b042009-07-27 12:05:15 -07001740 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001741 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001742 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001743 int i;
1744
Lin Wang4daf9df2015-01-09 16:06:31 +02001745 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001746 if (!ctrl_ctx) {
1747 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1748 __func__);
1749 return;
1750 }
1751
Sarah Sharpf94e01862009-04-27 19:58:38 -07001752 /* When a device's add flag and drop flag are zero, any subsequent
1753 * configure endpoint command will leave that endpoint's state
1754 * untouched. Make sure we don't leave any old state in the input
1755 * endpoint contexts.
1756 */
John Yound115b042009-07-27 12:05:15 -07001757 ctrl_ctx->drop_flags = 0;
1758 ctrl_ctx->add_flags = 0;
1759 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001760 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001761 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001762 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Felipe Balbi98871e92017-01-23 14:20:04 +02001763 for (i = 1; i < 31; i++) {
John Yound115b042009-07-27 12:05:15 -07001764 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001765 ep_ctx->ep_info = 0;
1766 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001767 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001768 ep_ctx->tx_info = 0;
1769 }
1770}
1771
Sarah Sharpf2217e82009-08-07 14:04:43 -07001772static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001773 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001774{
1775 int ret;
1776
Sarah Sharp913a8a32009-09-04 10:53:13 -07001777 switch (*cmd_status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001778 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03001779 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03001780 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
1781 ret = -ETIME;
1782 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001783 case COMP_RESOURCE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001784 dev_warn(&udev->dev,
1785 "Not enough host controller resources for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001786 ret = -ENOMEM;
1787 /* FIXME: can we allocate more resources for the HC? */
1788 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001789 case COMP_BANDWIDTH_ERROR:
1790 case COMP_SECONDARY_BANDWIDTH_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001791 dev_warn(&udev->dev,
1792 "Not enough bandwidth for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001793 ret = -ENOSPC;
1794 /* FIXME: can we go back to the old state? */
1795 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001796 case COMP_TRB_ERROR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001797 /* the HCD set up something wrong */
1798 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1799 "add flag = 1, "
1800 "and endpoint is not disabled.\n");
1801 ret = -EINVAL;
1802 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001803 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001804 dev_warn(&udev->dev,
1805 "ERROR: Incompatible device for endpoint configure command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001806 ret = -ENODEV;
1807 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001808 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001809 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1810 "Successful Endpoint Configure command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001811 ret = 0;
1812 break;
1813 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001814 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1815 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001816 ret = -EINVAL;
1817 break;
1818 }
1819 return ret;
1820}
1821
1822static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001823 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001824{
1825 int ret;
1826
Sarah Sharp913a8a32009-09-04 10:53:13 -07001827 switch (*cmd_status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001828 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03001829 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03001830 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
1831 ret = -ETIME;
1832 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001833 case COMP_PARAMETER_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001834 dev_warn(&udev->dev,
1835 "WARN: xHCI driver setup invalid evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001836 ret = -EINVAL;
1837 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001838 case COMP_SLOT_NOT_ENABLED_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001839 dev_warn(&udev->dev,
1840 "WARN: slot not enabled for evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001841 ret = -EINVAL;
1842 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001843 case COMP_CONTEXT_STATE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001844 dev_warn(&udev->dev,
1845 "WARN: invalid context state for evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001846 ret = -EINVAL;
1847 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001848 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001849 dev_warn(&udev->dev,
1850 "ERROR: Incompatible device for evaluate context command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001851 ret = -ENODEV;
1852 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001853 case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
Alex He1bb73a82011-05-05 18:14:12 +08001854 /* Max Exit Latency too large error */
1855 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1856 ret = -EINVAL;
1857 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001858 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001859 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1860 "Successful evaluate context command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001861 ret = 0;
1862 break;
1863 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001864 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1865 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001866 ret = -EINVAL;
1867 break;
1868 }
1869 return ret;
1870}
1871
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001872static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001873 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001874{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001875 u32 valid_add_flags;
1876 u32 valid_drop_flags;
1877
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001878 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1879 * (bit 1). The default control endpoint is added during the Address
1880 * Device command and is never removed until the slot is disabled.
1881 */
Xenia Ragiadakouef734002013-09-09 21:03:06 +03001882 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1883 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001884
1885 /* Use hweight32 to count the number of ones in the add flags, or
1886 * number of endpoints added. Don't count endpoints that are changed
1887 * (both added and dropped).
1888 */
1889 return hweight32(valid_add_flags) -
1890 hweight32(valid_add_flags & valid_drop_flags);
1891}
1892
1893static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001894 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001895{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001896 u32 valid_add_flags;
1897 u32 valid_drop_flags;
1898
Xenia Ragiadakou78d1ff02013-09-09 21:03:07 +03001899 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1900 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001901
1902 return hweight32(valid_drop_flags) -
1903 hweight32(valid_add_flags & valid_drop_flags);
1904}
1905
1906/*
1907 * We need to reserve the new number of endpoints before the configure endpoint
1908 * command completes. We can't subtract the dropped endpoints from the number
1909 * of active endpoints until the command completes because we can oversubscribe
1910 * the host in this case:
1911 *
1912 * - the first configure endpoint command drops more endpoints than it adds
1913 * - a second configure endpoint command that adds more endpoints is queued
1914 * - the first configure endpoint command fails, so the config is unchanged
1915 * - the second command may succeed, even though there isn't enough resources
1916 *
1917 * Must be called with xhci->lock held.
1918 */
1919static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001920 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001921{
1922 u32 added_eps;
1923
Sarah Sharp92f8e762013-04-23 17:11:14 -07001924 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001925 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001926 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1927 "Not enough ep ctxs: "
1928 "%u active, need to add %u, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001929 xhci->num_active_eps, added_eps,
1930 xhci->limit_active_eps);
1931 return -ENOMEM;
1932 }
1933 xhci->num_active_eps += added_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001934 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1935 "Adding %u ep ctxs, %u now active.", added_eps,
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001936 xhci->num_active_eps);
1937 return 0;
1938}
1939
1940/*
1941 * The configure endpoint was failed by the xHC for some other reason, so we
1942 * need to revert the resources that failed configuration would have used.
1943 *
1944 * Must be called with xhci->lock held.
1945 */
1946static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001947 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001948{
1949 u32 num_failed_eps;
1950
Sarah Sharp92f8e762013-04-23 17:11:14 -07001951 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001952 xhci->num_active_eps -= num_failed_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001953 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1954 "Removing %u failed ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001955 num_failed_eps,
1956 xhci->num_active_eps);
1957}
1958
1959/*
1960 * Now that the command has completed, clean up the active endpoint count by
1961 * subtracting out the endpoints that were dropped (but not changed).
1962 *
1963 * Must be called with xhci->lock held.
1964 */
1965static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001966 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001967{
1968 u32 num_dropped_eps;
1969
Sarah Sharp92f8e762013-04-23 17:11:14 -07001970 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001971 xhci->num_active_eps -= num_dropped_eps;
1972 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001973 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1974 "Removing %u dropped ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001975 num_dropped_eps,
1976 xhci->num_active_eps);
1977}
1978
Felipe Balbied384bd2012-08-07 14:10:03 +03001979static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07001980{
1981 switch (udev->speed) {
1982 case USB_SPEED_LOW:
1983 case USB_SPEED_FULL:
1984 return FS_BLOCK;
1985 case USB_SPEED_HIGH:
1986 return HS_BLOCK;
1987 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02001988 case USB_SPEED_SUPER_PLUS:
Sarah Sharpc29eea62011-09-02 11:05:52 -07001989 return SS_BLOCK;
1990 case USB_SPEED_UNKNOWN:
1991 case USB_SPEED_WIRELESS:
1992 default:
1993 /* Should never happen */
1994 return 1;
1995 }
1996}
1997
Felipe Balbied384bd2012-08-07 14:10:03 +03001998static unsigned int
1999xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002000{
2001 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2002 return LS_OVERHEAD;
2003 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2004 return FS_OVERHEAD;
2005 return HS_OVERHEAD;
2006}
2007
2008/* If we are changing a LS/FS device under a HS hub,
2009 * make sure (if we are activating a new TT) that the HS bus has enough
2010 * bandwidth for this new TT.
2011 */
2012static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2013 struct xhci_virt_device *virt_dev,
2014 int old_active_eps)
2015{
2016 struct xhci_interval_bw_table *bw_table;
2017 struct xhci_tt_bw_info *tt_info;
2018
2019 /* Find the bandwidth table for the root port this TT is attached to. */
2020 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2021 tt_info = virt_dev->tt_info;
2022 /* If this TT already had active endpoints, the bandwidth for this TT
2023 * has already been added. Removing all periodic endpoints (and thus
2024 * making the TT enactive) will only decrease the bandwidth used.
2025 */
2026 if (old_active_eps)
2027 return 0;
2028 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2029 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2030 return -ENOMEM;
2031 return 0;
2032 }
2033 /* Not sure why we would have no new active endpoints...
2034 *
2035 * Maybe because of an Evaluate Context change for a hub update or a
2036 * control endpoint 0 max packet size change?
2037 * FIXME: skip the bandwidth calculation in that case.
2038 */
2039 return 0;
2040}
2041
Sarah Sharp2b698992011-09-13 16:41:13 -07002042static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2043 struct xhci_virt_device *virt_dev)
2044{
2045 unsigned int bw_reserved;
2046
2047 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2048 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2049 return -ENOMEM;
2050
2051 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2052 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2053 return -ENOMEM;
2054
2055 return 0;
2056}
2057
Sarah Sharpc29eea62011-09-02 11:05:52 -07002058/*
2059 * This algorithm is a very conservative estimate of the worst-case scheduling
2060 * scenario for any one interval. The hardware dynamically schedules the
2061 * packets, so we can't tell which microframe could be the limiting factor in
2062 * the bandwidth scheduling. This only takes into account periodic endpoints.
2063 *
2064 * Obviously, we can't solve an NP complete problem to find the minimum worst
2065 * case scenario. Instead, we come up with an estimate that is no less than
2066 * the worst case bandwidth used for any one microframe, but may be an
2067 * over-estimate.
2068 *
2069 * We walk the requirements for each endpoint by interval, starting with the
2070 * smallest interval, and place packets in the schedule where there is only one
2071 * possible way to schedule packets for that interval. In order to simplify
2072 * this algorithm, we record the largest max packet size for each interval, and
2073 * assume all packets will be that size.
2074 *
2075 * For interval 0, we obviously must schedule all packets for each interval.
2076 * The bandwidth for interval 0 is just the amount of data to be transmitted
2077 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2078 * the number of packets).
2079 *
2080 * For interval 1, we have two possible microframes to schedule those packets
2081 * in. For this algorithm, if we can schedule the same number of packets for
2082 * each possible scheduling opportunity (each microframe), we will do so. The
2083 * remaining number of packets will be saved to be transmitted in the gaps in
2084 * the next interval's scheduling sequence.
2085 *
2086 * As we move those remaining packets to be scheduled with interval 2 packets,
2087 * we have to double the number of remaining packets to transmit. This is
2088 * because the intervals are actually powers of 2, and we would be transmitting
2089 * the previous interval's packets twice in this interval. We also have to be
2090 * sure that when we look at the largest max packet size for this interval, we
2091 * also look at the largest max packet size for the remaining packets and take
2092 * the greater of the two.
2093 *
2094 * The algorithm continues to evenly distribute packets in each scheduling
2095 * opportunity, and push the remaining packets out, until we get to the last
2096 * interval. Then those packets and their associated overhead are just added
2097 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002098 */
2099static int xhci_check_bw_table(struct xhci_hcd *xhci,
2100 struct xhci_virt_device *virt_dev,
2101 int old_active_eps)
2102{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002103 unsigned int bw_reserved;
2104 unsigned int max_bandwidth;
2105 unsigned int bw_used;
2106 unsigned int block_size;
2107 struct xhci_interval_bw_table *bw_table;
2108 unsigned int packet_size = 0;
2109 unsigned int overhead = 0;
2110 unsigned int packets_transmitted = 0;
2111 unsigned int packets_remaining = 0;
2112 unsigned int i;
2113
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002114 if (virt_dev->udev->speed >= USB_SPEED_SUPER)
Sarah Sharp2b698992011-09-13 16:41:13 -07002115 return xhci_check_ss_bw(xhci, virt_dev);
2116
Sarah Sharpc29eea62011-09-02 11:05:52 -07002117 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2118 max_bandwidth = HS_BW_LIMIT;
2119 /* Convert percent of bus BW reserved to blocks reserved */
2120 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2121 } else {
2122 max_bandwidth = FS_BW_LIMIT;
2123 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2124 }
2125
2126 bw_table = virt_dev->bw_table;
2127 /* We need to translate the max packet size and max ESIT payloads into
2128 * the units the hardware uses.
2129 */
2130 block_size = xhci_get_block_size(virt_dev->udev);
2131
2132 /* If we are manipulating a LS/FS device under a HS hub, double check
2133 * that the HS bus has enough bandwidth if we are activing a new TT.
2134 */
2135 if (virt_dev->tt_info) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002136 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2137 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002138 virt_dev->real_port);
2139 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2140 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2141 "newly activated TT.\n");
2142 return -ENOMEM;
2143 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002144 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2145 "Recalculating BW for TT slot %u port %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002146 virt_dev->tt_info->slot_id,
2147 virt_dev->tt_info->ttport);
2148 } else {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002149 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2150 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002151 virt_dev->real_port);
2152 }
2153
2154 /* Add in how much bandwidth will be used for interval zero, or the
2155 * rounded max ESIT payload + number of packets * largest overhead.
2156 */
2157 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2158 bw_table->interval_bw[0].num_packets *
2159 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2160
2161 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2162 unsigned int bw_added;
2163 unsigned int largest_mps;
2164 unsigned int interval_overhead;
2165
2166 /*
2167 * How many packets could we transmit in this interval?
2168 * If packets didn't fit in the previous interval, we will need
2169 * to transmit that many packets twice within this interval.
2170 */
2171 packets_remaining = 2 * packets_remaining +
2172 bw_table->interval_bw[i].num_packets;
2173
2174 /* Find the largest max packet size of this or the previous
2175 * interval.
2176 */
2177 if (list_empty(&bw_table->interval_bw[i].endpoints))
2178 largest_mps = 0;
2179 else {
2180 struct xhci_virt_ep *virt_ep;
2181 struct list_head *ep_entry;
2182
2183 ep_entry = bw_table->interval_bw[i].endpoints.next;
2184 virt_ep = list_entry(ep_entry,
2185 struct xhci_virt_ep, bw_endpoint_list);
2186 /* Convert to blocks, rounding up */
2187 largest_mps = DIV_ROUND_UP(
2188 virt_ep->bw_info.max_packet_size,
2189 block_size);
2190 }
2191 if (largest_mps > packet_size)
2192 packet_size = largest_mps;
2193
2194 /* Use the larger overhead of this or the previous interval. */
2195 interval_overhead = xhci_get_largest_overhead(
2196 &bw_table->interval_bw[i]);
2197 if (interval_overhead > overhead)
2198 overhead = interval_overhead;
2199
2200 /* How many packets can we evenly distribute across
2201 * (1 << (i + 1)) possible scheduling opportunities?
2202 */
2203 packets_transmitted = packets_remaining >> (i + 1);
2204
2205 /* Add in the bandwidth used for those scheduled packets */
2206 bw_added = packets_transmitted * (overhead + packet_size);
2207
2208 /* How many packets do we have remaining to transmit? */
2209 packets_remaining = packets_remaining % (1 << (i + 1));
2210
2211 /* What largest max packet size should those packets have? */
2212 /* If we've transmitted all packets, don't carry over the
2213 * largest packet size.
2214 */
2215 if (packets_remaining == 0) {
2216 packet_size = 0;
2217 overhead = 0;
2218 } else if (packets_transmitted > 0) {
2219 /* Otherwise if we do have remaining packets, and we've
2220 * scheduled some packets in this interval, take the
2221 * largest max packet size from endpoints with this
2222 * interval.
2223 */
2224 packet_size = largest_mps;
2225 overhead = interval_overhead;
2226 }
2227 /* Otherwise carry over packet_size and overhead from the last
2228 * time we had a remainder.
2229 */
2230 bw_used += bw_added;
2231 if (bw_used > max_bandwidth) {
2232 xhci_warn(xhci, "Not enough bandwidth. "
2233 "Proposed: %u, Max: %u\n",
2234 bw_used, max_bandwidth);
2235 return -ENOMEM;
2236 }
2237 }
2238 /*
2239 * Ok, we know we have some packets left over after even-handedly
2240 * scheduling interval 15. We don't know which microframes they will
2241 * fit into, so we over-schedule and say they will be scheduled every
2242 * microframe.
2243 */
2244 if (packets_remaining > 0)
2245 bw_used += overhead + packet_size;
2246
2247 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2248 unsigned int port_index = virt_dev->real_port - 1;
2249
2250 /* OK, we're manipulating a HS device attached to a
2251 * root port bandwidth domain. Include the number of active TTs
2252 * in the bandwidth used.
2253 */
2254 bw_used += TT_HS_OVERHEAD *
2255 xhci->rh_bw[port_index].num_active_tts;
2256 }
2257
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002258 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2259 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2260 "Available: %u " "percent",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002261 bw_used, max_bandwidth, bw_reserved,
2262 (max_bandwidth - bw_used - bw_reserved) * 100 /
2263 max_bandwidth);
2264
2265 bw_used += bw_reserved;
2266 if (bw_used > max_bandwidth) {
2267 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2268 bw_used, max_bandwidth);
2269 return -ENOMEM;
2270 }
2271
2272 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002273 return 0;
2274}
2275
2276static bool xhci_is_async_ep(unsigned int ep_type)
2277{
2278 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2279 ep_type != ISOC_IN_EP &&
2280 ep_type != INT_IN_EP);
2281}
2282
Sarah Sharp2b698992011-09-13 16:41:13 -07002283static bool xhci_is_sync_in_ep(unsigned int ep_type)
2284{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002285 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002286}
2287
2288static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2289{
2290 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2291
2292 if (ep_bw->ep_interval == 0)
2293 return SS_OVERHEAD_BURST +
2294 (ep_bw->mult * ep_bw->num_packets *
2295 (SS_OVERHEAD + mps));
2296 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2297 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2298 1 << ep_bw->ep_interval);
2299
2300}
2301
Lu Baolu39693842017-04-07 17:57:04 +03002302static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
Sarah Sharp2e279802011-09-02 11:05:50 -07002303 struct xhci_bw_info *ep_bw,
2304 struct xhci_interval_bw_table *bw_table,
2305 struct usb_device *udev,
2306 struct xhci_virt_ep *virt_ep,
2307 struct xhci_tt_bw_info *tt_info)
2308{
2309 struct xhci_interval_bw *interval_bw;
2310 int normalized_interval;
2311
Sarah Sharp2b698992011-09-13 16:41:13 -07002312 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002313 return;
2314
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002315 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharp2b698992011-09-13 16:41:13 -07002316 if (xhci_is_sync_in_ep(ep_bw->type))
2317 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2318 xhci_get_ss_bw_consumed(ep_bw);
2319 else
2320 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2321 xhci_get_ss_bw_consumed(ep_bw);
2322 return;
2323 }
2324
2325 /* SuperSpeed endpoints never get added to intervals in the table, so
2326 * this check is only valid for HS/FS/LS devices.
2327 */
2328 if (list_empty(&virt_ep->bw_endpoint_list))
2329 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002330 /* For LS/FS devices, we need to translate the interval expressed in
2331 * microframes to frames.
2332 */
2333 if (udev->speed == USB_SPEED_HIGH)
2334 normalized_interval = ep_bw->ep_interval;
2335 else
2336 normalized_interval = ep_bw->ep_interval - 3;
2337
2338 if (normalized_interval == 0)
2339 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2340 interval_bw = &bw_table->interval_bw[normalized_interval];
2341 interval_bw->num_packets -= ep_bw->num_packets;
2342 switch (udev->speed) {
2343 case USB_SPEED_LOW:
2344 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2345 break;
2346 case USB_SPEED_FULL:
2347 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2348 break;
2349 case USB_SPEED_HIGH:
2350 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2351 break;
2352 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002353 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002354 case USB_SPEED_UNKNOWN:
2355 case USB_SPEED_WIRELESS:
2356 /* Should never happen because only LS/FS/HS endpoints will get
2357 * added to the endpoint list.
2358 */
2359 return;
2360 }
2361 if (tt_info)
2362 tt_info->active_eps -= 1;
2363 list_del_init(&virt_ep->bw_endpoint_list);
2364}
2365
2366static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2367 struct xhci_bw_info *ep_bw,
2368 struct xhci_interval_bw_table *bw_table,
2369 struct usb_device *udev,
2370 struct xhci_virt_ep *virt_ep,
2371 struct xhci_tt_bw_info *tt_info)
2372{
2373 struct xhci_interval_bw *interval_bw;
2374 struct xhci_virt_ep *smaller_ep;
2375 int normalized_interval;
2376
2377 if (xhci_is_async_ep(ep_bw->type))
2378 return;
2379
Sarah Sharp2b698992011-09-13 16:41:13 -07002380 if (udev->speed == USB_SPEED_SUPER) {
2381 if (xhci_is_sync_in_ep(ep_bw->type))
2382 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2383 xhci_get_ss_bw_consumed(ep_bw);
2384 else
2385 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2386 xhci_get_ss_bw_consumed(ep_bw);
2387 return;
2388 }
2389
Sarah Sharp2e279802011-09-02 11:05:50 -07002390 /* For LS/FS devices, we need to translate the interval expressed in
2391 * microframes to frames.
2392 */
2393 if (udev->speed == USB_SPEED_HIGH)
2394 normalized_interval = ep_bw->ep_interval;
2395 else
2396 normalized_interval = ep_bw->ep_interval - 3;
2397
2398 if (normalized_interval == 0)
2399 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2400 interval_bw = &bw_table->interval_bw[normalized_interval];
2401 interval_bw->num_packets += ep_bw->num_packets;
2402 switch (udev->speed) {
2403 case USB_SPEED_LOW:
2404 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2405 break;
2406 case USB_SPEED_FULL:
2407 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2408 break;
2409 case USB_SPEED_HIGH:
2410 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2411 break;
2412 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002413 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002414 case USB_SPEED_UNKNOWN:
2415 case USB_SPEED_WIRELESS:
2416 /* Should never happen because only LS/FS/HS endpoints will get
2417 * added to the endpoint list.
2418 */
2419 return;
2420 }
2421
2422 if (tt_info)
2423 tt_info->active_eps += 1;
2424 /* Insert the endpoint into the list, largest max packet size first. */
2425 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2426 bw_endpoint_list) {
2427 if (ep_bw->max_packet_size >=
2428 smaller_ep->bw_info.max_packet_size) {
2429 /* Add the new ep before the smaller endpoint */
2430 list_add_tail(&virt_ep->bw_endpoint_list,
2431 &smaller_ep->bw_endpoint_list);
2432 return;
2433 }
2434 }
2435 /* Add the new endpoint at the end of the list. */
2436 list_add_tail(&virt_ep->bw_endpoint_list,
2437 &interval_bw->endpoints);
2438}
2439
2440void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2441 struct xhci_virt_device *virt_dev,
2442 int old_active_eps)
2443{
2444 struct xhci_root_port_bw_info *rh_bw_info;
2445 if (!virt_dev->tt_info)
2446 return;
2447
2448 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2449 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 } else if (old_active_eps != 0 &&
2454 virt_dev->tt_info->active_eps == 0) {
2455 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002456 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002457 }
2458}
2459
2460static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2461 struct xhci_virt_device *virt_dev,
2462 struct xhci_container_ctx *in_ctx)
2463{
2464 struct xhci_bw_info ep_bw_info[31];
2465 int i;
2466 struct xhci_input_control_ctx *ctrl_ctx;
2467 int old_active_eps = 0;
2468
Sarah Sharp2e279802011-09-02 11:05:50 -07002469 if (virt_dev->tt_info)
2470 old_active_eps = virt_dev->tt_info->active_eps;
2471
Lin Wang4daf9df2015-01-09 16:06:31 +02002472 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002473 if (!ctrl_ctx) {
2474 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2475 __func__);
2476 return -ENOMEM;
2477 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002478
2479 for (i = 0; i < 31; i++) {
2480 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2481 continue;
2482
2483 /* Make a copy of the BW info in case we need to revert this */
2484 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2485 sizeof(ep_bw_info[i]));
2486 /* Drop the endpoint from the interval table if the endpoint is
2487 * being dropped or changed.
2488 */
2489 if (EP_IS_DROPPED(ctrl_ctx, i))
2490 xhci_drop_ep_from_interval_table(xhci,
2491 &virt_dev->eps[i].bw_info,
2492 virt_dev->bw_table,
2493 virt_dev->udev,
2494 &virt_dev->eps[i],
2495 virt_dev->tt_info);
2496 }
2497 /* Overwrite the information stored in the endpoints' bw_info */
2498 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2499 for (i = 0; i < 31; i++) {
2500 /* Add any changed or added endpoints to the interval table */
2501 if (EP_IS_ADDED(ctrl_ctx, i))
2502 xhci_add_ep_to_interval_table(xhci,
2503 &virt_dev->eps[i].bw_info,
2504 virt_dev->bw_table,
2505 virt_dev->udev,
2506 &virt_dev->eps[i],
2507 virt_dev->tt_info);
2508 }
2509
2510 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2511 /* Ok, this fits in the bandwidth we have.
2512 * Update the number of active TTs.
2513 */
2514 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2515 return 0;
2516 }
2517
2518 /* We don't have enough bandwidth for this, revert the stored info. */
2519 for (i = 0; i < 31; i++) {
2520 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2521 continue;
2522
2523 /* Drop the new copies of any added or changed endpoints from
2524 * the interval table.
2525 */
2526 if (EP_IS_ADDED(ctrl_ctx, i)) {
2527 xhci_drop_ep_from_interval_table(xhci,
2528 &virt_dev->eps[i].bw_info,
2529 virt_dev->bw_table,
2530 virt_dev->udev,
2531 &virt_dev->eps[i],
2532 virt_dev->tt_info);
2533 }
2534 /* Revert the endpoint back to its old information */
2535 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2536 sizeof(ep_bw_info[i]));
2537 /* Add any changed or dropped endpoints back into the table */
2538 if (EP_IS_DROPPED(ctrl_ctx, i))
2539 xhci_add_ep_to_interval_table(xhci,
2540 &virt_dev->eps[i].bw_info,
2541 virt_dev->bw_table,
2542 virt_dev->udev,
2543 &virt_dev->eps[i],
2544 virt_dev->tt_info);
2545 }
2546 return -ENOMEM;
2547}
2548
2549
Sarah Sharpf2217e82009-08-07 14:04:43 -07002550/* Issue a configure endpoint command or evaluate context command
2551 * and wait for it to finish.
2552 */
2553static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002554 struct usb_device *udev,
2555 struct xhci_command *command,
2556 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002557{
2558 int ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002559 unsigned long flags;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002560 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002561 struct xhci_virt_device *virt_dev;
Mathias Nymane3a78ff2017-10-05 11:21:48 +03002562 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002563
2564 if (!command)
2565 return -EINVAL;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002566
2567 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03002568
2569 if (xhci->xhc_state & XHCI_STATE_DYING) {
2570 spin_unlock_irqrestore(&xhci->lock, flags);
2571 return -ESHUTDOWN;
2572 }
2573
Sarah Sharp913a8a32009-09-04 10:53:13 -07002574 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002575
Lin Wang4daf9df2015-01-09 16:06:31 +02002576 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002577 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002578 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002579 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2580 __func__);
2581 return -ENOMEM;
2582 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002583
2584 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002585 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002586 spin_unlock_irqrestore(&xhci->lock, flags);
2587 xhci_warn(xhci, "Not enough host resources, "
2588 "active endpoint contexts = %u\n",
2589 xhci->num_active_eps);
2590 return -ENOMEM;
2591 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002592 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002593 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
Sarah Sharp2e279802011-09-02 11:05:50 -07002594 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002595 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002596 spin_unlock_irqrestore(&xhci->lock, flags);
2597 xhci_warn(xhci, "Not enough bandwidth\n");
2598 return -ENOMEM;
2599 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002600
Mathias Nymane3a78ff2017-10-05 11:21:48 +03002601 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
2602 trace_xhci_configure_endpoint(slot_ctx);
2603
Sarah Sharpf2217e82009-08-07 14:04:43 -07002604 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002605 ret = xhci_queue_configure_endpoint(xhci, command,
2606 command->in_ctx->dma,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002607 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002608 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002609 ret = xhci_queue_evaluate_context(xhci, command,
2610 command->in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002611 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002612 if (ret < 0) {
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002613 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002614 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002615 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03002616 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2617 "FIXME allocate a new ring segment");
Sarah Sharpf2217e82009-08-07 14:04:43 -07002618 return -ENOMEM;
2619 }
2620 xhci_ring_cmd_db(xhci);
2621 spin_unlock_irqrestore(&xhci->lock, flags);
2622
2623 /* Wait for the configure endpoint command to complete */
Mathias Nymanc311e392014-05-08 19:26:03 +03002624 wait_for_completion(command->completion);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002625
2626 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002627 ret = xhci_configure_endpoint_result(xhci, udev,
2628 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002629 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002630 ret = xhci_evaluate_context_result(xhci, udev,
2631 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002632
2633 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2634 spin_lock_irqsave(&xhci->lock, flags);
2635 /* If the command failed, remove the reserved resources.
2636 * Otherwise, clean up the estimate to include dropped eps.
2637 */
2638 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002639 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002640 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002641 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002642 spin_unlock_irqrestore(&xhci->lock, flags);
2643 }
2644 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002645}
2646
Hans de Goededf613832013-10-04 00:29:45 +02002647static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2648 struct xhci_virt_device *vdev, int i)
2649{
2650 struct xhci_virt_ep *ep = &vdev->eps[i];
2651
2652 if (ep->ep_state & EP_HAS_STREAMS) {
2653 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2654 xhci_get_endpoint_address(i));
2655 xhci_free_stream_info(xhci, ep->stream_info);
2656 ep->stream_info = NULL;
2657 ep->ep_state &= ~EP_HAS_STREAMS;
2658 }
2659}
2660
Sarah Sharpf88ba782009-05-14 11:44:22 -07002661/* Called after one or more calls to xhci_add_endpoint() or
2662 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2663 * to call xhci_reset_bandwidth().
2664 *
2665 * Since we are in the middle of changing either configuration or
2666 * installing a new alt setting, the USB core won't allow URBs to be
2667 * enqueued for any endpoint on the old config or interface. Nothing
2668 * else should be touching the xhci->devs[slot_id] structure, so we
2669 * don't need to take the xhci->lock for manipulating that.
2670 */
Lu Baolu39693842017-04-07 17:57:04 +03002671static int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002672{
2673 int i;
2674 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002675 struct xhci_hcd *xhci;
2676 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002677 struct xhci_input_control_ctx *ctrl_ctx;
2678 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002679 struct xhci_command *command;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002680
Andiry Xu64927732010-10-14 07:22:45 -07002681 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002682 if (ret <= 0)
2683 return ret;
2684 xhci = hcd_to_xhci(hcd);
Mathias Nyman98d74f92016-04-08 16:25:10 +03002685 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
2686 (xhci->xhc_state & XHCI_STATE_REMOVING))
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002687 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002688
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002689 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002690 virt_dev = xhci->devs[udev->slot_id];
2691
Mathias Nyman103afda2017-12-08 17:59:08 +02002692 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002693 if (!command)
2694 return -ENOMEM;
2695
2696 command->in_ctx = virt_dev->in_ctx;
2697
Sarah Sharpf94e01862009-04-27 19:58:38 -07002698 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
Lin Wang4daf9df2015-01-09 16:06:31 +02002699 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002700 if (!ctrl_ctx) {
2701 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2702 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002703 ret = -ENOMEM;
2704 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002705 }
Matt Evans28ccd292011-03-29 13:40:46 +11002706 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2707 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2708 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002709
2710 /* Don't issue the command if there's no endpoints to update. */
2711 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002712 ctrl_ctx->drop_flags == 0) {
2713 ret = 0;
2714 goto command_cleanup;
2715 }
Julius Wernerd6759132014-06-24 17:14:42 +03002716 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
John Yound115b042009-07-27 12:05:15 -07002717 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Julius Wernerd6759132014-06-24 17:14:42 +03002718 for (i = 31; i >= 1; i--) {
2719 __le32 le32 = cpu_to_le32(BIT(i));
2720
2721 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
2722 || (ctrl_ctx->add_flags & le32) || i == 1) {
2723 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
2724 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
2725 break;
2726 }
2727 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07002728
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002729 ret = xhci_configure_endpoint(xhci, udev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002730 false, false);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002731 if (ret)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002732 /* Callee should call reset_bandwidth() */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002733 goto command_cleanup;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002734
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002735 /* Free any rings that were dropped, but not changed. */
Felipe Balbi98871e92017-01-23 14:20:04 +02002736 for (i = 1; i < 31; i++) {
Matt Evans4819fef2011-06-01 13:01:07 +10002737 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
Hans de Goededf613832013-10-04 00:29:45 +02002738 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03002739 xhci_free_endpoint_ring(xhci, virt_dev, i);
Hans de Goededf613832013-10-04 00:29:45 +02002740 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2741 }
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002742 }
John Yound115b042009-07-27 12:05:15 -07002743 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002744 /*
2745 * Install any rings for completely new endpoints or changed endpoints,
Mathias Nymanc5628a22017-06-15 11:55:42 +03002746 * and free any old rings from changed endpoints.
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002747 */
Felipe Balbi98871e92017-01-23 14:20:04 +02002748 for (i = 1; i < 31; i++) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002749 if (!virt_dev->eps[i].new_ring)
2750 continue;
Mathias Nymanc5628a22017-06-15 11:55:42 +03002751 /* Only free the old ring if it exists.
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002752 * It may not if this is the first add of an endpoint.
2753 */
2754 if (virt_dev->eps[i].ring) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03002755 xhci_free_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002756 }
Hans de Goededf613832013-10-04 00:29:45 +02002757 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002758 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2759 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002760 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002761command_cleanup:
2762 kfree(command->completion);
2763 kfree(command);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002764
Sarah Sharpf94e01862009-04-27 19:58:38 -07002765 return ret;
2766}
2767
Lu Baolu39693842017-04-07 17:57:04 +03002768static void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002769{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002770 struct xhci_hcd *xhci;
2771 struct xhci_virt_device *virt_dev;
2772 int i, ret;
2773
Andiry Xu64927732010-10-14 07:22:45 -07002774 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002775 if (ret <= 0)
2776 return;
2777 xhci = hcd_to_xhci(hcd);
2778
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002779 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002780 virt_dev = xhci->devs[udev->slot_id];
2781 /* Free any rings allocated for added endpoints */
Felipe Balbi98871e92017-01-23 14:20:04 +02002782 for (i = 0; i < 31; i++) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002783 if (virt_dev->eps[i].new_ring) {
Lu Baolu02b6fdc2017-10-05 11:21:39 +03002784 xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002785 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2786 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002787 }
2788 }
John Yound115b042009-07-27 12:05:15 -07002789 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002790}
2791
Sarah Sharp5270b952009-09-04 10:53:11 -07002792static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002793 struct xhci_container_ctx *in_ctx,
2794 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002795 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002796 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002797{
Matt Evans28ccd292011-03-29 13:40:46 +11002798 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2799 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002800 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002801 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002802}
2803
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002804static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002805 unsigned int slot_id, unsigned int ep_index,
2806 struct xhci_dequeue_state *deq_state)
2807{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002808 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002809 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002810 struct xhci_ep_ctx *ep_ctx;
2811 u32 added_ctxs;
2812 dma_addr_t addr;
2813
Sarah Sharp92f8e762013-04-23 17:11:14 -07002814 in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02002815 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002816 if (!ctrl_ctx) {
2817 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2818 __func__);
2819 return;
2820 }
2821
Sarah Sharp913a8a32009-09-04 10:53:13 -07002822 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2823 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002824 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2825 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2826 deq_state->new_deq_ptr);
2827 if (addr == 0) {
2828 xhci_warn(xhci, "WARN Cannot submit config ep after "
2829 "reset ep command\n");
2830 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2831 deq_state->new_deq_seg,
2832 deq_state->new_deq_ptr);
2833 return;
2834 }
Matt Evans28ccd292011-03-29 13:40:46 +11002835 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002836
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002837 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002838 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002839 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2840 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002841}
2842
Mathias Nymand36374f2017-06-15 11:55:47 +03002843void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
2844 unsigned int stream_id, struct xhci_td *td)
Sarah Sharp82d10092009-08-07 14:04:52 -07002845{
2846 struct xhci_dequeue_state deq_state;
Mathias Nymand97b4f82014-11-27 18:19:16 +02002847 struct usb_device *udev = td->urb->dev;
Sarah Sharp82d10092009-08-07 14:04:52 -07002848
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002849 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2850 "Cleaning up stalled endpoint ring");
Sarah Sharp82d10092009-08-07 14:04:52 -07002851 /* We need to move the HW's dequeue pointer past this TD,
2852 * or it will attempt to resend it on the next doorbell ring.
2853 */
2854 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Mathias Nymand36374f2017-06-15 11:55:47 +03002855 ep_index, stream_id, td, &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002856
Mathias Nyman365038d2014-08-19 15:17:58 +03002857 if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
2858 return;
2859
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002860 /* HW with the reset endpoint quirk will use the saved dequeue state to
2861 * issue a configure endpoint command later.
2862 */
2863 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002864 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2865 "Queueing new dequeue state");
Hans de Goede1e3452e2014-08-20 16:41:52 +03002866 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Mathias Nyman87907362017-06-02 16:36:23 +03002867 ep_index, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002868 } else {
2869 /* Better hope no one uses the input context between now and the
2870 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002871 * XXX: No idea how this hardware will react when stream rings
2872 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002873 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002874 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2875 "Setting up input context for "
2876 "configure endpoint command");
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002877 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2878 ep_index, &deq_state);
2879 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002880}
2881
Mathias Nymanf5249462018-03-16 16:33:04 +02002882/*
2883 * Called after usb core issues a clear halt control message.
2884 * The host side of the halt should already be cleared by a reset endpoint
2885 * command issued when the STALL event was received.
Mathias Nymand0167ad2015-03-10 19:49:00 +02002886 *
Mathias Nymanf5249462018-03-16 16:33:04 +02002887 * The reset endpoint command may only be issued to endpoints in the halted
2888 * state. For software that wishes to reset the data toggle or sequence number
2889 * of an endpoint that isn't in the halted state this function will issue a
2890 * configure endpoint command with the Drop and Add bits set for the target
2891 * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
Sarah Sharpa1587d92009-07-27 12:03:15 -07002892 */
Mathias Nyman8e71a322014-11-18 11:27:12 +02002893
Lu Baolu39693842017-04-07 17:57:04 +03002894static void xhci_endpoint_reset(struct usb_hcd *hcd,
Mathias Nymanf5249462018-03-16 16:33:04 +02002895 struct usb_host_endpoint *host_ep)
Sarah Sharpa1587d92009-07-27 12:03:15 -07002896{
2897 struct xhci_hcd *xhci;
Mathias Nymanf5249462018-03-16 16:33:04 +02002898 struct usb_device *udev;
2899 struct xhci_virt_device *vdev;
2900 struct xhci_virt_ep *ep;
2901 struct xhci_input_control_ctx *ctrl_ctx;
2902 struct xhci_command *stop_cmd, *cfg_cmd;
2903 unsigned int ep_index;
2904 unsigned long flags;
2905 u32 ep_flag;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002906
2907 xhci = hcd_to_xhci(hcd);
Mathias Nymanf5249462018-03-16 16:33:04 +02002908 if (!host_ep->hcpriv)
2909 return;
2910 udev = (struct usb_device *) host_ep->hcpriv;
2911 vdev = xhci->devs[udev->slot_id];
2912 ep_index = xhci_get_endpoint_index(&host_ep->desc);
2913 ep = &vdev->eps[ep_index];
2914
2915 /* Bail out if toggle is already being cleared by a endpoint reset */
2916 if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) {
2917 ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE;
2918 return;
2919 }
2920 /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */
2921 if (usb_endpoint_xfer_control(&host_ep->desc) ||
2922 usb_endpoint_xfer_isoc(&host_ep->desc))
2923 return;
2924
2925 ep_flag = xhci_get_endpoint_flag(&host_ep->desc);
2926
2927 if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG)
2928 return;
2929
2930 stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT);
2931 if (!stop_cmd)
2932 return;
2933
2934 cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT);
2935 if (!cfg_cmd)
2936 goto cleanup;
2937
2938 spin_lock_irqsave(&xhci->lock, flags);
2939
2940 /* block queuing new trbs and ringing ep doorbell */
2941 ep->ep_state |= EP_SOFT_CLEAR_TOGGLE;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002942
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002943 /*
Mathias Nymanf5249462018-03-16 16:33:04 +02002944 * Make sure endpoint ring is empty before resetting the toggle/seq.
2945 * Driver is required to synchronously cancel all transfer request.
2946 * Stop the endpoint to force xHC to update the output context
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002947 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002948
Mathias Nymanf5249462018-03-16 16:33:04 +02002949 if (!list_empty(&ep->ring->td_list)) {
2950 dev_err(&udev->dev, "EP not empty, refuse reset\n");
2951 spin_unlock_irqrestore(&xhci->lock, flags);
2952 goto cleanup;
2953 }
2954 xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id, ep_index, 0);
2955 xhci_ring_cmd_db(xhci);
2956 spin_unlock_irqrestore(&xhci->lock, flags);
2957
2958 wait_for_completion(stop_cmd->completion);
2959
2960 spin_lock_irqsave(&xhci->lock, flags);
2961
2962 /* config ep command clears toggle if add and drop ep flags are set */
2963 ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx);
2964 xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx,
2965 ctrl_ctx, ep_flag, ep_flag);
2966 xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index);
2967
2968 xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma,
2969 udev->slot_id, false);
2970 xhci_ring_cmd_db(xhci);
2971 spin_unlock_irqrestore(&xhci->lock, flags);
2972
2973 wait_for_completion(cfg_cmd->completion);
2974
2975 ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE;
2976 xhci_free_command(xhci, cfg_cmd);
2977cleanup:
2978 xhci_free_command(xhci, stop_cmd);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002979}
2980
Sarah Sharp8df75f42010-04-02 15:34:16 -07002981static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2982 struct usb_device *udev, struct usb_host_endpoint *ep,
2983 unsigned int slot_id)
2984{
2985 int ret;
2986 unsigned int ep_index;
2987 unsigned int ep_state;
2988
2989 if (!ep)
2990 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002991 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002992 if (ret <= 0)
2993 return -EINVAL;
Hans de Goedea3901532013-10-04 17:05:55 +02002994 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002995 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2996 " descriptor for ep 0x%x does not support streams\n",
2997 ep->desc.bEndpointAddress);
2998 return -EINVAL;
2999 }
3000
3001 ep_index = xhci_get_endpoint_index(&ep->desc);
3002 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3003 if (ep_state & EP_HAS_STREAMS ||
3004 ep_state & EP_GETTING_STREAMS) {
3005 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
3006 "already has streams set up.\n",
3007 ep->desc.bEndpointAddress);
3008 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
3009 "dynamic stream context array reallocation.\n");
3010 return -EINVAL;
3011 }
3012 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
3013 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
3014 "endpoint 0x%x; URBs are pending.\n",
3015 ep->desc.bEndpointAddress);
3016 return -EINVAL;
3017 }
3018 return 0;
3019}
3020
3021static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3022 unsigned int *num_streams, unsigned int *num_stream_ctxs)
3023{
3024 unsigned int max_streams;
3025
3026 /* The stream context array size must be a power of two */
3027 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3028 /*
3029 * Find out how many primary stream array entries the host controller
3030 * supports. Later we may use secondary stream arrays (similar to 2nd
3031 * level page entries), but that's an optional feature for xHCI host
3032 * controllers. xHCs must support at least 4 stream IDs.
3033 */
3034 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3035 if (*num_stream_ctxs > max_streams) {
3036 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3037 max_streams);
3038 *num_stream_ctxs = max_streams;
3039 *num_streams = max_streams;
3040 }
3041}
3042
3043/* Returns an error code if one of the endpoint already has streams.
3044 * This does not change any data structures, it only checks and gathers
3045 * information.
3046 */
3047static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3048 struct usb_device *udev,
3049 struct usb_host_endpoint **eps, unsigned int num_eps,
3050 unsigned int *num_streams, u32 *changed_ep_bitmask)
3051{
Sarah Sharp8df75f42010-04-02 15:34:16 -07003052 unsigned int max_streams;
3053 unsigned int endpoint_flag;
3054 int i;
3055 int ret;
3056
3057 for (i = 0; i < num_eps; i++) {
3058 ret = xhci_check_streams_endpoint(xhci, udev,
3059 eps[i], udev->slot_id);
3060 if (ret < 0)
3061 return ret;
3062
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003063 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003064 if (max_streams < (*num_streams - 1)) {
3065 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3066 eps[i]->desc.bEndpointAddress,
3067 max_streams);
3068 *num_streams = max_streams+1;
3069 }
3070
3071 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3072 if (*changed_ep_bitmask & endpoint_flag)
3073 return -EINVAL;
3074 *changed_ep_bitmask |= endpoint_flag;
3075 }
3076 return 0;
3077}
3078
3079static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3080 struct usb_device *udev,
3081 struct usb_host_endpoint **eps, unsigned int num_eps)
3082{
3083 u32 changed_ep_bitmask = 0;
3084 unsigned int slot_id;
3085 unsigned int ep_index;
3086 unsigned int ep_state;
3087 int i;
3088
3089 slot_id = udev->slot_id;
3090 if (!xhci->devs[slot_id])
3091 return 0;
3092
3093 for (i = 0; i < num_eps; i++) {
3094 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3095 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3096 /* Are streams already being freed for the endpoint? */
3097 if (ep_state & EP_GETTING_NO_STREAMS) {
3098 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003099 "endpoint 0x%x, "
3100 "streams are being disabled already\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003101 eps[i]->desc.bEndpointAddress);
3102 return 0;
3103 }
3104 /* Are there actually any streams to free? */
3105 if (!(ep_state & EP_HAS_STREAMS) &&
3106 !(ep_state & EP_GETTING_STREAMS)) {
3107 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003108 "endpoint 0x%x, "
3109 "streams are already disabled!\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003110 eps[i]->desc.bEndpointAddress);
3111 xhci_warn(xhci, "WARN xhci_free_streams() called "
3112 "with non-streams endpoint\n");
3113 return 0;
3114 }
3115 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3116 }
3117 return changed_ep_bitmask;
3118}
3119
3120/*
Luis de Bethencourtc2a298d2015-06-30 16:48:54 +02003121 * The USB device drivers use this function (through the HCD interface in USB
Sarah Sharp8df75f42010-04-02 15:34:16 -07003122 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3123 * coordinate mass storage command queueing across multiple endpoints (basically
3124 * a stream ID == a task ID).
3125 *
3126 * Setting up streams involves allocating the same size stream context array
3127 * for each endpoint and issuing a configure endpoint command for all endpoints.
3128 *
3129 * Don't allow the call to succeed if one endpoint only supports one stream
3130 * (which means it doesn't support streams at all).
3131 *
3132 * Drivers may get less stream IDs than they asked for, if the host controller
3133 * hardware or endpoints claim they can't support the number of requested
3134 * stream IDs.
3135 */
Lu Baolu39693842017-04-07 17:57:04 +03003136static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003137 struct usb_host_endpoint **eps, unsigned int num_eps,
3138 unsigned int num_streams, gfp_t mem_flags)
3139{
3140 int i, ret;
3141 struct xhci_hcd *xhci;
3142 struct xhci_virt_device *vdev;
3143 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003144 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003145 unsigned int ep_index;
3146 unsigned int num_stream_ctxs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003147 unsigned int max_packet;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003148 unsigned long flags;
3149 u32 changed_ep_bitmask = 0;
3150
3151 if (!eps)
3152 return -EINVAL;
3153
3154 /* Add one to the number of streams requested to account for
3155 * stream 0 that is reserved for xHCI usage.
3156 */
3157 num_streams += 1;
3158 xhci = hcd_to_xhci(hcd);
3159 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3160 num_streams);
3161
Hans de Goedef7920882013-11-15 12:14:38 +01003162 /* MaxPSASize value 0 (2 streams) means streams are not supported */
Hans de Goede8f873c12014-07-25 22:01:18 +02003163 if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
3164 HCC_MAX_PSA(xhci->hcc_params) < 4) {
Hans de Goedef7920882013-11-15 12:14:38 +01003165 xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3166 return -ENOSYS;
3167 }
3168
Mathias Nyman14d49b72017-12-08 17:59:07 +02003169 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
Lu Baolu74e0b562017-04-07 17:57:05 +03003170 if (!config_cmd)
Sarah Sharp8df75f42010-04-02 15:34:16 -07003171 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +03003172
Lin Wang4daf9df2015-01-09 16:06:31 +02003173 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003174 if (!ctrl_ctx) {
3175 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3176 __func__);
3177 xhci_free_command(xhci, config_cmd);
3178 return -ENOMEM;
3179 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003180
3181 /* Check to make sure all endpoints are not already configured for
3182 * streams. While we're at it, find the maximum number of streams that
3183 * all the endpoints will support and check for duplicate endpoints.
3184 */
3185 spin_lock_irqsave(&xhci->lock, flags);
3186 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3187 num_eps, &num_streams, &changed_ep_bitmask);
3188 if (ret < 0) {
3189 xhci_free_command(xhci, config_cmd);
3190 spin_unlock_irqrestore(&xhci->lock, flags);
3191 return ret;
3192 }
3193 if (num_streams <= 1) {
3194 xhci_warn(xhci, "WARN: endpoints can't handle "
3195 "more than one stream.\n");
3196 xhci_free_command(xhci, config_cmd);
3197 spin_unlock_irqrestore(&xhci->lock, flags);
3198 return -EINVAL;
3199 }
3200 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003201 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003202 * xhci_urb_enqueue() will reject all URBs.
3203 */
3204 for (i = 0; i < num_eps; i++) {
3205 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3206 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3207 }
3208 spin_unlock_irqrestore(&xhci->lock, flags);
3209
3210 /* Setup internal data structures and allocate HW data structures for
3211 * streams (but don't install the HW structures in the input context
3212 * until we're sure all memory allocation succeeded).
3213 */
3214 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3215 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3216 num_stream_ctxs, num_streams);
3217
3218 for (i = 0; i < num_eps; i++) {
3219 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
Felipe Balbi734d3dd2016-09-28 13:46:37 +03003220 max_packet = usb_endpoint_maxp(&eps[i]->desc);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003221 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3222 num_stream_ctxs,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003223 num_streams,
3224 max_packet, mem_flags);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003225 if (!vdev->eps[ep_index].stream_info)
3226 goto cleanup;
3227 /* Set maxPstreams in endpoint context and update deq ptr to
3228 * point to stream context array. FIXME
3229 */
3230 }
3231
3232 /* Set up the input context for a configure endpoint command. */
3233 for (i = 0; i < num_eps; i++) {
3234 struct xhci_ep_ctx *ep_ctx;
3235
3236 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3237 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3238
3239 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3240 vdev->out_ctx, ep_index);
3241 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3242 vdev->eps[ep_index].stream_info);
3243 }
3244 /* Tell the HW to drop its old copy of the endpoint context info
3245 * and add the updated copy from the input context.
3246 */
3247 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003248 vdev->out_ctx, ctrl_ctx,
3249 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003250
3251 /* Issue and wait for the configure endpoint command */
3252 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3253 false, false);
3254
3255 /* xHC rejected the configure endpoint command for some reason, so we
3256 * leave the old ring intact and free our internal streams data
3257 * structure.
3258 */
3259 if (ret < 0)
3260 goto cleanup;
3261
3262 spin_lock_irqsave(&xhci->lock, flags);
3263 for (i = 0; i < num_eps; i++) {
3264 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3265 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3266 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3267 udev->slot_id, ep_index);
3268 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3269 }
3270 xhci_free_command(xhci, config_cmd);
3271 spin_unlock_irqrestore(&xhci->lock, flags);
3272
3273 /* Subtract 1 for stream 0, which drivers can't use */
3274 return num_streams - 1;
3275
3276cleanup:
3277 /* If it didn't work, free the streams! */
3278 for (i = 0; i < num_eps; i++) {
3279 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3280 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003281 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003282 /* FIXME Unset maxPstreams in endpoint context and
3283 * update deq ptr to point to normal string ring.
3284 */
3285 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3286 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3287 xhci_endpoint_zero(xhci, vdev, eps[i]);
3288 }
3289 xhci_free_command(xhci, config_cmd);
3290 return -ENOMEM;
3291}
3292
3293/* Transition the endpoint from using streams to being a "normal" endpoint
3294 * without streams.
3295 *
3296 * Modify the endpoint context state, submit a configure endpoint command,
3297 * and free all endpoint rings for streams if that completes successfully.
3298 */
Lu Baolu39693842017-04-07 17:57:04 +03003299static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003300 struct usb_host_endpoint **eps, unsigned int num_eps,
3301 gfp_t mem_flags)
3302{
3303 int i, ret;
3304 struct xhci_hcd *xhci;
3305 struct xhci_virt_device *vdev;
3306 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003307 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003308 unsigned int ep_index;
3309 unsigned long flags;
3310 u32 changed_ep_bitmask;
3311
3312 xhci = hcd_to_xhci(hcd);
3313 vdev = xhci->devs[udev->slot_id];
3314
3315 /* Set up a configure endpoint command to remove the streams rings */
3316 spin_lock_irqsave(&xhci->lock, flags);
3317 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3318 udev, eps, num_eps);
3319 if (changed_ep_bitmask == 0) {
3320 spin_unlock_irqrestore(&xhci->lock, flags);
3321 return -EINVAL;
3322 }
3323
3324 /* Use the xhci_command structure from the first endpoint. We may have
3325 * allocated too many, but the driver may call xhci_free_streams() for
3326 * each endpoint it grouped into one call to xhci_alloc_streams().
3327 */
3328 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3329 command = vdev->eps[ep_index].stream_info->free_streams_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02003330 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003331 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003332 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003333 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3334 __func__);
3335 return -EINVAL;
3336 }
3337
Sarah Sharp8df75f42010-04-02 15:34:16 -07003338 for (i = 0; i < num_eps; i++) {
3339 struct xhci_ep_ctx *ep_ctx;
3340
3341 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3342 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3343 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3344 EP_GETTING_NO_STREAMS;
3345
3346 xhci_endpoint_copy(xhci, command->in_ctx,
3347 vdev->out_ctx, ep_index);
Lin Wang4daf9df2015-01-09 16:06:31 +02003348 xhci_setup_no_streams_ep_input_ctx(ep_ctx,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003349 &vdev->eps[ep_index]);
3350 }
3351 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003352 vdev->out_ctx, ctrl_ctx,
3353 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003354 spin_unlock_irqrestore(&xhci->lock, flags);
3355
3356 /* Issue and wait for the configure endpoint command,
3357 * which must succeed.
3358 */
3359 ret = xhci_configure_endpoint(xhci, udev, command,
3360 false, true);
3361
3362 /* xHC rejected the configure endpoint command for some reason, so we
3363 * leave the streams rings intact.
3364 */
3365 if (ret < 0)
3366 return ret;
3367
3368 spin_lock_irqsave(&xhci->lock, flags);
3369 for (i = 0; i < num_eps; i++) {
3370 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3371 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003372 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003373 /* FIXME Unset maxPstreams in endpoint context and
3374 * update deq ptr to point to normal string ring.
3375 */
3376 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3377 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3378 }
3379 spin_unlock_irqrestore(&xhci->lock, flags);
3380
3381 return 0;
3382}
3383
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003384/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003385 * Deletes endpoint resources for endpoints that were active before a Reset
3386 * Device command, or a Disable Slot command. The Reset Device command leaves
3387 * the control endpoint intact, whereas the Disable Slot command deletes it.
3388 *
3389 * Must be called with xhci->lock held.
3390 */
3391void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3392 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3393{
3394 int i;
3395 unsigned int num_dropped_eps = 0;
3396 unsigned int drop_flags = 0;
3397
3398 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3399 if (virt_dev->eps[i].ring) {
3400 drop_flags |= 1 << i;
3401 num_dropped_eps++;
3402 }
3403 }
3404 xhci->num_active_eps -= num_dropped_eps;
3405 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003406 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3407 "Dropped %u ep ctxs, flags = 0x%x, "
3408 "%u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003409 num_dropped_eps, drop_flags,
3410 xhci->num_active_eps);
3411}
3412
3413/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003414 * This submits a Reset Device Command, which will set the device state to 0,
3415 * set the device address to 0, and disable all the endpoints except the default
3416 * control endpoint. The USB core should come back and call
3417 * xhci_address_device(), and then re-set up the configuration. If this is
3418 * called because of a usb_reset_and_verify_device(), then the old alternate
3419 * settings will be re-installed through the normal bandwidth allocation
3420 * functions.
3421 *
3422 * Wait for the Reset Device command to finish. Remove all structures
3423 * associated with the endpoints that were disabled. Clear the input device
Mathias Nymanc5628a22017-06-15 11:55:42 +03003424 * structure? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003425 *
3426 * If the virt_dev to be reset does not exist or does not match the udev,
3427 * it means the device is lost, possibly due to the xHC restore error and
3428 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3429 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003430 */
Lu Baolu39693842017-04-07 17:57:04 +03003431static int xhci_discover_or_reset_device(struct usb_hcd *hcd,
3432 struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003433{
3434 int ret, i;
3435 unsigned long flags;
3436 struct xhci_hcd *xhci;
3437 unsigned int slot_id;
3438 struct xhci_virt_device *virt_dev;
3439 struct xhci_command *reset_device_cmd;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003440 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003441 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003442
Andiry Xuf0615c42010-10-14 07:22:48 -07003443 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003444 if (ret <= 0)
3445 return ret;
3446 xhci = hcd_to_xhci(hcd);
3447 slot_id = udev->slot_id;
3448 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003449 if (!virt_dev) {
3450 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3451 "not exist. Re-allocate the device\n", slot_id);
3452 ret = xhci_alloc_dev(hcd, udev);
3453 if (ret == 1)
3454 return 0;
3455 else
3456 return -EINVAL;
3457 }
3458
Brian Campbell326124a2015-07-21 17:20:28 +03003459 if (virt_dev->tt_info)
3460 old_active_eps = virt_dev->tt_info->active_eps;
3461
Andiry Xuf0615c42010-10-14 07:22:48 -07003462 if (virt_dev->udev != udev) {
3463 /* If the virt_dev and the udev does not match, this virt_dev
3464 * may belong to another udev.
3465 * Re-allocate the device.
3466 */
3467 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3468 "not match the udev. Re-allocate the device\n",
3469 slot_id);
3470 ret = xhci_alloc_dev(hcd, udev);
3471 if (ret == 1)
3472 return 0;
3473 else
3474 return -EINVAL;
3475 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003476
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003477 /* If device is not setup, there is no point in resetting it */
3478 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3479 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3480 SLOT_STATE_DISABLED)
3481 return 0;
3482
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003483 trace_xhci_discover_or_reset_device(slot_ctx);
3484
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003485 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3486 /* Allocate the command structure that holds the struct completion.
3487 * Assume we're in process context, since the normal device reset
3488 * process has to wait for the device anyway. Storage devices are
3489 * reset as part of error handling, so use GFP_NOIO instead of
3490 * GFP_KERNEL.
3491 */
Mathias Nyman103afda2017-12-08 17:59:08 +02003492 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003493 if (!reset_device_cmd) {
3494 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3495 return -ENOMEM;
3496 }
3497
3498 /* Attempt to submit the Reset Device command to the command ring */
3499 spin_lock_irqsave(&xhci->lock, flags);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003500
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003501 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003502 if (ret) {
3503 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003504 spin_unlock_irqrestore(&xhci->lock, flags);
3505 goto command_cleanup;
3506 }
3507 xhci_ring_cmd_db(xhci);
3508 spin_unlock_irqrestore(&xhci->lock, flags);
3509
3510 /* Wait for the Reset Device command to finish */
Mathias Nymanc311e392014-05-08 19:26:03 +03003511 wait_for_completion(reset_device_cmd->completion);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003512
3513 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3514 * unless we tried to reset a slot ID that wasn't enabled,
3515 * or the device wasn't in the addressed or configured state.
3516 */
3517 ret = reset_device_cmd->status;
3518 switch (ret) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003519 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03003520 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03003521 xhci_warn(xhci, "Timeout waiting for reset device command\n");
3522 ret = -ETIME;
3523 goto command_cleanup;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003524 case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */
3525 case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003526 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003527 slot_id,
3528 xhci_get_slot_state(xhci, virt_dev->out_ctx));
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003529 xhci_dbg(xhci, "Not freeing device rings.\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003530 /* Don't treat this as an error. May change my mind later. */
3531 ret = 0;
3532 goto command_cleanup;
3533 case COMP_SUCCESS:
3534 xhci_dbg(xhci, "Successful reset device command.\n");
3535 break;
3536 default:
3537 if (xhci_is_vendor_info_code(xhci, ret))
3538 break;
3539 xhci_warn(xhci, "Unknown completion code %u for "
3540 "reset device command.\n", ret);
3541 ret = -EINVAL;
3542 goto command_cleanup;
3543 }
3544
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003545 /* Free up host controller endpoint resources */
3546 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3547 spin_lock_irqsave(&xhci->lock, flags);
3548 /* Don't delete the default control endpoint resources */
3549 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3550 spin_unlock_irqrestore(&xhci->lock, flags);
3551 }
3552
Mathias Nymanc5628a22017-06-15 11:55:42 +03003553 /* Everything but endpoint 0 is disabled, so free the rings. */
Felipe Balbi98871e92017-01-23 14:20:04 +02003554 for (i = 1; i < 31; i++) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003555 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3556
3557 if (ep->ep_state & EP_HAS_STREAMS) {
Hans de Goededf613832013-10-04 00:29:45 +02003558 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3559 xhci_get_endpoint_address(i));
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003560 xhci_free_stream_info(xhci, ep->stream_info);
3561 ep->stream_info = NULL;
3562 ep->ep_state &= ~EP_HAS_STREAMS;
3563 }
3564
3565 if (ep->ring) {
Lu Baolu02b6fdc2017-10-05 11:21:39 +03003566 xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
Mathias Nymanc5628a22017-06-15 11:55:42 +03003567 xhci_free_endpoint_ring(xhci, virt_dev, i);
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003568 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003569 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3570 xhci_drop_ep_from_interval_table(xhci,
3571 &virt_dev->eps[i].bw_info,
3572 virt_dev->bw_table,
3573 udev,
3574 &virt_dev->eps[i],
3575 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003576 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003577 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003578 /* If necessary, update the number of active TTs on this root port */
3579 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003580 ret = 0;
3581
3582command_cleanup:
3583 xhci_free_command(xhci, reset_device_cmd);
3584 return ret;
3585}
3586
3587/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003588 * At this point, the struct usb_device is about to go away, the device has
3589 * disconnected, and all traffic has been stopped and the endpoints have been
3590 * disabled. Free any HC data structures associated with that device.
3591 */
Lu Baolu39693842017-04-07 17:57:04 +03003592static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003593{
3594 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003595 struct xhci_virt_device *virt_dev;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003596 struct xhci_slot_ctx *slot_ctx;
Andiry Xu64927732010-10-14 07:22:45 -07003597 int i, ret;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003598
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003599#ifndef CONFIG_USB_DEFAULT_PERSIST
3600 /*
3601 * We called pm_runtime_get_noresume when the device was attached.
3602 * Decrement the counter here to allow controller to runtime suspend
3603 * if no devices remain.
3604 */
3605 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003606 pm_runtime_put_noidle(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003607#endif
3608
Andiry Xu64927732010-10-14 07:22:45 -07003609 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003610 /* If the host is halted due to driver unload, we still need to free the
3611 * device.
3612 */
Lu Baolucd3f1792017-10-05 11:21:41 +03003613 if (ret <= 0 && ret != -ENODEV)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003614 return;
Andiry Xu64927732010-10-14 07:22:45 -07003615
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003616 virt_dev = xhci->devs[udev->slot_id];
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003617 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3618 trace_xhci_free_dev(slot_ctx);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003619
3620 /* Stop any wayward timer functions (which may grab the lock) */
Felipe Balbi98871e92017-01-23 14:20:04 +02003621 for (i = 0; i < 31; i++) {
Mathias Nyman9983a5f2017-01-23 14:19:52 +02003622 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003623 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3624 }
Zhengjun Xing8c5a93e2018-02-12 14:24:50 +02003625 xhci_debugfs_remove_slot(xhci, udev->slot_id);
Mathias Nyman44a182b2018-05-03 17:30:07 +03003626 virt_dev->udev = NULL;
Lu Baolu11ec7582017-10-05 11:21:42 +03003627 ret = xhci_disable_slot(xhci, udev->slot_id);
Zhengjun Xing8c5a93e2018-02-12 14:24:50 +02003628 if (ret)
Lu Baolu11ec7582017-10-05 11:21:42 +03003629 xhci_free_virt_device(xhci, udev->slot_id);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003630}
3631
Lu Baolucd3f1792017-10-05 11:21:41 +03003632int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id)
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003633{
Lu Baolucd3f1792017-10-05 11:21:41 +03003634 struct xhci_command *command;
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003635 unsigned long flags;
3636 u32 state;
3637 int ret = 0;
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003638
Mathias Nyman103afda2017-12-08 17:59:08 +02003639 command = xhci_alloc_command(xhci, false, GFP_KERNEL);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003640 if (!command)
3641 return -ENOMEM;
3642
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003643 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003644 /* Don't disable the slot if the host controller is dead. */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02003645 state = readl(&xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003646 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3647 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003648 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003649 kfree(command);
Lu Baoludcabc76f2017-10-05 11:21:43 +03003650 return -ENODEV;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003651 }
3652
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003653 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3654 slot_id);
3655 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003656 spin_unlock_irqrestore(&xhci->lock, flags);
Lu Baolucd3f1792017-10-05 11:21:41 +03003657 kfree(command);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003658 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003659 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003660 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003661 spin_unlock_irqrestore(&xhci->lock, flags);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003662 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003663}
3664
3665/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003666 * Checks if we have enough host controller resources for the default control
3667 * endpoint.
3668 *
3669 * Must be called with xhci->lock held.
3670 */
3671static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3672{
3673 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003674 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3675 "Not enough ep ctxs: "
3676 "%u active, need to add 1, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003677 xhci->num_active_eps, xhci->limit_active_eps);
3678 return -ENOMEM;
3679 }
3680 xhci->num_active_eps += 1;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003681 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3682 "Adding 1 ep ctx, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003683 xhci->num_active_eps);
3684 return 0;
3685}
3686
3687
3688/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003689 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3690 * timed out, or allocating memory failed. Returns 1 on success.
3691 */
3692int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3693{
3694 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003695 struct xhci_virt_device *vdev;
3696 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003697 unsigned long flags;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003698 int ret, slot_id;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003699 struct xhci_command *command;
3700
Mathias Nyman103afda2017-12-08 17:59:08 +02003701 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003702 if (!command)
3703 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003704
3705 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003706 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003707 if (ret) {
3708 spin_unlock_irqrestore(&xhci->lock, flags);
3709 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Lu Baolu87e44f22016-11-11 15:13:30 +02003710 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003711 return 0;
3712 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003713 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003714 spin_unlock_irqrestore(&xhci->lock, flags);
3715
Mathias Nymanc311e392014-05-08 19:26:03 +03003716 wait_for_completion(command->completion);
Lu Baoluc2d3d492016-11-11 15:13:31 +02003717 slot_id = command->slot_id;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003718
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003719 if (!slot_id || command->status != COMP_SUCCESS) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003720 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharpbe982032014-05-08 19:25:59 +03003721 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
3722 HCS_MAX_SLOTS(
3723 readl(&xhci->cap_regs->hcs_params1)));
Lu Baolu87e44f22016-11-11 15:13:30 +02003724 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003725 return 0;
3726 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003727
Lu Baolucd3f1792017-10-05 11:21:41 +03003728 xhci_free_command(xhci, command);
3729
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003730 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3731 spin_lock_irqsave(&xhci->lock, flags);
3732 ret = xhci_reserve_host_control_ep_resources(xhci);
3733 if (ret) {
3734 spin_unlock_irqrestore(&xhci->lock, flags);
3735 xhci_warn(xhci, "Not enough host resources, "
3736 "active endpoint contexts = %u\n",
3737 xhci->num_active_eps);
3738 goto disable_slot;
3739 }
3740 spin_unlock_irqrestore(&xhci->lock, flags);
3741 }
3742 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003743 * xhci_discover_or_reset_device(), which may be called as part of
3744 * mass storage driver error handling.
3745 */
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003746 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003747 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003748 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003749 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003750 vdev = xhci->devs[slot_id];
3751 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
3752 trace_xhci_alloc_dev(slot_ctx);
3753
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003754 udev->slot_id = slot_id;
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003755
Lu Baolu02b6fdc2017-10-05 11:21:39 +03003756 xhci_debugfs_create_slot(xhci, slot_id);
3757
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003758#ifndef CONFIG_USB_DEFAULT_PERSIST
3759 /*
3760 * If resetting upon resume, we can't put the controller into runtime
3761 * suspend if there is a device attached.
3762 */
3763 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003764 pm_runtime_get_noresume(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003765#endif
3766
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003767 /* Is this a LS or FS device under a HS hub? */
3768 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003769 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003770
3771disable_slot:
Lu Baolu11ec7582017-10-05 11:21:42 +03003772 ret = xhci_disable_slot(xhci, udev->slot_id);
3773 if (ret)
3774 xhci_free_virt_device(xhci, udev->slot_id);
3775
3776 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003777}
3778
3779/*
Dan Williams48fc7db2013-12-05 17:07:27 -08003780 * Issue an Address Device command and optionally send a corresponding
3781 * SetAddress request to the device.
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003782 */
Dan Williams48fc7db2013-12-05 17:07:27 -08003783static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
3784 enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003785{
Dan Williams6f8ffc02013-11-22 01:20:01 -08003786 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003787 unsigned long flags;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003788 struct xhci_virt_device *virt_dev;
3789 int ret = 0;
3790 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003791 struct xhci_slot_ctx *slot_ctx;
3792 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003793 u64 temp_64;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003794 struct xhci_command *command = NULL;
3795
3796 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003797
Lu Baolu90797ae2017-01-03 18:28:44 +02003798 if (xhci->xhc_state) { /* dying, removing or halted */
3799 ret = -ESHUTDOWN;
Roger Quadros448116b2015-09-21 17:46:15 +03003800 goto out;
Lu Baolu90797ae2017-01-03 18:28:44 +02003801 }
Roger Quadros448116b2015-09-21 17:46:15 +03003802
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003803 if (!udev->slot_id) {
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003804 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3805 "Bad Slot ID %d", udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003806 ret = -EINVAL;
3807 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003808 }
3809
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003810 virt_dev = xhci->devs[udev->slot_id];
3811
Matt Evans7ed603e2011-03-29 13:40:56 +11003812 if (WARN_ON(!virt_dev)) {
3813 /*
3814 * In plug/unplug torture test with an NEC controller,
3815 * a zero-dereference was observed once due to virt_dev = 0.
3816 * Print useful debug rather than crash if it is observed again!
3817 */
3818 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3819 udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003820 ret = -EINVAL;
3821 goto out;
Matt Evans7ed603e2011-03-29 13:40:56 +11003822 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003823 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3824 trace_xhci_setup_device_slot(slot_ctx);
Matt Evans7ed603e2011-03-29 13:40:56 +11003825
Mathias Nymanf161ead2015-01-09 17:18:28 +02003826 if (setup == SETUP_CONTEXT_ONLY) {
Mathias Nymanf161ead2015-01-09 17:18:28 +02003827 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3828 SLOT_STATE_DEFAULT) {
3829 xhci_dbg(xhci, "Slot already in default state\n");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003830 goto out;
Mathias Nymanf161ead2015-01-09 17:18:28 +02003831 }
3832 }
3833
Mathias Nyman103afda2017-12-08 17:59:08 +02003834 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003835 if (!command) {
3836 ret = -ENOMEM;
3837 goto out;
3838 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003839
3840 command->in_ctx = virt_dev->in_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003841
Andiry Xuf0615c42010-10-14 07:22:48 -07003842 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Lin Wang4daf9df2015-01-09 16:06:31 +02003843 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003844 if (!ctrl_ctx) {
3845 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3846 __func__);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003847 ret = -EINVAL;
3848 goto out;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003849 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003850 /*
3851 * If this is the first Set Address since device plug-in or
3852 * virt_device realloaction after a resume with an xHCI power loss,
3853 * then set up the slot context.
3854 */
3855 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003856 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003857 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003858 else
3859 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003860 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3861 ctrl_ctx->drop_flags = 0;
3862
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003863 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003864 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003865
Sarah Sharpf88ba782009-05-14 11:44:22 -07003866 spin_lock_irqsave(&xhci->lock, flags);
Felipe Balbia711ede2017-01-23 14:20:23 +02003867 trace_xhci_setup_device(virt_dev);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003868 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
Dan Williams48fc7db2013-12-05 17:07:27 -08003869 udev->slot_id, setup);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003870 if (ret) {
3871 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003872 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3873 "FIXME: allocate a command ring segment");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003874 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003875 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003876 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003877 spin_unlock_irqrestore(&xhci->lock, flags);
3878
3879 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
Mathias Nymanc311e392014-05-08 19:26:03 +03003880 wait_for_completion(command->completion);
3881
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003882 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3883 * the SetAddress() "recovery interval" required by USB and aborting the
3884 * command on a timeout.
3885 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03003886 switch (command->status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003887 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03003888 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03003889 xhci_warn(xhci, "Timeout while waiting for setup device command\n");
3890 ret = -ETIME;
3891 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003892 case COMP_CONTEXT_STATE_ERROR:
3893 case COMP_SLOT_NOT_ENABLED_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003894 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
3895 act, udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003896 ret = -EINVAL;
3897 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003898 case COMP_USB_TRANSACTION_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003899 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
Lu Baolu651aaf32017-10-05 11:21:45 +03003900
3901 mutex_unlock(&xhci->mutex);
3902 ret = xhci_disable_slot(xhci, udev->slot_id);
3903 if (!ret)
3904 xhci_alloc_dev(hcd, udev);
3905 kfree(command->completion);
3906 kfree(command);
3907 return -EPROTO;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003908 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003909 dev_warn(&udev->dev,
3910 "ERROR: Incompatible device for setup %s command\n", act);
Alex Hef6ba6fe2011-06-08 18:34:06 +08003911 ret = -ENODEV;
3912 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003913 case COMP_SUCCESS:
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003914 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williams6f8ffc02013-11-22 01:20:01 -08003915 "Successful setup %s command", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003916 break;
3917 default:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003918 xhci_err(xhci,
3919 "ERROR: unexpected setup %s command completion code 0x%x.\n",
Mathias Nyman9ea18332014-05-08 19:26:02 +03003920 act, command->status);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003921 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003922 ret = -EINVAL;
3923 break;
3924 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003925 if (ret)
3926 goto out;
Sarah Sharpf7b2e402014-01-30 13:27:49 -08003927 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003928 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3929 "Op regs DCBAA ptr = %#016llx", temp_64);
3930 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3931 "Slot ID %d dcbaa entry @%p = %#016llx",
3932 udev->slot_id,
3933 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3934 (unsigned long long)
3935 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3936 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3937 "Output Context DMA address = %#08llx",
John Yound115b042009-07-27 12:05:15 -07003938 (unsigned long long)virt_dev->out_ctx->dma);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003939 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003940 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003941 /*
3942 * USB core uses address 1 for the roothubs, so we add one to the
3943 * address given back to us by the HC.
3944 */
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003945 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003946 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003947 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003948 ctrl_ctx->add_flags = 0;
3949 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003950
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003951 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williamsa2cdc342013-10-16 12:25:44 -07003952 "Internal device address = %d",
3953 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003954out:
3955 mutex_unlock(&xhci->mutex);
Lu Baolu87e44f22016-11-11 15:13:30 +02003956 if (command) {
3957 kfree(command->completion);
3958 kfree(command);
3959 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003960 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003961}
3962
Lu Baolu39693842017-04-07 17:57:04 +03003963static int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
Dan Williams48fc7db2013-12-05 17:07:27 -08003964{
3965 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
3966}
3967
Lu Baolu39693842017-04-07 17:57:04 +03003968static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
Dan Williams48fc7db2013-12-05 17:07:27 -08003969{
3970 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
3971}
3972
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003973/*
3974 * Transfer the port index into real index in the HW port status
3975 * registers. Caculate offset between the port's PORTSC register
3976 * and port status base. Divide the number of per port register
3977 * to get the real index. The raw port number bases 1.
3978 */
3979int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3980{
Mathias Nyman38986ff2018-05-21 16:40:01 +03003981 struct xhci_hub *rhub;
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003982
Mathias Nyman38986ff2018-05-21 16:40:01 +03003983 rhub = xhci_get_rhub(hcd);
3984 return rhub->ports[port1 - 1]->hw_portnum + 1;
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003985}
3986
Mathias Nymana558ccd2013-05-23 17:14:30 +03003987/*
3988 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3989 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3990 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07003991static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03003992 struct usb_device *udev, u16 max_exit_latency)
3993{
3994 struct xhci_virt_device *virt_dev;
3995 struct xhci_command *command;
3996 struct xhci_input_control_ctx *ctrl_ctx;
3997 struct xhci_slot_ctx *slot_ctx;
3998 unsigned long flags;
3999 int ret;
4000
4001 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nyman96044692014-09-11 13:55:50 +03004002
4003 virt_dev = xhci->devs[udev->slot_id];
4004
4005 /*
4006 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4007 * xHC was re-initialized. Exit latency will be set later after
4008 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4009 */
4010
4011 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004012 spin_unlock_irqrestore(&xhci->lock, flags);
4013 return 0;
4014 }
4015
4016 /* Attempt to issue an Evaluate Context command to change the MEL. */
Mathias Nymana558ccd2013-05-23 17:14:30 +03004017 command = xhci->lpm_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02004018 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004019 if (!ctrl_ctx) {
4020 spin_unlock_irqrestore(&xhci->lock, flags);
4021 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4022 __func__);
4023 return -ENOMEM;
4024 }
4025
Mathias Nymana558ccd2013-05-23 17:14:30 +03004026 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
4027 spin_unlock_irqrestore(&xhci->lock, flags);
4028
Mathias Nymana558ccd2013-05-23 17:14:30 +03004029 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4030 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
4031 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
4032 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
Mathias Nyman4801d4ea2014-11-27 18:19:15 +02004033 slot_ctx->dev_state = 0;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004034
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03004035 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
4036 "Set up evaluate context for LPM MEL change.");
Mathias Nymana558ccd2013-05-23 17:14:30 +03004037
4038 /* Issue and wait for the evaluate context command. */
4039 ret = xhci_configure_endpoint(xhci, udev, command,
4040 true, true);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004041
4042 if (!ret) {
4043 spin_lock_irqsave(&xhci->lock, flags);
4044 virt_dev->current_mel = max_exit_latency;
4045 spin_unlock_irqrestore(&xhci->lock, flags);
4046 }
4047 return ret;
4048}
4049
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004050#ifdef CONFIG_PM
Andiry Xu95743232011-09-23 14:19:51 -07004051
4052/* BESL to HIRD Encoding array for USB2 LPM */
4053static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4054 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4055
4056/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08004057static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4058 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07004059{
Andiry Xuf99298b2011-12-12 16:45:28 +08004060 int u2del, besl, besl_host;
4061 int besl_device = 0;
4062 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07004063
Andiry Xuf99298b2011-12-12 16:45:28 +08004064 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4065 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4066
4067 if (field & USB_BESL_SUPPORT) {
4068 for (besl_host = 0; besl_host < 16; besl_host++) {
4069 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07004070 break;
4071 }
Andiry Xuf99298b2011-12-12 16:45:28 +08004072 /* Use baseline BESL value as default */
4073 if (field & USB_BESL_BASELINE_VALID)
4074 besl_device = USB_GET_BESL_BASELINE(field);
4075 else if (field & USB_BESL_DEEP_VALID)
4076 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07004077 } else {
4078 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08004079 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07004080 else
Andiry Xuf99298b2011-12-12 16:45:28 +08004081 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07004082 }
4083
Andiry Xuf99298b2011-12-12 16:45:28 +08004084 besl = besl_host + besl_device;
4085 if (besl > 15)
4086 besl = 15;
4087
4088 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07004089}
4090
Mathias Nymana558ccd2013-05-23 17:14:30 +03004091/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4092static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4093{
4094 u32 field;
4095 int l1;
4096 int besld = 0;
4097 int hirdm = 0;
4098
4099 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4100
4101 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004102 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004103
4104 /* device has preferred BESLD */
4105 if (field & USB_BESL_DEEP_VALID) {
4106 besld = USB_GET_BESL_DEEP(field);
4107 hirdm = 1;
4108 }
4109
4110 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4111}
4112
Lu Baolu39693842017-04-07 17:57:04 +03004113static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
Andiry Xu65580b432011-09-23 14:19:52 -07004114 struct usb_device *udev, int enable)
4115{
4116 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nyman38986ff2018-05-21 16:40:01 +03004117 struct xhci_port **ports;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004118 __le32 __iomem *pm_addr, *hlpm_addr;
4119 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004120 unsigned int port_num;
4121 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004122 int hird, exit_latency;
4123 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004124
Mathias Nymanb50107b2015-10-01 18:40:38 +03004125 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
Andiry Xu65580b432011-09-23 14:19:52 -07004126 !udev->lpm_capable)
4127 return -EPERM;
4128
4129 if (!udev->parent || udev->parent->parent ||
4130 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4131 return -EPERM;
4132
4133 if (udev->usb2_hw_lpm_capable != 1)
4134 return -EPERM;
4135
4136 spin_lock_irqsave(&xhci->lock, flags);
4137
Mathias Nyman38986ff2018-05-21 16:40:01 +03004138 ports = xhci->usb2_rhub.ports;
Andiry Xu65580b432011-09-23 14:19:52 -07004139 port_num = udev->portnum - 1;
Mathias Nyman38986ff2018-05-21 16:40:01 +03004140 pm_addr = ports[port_num]->addr + PORTPMSC;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004141 pm_val = readl(pm_addr);
Mathias Nyman38986ff2018-05-21 16:40:01 +03004142 hlpm_addr = ports[port_num]->addr + PORTHLPMC;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004143 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004144
4145 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
Lin Wang654a55d2014-05-08 19:25:54 +03004146 enable ? "enable" : "disable", port_num + 1);
Andiry Xu65580b432011-09-23 14:19:52 -07004147
Thang Q. Nguyen4750bc72017-10-05 11:21:37 +03004148 if (enable && !(xhci->quirks & XHCI_HW_LPM_DISABLE)) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004149 /* Host supports BESL timeout instead of HIRD */
4150 if (udev->usb2_hw_lpm_besl_capable) {
4151 /* if device doesn't have a preferred BESL value use a
4152 * default one which works with mixed HIRD and BESL
4153 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4154 */
4155 if ((field & USB_BESL_SUPPORT) &&
4156 (field & USB_BESL_BASELINE_VALID))
4157 hird = USB_GET_BESL_BASELINE(field);
4158 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004159 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004160
4161 exit_latency = xhci_besl_encoding[hird];
4162 spin_unlock_irqrestore(&xhci->lock, flags);
4163
4164 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4165 * input context for link powermanagement evaluate
4166 * context commands. It is protected by hcd->bandwidth
4167 * mutex and is shared by all devices. We need to set
4168 * the max ext latency in USB 2 BESL LPM as well, so
4169 * use the same mutex and xhci_change_max_exit_latency()
4170 */
4171 mutex_lock(hcd->bandwidth_mutex);
4172 ret = xhci_change_max_exit_latency(xhci, udev,
4173 exit_latency);
4174 mutex_unlock(hcd->bandwidth_mutex);
4175
4176 if (ret < 0)
4177 return ret;
4178 spin_lock_irqsave(&xhci->lock, flags);
4179
4180 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004181 writel(hlpm_val, hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004182 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004183 readl(hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004184 } else {
4185 hird = xhci_calculate_hird_besl(xhci, udev);
4186 }
4187
4188 pm_val &= ~PORT_HIRD_MASK;
Sarah Sharp58e21f72013-10-07 17:17:20 -07004189 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004190 writel(pm_val, pm_addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004191 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004192 pm_val |= PORT_HLE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004193 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004194 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004195 readl(pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004196 } else {
Sarah Sharp58e21f72013-10-07 17:17:20 -07004197 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004198 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004199 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004200 readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004201 if (udev->usb2_hw_lpm_besl_capable) {
4202 spin_unlock_irqrestore(&xhci->lock, flags);
4203 mutex_lock(hcd->bandwidth_mutex);
4204 xhci_change_max_exit_latency(xhci, udev, 0);
4205 mutex_unlock(hcd->bandwidth_mutex);
4206 return 0;
4207 }
Andiry Xu65580b432011-09-23 14:19:52 -07004208 }
4209
4210 spin_unlock_irqrestore(&xhci->lock, flags);
4211 return 0;
4212}
4213
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004214/* check if a usb2 port supports a given extened capability protocol
4215 * only USB2 ports extended protocol capability values are cached.
4216 * Return 1 if capability is supported
4217 */
4218static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4219 unsigned capability)
4220{
4221 u32 port_offset, port_count;
4222 int i;
4223
4224 for (i = 0; i < xhci->num_ext_caps; i++) {
4225 if (xhci->ext_caps[i] & capability) {
4226 /* port offsets starts at 1 */
4227 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4228 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4229 if (port >= port_offset &&
4230 port < port_offset + port_count)
4231 return 1;
4232 }
4233 }
4234 return 0;
4235}
4236
Lu Baolu39693842017-04-07 17:57:04 +03004237static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004238{
4239 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004240 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004241
Mathias Nymanb50107b2015-10-01 18:40:38 +03004242 if (hcd->speed >= HCD_USB3 || !xhci->sw_lpm_support ||
Sarah Sharpde68bab2013-09-30 17:26:28 +03004243 !udev->lpm_capable)
4244 return 0;
4245
4246 /* we only support lpm for non-hub device connected to root hub yet */
4247 if (!udev->parent || udev->parent->parent ||
4248 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4249 return 0;
4250
4251 if (xhci->hw_lpm_support == 1 &&
4252 xhci_check_usb2_port_capability(
4253 xhci, portnum, XHCI_HLC)) {
4254 udev->usb2_hw_lpm_capable = 1;
4255 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4256 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4257 if (xhci_check_usb2_port_capability(xhci, portnum,
4258 XHCI_BLC))
4259 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004260 }
4261
4262 return 0;
4263}
4264
Sarah Sharp3b3db022012-05-09 10:55:03 -07004265/*---------------------- USB 3.0 Link PM functions ------------------------*/
4266
Sarah Sharpe3567d22012-05-16 13:36:24 -07004267/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4268static unsigned long long xhci_service_interval_to_ns(
4269 struct usb_endpoint_descriptor *desc)
4270{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004271 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004272}
4273
Sarah Sharp3b3db022012-05-09 10:55:03 -07004274static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4275 enum usb3_link_state state)
4276{
4277 unsigned long long sel;
4278 unsigned long long pel;
4279 unsigned int max_sel_pel;
4280 char *state_name;
4281
4282 switch (state) {
4283 case USB3_LPM_U1:
4284 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4285 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4286 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4287 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4288 state_name = "U1";
4289 break;
4290 case USB3_LPM_U2:
4291 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4292 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4293 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4294 state_name = "U2";
4295 break;
4296 default:
4297 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4298 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004299 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004300 }
4301
4302 if (sel <= max_sel_pel && pel <= max_sel_pel)
4303 return USB3_LPM_DEVICE_INITIATED;
4304
4305 if (sel > max_sel_pel)
4306 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4307 "due to long SEL %llu ms\n",
4308 state_name, sel);
4309 else
4310 dev_dbg(&udev->dev, "Device-initiated %s disabled "
Joe Perches03e64e92013-07-16 19:25:59 -07004311 "due to long PEL %llu ms\n",
Sarah Sharp3b3db022012-05-09 10:55:03 -07004312 state_name, pel);
4313 return USB3_LPM_DISABLED;
4314}
4315
Pratyush Anand9502c462014-07-04 17:01:23 +03004316/* The U1 timeout should be the maximum of the following values:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004317 * - For control endpoints, U1 system exit latency (SEL) * 3
4318 * - For bulk endpoints, U1 SEL * 5
4319 * - For interrupt endpoints:
4320 * - Notification EPs, U1 SEL * 3
4321 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4322 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4323 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004324static unsigned long long xhci_calculate_intel_u1_timeout(
4325 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004326 struct usb_endpoint_descriptor *desc)
4327{
4328 unsigned long long timeout_ns;
4329 int ep_type;
4330 int intr_type;
4331
4332 ep_type = usb_endpoint_type(desc);
4333 switch (ep_type) {
4334 case USB_ENDPOINT_XFER_CONTROL:
4335 timeout_ns = udev->u1_params.sel * 3;
4336 break;
4337 case USB_ENDPOINT_XFER_BULK:
4338 timeout_ns = udev->u1_params.sel * 5;
4339 break;
4340 case USB_ENDPOINT_XFER_INT:
4341 intr_type = usb_endpoint_interrupt_type(desc);
4342 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4343 timeout_ns = udev->u1_params.sel * 3;
4344 break;
4345 }
4346 /* Otherwise the calculation is the same as isoc eps */
Gustavo A. R. Silva7d864992017-10-25 13:49:01 -05004347 /* fall through */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004348 case USB_ENDPOINT_XFER_ISOC:
4349 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004350 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004351 if (timeout_ns < udev->u1_params.sel * 2)
4352 timeout_ns = udev->u1_params.sel * 2;
4353 break;
4354 default:
4355 return 0;
4356 }
4357
Pratyush Anand9502c462014-07-04 17:01:23 +03004358 return timeout_ns;
4359}
4360
4361/* Returns the hub-encoded U1 timeout value. */
4362static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
4363 struct usb_device *udev,
4364 struct usb_endpoint_descriptor *desc)
4365{
4366 unsigned long long timeout_ns;
4367
4368 if (xhci->quirks & XHCI_INTEL_HOST)
4369 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
4370 else
4371 timeout_ns = udev->u1_params.sel;
4372
4373 /* The U1 timeout is encoded in 1us intervals.
4374 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4375 */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004376 if (timeout_ns == USB3_LPM_DISABLED)
Pratyush Anand9502c462014-07-04 17:01:23 +03004377 timeout_ns = 1;
4378 else
4379 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004380
4381 /* If the necessary timeout value is bigger than what we can set in the
4382 * USB 3.0 hub, we have to disable hub-initiated U1.
4383 */
4384 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4385 return timeout_ns;
4386 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4387 "due to long timeout %llu ms\n", timeout_ns);
4388 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4389}
4390
Pratyush Anand9502c462014-07-04 17:01:23 +03004391/* The U2 timeout should be the maximum of:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004392 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4393 * - largest bInterval of any active periodic endpoint (to avoid going
4394 * into lower power link states between intervals).
4395 * - the U2 Exit Latency of the device
4396 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004397static unsigned long long xhci_calculate_intel_u2_timeout(
4398 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004399 struct usb_endpoint_descriptor *desc)
4400{
4401 unsigned long long timeout_ns;
4402 unsigned long long u2_del_ns;
4403
4404 timeout_ns = 10 * 1000 * 1000;
4405
4406 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4407 (xhci_service_interval_to_ns(desc) > timeout_ns))
4408 timeout_ns = xhci_service_interval_to_ns(desc);
4409
Oliver Neukum966e7a82012-10-17 12:17:50 +02004410 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004411 if (u2_del_ns > timeout_ns)
4412 timeout_ns = u2_del_ns;
4413
Pratyush Anand9502c462014-07-04 17:01:23 +03004414 return timeout_ns;
4415}
4416
4417/* Returns the hub-encoded U2 timeout value. */
4418static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
4419 struct usb_device *udev,
4420 struct usb_endpoint_descriptor *desc)
4421{
4422 unsigned long long timeout_ns;
4423
4424 if (xhci->quirks & XHCI_INTEL_HOST)
4425 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
4426 else
4427 timeout_ns = udev->u2_params.sel;
4428
Sarah Sharpe3567d22012-05-16 13:36:24 -07004429 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004430 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004431 /* If the necessary timeout value is bigger than what we can set in the
4432 * USB 3.0 hub, we have to disable hub-initiated U2.
4433 */
4434 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4435 return timeout_ns;
4436 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4437 "due to long timeout %llu ms\n", timeout_ns);
4438 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4439}
4440
Sarah Sharp3b3db022012-05-09 10:55:03 -07004441static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4442 struct usb_device *udev,
4443 struct usb_endpoint_descriptor *desc,
4444 enum usb3_link_state state,
4445 u16 *timeout)
4446{
Pratyush Anand9502c462014-07-04 17:01:23 +03004447 if (state == USB3_LPM_U1)
4448 return xhci_calculate_u1_timeout(xhci, udev, desc);
4449 else if (state == USB3_LPM_U2)
4450 return xhci_calculate_u2_timeout(xhci, udev, desc);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004451
Sarah Sharp3b3db022012-05-09 10:55:03 -07004452 return USB3_LPM_DISABLED;
4453}
4454
4455static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4456 struct usb_device *udev,
4457 struct usb_endpoint_descriptor *desc,
4458 enum usb3_link_state state,
4459 u16 *timeout)
4460{
4461 u16 alt_timeout;
4462
4463 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4464 desc, state, timeout);
4465
4466 /* If we found we can't enable hub-initiated LPM, or
4467 * the U1 or U2 exit latency was too high to allow
4468 * device-initiated LPM as well, just stop searching.
4469 */
4470 if (alt_timeout == USB3_LPM_DISABLED ||
4471 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4472 *timeout = alt_timeout;
4473 return -E2BIG;
4474 }
4475 if (alt_timeout > *timeout)
4476 *timeout = alt_timeout;
4477 return 0;
4478}
4479
4480static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4481 struct usb_device *udev,
4482 struct usb_host_interface *alt,
4483 enum usb3_link_state state,
4484 u16 *timeout)
4485{
4486 int j;
4487
4488 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4489 if (xhci_update_timeout_for_endpoint(xhci, udev,
4490 &alt->endpoint[j].desc, state, timeout))
4491 return -E2BIG;
4492 continue;
4493 }
4494 return 0;
4495}
4496
Sarah Sharpe3567d22012-05-16 13:36:24 -07004497static int xhci_check_intel_tier_policy(struct usb_device *udev,
4498 enum usb3_link_state state)
4499{
4500 struct usb_device *parent;
4501 unsigned int num_hubs;
4502
4503 if (state == USB3_LPM_U2)
4504 return 0;
4505
4506 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4507 for (parent = udev->parent, num_hubs = 0; parent->parent;
4508 parent = parent->parent)
4509 num_hubs++;
4510
4511 if (num_hubs < 2)
4512 return 0;
4513
4514 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4515 " below second-tier hub.\n");
4516 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4517 "to decrease power consumption.\n");
4518 return -E2BIG;
4519}
4520
Sarah Sharp3b3db022012-05-09 10:55:03 -07004521static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4522 struct usb_device *udev,
4523 enum usb3_link_state state)
4524{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004525 if (xhci->quirks & XHCI_INTEL_HOST)
4526 return xhci_check_intel_tier_policy(udev, state);
Pratyush Anand9502c462014-07-04 17:01:23 +03004527 else
4528 return 0;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004529}
4530
4531/* Returns the U1 or U2 timeout that should be enabled.
4532 * If the tier check or timeout setting functions return with a non-zero exit
4533 * code, that means the timeout value has been finalized and we shouldn't look
4534 * at any more endpoints.
4535 */
4536static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4537 struct usb_device *udev, enum usb3_link_state state)
4538{
4539 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4540 struct usb_host_config *config;
4541 char *state_name;
4542 int i;
4543 u16 timeout = USB3_LPM_DISABLED;
4544
4545 if (state == USB3_LPM_U1)
4546 state_name = "U1";
4547 else if (state == USB3_LPM_U2)
4548 state_name = "U2";
4549 else {
4550 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4551 state);
4552 return timeout;
4553 }
4554
4555 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4556 return timeout;
4557
4558 /* Gather some information about the currently installed configuration
4559 * and alternate interface settings.
4560 */
4561 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4562 state, &timeout))
4563 return timeout;
4564
4565 config = udev->actconfig;
4566 if (!config)
4567 return timeout;
4568
Xenia Ragiadakou64ba4192013-08-26 23:29:46 +03004569 for (i = 0; i < config->desc.bNumInterfaces; i++) {
Sarah Sharp3b3db022012-05-09 10:55:03 -07004570 struct usb_driver *driver;
4571 struct usb_interface *intf = config->interface[i];
4572
4573 if (!intf)
4574 continue;
4575
4576 /* Check if any currently bound drivers want hub-initiated LPM
4577 * disabled.
4578 */
4579 if (intf->dev.driver) {
4580 driver = to_usb_driver(intf->dev.driver);
4581 if (driver && driver->disable_hub_initiated_lpm) {
4582 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4583 "at request of driver %s\n",
4584 state_name, driver->name);
4585 return xhci_get_timeout_no_hub_lpm(udev, state);
4586 }
4587 }
4588
4589 /* Not sure how this could happen... */
4590 if (!intf->cur_altsetting)
4591 continue;
4592
4593 if (xhci_update_timeout_for_interface(xhci, udev,
4594 intf->cur_altsetting,
4595 state, &timeout))
4596 return timeout;
4597 }
4598 return timeout;
4599}
4600
Sarah Sharp3b3db022012-05-09 10:55:03 -07004601static int calculate_max_exit_latency(struct usb_device *udev,
4602 enum usb3_link_state state_changed,
4603 u16 hub_encoded_timeout)
4604{
4605 unsigned long long u1_mel_us = 0;
4606 unsigned long long u2_mel_us = 0;
4607 unsigned long long mel_us = 0;
4608 bool disabling_u1;
4609 bool disabling_u2;
4610 bool enabling_u1;
4611 bool enabling_u2;
4612
4613 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4614 hub_encoded_timeout == USB3_LPM_DISABLED);
4615 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4616 hub_encoded_timeout == USB3_LPM_DISABLED);
4617
4618 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4619 hub_encoded_timeout != USB3_LPM_DISABLED);
4620 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4621 hub_encoded_timeout != USB3_LPM_DISABLED);
4622
4623 /* If U1 was already enabled and we're not disabling it,
4624 * or we're going to enable U1, account for the U1 max exit latency.
4625 */
4626 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4627 enabling_u1)
4628 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4629 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4630 enabling_u2)
4631 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4632
4633 if (u1_mel_us > u2_mel_us)
4634 mel_us = u1_mel_us;
4635 else
4636 mel_us = u2_mel_us;
4637 /* xHCI host controller max exit latency field is only 16 bits wide. */
4638 if (mel_us > MAX_EXIT) {
4639 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4640 "is too big.\n", mel_us);
4641 return -E2BIG;
4642 }
4643 return mel_us;
4644}
4645
4646/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
Lu Baolu39693842017-04-07 17:57:04 +03004647static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharp3b3db022012-05-09 10:55:03 -07004648 struct usb_device *udev, enum usb3_link_state state)
4649{
4650 struct xhci_hcd *xhci;
4651 u16 hub_encoded_timeout;
4652 int mel;
4653 int ret;
4654
4655 xhci = hcd_to_xhci(hcd);
4656 /* The LPM timeout values are pretty host-controller specific, so don't
4657 * enable hub-initiated timeouts unless the vendor has provided
4658 * information about their timeout algorithm.
4659 */
4660 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4661 !xhci->devs[udev->slot_id])
4662 return USB3_LPM_DISABLED;
4663
4664 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4665 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4666 if (mel < 0) {
4667 /* Max Exit Latency is too big, disable LPM. */
4668 hub_encoded_timeout = USB3_LPM_DISABLED;
4669 mel = 0;
4670 }
4671
4672 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4673 if (ret)
4674 return ret;
4675 return hub_encoded_timeout;
4676}
4677
Lu Baolu39693842017-04-07 17:57:04 +03004678static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharp3b3db022012-05-09 10:55:03 -07004679 struct usb_device *udev, enum usb3_link_state state)
4680{
4681 struct xhci_hcd *xhci;
4682 u16 mel;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004683
4684 xhci = hcd_to_xhci(hcd);
4685 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4686 !xhci->devs[udev->slot_id])
4687 return 0;
4688
4689 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
Saurabh Karajgaonkarf1cda542015-08-04 14:04:09 +00004690 return xhci_change_max_exit_latency(xhci, udev, mel);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004691}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004692#else /* CONFIG_PM */
4693
Lu Baolu39693842017-04-07 17:57:04 +03004694static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004695 struct usb_device *udev, int enable)
4696{
4697 return 0;
4698}
4699
Lu Baolu39693842017-04-07 17:57:04 +03004700static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004701{
4702 return 0;
4703}
4704
Lu Baolu39693842017-04-07 17:57:04 +03004705static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004706 struct usb_device *udev, enum usb3_link_state state)
4707{
4708 return USB3_LPM_DISABLED;
4709}
4710
Lu Baolu39693842017-04-07 17:57:04 +03004711static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004712 struct usb_device *udev, enum usb3_link_state state)
4713{
4714 return 0;
4715}
4716#endif /* CONFIG_PM */
4717
Sarah Sharp3b3db022012-05-09 10:55:03 -07004718/*-------------------------------------------------------------------------*/
4719
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004720/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4721 * internal data structures for the device.
4722 */
Lu Baolu39693842017-04-07 17:57:04 +03004723static int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004724 struct usb_tt *tt, gfp_t mem_flags)
4725{
4726 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4727 struct xhci_virt_device *vdev;
4728 struct xhci_command *config_cmd;
4729 struct xhci_input_control_ctx *ctrl_ctx;
4730 struct xhci_slot_ctx *slot_ctx;
4731 unsigned long flags;
4732 unsigned think_time;
4733 int ret;
4734
4735 /* Ignore root hubs */
4736 if (!hdev->parent)
4737 return 0;
4738
4739 vdev = xhci->devs[hdev->slot_id];
4740 if (!vdev) {
4741 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4742 return -EINVAL;
4743 }
Lu Baolu74e0b562017-04-07 17:57:05 +03004744
Mathias Nyman14d49b72017-12-08 17:59:07 +02004745 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
Lu Baolu74e0b562017-04-07 17:57:05 +03004746 if (!config_cmd)
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004747 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +03004748
Lin Wang4daf9df2015-01-09 16:06:31 +02004749 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004750 if (!ctrl_ctx) {
4751 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4752 __func__);
4753 xhci_free_command(xhci, config_cmd);
4754 return -ENOMEM;
4755 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004756
4757 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004758 if (hdev->speed == USB_SPEED_HIGH &&
4759 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4760 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4761 xhci_free_command(xhci, config_cmd);
4762 spin_unlock_irqrestore(&xhci->lock, flags);
4763 return -ENOMEM;
4764 }
4765
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004766 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004767 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004768 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004769 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004770 /*
4771 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4772 * but it may be already set to 1 when setup an xHCI virtual
4773 * device, so clear it anyway.
4774 */
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004775 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004776 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004777 else if (hdev->speed == USB_SPEED_FULL)
4778 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
4779
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004780 if (xhci->hci_version > 0x95) {
4781 xhci_dbg(xhci, "xHCI version %x needs hub "
4782 "TT think time and number of ports\n",
4783 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004784 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004785 /* Set TT think time - convert from ns to FS bit times.
4786 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4787 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004788 *
4789 * xHCI 1.0: this field shall be 0 if the device is not a
4790 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004791 */
4792 think_time = tt->think_time;
4793 if (think_time != 0)
4794 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004795 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4796 slot_ctx->tt_info |=
4797 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004798 } else {
4799 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4800 "TT think time or number of ports\n",
4801 (unsigned int) xhci->hci_version);
4802 }
4803 slot_ctx->dev_state = 0;
4804 spin_unlock_irqrestore(&xhci->lock, flags);
4805
4806 xhci_dbg(xhci, "Set up %s for hub device.\n",
4807 (xhci->hci_version > 0x95) ?
4808 "configure endpoint" : "evaluate context");
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004809
4810 /* Issue and wait for the configure endpoint or
4811 * evaluate context command.
4812 */
4813 if (xhci->hci_version > 0x95)
4814 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4815 false, false);
4816 else
4817 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4818 true, false);
4819
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004820 xhci_free_command(xhci, config_cmd);
4821 return ret;
4822}
4823
Lu Baolu39693842017-04-07 17:57:04 +03004824static int xhci_get_frame(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004825{
4826 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4827 /* EHCI mods by the periodic size. Why? */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004828 return readl(&xhci->run_regs->microframe_index) >> 3;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004829}
4830
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004831int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4832{
4833 struct xhci_hcd *xhci;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +08004834 /*
4835 * TODO: Check with DWC3 clients for sysdev according to
4836 * quirks
4837 */
4838 struct device *dev = hcd->self.sysdev;
Mathias Nyman0ee78c12018-03-16 16:33:06 +02004839 unsigned int minor_rev;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004840 int retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004841
Sarah Sharp1386ff72014-01-31 11:45:02 -08004842 /* Accept arbitrarily long scatter-gather lists */
4843 hcd->self.sg_tablesize = ~0;
Ming Leifc760512013-08-08 21:48:23 +08004844
Mathias Nymane2ed5112014-03-07 17:06:57 +02004845 /* support to build packet from discontinuous buffers */
4846 hcd->self.no_sg_constraint = 1;
4847
Hans de Goede19181bc2012-07-04 09:18:02 +02004848 /* XHCI controllers don't stop the ep queue on short packets :| */
4849 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004850
Mathias Nymanb50107b2015-10-01 18:40:38 +03004851 xhci = hcd_to_xhci(hcd);
4852
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004853 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004854 xhci->main_hcd = hcd;
Mathias Nyman9ea95ec2018-05-21 16:39:53 +03004855 xhci->usb2_rhub.hcd = hcd;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004856 /* Mark the first roothub as being USB 2.0.
4857 * The xHCI driver will register the USB 3.0 roothub.
4858 */
4859 hcd->speed = HCD_USB2;
4860 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4861 /*
4862 * USB 2.0 roothub under xHCI has an integrated TT,
4863 * (rate matching hub) as opposed to having an OHCI/UHCI
4864 * companion controller.
4865 */
4866 hcd->has_tt = 1;
4867 } else {
Mathias Nyman0ee78c12018-03-16 16:33:06 +02004868 /*
4869 * Some 3.1 hosts return sbrn 0x30, use xhci supported protocol
4870 * minor revision instead of sbrn
4871 */
4872 minor_rev = xhci->usb3_rhub.min_rev;
4873 if (minor_rev) {
Mathias Nymanb50107b2015-10-01 18:40:38 +03004874 hcd->speed = HCD_USB31;
Mathias Nyman2c0e06f2016-01-25 15:30:45 +02004875 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
Mathias Nymanb50107b2015-10-01 18:40:38 +03004876 }
Mathias Nyman0ee78c12018-03-16 16:33:06 +02004877 xhci_info(xhci, "Host supports USB 3.%x %s SuperSpeed\n",
4878 minor_rev,
4879 minor_rev ? "Enhanced" : "");
4880
Mathias Nyman9ea95ec2018-05-21 16:39:53 +03004881 xhci->usb3_rhub.hcd = hcd;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004882 /* xHCI private pointer was set in xhci_pci_probe for the second
4883 * registered roothub.
4884 */
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004885 return 0;
4886 }
4887
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004888 mutex_init(&xhci->mutex);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004889 xhci->cap_regs = hcd->regs;
4890 xhci->op_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004891 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004892 xhci->run_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004893 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004894 /* Cache read-only capability registers */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004895 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
4896 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
4897 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
4898 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004899 xhci->hci_version = HC_VERSION(xhci->hcc_params);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004900 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
Lu Baolu04abb6d2015-10-01 18:40:31 +03004901 if (xhci->hci_version > 0x100)
4902 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004903
Mathias Nyman757de492016-06-01 18:09:10 +03004904 xhci->quirks |= quirks;
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +01004905
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004906 get_quirks(dev, xhci);
4907
George Cherian07f3cb72013-07-01 10:59:12 +05304908 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4909 * success event after a short transfer. This quirk will ignore such
4910 * spurious event.
4911 */
4912 if (xhci->hci_version > 0x96)
4913 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4914
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004915 /* Make sure the HC is halted. */
4916 retval = xhci_halt(xhci);
4917 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004918 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004919
4920 xhci_dbg(xhci, "Resetting HCD\n");
4921 /* Reset the internal HC memory state and registers. */
4922 retval = xhci_reset(xhci);
4923 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004924 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004925 xhci_dbg(xhci, "Reset complete\n");
4926
Yoshihiro Shimoda0a380be2016-04-08 16:25:07 +03004927 /*
4928 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
4929 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
4930 * address memory pointers actually. So, this driver clears the AC64
4931 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
4932 * DMA_BIT_MASK(32)) in this xhci_gen_setup().
4933 */
4934 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
4935 xhci->hcc_params &= ~BIT(0);
4936
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004937 /* Set dma_mask and coherent_dma_mask to 64-bits,
4938 * if xHC supports 64-bit addressing */
4939 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4940 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004941 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004942 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
Duc Dangfda182d2015-10-09 13:30:13 +03004943 } else {
4944 /*
4945 * This is to avoid error in cases where a 32-bit USB
4946 * controller is used on a 64-bit capable system.
4947 */
4948 retval = dma_set_mask(dev, DMA_BIT_MASK(32));
4949 if (retval)
4950 return retval;
4951 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
4952 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004953 }
4954
4955 xhci_dbg(xhci, "Calling HCD init\n");
4956 /* Initialize HCD and host controller data structures. */
4957 retval = xhci_init(hcd);
4958 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004959 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004960 xhci_dbg(xhci, "Called HCD init\n");
Hans de Goede99705092015-01-16 17:54:01 +02004961
Marc Zyngier36b68572018-05-23 18:41:36 +01004962 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n",
Hans de Goede99705092015-01-16 17:54:01 +02004963 xhci->hcc_params, xhci->hci_version, xhci->quirks);
4964
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004965 return 0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004966}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03004967EXPORT_SYMBOL_GPL(xhci_gen_setup);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004968
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004969static const struct hc_driver xhci_hc_driver = {
4970 .description = "xhci-hcd",
4971 .product_desc = "xHCI Host Controller",
Yoshihiro Shimoda32479d42015-11-24 13:09:47 +02004972 .hcd_priv_size = sizeof(struct xhci_hcd),
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03004973
4974 /*
4975 * generic hardware linkage
4976 */
4977 .irq = xhci_irq,
4978 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
4979
4980 /*
4981 * basic lifecycle operations
4982 */
4983 .reset = NULL, /* set in xhci_init_driver() */
4984 .start = xhci_run,
4985 .stop = xhci_stop,
4986 .shutdown = xhci_shutdown,
4987
4988 /*
4989 * managing i/o requests and associated device resources
4990 */
4991 .urb_enqueue = xhci_urb_enqueue,
4992 .urb_dequeue = xhci_urb_dequeue,
4993 .alloc_dev = xhci_alloc_dev,
4994 .free_dev = xhci_free_dev,
4995 .alloc_streams = xhci_alloc_streams,
4996 .free_streams = xhci_free_streams,
4997 .add_endpoint = xhci_add_endpoint,
4998 .drop_endpoint = xhci_drop_endpoint,
4999 .endpoint_reset = xhci_endpoint_reset,
5000 .check_bandwidth = xhci_check_bandwidth,
5001 .reset_bandwidth = xhci_reset_bandwidth,
5002 .address_device = xhci_address_device,
5003 .enable_device = xhci_enable_device,
5004 .update_hub_device = xhci_update_hub_device,
5005 .reset_device = xhci_discover_or_reset_device,
5006
5007 /*
5008 * scheduling support
5009 */
5010 .get_frame_number = xhci_get_frame,
5011
5012 /*
5013 * root hub support
5014 */
5015 .hub_control = xhci_hub_control,
5016 .hub_status_data = xhci_hub_status_data,
5017 .bus_suspend = xhci_bus_suspend,
5018 .bus_resume = xhci_bus_resume,
5019
5020 /*
5021 * call back when device connected and addressed
5022 */
5023 .update_device = xhci_update_device,
5024 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
5025 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
5026 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
5027 .find_raw_port_number = xhci_find_raw_port_number,
5028};
5029
Roger Quadroscd33a322015-05-29 17:01:46 +03005030void xhci_init_driver(struct hc_driver *drv,
5031 const struct xhci_driver_overrides *over)
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005032{
Roger Quadroscd33a322015-05-29 17:01:46 +03005033 BUG_ON(!over);
5034
5035 /* Copy the generic table to drv then apply the overrides */
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005036 *drv = xhci_hc_driver;
Roger Quadroscd33a322015-05-29 17:01:46 +03005037
5038 if (over) {
5039 drv->hcd_priv_size += over->extra_priv_size;
5040 if (over->reset)
5041 drv->reset = over->reset;
5042 if (over->start)
5043 drv->start = over->start;
5044 }
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005045}
5046EXPORT_SYMBOL_GPL(xhci_init_driver);
5047
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005048MODULE_DESCRIPTION(DRIVER_DESC);
5049MODULE_AUTHOR(DRIVER_AUTHOR);
5050MODULE_LICENSE("GPL");
5051
5052static int __init xhci_hcd_init(void)
5053{
Sarah Sharp98441972009-05-14 11:44:18 -07005054 /*
5055 * Check the compiler generated sizes of structures that must be laid
5056 * out in specific ways for hardware access.
5057 */
5058 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
5059 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
5060 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
5061 /* xhci_device_control has eight fields, and also
5062 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5063 */
Sarah Sharp98441972009-05-14 11:44:18 -07005064 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
5065 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
5066 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
Lu Baolu04abb6d2015-10-01 18:40:31 +03005067 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
Sarah Sharp98441972009-05-14 11:44:18 -07005068 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
5069 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5070 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Oliver Neukum1eaf35e2015-12-03 15:03:34 +01005071
5072 if (usb_disabled())
5073 return -ENODEV;
5074
Lu Baolu02b6fdc2017-10-05 11:21:39 +03005075 xhci_debugfs_create_root();
5076
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005077 return 0;
5078}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005079
5080/*
5081 * If an init function is provided, an exit function must also be provided
5082 * to allow module unload.
5083 */
Lu Baolu02b6fdc2017-10-05 11:21:39 +03005084static void __exit xhci_hcd_fini(void)
5085{
5086 xhci_debugfs_remove_root();
5087}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005088
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005089module_init(xhci_hcd_init);
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005090module_exit(xhci_hcd_fini);